1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
4 This software is provided AS-IS with no warranty, either express or
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134, San Rafael,
13 CA 94903, U.S.A., +1(415)492-9861, for further information.
21 /* An implementation of MMR decoding. This is based on the
22 implementation in Fitz, which in turn is based on the one
36 #include "jbig2_priv.h"
37 #include "jbig2_arith.h"
38 #include "jbig2_generic.h"
39 #include "jbig2_mmr.h"
53 jbig2_decode_mmr_init(Jbig2MmrCtx *mmr, int width, int height, const byte *data, size_t size)
65 for (i = 0; i < size && i < 4; i++)
66 word |= (data[i] << ((3 - i) << 3));
71 jbig2_decode_mmr_consume(Jbig2MmrCtx *mmr, int n_bits)
74 mmr->bit_index += n_bits;
75 while (mmr->bit_index >= 8) {
77 if (mmr->data_index + 4 < mmr->size)
78 mmr->word |= (mmr->data[mmr->data_index + 4] << mmr->bit_index);
84 <raph> the first 2^(initialbits) entries map bit patterns to decodes
85 <raph> let's say initial_bits is 8 for the sake of example
86 <raph> and that the code is 1001
87 <raph> that means that entries 0x90 .. 0x9f have the entry { val, 4 }
88 <raph> because those are all the bytes that start with the code
89 <raph> and the 4 is the length of the code
90 ... if (n_bits > initial_bits) ...
91 <raph> anyway, in that case, it basically points to a mini table
92 <raph> the n_bits is the maximum length of all codes beginning with that byte
93 <raph> so 2^(n_bits - initial_bits) is the size of the mini-table
94 <raph> peter came up with this, and it makes sense
102 /* white decode table (runlength huffman codes) */
103 const mmr_table_node jbig2_mmr_white_decode[] = {
410 /* black decode table (runlength huffman codes) */
411 const mmr_table_node jbig2_mmr_black_decode[] = {
734 #define getbit(buf, x) ( ( buf[x >> 3] >> ( 7 - (x & 7) ) ) & 1 )
737 jbig2_find_changing_element(const byte *line, int x, int w)
764 jbig2_find_changing_element_of_color(const byte *line, int x, int w, int color)
768 x = jbig2_find_changing_element(line, x, w);
769 if (x < w && getbit(line, x) != color)
770 x = jbig2_find_changing_element(line, x, w);
774 static const byte lm[8] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
775 static const byte rm[8] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
778 jbig2_set_bits(byte *line, int x0, int x1)
780 int a0, a1, b0, b1, a;
789 line[a0] |= lm[b0] & rm[b1];
793 for (a = a0 + 1; a < a1; a++)
795 if (b1) line[a1] |= rm[b1];
801 jbig2_decode_get_code(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
803 uint32_t word = mmr->word;
804 int table_ix = word >> (32 - initial_bits);
805 int val = table[table_ix].val;
806 int n_bits = table[table_ix].n_bits;
808 if (n_bits > initial_bits) {
809 int mask = (1 << (32 - initial_bits)) - 1;
810 table_ix = val + ((word & mask) >> (32 - n_bits));
811 val = table[table_ix].val;
812 n_bits = initial_bits + table[table_ix].n_bits;
815 jbig2_decode_mmr_consume(mmr, n_bits);
821 jbig2_decode_get_run(Jbig2MmrCtx *mmr, const mmr_table_node *table, int initial_bits)
827 val = jbig2_decode_get_code(mmr, table, initial_bits);
835 jbig2_decode_mmr_line(Jbig2MmrCtx *mmr, const byte *ref, byte *dst)
839 int c = 0; /* 0 is white, black is 1 */
843 uint32_t word = mmr->word;
844 /* printf ("%08x\n", word); */
846 if (a0 >= mmr->width)
849 if ((word >> (32 - 3)) == 1)
851 int white_run, black_run;
853 jbig2_decode_mmr_consume(mmr, 3);
859 white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
860 black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
863 if (a1 > mmr->width) a1 = mmr->width;
864 if (a2 > mmr->width) a2 = mmr->width;
865 if (a2 < a1 || a1 < 0) return -1;
866 jbig2_set_bits(dst, a1, a2);
868 /* printf ("H %d %d\n", white_run, black_run); */
872 black_run = jbig2_decode_get_run(mmr, jbig2_mmr_black_decode, 7);
873 white_run = jbig2_decode_get_run(mmr, jbig2_mmr_white_decode, 8);
876 if (a1 > mmr->width) a1 = mmr->width;
877 if (a2 > mmr->width) a2 = mmr->width;
878 if (a1 < a0 || a0 < 0) return -1;
879 jbig2_set_bits(dst, a0, a1);
881 /* printf ("H %d %d\n", black_run, white_run); */
885 else if ((word >> (32 - 4)) == 1)
887 /* printf ("P\n"); */
888 jbig2_decode_mmr_consume(mmr, 4);
889 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
890 b2 = jbig2_find_changing_element(ref, b1, mmr->width);
893 if (b2 < a0 || a0 < 0) return -1;
894 jbig2_set_bits(dst, a0, b2);
899 else if ((word >> (32 - 1)) == 1)
901 /* printf ("V(0)\n"); */
902 jbig2_decode_mmr_consume(mmr, 1);
903 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
906 if (b1 < a0 || a0 < 0) return -1;
907 jbig2_set_bits(dst, a0, b1);
913 else if ((word >> (32 - 3)) == 3)
915 /* printf ("VR(1)\n"); */
916 jbig2_decode_mmr_consume(mmr, 3);
917 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
918 if (b1 + 1 > mmr->width) break;
921 if (b1 + 1 < a0 || a0 < 0) return -1;
922 jbig2_set_bits(dst, a0, b1 + 1);
928 else if ((word >> (32 - 6)) == 3)
930 /* printf ("VR(2)\n"); */
931 jbig2_decode_mmr_consume(mmr, 6);
932 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
933 if (b1 + 2 > mmr->width) break;
936 if (b1 + 2 < a0 || a0 < 0) return -1;
937 jbig2_set_bits(dst, a0, b1 + 2);
943 else if ((word >> (32 - 7)) == 3)
945 /* printf ("VR(3)\n"); */
946 jbig2_decode_mmr_consume(mmr, 7);
947 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
948 if (b1 + 3 > mmr->width) break;
951 if (b1 + 3 < a0 || a0 < 0) return -1;
952 jbig2_set_bits(dst, a0, b1 + 3);
958 else if ((word >> (32 - 3)) == 2)
960 /* printf ("VL(1)\n"); */
961 jbig2_decode_mmr_consume(mmr, 3);
962 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
963 if (b1 - 1 < 0) break;
966 if (b1 - 1 < a0 || a0 < 0) return -1;
967 jbig2_set_bits(dst, a0, b1 - 1);
973 else if ((word >> (32 - 6)) == 2)
975 /* printf ("VL(2)\n"); */
976 jbig2_decode_mmr_consume(mmr, 6);
977 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
978 if (b1 - 2 < 0) break;
981 if (b1 - 2 < a0 || a0 < 0) return -1;
982 jbig2_set_bits(dst, a0, b1 - 2);
988 else if ((word >> (32 - 7)) == 2)
990 /* printf ("VL(3)\n"); */
991 jbig2_decode_mmr_consume(mmr, 7);
992 b1 = jbig2_find_changing_element_of_color(ref, a0, mmr->width, !c);
993 if (b1 - 3 < 0) break;
996 if (b1 - 3 < a0 || a0 < 0) return -1;
997 jbig2_set_bits(dst, a0, b1 - 3);
1011 jbig2_decode_generic_mmr(Jbig2Ctx *ctx,
1012 Jbig2Segment *segment,
1013 const Jbig2GenericRegionParams *params,
1014 const byte *data, size_t size,
1018 const int rowstride = image->stride;
1019 byte *dst = image->data;
1024 jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
1026 for (y = 0; y < image->height; y++) {
1027 memset(dst, 0, rowstride);
1028 code = jbig2_decode_mmr_line(&mmr, ref, dst);
1029 if (code < 0) return code;
1038 * jbig2_decode_halftone_mmr: decode mmr region inside of halftones
1040 * @ctx: jbig2 decoder context
1041 * @params: parameters for decoding
1042 * @data: pointer to text region data to be decoded
1043 * @size: length of text region data
1044 * @image: return of decoded image
1045 * @consumed_bytes: return of consumed bytes from @data
1047 * MMR decoding that consumes EOFB and returns consumed bytes (@consumed_bytes)
1052 jbig2_decode_halftone_mmr(Jbig2Ctx *ctx,
1053 const Jbig2GenericRegionParams *params,
1054 const byte *data, size_t size,
1055 Jbig2Image *image, size_t* consumed_bytes)
1058 const int rowstride = image->stride;
1059 byte *dst = image->data;
1063 const uint32_t EOFB = 0x001001;
1065 jbig2_decode_mmr_init(&mmr, image->width, image->height, data, size);
1067 for (y = 0; y < image->height; y++) {
1068 memset(dst, 0, rowstride);
1069 code = jbig2_decode_mmr_line(&mmr, ref, dst);
1070 if (code < 0) return code;
1075 /* test for EOFB (see section 6.2.6) */
1076 if (mmr.word >> 8 == EOFB) {
1077 mmr.data_index += 3;
1080 *consumed_bytes += mmr.data_index + (mmr.bit_index >> 3) +
1081 (mmr.bit_index > 0 ? 1 : 0);