| 1 | /****************************************************************************** |
| 2 | * pixma_parse.h parser for Canon BJL printjobs |
| 3 | * Copyright (c) 2005 - 2007 Sascha Sommer <[email protected]>. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | *****************************************************************************/ |
| 20 | |
| 21 | |
| 22 | #ifndef PIXMA_PARSE_H |
| 23 | #define PIXMA_PARSE_H 1 |
| 24 | |
| 25 | |
| 26 | #define bswap_32(x) \ |
| 27 | ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ |
| 28 | (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) |
| 29 | |
| 30 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 31 | #define be2me_32(x) bswap_32(x) |
| 32 | #else |
| 33 | #define be2me_32 |
| 34 | #endif |
| 35 | |
| 36 | |
| 37 | |
| 38 | /* Bitstream reader from FFmpeg (http://www.ffmpeg.org) |
| 39 | * libavcodec/bitstream.h |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | typedef struct PutBitContext { |
| 44 | uint32_t bit_buf; |
| 45 | int bit_left; |
| 46 | uint8_t *buf, *buf_ptr, *buf_end; |
| 47 | } PutBitContext; |
| 48 | |
| 49 | static inline void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size) |
| 50 | { |
| 51 | s->buf = buffer; |
| 52 | s->buf_end = s->buf + buffer_size; |
| 53 | s->buf_ptr = s->buf; |
| 54 | s->bit_left=32; |
| 55 | s->bit_buf=0; |
| 56 | } |
| 57 | |
| 58 | static inline void put_bits(PutBitContext *s, int n, unsigned int value) |
| 59 | { |
| 60 | unsigned int bit_buf; |
| 61 | int bit_left; |
| 62 | |
| 63 | /* printf("put_bits=%d %x\n", n, value); */ |
| 64 | |
| 65 | |
| 66 | bit_buf = s->bit_buf; |
| 67 | bit_left = s->bit_left; |
| 68 | |
| 69 | /* printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf); */ |
| 70 | /* XXX: optimize */ |
| 71 | if (n < bit_left) { |
| 72 | bit_buf = (bit_buf<<n) | value; |
| 73 | bit_left-=n; |
| 74 | } else { |
| 75 | bit_buf<<=bit_left; |
| 76 | bit_buf |= value >> (n - bit_left); |
| 77 | *(uint32_t *)s->buf_ptr = be2me_32(bit_buf); |
| 78 | /* printf("bitbuf = %08x\n", bit_buf); */ |
| 79 | s->buf_ptr+=4; |
| 80 | bit_left+=32 - n; |
| 81 | bit_buf = value; |
| 82 | } |
| 83 | |
| 84 | s->bit_buf = bit_buf; |
| 85 | s->bit_left = bit_left; |
| 86 | } |
| 87 | |
| 88 | /* bit input */ |
| 89 | /* buffer, buffer_end and size_in_bits must be present and used by every reader */ |
| 90 | typedef struct GetBitContext { |
| 91 | const uint8_t *buffer, *buffer_end; |
| 92 | uint32_t *buffer_ptr; |
| 93 | uint32_t cache0; |
| 94 | uint32_t cache1; |
| 95 | int bit_count; |
| 96 | int size_in_bits; |
| 97 | } GetBitContext; |
| 98 | |
| 99 | |
| 100 | # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s))) |
| 101 | # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s))) |
| 102 | |
| 103 | |
| 104 | # define MIN_CACHE_BITS 32 |
| 105 | |
| 106 | # define OPEN_READER(name, gb)\ |
| 107 | int name##_bit_count=(gb)->bit_count;\ |
| 108 | uint32_t name##_cache0= (gb)->cache0;\ |
| 109 | uint32_t name##_cache1= (gb)->cache1;\ |
| 110 | uint32_t * name##_buffer_ptr=(gb)->buffer_ptr;\ |
| 111 | |
| 112 | # define CLOSE_READER(name, gb)\ |
| 113 | (gb)->bit_count= name##_bit_count;\ |
| 114 | (gb)->cache0= name##_cache0;\ |
| 115 | (gb)->cache1= name##_cache1;\ |
| 116 | (gb)->buffer_ptr= name##_buffer_ptr;\ |
| 117 | |
| 118 | # define UPDATE_CACHE(name, gb)\ |
| 119 | if(name##_bit_count > 0){\ |
| 120 | const uint32_t next= be2me_32( *name##_buffer_ptr );\ |
| 121 | name##_cache0 |= NEG_USR32(next,name##_bit_count);\ |
| 122 | name##_cache1 |= next<<name##_bit_count;\ |
| 123 | name##_buffer_ptr++;\ |
| 124 | name##_bit_count-= 32;\ |
| 125 | }\ |
| 126 | |
| 127 | # define SKIP_CACHE(name, gb, num)\ |
| 128 | name##_cache0 <<= (num);\ |
| 129 | name##_cache0 |= NEG_USR32(name##_cache1,num);\ |
| 130 | name##_cache1 <<= (num); |
| 131 | |
| 132 | # define SKIP_COUNTER(name, gb, num)\ |
| 133 | name##_bit_count += (num);\ |
| 134 | |
| 135 | # define SKIP_BITS(name, gb, num)\ |
| 136 | {\ |
| 137 | SKIP_CACHE(name, gb, num)\ |
| 138 | SKIP_COUNTER(name, gb, num)\ |
| 139 | }\ |
| 140 | |
| 141 | # define LAST_SKIP_BITS(name, gb, num) SKIP_BITS(name, gb, num) |
| 142 | # define LAST_SKIP_CACHE(name, gb, num) SKIP_CACHE(name, gb, num) |
| 143 | |
| 144 | # define SHOW_UBITS(name, gb, num)\ |
| 145 | NEG_USR32(name##_cache0, num) |
| 146 | |
| 147 | # define SHOW_SBITS(name, gb, num)\ |
| 148 | NEG_SSR32(name##_cache0, num) |
| 149 | |
| 150 | # define GET_CACHE(name, gb)\ |
| 151 | (name##_cache0) |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * reads 0-17 bits. |
| 156 | * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't |
| 157 | */ |
| 158 | static inline unsigned int get_bits(GetBitContext *s, int n){ |
| 159 | register int tmp; |
| 160 | OPEN_READER(re, s) |
| 161 | UPDATE_CACHE(re, s) |
| 162 | tmp= SHOW_UBITS(re, s, n); |
| 163 | LAST_SKIP_BITS(re, s, n) |
| 164 | CLOSE_READER(re, s) |
| 165 | return tmp; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * init GetBitContext. |
| 170 | * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits |
| 171 | * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end |
| 172 | * @param bit_size the size of the buffer in bits |
| 173 | */ |
| 174 | static inline void init_get_bits(GetBitContext *s, |
| 175 | uint8_t *buffer, int bit_size) |
| 176 | { |
| 177 | const int buffer_size= (bit_size+7)>>3; |
| 178 | |
| 179 | s->buffer= buffer; |
| 180 | s->size_in_bits= bit_size; |
| 181 | s->buffer_end= buffer + buffer_size; |
| 182 | s->buffer_ptr = (uint32_t*)buffer; |
| 183 | s->bit_count = 32; |
| 184 | s->cache0 = 0; |
| 185 | s->cache1 = 0; |
| 186 | |
| 187 | { |
| 188 | OPEN_READER(re, s) |
| 189 | UPDATE_CACHE(re, s) |
| 190 | UPDATE_CACHE(re, s) |
| 191 | CLOSE_READER(re, s) |
| 192 | } |
| 193 | s->cache1 = 0; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | /* 10to8 decompression table for 2-bit inks with 3 levels: 3^5 = 243 combinations --- 5 pixels can be compressed into 1 byte */ |
| 198 | static const unsigned short Table8[] = { |
| 199 | 0x0,0x1,0x2,0x4,0x5,0x6,0x8,0x9,0xa,0x10,0x11,0x12,0x14,0x15,0x16, |
| 200 | 0x18,0x19,0x1a,0x20,0x21,0x22,0x24,0x25,0x26,0x28,0x29,0x2a,0x40,0x41,0x42, |
| 201 | 0x44,0x45,0x46,0x48,0x49,0x4a,0x50,0x51,0x52,0x54,0x55,0x56,0x58,0x59,0x5a, |
| 202 | 0x60,0x61,0x62,0x64,0x65,0x66,0x68,0x69,0x6a,0x80,0x81,0x82,0x84,0x85,0x86, |
| 203 | 0x88,0x89,0x8a,0x90,0x91,0x92,0x94,0x95,0x96,0x98,0x99,0x9a,0xa0,0xa1,0xa2, |
| 204 | 0xa4,0xa5,0xa6,0xa8,0xa9,0xaa,0x100,0x101,0x102,0x104,0x105,0x106,0x108,0x109,0x10a, |
| 205 | 0x110,0x111,0x112,0x114,0x115,0x116,0x118,0x119,0x11a,0x120,0x121,0x122,0x124,0x125,0x126, |
| 206 | 0x128,0x129,0x12a,0x140,0x141,0x142,0x144,0x145,0x146,0x148,0x149,0x14a,0x150,0x151,0x152, |
| 207 | 0x154,0x155,0x156,0x158,0x159,0x15a,0x160,0x161,0x162,0x164,0x165,0x166,0x168,0x169,0x16a, |
| 208 | 0x180,0x181,0x182,0x184,0x185,0x186,0x188,0x189,0x18a,0x190,0x191,0x192,0x194,0x195,0x196, |
| 209 | 0x198,0x199,0x19a,0x1a0,0x1a1,0x1a2,0x1a4,0x1a5,0x1a6,0x1a8,0x1a9,0x1aa,0x200,0x201,0x202, |
| 210 | 0x204,0x205,0x206,0x208,0x209,0x20a,0x210,0x211,0x212,0x214,0x215,0x216,0x218,0x219,0x21a, |
| 211 | 0x220,0x221,0x222,0x224,0x225,0x226,0x228,0x229,0x22a,0x240,0x241,0x242,0x244,0x245,0x246, |
| 212 | 0x248,0x249,0x24a,0x250,0x251,0x252,0x254,0x255,0x256,0x258,0x259,0x25a,0x260,0x261,0x262, |
| 213 | 0x264,0x265,0x266,0x268,0x269,0x26a,0x280,0x281,0x282,0x284,0x285,0x286,0x288,0x289,0x28a, |
| 214 | 0x290,0x291,0x292,0x294,0x295,0x296,0x298,0x299,0x29a,0x2a0,0x2a1,0x2a2,0x2a4,0x2a5,0x2a6, |
| 215 | 0x2a8,0x2a9,0x2aa,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 |
| 216 | }; |
| 217 | |
| 218 | /* 5-level decompression table for 4-bit inks with 5 levels: 5^3 = 125 combinations --- 3 pixels can be compressed into 1 byte */ |
| 219 | /* So then we can put 3 in the lower 8 */ |
| 220 | static const unsigned short Table5Level[] = { |
| 221 | 0x0,0x1,0x2,0x3,0x4,0x10,0x11,0x12,0x13,0x14,0x20,0x21,0x22,0x23,0x24,0x30, |
| 222 | 0x31,0x32,0x33,0x34,0x40,0x41,0x42,0x43,0x44,0x100,0x101,0x102,0x103,0x104,0x110,0x111, |
| 223 | 0x112,0x113,0x114,0x120,0x121,0x122,0x123,0x124,0x130,0x131,0x132,0x133,0x134,0x140,0x141,0x142, |
| 224 | 0x143,0x144,0x200,0x201,0x202,0x203,0x204,0x210,0x211,0x212,0x213,0x214,0x220,0x221,0x222,0x223, |
| 225 | 0x224,0x230,0x231,0x232,0x233,0x234,0x240,0x241,0x242,0x243,0x244,0x300,0x301,0x302,0x303,0x304, |
| 226 | 0x310,0x311,0x312,0x313,0x314,0x320,0x321,0x322,0x323,0x324,0x330,0x331,0x332,0x333,0x334,0x340, |
| 227 | 0x341,0x342,0x343,0x344,0x400,0x401,0x402,0x403,0x404,0x410,0x411,0x412,0x413,0x414,0x420,0x421, |
| 228 | 0x422,0x423,0x424,0x430,0x431,0x432,0x433,0x434,0x440,0x441,0x442,0x443,0x444,0x0,0x0,0x0, |
| 229 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 230 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 231 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 232 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 233 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 234 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 235 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 236 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 |
| 237 | }; |
| 238 | |
| 239 | /* 6-level decompression table for 4-bit inks with 6 levels: 6^3 = 216 combinations --- 3 pixels can be compressed into 1 byte */ |
| 240 | static const unsigned short Table6Level[] = { |
| 241 | 0x0,0x1,0x2,0x3,0x4,0x5,0x10,0x11,0x12,0x13,0x14,0x15,0x20,0x21,0x22,0x23, |
| 242 | 0x24,0x25,0x30,0x31,0x32,0x33,0x34,0x35,0x40,0x41,0x42,0x43,0x44,0x45,0x50,0x51, |
| 243 | 0x52,0x53,0x54,0x55,0x100,0x101,0x102,0x103,0x104,0x105,0x110,0x111,0x112,0x113,0x114,0x115, |
| 244 | 0x120,0x121,0x122,0x123,0x124,0x125,0x130,0x131,0x132,0x133,0x134,0x135,0x140,0x141,0x142,0x143, |
| 245 | 0x144,0x145,0x150,0x151,0x152,0x153,0x154,0x155,0x200,0x201,0x202,0x203,0x204,0x205,0x210,0x211, |
| 246 | 0x212,0x213,0x214,0x215,0x220,0x221,0x222,0x223,0x224,0x225,0x230,0x231,0x232,0x233,0x234,0x235, |
| 247 | 0x240,0x241,0x242,0x243,0x244,0x245,0x250,0x251,0x252,0x253,0x254,0x255,0x300,0x301,0x302,0x303, |
| 248 | 0x304,0x305,0x310,0x311,0x312,0x313,0x314,0x315,0x320,0x321,0x322,0x323,0x324,0x325,0x330,0x331, |
| 249 | 0x332,0x333,0x334,0x335,0x340,0x341,0x342,0x343,0x344,0x345,0x350,0x351,0x352,0x353,0x354,0x355, |
| 250 | 0x400,0x401,0x402,0x403,0x404,0x405,0x410,0x411,0x412,0x413,0x414,0x415,0x420,0x421,0x422,0x423, |
| 251 | 0x424,0x425,0x430,0x431,0x432,0x433,0x434,0x435,0x440,0x441,0x442,0x443,0x444,0x445,0x450,0x451, |
| 252 | 0x452,0x453,0x454,0x455,0x500,0x501,0x502,0x503,0x504,0x505,0x510,0x511,0x512,0x513,0x514,0x515, |
| 253 | 0x520,0x521,0x522,0x523,0x524,0x525,0x530,0x531,0x532,0x533,0x534,0x535,0x540,0x541,0x542,0x543, |
| 254 | 0x544,0x545,0x550,0x551,0x552,0x553,0x554,0x555,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 255 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0, |
| 256 | 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0 |
| 257 | }; |
| 258 | |
| 259 | typedef struct rasterline_s { |
| 260 | unsigned char* buf; |
| 261 | unsigned int len; |
| 262 | unsigned int line; |
| 263 | struct rasterline_s* next; |
| 264 | } rasterline_t; |
| 265 | |
| 266 | |
| 267 | typedef struct color_s { |
| 268 | char name; /* name (one of CMYKcmykH) */ |
| 269 | int bpp; /* number of bits */ |
| 270 | int level; /* number of levels */ |
| 271 | int density; /* relative density to the other colors*/ |
| 272 | unsigned int value;/* last used dot value */ |
| 273 | unsigned int* dots; /* number of dots for every level */ |
| 274 | unsigned int* usedlevels; /* actual levels used. Array of length 2^bpp */ |
| 275 | int compression; /* bits are compressed */ |
| 276 | rasterline_t* head; /* end of linked list */ |
| 277 | rasterline_t* tail; /* start of linked list */ |
| 278 | rasterline_t* pos; /* iterator position */ |
| 279 | } color_t; |
| 280 | |
| 281 | |
| 282 | typedef struct image_s { |
| 283 | unsigned int width; |
| 284 | unsigned int height; |
| 285 | unsigned int dots; |
| 286 | unsigned int image_top; |
| 287 | unsigned int image_bottom; |
| 288 | unsigned int image_left; |
| 289 | unsigned int image_right; |
| 290 | float top; |
| 291 | float left; |
| 292 | int xres,yres; |
| 293 | int y; |
| 294 | color_t color[MAX_COLORS]; |
| 295 | char* color_order; |
| 296 | int num_colors; |
| 297 | int cur_color; |
| 298 | int lines_per_block; |
| 299 | } image_t; |
| 300 | |
| 301 | /* static const unsigned char valid_colors[] = {'C','M','Y','K','c','m','y','k'}; */ |
| 302 | /* static const unsigned char valid_colors[] = {'C','M','Y','K','c','m','y','k',0xa3,0xad}; */ |
| 303 | static const unsigned char valid_colors[] = {'C','M','Y','K','c','m','y','k','R','G','H','A','B','D','E','F','I','J','L','M','N','O','P','Q','S','T','U','V','W','X','Z','a','b','d','e','f'}; |
| 304 | #endif |