Tizen 2.1 base
[platform/upstream/hplip.git] / ip / xjpg_enc.c
1 /* libhpojip -- HP OfficeJet image-processing library. */
2
3 /* Copyright (C) 1995-2002 Hewlett-Packard Company
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
12  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
13  * NON-INFRINGEMENT.  See the 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., 59 Temple Place - Suite 330, Boston,
18  * MA 02111-1307, USA.
19  *
20  * In addition, as a special exception, Hewlett-Packard Company
21  * gives permission to link the code of this program with any
22  * version of the OpenSSL library which is distributed under a
23  * license identical to that listed in the included LICENSE.OpenSSL
24  * file, and distribute linked combinations including the two.
25  * You must obey the GNU General Public License in all respects
26  * for all of the code used other than OpenSSL.  If you modify
27  * this file, you may extend this exception to your version of the
28  * file, but you are not obligated to do so.  If you do not wish to
29  * do so, delete this exception statement from your version.
30  */
31
32 /* Original author: Mark Overton and others.
33  *
34  * Ported to Linux by David Paschal.
35  */
36
37 /*****************************************************************************\
38  *
39  * xjpg_enc.c - Converts raw gray image into a valid JPEG file
40  *
41  *****************************************************************************
42  *
43  * Name of Global Jump-Table:
44  *
45  *    jpgEncodeTbl
46  *
47  * Items in aXformInfo array passed into setXformSpec:
48  *
49  *    aXformInfo[IP_JPG_ENCODE_QUALITY_FACTORS]:
50  *            Quality factors. Each 1..255 (best..worst), normal=20 or 0.
51  *            If 2nd least significant byte is non-zero, then it is the
52  *            DC factor, and lsb is the AC factor.  If 2nd lsb is zero,
53  *            then the lsb is both AC and DC factor.  Q factors match PML.
54  *    aXformInfo[IP_JPG_ENCODE_SAMPLE_FACTORS]:
55  *            Sample factors in nibbles: HHHHVVVV, 0 means use defaults.
56  *    aXformInfo[IP_JPG_ENCODE_ALREADY_SUBSAMPLED]:
57  *            Is raw data already subsampled? 0=no, 1=yes.
58  *    aXformInfo[IP_JPG_ENCODE_FOR_DENALI]:
59  *            Output is for Denali? 0=no, 1=yes
60  *    aXformInfo[IP_JPG_ENCODE_OUTPUT_DNL]:
61  *            Output a DNL (Define Number of Lines) marker? 0=no, 1=yes.
62  *    aXformInfo[IP_JPG_ENCODE_FOR_COLOR_FAX]:
63  *            Output an APP1 per the G3 color fax standard? 0=no, 1=yes.
64  *    aXformInfo[IP_JPG_ENCODE_DUMMY_HEADER_LEN]:
65  *            # bytes in dummy header.  0 means output normal header.
66  *            The firmware discards the header in the first raster data
67  *            record. This value is the # data bytes in that record.
68  *            This MUST be zero for JPEG files not being sent to firmware.
69  *
70  *    The aXformInfo items above may all be set to 0 for typical JPEG files.
71  *
72  *    For Denali, the JPEG that's output will be changed thusly:
73  *        - An EOB will always follow every 8x8 block
74  *        - A small change in the Huffman tables (no 15-bit codes)
75  *
76  * Capabilities and Limitations:
77  *
78  *    Encodes a standard JPEG file with a JFIF 1.0 marker.
79  *    Will *not* output a non-interleaved file; it's interleaved only.
80  *    If the number of rows was unknown in the input traits, this encoder
81  *    seeks back to the beginning of the file and fills in the row-count.
82  *    A DNL is always output if the always-output-DNL item is set in
83  *    the aXformInfo array above.
84  *    Handles 8-bit gray, or 3-component 24-bit color.
85  *
86  * Default Input Traits, and Output Traits:
87  *
88  *          trait             default input             output
89  *    -------------------  ---------------------  ------------------------
90  *    iPixelsPerRow         * passed into output   same as default input
91  *    iBitsPerPixel         * passed into output   same as default input
92  *    iComponentsPerPixel   * must be 1 or 3       same as default input
93  *    lHorizDPI               passed into output   same as default input
94  *    lVertDPI                passed into output   same as default input
95  *    lNumRows                passed into output   same as default input
96  *    iNumPages               passed into output   same as default input
97  *    iPageNum                passed into output   same as default input
98  *
99  *    Above, a "*" by an item indicates it must be valid (not negative).
100  *
101  * Jan 1998:  Ported to Image Processor module of Windows software.
102  * Feb 1996:  Written for firmware, Mark Overton;
103  *            sections of this code were ported from HP-Labs (Hugh P. Nguyen).
104  *
105 \*****************************************************************************/
106
107 #include <string.h>
108 #include "hpip.h"
109 #include "ipdefs.h"
110 #include "xjpg_dct.h"
111 #include "xjpg_mrk.h"
112
113
114 #if 0
115     #include "stdio.h"
116     #include <tchar.h>
117     #define PRINT(msg,arg1,arg2) \
118         _ftprintf(stdout, _T("(jpeg) ") msg, (int)arg1, (int)arg2)
119 #else
120     #define PRINT(msg,arg1,arg2)
121 #endif
122
123
124 #define RUN_OF_16         0xf0  /* RLE code for run-of-16-zeroes */
125 #define MAX_HEADER_SIZE   2000
126 #define MAX_BLOCKS_IN_MCU 6
127 #define MAX_MCU_SIZE      (MAX_BLOCKS_IN_MCU*304)
128                                 /* max encoded MCU size, plus stuff-bytes */
129 #define OUTBUF_NUM_MCUS   2     /* workbuf will be this multiple of max MCU */
130
131 #define Q_DEFAULT         20    /* default quality-factor */
132 #define MONO_FACTORS      0x10001000u  /* default mono  sample factors */
133 #define COLOR_FACTORS     0x21102110u  /* default color sample factors */
134
135 #define CHECK_VALUE 0xAceC0de4U
136
137
138 /*____________________________________________________________________________
139  |                                                                            |
140  | Configuration Variables                                                    |
141  |____________________________________________________________________________|
142 */
143
144 /* Encoding is centered around in_rows_ap.  It is indexed by
145  * [color_component_number][row_number].
146  * color_component_number 0 is Y (intensity); mono only has this component.
147  *
148  * Each component in in_rows_ap has a height (# rows) equal to the number of
149  * samples in the MCU, and a width equal to the number of samples in all the
150  * MCUs in a row.  That is, pixels are stored in in_rows_ap with NO REPLICATION.
151  * So if a component has sample factors of H and V, it will have 8*V rows and
152  * pixels_in_row*H/max_horiz_sam_fac columns in in_rows_ap.
153  */
154
155 typedef struct {
156
157     /**** Configuration ****/
158
159     BYTE  lum_quant[64];
160     BYTE  chrom_quant[64];
161
162     int   wino_lum_quant[64];
163     int   wino_lum_quant_thres[64];
164     int   wino_chrom_quant[64];
165     int   wino_chrom_quant_thres[64];
166
167     BOOL  input_subsampled;
168     BOOL  fDenali;
169     BOOL  fOutputDNL;
170     BOOL  fOutputG3APP1;
171     DWORD dwDummyHeaderBytes;
172     UINT  rows_in_mcu;         /* # rows & cols in each MCU */
173     UINT  cols_in_mcu;
174     UINT  mcus_in_row;         /* # of MCUs in each row */
175     DWORD sample_factors;      /* H and V sample factors, one/nibble */
176     BYTE  horiz_sam_facs[4];   /* horizontal sampling factors */
177     BYTE  vert_sam_facs [4];   /* vertical sampling factors */
178     BYTE  max_horiz_sam_fac;   /* max sample factors */
179     BYTE  max_vert_sam_fac;
180     BYTE  dc_q_factor;
181     BYTE  ac_q_factor;
182     BYTE  comps_in_pixel;      /* # components per pixel (1 or 3) */
183     BYTE  whitePixel[4];       /* the value of a white pixel */
184     UINT  pixels_in_row;
185     int   rows_in_page;        /* negative means "unknown" */
186     int   xRes;                /* dots per inch */
187     int   yRes;
188
189     /**** Writing Bits ****/
190
191     DWORD wr_bit_buf;
192     /* Bits to be written to outbuf (accumulated left-to-right). */
193
194     int wr_bits_avail;
195     /* Number of bits not yet written in wr_bit_buf (= 32 - number written). */
196
197     BYTE *wr_outbuf_beg;
198     /* The beginning of the output buffer. */
199
200     BYTE *write_buf_next;
201     /* Next byte in outbuf to be written. */
202
203     /**** Encoding Blocks ****/
204
205     int  enc_block[64];           /* scratch-pad 8x8 block */
206     int *enc_block_zz[64+16];     /* zig-zag ptrs into above block */
207     int  prior_DC[4];
208
209     /**** Top Level Control ****/
210
211     UINT rows_received;
212     UINT rows_loaded;
213     UINT mcus_sent;
214     BOOL loading_rows;
215
216     /**** Miscellaneous ****/
217
218     IP_IMAGE_TRAITS traits;
219     BYTE *in_rows_ap[4][32];   /* row-buffers [component][row] */
220     BOOL  fDidHeader;          /* output the header yet? */
221     DWORD dwInNextPos;         /* next read pos in input file */
222     DWORD dwOutNextPos;        /* next write pos in output file */
223     DWORD dwValidChk;          /* struct validity check value */
224
225 } JENC_INST, *PJENC_INST;
226
227
228 /*____________________________________________________________________________
229  |                                                                            |
230  | Normal Quantization Tables                                                 |
231  |____________________________________________________________________________|
232 */
233
234 #if 0
235
236 static const BYTE orig_lum_quant[64] = {
237     16, 11, 10, 16,  24,  40,  51,  61,
238     12, 12, 14, 19,  26,  58,  60,  55,
239     14, 13, 16, 24,  40,  57,  69,  56,
240     14, 17, 22, 29,  51,  87,  80,  62,
241     18, 22, 37, 56,  68, 109, 103,  77,
242     24, 35, 55, 64,  81, 104, 113,  92,
243     49, 64, 78, 87, 103, 121, 120, 101,
244     72, 92, 95, 98, 112, 100, 103,  99
245 };
246
247
248 static const BYTE orig_chrom_quant[64] = {
249     17, 18, 24, 47, 99, 99, 99, 99,
250     18, 21, 26, 66, 99, 99, 99, 99,
251     24, 26, 56, 99, 99, 99, 99, 99,
252     47, 66, 99, 99, 99, 99, 99, 99,
253     99, 99, 99, 99, 99, 99, 99, 99,
254     99, 99, 99, 99, 99, 99, 99, 99,
255     99, 99, 99, 99, 99, 99, 99, 99,
256     99, 99, 99, 99, 99, 99, 99, 99
257 };
258
259 #endif
260
261
262 /*____________________________________________________________________________
263  |                                                                            |
264  | Zigzag of Normal Quantization Tables                                       |
265  |____________________________________________________________________________|
266 */
267
268 static const BYTE orig_lum_quant[64] = {
269   #if 0
270      /* these make color fax look better, but break gray copy
271       * because JPEG is sent to the device, so our tables must
272       * match those in the firmware
273       */
274      10,  10,  10,  14,  12,  10,  16,  14,
275   #else
276      16,  11,  12,  14,  12,  10,  16,  14,
277   #endif
278      13,  14,  18,  17,  16,  19,  24,  40,
279      26,  24,  22,  22,  24,  49,  35,  37,
280      29,  40,  58,  51,  61,  60,  57,  51,
281      56,  55,  64,  72,  92,  78,  64,  68,
282      87,  69,  55,  56,  80, 109,  81,  87,
283      95,  98, 103, 104, 103,  62,  77, 113,
284     121, 112, 100, 120,  92, 101, 103,  99
285 };
286
287
288 static const BYTE orig_chrom_quant[64] = {
289   #if 0
290     10,  14,  14,  24,  21,  24,  47,  26,
291   #else
292     17,  18,  18,  24,  21,  24,  47,  26,
293   #endif
294     26,  47,  99,  66,  56,  66,  99,  99,
295     99,  99,  99,  99,  99,  99,  99,  99,
296     99,  99,  99,  99,  99,  99,  99,  99,
297     99,  99,  99,  99,  99,  99,  99,  99,
298     99,  99,  99,  99,  99,  99,  99,  99,
299     99,  99,  99,  99,  99,  99,  99,  99,
300     99,  99,  99,  99,  99,  99,  99,  99
301 };
302
303
304 /*____________________________________________________________________________
305  |                |                                                           |
306  | codesize_array | Array giving # of bits required to represent the index    |
307  |________________|___________________________________________________________|
308 */
309
310 static const BYTE codesize_array[256] = {
311    0,
312    1,
313    2, 2,
314    3, 3, 3, 3,
315    4, 4, 4, 4, 4, 4, 4, 4,
316    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
317    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
318    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
319    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
320    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
321    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
322    7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
323    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
324    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
325    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
326    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
327    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
328    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
329    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
330    8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
331 };
332
333
334
335 /*____________________________________________________________________________
336  |                                                                            |
337  | Huffman Tables                                                             |
338  |____________________________________________________________________________|
339 */
340
341 static const BYTE lum_DC_counts[16] = {
342     0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
343     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
344 };
345
346 static const BYTE lum_DC_values[12] = {
347     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
348 };
349
350 static const BYTE chrom_DC_counts[16] = {
351     0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
352     0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
353 };
354
355 static const BYTE chrom_DC_values[12] = {
356     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
357 };
358
359 static const BYTE lum_AC_counts[16] = {
360     0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
361     0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d
362 };
363
364 static const BYTE lum_AC_counts_Denali[16] = {
365     0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
366     0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x00, 0x7e
367     /* Above, the [01,7d] in the normal table was changed to [00,7e].
368      * This alteration eliminates the sole 15-bit code, and yields
369      * Huffman codes as follows:
370      *    - common codes are 12 bits wide or less,
371      *    - uncommon codes are exactly 16 bits wide, and all those codes
372      *      start with nine '1' bits, leaving seven bits of useful info.
373      * Denali uses a 4K-entry table for the common codes, and a
374      * quick lookup for the 7-bit leftover codes.  So parsing of all
375      * codes is simple and fast.
376      */
377 };
378
379 static const BYTE lum_AC_values[162] = {
380     0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
381     0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
382     0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
383     0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
384     0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
385     0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
386     0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
387     0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
388     0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
389     0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
390     0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
391     0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
392     0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
393     0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
394     0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
395     0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
396     0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
397     0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
398     0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
399     0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
400     0xf9, 0xfa
401 };
402
403 static const BYTE chrom_AC_counts[16] = {
404     0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
405     0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77
406 };
407
408 static const BYTE chrom_AC_values[162] = {
409     0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
410     0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
411     0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
412     0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
413     0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
414     0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
415     0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
416     0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
417     0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
418     0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
419     0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
420     0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
421     0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
422     0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
423     0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
424     0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
425     0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
426     0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
427     0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
428     0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
429     0xf9, 0xfa
430 };
431
432
433
434 /*____________________________________________________________________________
435  |                                                                            |
436  | Huffman tables output by mk_jpg_huff program                               |
437  |____________________________________________________________________________|
438 */
439
440 typedef struct {
441     WORD code;    /* the Huff code to use */
442     BYTE size;    /* number of bits in above Huff code */
443 } huff_elem_t;
444
445
446 static const huff_elem_t lum_DC_table[] = {
447     { 0x0000,  2 }, { 0x0002,  3 }, { 0x0003,  3 }, { 0x0004,  3 },
448     { 0x0005,  3 }, { 0x0006,  3 }, { 0x000e,  4 }, { 0x001e,  5 },
449     { 0x003e,  6 }, { 0x007e,  7 }, { 0x00fe,  8 }, { 0x01fe,  9 },
450     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
451     { 0x0000,  0 }, };
452
453
454 static const huff_elem_t lum_AC_table[] = {
455     { 0x000a,  4 }, { 0x0000,  2 }, { 0x0001,  2 }, { 0x0004,  3 },
456     { 0x000b,  4 }, { 0x001a,  5 }, { 0x0078,  7 }, { 0x00f8,  8 },
457     { 0x03f6, 10 }, { 0xff82, 16 }, { 0xff83, 16 }, { 0x0000,  0 },
458     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
459     { 0x0000,  0 }, { 0x000c,  4 }, { 0x001b,  5 }, { 0x0079,  7 },
460     { 0x01f6,  9 }, { 0x07f6, 11 }, { 0xff84, 16 }, { 0xff85, 16 },
461     { 0xff86, 16 }, { 0xff87, 16 }, { 0xff88, 16 }, { 0x0000,  0 },
462     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
463     { 0x0000,  0 }, { 0x001c,  5 }, { 0x00f9,  8 }, { 0x03f7, 10 },
464     { 0x0ff4, 12 }, { 0xff89, 16 }, { 0xff8a, 16 }, { 0xff8b, 16 },
465     { 0xff8c, 16 }, { 0xff8d, 16 }, { 0xff8e, 16 }, { 0x0000,  0 },
466     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
467     { 0x0000,  0 }, { 0x003a,  6 }, { 0x01f7,  9 }, { 0x0ff5, 12 },
468     { 0xff8f, 16 }, { 0xff90, 16 }, { 0xff91, 16 }, { 0xff92, 16 },
469     { 0xff93, 16 }, { 0xff94, 16 }, { 0xff95, 16 }, { 0x0000,  0 },
470     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
471     { 0x0000,  0 }, { 0x003b,  6 }, { 0x03f8, 10 }, { 0xff96, 16 },
472     { 0xff97, 16 }, { 0xff98, 16 }, { 0xff99, 16 }, { 0xff9a, 16 },
473     { 0xff9b, 16 }, { 0xff9c, 16 }, { 0xff9d, 16 }, { 0x0000,  0 },
474     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
475     { 0x0000,  0 }, { 0x007a,  7 }, { 0x07f7, 11 }, { 0xff9e, 16 },
476     { 0xff9f, 16 }, { 0xffa0, 16 }, { 0xffa1, 16 }, { 0xffa2, 16 },
477     { 0xffa3, 16 }, { 0xffa4, 16 }, { 0xffa5, 16 }, { 0x0000,  0 },
478     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
479     { 0x0000,  0 }, { 0x007b,  7 }, { 0x0ff6, 12 }, { 0xffa6, 16 },
480     { 0xffa7, 16 }, { 0xffa8, 16 }, { 0xffa9, 16 }, { 0xffaa, 16 },
481     { 0xffab, 16 }, { 0xffac, 16 }, { 0xffad, 16 }, { 0x0000,  0 },
482     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
483     { 0x0000,  0 }, { 0x00fa,  8 }, { 0x0ff7, 12 }, { 0xffae, 16 },
484     { 0xffaf, 16 }, { 0xffb0, 16 }, { 0xffb1, 16 }, { 0xffb2, 16 },
485     { 0xffb3, 16 }, { 0xffb4, 16 }, { 0xffb5, 16 }, { 0x0000,  0 },
486     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
487     { 0x0000,  0 }, { 0x01f8,  9 }, { 0x7fc0, 15 }, { 0xffb6, 16 },
488     { 0xffb7, 16 }, { 0xffb8, 16 }, { 0xffb9, 16 }, { 0xffba, 16 },
489     { 0xffbb, 16 }, { 0xffbc, 16 }, { 0xffbd, 16 }, { 0x0000,  0 },
490     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
491     { 0x0000,  0 }, { 0x01f9,  9 }, { 0xffbe, 16 }, { 0xffbf, 16 },
492     { 0xffc0, 16 }, { 0xffc1, 16 }, { 0xffc2, 16 }, { 0xffc3, 16 },
493     { 0xffc4, 16 }, { 0xffc5, 16 }, { 0xffc6, 16 }, { 0x0000,  0 },
494     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
495     { 0x0000,  0 }, { 0x01fa,  9 }, { 0xffc7, 16 }, { 0xffc8, 16 },
496     { 0xffc9, 16 }, { 0xffca, 16 }, { 0xffcb, 16 }, { 0xffcc, 16 },
497     { 0xffcd, 16 }, { 0xffce, 16 }, { 0xffcf, 16 }, { 0x0000,  0 },
498     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
499     { 0x0000,  0 }, { 0x03f9, 10 }, { 0xffd0, 16 }, { 0xffd1, 16 },
500     { 0xffd2, 16 }, { 0xffd3, 16 }, { 0xffd4, 16 }, { 0xffd5, 16 },
501     { 0xffd6, 16 }, { 0xffd7, 16 }, { 0xffd8, 16 }, { 0x0000,  0 },
502     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
503     { 0x0000,  0 }, { 0x03fa, 10 }, { 0xffd9, 16 }, { 0xffda, 16 },
504     { 0xffdb, 16 }, { 0xffdc, 16 }, { 0xffdd, 16 }, { 0xffde, 16 },
505     { 0xffdf, 16 }, { 0xffe0, 16 }, { 0xffe1, 16 }, { 0x0000,  0 },
506     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
507     { 0x0000,  0 }, { 0x07f8, 11 }, { 0xffe2, 16 }, { 0xffe3, 16 },
508     { 0xffe4, 16 }, { 0xffe5, 16 }, { 0xffe6, 16 }, { 0xffe7, 16 },
509     { 0xffe8, 16 }, { 0xffe9, 16 }, { 0xffea, 16 }, { 0x0000,  0 },
510     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
511     { 0x0000,  0 }, { 0xffeb, 16 }, { 0xffec, 16 }, { 0xffed, 16 },
512     { 0xffee, 16 }, { 0xffef, 16 }, { 0xfff0, 16 }, { 0xfff1, 16 },
513     { 0xfff2, 16 }, { 0xfff3, 16 }, { 0xfff4, 16 }, { 0x0000,  0 },
514     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
515     { 0x07f9, 11 }, { 0xfff5, 16 }, { 0xfff6, 16 }, { 0xfff7, 16 },
516     { 0xfff8, 16 }, { 0xfff9, 16 }, { 0xfffa, 16 }, { 0xfffb, 16 },
517     { 0xfffc, 16 }, { 0xfffd, 16 }, { 0xfffe, 16 }, { 0x0000,  0 },
518     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
519 };
520
521
522 static const huff_elem_t lum_AC_table_Denali[] = {
523     { 0x000a,  4 }, { 0x0000,  2 }, { 0x0001,  2 }, { 0x0004,  3 },
524     { 0x000b,  4 }, { 0x001a,  5 }, { 0x0078,  7 }, { 0x00f8,  8 },
525     { 0x03f6, 10 }, { 0xff81, 16 }, { 0xff82, 16 }, { 0x0000,  0 },
526     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
527     { 0x0000,  0 }, { 0x000c,  4 }, { 0x001b,  5 }, { 0x0079,  7 },
528     { 0x01f6,  9 }, { 0x07f6, 11 }, { 0xff83, 16 }, { 0xff84, 16 },
529     { 0xff85, 16 }, { 0xff86, 16 }, { 0xff87, 16 }, { 0x0000,  0 },
530     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
531     { 0x0000,  0 }, { 0x001c,  5 }, { 0x00f9,  8 }, { 0x03f7, 10 },
532     { 0x0ff4, 12 }, { 0xff88, 16 }, { 0xff89, 16 }, { 0xff8a, 16 },
533     { 0xff8b, 16 }, { 0xff8c, 16 }, { 0xff8d, 16 }, { 0x0000,  0 },
534     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
535     { 0x0000,  0 }, { 0x003a,  6 }, { 0x01f7,  9 }, { 0x0ff5, 12 },
536     { 0xff8e, 16 }, { 0xff8f, 16 }, { 0xff90, 16 }, { 0xff91, 16 },
537     { 0xff92, 16 }, { 0xff93, 16 }, { 0xff94, 16 }, { 0x0000,  0 },
538     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
539     { 0x0000,  0 }, { 0x003b,  6 }, { 0x03f8, 10 }, { 0xff95, 16 },
540     { 0xff96, 16 }, { 0xff97, 16 }, { 0xff98, 16 }, { 0xff99, 16 },
541     { 0xff9a, 16 }, { 0xff9b, 16 }, { 0xff9c, 16 }, { 0x0000,  0 },
542     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
543     { 0x0000,  0 }, { 0x007a,  7 }, { 0x07f7, 11 }, { 0xff9d, 16 },
544     { 0xff9e, 16 }, { 0xff9f, 16 }, { 0xffa0, 16 }, { 0xffa1, 16 },
545     { 0xffa2, 16 }, { 0xffa3, 16 }, { 0xffa4, 16 }, { 0x0000,  0 },
546     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
547     { 0x0000,  0 }, { 0x007b,  7 }, { 0x0ff6, 12 }, { 0xffa5, 16 },
548     { 0xffa6, 16 }, { 0xffa7, 16 }, { 0xffa8, 16 }, { 0xffa9, 16 },
549     { 0xffaa, 16 }, { 0xffab, 16 }, { 0xffac, 16 }, { 0x0000,  0 },
550     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
551     { 0x0000,  0 }, { 0x00fa,  8 }, { 0x0ff7, 12 }, { 0xffad, 16 },
552     { 0xffae, 16 }, { 0xffaf, 16 }, { 0xffb0, 16 }, { 0xffb1, 16 },
553     { 0xffb2, 16 }, { 0xffb3, 16 }, { 0xffb4, 16 }, { 0x0000,  0 },
554     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
555     { 0x0000,  0 }, { 0x01f8,  9 }, { 0xff80, 16 }, { 0xffb5, 16 },
556     { 0xffb6, 16 }, { 0xffb7, 16 }, { 0xffb8, 16 }, { 0xffb9, 16 },
557     { 0xffba, 16 }, { 0xffbb, 16 }, { 0xffbc, 16 }, { 0x0000,  0 },
558     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
559     { 0x0000,  0 }, { 0x01f9,  9 }, { 0xffbd, 16 }, { 0xffbe, 16 },
560     { 0xffbf, 16 }, { 0xffc0, 16 }, { 0xffc1, 16 }, { 0xffc2, 16 },
561     { 0xffc3, 16 }, { 0xffc4, 16 }, { 0xffc5, 16 }, { 0x0000,  0 },
562     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
563     { 0x0000,  0 }, { 0x01fa,  9 }, { 0xffc6, 16 }, { 0xffc7, 16 },
564     { 0xffc8, 16 }, { 0xffc9, 16 }, { 0xffca, 16 }, { 0xffcb, 16 },
565     { 0xffcc, 16 }, { 0xffcd, 16 }, { 0xffce, 16 }, { 0x0000,  0 },
566     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
567     { 0x0000,  0 }, { 0x03f9, 10 }, { 0xffcf, 16 }, { 0xffd0, 16 },
568     { 0xffd1, 16 }, { 0xffd2, 16 }, { 0xffd3, 16 }, { 0xffd4, 16 },
569     { 0xffd5, 16 }, { 0xffd6, 16 }, { 0xffd7, 16 }, { 0x0000,  0 },
570     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
571     { 0x0000,  0 }, { 0x03fa, 10 }, { 0xffd8, 16 }, { 0xffd9, 16 },
572     { 0xffda, 16 }, { 0xffdb, 16 }, { 0xffdc, 16 }, { 0xffdd, 16 },
573     { 0xffde, 16 }, { 0xffdf, 16 }, { 0xffe0, 16 }, { 0x0000,  0 },
574     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
575     { 0x0000,  0 }, { 0x07f8, 11 }, { 0xffe1, 16 }, { 0xffe2, 16 },
576     { 0xffe3, 16 }, { 0xffe4, 16 }, { 0xffe5, 16 }, { 0xffe6, 16 },
577     { 0xffe7, 16 }, { 0xffe8, 16 }, { 0xffe9, 16 }, { 0x0000,  0 },
578     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
579     { 0x0000,  0 }, { 0xffea, 16 }, { 0xffeb, 16 }, { 0xffec, 16 },
580     { 0xffed, 16 }, { 0xffee, 16 }, { 0xffef, 16 }, { 0xfff0, 16 },
581     { 0xfff1, 16 }, { 0xfff2, 16 }, { 0xfff3, 16 }, { 0x0000,  0 },
582     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
583     { 0x07f9, 11 }, { 0xfff4, 16 }, { 0xfff5, 16 }, { 0xfff6, 16 },
584     { 0xfff7, 16 }, { 0xfff8, 16 }, { 0xfff9, 16 }, { 0xfffa, 16 },
585     { 0xfffb, 16 }, { 0xfffc, 16 }, { 0xfffd, 16 }, { 0x0000,  0 },
586     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
587 };
588
589
590 static const huff_elem_t chrom_DC_table[] = {
591     { 0x0000,  2 }, { 0x0001,  2 }, { 0x0002,  2 }, { 0x0006,  3 },
592     { 0x000e,  4 }, { 0x001e,  5 }, { 0x003e,  6 }, { 0x007e,  7 },
593     { 0x00fe,  8 }, { 0x01fe,  9 }, { 0x03fe, 10 }, { 0x07fe, 11 },
594     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
595     { 0x0000,  0 }, };
596
597
598 static const huff_elem_t chrom_AC_table[] = {
599     { 0x0000,  2 }, { 0x0001,  2 }, { 0x0004,  3 }, { 0x000a,  4 },
600     { 0x0018,  5 }, { 0x0019,  5 }, { 0x0038,  6 }, { 0x0078,  7 },
601     { 0x01f4,  9 }, { 0x03f6, 10 }, { 0x0ff4, 12 }, { 0x0000,  0 },
602     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
603     { 0x0000,  0 }, { 0x000b,  4 }, { 0x0039,  6 }, { 0x00f6,  8 },
604     { 0x01f5,  9 }, { 0x07f6, 11 }, { 0x0ff5, 12 }, { 0xff88, 16 },
605     { 0xff89, 16 }, { 0xff8a, 16 }, { 0xff8b, 16 }, { 0x0000,  0 },
606     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
607     { 0x0000,  0 }, { 0x001a,  5 }, { 0x00f7,  8 }, { 0x03f7, 10 },
608     { 0x0ff6, 12 }, { 0x7fc2, 15 }, { 0xff8c, 16 }, { 0xff8d, 16 },
609     { 0xff8e, 16 }, { 0xff8f, 16 }, { 0xff90, 16 }, { 0x0000,  0 },
610     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
611     { 0x0000,  0 }, { 0x001b,  5 }, { 0x00f8,  8 }, { 0x03f8, 10 },
612     { 0x0ff7, 12 }, { 0xff91, 16 }, { 0xff92, 16 }, { 0xff93, 16 },
613     { 0xff94, 16 }, { 0xff95, 16 }, { 0xff96, 16 }, { 0x0000,  0 },
614     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
615     { 0x0000,  0 }, { 0x003a,  6 }, { 0x01f6,  9 }, { 0xff97, 16 },
616     { 0xff98, 16 }, { 0xff99, 16 }, { 0xff9a, 16 }, { 0xff9b, 16 },
617     { 0xff9c, 16 }, { 0xff9d, 16 }, { 0xff9e, 16 }, { 0x0000,  0 },
618     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
619     { 0x0000,  0 }, { 0x003b,  6 }, { 0x03f9, 10 }, { 0xff9f, 16 },
620     { 0xffa0, 16 }, { 0xffa1, 16 }, { 0xffa2, 16 }, { 0xffa3, 16 },
621     { 0xffa4, 16 }, { 0xffa5, 16 }, { 0xffa6, 16 }, { 0x0000,  0 },
622     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
623     { 0x0000,  0 }, { 0x0079,  7 }, { 0x07f7, 11 }, { 0xffa7, 16 },
624     { 0xffa8, 16 }, { 0xffa9, 16 }, { 0xffaa, 16 }, { 0xffab, 16 },
625     { 0xffac, 16 }, { 0xffad, 16 }, { 0xffae, 16 }, { 0x0000,  0 },
626     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
627     { 0x0000,  0 }, { 0x007a,  7 }, { 0x07f8, 11 }, { 0xffaf, 16 },
628     { 0xffb0, 16 }, { 0xffb1, 16 }, { 0xffb2, 16 }, { 0xffb3, 16 },
629     { 0xffb4, 16 }, { 0xffb5, 16 }, { 0xffb6, 16 }, { 0x0000,  0 },
630     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
631     { 0x0000,  0 }, { 0x00f9,  8 }, { 0xffb7, 16 }, { 0xffb8, 16 },
632     { 0xffb9, 16 }, { 0xffba, 16 }, { 0xffbb, 16 }, { 0xffbc, 16 },
633     { 0xffbd, 16 }, { 0xffbe, 16 }, { 0xffbf, 16 }, { 0x0000,  0 },
634     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
635     { 0x0000,  0 }, { 0x01f7,  9 }, { 0xffc0, 16 }, { 0xffc1, 16 },
636     { 0xffc2, 16 }, { 0xffc3, 16 }, { 0xffc4, 16 }, { 0xffc5, 16 },
637     { 0xffc6, 16 }, { 0xffc7, 16 }, { 0xffc8, 16 }, { 0x0000,  0 },
638     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
639     { 0x0000,  0 }, { 0x01f8,  9 }, { 0xffc9, 16 }, { 0xffca, 16 },
640     { 0xffcb, 16 }, { 0xffcc, 16 }, { 0xffcd, 16 }, { 0xffce, 16 },
641     { 0xffcf, 16 }, { 0xffd0, 16 }, { 0xffd1, 16 }, { 0x0000,  0 },
642     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
643     { 0x0000,  0 }, { 0x01f9,  9 }, { 0xffd2, 16 }, { 0xffd3, 16 },
644     { 0xffd4, 16 }, { 0xffd5, 16 }, { 0xffd6, 16 }, { 0xffd7, 16 },
645     { 0xffd8, 16 }, { 0xffd9, 16 }, { 0xffda, 16 }, { 0x0000,  0 },
646     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
647     { 0x0000,  0 }, { 0x01fa,  9 }, { 0xffdb, 16 }, { 0xffdc, 16 },
648     { 0xffdd, 16 }, { 0xffde, 16 }, { 0xffdf, 16 }, { 0xffe0, 16 },
649     { 0xffe1, 16 }, { 0xffe2, 16 }, { 0xffe3, 16 }, { 0x0000,  0 },
650     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
651     { 0x0000,  0 }, { 0x07f9, 11 }, { 0xffe4, 16 }, { 0xffe5, 16 },
652     { 0xffe6, 16 }, { 0xffe7, 16 }, { 0xffe8, 16 }, { 0xffe9, 16 },
653     { 0xffea, 16 }, { 0xffeb, 16 }, { 0xffec, 16 }, { 0x0000,  0 },
654     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
655     { 0x0000,  0 }, { 0x3fe0, 14 }, { 0xffed, 16 }, { 0xffee, 16 },
656     { 0xffef, 16 }, { 0xfff0, 16 }, { 0xfff1, 16 }, { 0xfff2, 16 },
657     { 0xfff3, 16 }, { 0xfff4, 16 }, { 0xfff5, 16 }, { 0x0000,  0 },
658     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
659     { 0x03fa, 10 }, { 0x7fc3, 15 }, { 0xfff6, 16 }, { 0xfff7, 16 },
660     { 0xfff8, 16 }, { 0xfff9, 16 }, { 0xfffa, 16 }, { 0xfffb, 16 },
661     { 0xfffc, 16 }, { 0xfffd, 16 }, { 0xfffe, 16 }, { 0x0000,  0 },
662     { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 }, { 0x0000,  0 },
663 };
664
665
666
667 /******************************************************************************
668  ******************************************************************************
669
670                             WRITING-BITS SECTION
671
672  ******************************************************************************
673  ******************************************************************************
674
675
676  Interface into this section:
677
678     write_init          - inits this section
679     write_buf_open      - we are being given a (new) output buffer
680     write_buf_close     - done with current output buffer; return # bytes sent
681     write_buf_next      - (a var) the next byte to write in the output buffer
682     WRITE_BITS_OPEN     - setup code for WRITE_BITS
683     WRITE_BITS          - outputs bits (fast)
684     WRITE_BITS_HIZERO   - outputs bits (faster) assuming higher-order bits are 0
685     WRITE_BITS_CLOSE    - teardown code for WRITE_BITS
686     write_bits_flush    - flushes the bit-cache
687
688  ******************************************************************************/
689
690
691
692 #define INITIAL_BITS_AVAIL (8*sizeof(DWORD))
693     /* Number of bits in wr_bit_buf, which is the # of bits in a DWORD . */
694
695
696 /*____________________________________________________________________________
697  |                |                                                           |
698  | PUT_BYTE_STUFF | Puts byte into outbuf, with byte-stuffing as needed       |
699  |________________|___________________________________________________________|
700  |                                                                            |
701  | A 0xff byte within bit-data is always followed by a 0x00 byte to           |
702  | distinguish it from markers, which are 0xff followed by a non-zero byte.   |
703  | Therefore, PUT_BYTE_STUFF should never be used to write markers.           |
704  |____________________________________________________________________________|
705 */
706 #define PUT_BYTE_STUFF(g, pbs_byte_expression) {               \
707     BYTE pbs_byte;                                             \
708                                                                \
709     pbs_byte = (BYTE)(pbs_byte_expression);                    \
710     *(g->write_buf_next)++ = pbs_byte;                         \
711     if (pbs_byte == (BYTE)0xff)                                \
712         *(g->write_buf_next)++ = 0;                            \
713 }
714
715
716
717 /*____________________________________________________________________________
718  |            |                                                               |
719  | write_init | Inits this package, and registers the "write bytes" callback  |
720  |____________|_______________________________________________________________|
721 */
722 static void write_init (PJENC_INST g)
723 {
724     g->wr_bits_avail = INITIAL_BITS_AVAIL;
725     g->wr_bit_buf = 0;
726 }
727
728
729
730 /*____________________________________________________________________________
731  |                |                                                           |
732  | write_buf_open | We are being given a (new) buffer to receive output       |
733  |________________|___________________________________________________________|
734  |                                                                            |
735  | This routine records the location of the new output buffer.                |
736  |____________________________________________________________________________|
737 */
738 static void write_buf_open (PJENC_INST g, BYTE *buf_p)
739 {
740     g->wr_outbuf_beg = g->write_buf_next = buf_p;
741 }
742
743
744
745 /*____________________________________________________________________________
746  |                 |                                                          |
747  | write_buf_close | We are done with the current output buffer               |
748  |_________________|__________________________________________________________|
749  |                                                                            |
750  | This function returns # bytes written to the output buffer                 |
751  |____________________________________________________________________________|
752 */
753 static int write_buf_close (PJENC_INST g)
754 {
755     return g->write_buf_next - g->wr_outbuf_beg;
756 }
757
758
759
760 /*____________________________________________________________________________
761  |            |                                                               |
762  | WRITE_BITS | Writes the given number of bits                               |
763  |____________|_______________________________________________________________|
764  |                                                                            |
765  | WRITE_BITS_HIZERO assumes that the higher-order bits are all zero.         |
766  | WRITE_BITS masks them out for you.                                         |
767  |____________________________________________________________________________|
768 */
769
770 #define WRITE_BITS_OPEN(g)                         \
771     int    tmp_bits_avail = g->wr_bits_avail;      \
772     DWORD  tmp_bit_buf    = g->wr_bit_buf;
773
774
775 #define WRITE_BITS_HIZERO(g,wb_value,wb_nbits) {                        \
776     DWORD  bits_loc;                                                    \
777     int    length_loc;                                                  \
778                                                                         \
779     length_loc = (int)(wb_nbits);                                       \
780     bits_loc = (DWORD)(wb_value);                                       \
781                                                                         \
782     if (length_loc > tmp_bits_avail) {                                  \
783         do {                                                            \
784             PUT_BYTE_STUFF (g, tmp_bit_buf >> 24)                       \
785             tmp_bit_buf <<= 8;                                          \
786             tmp_bits_avail += 8;                                        \
787         } while (tmp_bits_avail <= 24);                                 \
788     }                                                                   \
789                                                                         \
790     tmp_bits_avail -= length_loc;                                       \
791     tmp_bit_buf |= bits_loc << tmp_bits_avail;                          \
792 }
793
794
795 #define WRITE_BITS(g,wb_value,wb_nbits) {                                   \
796     int len_loc;                                                            \
797                                                                             \
798     len_loc = (int)(wb_nbits);                                              \
799     WRITE_BITS_HIZERO (g, (DWORD )(wb_value) & ((1ul<<len_loc)-1), len_loc) \
800 }
801
802
803 #define WRITE_BITS_CLOSE(g) {                      \
804     g->wr_bits_avail = tmp_bits_avail;             \
805     g->wr_bit_buf    = tmp_bit_buf;                \
806 }
807
808
809
810 /*____________________________________________________________________________
811  |                  |                                                         |
812  | write_bits_flush | Writes any buffered bits, with buffering via outbuf     |
813  |__________________|_________________________________________________________|
814  |                                                                            |
815  | Bits are right-padded with ones if necessary to a byte-boundary.           |
816  |____________________________________________________________________________|
817 */
818 static void write_bits_flush (PJENC_INST g)
819 {
820     int bits_used;
821
822     if (g->wr_bits_avail != INITIAL_BITS_AVAIL) {
823         /* JPEG wants pad-bits to be 1's */
824         g->wr_bit_buf |= ((DWORD )1 << g->wr_bits_avail) - 1;
825
826         for (bits_used = INITIAL_BITS_AVAIL - g->wr_bits_avail;
827              bits_used > 0;
828              bits_used -= 8) {
829             PUT_BYTE_STUFF (g, g->wr_bit_buf >> 24)
830             g->wr_bit_buf <<= 8;
831         }
832
833         g->wr_bits_avail = INITIAL_BITS_AVAIL;
834         g->wr_bit_buf = 0;
835     }
836 }
837
838
839
840 /******************************************************************************
841  ******************************************************************************
842
843                               MARKERS SECTION
844
845  ******************************************************************************
846  ******************************************************************************/
847
848
849
850 /*____________________________________________________________________________
851  |                                                                            |
852  | Macros                                                                     |
853  |____________________________________________________________________________|
854 */
855
856
857 #define PUT_MARKER(g,marker)                    \
858 do {                                            \
859     *(g->write_buf_next)++ = 0xff;              \
860     *(g->write_buf_next)++ = (BYTE)marker;      \
861 } while (0)
862
863
864 #define PUT_INT(g,value)                        \
865 do {                                            \
866     unsigned loc_val = (unsigned)(value);       \
867     *(g->write_buf_next)++ = loc_val >> 8;      \
868     *(g->write_buf_next)++ = loc_val & 0xff;    \
869 } while (0)
870
871
872
873 /*____________________________________________________________________________
874  |              |                                                             |
875  | em_write_SOI | Writes Start-Of-Image marker                                |
876  |______________|_____________________________________________________________|
877 */
878 static void em_write_SOI (PJENC_INST g)
879 {
880     PUT_MARKER(g, MARKER_SOI);
881 }
882
883
884 /*____________________________________________________________________________
885  |              |                                                             |
886  | em_write_EOI | Writes End-Of-Image marker                                  |
887  |______________|_____________________________________________________________|
888 */
889 static void em_write_EOI (PJENC_INST g)
890 {
891     PUT_MARKER(g, MARKER_EOI);
892 }
893
894
895 /*____________________________________________________________________________
896  |                    |                                                       |
897  | em_write_JFIF_APP0 | Writes Application marker for JFIF 1,0                |
898  |____________________|_______________________________________________________|
899 */
900 static void em_write_JFIF_APP0 (
901     PJENC_INST g,
902     int        xRes,    /* X resolution of image (number of pixels per inch) */
903     int        yRes)    /* Y resolution of image (number of pixels per inch) */
904 {
905     PUT_MARKER (g, MARKER_APP+0);
906     PUT_INT (g, 16);  /* the length */
907     
908     /*** ID ***/
909     *(g->write_buf_next)++ = 'J';
910     *(g->write_buf_next)++ = 'F';
911     *(g->write_buf_next)++ = 'I';
912     *(g->write_buf_next)++ = 'F';
913     *(g->write_buf_next)++ = '\0';
914     
915     /*** Version 1.0 ***/
916     *(g->write_buf_next)++ = 0x01;
917     *(g->write_buf_next)++ = 0x00;
918     
919     /*** Units (dots per inch) ***/
920     *(g->write_buf_next)++ = 0x01;
921     
922     PUT_INT(g, xRes);
923     PUT_INT(g, yRes);
924
925     /*** Thumbnail X and Y ***/
926     *(g->write_buf_next)++ = 0x00;
927     *(g->write_buf_next)++ = 0x00;
928 }
929
930
931 /*____________________________________________________________________________
932  |                  |                                                         |
933  | em_write_G3_APP1 | Writes Application marker for G3 color fax standard     |
934  |__________________|_________________________________________________________|
935 */
936 static void em_write_G3_APP1 (
937     PJENC_INST g,
938     int        res)    /* resolution of image (number of pixels per inch) */
939 {
940     res = ((res+50)/100)*100;   /* round res to the nearest 100 */
941
942     PUT_MARKER (g, MARKER_APP+1);
943     PUT_INT (g, 12);  /* the length */
944
945     /*** ID ***/
946     *(g->write_buf_next)++ = 'G';
947     *(g->write_buf_next)++ = '3';
948     *(g->write_buf_next)++ = 'F';
949     *(g->write_buf_next)++ = 'A';
950     *(g->write_buf_next)++ = 'X';
951     *(g->write_buf_next)++ = 0;
952
953     PUT_INT (g, 1994);   /* version is year the std was approved */
954     PUT_INT (g, res);    /* finally, the DPI */
955 }
956
957
958 /*____________________________________________________________________________
959  |              |                                                             |
960  | em_write_SOF | Writes Start-Of-Frame marker                                |
961  |______________|_____________________________________________________________|
962 */
963 static void em_write_SOF (
964     PJENC_INST g,
965     int        width,         /* width of image (number of pixels per row) */
966     int        height,        /* height of image (number of rows) */
967     int        ncomps,        /* # of color components; 1 is gray, 3 is color */
968     BYTE       h_sam_facs[],  /* horizontal sampling factors */
969     BYTE       v_sam_facs[])  /* vertical sampling factors */
970 {
971     int i;
972
973     PUT_MARKER(g, MARKER_SOF0);
974     PUT_INT(g, 8 + ncomps*3);  /* the length */
975     *(g->write_buf_next)++ = 8;
976     PUT_INT(g, height);
977     PUT_INT(g, width);
978     *(g->write_buf_next)++ = ncomps;
979
980     for (i=0; i<ncomps; i++) {
981        *(g->write_buf_next)++ = i;
982        *(g->write_buf_next)++ = h_sam_facs[i]<<4 | v_sam_facs[i];
983        *(g->write_buf_next)++ = (i==0 ? 0 : 1);
984     }
985 }
986
987
988 /*____________________________________________________________________________
989  |              |                                                             |
990  | em_write_DQT | Writes Define-Quantization-Table marker                     |
991  |______________|_____________________________________________________________|
992 */
993 static void em_write_DQT (
994     PJENC_INST g,
995     int        precision,        /* 0 = 8-bit, 1 = 16-bit */
996     int        ident,            /* which table, 0-3 */
997     BYTE       elements[64])
998 {
999     PUT_MARKER(g, MARKER_DQT);
1000     PUT_INT(g, 67);  /* the length */
1001     *(g->write_buf_next)++ = (precision << 4) + ident;
1002     memcpy (g->write_buf_next, elements, 64);
1003     g->write_buf_next += 64;
1004 }
1005
1006
1007 /*____________________________________________________________________________
1008  |               |                                                            |
1009  | em_write_DHTs | Writes Define-Huffman-Tables marker                        |
1010  |_______________|____________________________________________________________|
1011 */
1012 static void em_write_DHTs (
1013     PJENC_INST  g,
1014     int         ntables,          /* number of tables */
1015     BYTE        hclass[],         /* 0 = DC or lossless table, 1 = AC table */
1016     BYTE        ident[],          /* 0-3 = which Huffman table this is */
1017     const BYTE *counts[],    /* # Huffman codes of lengths 1-16 */
1018     const BYTE *huffval[])   /* list of associated values */
1019 {
1020     int nvals, i, j;
1021
1022     nvals = 0;
1023     for (i=0; i<ntables; i++) {
1024         for (j=0; j<16; j++)
1025             nvals += counts[i][j];
1026     }
1027
1028     PUT_MARKER(g, MARKER_DHT);
1029     PUT_INT(g, 2 + 17*ntables + nvals);  /* the length */
1030
1031     for (i=0; i<ntables; i++) {
1032         for (nvals=j=0; j<16; j++)
1033             nvals += counts[i][j];
1034
1035         *(g->write_buf_next)++ = hclass[i]<<4 | ident[i];
1036         for (j=0; j<16; j++)    *(g->write_buf_next)++ = counts[i][j];
1037         for (j=0; j<nvals; j++) *(g->write_buf_next)++ = huffval[i][j];
1038     }
1039 }
1040
1041
1042 /*____________________________________________________________________________
1043  |              |                                                             |
1044  | em_write_SOS | Writes Start-Of-Scan marker                                 |
1045  |______________|_____________________________________________________________|
1046 */
1047 static void em_write_SOS (
1048     PJENC_INST g,
1049     int        ncomps)  /* number of color components; 1 (gray) or 3 (color) */
1050 {
1051     int i;
1052
1053     PUT_MARKER(g, MARKER_SOS);
1054     PUT_INT(g, 6 + 2*ncomps);  /* the length */
1055     *(g->write_buf_next)++ = ncomps;
1056
1057     for (i=0; i<ncomps; i++) {
1058         *(g->write_buf_next)++ = i;
1059         *(g->write_buf_next)++ = (i==0 ? 0x00 : 0x11);
1060     }
1061
1062     *(g->write_buf_next)++ = 0;
1063     *(g->write_buf_next)++ = 63;
1064     *(g->write_buf_next)++ = 0;
1065 }
1066
1067
1068 /*____________________________________________________________________________
1069  |              |                                                             |
1070  | em_write_DNL | Writes Define-Number-of-Lines marker                        |
1071  |______________|_____________________________________________________________|
1072 */
1073 static void em_write_DNL (
1074     PJENC_INST g,
1075     int nlines)  /* number of lines (raster rows) in the JPEG file */
1076 {
1077     PUT_MARKER(g, MARKER_DNL);
1078     PUT_INT(g, 4);  /* the length */
1079     PUT_INT(g, nlines);
1080 }
1081
1082
1083
1084 /*____________________________________________________________________________
1085  |                       |                                                    |
1086  | em_write_short_header | Writes a short non-standard header                 |
1087  |_______________________|____________________________________________________|
1088  |                                                                            |
1089  | This header is also output by the firmware, and is only in this            |
1090  | JPEG encoder for testing purposes.                                         |
1091  |____________________________________________________________________________|
1092 */
1093 static void em_write_short_header (
1094     PJENC_INST g,
1095     UINT  rows_in_page,
1096     UINT  pixels_in_row,
1097     UINT  xRes,
1098     UINT  yRes,
1099     UINT  dc_q_factor,
1100     UINT  ac_q_factor,
1101     UINT  comps_in_pixel,
1102     DWORD sample_factors)
1103 {
1104     PUT_MARKER (g, MARKER_SHORT_HEADER);
1105     PUT_INT (g, 18);                              /* the length */
1106     PUT_INT (g, rows_in_page);
1107     PUT_INT (g, pixels_in_row);
1108     PUT_INT (g, xRes);
1109     PUT_INT (g, yRes);
1110     *(g->write_buf_next)++ = ac_q_factor * 5 / 2;
1111     *(g->write_buf_next)++ = comps_in_pixel;
1112     PUT_INT (g, sample_factors >> 16);               /* horiz sample factors */
1113     PUT_INT (g, sample_factors & 0x0000ffffu);       /* vert  sample factors */
1114     *(g->write_buf_next)++ = dc_q_factor * 5 / 2;
1115     *(g->write_buf_next)++ = 0;                   /* reserved for future use */
1116 }
1117
1118
1119
1120 /******************************************************************************
1121  ******************************************************************************
1122
1123                            WINOGRAD DCT SECTION
1124
1125  ******************************************************************************
1126  ******************************************************************************/
1127
1128
1129
1130 #define BITS_IN_DCT_FRAC 15
1131 #define ROUND(x) (((x)+(1l<<(BITS_IN_DCT_FRAC-1))) >> BITS_IN_DCT_FRAC)
1132
1133
1134 /*____________________________________________________________________________
1135  |          |                                                                 |
1136  | As table | The As multiplication constants used in the Winograd transform  |
1137  |__________|_________________________________________________________________|
1138  |                                                                            |
1139  | These have 15 bits of fraction.                                            |
1140  |____________________________________________________________________________|
1141 */
1142 #define a1 23170L 
1143 #define a2 17734L
1144 #define a3 23170L
1145 #define a4 42813L
1146 #define a5 12540L
1147
1148
1149 /*____________________________________________________________________________
1150  |               |                                                            |
1151  | wino_norm_tbl | DCT scale-factors; this table has been zigzagged           |
1152  |_______________|____________________________________________________________|
1153 */
1154 /* todo: do this in fixed-point (see image_proc.c in firmware) */
1155 static float const wino_norm_tbl[] = {
1156     0.125000f, 0.090120f, 0.090120f, 0.095671f,
1157     0.064973f, 0.095671f, 0.106304f, 0.068975f,
1158     0.068975f, 0.106304f, 0.125000f, 0.076641f,
1159     0.073223f, 0.076641f, 0.125000f, 0.159095f,
1160     0.090120f, 0.081361f, 0.081361f, 0.090120f,
1161     0.159095f, 0.230970f, 0.114701f, 0.095671f,
1162     0.090404f, 0.095671f, 0.114701f, 0.230970f,
1163     0.453064f, 0.166520f, 0.121766f, 0.106304f,
1164     0.106304f, 0.121766f, 0.166520f, 0.453064f,
1165     0.326641f, 0.176777f, 0.135299f, 0.125000f,
1166     0.135299f, 0.176777f, 0.326641f, 0.346760f,
1167     0.196424f, 0.159095f, 0.159095f, 0.196424f,
1168     0.346760f, 0.385299f, 0.230970f, 0.202489f,
1169     0.230970f, 0.385299f, 0.453064f, 0.293969f,
1170     0.293969f, 0.453064f, 0.576641f, 0.426777f,
1171     0.576641f, 0.837153f, 0.837153f, 1.642134f
1172 };
1173
1174
1175 /*____________________________________________________________________________
1176  |                |                                                           |
1177  | scale_for_wino | Computes quant-table and threshold-table for Wino's DCT   |
1178  |________________|___________________________________________________________|
1179 */
1180 static void scale_for_wino (
1181     BYTE *in,        /* in:  regular quantization table */
1182     int  *out,       /* out: winograd quantization table */
1183     int  *thresh)    /* out: threshold table */
1184 {
1185     #define FIX(x)  ((long) ((x)*(1l<<BITS_IN_DCT_FRAC) + 0.5))
1186     float const *fptr;
1187     int i, q;
1188
1189     /* Note that in order for FIX(fptr[i]/in[i]) to fit inside 16-bit signed
1190      * int, (fptr[i]/in[i] < 1) => (in[i] > fptr[i]) (1). Since in[i] >= 1,
1191      * and (fptr[i] < 1) for i = 0 to 62, (1) is true for i = 0 to 62. For
1192      * i = 63, (1) is true only when in[63] >= 2
1193      */
1194     if (in[63] < 2) in[63] = 2;
1195
1196     fptr = wino_norm_tbl;
1197     for (i=0; i<64; i++) {
1198         q = FIX (*fptr++ / (float)(*in++));
1199         *out++ = q;
1200         if (q == 0) *thresh++ = 32767;
1201         else        *thresh++ = (1 << (BITS_IN_DCT_FRAC-1)) / q;
1202     }
1203 }
1204
1205
1206
1207 /******************************************************************************
1208  ******************************************************************************
1209
1210                               E N C O D I N G
1211
1212  ******************************************************************************
1213  ******************************************************************************/
1214
1215
1216
1217 static const BYTE zigzag_index_table[64] = {
1218      0,  1,  8, 16,  9,  2,  3, 10,
1219     17, 24, 32, 25, 18, 11,  4,  5,
1220     12, 19, 26, 33, 40, 48, 41, 34,
1221     27, 20, 13,  6,  7, 14, 21, 28,
1222     35, 42, 49, 56, 57, 50, 43, 36,
1223     29, 22, 15, 23, 30, 37, 44, 51,
1224     58, 59, 52, 45, 38, 31, 39, 46,
1225     53, 60, 61, 54, 47, 55, 62, 63
1226 };
1227
1228
1229 #define WRITE_HUFF_CODE(g,val,huffman) {                                \
1230     int whc_val = (int)(val);                                                 \
1231     WRITE_BITS_HIZERO (g, huffman[whc_val].code, huffman[whc_val].size)    \
1232 }
1233
1234
1235
1236 static void zero_prior_DC (PJENC_INST g)
1237 {
1238     g->prior_DC[0] = g->prior_DC[1] = g->prior_DC[2] = g->prior_DC[3] = 0;
1239 }
1240
1241
1242
1243 /*____________________________________________________________________________
1244  |             |                                                              |
1245  | encode_init | Inits this section                                           |
1246  |_____________|______________________________________________________________|
1247 */
1248 static void encode_init (PJENC_INST g)
1249 {
1250     BYTE const *zig_p;
1251     int       **block_pp;
1252
1253     zig_p = zigzag_index_table;
1254     for (block_pp=g->enc_block_zz; block_pp<g->enc_block_zz+(64+16); block_pp++)
1255         *block_pp = &(g->enc_block[*zig_p++]);
1256 }
1257
1258
1259
1260 /*____________________________________________________________________________
1261  |              |                                                             |
1262  | encode_block | Encodes an 8x8 block of pixels into Huffman data            |
1263  |______________|_____________________________________________________________|
1264  |                                                                            |
1265  | The input data is variable 'enc_block' via 'enc_block_zz'.                 |
1266  |                                                                            |
1267  | The data to be quantized is compared to thresh to determine if the         |
1268  | quantized data will be zero.  In most cases, this is true, thus            |
1269  | eliminating the quantization steps (multiplication + add + shift).         |
1270  |____________________________________________________________________________|
1271 */
1272 static void encode_block (
1273     PJENC_INST         g,
1274     int                comp,       /* image component number */
1275     const huff_elem_t *dc_huff_p,
1276     const huff_elem_t *ac_huff_p,
1277     int               *quant_p,
1278     int               *thresh_p)
1279 {
1280     int  **block_p;
1281     int    i, data, absdata, run, siz;
1282     int    diff, absdiff;
1283     WRITE_BITS_OPEN(g)
1284
1285       /************************************/
1286      /* Quantize and Encode DC component */
1287     /************************************/
1288
1289     thresh_p++;
1290     block_p = g->enc_block_zz;
1291     data = ROUND ((long)*(*block_p++) * (*quant_p++));
1292     absdiff = diff = data - g->prior_DC[comp];
1293     if (absdiff < 0) absdiff = -absdiff;
1294     g->prior_DC[comp] = data;
1295
1296     if (absdiff < 256) siz = codesize_array[absdiff];
1297     else               siz = codesize_array[absdiff>>8] + 8;
1298
1299     WRITE_HUFF_CODE (g, siz, dc_huff_p)
1300     WRITE_BITS (g, diff<0 ? diff-1 : diff, siz)
1301
1302       /************************************************/
1303      /* Quantize, Zigzag, and Encode AC coefficients */
1304     /************************************************/
1305
1306     run = 0;
1307
1308     for (i=63; i>0; i--)   /* do 63 times... */
1309     {
1310         absdata = data = *(*block_p++);
1311         if (absdata < 0) absdata = -absdata;
1312
1313         if (absdata <= *thresh_p++) {
1314             /* quantization would be zero */
1315             quant_p++;
1316             run++;       /* increment run-length of zeroes */
1317         }
1318         else     /* need to quantize */
1319         {
1320             while (run >= 16) {
1321                 WRITE_HUFF_CODE (g, RUN_OF_16, ac_huff_p)
1322                 run -= 16;
1323             }
1324
1325             absdata = ROUND ((DWORD )absdata * (DWORD )(*quant_p++));
1326             if (absdata < 256) siz = codesize_array[absdata];
1327             else               siz = codesize_array[absdata>>8] + 8;
1328
1329             /* output the RLE code, and the AC term */
1330             WRITE_HUFF_CODE (g, (run<<4) + siz, ac_huff_p)
1331             WRITE_BITS (g, data<0 ? ~absdata : absdata, siz)
1332             run = 0;
1333         }
1334     }
1335
1336     if (run>0 || g->fDenali)
1337         WRITE_HUFF_CODE (g, 0, ac_huff_p)   /* output EOB code */
1338
1339     WRITE_BITS_CLOSE(g)
1340 }
1341
1342
1343
1344 /*____________________________________________________________________________
1345  |            |                                                               |
1346  | encode_MCU | encodes the given MCU from the input rows in in_rows_ap       |
1347  |____________|_______________________________________________________________|
1348 */
1349 static void encode_MCU (
1350     PJENC_INST g,
1351     UINT       mcu_index)
1352 {
1353     int   *block_p;
1354     UINT   comp;
1355     UINT   h_block, v_block;
1356     UINT   ul_row, ul_col;
1357     UINT   row;
1358     BYTE **row_pp;
1359     BYTE  *row_p;
1360
1361     for (comp=0; comp<g->comps_in_pixel; comp++) {
1362         for (v_block=0; v_block<g->vert_sam_facs[comp]; v_block++) {
1363             for (h_block=0; h_block<g->horiz_sam_facs[comp]; h_block++) {
1364
1365                 /***** level-shift and copy the block into 'enc_block' *****/
1366
1367                 ul_row = v_block*8;
1368                 ul_col = (mcu_index*g->horiz_sam_facs[comp] + h_block) * 8;
1369                 row_pp = &(g->in_rows_ap[comp][ul_row]);
1370                 block_p = g->enc_block;
1371
1372                 for (row=0; row<8; row++) {
1373                     row_p = *row_pp++ + ul_col;
1374
1375                     *block_p++ = (int)(UINT)*row_p++ - 128;
1376                     *block_p++ = (int)(UINT)*row_p++ - 128;
1377                     *block_p++ = (int)(UINT)*row_p++ - 128;
1378                     *block_p++ = (int)(UINT)*row_p++ - 128;
1379                     *block_p++ = (int)(UINT)*row_p++ - 128;
1380                     *block_p++ = (int)(UINT)*row_p++ - 128;
1381                     *block_p++ = (int)(UINT)*row_p++ - 128;
1382                     *block_p++ = (int)(UINT)*row_p   - 128;
1383                 }
1384
1385                 dct_forward (g->enc_block);
1386
1387                 /***** encode and output the block *****/
1388     
1389                 if (comp == 0) {
1390                     encode_block (g, comp, lum_DC_table, g->fDenali ? lum_AC_table_Denali : lum_AC_table,
1391                                   g->wino_lum_quant, g->wino_lum_quant_thres);
1392                 } else {
1393                     encode_block (g, comp, chrom_DC_table, chrom_AC_table,
1394                                   g->wino_chrom_quant, g->wino_chrom_quant_thres);
1395                 }
1396             }
1397         }
1398     }
1399 }
1400
1401
1402
1403 /*____________________________________________________________________________
1404  |              |                                                             |
1405  | copy_in_rows | copies a row (or rows) from input buffer to in_rows_ap      |
1406  |______________|_____________________________________________________________|
1407  |                                                                            |
1408  | If input_subsampled is true, the input row data must be in the same odd    |
1409  | order which the ASIC outputs it.                                           |
1410  |                                                                            |
1411  | View a period as being a group of max_horiz_sam_fac by max_vert_sam_fac    |
1412  | samples.  (BTW, an MCU contains exactly 8x8 periods.)  Divide the entire   |
1413  | image into a grid of such periods.  The ASIC outputs the periods left to   |
1414  | right, top to bottom.  It outputs all the data in each period, before      |
1415  | moving to the next period.  Within a period, it outputs the components in  |
1416  | succession; within a component, it outputs the samples left to right, top  |
1417  | to bottom.                                                                 |
1418  |                                                                            |
1419  | For example:                                                               |
1420  |     Three components = Y U V                                               |
1421  |     Horiz sample factors = 2 1 1                                           |
1422  |     Vert  sample factors = 2 1 1                                           |
1423  |     A period is 2x2 samples (i.e., a 2x2 square).                          |
1424  |     Each period contains these samples:  YYYYUV.                           |
1425  |     Bytes output:  YYYYUV YYYYUV YYYYUV ....                               |
1426  |                                                                            |
1427  | In this routine,                                                           |
1428  |                                                                            |
1429  |     vert_period  = index of which period we're inputting vertically within |
1430  |                    an MCU (0-7).                                           |
1431  |                                                                            |
1432  |     horiz_period = index of which period we're inputting horizontally;     |
1433  |                    since an MCU has 8 periods, this is 0 .. 8*mcus_in_row. |
1434  |____________________________________________________________________________|
1435 */
1436 static void copy_in_rows (
1437     PJENC_INST g,
1438     UINT  row_index,         /* in:  index of next row to get within MCU */
1439     BYTE *inbuf_p,           /* in:  input row data */
1440     UINT *n_rows_p,          /* out: # of rows copied in */
1441     UINT *n_bytes_p)         /* out: # of bytes copied in */
1442 {
1443     BYTE *in_p;
1444     UINT  comp;
1445     UINT  vert_period, vert_mod;
1446
1447     vert_period = row_index / g->max_vert_sam_fac;
1448     vert_mod    = row_index % g->max_vert_sam_fac;
1449
1450     if (!g->input_subsampled || (g->max_vert_sam_fac==1 && g->max_horiz_sam_fac==1))
1451     {
1452           /********************************************************/
1453          /* Perform subsampling (or copying of non-sampled data) */
1454         /********************************************************/
1455
1456         BYTE *row_p;
1457         UINT  sam_fac;
1458         UINT  col, horiz_mod;
1459         UINT  inc;
1460
1461         for (comp=0; comp<g->comps_in_pixel; comp++) {
1462             sam_fac = g->vert_sam_facs[comp];
1463
1464             if (vert_mod < sam_fac) {
1465                 in_p = inbuf_p + comp;
1466                 row_p = g->in_rows_ap[comp][vert_period*sam_fac + vert_mod];
1467                 horiz_mod = 0;
1468                 sam_fac = g->horiz_sam_facs[comp];
1469
1470                 if (sam_fac == g->max_horiz_sam_fac) {
1471
1472                     /***** fast case: copying all pixels *****/
1473
1474                     if (g->comps_in_pixel == 1) {
1475                         memcpy (row_p, in_p, g->pixels_in_row);
1476                     } else {
1477                         for (col=0; col<g->pixels_in_row; col+=4) {
1478                             *row_p++ = *in_p;  in_p += g->comps_in_pixel;
1479                             *row_p++ = *in_p;  in_p += g->comps_in_pixel;
1480                             *row_p++ = *in_p;  in_p += g->comps_in_pixel;
1481                             *row_p++ = *in_p;  in_p += g->comps_in_pixel;
1482                         }
1483                     }
1484
1485                 } else if (sam_fac==1 && g->max_horiz_sam_fac==2) {
1486
1487                     /***** fast case: copying every other pixel *****/
1488
1489                     inc = 2 * g->comps_in_pixel;
1490                     for (col=0; col<g->pixels_in_row; col+=8) {
1491                         *row_p++ = *in_p; in_p += inc;
1492                         *row_p++ = *in_p; in_p += inc;
1493                         *row_p++ = *in_p; in_p += inc;
1494                         *row_p++ = *in_p; in_p += inc;
1495                     }
1496
1497                 } else {
1498
1499                     /***** slow general case *****/
1500
1501                     for (col=0; col<g->pixels_in_row; col++) {
1502                         if (horiz_mod < sam_fac) *row_p++ = *in_p;
1503                         in_p += g->comps_in_pixel;
1504                         horiz_mod += 1;
1505                         if (horiz_mod == g->max_horiz_sam_fac) horiz_mod = 0;
1506                     }
1507                 }
1508             }
1509         }
1510
1511         *n_rows_p = 1;
1512         *n_bytes_p = g->comps_in_pixel * g->pixels_in_row;
1513
1514     } else {
1515
1516           /************************************************/
1517          /* Already subsampled (in the ASIC's odd order) */
1518         /************************************************/
1519
1520         UINT horiz_period, periods_in_row;
1521         UINT ul_row, ul_col, row, col;
1522         BYTE  *row_y1_p, *row_y2_p, *row_cb_p, *row_cr_p;
1523
1524         in_p = inbuf_p;
1525         periods_in_row = g->mcus_in_row * 8;
1526
1527         if (g->sample_factors == 0x21102110u) {  /* fast case: 4-1-1 subsampling */
1528             row_y1_p = g->in_rows_ap[0][2*vert_period];
1529             row_y2_p = g->in_rows_ap[0][2*vert_period+1];
1530             row_cb_p = g->in_rows_ap[1][vert_period];
1531             row_cr_p = g->in_rows_ap[2][vert_period];
1532             for (horiz_period=0; horiz_period<periods_in_row; horiz_period++) {
1533                 *row_y1_p++ = *in_p++;
1534                 *row_y1_p++ = *in_p++;
1535                 *row_y2_p++ = *in_p++;
1536                 *row_y2_p++ = *in_p++;
1537                 *row_cb_p++ = *in_p++;
1538                 *row_cr_p++ = *in_p++;
1539             }
1540         } else {  /* slow general case */
1541
1542             for (horiz_period=0; horiz_period<periods_in_row; horiz_period++) {
1543                 for (comp=0; comp<g->comps_in_pixel; comp++) {
1544                     ul_row =  vert_period *  g->vert_sam_facs[comp];
1545                     ul_col = horiz_period * g->horiz_sam_facs[comp];
1546
1547                     for (row=ul_row; row<ul_row+ g->vert_sam_facs[comp]; row++)
1548                     for (col=ul_col; col<ul_col+g->horiz_sam_facs[comp]; col++) {
1549                         g->in_rows_ap[comp][row][col] = *in_p++;
1550                     }
1551                 }
1552             }
1553         }
1554
1555         *n_rows_p = g->max_vert_sam_fac;
1556         *n_bytes_p = in_p - inbuf_p;
1557     }
1558 }
1559
1560
1561 /******************************************************************************
1562  ******************************************************************************
1563
1564                              EXPORTED ROUTINES
1565
1566  ******************************************************************************
1567  ******************************************************************************/
1568
1569
1570
1571 /*____________________________________________________________________________
1572  |               |                                                            |
1573  | scale_q_table | scales a q-table according to the q-factors                |
1574  |_______________|____________________________________________________________|
1575 */
1576 static void scale_q_table (
1577     UINT        dc_q_factor,
1578     UINT        ac_q_factor,
1579     const BYTE *in,
1580     BYTE       *out)
1581 {
1582     #define FINAL_DC_INDEX  9
1583
1584     UINT i, val;
1585     UINT q;
1586
1587     q = dc_q_factor;
1588
1589     for (i=0; i<64; i++) {
1590         val = ((*in++)*q + Q_DEFAULT/2) / Q_DEFAULT;
1591         if (val < 1)   val = 1;
1592         if (val > 255) val = 255;
1593         *out++ = (BYTE)val;
1594         if (i == FINAL_DC_INDEX)
1595             q = ac_q_factor;
1596     }
1597 }
1598
1599
1600
1601 /*____________________________________________________________________________
1602  |               |                                                            |
1603  | output_header | outputs the JPEG header                                    |
1604  |_______________|____________________________________________________________|
1605  |                                                                            |
1606  | Inputs:  lum_quant, chrom_quant, xRes, yRes, pixels_in_row, comps_in_pixel,|
1607  |          horiz_sam_facs, vert_sam_facs                                     |
1608  |____________________________________________________________________________|
1609 */
1610 static UINT output_header (
1611     PJENC_INST g,
1612     BYTE       *buf_p)
1613 {
1614           BYTE  hclass[4];
1615           BYTE  ident[4];
1616     const BYTE *counts[4];
1617     const BYTE *huffval[4];
1618
1619     hclass [0] = 0;
1620     ident  [0] = 0;
1621     counts [0] = lum_DC_counts;
1622     huffval[0] = lum_DC_values;
1623
1624     hclass [1] = 1;
1625     ident  [1] = 0;
1626     counts [1] = g->fDenali ? lum_AC_counts_Denali : lum_AC_counts;
1627     huffval[1] = lum_AC_values;
1628
1629     hclass [2] = 0;
1630     ident  [2] = 1;
1631     counts [2] = chrom_DC_counts;
1632     huffval[2] = chrom_DC_values;
1633
1634     hclass [3] = 1;
1635     ident  [3] = 1;
1636     counts [3] = chrom_AC_counts;
1637     huffval[3] = chrom_AC_values;
1638
1639     write_buf_open (g, buf_p);
1640         em_write_SOI (g);
1641
1642         if (0  /* todo */ ) {
1643             em_write_short_header (g, g->rows_in_page<0 ? 0 : g->rows_in_page,
1644                                    g->pixels_in_row,
1645                                    g->xRes, g->yRes,
1646                                    g->dc_q_factor, g->ac_q_factor,
1647                                    g->comps_in_pixel,
1648                                    g->sample_factors);
1649         } else {
1650             if (g->fOutputG3APP1)
1651                 em_write_G3_APP1 (g, g->xRes);
1652             else
1653                 em_write_JFIF_APP0 (g, g->xRes, g->yRes);
1654             em_write_SOF (g, g->pixels_in_row, g->rows_in_page<0 ? 0 : g->rows_in_page,
1655                           g->comps_in_pixel, g->horiz_sam_facs, g->vert_sam_facs);
1656             em_write_DQT (g, 0, 0, g->lum_quant);
1657             if (g->comps_in_pixel > 1)
1658                 em_write_DQT (g, 0, 1, g->chrom_quant);
1659             em_write_DHTs (g, g->comps_in_pixel==1 ? 2 : 4,
1660                            hclass, ident, counts, huffval);
1661             em_write_SOS (g, g->comps_in_pixel);
1662         }
1663     return write_buf_close (g);
1664 }
1665
1666
1667
1668 /*****************************************************************************\
1669  *
1670  * jpgEncode_openXform - Creates a new instance of the transformer
1671  *
1672  *****************************************************************************
1673  *
1674  * This returns a handle for the new instance to be passed into
1675  * all subsequent calls.
1676  *
1677  * Return value: IP_DONE=success; IP_FATAL_ERROR=misc error.
1678  *
1679 \*****************************************************************************/
1680
1681 static WORD jpgEncode_openXform (
1682     IP_XFORM_HANDLE *pXform)   /* out: returned handle */
1683 {
1684     PJENC_INST g;
1685
1686     INSURE (pXform != NULL);
1687     IP_MEM_ALLOC (sizeof(JENC_INST), g);
1688     *pXform = g;
1689     memset (g, 0, sizeof(JENC_INST));
1690     g->dwValidChk = CHECK_VALUE;
1691     return IP_DONE;
1692
1693     fatal_error:
1694     return IP_FATAL_ERROR;
1695 }
1696
1697
1698
1699 /*****************************************************************************\
1700  *
1701  * jpgEncode_setDefaultInputTraits - Specifies default input image traits
1702  *
1703  *****************************************************************************
1704  *
1705  * The header of the file-type handled by the transform probably does
1706  * not include *all* the image traits we'd like to know.  Those not
1707  * specified in the file-header are filled in from info provided by
1708  * this routine.
1709  *
1710  * Return value: IP_DONE=success; IP_FATAL_ERROR=misc error.
1711  *
1712 \*****************************************************************************/
1713
1714 static WORD jpgEncode_setDefaultInputTraits (
1715     IP_XFORM_HANDLE  hXform,     /* in: handle for xform */
1716     PIP_IMAGE_TRAITS pTraits)    /* in: default image traits */
1717 {
1718     PJENC_INST g;
1719
1720     HANDLE_TO_PTR (hXform, g);
1721     g->traits = *pTraits;   /* a structure copy */
1722
1723     INSURE (g->traits.iPixelsPerRow > 0);
1724     INSURE (g->traits.iComponentsPerPixel==1 || g->traits.iComponentsPerPixel==3);
1725
1726     g->pixels_in_row  = g->traits.iPixelsPerRow;
1727     g->comps_in_pixel = g->traits.iComponentsPerPixel;
1728     g->rows_in_page   = g->traits.lNumRows;
1729     g->xRes           = g->traits.lHorizDPI >> 16;
1730     g->yRes           = g->traits.lVertDPI  >> 16;
1731     if (g->xRes < 0) g->xRes = 300;
1732     if (g->yRes < 0) g->yRes = 300;
1733
1734     g->fDidHeader = FALSE;
1735     return IP_DONE;
1736
1737     fatal_error:
1738         return IP_FATAL_ERROR;
1739 }
1740
1741
1742
1743 /*****************************************************************************\
1744  *
1745  * jpgEncode_setXformSpec - Provides xform-specific information
1746  *
1747 \*****************************************************************************/
1748
1749 static WORD jpgEncode_setXformSpec (
1750     IP_XFORM_HANDLE  hXform,         /* in: handle for xform */
1751     DWORD_OR_PVOID   aXformInfo[])   /* in: xform information */
1752 {
1753     PJENC_INST g;
1754     UINT       qfacs;
1755
1756     HANDLE_TO_PTR (hXform, g);
1757
1758     qfacs                 =       aXformInfo[IP_JPG_ENCODE_QUALITY_FACTORS].dword;
1759     g->sample_factors     =       aXformInfo[IP_JPG_ENCODE_SAMPLE_FACTORS].dword;
1760     g->input_subsampled   =       aXformInfo[IP_JPG_ENCODE_ALREADY_SUBSAMPLED].dword;
1761     g->fDenali            = (BOOL)aXformInfo[IP_JPG_ENCODE_FOR_DENALI].dword;
1762     g->fOutputDNL         = (BOOL)aXformInfo[IP_JPG_ENCODE_OUTPUT_DNL].dword;
1763     g->fOutputG3APP1      = (BOOL)aXformInfo[IP_JPG_ENCODE_FOR_COLOR_FAX].dword;
1764     g->dwDummyHeaderBytes =       aXformInfo[IP_JPG_ENCODE_DUMMY_HEADER_LEN].dword;
1765
1766     g->dc_q_factor = (BYTE)(qfacs >> 8);
1767     g->ac_q_factor = (BYTE)(qfacs);
1768
1769     return IP_DONE;
1770
1771     fatal_error:
1772         return IP_FATAL_ERROR;
1773 }
1774
1775
1776
1777 /*****************************************************************************\
1778  *
1779  * jpgEncode_getHeaderBufSize- Returns size of input buf needed to hold header
1780  *
1781 \*****************************************************************************/
1782
1783 static WORD jpgEncode_getHeaderBufSize (
1784     IP_XFORM_HANDLE  hXform,         /* in:  handle for xform */
1785     DWORD           *pdwInBufLen)    /* out: buf size for parsing header */
1786 {
1787     PJENC_INST g;
1788
1789     HANDLE_TO_PTR (hXform, g);
1790     /* since input is raw pixels, there is no header, so set it to zero */
1791     *pdwInBufLen = 0;
1792     return IP_DONE;
1793
1794     fatal_error:
1795         return IP_FATAL_ERROR;
1796 }
1797
1798
1799
1800 /*****************************************************************************\
1801  *
1802  * jpgEncode_getActualTraits - Parses header, and returns input & output traits
1803  *
1804 \*****************************************************************************/
1805
1806 static WORD jpgEncode_getActualTraits (
1807     IP_XFORM_HANDLE  hXform,         /* in:  handle for xform */
1808     DWORD            dwInputAvail,   /* in:  # avail bytes in input buf */
1809     PBYTE            pbInputBuf,     /* in:  ptr to input buffer */
1810     PDWORD           pdwInputUsed,   /* out: # bytes used from input buf */
1811     PDWORD           pdwInputNextPos,/* out: file-pos to read from next */
1812     PIP_IMAGE_TRAITS pInTraits,      /* out: input image traits */
1813     PIP_IMAGE_TRAITS pOutTraits)     /* out: output image traits */
1814 {
1815     PJENC_INST g;
1816
1817     HANDLE_TO_PTR (hXform, g);
1818
1819     /* Since there is no header, we'll report no usage of input */
1820     *pdwInputUsed    = 0;
1821     *pdwInputNextPos = 0;
1822
1823     /* Since we don't change traits, just copy out the default traits */
1824     *pInTraits  = g->traits;
1825     *pOutTraits = g->traits;
1826
1827     return IP_DONE | IP_READY_FOR_DATA;
1828
1829     fatal_error:
1830         return IP_FATAL_ERROR;
1831 }
1832
1833
1834
1835 /****************************************************************************\
1836  *
1837  * jpgEncode_getActualBufSizes - Returns buf sizes needed for remainder of job
1838  *
1839 \****************************************************************************/
1840
1841 static WORD jpgEncode_getActualBufSizes (
1842     IP_XFORM_HANDLE hXform,           /* in:  handle for xform */
1843     PDWORD          pdwMinInBufLen,   /* out: min input buf size */
1844     PDWORD          pdwMinOutBufLen)  /* out: min output buf size */
1845 {
1846     PJENC_INST g;
1847     WORD       n;
1848
1849     HANDLE_TO_PTR (hXform, g);
1850     *pdwMinInBufLen = g->comps_in_pixel * g->pixels_in_row;
1851
1852     n = OUTBUF_NUM_MCUS * MAX_MCU_SIZE;
1853     if (n < MAX_HEADER_SIZE) n = MAX_HEADER_SIZE;
1854     *pdwMinOutBufLen = n;
1855
1856     return IP_DONE;
1857
1858     fatal_error:
1859         return IP_FATAL_ERROR;
1860 }
1861
1862
1863
1864 /*****************************************************************************\
1865  *
1866  * jpgEncode_convert - the work-horse routine
1867  *
1868 \*****************************************************************************/
1869
1870 static WORD jpgEncode_convert (
1871     IP_XFORM_HANDLE hXform,
1872     DWORD           dwInputAvail,     /* in:  # avail bytes in in-buf */
1873     PBYTE           pbInputBuf,       /* in:  ptr to in-buffer */
1874     PDWORD          pdwInputUsed,     /* out: # bytes used from in-buf */
1875     PDWORD          pdwInputNextPos,  /* out: file-pos to read from next */
1876     DWORD           dwOutputAvail,    /* in:  # avail bytes in out-buf */
1877     PBYTE           pbOutputBuf,      /* in:  ptr to out-buffer */
1878     PDWORD          pdwOutputUsed,    /* out: # bytes written in out-buf */
1879     PDWORD          pdwOutputThisPos) /* out: file-pos to write the data */
1880 {
1881     PJENC_INST g;
1882     unsigned   ret_val;
1883     UINT       row, n_rows, n_bytes;
1884     int        comp;
1885     UINT       row_len;
1886     UINT       n_loaded;
1887
1888     HANDLE_TO_PTR (hXform, g);
1889
1890     *pdwInputNextPos  = g->dwInNextPos;
1891     *pdwInputUsed     = 0;
1892     *pdwOutputThisPos = g->dwOutNextPos;
1893     *pdwOutputUsed    = 0;
1894     ret_val           = IP_READY_FOR_DATA;
1895
1896       /****************************************************/
1897      /* Init and output the Header if we haven't already */
1898     /****************************************************/
1899
1900     if (! g->fDidHeader) {
1901         UINT  row_len;
1902         BYTE *p;
1903         DWORD factors;
1904         UINT  fac;
1905
1906         /* Init */
1907
1908         g->rows_loaded = 0;
1909         g->mcus_sent = 0;
1910         g->loading_rows = TRUE;
1911         write_init (g);
1912         zero_prior_DC (g);
1913         encode_init (g);
1914
1915         if (g->ac_q_factor == 0)
1916             g->ac_q_factor = Q_DEFAULT;
1917         if (g->dc_q_factor == 0)
1918             g->dc_q_factor = g->ac_q_factor;
1919
1920         if (g->sample_factors == 0)
1921             g->sample_factors = g->comps_in_pixel==1 ? MONO_FACTORS : COLOR_FACTORS;
1922
1923         factors = g->sample_factors >> 16;
1924         g->max_horiz_sam_fac = 0;
1925         for (comp=3; comp>=0; comp--) {
1926             fac = factors & 0x0fu;
1927             if (fac > g->max_horiz_sam_fac) g->max_horiz_sam_fac = fac;
1928             g->horiz_sam_facs[comp] = fac;
1929             factors >>= 4;
1930         }
1931
1932         factors = g->sample_factors & 0x0000ffffu;
1933         g->max_vert_sam_fac = 0;
1934         for (comp=3; comp>=0; comp--) {
1935             fac = factors & 0x0fu;
1936             if (fac > g->max_vert_sam_fac) g->max_vert_sam_fac = fac;
1937             g->vert_sam_facs[comp] = fac;
1938             factors >>= 4;
1939         }
1940
1941         g->cols_in_mcu = g->max_horiz_sam_fac * 8;
1942         g->rows_in_mcu =  g->max_vert_sam_fac * 8;
1943         g->mcus_in_row = (g->pixels_in_row + g->cols_in_mcu - 1) / g->cols_in_mcu;
1944
1945         scale_q_table (g->dc_q_factor, g->ac_q_factor, orig_lum_quant,   g->lum_quant);
1946         scale_q_table (g->dc_q_factor, g->ac_q_factor, orig_chrom_quant, g->chrom_quant);
1947
1948         /* scale lum_quant & chrom_quant for Wino DCT */
1949         scale_for_wino (g->lum_quant,   g->wino_lum_quant,   g->wino_lum_quant_thres);
1950         scale_for_wino (g->chrom_quant, g->wino_chrom_quant, g->wino_chrom_quant_thres);
1951
1952         g->whitePixel[0] = 255;   /* YCC value of a white pixel (color) */
1953         g->whitePixel[1] = 128;   /* The 255 is white in mono too */
1954         g->whitePixel[2] = 128;
1955         g->whitePixel[3] = 255;
1956
1957         g->dwOutNextPos = *pdwOutputUsed = (g->dwDummyHeaderBytes == 0)
1958             ? output_header (g,pbOutputBuf)
1959             : g->dwDummyHeaderBytes;
1960
1961         *pdwOutputThisPos = 0;
1962
1963         /* Allocate the row-buffers in in_rows_ap */
1964
1965         memset (g->in_rows_ap, 0, sizeof(g->in_rows_ap));
1966
1967         for (comp=0; comp<(int)g->comps_in_pixel; comp++) {
1968             row_len = g->horiz_sam_facs[comp] * g->mcus_in_row * 8;
1969             n_rows = g->vert_sam_facs[comp] * 8;
1970
1971             for (row=0; row<n_rows; row++) {
1972                 IP_MEM_ALLOC (row_len, p);
1973                 g->in_rows_ap[comp][row] = p;
1974             }
1975         }
1976
1977         *pdwInputUsed    = 0;
1978         *pdwInputNextPos = 0;
1979         g->dwInNextPos = 0;
1980         g->fDidHeader = TRUE;
1981
1982         return IP_READY_FOR_DATA;
1983     }
1984
1985       /*********************************/
1986      /* We are filling the input rows */
1987     /*********************************/
1988
1989     if (g->loading_rows) {
1990
1991         /***** Init all row-buffers to white if starting new row-set *****/
1992
1993         n_loaded = g->rows_loaded % g->rows_in_mcu;
1994
1995         if (n_loaded == 0) {
1996             for (comp=0; comp<g->comps_in_pixel; comp++) {
1997                 row_len = g->horiz_sam_facs[comp] * g->mcus_in_row * 8;
1998                 n_rows = g->vert_sam_facs[comp] * 8;
1999
2000                 for (row=0; row<n_rows; row++)
2001                     memset (g->in_rows_ap[comp][row], g->whitePixel[comp], row_len);
2002             }
2003         }
2004
2005         if (pbInputBuf == NULL) {
2006
2007             /***** We are being told to flush *****/
2008
2009             if (n_loaded != 0) {
2010                 /* some rows were loaded; start compressing rows now */
2011                 g->loading_rows = FALSE;
2012                 /* boost rows_loaded to next multiple of rows_in_mcu so
2013                  * n_loaded will be 0 next time around */
2014                 g->rows_loaded += g->rows_in_mcu - n_loaded;
2015             } else if ((g->rows_in_page<0 || (int)g->rows_received<g->rows_in_page)
2016                        && g->dwDummyHeaderBytes == 0) {
2017                 /* row-count was unknown or too big, output header w/ correct row-count */
2018                 g->rows_in_page = g->rows_received;
2019                 *pdwOutputUsed    = output_header (g, pbOutputBuf);
2020                 *pdwOutputThisPos = 0;
2021             } else {
2022                 /* no rows were loaded, and row-count is valid, so we're done */
2023                 PRINT (_T("jpeg_encode_convert_row: Done\n"),0,0);
2024                 write_buf_open (g, pbOutputBuf);
2025                     write_bits_flush (g);
2026                     if (g->fOutputDNL)
2027                         em_write_DNL (g, g->rows_received);
2028                     em_write_EOI (g);
2029                 *pdwOutputUsed = write_buf_close (g);
2030                 ret_val |= IP_DONE;
2031             }
2032         } else {
2033
2034             /***** Copy the row(s) into in_rows_ap *****/
2035
2036             copy_in_rows (
2037                 g,
2038                 n_loaded,              /* in:  index of next row to get */
2039                 pbInputBuf,            /* in:  copied-in row data */
2040                 &n_rows,               /* out: # of rows copied in */
2041                 &n_bytes);             /* out: # of bytes fetched */
2042
2043             *pdwInputUsed     = n_bytes;
2044             *pdwInputNextPos  = (g->dwInNextPos += n_bytes);
2045             g->rows_loaded   += n_rows;
2046             g->rows_received += n_rows;
2047             n_loaded = g->rows_loaded % g->rows_in_mcu;
2048
2049             /* it's hard to set IP_PRODUCED_ROW 8 times per 8 rows, so cheat */
2050             if (n_rows > 0)
2051                 ret_val |= IP_CONSUMED_ROW | IP_PRODUCED_ROW;
2052
2053             /***** Check if all needed rows are loaded *****/
2054
2055             if (n_loaded == 0) {
2056                 /* # rows loaded is rows_in_mcu, but the mod wrapped to zero */
2057                 g->loading_rows = FALSE;  /* start compressing rows now */
2058             }
2059         }
2060     }
2061
2062       /************************************************************/
2063      /* Compress as many MCUs as will fit into the output buffer */
2064     /************************************************************/
2065
2066     while (!g->loading_rows &&
2067            dwOutputAvail-*pdwOutputUsed > MAX_MCU_SIZE) {
2068         PRINT (_T("jpeg_encode_convert_row: Encoding MCU, mcus_sent=%d\n"),
2069                 g->mcus_sent, 0);
2070
2071         write_buf_open (g, pbOutputBuf + *pdwOutputUsed);
2072             encode_MCU (g, g->mcus_sent);
2073         n_bytes = write_buf_close (g);
2074
2075         g->dwOutNextPos += n_bytes;
2076         *pdwOutputUsed  += n_bytes;
2077
2078         g->mcus_sent += 1;
2079         if (g->mcus_sent >= g->mcus_in_row) {
2080             g->loading_rows = TRUE;
2081             g->mcus_sent = 0;
2082         }
2083     }
2084
2085     PRINT (_T("jpeg_encode_convert_row: Returning %04x, out_used=%d\n"),
2086            ret_val, *pdwOutputUsed);
2087     return ret_val;
2088
2089     fatal_error:
2090     return IP_FATAL_ERROR;
2091 }
2092
2093
2094
2095 /*****************************************************************************\
2096  *
2097  * jpgEncode_insertedData - client inserted into our output stream
2098  *
2099 \*****************************************************************************/
2100
2101 static WORD jpgEncode_insertedData (
2102     IP_XFORM_HANDLE hXform,
2103     DWORD           dwNumBytes)
2104 {
2105     fatalBreakPoint ();
2106     return IP_FATAL_ERROR;   /* must never be called (can't insert data) */
2107 }
2108
2109
2110
2111 /*****************************************************************************\
2112  *
2113  * jpgEncode_newPage - Tells us to flush this page, and start a new page
2114  *
2115 \*****************************************************************************/
2116
2117 static WORD jpgEncode_newPage (
2118     IP_XFORM_HANDLE hXform)
2119 {
2120     PJENC_INST g;
2121
2122     HANDLE_TO_PTR (hXform, g);
2123     /* todo: return fatal error if convert is called again? */
2124     return IP_DONE;   /* can't insert page-breaks, so ignore this call */
2125
2126     fatal_error:
2127     return IP_FATAL_ERROR;
2128 }
2129
2130
2131
2132 /*****************************************************************************\
2133  *
2134  * jpgEncode_closeXform - Destroys this instance
2135  *
2136 \*****************************************************************************/
2137
2138 static WORD jpgEncode_closeXform (IP_XFORM_HANDLE hXform)
2139 {
2140     PJENC_INST g;
2141     BYTE       **row_pp, **after_pp, *p;
2142
2143     HANDLE_TO_PTR (hXform, g);
2144     row_pp = &(g->in_rows_ap[0][0]);
2145     after_pp = row_pp + (sizeof(g->in_rows_ap)/sizeof(BYTE*));
2146
2147     for ( ; row_pp<after_pp; row_pp++) {
2148         p = *row_pp;
2149         if (p != NULL) {
2150             IP_MEM_FREE (p);
2151             *row_pp = NULL;
2152         }
2153     }
2154
2155     g->dwValidChk = 0;
2156     IP_MEM_FREE (g);       /* free memory for the instance */
2157     return IP_DONE;
2158
2159     fatal_error:
2160     return IP_FATAL_ERROR;
2161 }
2162
2163
2164
2165 /*****************************************************************************\
2166  *
2167  * jpgEncodeTbl - Jump-table for encoder
2168  *
2169 \*****************************************************************************/
2170
2171 IP_XFORM_TBL jpgEncodeTbl = {
2172     jpgEncode_openXform,
2173     jpgEncode_setDefaultInputTraits,
2174     jpgEncode_setXformSpec,
2175     jpgEncode_getHeaderBufSize,
2176     jpgEncode_getActualTraits,
2177     jpgEncode_getActualBufSizes,
2178     jpgEncode_convert,
2179     jpgEncode_newPage,
2180     jpgEncode_insertedData,
2181     jpgEncode_closeXform
2182 };
2183
2184 /* End of File */