1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
implement WImagefile;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Chans, Display, Image, Rect: import draw;
include "bufio.m";
bufio: Bufio;
Iobuf: import bufio;
include "imagefile.m";
Nhash: con 4001;
Entry: adt
{
index: int;
prefix: int;
exten: int;
next: cyclic ref Entry;
};
IO: adt
{
fd: ref Iobuf;
buf: array of byte;
i: int;
nbits: int; # bits in right side of shift register
sreg: int; # shift register
};
tbl: array of ref Entry;
colormap: array of array of byte;
log2 := array[] of {1 => 0, 2 => 1, 4 => 2, 8 => 3, * => -1};
init(iomod: Bufio)
{
if(sys == nil){
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
}
bufio = iomod;
}
writeimage(fd: ref Iobuf, image: ref Image): string
{
case image.chans.desc {
(Draw->GREY1).desc or (Draw->GREY2).desc or
(Draw->GREY4).desc or (Draw->GREY8).desc or
(Draw->CMAP8).desc =>
if(image.depth > 8 || (image.depth&(image.depth-1)) != 0)
return "inconsistent depth";
* =>
return "unsupported channel type";
}
inittbl();
writeheader(fd, image);
writedescriptor(fd, image);
err := writedata(fd, image);
if(err != nil)
return err;
writetrailer(fd);
fd.flush();
return err;
}
inittbl()
{
tbl = array[4096] of ref Entry;
for(i:=0; i<len tbl; i++)
tbl[i] = ref Entry(i, -1, i, nil);
}
# Write header, logical screen descriptor, and color map
writeheader(fd: ref Iobuf, image: ref Image): string
{
# Header
fd.puts("GIF89a");
# Logical Screen Descriptor
put2(fd, image.r.dx());
put2(fd, image.r.dy());
# color table present, 4 bits per color (for RGBV best case), size of color map
fd.putb(byte ((1<<7)|(3<<4)|(image.depth-1)));
fd.putb(byte 0); # white background (doesn't matter anyway)
fd.putb(byte 0); # pixel aspect ratio - unused
# Global Color Table
getcolormap(image);
ldepth := log2[image.depth];
if(image.chans.eq(Draw->GREY8))
ldepth = 4;
fd.write(colormap[ldepth], len colormap[ldepth]);
return nil;
}
# Write image descriptor
writedescriptor(fd: ref Iobuf, image: ref Image)
{
# Image Separator
fd.putb(byte 16r2C);
# Left, top, width, height
put2(fd, 0);
put2(fd, 0);
put2(fd, image.r.dx());
put2(fd, image.r.dy());
# no special processing
fd.putb(byte 0);
}
# Write data
writedata(fd: ref Iobuf, image: ref Image): string
{
# LZW Minimum code size
if(image.depth == 1)
fd.putb(byte 2);
else
fd.putb(byte image.depth);
# Encode and emit the data
err := encode(fd, image);
if(err != nil)
return err;
# Block Terminator
fd.putb(byte 0);
return nil;
}
# Write data
writetrailer(fd: ref Iobuf)
{
fd.putb(byte 16r3B);
}
# Write little-endian 16-bit integer
put2(fd: ref Iobuf, i: int)
{
fd.putb(byte i);
fd.putb(byte (i>>8));
}
# Get color map for all ldepths, in format suitable for writing out
getcolormap(image: ref Draw->Image)
{
if(colormap != nil)
return;
colormap = array[5] of array of byte;
display := image.display;
colormap[4] = array[3*256] of byte;
colormap[3] = array[3*256] of byte;
colormap[2] = array[3*16] of byte;
colormap[1] = array[3*4] of byte;
colormap[0] = array[3*2] of byte;
c := colormap[4];
for(i:=0; i<256; i++){
c[3*i+0] = byte i;
c[3*i+1] = byte i;
c[3*i+2] = byte i;
}
c = colormap[3];
for(i=0; i<256; i++){
(r, g, b) := display.cmap2rgb(i);
c[3*i+0] = byte r;
c[3*i+1] = byte g;
c[3*i+2] = byte b;
}
c = colormap[2];
for(i=0; i<16; i++){
col := (i<<4)|i;
(r, g, b) := display.cmap2rgb(col);
c[3*i+0] = byte r;
c[3*i+1] = byte g;
c[3*i+2] = byte b;
}
c = colormap[1];
for(i=0; i<4; i++){
col := (i<<6)|(i<<4)|(i<<2)|i;
(r, g, b) := display.cmap2rgb(col);
c[3*i+0] = byte r;
c[3*i+1] = byte g;
c[3*i+2] = byte b;
}
c = colormap[0];
for(i=0; i<2; i++){
if(i == 0)
col := 0;
else
col = 16rFF;
(r, g, b) := display.cmap2rgb(col);
c[3*i+0] = byte r;
c[3*i+1] = byte g;
c[3*i+2] = byte b;
}
}
# Put n bits of c into output at io.buf[i];
output(io: ref IO, c, n: int)
{
if(c < 0){
if(io.nbits != 0)
io.buf[io.i++] = byte io.sreg;
io.fd.putb(byte io.i);
io.fd.write(io.buf, io.i);
io.nbits = 0;
return;
}
if(io.nbits+n >= 31){
sys->print("panic: WriteGIF sr overflow\n");
exit;
}
io.sreg |= c<<io.nbits;
io.nbits += n;
while(io.nbits >= 8){
io.buf[io.i++] = byte io.sreg;
io.sreg >>= 8;
io.nbits -= 8;
}
if(io.i >= 255){
io.fd.putb(byte 255);
io.fd.write(io.buf, 255);
io.buf[0:] = io.buf[255:io.i];
io.i -= 255;
}
}
# LZW encoder
encode(fd: ref Iobuf, image: ref Image): string
{
c, h, csize, prefix: int;
e, oe: ref Entry;
first := 1;
ld := log2[image.depth];
# ldepth 0 must generate codesize 2 with values 0 and 1 (see the spec.)
ld0 := ld;
if(ld0 == 0)
ld0 = 1;
codesize := (1<<ld0);
CTM := 1<<codesize;
EOD := CTM+1;
io := ref IO (fd, array[300] of byte, 0, 0, 0);
sreg := 0;
nbits := 0;
bitsperpixel := 1<<ld;
pm := (1<<bitsperpixel)-1;
# Read image data into memory
# potentially one extra byte on each end of each scan line
data := array[image.r.dy()*(2+(image.r.dx()>>(3-log2[image.depth])))] of byte;
ndata := image.readpixels(image.r, data);
if(ndata < 0)
return sys->sprint("WriteGIF: readpixels: %r");
datai := 0;
x := image.r.min.x;
Init:
for(;;){
csize = codesize+1;
nentry := EOD+1;
maxentry := (1<<csize);
hash := array[Nhash] of ref Entry;
for(i := 0; i<nentry; i++){
e = tbl[i];
h = (e.prefix<<24) | (e.exten<<8);
h %= Nhash;
if(h < 0)
h += Nhash;
e.next = hash[h];
hash[h] = e;
}
prefix = -1;
if(first)
output(io, CTM, csize);
first = 0;
# Scan over pixels. Because of partially filled bytes on ends of scan lines,
# which must be ignored in the data stream passed to GIF, this is more
# complex than we'd like
Next:
for(;;){
if(ld != 3){
# beginning of scan line is difficult; prime the shift register
if(x == image.r.min.x){
if(datai == ndata)
break;
sreg = int data[datai++];
nbits = 8-((x&(7>>ld))<<ld);
}
x++;
if(x == image.r.max.x)
x = image.r.min.x;
}
if(nbits == 0){
if(datai == ndata)
break;
sreg = int data[datai++];
nbits = 8;
}
nbits -= bitsperpixel;
c = sreg>>nbits & pm;
h = prefix<<24 | c<<8;
h %= Nhash;
if(h < 0)
h += Nhash;
oe = nil;
for(e = hash[h]; e!=nil; e=e.next){
if(e.prefix == prefix && e.exten == c){
if(oe != nil){
oe.next = e.next;
e.next = hash[h];
hash[h] = e;
}
prefix = e.index;
continue Next;
}
oe = e;
}
output(io, prefix, csize);
early:=0; # peculiar tiff feature here for reference
if(nentry == maxentry-early){
if(csize == 12){
nbits += codesize; # unget pixel
x--;
output(io, CTM, csize);
continue Init;
}
csize++;
maxentry = (1<<csize);
}
e = tbl[nentry];
e.prefix = prefix;
e.exten = c;
e.next = hash[h];
hash[h] = e;
prefix = c;
nentry++;
}
break Init;
}
output(io, prefix, csize);
output(io, EOD, csize);
output(io, -1, csize);
return nil;
}
|