Debian Codegraph @

libs/pplib/pplib-src/src/util/utilfpred.c

raw
1 /* predictor filters; common for flate and lzw */
2
3 #include "utilmem.h"
4 #include "utillog.h"
5 #include "utilfpred.h"
6
7 /*
8 Here we implement predictor filters used with flate and lzw compressions in PDF streams. The main idea of data prediction
9 is to compute and output the differences between data records instead of those records. Adjacent pixels in images are usually
10 similar, so differences between pixel values tends to be zero. And both Flate and LZW performs better when the input
11 is rather smooth. Although a preliminary use of predictors is related to bitmap data, The actual need for predictor filter
12 came from the fact that xref streams may also be predicted (usually with PNG up-predictor).
13
14 PDF specification allows to use several predictor algorithms, specified by /Predictor key in /DecodeParms dictionary:
15
16 1 - no predictor (default)
17 2 - TIFF horizontal predictor
18 10 - PNG none predictor
19 11 - PNG sub predictor
20 12 - PNG up predictor
21 13 - PNG average predictor
22 14 - PNG paeth predictor
23
24 All PNG predictors works on bytes, regardless the image color-depth. While encoding, every input data byte is decreased
25 by the appropriate byte of the previous pixel. Even if the pixel does not fit a full byte, PNG predictors use an artificial
26 pixel size rounded up to a full byte. PNG predictors utilizes previous (left) pixel, pixel above and previous to above
27 pixel. In case of PNG, the type of the predictor is written on a dedicated byte at the beginning of every scanline. It
28 means all predictor functions must maintain and information about left, above and left-above pixels.
29
30 Despite the same differencing idea, TIFF predictors are different. The prediction process bases on pixel components,
31 which are not necessarily bytes (component of a pixel is added/substracted from a relevant component of a previous
32 pixel). In TIFF predictor 2, only the previous (the left) pixel is taken into account, there is no need to keep
33 an information about other surrounding pixels. Also there is no expicit algorithm marker in data; the same prediction
34 method is applied to all input rows.
35
36 Not surprisingly, predictor encoders and decoders are pretty similar. Encoders take some input value and the previous
37 input value (or 0 at the beginning of the scanline) and output a difference between them. Decoders takes an input value,
38 previously decoded value (or zero) and outputs their sum. When encoding, the result is cast to the proper unsigned integer,
39 when decoding, modulo 256 (or appropriate) is used, which makes encoding and decoding looseless.
40
41 Some extra bits trickery is involved in TIFF predictor function, when components don't fit bytes boundary. In that case,
42 an input is treated as a bits stream. Every input byte is "buffered" in a larger integer, as its lower bits (from right).
43 Every output value is taken from its higher (left) bits. In a special case of bits-per-component equal 1, we buffer all
44 pixel bits and use XOR to compute bits difference between pixels. I've excerpted that trick from poppler, but I'm not
45 really sure if it works any better, especially when the number of components per pixel is 1. In that case we do a hard
46 bit-by-bit work anyway.
47
48 In PNG prediction, we record every pixel byte (in decoded form) in state->rowsave. At the end of a scanline
49 we copy state->rowsave to state->rowup, so that in the next scanline we can access up-pixel byte.
50 Left pixel byte is accessed as state->rowsave (the byte recently stored or virtual left edge byte \0).
51 Up-left pixel byte is accessed via state->rowup, but with state->pixelsize offset (same as left byte, possibly \0
52 at the left edge of the row). Both state->rowup and state->rowsave has a safe span of pixelsize bytes on the left,
53 that are permanently \0.
54 */
55
56 #define predictor_component_t uint16_t
57 #define predictor_pixel1b_t uint32_t
58
59 #define MAX_COMPONENTS 8
60
61 struct predictor_state {
62 int default_predictor; /* default predictor indicator */
63 int current_predictor; /* current predictor, possibly taken from algorithm marker in PNG data */
64 int rowsamples; /* number of pixels in a scanline (/DecodeParms << /Columns ... >>) */
65 int compbits; /* number of bits per component (/DecodeParms << /BitsPerComponent ... >>) */
66 int components; /* number of components (/DecodeParms << /Colors ... >>) */
67 uint8_t *buffer; /* temporary private buffer area */
68 uint8_t *rowin; /* an input row buffer position */
69 int rowsize; /* size of a current scanline in bytes (rounded up) */
70 int rowend; /* an input buffer end position */
71 int rowindex; /* an output buffer position */
72 union {
73 struct { /* used by PNG predictor codecs */
74 uint8_t *rowup, *rowsave; /* previous scanline buffers */
75 int predictorbyte; /* flag indicating that algorithm byte is read/written */
76 int pixelsize; /* number of bytes per pixel (rounded up) */
77 };
78 struct { /* used by TIFF predictor codecs */
79 predictor_component_t compbuffer[MAX_COMPONENTS];
80 union {
81 predictor_component_t *prevcomp; /* an array of left pixel components, typically eq ->compbuffer */
82 predictor_pixel1b_t *prevpixel; /* left pixel value stored on a single integer (for 1bit color-depth) */
83 };
84 int compin, compout; /* bit stream buffers */
85 int bitsin, bitsout; /* bit stream counters */
86 int sampleindex; /* pixel counter */
87 int compindex; /* component counter */
88 int pixbufsize; /* size of pixel buffer in bytes */
89 };
90 };
91 int flush;
92 int status;
93 };
94
95 typedef union { predictor_state *predictorstate; void *voidstate; } predictor_state_pointer; // to avoid 'dereferencing type-puned ...' warnings
96
97 enum {
98 STATUS_LAST = 0,
99 STATUS_CONTINUE = 1 // any value different then IOFEOF, IOFERR, ... which are < 0
100 };
101
102 /*
103 Predictor type identifiers (pdf spec 76). lpdf doesn't hire the codec if predictor is 1. Predictor 15 indicates
104 that the type of PNG prediction algorithm may change in subsequent lines. We always check algorithm marker anyway.
105 */
106
107 enum predictor_code {
108 NONE_PREDICTOR = 1,
109 TIFF_PREDICTOR = 2,
110 PNG_NONE_PREDICTOR = 10,
111 PNG_SUB_PREDICTOR = 11,
112 PNG_UP_PREDICTOR = 12,
113 PNG_AVERAGE_PREDICTOR = 13,
114 PNG_PAETH_PREDICTOR = 14,
115 PNG_OPTIMUM_PREDICTOR = 15
116 };
117
118 predictor_state * predictor_decoder_init (predictor_state *state, int predictor, int rowsamples, int components, int compbits)
119 {
120 int rowsize, pixelsize;
121 #define storage_pos(b, p, size) ((b = p), (p += size))
122 uint8_t *buffer, *p;
123 size_t buffersize;
124
125 pixelsize = (components * compbits + 7) >> 3; // to bytes, rounded up
126 rowsize = (rowsamples * components * compbits + 7) >> 3;
127
128 state->default_predictor = state->current_predictor = predictor;
129 state->rowsamples = rowsamples;
130 state->components = components;
131 state->compbits = compbits;
132
133 if (predictor == TIFF_PREDICTOR)
134 { /* tiff predictor */
135 size_t compbuf, pixbuf;
136 compbuf = components * sizeof(predictor_component_t);
137 pixbuf = 1 * sizeof(predictor_pixel1b_t);
138 state->pixbufsize = (int)(compbuf > pixbuf ? compbuf : pixbuf);
139 buffersize = rowsize * sizeof(uint8_t);
140 buffer = (uint8_t *)util_calloc(buffersize, 1);
141 if ((size_t)state->pixbufsize > sizeof(state->compbuffer)) // components > MAX_COMPONENTS
142 state->prevcomp = (predictor_component_t *)util_calloc(state->pixbufsize, 1);
143 else
144 state->prevcomp = state->compbuffer;
145 // &state->prevcomp == &state->prevpixel
146 state->sampleindex = state->compindex = 0;
147 state->bitsin = state->bitsout = 0;
148 state->compin = state->compout = 0;
149 }
150 else
151 { /* png predictors */
152 buffersize = (3 * rowsize + 2 * pixelsize + 1) * sizeof(uint8_t);
153 p = buffer = (uint8_t *)util_calloc(buffersize, 1);
154 storage_pos(state->rowin, p, 1 + rowsize); // one extra byte for prediction algorithm tag
155 p += pixelsize; // pixelsize extra bytes for virtual left pixel at the edge, eg. rowup[-1] (permanently \0)
156 storage_pos(state->rowup, p, rowsize); // actual row byte
157 p += pixelsize; // ditto
158 storage_pos(state->rowsave, p, rowsize);
159 state->pixelsize = pixelsize;
160 state->predictorbyte = 0;
161 }
162 state->buffer = buffer;
163 state->rowsize = rowsize;
164 state->rowindex = 0;
165 state->rowend = 0;
166 state->status = STATUS_CONTINUE;
167 return state;
168 }
169
170 predictor_state * predictor_encoder_init (predictor_state *state, int predictor, int rowsamples, int components, int compbits)
171 {
172 return predictor_decoder_init(state, predictor, rowsamples, components, compbits);
173 }
174
175 void predictor_decoder_close (predictor_state *state)
176 {
177 util_free(state->buffer);
178 if (state->default_predictor == TIFF_PREDICTOR && state->prevcomp != NULL && state->prevcomp != state->compbuffer)
179 util_free(state->prevcomp);
180 }
181
182 void predictor_encoder_close (predictor_state *state)
183 {
184 predictor_decoder_close(state);
185 }
186
187 /*
188 All predoctor codecs first read the entire data row into a buffer. This is not crucial for the process,
189 but allows to separate read/write states. In particular, there is one place in which codec functions
190 may return on EOD.
191 */
192
193 #define start_row(state) (state->rowindex = 0, state->rowin = state->buffer)
194
195 static int read_scanline (predictor_state *state, iof *I, int size)
196 {
197 int rowtail, left;
198 while ((rowtail = size - state->rowend) > 0)
199 {
200 left = (int)iof_left(I);
201 if (left >= rowtail)
202 {
203 memcpy(state->buffer + state->rowend, I->pos, (size_t)rowtail);
204 state->rowend += rowtail;
205 I->pos += rowtail;
206 start_row(state);
207 break;
208 }
209 else
210 {
211 if ((rowtail = left) > 0)
212 {
213 memcpy(state->buffer + state->rowend, I->pos, (size_t)rowtail);
214 state->rowend += rowtail;
215 I->pos += rowtail;
216 }
217 if (iof_input(I) == 0)
218 {
219 if (state->rowend == 0) // no scanline to process, no more input
220 return state->flush ? IOFEOF : IOFEMPTY;
221 /* If we are here, there is an incomplete scanline in buffer:
222 - if there is a chance for more (state->flush == 0), than wait for more
223 - otherwise encode/decode the last incomplete line?
224 pdf spec p. 76 says that "A row occupies a whole number of bytes",
225 so this situation should be considered abnormal (not found so far).
226 */
227 if (!state->flush)
228 return IOFEMPTY;
229 loggerf("incomplete scanline in predictor filter");
230 //return IOFERR;
231 state->status = STATUS_LAST;
232 state->rowsize -= size - state->rowend;
233 start_row(state);
234 break;
235 }
236 }
237 }
238 return STATUS_CONTINUE;
239 }
240
241 #define read_row(state, I, size, status) if ((status = read_scanline(state, I, size)) != STATUS_CONTINUE) return status
242
243 #define ensure_output_bytes(O, n) if (!iof_ensure(O, n)) return IOFFULL
244
245 #define tobyte(c) ((uint8_t)(c))
246 #define tocomp(c) ((uint16_t)(c))
247
248 #define row_byte(state) (state->rowin[state->rowindex])
249
250 /* png predictor macros; on bytes */
251
252 #define up_pixel_byte(state) (state->rowup[state->rowindex])
253 #define upleft_pixel_byte(state) (state->rowup[state->rowindex - state->pixelsize])
254 #define left_pixel_byte(state) (state->rowsave[state->rowindex - state->pixelsize])
255 #define save_pixel_byte(state, c) (state->rowsave[state->rowindex] = (uint8_t)(c))
256
257 /* tiff predictor macros; on components */
258
259 #define left_pixel_component(state) (state->prevcomp[state->compindex]) // tiff predictor with 2, 4, 8, 16 components
260 #define left_pixel_value(state) (state->prevpixel[0]) // tiff predictor with 1bit components
261
262 /* assignment in conditional
263 #define save_pixel_component(state, c) ((void)\
264 ((state->prevcomp[state->compindex] = (predictor_component_t)(c)), \
265 ++state->compindex, (state->compindex < state->components || (state->compindex = 0))))
266 */
267 #define save_pixel_component(state, c) \
268 do { state->prevcomp[state->compindex] = (predictor_component_t)(c); if (++state->compindex >= state->components) state->compindex = 0; } while (0)
269
270 #define save_pixel_value(state, c) (state->prevpixel[0] = (predictor_pixel1b_t)(c))
271
272 /* Once the codec function is done with the scanline, we set imaginary left pixel data to zero, and reset row counters to
273 zero in order to allow buffering another input scanline. */
274
275 #define reset_row(state) state->rowend = 0
276
277 #define reset_png_row(state) (memcpy(state->rowup, state->rowsave, state->rowsize), state->predictorbyte = 0, reset_row(state))
278
279 #define reset_tiff_row(state) \
280 memset(state->prevcomp, 0, state->pixbufsize), \
281 state->bitsin = state->bitsout = 0, \
282 state->compin = state->compout = 0, \
283 reset_row(state), \
284 state->sampleindex = state->compindex = 0
285
286 /* PNG paeth predictor function; http://www.libpng.org/pub/png/book/chapter09.html
287 Compute the base value p := left + up - upleft, then choose that byte the closest
288 (of the smallest absolute difference) to the base value. Left byte has a precedence. */
289
290
291 static int paeth (predictor_state *state)
292 {
293 int p, p1, p2, p3;
294 p = left_pixel_byte(state) + up_pixel_byte(state) - upleft_pixel_byte(state);
295 p1 = p >= left_pixel_byte(state) ? (p - left_pixel_byte(state)) : (left_pixel_byte(state) - p);
296 p2 = p >= up_pixel_byte(state) ? (p - up_pixel_byte(state)) : (up_pixel_byte(state) - p);
297 p3 = p >= upleft_pixel_byte(state) ? (p - upleft_pixel_byte(state)) : (upleft_pixel_byte(state) - p);
298 return (p1 <= p2 && p1 <= p3) ? left_pixel_byte(state) : (p2 <= p3 ? up_pixel_byte(state) : upleft_pixel_byte(state));
299 }
300
301 /* predictor decoder */
302
303 iof_status predictor_decode_state (iof *I, iof *O, predictor_state *state)
304 {
305 int status, c, d, outbytes;
306 while (state->status == STATUS_CONTINUE)
307 {
308 if (state->default_predictor >= 10) // PNG predictor?
309 {
310 read_row(state, I, state->rowsize + 1, status);
311 if (state->predictorbyte == 0)
312 { // we could actually check state->rowin <> state->buffer, but we need this flag for encoder anyway
313 state->current_predictor = row_byte(state) + 10;
314 state->predictorbyte = 1;
315 ++state->rowin;
316 }
317 }
318 else
319 {
320 read_row(state, I, state->rowsize, status);
321 }
322 switch (state->current_predictor)
323 {
324 case NONE_PREDICTOR:
325 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
326 {
327 ensure_output_bytes(O, 1);
328 c = row_byte(state);
329 iof_set(O, c);
330 }
331 reset_row(state);
332 break;
333 case TIFF_PREDICTOR:
334 switch (state->compbits)
335 {
336 case 1:
337 outbytes = (state->components + 7) >> 3;
338 for ( ; state->sampleindex < state->rowsamples; ++state->sampleindex)
339 {
340 ensure_output_bytes(O, outbytes);
341 while (state->bitsin < state->components)
342 {
343 state->compin = (state->compin << 8) | row_byte(state);
344 state->bitsin += 8;
345 ++state->rowindex;
346 }
347 state->bitsin -= state->components;
348 d = state->compin >> state->bitsin;
349 state->compin &= (1 << state->bitsin) - 1;
350 c = d ^ left_pixel_value(state);
351 save_pixel_value(state, c);
352 state->compout = (state->compout << state->components) | c;
353 state->bitsout += state->components;
354 while (state->bitsout >= 8)
355 {
356 state->bitsout -= 8;
357 iof_set(O, state->compout >> state->bitsout);
358 state->compout &= (1 << state->bitsout) - 1;
359 }
360 }
361 if (state->bitsout > 0)
362 {
363 ensure_output_bytes(O, 1);
364 iof_set(O, state->compin << (8 - state->bitsout));
365 }
366 break;
367 case 2: case 4:
368 for ( ; state->sampleindex < state->rowsamples; ++state->sampleindex)
369 {
370 for ( ; state->compindex < state->components; ) // state->compindex is ++ed by save_pixel_component()
371 {
372 ensure_output_bytes(O, 1);
373 if (state->bitsin < state->compbits)
374 {
375 state->compin = (state->compin << 8) | row_byte(state);
376 state->bitsin += 8;
377 ++state->rowindex;
378 }
379 state->bitsin -= state->compbits;
380 d = state->compin >> state->bitsin;
381 state->compin &= (1 << state->bitsin) - 1;
382 c = (d + left_pixel_component(state)) & 0xff;
383 save_pixel_component(state, c);
384 state->compout = (state->compout << state->compbits) | c;
385 state->bitsout += state->compbits;
386 if (state->bitsout >= 8)
387 {
388 state->bitsout -= 8;
389 iof_set(O, state->compout >> state->bitsout);
390 state->compout &= (1 << state->bitsout) - 1;
391 }
392 }
393 }
394 if (state->bitsout > 0)
395 {
396 ensure_output_bytes(O, 1);
397 iof_set(O, state->compin << (8 - state->bitsout));
398 }
399 break;
400 case 8:
401 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
402 {
403 ensure_output_bytes(O, 1);
404 c = (row_byte(state) + left_pixel_component(state)) & 0xff;
405 save_pixel_component(state, c);
406 iof_set(O, c);
407 }
408 break;
409 case 16:
410 for ( ; state->rowindex < state->rowsize - 1; ++state->rowindex)
411 {
412 ensure_output_bytes(O, 2);
413 d = row_byte(state) << 8;
414 ++state->rowindex;
415 d |= row_byte(state);
416 c = (d + left_pixel_component(state)) & 0xffff;
417 save_pixel_component(state, c);
418 iof_set2(O, c >> 8, c & 0xff);
419 }
420 break;
421 default:
422 return IOFERR;
423 }
424 reset_tiff_row(state);
425 break;
426 case PNG_NONE_PREDICTOR:
427 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
428 {
429 ensure_output_bytes(O, 1);
430 c = row_byte(state);
431 save_pixel_byte(state, c); // next row may need it
432 iof_set(O, c);
433 }
434 reset_png_row(state);
435 break;
436 case PNG_SUB_PREDICTOR:
437 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
438 {
439 ensure_output_bytes(O, 1);
440 c = (row_byte(state) + left_pixel_byte(state)) & 0xff;
441 save_pixel_byte(state, c);
442 iof_set(O, c);
443 }
444 reset_png_row(state);
445 break;
446 case PNG_UP_PREDICTOR:
447 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
448 {
449 ensure_output_bytes(O, 1);
450 c = (row_byte(state) + up_pixel_byte(state)) & 0xff;
451 save_pixel_byte(state, c);
452 iof_set(O, c);
453 }
454 reset_png_row(state);
455 break;
456 case PNG_AVERAGE_PREDICTOR:
457 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
458 {
459 ensure_output_bytes(O, 1);
460 c = (row_byte(state) + ((up_pixel_byte(state) + left_pixel_byte(state)) / 2)) & 0xff;
461 save_pixel_byte(state, c);
462 iof_set(O, c);
463 }
464 reset_png_row(state);
465 break;
466 case PNG_PAETH_PREDICTOR:
467 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
468 {
469 ensure_output_bytes(O, 1);
470 c = (row_byte(state) + paeth(state)) & 0xff;
471 save_pixel_byte(state, c);
472 iof_set(O, c);
473 }
474 reset_png_row(state);
475 break;
476 //case PNG_OPTIMUM_PREDICTOR: // valid as default_redictor, but not as algorithm identifier byte
477 default:
478 return IOFERR;
479 }
480 }
481 return state->status == STATUS_LAST ? IOFERR : IOFEOF;
482 }
483
484 /* predictor encoder */
485
486 iof_status predictor_encode_state (iof *I, iof *O, predictor_state *state)
487 {
488 int status, c, d, outbytes;
489 while (state->status == STATUS_CONTINUE)
490 {
491 read_row(state, I, state->rowsize, status);
492 if (state->current_predictor >= 10 && state->predictorbyte == 0)
493 {
494 ensure_output_bytes(O, 1);
495 iof_set(O, state->current_predictor - 10);
496 state->predictorbyte = 1;
497 }
498 switch (state->current_predictor)
499 {
500 case NONE_PREDICTOR:
501 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
502 {
503 ensure_output_bytes(O, 1);
504 c = row_byte(state);
505 iof_set(O, c);
506 }
507 reset_row(state);
508 break;
509 case TIFF_PREDICTOR:
510 switch (state->compbits)
511 {
512 case 1:
513 outbytes = (state->components + 7) >> 3;
514 for ( ; state->sampleindex < state->rowsamples; ++state->sampleindex)
515 {
516 ensure_output_bytes(O, outbytes);
517 while (state->bitsin < state->components)
518 {
519 state->compin = (state->compin << 8) | row_byte(state);
520 state->bitsin += 8;
521 ++state->rowindex;
522 }
523 state->bitsin -= state->components;
524 c = state->compin >> state->bitsin;
525 state->compin &= (1 << state->bitsin) - 1;
526 d = c ^ left_pixel_value(state);
527 save_pixel_value(state, c);
528 state->compout = (state->compout << state->components) | d;
529 state->bitsout += state->components;
530 while (state->bitsout >= 8)
531 {
532 state->bitsout -= 8;
533 iof_set(O, state->compout >> state->bitsout);
534 state->compout &= (1 << state->bitsout) - 1;
535 }
536 }
537 if (state->bitsout > 0)
538 {
539 ensure_output_bytes(O, 1);
540 iof_set(O, state->compin << (8 - state->bitsout));
541 }
542 break;
543 case 2: case 4:
544 for ( ; state->sampleindex < state->rowsamples; ++state->sampleindex)
545 {
546 for ( ; state->compindex < state->components; )
547 {
548 ensure_output_bytes(O, 1);
549 if (state->bitsin < state->compbits)
550 {
551 state->compin = (state->compin << 8) | row_byte(state);
552 state->bitsin += 8;
553 ++state->rowindex;
554 }
555 state->bitsin -= state->compbits;
556 c = state->compin >> state->bitsin;
557 state->compin &= (1 << state->bitsin) - 1;
558 d = tocomp(c - left_pixel_component(state));
559 save_pixel_component(state, c);
560 state->compout = (state->compout << state->compbits) | d;
561 state->bitsout += state->compbits;
562 if (state->bitsout >= 8)
563 {
564 state->bitsout -= 8;
565 iof_set(O, state->compout >> state->bitsout);
566 state->compout &= (1 << state->bitsout) - 1;
567 }
568 }
569 }
570 if (state->bitsout > 0)
571 {
572 ensure_output_bytes(O, 1);
573 iof_set(O, state->compin << (8 - state->bitsout));
574 }
575 break;
576 case 8:
577 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
578 {
579 ensure_output_bytes(O, 1);
580 c = row_byte(state);
581 d = tobyte(c - left_pixel_component(state));
582 save_pixel_component(state, c);
583 iof_set(O, d);
584 }
585 break;
586 case 16:
587 for ( ; state->rowindex < state->rowsize - 1; ++state->rowindex)
588 {
589 ensure_output_bytes(O, 2);
590 c = row_byte(state) << 8;
591 ++state->rowindex;
592 c |= row_byte(state);
593 d = tocomp(c - left_pixel_component(state));
594 save_pixel_component(state, c);
595 iof_set2(O, d >> 8, d & 0xff);
596 }
597 break;
598 default:
599 return IOFERR;
600 }
601 reset_tiff_row(state);
602 break;
603 case PNG_NONE_PREDICTOR:
604 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
605 {
606 ensure_output_bytes(O, 1);
607 c = row_byte(state);
608 save_pixel_byte(state, c); // next row may need it
609 iof_set(O, c);
610 }
611 reset_png_row(state);
612 break;
613 case PNG_SUB_PREDICTOR:
614 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
615 {
616 ensure_output_bytes(O, 1);
617 c = row_byte(state);
618 d = tobyte(c - left_pixel_byte(state));
619 save_pixel_byte(state, c);
620 iof_set(O, d);
621 }
622 reset_png_row(state);
623 break;
624 case PNG_OPTIMUM_PREDICTOR: // not worthy to perform optimization
625 case PNG_UP_PREDICTOR:
626 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
627 {
628 ensure_output_bytes(O, 1);
629 c = row_byte(state);
630 d = tobyte(c - up_pixel_byte(state));
631 save_pixel_byte(state, c);
632 iof_set(O, d);
633 }
634 reset_png_row(state);
635 break;
636 case PNG_AVERAGE_PREDICTOR:
637 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
638 {
639 ensure_output_bytes(O, 1);
640 c = row_byte(state);
641 d = tobyte(c - ((up_pixel_byte(state) + left_pixel_byte(state)) >> 1));
642 save_pixel_byte(state, c);
643 iof_set(O, d);
644 }
645 reset_png_row(state);
646 break;
647 case PNG_PAETH_PREDICTOR:
648 for ( ; state->rowindex < state->rowsize; ++state->rowindex)
649 {
650 ensure_output_bytes(O, 1);
651 c = row_byte(state);
652 d = tobyte(c - paeth(state));
653 save_pixel_byte(state, c);
654 iof_set(O, d);
655 }
656 reset_png_row(state);
657 break;
658 default:
659 return IOFERR;
660 }
661 }
662 return state->status == STATUS_LAST ? IOFERR : IOFEOF;
663 }
664
665 iof_status predictor_decode (iof *I, iof *O, int predictor, int rowsamples, int components, int compbits)
666 {
667 predictor_state state;
668 int ret;
669 predictor_decoder_init(&state, predictor, rowsamples, components, compbits);
670 state.flush = 1;
671 ret = predictor_decode_state(I, O, &state);
672 predictor_decoder_close(&state);
673 return ret;
674 }
675
676 iof_status predictor_encode (iof *I, iof *O, int predictor, int rowsamples, int components, int compbits)
677 {
678 predictor_state state;
679 int ret;
680 predictor_encoder_init(&state, predictor, rowsamples, components, compbits);
681 state.flush = 1;
682 ret = predictor_encode_state(I, O, &state);
683 predictor_encoder_close(&state);
684 return ret;
685 }
686
687 /* filters */
688
689 // predictor decoder function
690
691 static size_t predictor_decoder (iof *F, iof_mode mode)
692 {
693 predictor_state *state;
694 iof_status status;
695 size_t tail;
696
697 state = iof_filter_state(predictor_state *, F);
698 switch(mode)
699 {
700 case IOFLOAD:
701 case IOFREAD:
702 if (F->flags & IOF_STOPPED)
703 return 0;
704 tail = iof_tail(F);
705 F->pos = F->buf + tail;
706 F->end = F->buf + F->space;
707 do {
708 status = predictor_decode_state(F->next, F, state);
709 } while (mode == IOFLOAD && status == IOFFULL && iof_resize_buffer(F));
710 return iof_decoder_retval(F, "predictor", status);
711 case IOFCLOSE:
712 predictor_decoder_close(state);
713 iof_free(F);
714 return 0;
715 default:
716 break;
717 }
718 return 0;
719 }
720
721 // predictor encoder function
722
723 static size_t predictor_encoder (iof *F, iof_mode mode)
724 {
725 predictor_state *state;
726 iof_status status;
727
728 state = iof_filter_state(predictor_state *, F);
729 switch (mode)
730 {
731 case IOFFLUSH:
732 state->flush = 1;
733 FALLTHRU // fall through
734 case IOFWRITE:
735 F->end = F->pos;
736 F->pos = F->buf;
737 status = predictor_encode_state(F, F->next, state);
738 return iof_encoder_retval(F, "predictor", status);
739 case IOFCLOSE:
740 if (!state->flush)
741 predictor_encoder(F, IOFFLUSH);
742 predictor_encoder_close(state);
743 iof_free(F);
744 return 0;
745 default:
746 break;
747 }
748 return 0;
749 }
750
751 iof * iof_filter_predictor_decoder (iof *N, int predictor, int rowsamples, int components, int compbits)
752 {
753 iof *I;
754 predictor_state_pointer P;
755 I = iof_filter_reader(predictor_decoder, sizeof(predictor_state), &P.voidstate);
756 iof_setup_next(I, N);
757 if (predictor_decoder_init(P.predictorstate, predictor, rowsamples, components, compbits) == NULL)
758 {
759 iof_discard(I);
760 return NULL;
761 }
762 P.predictorstate->flush = 1;
763 return I;
764 }
765
766 iof * iof_filter_predictor_encoder (iof *N, int predictor, int rowsamples, int components, int compbits)
767 {
768 iof *O;
769 predictor_state_pointer P;
770 O = iof_filter_writer(predictor_encoder, sizeof(predictor_state), &P.voidstate);
771 iof_setup_next(O, N);
772 if (predictor_encoder_init(P.predictorstate, predictor, rowsamples, components, compbits) == NULL)
773 {
774 iof_discard(O);
775 return NULL;
776 }
777 return O;
778 }