Implement Paeth predictor.

This commit is contained in:
Michael R Sweet 2021-05-08 19:04:42 -04:00
parent 979ccf19ff
commit f8f048b87a
No known key found for this signature in database
GPG Key ID: 999559A027815955

View File

@ -18,6 +18,7 @@
// Local functions...
//
static unsigned char stream_paeth(unsigned char a, unsigned char b, unsigned char c);
static ssize_t stream_read(pdfio_stream_t *st, char *buffer, size_t bytes);
@ -504,6 +505,24 @@ pdfioStreamWrite(
}
//
// 'stream_paeth()' - PaethPredictor function for PNG decompression filter.
//
static unsigned char // O - Predictor value
stream_paeth(unsigned char a, // I - Left pixel
unsigned char b, // I - Top pixel
unsigned char c) // I - Top-left pixel
{
int p = a + b - c; // Initial estimate
int pa = abs(p - a); // Distance to a
int pb = abs(p - b); // Distance to b
int pc = abs(p - c); // Distance to c
return ((pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c);
}
//
// 'stream_read()' - Read data from a stream, including filters.
//
@ -653,8 +672,10 @@ stream_read(pdfio_stream_t *st, // I - Stream
*bufptr++ = *thisptr + (thisptr[-pbpixel] + *prevptr) / 2;
break;
case 4 : // Paeth
// TODO: Implement Paeth predictor
memcpy(buffer, thisptr, remaining);
for (; remaining > firstcol; remaining --, thisptr ++, prevptr ++)
*bufptr++ = *thisptr + stream_paeth(0, *prevptr, 0);
for (; remaining > 0; remaining --, thisptr ++, prevptr ++)
*bufptr++ = *thisptr + stream_paeth(thisptr[-pbpixel], *prevptr, prevptr[-pbpixel]);
break;
default :