Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include "fitz.h"
  2.  
  3. #ifdef _WIN32 /* Microsoft Visual C++ */
  4.  
  5. typedef signed char int8_t;
  6. typedef short int int16_t;
  7. typedef int int32_t;
  8. typedef __int64 int64_t;
  9.  
  10. typedef unsigned char uint8_t;
  11. typedef unsigned short int uint16_t;
  12. typedef unsigned int uint32_t;
  13.  
  14. #else
  15. #include <inttypes.h>
  16. #endif
  17.  
  18. #include <jbig2.h>
  19.  
  20. typedef struct fz_jbig2d_s fz_jbig2d;
  21.  
  22. struct fz_jbig2d_s
  23. {
  24.         fz_stream *chain;
  25.         Jbig2Ctx *ctx;
  26.         Jbig2GlobalCtx *gctx;
  27.         Jbig2Image *page;
  28.         int idx;
  29. };
  30.  
  31. static void
  32. close_jbig2d(fz_stream *stm)
  33. {
  34.         fz_jbig2d *state = stm->state;
  35.         if (state->page)
  36.                 jbig2_release_page(state->ctx, state->page);
  37.         if (state->gctx)
  38.                 jbig2_global_ctx_free(state->gctx);
  39.         jbig2_ctx_free(state->ctx);
  40.         fz_close(state->chain);
  41.         fz_free(state);
  42. }
  43.  
  44. static int
  45. read_jbig2d(fz_stream *stm, unsigned char *buf, int len)
  46. {
  47.         fz_jbig2d *state = stm->state;
  48.         unsigned char tmp[4096];
  49.         unsigned char *p = buf;
  50.         unsigned char *ep = buf + len;
  51.         unsigned char *s;
  52.         int x, w, n;
  53.  
  54.         if (!state->page)
  55.         {
  56.                 while (1)
  57.                 {
  58.                         n = fz_read(state->chain, tmp, sizeof tmp);
  59.                         if (n < 0)
  60.                                 return fz_rethrow(n, "read error in jbig2 filter");
  61.                         if (n == 0)
  62.                                 break;
  63.                         jbig2_data_in(state->ctx, tmp, n);
  64.                 }
  65.  
  66.                 jbig2_complete_page(state->ctx);
  67.  
  68.                 state->page = jbig2_page_out(state->ctx);
  69.                 if (!state->page)
  70.                         return fz_throw("jbig2_page_out failed");
  71.         }
  72.  
  73.         s = state->page->data;
  74.         w = state->page->height * state->page->stride;
  75.         x = state->idx;
  76.         while (p < ep && x < w)
  77.                 *p++ = s[x++] ^ 0xff;
  78.         state->idx = x;
  79.  
  80.         return p - buf;
  81. }
  82.  
  83. fz_stream *
  84. fz_open_jbig2d(fz_stream *chain, fz_buffer *globals)
  85. {
  86.         fz_jbig2d *state;
  87.  
  88.         state = fz_malloc(sizeof(fz_jbig2d));
  89.         state->chain = chain;
  90.         state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, NULL, NULL);
  91.         state->gctx = NULL;
  92.         state->page = NULL;
  93.         state->idx = 0;
  94.  
  95.         if (globals)
  96.         {
  97.                 jbig2_data_in(state->ctx, globals->data, globals->len);
  98.                 state->gctx = jbig2_make_global_ctx(state->ctx);
  99.                 state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, state->gctx, NULL, NULL);
  100.         }
  101.  
  102.         return fz_new_stream(state, read_jbig2d, close_jbig2d);
  103. }
  104.