1/******************************************************************************
2 * pixma_parse.h parser for Canon BJL printjobs
3 * Copyright (c) 2005 - 2007 Sascha Sommer <saschasommer@freenet.de>.
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
43typedef struct PutBitContext {
44    uint32_t bit_buf;
45    int bit_left;
46    uint8_t *buf, *buf_ptr, *buf_end;
47} PutBitContext;
48
49static 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
58static 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 */
90typedef 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 */
158static 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 */
174static 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 */
198static 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
219typedef struct rasterline_s {
220	unsigned char* buf;
221	unsigned int len;
222	unsigned int line;
223	struct rasterline_s* next;
224} rasterline_t;
225
226
227typedef struct color_s {
228	char name;          /* name (one of CMYKcmykRGH) */
229	int bpp;            /* number of bits */
230	int level;          /* number of levels */
231        int density;     /* relative density to the other colors*/
232	unsigned int  value;/* last used dot value */
233	unsigned int* dots;  /* number of dots for every level */
234        unsigned int* usedlevels; /* actual levels used. Array of length 2^bpp */
235        int compression;    /* bits are compressed */
236	rasterline_t* head; /* end of linked list */
237	rasterline_t* tail; /* start of linked list */
238	rasterline_t* pos;  /* iterator position */
239} color_t;
240
241
242typedef struct image_s {
243        unsigned int width;
244        unsigned int height;
245        unsigned int dots;
246	unsigned int image_top;
247	unsigned int image_bottom;
248	unsigned int image_left;
249	unsigned int image_right;
250	float top;
251	float left;
252	int xres,yres;
253        int y;
254        color_t color[MAX_COLORS];
255        char* color_order;
256        int num_colors;
257        int cur_color;
258        int lines_per_block;
259} image_t;
260
261/* FIXME what are the 0xa3 and 0xad for? they are used in the PIXMA iP4200 CD mode */
262/*static const unsigned char valid_colors[] = {'C','M','Y','K','c','m','y','k',0xa3,0xad}; */
263static 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'};
264#endif
265
266
267