Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.     jbig2dec
  3.  
  4.     Copyright (C) 2004 Artifex Software, Inc.
  5.  
  6.     This software is provided AS-IS with no warranty,
  7.     either express or implied.
  8.  
  9.     This software is distributed under license and may not
  10.     be copied, modified or distributed except as expressly
  11.     authorized under the terms of the license contained in
  12.     the file LICENSE in this distribution.
  13.  
  14.     For further licensing information refer to http://artifex.com/ or
  15.     contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
  16.     San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  17. */
  18.  
  19. /**
  20.  * Generic Refinement region handlers.
  21.  **/
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "os_types.h"
  27.  
  28. #include <stddef.h>
  29. #include <string.h> /* memcpy(), memset() */
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "jbig2.h"
  34. #include "jbig2_priv.h"
  35. #include "jbig2_arith.h"
  36. #include "jbig2_generic.h"
  37. #include "jbig2_image.h"
  38.  
  39. static int
  40. jbig2_decode_refinement_template0(Jbig2Ctx *ctx,
  41.                               Jbig2Segment *segment,
  42.                               const Jbig2RefinementRegionParams *params,
  43.                               Jbig2ArithState *as,
  44.                               Jbig2Image *image,
  45.                               Jbig2ArithCx *GR_stats)
  46. {
  47.   return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  48.     "refinement region template 0 NYI");
  49. }
  50.  
  51. static int
  52. jbig2_decode_refinement_template0_unopt(Jbig2Ctx *ctx,
  53.                               Jbig2Segment *segment,
  54.                               const Jbig2RefinementRegionParams *params,
  55.                               Jbig2ArithState *as,
  56.                               Jbig2Image *image,
  57.                               Jbig2ArithCx *GR_stats)
  58. {
  59.   const int GRW = image->width;
  60.   const int GRH = image->height;
  61.   const int dx = params->DX;
  62.   const int dy = params->DY;
  63.   Jbig2Image *ref = params->reference;
  64.   uint32_t CONTEXT;
  65.   int x,y;
  66.   bool bit;
  67.  
  68.   for (y = 0; y < GRH; y++) {
  69.     for (x = 0; x < GRW; x++) {
  70.       CONTEXT = 0;
  71.       CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  72.       CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  73.       CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  74.       CONTEXT |= jbig2_image_get_pixel(image, x + params->grat[0],
  75.         y + params->grat[1]) << 3;
  76.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+1) << 4;
  77.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+1) << 5;
  78.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+1) << 6;
  79.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+0) << 7;
  80.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+0) << 8;
  81.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+0) << 9;
  82.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy-1) << 10;
  83.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy-1) << 11;
  84.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+params->grat[2],
  85.         y-dy+params->grat[3]) << 12;
  86.       bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
  87.       jbig2_image_set_pixel(image, x, y, bit);
  88.     }
  89.   }
  90. #ifdef JBIG2_DEBUG_DUMP
  91.   {
  92.     static count = 0;
  93.     char name[32];
  94.     snprintf(name, 32, "refin-%d.pbm", count);
  95.     jbig2_image_write_pbm_file(ref, name);
  96.     snprintf(name, 32, "refout-%d.pbm", count);
  97.     jbig2_image_write_pbm_file(image, name);
  98.     count++;
  99.   }
  100. #endif
  101.  
  102.   return 0;
  103. }
  104.  
  105.  
  106. static int
  107. jbig2_decode_refinement_template1_unopt(Jbig2Ctx *ctx,
  108.                               Jbig2Segment *segment,
  109.                               const Jbig2RefinementRegionParams *params,
  110.                               Jbig2ArithState *as,
  111.                               Jbig2Image *image,
  112.                               Jbig2ArithCx *GR_stats)
  113. {
  114.   const int GRW = image->width;
  115.   const int GRH = image->height;
  116.   const int dx = params->DX;
  117.   const int dy = params->DY;
  118.   Jbig2Image *ref = params->reference;
  119.   uint32_t CONTEXT;
  120.   int x,y;
  121.   bool bit;
  122.  
  123.   for (y = 0; y < GRH; y++) {
  124.     for (x = 0; x < GRW; x++) {
  125.       CONTEXT = 0;
  126.       CONTEXT |= jbig2_image_get_pixel(image, x - 1, y + 0) << 0;
  127.       CONTEXT |= jbig2_image_get_pixel(image, x + 1, y - 1) << 1;
  128.       CONTEXT |= jbig2_image_get_pixel(image, x + 0, y - 1) << 2;
  129.       CONTEXT |= jbig2_image_get_pixel(image, x - 1, y - 1) << 3;
  130.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+1) << 4;
  131.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+1) << 5;
  132.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+1, y-dy+0) << 6;
  133.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy+0) << 7;
  134.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx-1, y-dy+0) << 8;
  135.       CONTEXT |= jbig2_image_get_pixel(ref, x-dx+0, y-dy-1) << 9;
  136.       bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
  137.       jbig2_image_set_pixel(image, x, y, bit);
  138.     }
  139.   }
  140.  
  141. #ifdef JBIG2_DEBUG_DUMP
  142.   {
  143.     static count = 0;
  144.     char name[32];
  145.     snprintf(name, 32, "refin-%d.pbm", count);
  146.     jbig2_image_write_pbm_file(ref, name);
  147.     snprintf(name, 32, "refout-%d.pbm", count);
  148.     jbig2_image_write_pbm_file(image, name);
  149.     count++;
  150.   }
  151. #endif
  152.  
  153.   return 0;
  154. }
  155.  
  156. static int
  157. jbig2_decode_refinement_template1(Jbig2Ctx *ctx,
  158.                               Jbig2Segment *segment,
  159.                               const Jbig2RefinementRegionParams *params,
  160.                               Jbig2ArithState *as,
  161.                               Jbig2Image *image,
  162.                               Jbig2ArithCx *GR_stats)
  163. {
  164.   const int GRW = image->width;
  165.   const int GRH = image->height;
  166.   const int stride = image->stride;
  167.   const int refstride = params->reference->stride;
  168.   const int dy = params->DY;
  169.   byte *grreg_line = (byte *)image->data;
  170.   byte *grref_line = (byte *)params->reference->data;
  171.   int x,y;
  172.  
  173.   for (y = 0; y < GRH; y++) {
  174.     const int padded_width = (GRW + 7) & -8;
  175.     uint32_t CONTEXT;
  176.     uint32_t refline_m1; /* previous line of the reference bitmap */
  177.     uint32_t refline_0;  /* current line of the reference bitmap */
  178.     uint32_t refline_1;  /* next line of the reference bitmap */
  179.     uint32_t line_m1;    /* previous line of the decoded bitmap */
  180.  
  181.     line_m1 = (y >= 1) ? grreg_line[-stride] : 0;
  182.     refline_m1 = ((y-dy) >= 1) ? grref_line[(-1-dy)*stride] << 2: 0;
  183.     refline_0  = (((y-dy) > 0) && ((y-dy) < GRH)) ? grref_line[(0-dy)*stride] << 4 : 0;
  184.     refline_1  = (y < GRH - 1) ? grref_line[(+1-dy)*stride] << 7 : 0;
  185.     CONTEXT = ((line_m1 >> 5) & 0x00e) |
  186.               ((refline_1 >> 5) & 0x030) |
  187.               ((refline_0 >> 5) & 0x1c0) |
  188.               ((refline_m1 >> 5) & 0x200);
  189.  
  190.     for (x = 0; x < padded_width; x += 8) {
  191.       byte result = 0;
  192.       int x_minor;
  193.       const int minor_width = GRW - x > 8 ? 8 : GRW - x;
  194.  
  195.       if (y >= 1) {
  196.         line_m1 = (line_m1 << 8) |
  197.           (x + 8 < GRW ? grreg_line[-stride + (x >> 3) + 1] : 0);
  198.         refline_m1 = (refline_m1 << 8) |
  199.           (x + 8 < GRW ? grref_line[-refstride + (x >> 3) + 1] << 2 : 0);
  200.       }
  201.  
  202.       refline_0 = (refline_0 << 8) |
  203.           (x + 8 < GRW ? grref_line[(x >> 3) + 1] << 4 : 0);
  204.  
  205.       if (y < GRH - 1)
  206.         refline_1 = (refline_1 << 8) |
  207.           (x + 8 < GRW ? grref_line[+refstride + (x >> 3) + 1] << 7 : 0);
  208.       else
  209.         refline_1 = 0;
  210.  
  211.       /* this is the speed critical inner-loop */
  212.       for (x_minor = 0; x_minor < minor_width; x_minor++) {
  213.         bool bit;
  214.  
  215.         bit = jbig2_arith_decode(as, &GR_stats[CONTEXT]);
  216.         result |= bit << (7 - x_minor);
  217.         CONTEXT = ((CONTEXT & 0x0d6) << 1) | bit |
  218.           ((line_m1 >> (9 - x_minor)) & 0x002) |
  219.           ((refline_1 >> (9 - x_minor)) & 0x010) |
  220.           ((refline_0 >> (9 - x_minor)) & 0x040) |
  221.           ((refline_m1 >> (9 - x_minor)) & 0x200);
  222.       }
  223.  
  224.       grreg_line[x>>3] = result;
  225.  
  226.     }
  227.  
  228.     grreg_line += stride;
  229.     grref_line += refstride;
  230.  
  231.   }
  232.  
  233.   return 0;
  234.  
  235. }
  236.  
  237.  
  238. /**
  239.  * jbig2_decode_refinement_region: Decode a generic refinement region.
  240.  * @ctx: The context for allocation and error reporting.
  241.  * @segment: A segment reference for error reporting.
  242.  * @params: Decoding parameter set.
  243.  * @as: Arithmetic decoder state.
  244.  * @image: Where to store the decoded image.
  245.  * @GR_stats: Arithmetic stats.
  246.  *
  247.  * Decodes a generic refinement region, according to section 6.3.
  248.  * an already allocated Jbig2Image object in @image for the result.
  249.  *
  250.  * Because this API is based on an arithmetic decoding state, it is
  251.  * not suitable for MMR decoding.
  252.  *
  253.  * Return code: 0 on success.
  254.  **/
  255. int
  256. jbig2_decode_refinement_region(Jbig2Ctx *ctx,
  257.                             Jbig2Segment *segment,
  258.                             const Jbig2RefinementRegionParams *params,
  259.                             Jbig2ArithState *as,
  260.                             Jbig2Image *image,
  261.                             Jbig2ArithCx *GR_stats)
  262. {
  263.   {
  264.     jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  265.       "decoding generic refinement region with offset %d,%x,\n"
  266.       "  GRTEMPLATE=%d, TPGRON=%d, RA1=(%d,%d) RA2=(%d,%d)\n",
  267.       params->DX, params->DY, params->GRTEMPLATE, params->TPGRON,
  268.       params->grat[0], params->grat[1], params->grat[2], params->grat[3]);
  269.   }
  270.   if (params->TPGRON)
  271.     return jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  272.         "decode_refinement_region: typical prediction coding NYI");
  273.   if (params->GRTEMPLATE)
  274.     return jbig2_decode_refinement_template1_unopt(ctx, segment, params,
  275.                                              as, image, GR_stats);
  276.   else
  277.     return jbig2_decode_refinement_template0_unopt(ctx, segment, params,
  278.                                              as, image, GR_stats);
  279. }
  280.  
  281. /**
  282.  * Find the first referred-to intermediate region segment
  283.  * with a non-NULL result for use as a reference image
  284.  */
  285. Jbig2Segment *
  286. jbig2_region_find_referred(Jbig2Ctx *ctx,Jbig2Segment *segment)
  287. {
  288.   const int nsegments = segment->referred_to_segment_count;
  289.   Jbig2Segment *rsegment;
  290.   int index;
  291.  
  292.   for (index = 0; index < nsegments; index++) {
  293.     rsegment = jbig2_find_segment(ctx,
  294.       segment->referred_to_segments[index]);
  295.     if (rsegment == NULL) {
  296.       jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  297.         "could not find referred to segment %d",
  298.         segment->referred_to_segments[index]);
  299.       continue;
  300.     }
  301.     switch (rsegment->flags & 63) {
  302.       case 4:  /* intermediate text region */
  303.       case 20: /* intermediate halftone region */
  304.       case 36: /* intermediate generic region */
  305.       case 40: /* intermediate generic refinement region */
  306.         if (rsegment->result) return rsegment;
  307.         break;
  308.       default: /* keep looking */
  309.         break;
  310.     }
  311.   }
  312.   /* no appropriate reference was found. */
  313.   return NULL;
  314. }
  315.  
  316. /**
  317.  * Handler for generic refinement region segments
  318.  */
  319. int
  320. jbig2_refinement_region(Jbig2Ctx *ctx, Jbig2Segment *segment,
  321.                                const byte *segment_data)
  322. {
  323.   Jbig2RefinementRegionParams params;
  324.   Jbig2RegionSegmentInfo rsi;
  325.   int offset = 0;
  326.   byte seg_flags;
  327.  
  328.   /* 7.4.7 */
  329.   if (segment->data_length < 18)
  330.     return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  331.                        "Segment too short");
  332.  
  333.   jbig2_get_region_segment_info(&rsi, segment_data);
  334.   jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  335.               "generic region: %d x %d @ (%d, %d), flags = %02x",
  336.               rsi.width, rsi.height, rsi.x, rsi.y, rsi.flags);
  337.  
  338.   /* 7.4.7.2 */
  339.   seg_flags = segment_data[17];
  340.   params.GRTEMPLATE = seg_flags & 0x01;
  341.   params.TPGRON = seg_flags & 0x02 ? 1 : 0;
  342.   jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  343.               "segment flags = %02x %s%s", seg_flags,
  344.               params.GRTEMPLATE ? " GRTEMPLATE" :"",
  345.               params.TPGRON ? " TPGRON" : "" );
  346.   if (seg_flags & 0xFC)
  347.     jbig2_error(ctx, JBIG2_SEVERITY_WARNING, segment->number,
  348.                 "reserved segment flag bits are non-zero");
  349.   offset += 18;
  350.  
  351.   /* 7.4.7.3 */
  352.   if (!params.GRTEMPLATE) {
  353.     if (segment->data_length < 22)
  354.       return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  355.                          "Segment too short");
  356.     params.grat[0] = segment_data[offset + 0];
  357.     params.grat[1] = segment_data[offset + 1];
  358.     params.grat[2] = segment_data[offset + 2];
  359.     params.grat[3] = segment_data[offset + 3];
  360.     jbig2_error(ctx, JBIG2_SEVERITY_INFO, segment->number,
  361.                    "grat1: (%d, %d) grat2: (%d, %d)",
  362.                    params.grat[0], params.grat[1],
  363.                    params.grat[2], params.grat[3]);
  364.     offset += 4;
  365.   }
  366.  
  367.   /* 7.4.7.4 - set up the reference image */
  368.   if (segment->referred_to_segment_count) {
  369.     Jbig2Segment *ref;
  370.  
  371.     ref = jbig2_region_find_referred(ctx, segment);
  372.     if (ref == NULL)
  373.       return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  374.         "could not find reference bitmap!");
  375.     /* the reference bitmap is the result of a previous
  376.        intermediate region segment; the reference selection
  377.        rules say to use the first one available, and not to
  378.        reuse any intermediate result, so we simply clone it
  379.        and free the original to keep track of this. */
  380.     params.reference = jbig2_image_clone(ctx, ref->result);
  381.     jbig2_image_release(ctx, ref->result);
  382.     ref->result = NULL;
  383.     jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  384.       "found reference bitmap in segment %d", ref->number);
  385.   } else {
  386.     /* the reference is just (a subset of) the page buffer */
  387.     params.reference = jbig2_image_clone(ctx,
  388.       ctx->pages[ctx->current_page].image);
  389.     /* TODO: subset the image if appropriate */
  390.   }
  391.  
  392.   /* 7.4.7.5 */
  393.   params.DX = 0;
  394.   params.DY = 0;
  395.   {
  396.     Jbig2WordStream *ws;
  397.     Jbig2ArithState *as;
  398.     Jbig2ArithCx *GR_stats = NULL;
  399.     int stats_size;
  400.     Jbig2Image *image;
  401.     int code;
  402.  
  403.     image = jbig2_image_new(ctx, rsi.width, rsi.height);
  404.     if (image == NULL)
  405.       return jbig2_error(ctx, JBIG2_SEVERITY_FATAL, segment->number,
  406.                "unable to allocate refinement image");
  407.     jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  408.       "allocated %d x %d image buffer for region decode results",
  409.           rsi.width, rsi.height);
  410.  
  411.     stats_size = params.GRTEMPLATE ? 1 << 10 : 1 << 13;
  412.     GR_stats = jbig2_alloc(ctx->allocator, stats_size);
  413.     memset(GR_stats, 0, stats_size);
  414.  
  415.     ws = jbig2_word_stream_buf_new(ctx, segment_data + offset,
  416.            segment->data_length - offset);
  417.     as = jbig2_arith_new(ctx, ws);
  418.     code = jbig2_decode_refinement_region(ctx, segment, &params,
  419.                               as, image, GR_stats);
  420.  
  421.     jbig2_free(ctx->allocator, as);
  422.     jbig2_word_stream_buf_free(ctx, ws);
  423.     jbig2_free(ctx->allocator, GR_stats);
  424.  
  425.     if ((segment->flags & 63) == 40) {
  426.         /* intermediate region. save the result for later */
  427.         segment->result = image;
  428.     } else {
  429.         /* immediate region. composite onto the page */
  430.         jbig2_error(ctx, JBIG2_SEVERITY_DEBUG, segment->number,
  431.             "composing %dx%d decoded refinement region onto page at (%d, %d)",
  432.             rsi.width, rsi.height, rsi.x, rsi.y);
  433.         jbig2_page_add_result(ctx, &ctx->pages[ctx->current_page],
  434.           image, rsi.x, rsi.y, rsi.op);
  435.         jbig2_image_release(ctx, image);
  436.     }
  437.   }
  438.  
  439.   return 0;
  440. }
  441.