| |
Unix Programming - Studying Cases - Case Study: SNG
Case Study: SNG
The program sng translates between
PNG format and an
all-text representation of it (SNG or Scriptable Network Graphics
format) that can be examined and modified with an ordinary text
editor. Run on a PNG file, it produces an SNG file; run on an SNG
file, it recovers the equivalent PNG. The transformation is 100%
faithful and lossless in both directions.
In syntactic style, SNG resembles CSS (Cascading Style Sheets),
another language for controlling presentation of graphics; this makes
at least a gesture in the direction of the Rule of Least
Surprise. Here is a test example:
Example6.2.An SNG Example.
#SNG: This is a synthetic SNG test file
# Our first test is a paletted (type 3) image.
IHDR: {
width: 16;
height: 19;
bitdepth: 8;
using color: palette;
with interlace;
}
# Sample bit depth chunk
sBIT: {
red: 8;
green: 8;
blue: 8;
}
# An example palette: three colors, one of which
# we will render transparent
PLTE: {
(0, 0, 255)
(255, 0, 0)
"dark slate gray",
}
# Suggested palette
sPLT {
name: "A random suggested palette";
depth: 8;
(0, 0, 255), 255, 7;
(255, 0, 0), 255, 5;
( 70, 70, 70), 255, 3;
}
# The viewer will actually use this...
IMAGE: {
pixels base64
2222222222222222
2222222222222222
0000001111100000
0000011111110000
0000111001111000
0001110000111100
0001110000111100
0000110001111000
0000000011110000
0000000111100000
0000001111000000
0000001111000000
0000000000000000
0000000110000000
0000001111000000
0000001111000000
0000000110000000
2222222222222222
2222222222222222
}
tEXt: { # Ordinary text chunk
keyword: "Title";
text: "Sample SNG script";
}
# Test file ends here
The point of this tool is to enable users to edit various
obscure PNG chunk types
that are not necessarily supported by conventional graphics editors.
Rather than writing special-purpose code to grovel through the PNG
binary format, the user can simply flip an image into an all-text
representation, edit that, and massage it back. Another potential
application is in making images amenable to version control; under
most version-control systems, text files are much easier to manage
than binary blobs, and diff operations on SNG representations actually
have some possibility of yielding useful information.
The gains here go beyond the time not spent writing
special-purpose code for manipulating binary PNGs, however. The code
of the sng program itself is not especially
transparent, but it promotes transparency in larger systems of
programs by making the entire contents of PNGs
discoverable.
[an error occurred while processing this directive]
|
|