1 /* libhpojip -- HP OfficeJet image-processing library. */
3 /* Copyright (C) 1995-2002 Hewlett-Packard Company
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.
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.
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,
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.
32 /* Original author: Mark Overton and others.
34 * Ported to Linux by David Paschal.
37 /*****************************************************************************\
39 * xjpg_enc.c - Converts raw gray image into a valid JPEG file
41 *****************************************************************************
43 * Name of Global Jump-Table:
47 * Items in aXformInfo array passed into setXformSpec:
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.
70 * The aXformInfo items above may all be set to 0 for typical JPEG files.
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)
76 * Capabilities and Limitations:
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.
86 * Default Input Traits, and Output Traits:
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
99 * Above, a "*" by an item indicates it must be valid (not negative).
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).
105 \*****************************************************************************/
110 #include "xjpg_dct.h"
111 #include "xjpg_mrk.h"
117 #define PRINT(msg,arg1,arg2) \
118 _ftprintf(stdout, _T("(jpeg) ") msg, (int)arg1, (int)arg2)
120 #define PRINT(msg,arg1,arg2)
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 */
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 */
135 #define CHECK_VALUE 0xAceC0de4U
138 /*____________________________________________________________________________
140 | Configuration Variables |
141 |____________________________________________________________________________|
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.
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.
157 /**** Configuration ****/
160 BYTE chrom_quant[64];
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];
167 BOOL input_subsampled;
171 DWORD dwDummyHeaderBytes;
172 UINT rows_in_mcu; /* # rows & cols in each 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;
182 BYTE comps_in_pixel; /* # components per pixel (1 or 3) */
183 BYTE whitePixel[4]; /* the value of a white pixel */
185 int rows_in_page; /* negative means "unknown" */
186 int xRes; /* dots per inch */
189 /**** Writing Bits ****/
192 /* Bits to be written to outbuf (accumulated left-to-right). */
195 /* Number of bits not yet written in wr_bit_buf (= 32 - number written). */
198 /* The beginning of the output buffer. */
200 BYTE *write_buf_next;
201 /* Next byte in outbuf to be written. */
203 /**** Encoding Blocks ****/
205 int enc_block[64]; /* scratch-pad 8x8 block */
206 int *enc_block_zz[64+16]; /* zig-zag ptrs into above block */
209 /**** Top Level Control ****/
216 /**** Miscellaneous ****/
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 */
225 } JENC_INST, *PJENC_INST;
228 /*____________________________________________________________________________
230 | Normal Quantization Tables |
231 |____________________________________________________________________________|
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
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
262 /*____________________________________________________________________________
264 | Zigzag of Normal Quantization Tables |
265 |____________________________________________________________________________|
268 static const BYTE orig_lum_quant[64] = {
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
274 10, 10, 10, 14, 12, 10, 16, 14,
276 16, 11, 12, 14, 12, 10, 16, 14,
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
288 static const BYTE orig_chrom_quant[64] = {
290 10, 14, 14, 24, 21, 24, 47, 26,
292 17, 18, 18, 24, 21, 24, 47, 26,
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
304 /*____________________________________________________________________________
306 | codesize_array | Array giving # of bits required to represent the index |
307 |________________|___________________________________________________________|
310 static const BYTE codesize_array[256] = {
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
335 /*____________________________________________________________________________
338 |____________________________________________________________________________|
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
346 static const BYTE lum_DC_values[12] = {
347 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
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
355 static const BYTE chrom_DC_values[12] = {
356 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b
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
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.
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,
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
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,
434 /*____________________________________________________________________________
436 | Huffman tables output by mk_jpg_huff program |
437 |____________________________________________________________________________|
441 WORD code; /* the Huff code to use */
442 BYTE size; /* number of bits in above Huff code */
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 },
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 },
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 },
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 },
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 },
667 /******************************************************************************
668 ******************************************************************************
672 ******************************************************************************
673 ******************************************************************************
676 Interface into this section:
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
688 ******************************************************************************/
692 #define INITIAL_BITS_AVAIL (8*sizeof(DWORD))
693 /* Number of bits in wr_bit_buf, which is the # of bits in a DWORD . */
696 /*____________________________________________________________________________
698 | PUT_BYTE_STUFF | Puts byte into outbuf, with byte-stuffing as needed |
699 |________________|___________________________________________________________|
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 |____________________________________________________________________________|
706 #define PUT_BYTE_STUFF(g, pbs_byte_expression) { \
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; \
717 /*____________________________________________________________________________
719 | write_init | Inits this package, and registers the "write bytes" callback |
720 |____________|_______________________________________________________________|
722 static void write_init (PJENC_INST g)
724 g->wr_bits_avail = INITIAL_BITS_AVAIL;
730 /*____________________________________________________________________________
732 | write_buf_open | We are being given a (new) buffer to receive output |
733 |________________|___________________________________________________________|
735 | This routine records the location of the new output buffer. |
736 |____________________________________________________________________________|
738 static void write_buf_open (PJENC_INST g, BYTE *buf_p)
740 g->wr_outbuf_beg = g->write_buf_next = buf_p;
745 /*____________________________________________________________________________
747 | write_buf_close | We are done with the current output buffer |
748 |_________________|__________________________________________________________|
750 | This function returns # bytes written to the output buffer |
751 |____________________________________________________________________________|
753 static int write_buf_close (PJENC_INST g)
755 return g->write_buf_next - g->wr_outbuf_beg;
760 /*____________________________________________________________________________
762 | WRITE_BITS | Writes the given number of bits |
763 |____________|_______________________________________________________________|
765 | WRITE_BITS_HIZERO assumes that the higher-order bits are all zero. |
766 | WRITE_BITS masks them out for you. |
767 |____________________________________________________________________________|
770 #define WRITE_BITS_OPEN(g) \
771 int tmp_bits_avail = g->wr_bits_avail; \
772 DWORD tmp_bit_buf = g->wr_bit_buf;
775 #define WRITE_BITS_HIZERO(g,wb_value,wb_nbits) { \
779 length_loc = (int)(wb_nbits); \
780 bits_loc = (DWORD)(wb_value); \
782 if (length_loc > tmp_bits_avail) { \
784 PUT_BYTE_STUFF (g, tmp_bit_buf >> 24) \
786 tmp_bits_avail += 8; \
787 } while (tmp_bits_avail <= 24); \
790 tmp_bits_avail -= length_loc; \
791 tmp_bit_buf |= bits_loc << tmp_bits_avail; \
795 #define WRITE_BITS(g,wb_value,wb_nbits) { \
798 len_loc = (int)(wb_nbits); \
799 WRITE_BITS_HIZERO (g, (DWORD )(wb_value) & ((1ul<<len_loc)-1), len_loc) \
803 #define WRITE_BITS_CLOSE(g) { \
804 g->wr_bits_avail = tmp_bits_avail; \
805 g->wr_bit_buf = tmp_bit_buf; \
810 /*____________________________________________________________________________
812 | write_bits_flush | Writes any buffered bits, with buffering via outbuf |
813 |__________________|_________________________________________________________|
815 | Bits are right-padded with ones if necessary to a byte-boundary. |
816 |____________________________________________________________________________|
818 static void write_bits_flush (PJENC_INST g)
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;
826 for (bits_used = INITIAL_BITS_AVAIL - g->wr_bits_avail;
829 PUT_BYTE_STUFF (g, g->wr_bit_buf >> 24)
833 g->wr_bits_avail = INITIAL_BITS_AVAIL;
840 /******************************************************************************
841 ******************************************************************************
845 ******************************************************************************
846 ******************************************************************************/
850 /*____________________________________________________________________________
853 |____________________________________________________________________________|
857 #define PUT_MARKER(g,marker) \
859 *(g->write_buf_next)++ = 0xff; \
860 *(g->write_buf_next)++ = (BYTE)marker; \
864 #define PUT_INT(g,value) \
866 unsigned loc_val = (unsigned)(value); \
867 *(g->write_buf_next)++ = loc_val >> 8; \
868 *(g->write_buf_next)++ = loc_val & 0xff; \
873 /*____________________________________________________________________________
875 | em_write_SOI | Writes Start-Of-Image marker |
876 |______________|_____________________________________________________________|
878 static void em_write_SOI (PJENC_INST g)
880 PUT_MARKER(g, MARKER_SOI);
884 /*____________________________________________________________________________
886 | em_write_EOI | Writes End-Of-Image marker |
887 |______________|_____________________________________________________________|
889 static void em_write_EOI (PJENC_INST g)
891 PUT_MARKER(g, MARKER_EOI);
895 /*____________________________________________________________________________
897 | em_write_JFIF_APP0 | Writes Application marker for JFIF 1,0 |
898 |____________________|_______________________________________________________|
900 static void em_write_JFIF_APP0 (
902 int xRes, /* X resolution of image (number of pixels per inch) */
903 int yRes) /* Y resolution of image (number of pixels per inch) */
905 PUT_MARKER (g, MARKER_APP+0);
906 PUT_INT (g, 16); /* the length */
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';
915 /*** Version 1.0 ***/
916 *(g->write_buf_next)++ = 0x01;
917 *(g->write_buf_next)++ = 0x00;
919 /*** Units (dots per inch) ***/
920 *(g->write_buf_next)++ = 0x01;
925 /*** Thumbnail X and Y ***/
926 *(g->write_buf_next)++ = 0x00;
927 *(g->write_buf_next)++ = 0x00;
931 /*____________________________________________________________________________
933 | em_write_G3_APP1 | Writes Application marker for G3 color fax standard |
934 |__________________|_________________________________________________________|
936 static void em_write_G3_APP1 (
938 int res) /* resolution of image (number of pixels per inch) */
940 res = ((res+50)/100)*100; /* round res to the nearest 100 */
942 PUT_MARKER (g, MARKER_APP+1);
943 PUT_INT (g, 12); /* the length */
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;
953 PUT_INT (g, 1994); /* version is year the std was approved */
954 PUT_INT (g, res); /* finally, the DPI */
958 /*____________________________________________________________________________
960 | em_write_SOF | Writes Start-Of-Frame marker |
961 |______________|_____________________________________________________________|
963 static void em_write_SOF (
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 */
973 PUT_MARKER(g, MARKER_SOF0);
974 PUT_INT(g, 8 + ncomps*3); /* the length */
975 *(g->write_buf_next)++ = 8;
978 *(g->write_buf_next)++ = ncomps;
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);
988 /*____________________________________________________________________________
990 | em_write_DQT | Writes Define-Quantization-Table marker |
991 |______________|_____________________________________________________________|
993 static void em_write_DQT (
995 int precision, /* 0 = 8-bit, 1 = 16-bit */
996 int ident, /* which table, 0-3 */
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;
1007 /*____________________________________________________________________________
1009 | em_write_DHTs | Writes Define-Huffman-Tables marker |
1010 |_______________|____________________________________________________________|
1012 static void em_write_DHTs (
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 */
1023 for (i=0; i<ntables; i++) {
1024 for (j=0; j<16; j++)
1025 nvals += counts[i][j];
1028 PUT_MARKER(g, MARKER_DHT);
1029 PUT_INT(g, 2 + 17*ntables + nvals); /* the length */
1031 for (i=0; i<ntables; i++) {
1032 for (nvals=j=0; j<16; j++)
1033 nvals += counts[i][j];
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];
1042 /*____________________________________________________________________________
1044 | em_write_SOS | Writes Start-Of-Scan marker |
1045 |______________|_____________________________________________________________|
1047 static void em_write_SOS (
1049 int ncomps) /* number of color components; 1 (gray) or 3 (color) */
1053 PUT_MARKER(g, MARKER_SOS);
1054 PUT_INT(g, 6 + 2*ncomps); /* the length */
1055 *(g->write_buf_next)++ = ncomps;
1057 for (i=0; i<ncomps; i++) {
1058 *(g->write_buf_next)++ = i;
1059 *(g->write_buf_next)++ = (i==0 ? 0x00 : 0x11);
1062 *(g->write_buf_next)++ = 0;
1063 *(g->write_buf_next)++ = 63;
1064 *(g->write_buf_next)++ = 0;
1068 /*____________________________________________________________________________
1070 | em_write_DNL | Writes Define-Number-of-Lines marker |
1071 |______________|_____________________________________________________________|
1073 static void em_write_DNL (
1075 int nlines) /* number of lines (raster rows) in the JPEG file */
1077 PUT_MARKER(g, MARKER_DNL);
1078 PUT_INT(g, 4); /* the length */
1084 /*____________________________________________________________________________
1086 | em_write_short_header | Writes a short non-standard header |
1087 |_______________________|____________________________________________________|
1089 | This header is also output by the firmware, and is only in this |
1090 | JPEG encoder for testing purposes. |
1091 |____________________________________________________________________________|
1093 static void em_write_short_header (
1101 UINT comps_in_pixel,
1102 DWORD sample_factors)
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);
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 */
1120 /******************************************************************************
1121 ******************************************************************************
1123 WINOGRAD DCT SECTION
1125 ******************************************************************************
1126 ******************************************************************************/
1130 #define BITS_IN_DCT_FRAC 15
1131 #define ROUND(x) (((x)+(1l<<(BITS_IN_DCT_FRAC-1))) >> BITS_IN_DCT_FRAC)
1134 /*____________________________________________________________________________
1136 | As table | The As multiplication constants used in the Winograd transform |
1137 |__________|_________________________________________________________________|
1139 | These have 15 bits of fraction. |
1140 |____________________________________________________________________________|
1149 /*____________________________________________________________________________
1151 | wino_norm_tbl | DCT scale-factors; this table has been zigzagged |
1152 |_______________|____________________________________________________________|
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
1175 /*____________________________________________________________________________
1177 | scale_for_wino | Computes quant-table and threshold-table for Wino's DCT |
1178 |________________|___________________________________________________________|
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 */
1185 #define FIX(x) ((long) ((x)*(1l<<BITS_IN_DCT_FRAC) + 0.5))
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
1194 if (in[63] < 2) in[63] = 2;
1196 fptr = wino_norm_tbl;
1197 for (i=0; i<64; i++) {
1198 q = FIX (*fptr++ / (float)(*in++));
1200 if (q == 0) *thresh++ = 32767;
1201 else *thresh++ = (1 << (BITS_IN_DCT_FRAC-1)) / q;
1207 /******************************************************************************
1208 ******************************************************************************
1212 ******************************************************************************
1213 ******************************************************************************/
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
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) \
1236 static void zero_prior_DC (PJENC_INST g)
1238 g->prior_DC[0] = g->prior_DC[1] = g->prior_DC[2] = g->prior_DC[3] = 0;
1243 /*____________________________________________________________________________
1245 | encode_init | Inits this section |
1246 |_____________|______________________________________________________________|
1248 static void encode_init (PJENC_INST g)
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++]);
1260 /*____________________________________________________________________________
1262 | encode_block | Encodes an 8x8 block of pixels into Huffman data |
1263 |______________|_____________________________________________________________|
1265 | The input data is variable 'enc_block' via 'enc_block_zz'. |
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 |____________________________________________________________________________|
1272 static void encode_block (
1274 int comp, /* image component number */
1275 const huff_elem_t *dc_huff_p,
1276 const huff_elem_t *ac_huff_p,
1281 int i, data, absdata, run, siz;
1285 /************************************/
1286 /* Quantize and Encode DC component */
1287 /************************************/
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;
1296 if (absdiff < 256) siz = codesize_array[absdiff];
1297 else siz = codesize_array[absdiff>>8] + 8;
1299 WRITE_HUFF_CODE (g, siz, dc_huff_p)
1300 WRITE_BITS (g, diff<0 ? diff-1 : diff, siz)
1302 /************************************************/
1303 /* Quantize, Zigzag, and Encode AC coefficients */
1304 /************************************************/
1308 for (i=63; i>0; i--) /* do 63 times... */
1310 absdata = data = *(*block_p++);
1311 if (absdata < 0) absdata = -absdata;
1313 if (absdata <= *thresh_p++) {
1314 /* quantization would be zero */
1316 run++; /* increment run-length of zeroes */
1318 else /* need to quantize */
1321 WRITE_HUFF_CODE (g, RUN_OF_16, ac_huff_p)
1325 absdata = ROUND ((DWORD )absdata * (DWORD )(*quant_p++));
1326 if (absdata < 256) siz = codesize_array[absdata];
1327 else siz = codesize_array[absdata>>8] + 8;
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)
1336 if (run>0 || g->fDenali)
1337 WRITE_HUFF_CODE (g, 0, ac_huff_p) /* output EOB code */
1344 /*____________________________________________________________________________
1346 | encode_MCU | encodes the given MCU from the input rows in in_rows_ap |
1347 |____________|_______________________________________________________________|
1349 static void encode_MCU (
1355 UINT h_block, v_block;
1356 UINT ul_row, ul_col;
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++) {
1365 /***** level-shift and copy the block into 'enc_block' *****/
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;
1372 for (row=0; row<8; row++) {
1373 row_p = *row_pp++ + ul_col;
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;
1385 dct_forward (g->enc_block);
1387 /***** encode and output the block *****/
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);
1393 encode_block (g, comp, chrom_DC_table, chrom_AC_table,
1394 g->wino_chrom_quant, g->wino_chrom_quant_thres);
1403 /*____________________________________________________________________________
1405 | copy_in_rows | copies a row (or rows) from input buffer to in_rows_ap |
1406 |______________|_____________________________________________________________|
1408 | If input_subsampled is true, the input row data must be in the same odd |
1409 | order which the ASIC outputs it. |
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 |
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 .... |
1427 | In this routine, |
1429 | vert_period = index of which period we're inputting vertically within |
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 |____________________________________________________________________________|
1436 static void copy_in_rows (
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 */
1445 UINT vert_period, vert_mod;
1447 vert_period = row_index / g->max_vert_sam_fac;
1448 vert_mod = row_index % g->max_vert_sam_fac;
1450 if (!g->input_subsampled || (g->max_vert_sam_fac==1 && g->max_horiz_sam_fac==1))
1452 /********************************************************/
1453 /* Perform subsampling (or copying of non-sampled data) */
1454 /********************************************************/
1458 UINT col, horiz_mod;
1461 for (comp=0; comp<g->comps_in_pixel; comp++) {
1462 sam_fac = g->vert_sam_facs[comp];
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];
1468 sam_fac = g->horiz_sam_facs[comp];
1470 if (sam_fac == g->max_horiz_sam_fac) {
1472 /***** fast case: copying all pixels *****/
1474 if (g->comps_in_pixel == 1) {
1475 memcpy (row_p, in_p, g->pixels_in_row);
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;
1485 } else if (sam_fac==1 && g->max_horiz_sam_fac==2) {
1487 /***** fast case: copying every other pixel *****/
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;
1499 /***** slow general case *****/
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;
1505 if (horiz_mod == g->max_horiz_sam_fac) horiz_mod = 0;
1512 *n_bytes_p = g->comps_in_pixel * g->pixels_in_row;
1516 /************************************************/
1517 /* Already subsampled (in the ASIC's odd order) */
1518 /************************************************/
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;
1525 periods_in_row = g->mcus_in_row * 8;
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++;
1540 } else { /* slow general case */
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];
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++;
1555 *n_rows_p = g->max_vert_sam_fac;
1556 *n_bytes_p = in_p - inbuf_p;
1561 /******************************************************************************
1562 ******************************************************************************
1566 ******************************************************************************
1567 ******************************************************************************/
1571 /*____________________________________________________________________________
1573 | scale_q_table | scales a q-table according to the q-factors |
1574 |_______________|____________________________________________________________|
1576 static void scale_q_table (
1582 #define FINAL_DC_INDEX 9
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;
1594 if (i == FINAL_DC_INDEX)
1601 /*____________________________________________________________________________
1603 | output_header | outputs the JPEG header |
1604 |_______________|____________________________________________________________|
1606 | Inputs: lum_quant, chrom_quant, xRes, yRes, pixels_in_row, comps_in_pixel,|
1607 | horiz_sam_facs, vert_sam_facs |
1608 |____________________________________________________________________________|
1610 static UINT output_header (
1616 const BYTE *counts[4];
1617 const BYTE *huffval[4];
1621 counts [0] = lum_DC_counts;
1622 huffval[0] = lum_DC_values;
1626 counts [1] = g->fDenali ? lum_AC_counts_Denali : lum_AC_counts;
1627 huffval[1] = lum_AC_values;
1631 counts [2] = chrom_DC_counts;
1632 huffval[2] = chrom_DC_values;
1636 counts [3] = chrom_AC_counts;
1637 huffval[3] = chrom_AC_values;
1639 write_buf_open (g, buf_p);
1642 if (0 /* todo */ ) {
1643 em_write_short_header (g, g->rows_in_page<0 ? 0 : g->rows_in_page,
1646 g->dc_q_factor, g->ac_q_factor,
1650 if (g->fOutputG3APP1)
1651 em_write_G3_APP1 (g, g->xRes);
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);
1663 return write_buf_close (g);
1668 /*****************************************************************************\
1670 * jpgEncode_openXform - Creates a new instance of the transformer
1672 *****************************************************************************
1674 * This returns a handle for the new instance to be passed into
1675 * all subsequent calls.
1677 * Return value: IP_DONE=success; IP_FATAL_ERROR=misc error.
1679 \*****************************************************************************/
1681 static WORD jpgEncode_openXform (
1682 IP_XFORM_HANDLE *pXform) /* out: returned handle */
1686 INSURE (pXform != NULL);
1687 IP_MEM_ALLOC (sizeof(JENC_INST), g);
1689 memset (g, 0, sizeof(JENC_INST));
1690 g->dwValidChk = CHECK_VALUE;
1694 return IP_FATAL_ERROR;
1699 /*****************************************************************************\
1701 * jpgEncode_setDefaultInputTraits - Specifies default input image traits
1703 *****************************************************************************
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
1710 * Return value: IP_DONE=success; IP_FATAL_ERROR=misc error.
1712 \*****************************************************************************/
1714 static WORD jpgEncode_setDefaultInputTraits (
1715 IP_XFORM_HANDLE hXform, /* in: handle for xform */
1716 PIP_IMAGE_TRAITS pTraits) /* in: default image traits */
1720 HANDLE_TO_PTR (hXform, g);
1721 g->traits = *pTraits; /* a structure copy */
1723 INSURE (g->traits.iPixelsPerRow > 0);
1724 INSURE (g->traits.iComponentsPerPixel==1 || g->traits.iComponentsPerPixel==3);
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;
1734 g->fDidHeader = FALSE;
1738 return IP_FATAL_ERROR;
1743 /*****************************************************************************\
1745 * jpgEncode_setXformSpec - Provides xform-specific information
1747 \*****************************************************************************/
1749 static WORD jpgEncode_setXformSpec (
1750 IP_XFORM_HANDLE hXform, /* in: handle for xform */
1751 DWORD_OR_PVOID aXformInfo[]) /* in: xform information */
1756 HANDLE_TO_PTR (hXform, g);
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;
1766 g->dc_q_factor = (BYTE)(qfacs >> 8);
1767 g->ac_q_factor = (BYTE)(qfacs);
1772 return IP_FATAL_ERROR;
1777 /*****************************************************************************\
1779 * jpgEncode_getHeaderBufSize- Returns size of input buf needed to hold header
1781 \*****************************************************************************/
1783 static WORD jpgEncode_getHeaderBufSize (
1784 IP_XFORM_HANDLE hXform, /* in: handle for xform */
1785 DWORD *pdwInBufLen) /* out: buf size for parsing header */
1789 HANDLE_TO_PTR (hXform, g);
1790 /* since input is raw pixels, there is no header, so set it to zero */
1795 return IP_FATAL_ERROR;
1800 /*****************************************************************************\
1802 * jpgEncode_getActualTraits - Parses header, and returns input & output traits
1804 \*****************************************************************************/
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 */
1817 HANDLE_TO_PTR (hXform, g);
1819 /* Since there is no header, we'll report no usage of input */
1821 *pdwInputNextPos = 0;
1823 /* Since we don't change traits, just copy out the default traits */
1824 *pInTraits = g->traits;
1825 *pOutTraits = g->traits;
1827 return IP_DONE | IP_READY_FOR_DATA;
1830 return IP_FATAL_ERROR;
1835 /****************************************************************************\
1837 * jpgEncode_getActualBufSizes - Returns buf sizes needed for remainder of job
1839 \****************************************************************************/
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 */
1849 HANDLE_TO_PTR (hXform, g);
1850 *pdwMinInBufLen = g->comps_in_pixel * g->pixels_in_row;
1852 n = OUTBUF_NUM_MCUS * MAX_MCU_SIZE;
1853 if (n < MAX_HEADER_SIZE) n = MAX_HEADER_SIZE;
1854 *pdwMinOutBufLen = n;
1859 return IP_FATAL_ERROR;
1864 /*****************************************************************************\
1866 * jpgEncode_convert - the work-horse routine
1868 \*****************************************************************************/
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 */
1883 UINT row, n_rows, n_bytes;
1888 HANDLE_TO_PTR (hXform, g);
1890 *pdwInputNextPos = g->dwInNextPos;
1892 *pdwOutputThisPos = g->dwOutNextPos;
1894 ret_val = IP_READY_FOR_DATA;
1896 /****************************************************/
1897 /* Init and output the Header if we haven't already */
1898 /****************************************************/
1900 if (! g->fDidHeader) {
1910 g->loading_rows = TRUE;
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;
1920 if (g->sample_factors == 0)
1921 g->sample_factors = g->comps_in_pixel==1 ? MONO_FACTORS : COLOR_FACTORS;
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;
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;
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;
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);
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);
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;
1957 g->dwOutNextPos = *pdwOutputUsed = (g->dwDummyHeaderBytes == 0)
1958 ? output_header (g,pbOutputBuf)
1959 : g->dwDummyHeaderBytes;
1961 *pdwOutputThisPos = 0;
1963 /* Allocate the row-buffers in in_rows_ap */
1965 memset (g->in_rows_ap, 0, sizeof(g->in_rows_ap));
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;
1971 for (row=0; row<n_rows; row++) {
1972 IP_MEM_ALLOC (row_len, p);
1973 g->in_rows_ap[comp][row] = p;
1978 *pdwInputNextPos = 0;
1980 g->fDidHeader = TRUE;
1982 return IP_READY_FOR_DATA;
1985 /*********************************/
1986 /* We are filling the input rows */
1987 /*********************************/
1989 if (g->loading_rows) {
1991 /***** Init all row-buffers to white if starting new row-set *****/
1993 n_loaded = g->rows_loaded % g->rows_in_mcu;
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;
2000 for (row=0; row<n_rows; row++)
2001 memset (g->in_rows_ap[comp][row], g->whitePixel[comp], row_len);
2005 if (pbInputBuf == NULL) {
2007 /***** We are being told to flush *****/
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;
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);
2027 em_write_DNL (g, g->rows_received);
2029 *pdwOutputUsed = write_buf_close (g);
2034 /***** Copy the row(s) into in_rows_ap *****/
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 */
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;
2049 /* it's hard to set IP_PRODUCED_ROW 8 times per 8 rows, so cheat */
2051 ret_val |= IP_CONSUMED_ROW | IP_PRODUCED_ROW;
2053 /***** Check if all needed rows are loaded *****/
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 */
2062 /************************************************************/
2063 /* Compress as many MCUs as will fit into the output buffer */
2064 /************************************************************/
2066 while (!g->loading_rows &&
2067 dwOutputAvail-*pdwOutputUsed > MAX_MCU_SIZE) {
2068 PRINT (_T("jpeg_encode_convert_row: Encoding MCU, mcus_sent=%d\n"),
2071 write_buf_open (g, pbOutputBuf + *pdwOutputUsed);
2072 encode_MCU (g, g->mcus_sent);
2073 n_bytes = write_buf_close (g);
2075 g->dwOutNextPos += n_bytes;
2076 *pdwOutputUsed += n_bytes;
2079 if (g->mcus_sent >= g->mcus_in_row) {
2080 g->loading_rows = TRUE;
2085 PRINT (_T("jpeg_encode_convert_row: Returning %04x, out_used=%d\n"),
2086 ret_val, *pdwOutputUsed);
2090 return IP_FATAL_ERROR;
2095 /*****************************************************************************\
2097 * jpgEncode_insertedData - client inserted into our output stream
2099 \*****************************************************************************/
2101 static WORD jpgEncode_insertedData (
2102 IP_XFORM_HANDLE hXform,
2106 return IP_FATAL_ERROR; /* must never be called (can't insert data) */
2111 /*****************************************************************************\
2113 * jpgEncode_newPage - Tells us to flush this page, and start a new page
2115 \*****************************************************************************/
2117 static WORD jpgEncode_newPage (
2118 IP_XFORM_HANDLE hXform)
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 */
2127 return IP_FATAL_ERROR;
2132 /*****************************************************************************\
2134 * jpgEncode_closeXform - Destroys this instance
2136 \*****************************************************************************/
2138 static WORD jpgEncode_closeXform (IP_XFORM_HANDLE hXform)
2141 BYTE **row_pp, **after_pp, *p;
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*));
2147 for ( ; row_pp<after_pp; row_pp++) {
2156 IP_MEM_FREE (g); /* free memory for the instance */
2160 return IP_FATAL_ERROR;
2165 /*****************************************************************************\
2167 * jpgEncodeTbl - Jump-table for encoder
2169 \*****************************************************************************/
2171 IP_XFORM_TBL jpgEncodeTbl = {
2172 jpgEncode_openXform,
2173 jpgEncode_setDefaultInputTraits,
2174 jpgEncode_setXformSpec,
2175 jpgEncode_getHeaderBufSize,
2176 jpgEncode_getActualTraits,
2177 jpgEncode_getActualBufSizes,
2180 jpgEncode_insertedData,
2181 jpgEncode_closeXform