From d592309df36d2a81fbd7123cafa1a09b37137daf Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 19 Nov 2009 22:26:58 +0100 Subject: Remove unused and largely untested encoding filters. --- fitz/Jamfile | 14 +- fitz/filt_a85d.c | 128 ---------------- fitz/filt_a85e.c | 126 ---------------- fitz/filt_ahxd.c | 112 -------------- fitz/filt_ahxe.c | 64 -------- fitz/filt_basic.c | 387 ++++++++++++++++++++++++++++++++++++++++++++++++ fitz/filt_copy.c | 49 ------- fitz/filt_dctc.h | 39 ----- fitz/filt_dctd.c | 35 ++++- fitz/filt_dcte.c | 254 -------------------------------- fitz/filt_faxc.h | 125 ---------------- fitz/filt_faxd.c | 126 +++++++++++++++- fitz/filt_faxe.c | 407 --------------------------------------------------- fitz/filt_faxe.h | 37 ----- fitz/filt_faxetab.c | 131 ----------------- fitz/filt_flate.c | 96 ------------ fitz/filt_jpxd.c | 163 +++++++++++++++++++++ fitz/filt_jpxd_jas.c | 132 ----------------- fitz/filt_jpxd_opj.c | 163 --------------------- fitz/filt_lzwe.c | 261 --------------------------------- fitz/filt_null.c | 54 ------- fitz/filt_predict.c | 90 +++--------- fitz/filt_rld.c | 73 --------- fitz/filt_rle.c | 238 ------------------------------ fitz/fitz_stream.h | 8 - 25 files changed, 734 insertions(+), 2578 deletions(-) delete mode 100644 fitz/filt_a85d.c delete mode 100644 fitz/filt_a85e.c delete mode 100644 fitz/filt_ahxd.c delete mode 100644 fitz/filt_ahxe.c create mode 100644 fitz/filt_basic.c delete mode 100644 fitz/filt_copy.c delete mode 100644 fitz/filt_dctc.h delete mode 100644 fitz/filt_dcte.c delete mode 100644 fitz/filt_faxc.h delete mode 100644 fitz/filt_faxe.c delete mode 100644 fitz/filt_faxe.h delete mode 100644 fitz/filt_faxetab.c create mode 100644 fitz/filt_jpxd.c delete mode 100644 fitz/filt_jpxd_jas.c delete mode 100644 fitz/filt_jpxd_opj.c delete mode 100644 fitz/filt_lzwe.c delete mode 100644 fitz/filt_null.c delete mode 100644 fitz/filt_rld.c delete mode 100644 fitz/filt_rle.c diff --git a/fitz/Jamfile b/fitz/Jamfile index 4ca443fd..4be1beb5 100644 --- a/fitz/Jamfile +++ b/fitz/Jamfile @@ -52,27 +52,15 @@ Library libfitz : stm_misc.c filt_pipeline.c - filt_copy.c + filt_basic.c filt_arc4.c filt_aesd.c - filt_null.c - - filt_a85d.c - filt_a85e.c - filt_ahxd.c - filt_ahxe.c filt_dctd.c - filt_dcte.c filt_faxd.c filt_faxdtab.c - filt_faxe.c - filt_faxetab.c filt_flate.c filt_lzwd.c - filt_lzwe.c filt_predict.c - filt_rld.c - filt_rle.c ; diff --git a/fitz/filt_a85d.c b/fitz/filt_a85d.c deleted file mode 100644 index 8d485a3c..00000000 --- a/fitz/filt_a85d.c +++ /dev/null @@ -1,128 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -typedef struct fz_a85d_s fz_a85d; - -struct fz_a85d_s -{ - fz_filter super; - unsigned long word; - int count; -}; - -static inline int iswhite(int a) -{ - switch (a) { - case '\n': case '\r': case '\t': case ' ': - case '\0': case '\f': case '\b': case 0177: - return 1; - } - return 0; -} - -fz_error -fz_newa85d(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_a85d, f, a85d); - f->word = 0; - f->count = 0; - return fz_okay; -} - -void -fz_dropa85d(fz_filter *f) -{ -} - -fz_error -fz_processa85d(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_a85d *f = (fz_a85d*)filter; - int c; - - while (1) - { - if (in->rp == in->wp) - return fz_ioneedin; - - c = *in->rp++; - - if (c >= '!' && c <= 'u') { - if (f->count == 4) { - if (out->wp + 4 > out->ep) { - in->rp --; - return fz_ioneedout; - } - - f->word = f->word * 85 + (c - '!'); - - *out->wp++ = (f->word >> 24) & 0xff; - *out->wp++ = (f->word >> 16) & 0xff; - *out->wp++ = (f->word >> 8) & 0xff; - *out->wp++ = (f->word) & 0xff; - - f->word = 0; - f->count = 0; - } - else { - f->word = f->word * 85 + (c - '!'); - f->count ++; - } - } - - else if (c == 'z' && f->count == 0) { - if (out->wp + 4 > out->ep) { - in->rp --; - return fz_ioneedout; - } - *out->wp++ = 0; - *out->wp++ = 0; - *out->wp++ = 0; - *out->wp++ = 0; - } - - else if (c == '~') { - if (in->rp == in->wp) { - in->rp --; - return fz_ioneedin; - } - - c = *in->rp++; - - if (c != '>') { - return fz_throw("bad eod marker in a85d"); - } - - if (out->wp + f->count - 1 > out->ep) { - in->rp -= 2; - return fz_ioneedout; - } - - switch (f->count) { - case 0: - break; - case 1: - return fz_throw("partial final byte in a85d"); - case 2: - f->word = f->word * (85L * 85 * 85) + 0xffffffL; - goto o1; - case 3: - f->word = f->word * (85L * 85) + 0xffffL; - goto o2; - case 4: - f->word = f->word * 85 + 0xffL; - *(out->wp+2) = f->word >> 8; -o2: *(out->wp+1) = f->word >> 16; -o1: *(out->wp+0) = f->word >> 24; - out->wp += f->count - 1; - break; - } - return fz_iodone; - } - - else if (!iswhite(c)) { - return fz_throw("bad data in a85d: '%c'", c); - } - } -} - diff --git a/fitz/filt_a85e.c b/fitz/filt_a85e.c deleted file mode 100644 index e2450bb5..00000000 --- a/fitz/filt_a85e.c +++ /dev/null @@ -1,126 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -typedef struct fz_a85e_s fz_a85e; - -struct fz_a85e_s -{ - fz_filter super; - int c; -}; - -fz_error -fz_newa85e(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_a85e, f, a85e); - f->c = 0; - return fz_okay; -} - -void -fz_dropa85e(fz_filter *f) -{ -} - -fz_error -fz_processa85e(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_a85e *f = (fz_a85e*)filter; - unsigned long word; - int count; - int n; - - n = 0; - - while (1) - { - if (f->c >= 70) { - if (out->wp + 1 > out->ep) - return fz_ioneedout; - *out->wp++ = '\n'; - f->c = 0; - n ++; - } - - if (in->rp + 4 <= in->wp) - { - word = (in->rp[0] << 24) | - (in->rp[1] << 16) | - (in->rp[2] << 8) | - (in->rp[3]); - if (word == 0) { - if (out->wp + 1 > out->ep) - return fz_ioneedout; - *out->wp++ = 'z'; - f->c ++; - n ++; - } - else { - unsigned long v1, v2, v3, v4; - - if (out->wp + 5 > out->ep) - return fz_ioneedout; - - v4 = word / 85; - v3 = v4 / 85; - v2 = v3 / 85; - v1 = v2 / 85; - - *out->wp++ = (v1 % 85) + '!'; - *out->wp++ = (v2 % 85) + '!'; - *out->wp++ = (v3 % 85) + '!'; - *out->wp++ = (v4 % 85) + '!'; - *out->wp++ = (word % 85) + '!'; - f->c += 5; - n += 5; - } - in->rp += 4; - } - - else if (in->eof) - { - unsigned long divisor; - - if (in->rp == in->wp) - goto needinput; /* handle clean eof here */ - - count = in->wp - in->rp; - - if (out->wp + count + 3 > out->ep) - return fz_ioneedout; - - word = 0; - switch (count) { - case 3: word |= in->rp[2] << 8; - case 2: word |= in->rp[1] << 16; - case 1: word |= in->rp[0] << 24; - } - in->rp += count; - - divisor = 85L * 85 * 85 * 85; - while (count-- >= 0) { - *out->wp++ = ((word / divisor) % 85) + '!'; - divisor /= 85; - } - - *out->wp++ = '~'; - *out->wp++ = '>'; - return fz_iodone; - } - - else { - goto needinput; - } - } - -needinput: - if (in->eof) { - if (out->wp + 2 > out->ep) - return fz_ioneedout; - *out->wp++ = '~'; - *out->wp++ = '>'; - return fz_iodone; - } - return fz_ioneedin; -} - diff --git a/fitz/filt_ahxd.c b/fitz/filt_ahxd.c deleted file mode 100644 index 5e1fe17c..00000000 --- a/fitz/filt_ahxd.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -typedef struct fz_ahxd_s fz_ahxd; - -struct fz_ahxd_s -{ - fz_filter super; - int odd; - int a; -}; - -static inline int iswhite(int a) -{ - switch (a) { - case '\n': case '\r': case '\t': case ' ': - case '\0': case '\f': case '\b': case 0177: - return 1; - } - return 0; -} - -static inline int ishex(int a) -{ - return (a >= 'A' && a <= 'F') || - (a >= 'a' && a <= 'f') || - (a >= '0' && a <= '9'); -} - -static inline int fromhex(int a) -{ - if (a >= 'A' && a <= 'F') - return a - 'A' + 0xA; - if (a >= 'a' && a <= 'f') - return a - 'a' + 0xA; - if (a >= '0' && a <= '9') - return a - '0'; - return 0; -} - -fz_error -fz_newahxd(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_ahxd, f, ahxd); - f->odd = 0; - f->a = 0; - return fz_okay; -} - -void -fz_dropahxd(fz_filter *f) -{ -} - -fz_error -fz_processahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_ahxd *f = (fz_ahxd*)filter; - int b, c; - - while (1) - { - if (in->rp == in->wp) - return fz_ioneedin; - - if (out->wp == out->ep) - return fz_ioneedout; - - c = *in->rp++; - - if (ishex(c)) { - if (!f->odd) { - f->a = fromhex(c); - f->odd = 1; - } - else { - b = fromhex(c); - *out->wp++ = (f->a << 4) | b; - f->odd = 0; - } - } - - else if (c == '>') { - if (f->odd) - *out->wp++ = (f->a << 4); - return fz_iodone; - } - - else if (!iswhite(c)) { - return fz_throw("bad data in ahxd: '%c'", c); - } - } -} - -void -fz_pushbackahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out, int n) -{ - int k; - - assert(filter->process == fz_processahxd); - assert(out->wp - n >= out->rp); - - k = 0; - while (k < n * 2) { - in->rp --; - if (ishex(*in->rp)) - k ++; - } - - out->wp -= n; -} - diff --git a/fitz/filt_ahxe.c b/fitz/filt_ahxe.c deleted file mode 100644 index ffa66b69..00000000 --- a/fitz/filt_ahxe.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -typedef struct fz_ahxe_s fz_ahxe; - -struct fz_ahxe_s -{ - fz_filter super; - int c; -}; - -static const char tohex[16] = "0123456789ABCDEF"; - -fz_error -fz_newahxe(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_ahxe, f, ahxe); - f->c = 0; - return fz_okay; -} - -void -fz_dropahxe(fz_filter *f) -{ -} - -fz_error -fz_processahxe(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_ahxe *f = (fz_ahxe*)filter; - int a, b, c; - - while (1) - { - if (in->rp == in->wp) - goto needinput; - - if (out->wp + 2 >= out->ep) /* can write 3 bytes from 1 */ - return fz_ioneedout; - - c = *in->rp++; - a = tohex[(c >> 4) & 0x0f]; - b = tohex[c & 0x0f]; - - *out->wp++ = a; - *out->wp++ = b; - - f->c += 2; - if (f->c == 60) { - *out->wp++ = '\n'; - f->c = 0; - } - } - -needinput: - if (in->eof) { - if (out->wp == out->ep) - return fz_ioneedout; - *out->wp++ = '>'; - return fz_iodone; - } - return fz_ioneedin; -} - diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c new file mode 100644 index 00000000..12408b5e --- /dev/null +++ b/fitz/filt_basic.c @@ -0,0 +1,387 @@ +#include "fitz_base.h" +#include "fitz_stream.h" + +fz_error +fz_newcopyfilter(fz_filter **fp) +{ + FZ_NEWFILTER(fz_filter, f, copyfilter); + return fz_okay; +} + +void +fz_dropcopyfilter(fz_filter *f) +{ +} + +fz_error +fz_processcopyfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out) +{ + int n; + + while (1) + { + if (in->rp + 1 > in->wp) + { + if (in->eof) + return fz_iodone; + return fz_ioneedin; + } + + if (out->wp + 1 > out->ep) + return fz_ioneedout; + + n = MIN(in->wp - in->rp, out->ep - out->wp); + if (n) + { + memcpy(out->wp, in->rp, n); + in->rp += n; + out->wp += n; + } + } +} + +typedef struct fz_nullfilter_s fz_nullfilter; + +struct fz_nullfilter_s +{ + fz_filter super; + int len; + int cur; +}; + +fz_error +fz_newnullfilter(fz_filter **fp, int len) +{ + FZ_NEWFILTER(fz_nullfilter, f, nullfilter); + f->len = len; + f->cur = 0; + return fz_okay; +} + +void +fz_dropnullfilter(fz_filter *f) +{ +} + +fz_error +fz_processnullfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out) +{ + fz_nullfilter *f = (fz_nullfilter*)filter; + int n; + + n = MIN(in->wp - in->rp, out->ep - out->wp); + if (f->len >= 0) + n = MIN(n, f->len - f->cur); + + if (n) + { + memcpy(out->wp, in->rp, n); + in->rp += n; + out->wp += n; + f->cur += n; + } + + if (f->cur == f->len) + return fz_iodone; + if (in->rp == in->wp) + return fz_ioneedin; + if (out->wp == out->ep) + return fz_ioneedout; + + return fz_throw("braindead programmer trapped in nullfilter"); +} + +typedef struct fz_ahxd_s fz_ahxd; + +struct fz_ahxd_s +{ + fz_filter super; + int odd; + int a; +}; + +static inline int iswhite(int a) +{ + switch (a) { + case '\n': case '\r': case '\t': case ' ': + case '\0': case '\f': case '\b': case 0177: + return 1; + } + return 0; +} + +static inline int ishex(int a) +{ + return (a >= 'A' && a <= 'F') || + (a >= 'a' && a <= 'f') || + (a >= '0' && a <= '9'); +} + +static inline int fromhex(int a) +{ + if (a >= 'A' && a <= 'F') + return a - 'A' + 0xA; + if (a >= 'a' && a <= 'f') + return a - 'a' + 0xA; + if (a >= '0' && a <= '9') + return a - '0'; + return 0; +} + +fz_error +fz_newahxd(fz_filter **fp, fz_obj *params) +{ + FZ_NEWFILTER(fz_ahxd, f, ahxd); + f->odd = 0; + f->a = 0; + return fz_okay; +} + +void +fz_dropahxd(fz_filter *f) +{ +} + +fz_error +fz_processahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out) +{ + fz_ahxd *f = (fz_ahxd*)filter; + int b, c; + + while (1) + { + if (in->rp == in->wp) + return fz_ioneedin; + + if (out->wp == out->ep) + return fz_ioneedout; + + c = *in->rp++; + + if (ishex(c)) { + if (!f->odd) { + f->a = fromhex(c); + f->odd = 1; + } + else { + b = fromhex(c); + *out->wp++ = (f->a << 4) | b; + f->odd = 0; + } + } + + else if (c == '>') { + if (f->odd) + *out->wp++ = (f->a << 4); + return fz_iodone; + } + + else if (!iswhite(c)) { + return fz_throw("bad data in ahxd: '%c'", c); + } + } +} + +void +fz_pushbackahxd(fz_filter *filter, fz_buffer *in, fz_buffer *out, int n) +{ + int k; + + assert(filter->process == fz_processahxd); + assert(out->wp - n >= out->rp); + + k = 0; + while (k < n * 2) { + in->rp --; + if (ishex(*in->rp)) + k ++; + } + + out->wp -= n; +} + +typedef struct fz_a85d_s fz_a85d; + +struct fz_a85d_s +{ + fz_filter super; + unsigned long word; + int count; +}; + +fz_error +fz_newa85d(fz_filter **fp, fz_obj *params) +{ + FZ_NEWFILTER(fz_a85d, f, a85d); + f->word = 0; + f->count = 0; + return fz_okay; +} + +void +fz_dropa85d(fz_filter *f) +{ +} + +fz_error +fz_processa85d(fz_filter *filter, fz_buffer *in, fz_buffer *out) +{ + fz_a85d *f = (fz_a85d*)filter; + int c; + + while (1) + { + if (in->rp == in->wp) + return fz_ioneedin; + + c = *in->rp++; + + if (c >= '!' && c <= 'u') { + if (f->count == 4) { + if (out->wp + 4 > out->ep) { + in->rp --; + return fz_ioneedout; + } + + f->word = f->word * 85 + (c - '!'); + + *out->wp++ = (f->word >> 24) & 0xff; + *out->wp++ = (f->word >> 16) & 0xff; + *out->wp++ = (f->word >> 8) & 0xff; + *out->wp++ = (f->word) & 0xff; + + f->word = 0; + f->count = 0; + } + else { + f->word = f->word * 85 + (c - '!'); + f->count ++; + } + } + + else if (c == 'z' && f->count == 0) { + if (out->wp + 4 > out->ep) { + in->rp --; + return fz_ioneedout; + } + *out->wp++ = 0; + *out->wp++ = 0; + *out->wp++ = 0; + *out->wp++ = 0; + } + + else if (c == '~') { + if (in->rp == in->wp) { + in->rp --; + return fz_ioneedin; + } + + c = *in->rp++; + + if (c != '>') { + return fz_throw("bad eod marker in a85d"); + } + + if (out->wp + f->count - 1 > out->ep) { + in->rp -= 2; + return fz_ioneedout; + } + + switch (f->count) { + case 0: + break; + case 1: + return fz_throw("partial final byte in a85d"); + case 2: + f->word = f->word * (85L * 85 * 85) + 0xffffffL; + goto o1; + case 3: + f->word = f->word * (85L * 85) + 0xffffL; + goto o2; + case 4: + f->word = f->word * 85 + 0xffL; + *(out->wp+2) = f->word >> 8; +o2: *(out->wp+1) = f->word >> 16; +o1: *(out->wp+0) = f->word >> 24; + out->wp += f->count - 1; + break; + } + return fz_iodone; + } + + else if (!iswhite(c)) { + return fz_throw("bad data in a85d: '%c'", c); + } + } +} + +fz_error +fz_newrld(fz_filter **fp, fz_obj *params) +{ + FZ_NEWFILTER(fz_filter, f, rld); + return fz_okay; +} + +void +fz_droprld(fz_filter *rld) +{ +} + +fz_error +fz_processrld(fz_filter *filter, fz_buffer *in, fz_buffer *out) +{ + int run, i; + unsigned char c; + + while (1) + { + if (in->rp == in->wp) + { + if (in->eof) + { + return fz_iodone; + } + return fz_ioneedin; + } + + if (out->wp == out->ep) + return fz_ioneedout; + + run = *in->rp++; + + if (run == 128) + { + return fz_iodone; + } + + else if (run < 128) { + run = run + 1; + if (in->rp + run > in->wp) { + in->rp --; + return fz_ioneedin; + } + if (out->wp + run > out->ep) { + in->rp --; + return fz_ioneedout; + } + for (i = 0; i < run; i++) + *out->wp++ = *in->rp++; + } + + else if (run > 128) { + run = 257 - run; + if (in->rp + 1 > in->wp) { + in->rp --; + return fz_ioneedin; + } + if (out->wp + run > out->ep) { + in->rp --; + return fz_ioneedout; + } + c = *in->rp++; + for (i = 0; i < run; i++) + *out->wp++ = c; + } + } +} + diff --git a/fitz/filt_copy.c b/fitz/filt_copy.c deleted file mode 100644 index 6c77ff17..00000000 --- a/fitz/filt_copy.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -typedef struct fz_copyfilter_s fz_copyfilter; - -struct fz_copyfilter_s -{ - fz_filter super; -}; - -fz_error -fz_newcopyfilter(fz_filter **fp) -{ - FZ_NEWFILTER(fz_copyfilter, f, copyfilter); - return fz_okay; -} - -void -fz_dropcopyfilter(fz_filter *f) -{ -} - -fz_error -fz_processcopyfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - int n; - - while (1) - { - if (in->rp + 1 > in->wp) - { - if (in->eof) - return fz_iodone; - return fz_ioneedin; - } - - if (out->wp + 1 > out->ep) - return fz_ioneedout; - - n = MIN(in->wp - in->rp, out->ep - out->wp); - if (n) - { - memcpy(out->wp, in->rp, n); - in->rp += n; - out->wp += n; - } - } -} - diff --git a/fitz/filt_dctc.h b/fitz/filt_dctc.h deleted file mode 100644 index 72f61ef2..00000000 --- a/fitz/filt_dctc.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Extend libjpegs error handler to use setjmp/longjmp - */ - -#include - -#include - -struct myerrmgr -{ - struct jpeg_error_mgr super; - jmp_buf jb; - char msg[JMSG_LENGTH_MAX]; -}; - -static void myerrexit(j_common_ptr cinfo) -{ - struct myerrmgr *err = (struct myerrmgr *)cinfo->err; - char msgbuf[JMSG_LENGTH_MAX]; - err->super.format_message(cinfo, msgbuf); - strlcpy(err->msg, msgbuf, sizeof err->msg); - longjmp(err->jb, 1); -} - -static void myoutmess(j_common_ptr cinfo) -{ - struct myerrmgr *err = (struct myerrmgr *)cinfo->err; - char msgbuf[JMSG_LENGTH_MAX]; - err->super.format_message(cinfo, msgbuf); - fz_warn("jpeg error: %s", msgbuf); -} - -static void myiniterr(struct myerrmgr *err) -{ - jpeg_std_error(&err->super); - err->super.error_exit = myerrexit; - err->super.output_message = myoutmess; -} - diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c index 39e97a0d..4f7778e8 100644 --- a/fitz/filt_dctd.c +++ b/fitz/filt_dctd.c @@ -1,10 +1,43 @@ #include "fitz_base.h" #include "fitz_stream.h" -#include "filt_dctc.h" +#include + +#include typedef struct fz_dctd_s fz_dctd; +struct myerrmgr +{ + struct jpeg_error_mgr super; + jmp_buf jb; + char msg[JMSG_LENGTH_MAX]; +}; + +static void myerrexit(j_common_ptr cinfo) +{ + struct myerrmgr *err = (struct myerrmgr *)cinfo->err; + char msgbuf[JMSG_LENGTH_MAX]; + err->super.format_message(cinfo, msgbuf); + strlcpy(err->msg, msgbuf, sizeof err->msg); + longjmp(err->jb, 1); +} + +static void myoutmess(j_common_ptr cinfo) +{ + struct myerrmgr *err = (struct myerrmgr *)cinfo->err; + char msgbuf[JMSG_LENGTH_MAX]; + err->super.format_message(cinfo, msgbuf); + fz_warn("jpeg error: %s", msgbuf); +} + +static void myiniterr(struct myerrmgr *err) +{ + jpeg_std_error(&err->super); + err->super.error_exit = myerrexit; + err->super.output_message = myoutmess; +} + struct mysrcmgr { struct jpeg_source_mgr super; diff --git a/fitz/filt_dcte.c b/fitz/filt_dcte.c deleted file mode 100644 index 0949d9a9..00000000 --- a/fitz/filt_dcte.c +++ /dev/null @@ -1,254 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -#include "filt_dctc.h" - -typedef struct fz_dcte_s fz_dcte; - -struct mydstmgr -{ - struct jpeg_destination_mgr super; - fz_buffer *buf; -}; - -struct fz_dcte_s -{ - fz_filter super; - - struct jpeg_compress_struct cinfo; - struct mydstmgr dst; - struct myerrmgr err; - int stage; - - int columns; - int rows; - int colors; -}; - -static void myinitdest(j_compress_ptr cinfo) { /* empty */ } -static boolean myemptybuf(j_compress_ptr cinfo) { return FALSE; } -static void mytermdest(j_compress_ptr cinfo) { /* empty */ } - -fz_error -fz_newdcte(fz_filter **fp, fz_obj *params) -{ - fz_error err; - fz_obj *obj; - int i; - - FZ_NEWFILTER(fz_dcte, e, dcte); - - e->stage = 0; - - obj = fz_dictgets(params, "Columns"); - if (!obj) { fz_free(e); return fz_throw("missing Columns parameter"); } - e->columns = fz_toint(obj); - - obj = fz_dictgets(params, "Rows"); - if (!obj) { fz_free(e); return fz_throw("missing Rows parameter"); } - e->rows = fz_toint(obj); - - obj = fz_dictgets(params, "Colors"); - if (!obj) { fz_free(e); return fz_throw("missing Colors parameter"); } - e->colors = fz_toint(obj); - - /* setup error callback first thing */ - myiniterr(&e->err); - e->cinfo.err = (struct jpeg_error_mgr*) &e->err; - - if (setjmp(e->err.jb)) - { - err = fz_throw("cannot encode jpeg: %s", e->err.msg); - fz_free(e); - return err; - } - - jpeg_create_compress(&e->cinfo); - - /* prepare destination manager */ - e->cinfo.dest = (struct jpeg_destination_mgr *) &e->dst; - e->dst.super.init_destination = myinitdest; - e->dst.super.empty_output_buffer = myemptybuf; - e->dst.super.term_destination = mytermdest; - - e->dst.super.next_output_byte = nil; - e->dst.super.free_in_buffer = 0; - - /* prepare required encoding options */ - e->cinfo.image_width = e->columns; - e->cinfo.image_height = e->rows; - e->cinfo.input_components = e->colors; - - switch (e->colors) { - case 1: e->cinfo.in_color_space = JCS_GRAYSCALE; break; - case 3: e->cinfo.in_color_space = JCS_RGB; break; - case 4: e->cinfo.in_color_space = JCS_CMYK; break; - default: e->cinfo.in_color_space = JCS_UNKNOWN; break; - } - - jpeg_set_defaults(&e->cinfo); - - /* FIXME check this */ - obj = fz_dictgets(params, "ColorTransform"); - if (obj) { - int colortransform = fz_toint(obj); - if (e->colors == 3 && !colortransform) - jpeg_set_colorspace(&e->cinfo, JCS_RGB); - if (e->colors == 4 && colortransform) - jpeg_set_colorspace(&e->cinfo, JCS_YCCK); - if (e->colors == 4 && !colortransform) - jpeg_set_colorspace(&e->cinfo, JCS_CMYK); - } - - obj = fz_dictgets(params, "HSamples"); - if (obj && fz_isarray(obj)) { - fz_obj *o; - for (i = 0; i < e->colors; i++) { - o = fz_arrayget(obj, i); - e->cinfo.comp_info[i].h_samp_factor = fz_toint(o); - } - } - - obj = fz_dictgets(params, "VSamples"); - if (obj && fz_isarray(obj)) { - fz_obj *o; - for (i = 0; i < e->colors; i++) { - o = fz_arrayget(obj, i); - e->cinfo.comp_info[i].v_samp_factor = fz_toint(o); - } - } - - /* TODO: quant-tables and huffman-tables */ - - return fz_okay; -} - -void -fz_dropdcte(fz_filter *filter) -{ - fz_dcte *e = (fz_dcte*)filter; - - if (setjmp(e->err.jb)) { - fz_warn("jpeg error: jpeg_destroy_compress: %s", e->err.msg); - return; - } - - jpeg_destroy_compress(&e->cinfo); -} - -/* Adobe says zigzag order. IJG > v6a says natural order. */ -#if JPEG_LIB_VERSION >= 61 -#define unzigzag(x) unzigzagorder[x] -/* zigzag array position of n'th element of natural array order */ -static const unsigned char unzigzagorder[] = -{ - 0, 1, 5, 6, 14, 15, 27, 28, - 2, 4, 7, 13, 16, 26, 29, 42, - 3, 8, 12, 17, 25, 30, 41, 43, - 9, 11, 18, 24, 31, 40, 44, 53, - 10, 19, 23, 32, 39, 45, 52, 54, - 20, 22, 33, 38, 46, 51, 55, 60, - 21, 34, 37, 47, 50, 56, 59, 61, - 35, 36, 48, 49, 57, 58, 62, 63 -}; -#else -#define unzigzag(x) (x) -#endif - -fz_error -fz_setquanttables(fz_dcte *e, unsigned int **qtables, int qfactor) -{ - int i, j; - unsigned int table[64]; - - if (setjmp(e->err.jb)) { - return fz_throw("jpeg error: jpeg_add_quant_table: %s", e->err.msg); - } - - /* TODO: check for duplicate tables */ - - for (i = 0; i < e->colors; i++) { - for (j = 0; j < 64; j++) { - table[j] = unzigzag(qtables[i][j]); - } - jpeg_add_quant_table(&e->cinfo, i, table, qfactor, TRUE); - e->cinfo.comp_info[i].quant_tbl_no = i; - } - - return fz_okay; -} - -fz_error -fz_processdcte(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_dcte *e = (fz_dcte*)filter; - JSAMPROW scanline[1]; - int stride; - int i; - - e->dst.buf = out; - e->dst.super.free_in_buffer = out->ep - out->wp; - e->dst.super.next_output_byte = out->wp; - - if (setjmp(e->err.jb)) - { - return fz_throw("cannot encode jpeg: %s", e->err.msg); - } - - switch (e->stage) - { - case 0: - /* must have enough space for markers, typically 600 bytes or so */ - if (out->wp + 1024 > out->ep) - goto needoutput; - - jpeg_start_compress(&e->cinfo, TRUE); - - /* TODO: write Adobe ColorTransform marker */ - - /* fall through */ - e->stage = 1; - - case 1: - stride = e->columns * e->colors; - - while (e->cinfo.next_scanline < e->cinfo.image_height) - { - if (in->rp + stride > in->wp) - goto needinput; - - scanline[0] = in->rp; - - i = jpeg_write_scanlines(&e->cinfo, scanline, 1); - - if (i == 0) - goto needoutput; - - in->rp += stride; - } - - /* fall through */ - e->stage = 2; - - case 2: - /* must have enough space for end markers */ - if (out->wp + 100 > out->ep) - goto needoutput; - - /* finish compress cannot suspend! */ - jpeg_finish_compress(&e->cinfo); - - e->stage = 3; - out->wp = out->ep - e->dst.super.free_in_buffer; - return fz_iodone; - } - -needinput: - out->wp = out->ep - e->dst.super.free_in_buffer; - return fz_ioneedin; - -needoutput: - out->wp = out->ep - e->dst.super.free_in_buffer; - return fz_ioneedout; -} - diff --git a/fitz/filt_faxc.h b/fitz/filt_faxc.h deleted file mode 100644 index a54ad5b6..00000000 --- a/fitz/filt_faxc.h +++ /dev/null @@ -1,125 +0,0 @@ -/* common bit magic */ - -static inline void -printbits(FILE *f, int code, int nbits) -{ - int n, b; - for (n = nbits - 1; n >= 0; n--) - { - b = (code >> n) & 1; - fprintf(f, "%c", b ? '1' : '0'); - } -} - -static inline int -getbit(const unsigned char *buf, int x) -{ - return ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1; -} - -static inline void -printline(FILE *f, unsigned char *line, int w) -{ - int i; - for (i = 0; i < w; i++) - fprintf(f, "%c", getbit(line, i) ? '#' : '.'); - fprintf(f, "\n"); -} - -static inline int -getrun(const unsigned char *line, int x, int w, int c) -{ - int z; - int b; - - if (x < 0) - x = 0; - - z = x; - while (z < w) - { - b = getbit(line, z); - if (c != b) - break; - z ++; - } - - return z - x; -} - -static inline int -findchanging(const unsigned char *line, int x, int w) -{ - int a, b; - - if (!line) - return w; - - if (x == -1) - { - a = 0; - x = 0; - } - else - { - a = getbit(line, x); - x++; - } - - while (x < w) - { - b = getbit(line, x); - if (a != b) - break; - x++; - } - - return x; -} - -static inline int -findchangingcolor(const unsigned char *line, int x, int w, int color) -{ - if (!line) - return w; - - x = findchanging(line, x, w); - - if (x < w && getbit(line, x) != color) - x = findchanging(line, x, w); - - return x; -} - -static const unsigned char lm[8] = - { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 }; - -static const unsigned char rm[8] = - { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; - -static inline void -setbits(unsigned char *line, int x0, int x1) -{ - int a0, a1, b0, b1, a; - - a0 = x0 >> 3; - a1 = x1 >> 3; - - b0 = x0 & 7; - b1 = x1 & 7; - - if (a0 == a1) - { - if (b1) - line[a0] |= lm[b0] & rm[b1]; - } - else - { - line[a0] |= lm[b0]; - for (a = a0 + 1; a < a1; a++) - line[a] = 0xFF; - if (b1) - line[a1] |= rm[b1]; - } -} - diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c index 5a0e03c1..7a5cb774 100644 --- a/fitz/filt_faxd.c +++ b/fitz/filt_faxd.c @@ -2,7 +2,131 @@ #include "fitz_stream.h" #include "filt_faxd.h" -#include "filt_faxc.h" + +/* common bit magic */ + +static inline void +printbits(FILE *f, int code, int nbits) +{ + int n, b; + for (n = nbits - 1; n >= 0; n--) + { + b = (code >> n) & 1; + fprintf(f, "%c", b ? '1' : '0'); + } +} + +static inline int +getbit(const unsigned char *buf, int x) +{ + return ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1; +} + +static inline void +printline(FILE *f, unsigned char *line, int w) +{ + int i; + for (i = 0; i < w; i++) + fprintf(f, "%c", getbit(line, i) ? '#' : '.'); + fprintf(f, "\n"); +} + +static inline int +getrun(const unsigned char *line, int x, int w, int c) +{ + int z; + int b; + + if (x < 0) + x = 0; + + z = x; + while (z < w) + { + b = getbit(line, z); + if (c != b) + break; + z ++; + } + + return z - x; +} + +static inline int +findchanging(const unsigned char *line, int x, int w) +{ + int a, b; + + if (!line) + return w; + + if (x == -1) + { + a = 0; + x = 0; + } + else + { + a = getbit(line, x); + x++; + } + + while (x < w) + { + b = getbit(line, x); + if (a != b) + break; + x++; + } + + return x; +} + +static inline int +findchangingcolor(const unsigned char *line, int x, int w, int color) +{ + if (!line) + return w; + + x = findchanging(line, x, w); + + if (x < w && getbit(line, x) != color) + x = findchanging(line, x, w); + + return x; +} + +static const unsigned char lm[8] = + { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 }; + +static const unsigned char rm[8] = + { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE }; + +static inline void +setbits(unsigned char *line, int x0, int x1) +{ + int a0, a1, b0, b1, a; + + a0 = x0 >> 3; + a1 = x1 >> 3; + + b0 = x0 & 7; + b1 = x1 & 7; + + if (a0 == a1) + { + if (b1) + line[a0] |= lm[b0] & rm[b1]; + } + else + { + line[a0] |= lm[b0]; + for (a = a0 + 1; a < a1; a++) + line[a] = 0xFF; + if (b1) + line[a1] |= rm[b1]; + } +} typedef enum fax_stage_e { diff --git a/fitz/filt_faxe.c b/fitz/filt_faxe.c deleted file mode 100644 index 9511da88..00000000 --- a/fitz/filt_faxe.c +++ /dev/null @@ -1,407 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -#include "filt_faxe.h" -#include "filt_faxc.h" - -/* TODO: honor Rows param */ - -typedef struct fz_faxe_s fz_faxe; - -struct fz_faxe_s -{ - fz_filter super; - - int k; - int endofline; - int encodedbytealign; - int columns; - int endofblock; - int blackis1; - - int stride; - int ridx; /* how many rows in total */ - int bidx; /* how many bits are already used in out->wp */ - unsigned char bsave; /* partial byte saved between process() calls */ - - int stage; - int a0, c; /* mid-line coding state */ - - unsigned char *ref; - unsigned char *src; -}; - -fz_error -fz_newfaxe(fz_filter **fp, fz_obj *params) -{ - fz_obj *obj; - - FZ_NEWFILTER(fz_faxe, fax, faxe); - - fax->ref = nil; - fax->src = nil; - - fax->k = 0; - fax->endofline = 0; - fax->encodedbytealign = 0; - fax->columns = 1728; - fax->endofblock = 1; - fax->blackis1 = 0; - - obj = fz_dictgets(params, "K"); - if (obj) fax->k = fz_toint(obj); - - obj = fz_dictgets(params, "EndOfLine"); - if (obj) fax->endofline = fz_tobool(obj); - - obj = fz_dictgets(params, "EncodedByteAlign"); - if (obj) fax->encodedbytealign = fz_tobool(obj); - - obj = fz_dictgets(params, "Columns"); - if (obj) fax->columns = fz_toint(obj); - - obj = fz_dictgets(params, "EndOfBlock"); - if (obj) fax->endofblock = fz_tobool(obj); - - obj = fz_dictgets(params, "BlackIs1"); - if (obj) fax->blackis1 = fz_tobool(obj); - - fax->stride = ((fax->columns - 1) >> 3) + 1; - fax->bidx = 0; - fax->ridx = 0; - - fax->stage = 0; - fax->a0 = -1; - fax->c = 0; - - fax->ref = fz_malloc(fax->stride); - if (!fax->ref) - { - fz_free(fax); - return fz_rethrow(-1, "out of memoryory: scanline buffer one"); - } - - fax->src = fz_malloc(fax->stride); - if (!fax->src) - { - fz_free(fax->ref); - fz_free(fax); - return fz_rethrow(-1, "out of memoryory: scanline buffer two"); - } - - memset(fax->ref, 0, fax->stride); - memset(fax->src, 0, fax->stride); - - return fz_okay; -} - -void -fz_dropfaxe(fz_filter *p) -{ - fz_faxe *fax = (fz_faxe*) p; - fz_free(fax->src); - fz_free(fax->ref); -} - -enum { codebytes = 2 }; - -static inline int runbytes(int run) -{ - int m = (run / 64) / 40 + 1; /* number of makeup codes */ - return codebytes * (m + 1); /* bytes for makeup + term codes */ -} - -static void -putbits(fz_faxe *fax, fz_buffer *out, int code, int nbits) -{ - while (nbits > 0) - { - if (fax->bidx == 0) - *out->wp = 0; - - /* code does not fit: shift right */ - if (nbits > (8 - fax->bidx)) - { - *out->wp |= code >> (nbits - (8 - fax->bidx)); - nbits = nbits - (8 - fax->bidx); - fax->bidx = 0; - out->wp ++; - } - - /* shift left */ - else - { - *out->wp |= code << ((8 - fax->bidx) - nbits); - fax->bidx += nbits; - if (fax->bidx == 8) - { - fax->bidx = 0; - out->wp ++; - } - nbits = 0; - } - } -} - -static inline void -putcode(fz_faxe *fax, fz_buffer *out, const cfe_code *run) -{ - putbits(fax, out, run->code, run->nbits); -} - -static void -putrun(fz_faxe *fax, fz_buffer *out, int run, int c) -{ - int m; - - const cf_runs *codetable = c ? &cf_black_runs : &cf_white_runs; - - if (run > 63) - { - m = run / 64; - while (m > 40) - { - putcode(fax, out, &codetable->makeup[40]); - m -= 40; - } - if (m > 0) - { - putcode(fax, out, &codetable->makeup[m]); - } - putcode(fax, out, &codetable->termination[run % 64]); - } - else - { - putcode(fax, out, &codetable->termination[run]); - } -} - -static fz_error -enc1d(fz_faxe *fax, unsigned char *line, fz_buffer *out) -{ - int run; - - if (fax->a0 < 0) - fax->a0 = 0; - - while (fax->a0 < fax->columns) - { - run = getrun(line, fax->a0, fax->columns, fax->c); - - if (out->wp + 1 + runbytes(run) > out->ep) - return fz_ioneedout; - - putrun(fax, out, run, fax->c); - - fax->a0 += run; - fax->c = !fax->c; - } - - return fz_okay; -} - -static fz_error -enc2d(fz_faxe *fax, unsigned char *ref, unsigned char *src, fz_buffer *out) -{ - int a1, a2, b1, b2; - int run1, run2, n; - - while (fax->a0 < fax->columns) - { - a1 = findchanging(src, fax->a0, fax->columns); - b1 = findchangingcolor(ref, fax->a0, fax->columns, !fax->c); - b2 = findchanging(ref, b1, fax->columns); - - /* pass */ - if (b2 < a1) - { - if (out->wp + 1 + codebytes > out->ep) - return fz_ioneedout; - - putcode(fax, out, &cf2_run_pass); - - fax->a0 = b2; - } - - /* vertical */ - else if (ABS(b1 - a1) <= 3) - { - if (out->wp + 1 + codebytes > out->ep) - return fz_ioneedout; - - putcode(fax, out, &cf2_run_vertical[b1 - a1 + 3]); - - fax->a0 = a1; - fax->c = !fax->c; - } - - /* horizontal */ - else - { - a2 = findchanging(src, a1, fax->columns); - run1 = a1 - fax->a0; - run2 = a2 - a1; - n = codebytes + runbytes(run1) + runbytes(run2); - - if (out->wp + 1 + n > out->ep) - return fz_ioneedout; - - putcode(fax, out, &cf2_run_horizontal); - putrun(fax, out, run1, fax->c); - putrun(fax, out, run2, !fax->c); - - fax->a0 = a2; - } - } - - return fz_okay; -} - -static fz_error -process(fz_faxe *fax, fz_buffer *in, fz_buffer *out) -{ - fz_error error; - int i, n; - - while (1) - { - if (in->rp == in->wp && in->eof) - goto rtc; - - switch (fax->stage) - { - case 0: - if (fax->encodedbytealign) - { - if (fax->endofline) - { - if (out->wp + 1 + 1 > out->ep) - return fz_ioneedout; - - /* make sure that EOL ends on a byte border */ - putbits(fax, out, 0, (12 - fax->bidx) & 7); - } - else - { - if (fax->bidx) - { - if (out->wp + 1 > out->ep) - return fz_ioneedout; - fax->bidx = 0; - out->wp ++; - } - } - } - - fax->stage ++; - - case 1: - if (fax->endofline) - { - if (out->wp + 1 + codebytes + 1 > out->ep) - return fz_ioneedout; - - if (fax->k > 0) - { - if (fax->ridx % fax->k == 0) - putcode(fax, out, &cf2_run_eol_1d); - else - putcode(fax, out, &cf2_run_eol_2d); - } - else - { - putcode(fax, out, &cf_run_eol); - } - } - - fax->stage ++; - - case 2: - if (in->rp + fax->stride > in->wp) - { - if (in->eof) /* XXX barf here? */ - goto rtc; - return fz_ioneedin; - } - - memcpy(fax->ref, fax->src, fax->stride); - memcpy(fax->src, in->rp, fax->stride); - - if (!fax->blackis1) - { - for (i = 0; i < fax->stride; i++) - fax->src[i] = ~fax->src[i]; - } - - in->rp += fax->stride; - - fax->c = 0; - fax->a0 = -1; - - fax->stage ++; - - case 3: - error = 0; /* to silence compiler */ - - if (fax->k < 0) - error = enc2d(fax, fax->ref, fax->src, out); - - else if (fax->k == 0) - error = enc1d(fax, fax->src, out); - - else if (fax->k > 0) - { - if (fax->ridx % fax->k == 0) - error = enc1d(fax, fax->src, out); - else - error = enc2d(fax, fax->ref, fax->src, out); - } - - if (error) - return error; /* one of fz_io* */ - - fax->ridx ++; - - fax->stage = 0; - } - } - -rtc: - if (fax->endofblock) - { - n = fax->k < 0 ? 2 : 6; - - if (out->wp + 1 + codebytes * n > out->ep) - return fz_ioneedout; - - for (i = 0; i < n; i++) - { - putcode(fax, out, &cf_run_eol); - if (fax->k > 0) - putbits(fax, out, 1, 1); - } - } - - if (fax->bidx) - out->wp ++; - - return fz_iodone; -} - -fz_error -fz_processfaxe(fz_filter *p, fz_buffer *in, fz_buffer *out) -{ - fz_faxe *fax = (fz_faxe*) p; - fz_error error; - - /* restore partial bits */ - *out->wp = fax->bsave; - - error = process(fax, in, out); - - /* save partial bits */ - fax->bsave = *out->wp; - - return error; -} - diff --git a/fitz/filt_faxe.h b/fitz/filt_faxe.h deleted file mode 100644 index dd3fc121..00000000 --- a/fitz/filt_faxe.h +++ /dev/null @@ -1,37 +0,0 @@ -/* Fax G3/G4 tables */ - -typedef struct cfe_code_s cfe_code; - -struct cfe_code_s -{ - unsigned short code; - unsigned short nbits; -}; - -typedef struct cf_runs_s { - cfe_code termination[64]; - cfe_code makeup[41]; -} cf_runs; - -/* Encoding tables */ - -/* Codes common to 1-D and 2-D encoding. */ -extern const cfe_code cf_run_eol; -extern const cf_runs cf_white_runs, cf_black_runs; -extern const cfe_code cf_uncompressed[6]; -extern const cfe_code cf_uncompressed_exit[10]; /* indexed by 2 x length of */ - -/* 1-D encoding. */ -extern const cfe_code cf1_run_uncompressed; - -/* 2-D encoding. */ -enum { cf2_run_vertical_offset = 3 }; -extern const cfe_code cf2_run_pass; -extern const cfe_code cf2_run_vertical[7]; /* indexed by b1 - a1 + offset */ -extern const cfe_code cf2_run_horizontal; -extern const cfe_code cf2_run_uncompressed; - -/* 2-D Group 3 encoding. */ -extern const cfe_code cf2_run_eol_1d; -extern const cfe_code cf2_run_eol_2d; - diff --git a/fitz/filt_faxetab.c b/fitz/filt_faxetab.c deleted file mode 100644 index 34914c2d..00000000 --- a/fitz/filt_faxetab.c +++ /dev/null @@ -1,131 +0,0 @@ -/* Tables for CCITTFaxEncode filter */ - -#include "filt_faxe.h" - -/* Define the end-of-line code. */ -const cfe_code cf_run_eol = {1, 12}; - -/* Define the 1-D code that signals uncompressed data. */ -const cfe_code cf1_run_uncompressed = {0xf, 12}; - -/* Define the 2-D run codes. */ -const cfe_code cf2_run_pass = {0x1, 4}; -const cfe_code cf2_run_vertical[7] = -{ - {0x3, 7}, - {0x3, 6}, - {0x3, 3}, - {0x1, 1}, - {0x2, 3}, - {0x2, 6}, - {0x2, 7} -}; -const cfe_code cf2_run_horizontal = {1, 3}; -const cfe_code cf2_run_uncompressed = {0xf, 10}; - -/* EOL codes for Group 3 2-D. */ -const cfe_code cf2_run_eol_1d = { (1 << 1) + 1, 12 + 1}; -const cfe_code cf2_run_eol_2d = { (1 << 1) + 0, 12 + 1}; - -/* White run codes. */ -const cf_runs cf_white_runs = -{ - /* Termination codes */ - { - {0x35, 8}, {0x7, 6}, {0x7, 4}, {0x8, 4}, - {0xb, 4}, {0xc, 4}, {0xe, 4}, {0xf, 4}, - {0x13, 5}, {0x14, 5}, {0x7, 5}, {0x8, 5}, - {0x8, 6}, {0x3, 6}, {0x34, 6}, {0x35, 6}, - {0x2a, 6}, {0x2b, 6}, {0x27, 7}, {0xc, 7}, - {0x8, 7}, {0x17, 7}, {0x3, 7}, {0x4, 7}, - {0x28, 7}, {0x2b, 7}, {0x13, 7}, {0x24, 7}, - {0x18, 7}, {0x2, 8}, {0x3, 8}, {0x1a, 8}, - {0x1b, 8}, {0x12, 8}, {0x13, 8}, {0x14, 8}, - {0x15, 8}, {0x16, 8}, {0x17, 8}, {0x28, 8}, - {0x29, 8}, {0x2a, 8}, {0x2b, 8}, {0x2c, 8}, - {0x2d, 8}, {0x4, 8}, {0x5, 8}, {0xa, 8}, - {0xb, 8}, {0x52, 8}, {0x53, 8}, {0x54, 8}, - {0x55, 8}, {0x24, 8}, {0x25, 8}, {0x58, 8}, - {0x59, 8}, {0x5a, 8}, {0x5b, 8}, {0x4a, 8}, - {0x4b, 8}, {0x32, 8}, {0x33, 8}, {0x34, 8} - }, - - /* Make-up codes */ - { - {0, 0} /* dummy */ , {0x1b, 5}, {0x12, 5}, {0x17, 6}, - {0x37, 7}, {0x36, 8}, {0x37, 8}, {0x64, 8}, - {0x65, 8}, {0x68, 8}, {0x67, 8}, {0xcc, 9}, - {0xcd, 9}, {0xd2, 9}, {0xd3, 9}, {0xd4, 9}, - {0xd5, 9}, {0xd6, 9}, {0xd7, 9}, {0xd8, 9}, - {0xd9, 9}, {0xda, 9}, {0xdb, 9}, {0x98, 9}, - {0x99, 9}, {0x9a, 9}, {0x18, 6}, {0x9b, 9}, - {0x8, 11}, {0xc, 11}, {0xd, 11}, {0x12, 12}, - {0x13, 12}, {0x14, 12}, {0x15, 12}, {0x16, 12}, - {0x17, 12}, {0x1c, 12}, {0x1d, 12}, {0x1e, 12}, - {0x1f, 12} - } -}; - -/* Black run codes. */ -const cf_runs cf_black_runs = -{ - /* Termination codes */ - { - {0x37, 10}, {0x2, 3}, {0x3, 2}, {0x2, 2}, - {0x3, 3}, {0x3, 4}, {0x2, 4}, {0x3, 5}, - {0x5, 6}, {0x4, 6}, {0x4, 7}, {0x5, 7}, - {0x7, 7}, {0x4, 8}, {0x7, 8}, {0x18, 9}, - {0x17, 10}, {0x18, 10}, {0x8, 10}, {0x67, 11}, - {0x68, 11}, {0x6c, 11}, {0x37, 11}, {0x28, 11}, - {0x17, 11}, {0x18, 11}, {0xca, 12}, {0xcb, 12}, - {0xcc, 12}, {0xcd, 12}, {0x68, 12}, {0x69, 12}, - {0x6a, 12}, {0x6b, 12}, {0xd2, 12}, {0xd3, 12}, - {0xd4, 12}, {0xd5, 12}, {0xd6, 12}, {0xd7, 12}, - {0x6c, 12}, {0x6d, 12}, {0xda, 12}, {0xdb, 12}, - {0x54, 12}, {0x55, 12}, {0x56, 12}, {0x57, 12}, - {0x64, 12}, {0x65, 12}, {0x52, 12}, {0x53, 12}, - {0x24, 12}, {0x37, 12}, {0x38, 12}, {0x27, 12}, - {0x28, 12}, {0x58, 12}, {0x59, 12}, {0x2b, 12}, - {0x2c, 12}, {0x5a, 12}, {0x66, 12}, {0x67, 12} - }, - - /* Make-up codes. */ - { - {0, 0} /* dummy */ , {0xf, 10}, {0xc8, 12}, {0xc9, 12}, - {0x5b, 12}, {0x33, 12}, {0x34, 12}, {0x35, 12}, - {0x6c, 13}, {0x6d, 13}, {0x4a, 13}, {0x4b, 13}, - {0x4c, 13}, {0x4d, 13}, {0x72, 13}, {0x73, 13}, - {0x74, 13}, {0x75, 13}, {0x76, 13}, {0x77, 13}, - {0x52, 13}, {0x53, 13}, {0x54, 13}, {0x55, 13}, - {0x5a, 13}, {0x5b, 13}, {0x64, 13}, {0x65, 13}, - {0x8, 11}, {0xc, 11}, {0xd, 11}, {0x12, 12}, - {0x13, 12}, {0x14, 12}, {0x15, 12}, {0x16, 12}, - {0x17, 12}, {0x1c, 12}, {0x1d, 12}, {0x1e, 12}, - {0x1f, 12} - } -}; - -/* Uncompressed codes. */ -const cfe_code cf_uncompressed[6] = -{ - {1, 1}, - {1, 2}, - {1, 3}, - {1, 4}, - {1, 5}, - {1, 6} -}; - -/* Uncompressed exit codes. */ -const cfe_code cf_uncompressed_exit[10] = -{ - {2, 8}, {3, 8}, - {2, 9}, {3, 9}, - {2, 10}, {3, 10}, - {2, 11}, {3, 11}, - {2, 12}, {3, 12} -}; - -/* Some C compilers insist on having executable code in every file.... */ -void scfetab_dummy(void) { } - diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c index c9e6698b..7bd2a448 100644 --- a/fitz/filt_flate.c +++ b/fitz/filt_flate.c @@ -120,99 +120,3 @@ fz_processflated(fz_filter *f, fz_buffer *in, fz_buffer *out) } } -fz_error -fz_newflatee(fz_filter **fp, fz_obj *params) -{ - fz_obj *obj; - fz_error eo; - int effort; - int zipfmt; - int ei; - - FZ_NEWFILTER(fz_flate, f, flatee); - - effort = -1; - zipfmt = 0; - - if (params) - { - obj = fz_dictgets(params, "Effort"); - if (obj) effort = fz_toint(obj); - obj = fz_dictgets(params, "ZIP"); - if (obj) zipfmt = fz_tobool(obj); - } - - f->z.zalloc = zmalloc; - f->z.zfree = zfree; - f->z.opaque = nil; - f->z.next_in = nil; - f->z.avail_in = 0; - - if (zipfmt) - ei = deflateInit2(&f->z, effort, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); - else - ei = deflateInit(&f->z, effort); - - if (ei != Z_OK) - { - eo = fz_throw("zlib error: deflateInit: %s", f->z.msg); - fz_free(f); - return eo; - } - - return fz_okay; -} - -void -fz_dropflatee(fz_filter *f) -{ - z_streamp zp = &((fz_flate*)f)->z; - int err; - - err = deflateEnd(zp); - if (err != Z_OK) - fprintf(stderr, "deflateEnd: %s", zp->msg); - - fz_free(f); -} - -fz_error -fz_processflatee(fz_filter *f, fz_buffer *in, fz_buffer *out) -{ - z_streamp zp = &((fz_flate*)f)->z; - int err; - - if (in->rp == in->wp && !in->eof) - return fz_ioneedin; - if (out->wp == out->ep) - return fz_ioneedout; - - zp->next_in = in->rp; - zp->avail_in = in->wp - in->rp; - - zp->next_out = out->wp; - zp->avail_out = out->ep - out->wp; - - err = deflate(zp, in->eof ? Z_FINISH : Z_NO_FLUSH); - - in->rp = in->wp - zp->avail_in; - out->wp = out->ep - zp->avail_out; - - if (err == Z_STREAM_END) - { - return fz_iodone; - } - else if (err == Z_OK) - { - if (in->rp == in->wp && !in->eof) - return fz_ioneedin; - if (out->wp == out->ep) - return fz_ioneedout; - return fz_ioneedin; /* hmm? */ - } - else - { - return fz_throw("zlib error: deflate: %s", zp->msg); - } -} - diff --git a/fitz/filt_jpxd.c b/fitz/filt_jpxd.c new file mode 100644 index 00000000..4f34e41a --- /dev/null +++ b/fitz/filt_jpxd.c @@ -0,0 +1,163 @@ +#include "fitz_base.h" +#include "fitz_stream.h" + +#include + +typedef struct fz_jpxd_s fz_jpxd; + +struct fz_jpxd_s +{ + fz_filter super; + opj_event_mgr_t evtmgr; + opj_dparameters_t params; + opj_dinfo_t *info; + opj_image_t *image; + int stage; + int x, y, k; +}; + +static void fz_opj_error_callback(const char *msg, void *client_data) +{ + fprintf(stderr, "openjpeg error: %s", msg); +} + +static void fz_opj_warning_callback(const char *msg, void *client_data) +{ + fprintf(stderr, "openjpeg warning: %s", msg); +} + +static void fz_opj_info_callback(const char *msg, void *client_data) +{ + /* fprintf(stdout, "openjpeg info: %s", msg); */ +} + + +fz_error +fz_newjpxd(fz_filter **fp, fz_obj *params) +{ + FZ_NEWFILTER(fz_jpxd, d, jpxd); + + d->info = nil; + d->image = nil; + d->stage = 0; + + d->x = 0; + d->y = 0; + d->k = 0; + + memset(&d->evtmgr, 0, sizeof(d->evtmgr)); + d->evtmgr.error_handler = fz_opj_error_callback; + d->evtmgr.warning_handler = fz_opj_warning_callback; + d->evtmgr.info_handler = fz_opj_info_callback; + + opj_set_default_decoder_parameters(&d->params); + + d->info = opj_create_decompress(CODEC_JP2); + if (!d->info) + return fz_throw("opj_create_decompress failed"); + + opj_set_event_mgr((opj_common_ptr)d->info, &d->evtmgr, stderr); + opj_setup_decoder(d->info, &d->params); + + return fz_okay; +} + +void +fz_dropjpxd(fz_filter *filter) +{ + fz_jpxd *d = (fz_jpxd*)filter; + if (d->image) opj_image_destroy(d->image); + if (d->info) opj_destroy_decompress(d->info); +} + +fz_error +fz_processjpxd(fz_filter *filter, fz_buffer *in, fz_buffer *out) +{ + fz_jpxd *d = (fz_jpxd*)filter; + int n, w, h, depth, sgnd; + int k, v; + + opj_cio_t *cio; + + switch (d->stage) + { + case 0: goto input; + case 1: goto decode; + case 2: goto output; + } + +input: + /* Wait until we have the entire file in the input buffer */ + if (!in->eof) + return fz_ioneedin; + + d->stage = 1; + +decode: + cio = opj_cio_open((opj_common_ptr)d->info, in->rp, in->wp - in->rp); + in->rp = in->wp; + + d->image = opj_decode(d->info, cio); + if (!d->image) + { + opj_cio_close(cio); + return fz_throw("opj_decode failed"); + } + + opj_cio_close(cio); + + d->stage = 2; + + for (k = 1; k < d->image->numcomps; k++) + { + if (d->image->comps[k].w != d->image->comps[0].w) + return fz_throw("image components have different width"); + if (d->image->comps[k].h != d->image->comps[0].h) + return fz_throw("image components have different height"); + if (d->image->comps[k].prec != d->image->comps[0].prec) + return fz_throw("image components have different precision"); + } + + { + n = d->image->numcomps; + w = d->image->comps[0].w; + h = d->image->comps[0].h; + depth = d->image->comps[0].prec; + } + +output: + n = d->image->numcomps; + w = d->image->comps[0].w; + h = d->image->comps[0].h; + depth = d->image->comps[0].prec; + sgnd = d->image->comps[0].sgnd; + + while (d->y < h) + { + while (d->x < w) + { + while (d->k < n) + { + if (out->wp == out->ep) + return fz_ioneedout; + + v = d->image->comps[d->k].data[d->y * w + d->x]; + if (sgnd) + v = v + (1 << (depth - 1)); + if (depth > 8) + v = v >> (depth - 8); + + *out->wp++ = v; + + d->k ++; + } + d->x ++; + d->k = 0; + } + d->y ++; + d->x = 0; + } + + return fz_iodone; +} + diff --git a/fitz/filt_jpxd_jas.c b/fitz/filt_jpxd_jas.c deleted file mode 100644 index dcdc18c6..00000000 --- a/fitz/filt_jpxd_jas.c +++ /dev/null @@ -1,132 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -/* TODO: bpc */ - -/* - * We use the Jasper JPEG2000 coder for now. - */ - -#include - -static char *colorspacename(jas_clrspc_t clrspc) -{ - switch (jas_clrspc_fam(clrspc)) - { - case JAS_CLRSPC_FAM_UNKNOWN: return "Unknown"; - case JAS_CLRSPC_FAM_XYZ: return "XYZ"; - case JAS_CLRSPC_FAM_LAB: return "Lab"; - case JAS_CLRSPC_FAM_GRAY: return "Gray"; - case JAS_CLRSPC_FAM_RGB: return "RGB"; - case JAS_CLRSPC_FAM_YCBCR: return "YCbCr"; - } - return "Unknown"; -} - -typedef struct fz_jpxd_s fz_jpxd; - -struct fz_jpxd_s -{ - fz_filter super; - jas_stream_t *stream; - jas_image_t *image; - int offset; - int stage; -}; - -fz_error -fz_newjpxd(fz_filter **fp, fz_obj *params) -{ - int err; - - FZ_NEWFILTER(fz_jpxd, d, jpxd); - - err = jas_init(); - if (err) - { - fz_free(d); - return fz_throw("jasper error: jas_init()"); - } - - d->stream = jas_stream_memopen(nil, 0); - if (!d->stream) - { - fz_free(d); - return fz_throw("jasper error: jas_stream_memopen()"); - } - - d->image = nil; - d->offset = 0; - d->stage = 0; - - return fz_okay; -} - -void -fz_dropjpxd(fz_filter *filter) -{ - fz_jpxd *d = (fz_jpxd*)filter; - if (d->stream) jas_stream_close(d->stream); - if (d->image) jas_image_destroy(d->image); -} - -fz_error -fz_processjpxd(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_jpxd *d = (fz_jpxd*)filter; - int n, bpc, w, h; - int i, x, y; - - switch (d->stage) - { - case 0: goto input; - case 1: goto decode; - case 2: goto output; - } - -input: - while (in->rp < in->wp) - { - n = jas_stream_write(d->stream, in->rp, in->wp - in->rp); - in->rp += n; - } - - if (!in->eof) - return fz_ioneedin; - - d->stage = 1; - -decode: - jas_stream_seek(d->stream, 0, SEEK_SET); - - d->image = jas_image_decode(d->stream, -1, nil); - if (!d->image) - return fz_throw("jasper error: jas_image_decode()"); - - d->stage = 2; - -output: - w = jas_image_width(d->image); - h = jas_image_height(d->image); - n = jas_image_numcmpts(d->image); - bpc = jas_image_cmptprec(d->image, 0); /* use precision of first component for all... */ - - while (d->offset < w * h) - { - y = d->offset / w; - x = d->offset - y * w; - - /* FIXME bpc != 8 */ - - if (out->wp + n >= out->ep) - return fz_ioneedout; - - for (i = 0; i < n; i++) - *out->wp++ = jas_image_readcmptsample(d->image, i, x, y); - - d->offset ++; - } - - return fz_iodone; -} - diff --git a/fitz/filt_jpxd_opj.c b/fitz/filt_jpxd_opj.c deleted file mode 100644 index 4f34e41a..00000000 --- a/fitz/filt_jpxd_opj.c +++ /dev/null @@ -1,163 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -#include - -typedef struct fz_jpxd_s fz_jpxd; - -struct fz_jpxd_s -{ - fz_filter super; - opj_event_mgr_t evtmgr; - opj_dparameters_t params; - opj_dinfo_t *info; - opj_image_t *image; - int stage; - int x, y, k; -}; - -static void fz_opj_error_callback(const char *msg, void *client_data) -{ - fprintf(stderr, "openjpeg error: %s", msg); -} - -static void fz_opj_warning_callback(const char *msg, void *client_data) -{ - fprintf(stderr, "openjpeg warning: %s", msg); -} - -static void fz_opj_info_callback(const char *msg, void *client_data) -{ - /* fprintf(stdout, "openjpeg info: %s", msg); */ -} - - -fz_error -fz_newjpxd(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_jpxd, d, jpxd); - - d->info = nil; - d->image = nil; - d->stage = 0; - - d->x = 0; - d->y = 0; - d->k = 0; - - memset(&d->evtmgr, 0, sizeof(d->evtmgr)); - d->evtmgr.error_handler = fz_opj_error_callback; - d->evtmgr.warning_handler = fz_opj_warning_callback; - d->evtmgr.info_handler = fz_opj_info_callback; - - opj_set_default_decoder_parameters(&d->params); - - d->info = opj_create_decompress(CODEC_JP2); - if (!d->info) - return fz_throw("opj_create_decompress failed"); - - opj_set_event_mgr((opj_common_ptr)d->info, &d->evtmgr, stderr); - opj_setup_decoder(d->info, &d->params); - - return fz_okay; -} - -void -fz_dropjpxd(fz_filter *filter) -{ - fz_jpxd *d = (fz_jpxd*)filter; - if (d->image) opj_image_destroy(d->image); - if (d->info) opj_destroy_decompress(d->info); -} - -fz_error -fz_processjpxd(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_jpxd *d = (fz_jpxd*)filter; - int n, w, h, depth, sgnd; - int k, v; - - opj_cio_t *cio; - - switch (d->stage) - { - case 0: goto input; - case 1: goto decode; - case 2: goto output; - } - -input: - /* Wait until we have the entire file in the input buffer */ - if (!in->eof) - return fz_ioneedin; - - d->stage = 1; - -decode: - cio = opj_cio_open((opj_common_ptr)d->info, in->rp, in->wp - in->rp); - in->rp = in->wp; - - d->image = opj_decode(d->info, cio); - if (!d->image) - { - opj_cio_close(cio); - return fz_throw("opj_decode failed"); - } - - opj_cio_close(cio); - - d->stage = 2; - - for (k = 1; k < d->image->numcomps; k++) - { - if (d->image->comps[k].w != d->image->comps[0].w) - return fz_throw("image components have different width"); - if (d->image->comps[k].h != d->image->comps[0].h) - return fz_throw("image components have different height"); - if (d->image->comps[k].prec != d->image->comps[0].prec) - return fz_throw("image components have different precision"); - } - - { - n = d->image->numcomps; - w = d->image->comps[0].w; - h = d->image->comps[0].h; - depth = d->image->comps[0].prec; - } - -output: - n = d->image->numcomps; - w = d->image->comps[0].w; - h = d->image->comps[0].h; - depth = d->image->comps[0].prec; - sgnd = d->image->comps[0].sgnd; - - while (d->y < h) - { - while (d->x < w) - { - while (d->k < n) - { - if (out->wp == out->ep) - return fz_ioneedout; - - v = d->image->comps[d->k].data[d->y * w + d->x]; - if (sgnd) - v = v + (1 << (depth - 1)); - if (depth > 8) - v = v >> (depth - 8); - - *out->wp++ = v; - - d->k ++; - } - d->x ++; - d->k = 0; - } - d->y ++; - d->x = 0; - } - - return fz_iodone; -} - diff --git a/fitz/filt_lzwe.c b/fitz/filt_lzwe.c deleted file mode 100644 index 6b271dfa..00000000 --- a/fitz/filt_lzwe.c +++ /dev/null @@ -1,261 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -#define noDEBUG 1 - -enum -{ - MINBITS = 9, - MAXBITS = 12, - MAXBYTES = 2, - NUMCODES = (1 << MAXBITS), - LZW_CLEAR = 256, - LZW_EOD = 257, - LZW_FIRST = 258, - HSIZE = 9001, /* 91% occupancy (???) */ - HSHIFT = (13 - 8) -}; - -typedef struct lzw_hash_s lzw_hash; - -struct lzw_hash_s -{ - int hash; - int code; -}; - -typedef struct fz_lzwe_s fz_lzwe; - -struct fz_lzwe_s -{ - fz_filter super; - - int earlychange; - - int bidx; /* partial bits used in out->wp */ - unsigned char bsave; /* partial byte saved between process() calls */ - - int resume; - int code; - int fcode; - int hcode; - - int codebits; - int oldcode; - int nextcode; - - lzw_hash table[HSIZE]; -}; - -static void -clearhash(fz_lzwe *lzw) -{ - int i; - for (i = 0; i < HSIZE; i++) - lzw->table[i].hash = -1; -} - -fz_error -fz_newlzwe(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_lzwe, lzw, lzwe); - - lzw->earlychange = 0; - - if (params) - { - fz_obj *obj; - obj = fz_dictgets(params, "EarlyChange"); - if (obj) lzw->earlychange = fz_toint(obj) != 0; - } - - lzw->bidx = 0; - lzw->bsave = 0; - - lzw->resume = 0; - lzw->code = -1; - lzw->hcode = -1; - lzw->fcode = -1; - - lzw->codebits = MINBITS; - lzw->nextcode = LZW_FIRST; - lzw->oldcode = -1; /* generates LZW_CLEAR */ - - clearhash(lzw); - - return fz_okay; -} - -void -fz_droplzwe(fz_filter *filter) -{ -} - -static void -putcode(fz_lzwe *lzw, fz_buffer *out, int code) -{ - int nbits = lzw->codebits; - - while (nbits > 0) - { - if (lzw->bidx == 0) - { - *out->wp = 0; - } - - /* code does not fit: shift right */ - if (nbits > (8 - lzw->bidx)) - { - *out->wp |= code >> (nbits - (8 - lzw->bidx)); - nbits = nbits - (8 - lzw->bidx); - lzw->bidx = 0; - out->wp ++; - } - - /* shift left */ - else - { - *out->wp |= code << ((8 - lzw->bidx) - nbits); - lzw->bidx += nbits; - if (lzw->bidx == 8) - { - lzw->bidx = 0; - out->wp ++; - } - nbits = 0; - } - } -} - - -static fz_error -compress(fz_lzwe *lzw, fz_buffer *in, fz_buffer *out) -{ - if (lzw->resume) - { - lzw->resume = 0; - goto resume; - } - - /* at start of data, output a clear code */ - if (lzw->oldcode == -1) - { - if (out->wp + 3 > out->ep) - return fz_ioneedout; - - if (in->rp + 1 > in->wp) - { - if (in->eof) - goto eof; - return fz_ioneedin; - } - - putcode(lzw, out, LZW_CLEAR); - - lzw->oldcode = *in->rp++; - } - -begin: - while (1) - { - if (in->rp + 1 > in->wp) - { - if (in->eof) - goto eof; - return fz_ioneedin; - } - - /* read character */ - lzw->code = *in->rp++; - - /* hash string + character */ - lzw->fcode = (lzw->code << MAXBITS) + lzw->oldcode; - lzw->hcode = (lzw->code << HSHIFT) ^ lzw->oldcode; - - /* primary hash */ - if (lzw->table[lzw->hcode].hash == lzw->fcode) - { - lzw->oldcode = lzw->table[lzw->hcode].code; - continue; - } - - /* secondary hash */ - if (lzw->table[lzw->hcode].hash != -1) - { - int disp = HSIZE - lzw->hcode; - if (lzw->hcode == 0) - disp = 1; - do - { - lzw->hcode = lzw->hcode - disp; - if (lzw->hcode < 0) - lzw->hcode += HSIZE; - if (lzw->table[lzw->hcode].hash == lzw->fcode) - { - lzw->oldcode = lzw->table[lzw->hcode].code; - goto begin; - } - } while (lzw->table[lzw->hcode].hash != -1); - } - -resume: - /* new entry: emit code and add to table */ - - /* reserve space for this code and an eventual CLEAR code */ - if (out->wp + 5 > out->ep) - { - lzw->resume = 1; - return fz_ioneedout; - } - - putcode(lzw, out, lzw->oldcode); - - lzw->oldcode = lzw->code; - lzw->table[lzw->hcode].code = lzw->nextcode; - lzw->table[lzw->hcode].hash = lzw->fcode; - - lzw->nextcode ++; - - /* table is full: emit clear code and reset */ - if (lzw->nextcode == NUMCODES - 1) - { - putcode(lzw, out, LZW_CLEAR); - clearhash(lzw); - lzw->nextcode = LZW_FIRST; - lzw->codebits = MINBITS; - } - - /* check if next entry will be too big for the code size */ - else if (lzw->nextcode >= (1 << lzw->codebits) - lzw->earlychange) - { - lzw->codebits ++; - } - } - -eof: - if (out->wp + 5 > out->ep) - return fz_ioneedout; - - putcode(lzw, out, lzw->oldcode); - putcode(lzw, out, LZW_EOD); - - return fz_iodone; -} - -fz_error -fz_processlzwe(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_lzwe *lzw = (fz_lzwe*)filter; - fz_error error; - - /* restore partial bits */ - *out->wp = lzw->bsave; - - error = compress(lzw, in, out); - - /* save partial bits */ - lzw->bsave = *out->wp; - - return error; -} - diff --git a/fitz/filt_null.c b/fitz/filt_null.c deleted file mode 100644 index ff19f64e..00000000 --- a/fitz/filt_null.c +++ /dev/null @@ -1,54 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -typedef struct fz_nullfilter_s fz_nullfilter; - -struct fz_nullfilter_s -{ - fz_filter super; - int len; - int cur; -}; - -fz_error -fz_newnullfilter(fz_filter **fp, int len) -{ - FZ_NEWFILTER(fz_nullfilter, f, nullfilter); - f->len = len; - f->cur = 0; - return fz_okay; -} - -void -fz_dropnullfilter(fz_filter *f) -{ -} - -fz_error -fz_processnullfilter(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_nullfilter *f = (fz_nullfilter*)filter; - int n; - - n = MIN(in->wp - in->rp, out->ep - out->wp); - if (f->len >= 0) - n = MIN(n, f->len - f->cur); - - if (n) - { - memcpy(out->wp, in->rp, n); - in->rp += n; - out->wp += n; - f->cur += n; - } - - if (f->cur == f->len) - return fz_iodone; - if (in->rp == in->wp) - return fz_ioneedin; - if (out->wp == out->ep) - return fz_ioneedout; - - return fz_throw("braindead programmer trapped in nullfilter"); -} - diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index 29416f0b..77e03799 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -19,26 +19,23 @@ struct fz_predict_s int stride; int bpp; unsigned char *ref; - - int encode; }; fz_error -fz_newpredict(fz_filter **fp, fz_obj *params, int encode) +fz_newpredictd(fz_filter **fp, fz_obj *params) { fz_obj *obj; FZ_NEWFILTER(fz_predict, p, predict); - p->encode = encode; - p->predictor = 1; p->columns = 1; p->colors = 1; p->bpc = 8; obj = fz_dictgets(params, "Predictor"); - if (obj) p->predictor = fz_toint(obj); + if (obj) + p->predictor = fz_toint(obj); if (p->predictor != 1 && p->predictor != 2 && p->predictor != 10 && p->predictor != 11 && @@ -50,13 +47,16 @@ fz_newpredict(fz_filter **fp, fz_obj *params, int encode) } obj = fz_dictgets(params, "Columns"); - if (obj) p->columns = fz_toint(obj); + if (obj) + p->columns = fz_toint(obj); obj = fz_dictgets(params, "Colors"); - if (obj) p->colors = fz_toint(obj); + if (obj) + p->colors = fz_toint(obj); obj = fz_dictgets(params, "BitsPerComponent"); - if (obj) p->bpc = fz_toint(obj); + if (obj) + p->bpc = fz_toint(obj); p->stride = (p->bpc * p->colors * p->columns + 7) / 8; p->bpp = (p->bpc * p->colors + 7) / 8; @@ -123,13 +123,13 @@ paeth(int a, int b, int c) } static inline void -none(fz_predict *p, unsigned char *in, unsigned char *out) +fz_predictnone(fz_predict *p, unsigned char *in, unsigned char *out) { memcpy(out, in, p->stride); } static void -tiff(fz_predict *p, unsigned char *in, unsigned char *out) +fz_predicttiff(fz_predict *p, unsigned char *in, unsigned char *out) { int left[MAXC]; int i, k; @@ -142,16 +142,16 @@ tiff(fz_predict *p, unsigned char *in, unsigned char *out) for (k = 0; k < p->colors; k++) { int a = getcomponent(in, i * p->colors + k, p->bpc); - int b = p->encode ? a - left[k] : a + left[k]; + int b = a + left[k]; int c = b % (1 << p->bpc); putcomponent(out, i * p->colors + k, p->bpc, c); - left[k] = p->encode ? a : c; + left[k] = c; } } } static void -png(fz_predict *p, unsigned char *in, unsigned char *out, int predictor) +fz_predictpng(fz_predict *p, unsigned char *in, unsigned char *out, int predictor) { int upleft[MAXC], left[MAXC], i, k; @@ -161,38 +161,18 @@ png(fz_predict *p, unsigned char *in, unsigned char *out, int predictor) upleft[k] = 0; } - if (p->encode) - { - for (k = 0, i = 0; i < p->stride; k = (k + 1) % p->bpp, i ++) - { - switch (predictor) - { - case 0: out[i] = in[i]; break; - case 1: out[i] = in[i] - left[k]; break; - case 2: out[i] = in[i] - p->ref[i]; break; - case 3: out[i] = in[i] - (left[k] + p->ref[i]) / 2; break; - case 4: out[i] = in[i] - paeth(left[k], p->ref[i], upleft[k]); break; - } - left[k] = in[i]; - upleft[k] = p->ref[i]; - } - } - - else + for (k = 0, i = 0; i < p->stride; k = (k + 1) % p->bpp, i ++) { - for (k = 0, i = 0; i < p->stride; k = (k + 1) % p->bpp, i ++) + switch (predictor) { - switch (predictor) - { case 0: out[i] = in[i]; break; case 1: out[i] = in[i] + left[k]; break; case 2: out[i] = in[i] + p->ref[i]; break; case 3: out[i] = in[i] + (left[k] + p->ref[i]) / 2; break; case 4: out[i] = in[i] + paeth(left[k], p->ref[i], upleft[k]); break; - } - left[k] = out[i]; - upleft[k] = p->ref[i]; } + left[k] = out[i]; + upleft[k] = p->ref[i]; } } @@ -205,40 +185,30 @@ fz_processpredict(fz_filter *filter, fz_buffer *in, fz_buffer *out) while (1) { - if (in->rp + dec->stride + (!dec->encode && ispng) > in->wp) + if (in->rp + dec->stride + ispng > in->wp) { if (in->eof) return fz_iodone; return fz_ioneedin; } - if (out->wp + dec->stride + (dec->encode && ispng) > out->ep) + if (out->wp + dec->stride > out->ep) return fz_ioneedout; if (dec->predictor == 1) { - none(dec, in->rp, out->wp); + fz_predictnone(dec, in->rp, out->wp); } else if (dec->predictor == 2) { if (dec->bpc != 8) memset(out->wp, 0, dec->stride); - tiff(dec, in->rp, out->wp); + fz_predicttiff(dec, in->rp, out->wp); } else { - if (dec->encode) - { - predictor = dec->predictor - 10; - if (predictor < 0 || predictor > 4) - predictor = 1; - *out->wp ++ = predictor; - } - else - { - predictor = *in->rp++; - } - png(dec, in->rp, out->wp, predictor); + predictor = *in->rp++; + fz_predictpng(dec, in->rp, out->wp, predictor); } if (dec->ref) @@ -249,15 +219,3 @@ fz_processpredict(fz_filter *filter, fz_buffer *in, fz_buffer *out) } } -fz_error -fz_newpredictd(fz_filter **fp, fz_obj *params) -{ - return fz_newpredict(fp, params, 0); -} - -fz_error -fz_newpredicte(fz_filter **fp, fz_obj *params) -{ - return fz_newpredict(fp, params, 1); -} - diff --git a/fitz/filt_rld.c b/fitz/filt_rld.c deleted file mode 100644 index 8f3b842f..00000000 --- a/fitz/filt_rld.c +++ /dev/null @@ -1,73 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -fz_error -fz_newrld(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_filter, f, rld); - return fz_okay; -} - -void -fz_droprld(fz_filter *rld) -{ -} - -fz_error -fz_processrld(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - int run, i; - unsigned char c; - - while (1) - { - if (in->rp == in->wp) - { - if (in->eof) - { - return fz_iodone; - } - return fz_ioneedin; - } - - if (out->wp == out->ep) - return fz_ioneedout; - - run = *in->rp++; - - if (run == 128) - { - return fz_iodone; - } - - else if (run < 128) { - run = run + 1; - if (in->rp + run > in->wp) { - in->rp --; - return fz_ioneedin; - } - if (out->wp + run > out->ep) { - in->rp --; - return fz_ioneedout; - } - for (i = 0; i < run; i++) - *out->wp++ = *in->rp++; - } - - else if (run > 128) { - run = 257 - run; - if (in->rp + 1 > in->wp) { - in->rp --; - return fz_ioneedin; - } - if (out->wp + run > out->ep) { - in->rp --; - return fz_ioneedout; - } - c = *in->rp++; - for (i = 0; i < run; i++) - *out->wp++ = c; - } - } -} - diff --git a/fitz/filt_rle.c b/fitz/filt_rle.c deleted file mode 100644 index 65c6c0e8..00000000 --- a/fitz/filt_rle.c +++ /dev/null @@ -1,238 +0,0 @@ -#include "fitz_base.h" -#include "fitz_stream.h" - -/* TODO: rewrite! - * make it non-optimal or something, - * just not this horrid mess... - */ - -#define noDEBUG - -typedef struct fz_rle_s fz_rle; - -struct fz_rle_s -{ - fz_filter super; - int reclen; - int curlen; - int state; - int run; - unsigned char buf[128]; -}; - -enum { - ZERO, - ONE, - DIFF, - SAME, - END -}; - -fz_error -fz_newrle(fz_filter **fp, fz_obj *params) -{ - FZ_NEWFILTER(fz_rle, enc, rle); - - if (params) - enc->reclen = fz_toint(params); - else - enc->reclen = 0; - - enc->curlen = 0; - enc->state = ZERO; - enc->run = 0; - - return fz_okay; -} - -void -fz_droprle(fz_filter *enc) -{ -} - -static fz_error -putone(fz_rle *enc, fz_buffer *in, fz_buffer *out) -{ - if (out->wp + 2 >= out->ep) - return fz_ioneedout; - -#ifdef DEBUG -fprintf(stderr, "one '%c'\n", enc->buf[0]); -#endif - - *out->wp++ = 0; - *out->wp++ = enc->buf[0]; - - return fz_okay; -} - -static fz_error -putsame(fz_rle *enc, fz_buffer *in, fz_buffer *out) -{ - if (out->wp + enc->run >= out->ep) - return fz_ioneedout; - -#ifdef DEBUG -fprintf(stderr, "same %d x '%c'\n", enc->run, enc->buf[0]); -#endif - - *out->wp++ = 257 - enc->run; - *out->wp++ = enc->buf[0]; - return fz_okay; -} - -static fz_error -putdiff(fz_rle *enc, fz_buffer *in, fz_buffer *out) -{ - int i; - if (out->wp + enc->run >= out->ep) - return fz_ioneedout; - -#ifdef DEBUG -fprintf(stderr, "diff %d\n", enc->run); -#endif - - *out->wp++ = enc->run - 1; - for (i = 0; i < enc->run; i++) - *out->wp++ = enc->buf[i]; - return fz_okay; -} - -static fz_error -puteod(fz_rle *enc, fz_buffer *in, fz_buffer *out) -{ - if (out->wp + 1 >= out->ep) - return fz_ioneedout; - -#ifdef DEBUG -fprintf(stderr, "eod\n"); -#endif - - *out->wp++ = 128; - return fz_okay; -} - -static fz_error -savebuf(fz_rle *enc, fz_buffer *in, fz_buffer *out) -{ - switch (enc->state) - { - case ZERO: return fz_okay; - case ONE: return putone(enc, in, out); - case SAME: return putsame(enc, in, out); - case DIFF: return putdiff(enc, in, out); - case END: return puteod(enc, in, out); - default: assert(!"invalid state in rle"); return fz_okay; - } -} - -fz_error -fz_processrle(fz_filter *filter, fz_buffer *in, fz_buffer *out) -{ - fz_rle *enc = (fz_rle*)filter; - fz_error error; - unsigned char c; - - while (1) - { - - if (enc->reclen && enc->curlen == enc->reclen) { - error = savebuf(enc, in, out); - if (error) return error; -#ifdef DEBUG -fprintf(stderr, "--record--\n"); -#endif - enc->state = ZERO; - enc->curlen = 0; - } - - if (in->rp == in->wp) { - if (in->eof) { - if (enc->state != END) { - error = savebuf(enc, in, out); - if (error) return error; - } - enc->state = END; - } - else - return fz_ioneedin; - } - - c = *in->rp; - - switch (enc->state) - { - case ZERO: - enc->state = ONE; - enc->run = 1; - enc->buf[0] = c; - break; - - case ONE: - enc->state = DIFF; - enc->run = 2; - enc->buf[1] = c; - break; - - case DIFF: - /* out of space */ - if (enc->run == 128) { - error = putdiff(enc, in, out); - if (error) return error; - - enc->state = ONE; - enc->run = 1; - enc->buf[0] = c; - } - - /* run of three that are the same */ - else if ((enc->run > 1) && - (c == enc->buf[enc->run - 1]) && - (c == enc->buf[enc->run - 2])) - { - if (enc->run >= 3) { - enc->run -= 2; /* skip prev two for diff run */ - error = putdiff(enc, in, out); - if (error) return error; - } - - enc->state = SAME; - enc->run = 3; - enc->buf[0] = c; - } - - /* keep on collecting */ - else { - enc->buf[enc->run++] = c; - } - break; - - case SAME: - if (enc->run == 128 || c != enc->buf[0]) { - error = putsame(enc, in, out); - if (error) return error; - - enc->state = ONE; - enc->run = 1; - enc->buf[0] = c; - } - else { - enc->run ++; - } - break; - - case END: - error = puteod(enc, in, out); - if (error) - return error; - - return fz_iodone; - } - - in->rp ++; - - enc->curlen ++; - - } -} - diff --git a/fitz/fitz_stream.h b/fitz/fitz_stream.h index ba0be332..30036f47 100644 --- a/fitz/fitz_stream.h +++ b/fitz/fitz_stream.h @@ -252,21 +252,13 @@ fz_error fz_newcopyfilter(fz_filter **fp); fz_error fz_newarc4filter(fz_filter **fp, unsigned char *key, unsigned keylen); fz_error fz_newaesdfilter(fz_filter **fp, unsigned char *key, unsigned keylen); fz_error fz_newa85d(fz_filter **filterp, fz_obj *param); -fz_error fz_newa85e(fz_filter **filterp, fz_obj *param); fz_error fz_newahxd(fz_filter **filterp, fz_obj *param); -fz_error fz_newahxe(fz_filter **filterp, fz_obj *param); fz_error fz_newrld(fz_filter **filterp, fz_obj *param); -fz_error fz_newrle(fz_filter **filterp, fz_obj *param); fz_error fz_newdctd(fz_filter **filterp, fz_obj *param); -fz_error fz_newdcte(fz_filter **filterp, fz_obj *param); fz_error fz_newfaxd(fz_filter **filterp, fz_obj *param); -fz_error fz_newfaxe(fz_filter **filterp, fz_obj *param); fz_error fz_newflated(fz_filter **filterp, fz_obj *param); -fz_error fz_newflatee(fz_filter **filterp, fz_obj *param); fz_error fz_newlzwd(fz_filter **filterp, fz_obj *param); -fz_error fz_newlzwe(fz_filter **filterp, fz_obj *param); fz_error fz_newpredictd(fz_filter **filterp, fz_obj *param); -fz_error fz_newpredicte(fz_filter **filterp, fz_obj *param); fz_error fz_newjbig2d(fz_filter **filterp, fz_obj *param); fz_error fz_newjpxd(fz_filter **filterp, fz_obj *param); -- cgit v1.2.3