rev |
line source |
nuclear@26
|
1 /*
|
nuclear@26
|
2 * jcmarker.c
|
nuclear@26
|
3 *
|
nuclear@26
|
4 * Copyright (C) 1991-1998, Thomas G. Lane.
|
nuclear@26
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@26
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@26
|
7 *
|
nuclear@26
|
8 * This file contains routines to write JPEG datastream markers.
|
nuclear@26
|
9 */
|
nuclear@26
|
10
|
nuclear@26
|
11 #define JPEG_INTERNALS
|
nuclear@26
|
12 #include "jinclude.h"
|
nuclear@26
|
13 #include "jpeglib.h"
|
nuclear@26
|
14
|
nuclear@26
|
15
|
nuclear@26
|
16 typedef enum { /* JPEG marker codes */
|
nuclear@26
|
17 M_SOF0 = 0xc0,
|
nuclear@26
|
18 M_SOF1 = 0xc1,
|
nuclear@26
|
19 M_SOF2 = 0xc2,
|
nuclear@26
|
20 M_SOF3 = 0xc3,
|
nuclear@26
|
21
|
nuclear@26
|
22 M_SOF5 = 0xc5,
|
nuclear@26
|
23 M_SOF6 = 0xc6,
|
nuclear@26
|
24 M_SOF7 = 0xc7,
|
nuclear@26
|
25
|
nuclear@26
|
26 M_JPG = 0xc8,
|
nuclear@26
|
27 M_SOF9 = 0xc9,
|
nuclear@26
|
28 M_SOF10 = 0xca,
|
nuclear@26
|
29 M_SOF11 = 0xcb,
|
nuclear@26
|
30
|
nuclear@26
|
31 M_SOF13 = 0xcd,
|
nuclear@26
|
32 M_SOF14 = 0xce,
|
nuclear@26
|
33 M_SOF15 = 0xcf,
|
nuclear@26
|
34
|
nuclear@26
|
35 M_DHT = 0xc4,
|
nuclear@26
|
36
|
nuclear@26
|
37 M_DAC = 0xcc,
|
nuclear@26
|
38
|
nuclear@26
|
39 M_RST0 = 0xd0,
|
nuclear@26
|
40 M_RST1 = 0xd1,
|
nuclear@26
|
41 M_RST2 = 0xd2,
|
nuclear@26
|
42 M_RST3 = 0xd3,
|
nuclear@26
|
43 M_RST4 = 0xd4,
|
nuclear@26
|
44 M_RST5 = 0xd5,
|
nuclear@26
|
45 M_RST6 = 0xd6,
|
nuclear@26
|
46 M_RST7 = 0xd7,
|
nuclear@26
|
47
|
nuclear@26
|
48 M_SOI = 0xd8,
|
nuclear@26
|
49 M_EOI = 0xd9,
|
nuclear@26
|
50 M_SOS = 0xda,
|
nuclear@26
|
51 M_DQT = 0xdb,
|
nuclear@26
|
52 M_DNL = 0xdc,
|
nuclear@26
|
53 M_DRI = 0xdd,
|
nuclear@26
|
54 M_DHP = 0xde,
|
nuclear@26
|
55 M_EXP = 0xdf,
|
nuclear@26
|
56
|
nuclear@26
|
57 M_APP0 = 0xe0,
|
nuclear@26
|
58 M_APP1 = 0xe1,
|
nuclear@26
|
59 M_APP2 = 0xe2,
|
nuclear@26
|
60 M_APP3 = 0xe3,
|
nuclear@26
|
61 M_APP4 = 0xe4,
|
nuclear@26
|
62 M_APP5 = 0xe5,
|
nuclear@26
|
63 M_APP6 = 0xe6,
|
nuclear@26
|
64 M_APP7 = 0xe7,
|
nuclear@26
|
65 M_APP8 = 0xe8,
|
nuclear@26
|
66 M_APP9 = 0xe9,
|
nuclear@26
|
67 M_APP10 = 0xea,
|
nuclear@26
|
68 M_APP11 = 0xeb,
|
nuclear@26
|
69 M_APP12 = 0xec,
|
nuclear@26
|
70 M_APP13 = 0xed,
|
nuclear@26
|
71 M_APP14 = 0xee,
|
nuclear@26
|
72 M_APP15 = 0xef,
|
nuclear@26
|
73
|
nuclear@26
|
74 M_JPG0 = 0xf0,
|
nuclear@26
|
75 M_JPG13 = 0xfd,
|
nuclear@26
|
76 M_COM = 0xfe,
|
nuclear@26
|
77
|
nuclear@26
|
78 M_TEM = 0x01,
|
nuclear@26
|
79
|
nuclear@26
|
80 M_ERROR = 0x100
|
nuclear@26
|
81 } JPEG_MARKER;
|
nuclear@26
|
82
|
nuclear@26
|
83
|
nuclear@26
|
84 /* Private state */
|
nuclear@26
|
85
|
nuclear@26
|
86 typedef struct {
|
nuclear@26
|
87 struct jpeg_marker_writer pub; /* public fields */
|
nuclear@26
|
88
|
nuclear@26
|
89 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
|
nuclear@26
|
90 } my_marker_writer;
|
nuclear@26
|
91
|
nuclear@26
|
92 typedef my_marker_writer * my_marker_ptr;
|
nuclear@26
|
93
|
nuclear@26
|
94
|
nuclear@26
|
95 /*
|
nuclear@26
|
96 * Basic output routines.
|
nuclear@26
|
97 *
|
nuclear@26
|
98 * Note that we do not support suspension while writing a marker.
|
nuclear@26
|
99 * Therefore, an application using suspension must ensure that there is
|
nuclear@26
|
100 * enough buffer space for the initial markers (typ. 600-700 bytes) before
|
nuclear@26
|
101 * calling jpeg_start_compress, and enough space to write the trailing EOI
|
nuclear@26
|
102 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
|
nuclear@26
|
103 * modes are not supported at all with suspension, so those two are the only
|
nuclear@26
|
104 * points where markers will be written.
|
nuclear@26
|
105 */
|
nuclear@26
|
106
|
nuclear@26
|
107 LOCAL(void)
|
nuclear@26
|
108 emit_byte (j_compress_ptr cinfo, int val)
|
nuclear@26
|
109 /* Emit a byte */
|
nuclear@26
|
110 {
|
nuclear@26
|
111 struct jpeg_destination_mgr * dest = cinfo->dest;
|
nuclear@26
|
112
|
nuclear@26
|
113 *(dest->next_output_byte)++ = (JOCTET) val;
|
nuclear@26
|
114 if (--dest->free_in_buffer == 0) {
|
nuclear@26
|
115 if (! (*dest->empty_output_buffer) (cinfo))
|
nuclear@26
|
116 ERREXIT(cinfo, JERR_CANT_SUSPEND);
|
nuclear@26
|
117 }
|
nuclear@26
|
118 }
|
nuclear@26
|
119
|
nuclear@26
|
120
|
nuclear@26
|
121 LOCAL(void)
|
nuclear@26
|
122 emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
|
nuclear@26
|
123 /* Emit a marker code */
|
nuclear@26
|
124 {
|
nuclear@26
|
125 emit_byte(cinfo, 0xFF);
|
nuclear@26
|
126 emit_byte(cinfo, (int) mark);
|
nuclear@26
|
127 }
|
nuclear@26
|
128
|
nuclear@26
|
129
|
nuclear@26
|
130 LOCAL(void)
|
nuclear@26
|
131 emit_2bytes (j_compress_ptr cinfo, int value)
|
nuclear@26
|
132 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
|
nuclear@26
|
133 {
|
nuclear@26
|
134 emit_byte(cinfo, (value >> 8) & 0xFF);
|
nuclear@26
|
135 emit_byte(cinfo, value & 0xFF);
|
nuclear@26
|
136 }
|
nuclear@26
|
137
|
nuclear@26
|
138
|
nuclear@26
|
139 /*
|
nuclear@26
|
140 * Routines to write specific marker types.
|
nuclear@26
|
141 */
|
nuclear@26
|
142
|
nuclear@26
|
143 LOCAL(int)
|
nuclear@26
|
144 emit_dqt (j_compress_ptr cinfo, int index)
|
nuclear@26
|
145 /* Emit a DQT marker */
|
nuclear@26
|
146 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
|
nuclear@26
|
147 {
|
nuclear@26
|
148 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
|
nuclear@26
|
149 int prec;
|
nuclear@26
|
150 int i;
|
nuclear@26
|
151
|
nuclear@26
|
152 if (qtbl == NULL)
|
nuclear@26
|
153 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
|
nuclear@26
|
154
|
nuclear@26
|
155 prec = 0;
|
nuclear@26
|
156 for (i = 0; i < DCTSIZE2; i++) {
|
nuclear@26
|
157 if (qtbl->quantval[i] > 255)
|
nuclear@26
|
158 prec = 1;
|
nuclear@26
|
159 }
|
nuclear@26
|
160
|
nuclear@26
|
161 if (! qtbl->sent_table) {
|
nuclear@26
|
162 emit_marker(cinfo, M_DQT);
|
nuclear@26
|
163
|
nuclear@26
|
164 emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
|
nuclear@26
|
165
|
nuclear@26
|
166 emit_byte(cinfo, index + (prec<<4));
|
nuclear@26
|
167
|
nuclear@26
|
168 for (i = 0; i < DCTSIZE2; i++) {
|
nuclear@26
|
169 /* The table entries must be emitted in zigzag order. */
|
nuclear@26
|
170 unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
|
nuclear@26
|
171 if (prec)
|
nuclear@26
|
172 emit_byte(cinfo, (int) (qval >> 8));
|
nuclear@26
|
173 emit_byte(cinfo, (int) (qval & 0xFF));
|
nuclear@26
|
174 }
|
nuclear@26
|
175
|
nuclear@26
|
176 qtbl->sent_table = TRUE;
|
nuclear@26
|
177 }
|
nuclear@26
|
178
|
nuclear@26
|
179 return prec;
|
nuclear@26
|
180 }
|
nuclear@26
|
181
|
nuclear@26
|
182
|
nuclear@26
|
183 LOCAL(void)
|
nuclear@26
|
184 emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
|
nuclear@26
|
185 /* Emit a DHT marker */
|
nuclear@26
|
186 {
|
nuclear@26
|
187 JHUFF_TBL * htbl;
|
nuclear@26
|
188 int length, i;
|
nuclear@26
|
189
|
nuclear@26
|
190 if (is_ac) {
|
nuclear@26
|
191 htbl = cinfo->ac_huff_tbl_ptrs[index];
|
nuclear@26
|
192 index += 0x10; /* output index has AC bit set */
|
nuclear@26
|
193 } else {
|
nuclear@26
|
194 htbl = cinfo->dc_huff_tbl_ptrs[index];
|
nuclear@26
|
195 }
|
nuclear@26
|
196
|
nuclear@26
|
197 if (htbl == NULL)
|
nuclear@26
|
198 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
|
nuclear@26
|
199
|
nuclear@26
|
200 if (! htbl->sent_table) {
|
nuclear@26
|
201 emit_marker(cinfo, M_DHT);
|
nuclear@26
|
202
|
nuclear@26
|
203 length = 0;
|
nuclear@26
|
204 for (i = 1; i <= 16; i++)
|
nuclear@26
|
205 length += htbl->bits[i];
|
nuclear@26
|
206
|
nuclear@26
|
207 emit_2bytes(cinfo, length + 2 + 1 + 16);
|
nuclear@26
|
208 emit_byte(cinfo, index);
|
nuclear@26
|
209
|
nuclear@26
|
210 for (i = 1; i <= 16; i++)
|
nuclear@26
|
211 emit_byte(cinfo, htbl->bits[i]);
|
nuclear@26
|
212
|
nuclear@26
|
213 for (i = 0; i < length; i++)
|
nuclear@26
|
214 emit_byte(cinfo, htbl->huffval[i]);
|
nuclear@26
|
215
|
nuclear@26
|
216 htbl->sent_table = TRUE;
|
nuclear@26
|
217 }
|
nuclear@26
|
218 }
|
nuclear@26
|
219
|
nuclear@26
|
220
|
nuclear@26
|
221 LOCAL(void)
|
nuclear@26
|
222 emit_dac (j_compress_ptr cinfo)
|
nuclear@26
|
223 /* Emit a DAC marker */
|
nuclear@26
|
224 /* Since the useful info is so small, we want to emit all the tables in */
|
nuclear@26
|
225 /* one DAC marker. Therefore this routine does its own scan of the table. */
|
nuclear@26
|
226 {
|
nuclear@26
|
227 #ifdef C_ARITH_CODING_SUPPORTED
|
nuclear@26
|
228 char dc_in_use[NUM_ARITH_TBLS];
|
nuclear@26
|
229 char ac_in_use[NUM_ARITH_TBLS];
|
nuclear@26
|
230 int length, i;
|
nuclear@26
|
231 jpeg_component_info *compptr;
|
nuclear@26
|
232
|
nuclear@26
|
233 for (i = 0; i < NUM_ARITH_TBLS; i++)
|
nuclear@26
|
234 dc_in_use[i] = ac_in_use[i] = 0;
|
nuclear@26
|
235
|
nuclear@26
|
236 for (i = 0; i < cinfo->comps_in_scan; i++) {
|
nuclear@26
|
237 compptr = cinfo->cur_comp_info[i];
|
nuclear@26
|
238 dc_in_use[compptr->dc_tbl_no] = 1;
|
nuclear@26
|
239 ac_in_use[compptr->ac_tbl_no] = 1;
|
nuclear@26
|
240 }
|
nuclear@26
|
241
|
nuclear@26
|
242 length = 0;
|
nuclear@26
|
243 for (i = 0; i < NUM_ARITH_TBLS; i++)
|
nuclear@26
|
244 length += dc_in_use[i] + ac_in_use[i];
|
nuclear@26
|
245
|
nuclear@26
|
246 emit_marker(cinfo, M_DAC);
|
nuclear@26
|
247
|
nuclear@26
|
248 emit_2bytes(cinfo, length*2 + 2);
|
nuclear@26
|
249
|
nuclear@26
|
250 for (i = 0; i < NUM_ARITH_TBLS; i++) {
|
nuclear@26
|
251 if (dc_in_use[i]) {
|
nuclear@26
|
252 emit_byte(cinfo, i);
|
nuclear@26
|
253 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
|
nuclear@26
|
254 }
|
nuclear@26
|
255 if (ac_in_use[i]) {
|
nuclear@26
|
256 emit_byte(cinfo, i + 0x10);
|
nuclear@26
|
257 emit_byte(cinfo, cinfo->arith_ac_K[i]);
|
nuclear@26
|
258 }
|
nuclear@26
|
259 }
|
nuclear@26
|
260 #endif /* C_ARITH_CODING_SUPPORTED */
|
nuclear@26
|
261 }
|
nuclear@26
|
262
|
nuclear@26
|
263
|
nuclear@26
|
264 LOCAL(void)
|
nuclear@26
|
265 emit_dri (j_compress_ptr cinfo)
|
nuclear@26
|
266 /* Emit a DRI marker */
|
nuclear@26
|
267 {
|
nuclear@26
|
268 emit_marker(cinfo, M_DRI);
|
nuclear@26
|
269
|
nuclear@26
|
270 emit_2bytes(cinfo, 4); /* fixed length */
|
nuclear@26
|
271
|
nuclear@26
|
272 emit_2bytes(cinfo, (int) cinfo->restart_interval);
|
nuclear@26
|
273 }
|
nuclear@26
|
274
|
nuclear@26
|
275
|
nuclear@26
|
276 LOCAL(void)
|
nuclear@26
|
277 emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
|
nuclear@26
|
278 /* Emit a SOF marker */
|
nuclear@26
|
279 {
|
nuclear@26
|
280 int ci;
|
nuclear@26
|
281 jpeg_component_info *compptr;
|
nuclear@26
|
282
|
nuclear@26
|
283 emit_marker(cinfo, code);
|
nuclear@26
|
284
|
nuclear@26
|
285 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
|
nuclear@26
|
286
|
nuclear@26
|
287 /* Make sure image isn't bigger than SOF field can handle */
|
nuclear@26
|
288 if ((long) cinfo->image_height > 65535L ||
|
nuclear@26
|
289 (long) cinfo->image_width > 65535L)
|
nuclear@26
|
290 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
|
nuclear@26
|
291
|
nuclear@26
|
292 emit_byte(cinfo, cinfo->data_precision);
|
nuclear@26
|
293 emit_2bytes(cinfo, (int) cinfo->image_height);
|
nuclear@26
|
294 emit_2bytes(cinfo, (int) cinfo->image_width);
|
nuclear@26
|
295
|
nuclear@26
|
296 emit_byte(cinfo, cinfo->num_components);
|
nuclear@26
|
297
|
nuclear@26
|
298 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@26
|
299 ci++, compptr++) {
|
nuclear@26
|
300 emit_byte(cinfo, compptr->component_id);
|
nuclear@26
|
301 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
|
nuclear@26
|
302 emit_byte(cinfo, compptr->quant_tbl_no);
|
nuclear@26
|
303 }
|
nuclear@26
|
304 }
|
nuclear@26
|
305
|
nuclear@26
|
306
|
nuclear@26
|
307 LOCAL(void)
|
nuclear@26
|
308 emit_sos (j_compress_ptr cinfo)
|
nuclear@26
|
309 /* Emit a SOS marker */
|
nuclear@26
|
310 {
|
nuclear@26
|
311 int i, td, ta;
|
nuclear@26
|
312 jpeg_component_info *compptr;
|
nuclear@26
|
313
|
nuclear@26
|
314 emit_marker(cinfo, M_SOS);
|
nuclear@26
|
315
|
nuclear@26
|
316 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
|
nuclear@26
|
317
|
nuclear@26
|
318 emit_byte(cinfo, cinfo->comps_in_scan);
|
nuclear@26
|
319
|
nuclear@26
|
320 for (i = 0; i < cinfo->comps_in_scan; i++) {
|
nuclear@26
|
321 compptr = cinfo->cur_comp_info[i];
|
nuclear@26
|
322 emit_byte(cinfo, compptr->component_id);
|
nuclear@26
|
323 td = compptr->dc_tbl_no;
|
nuclear@26
|
324 ta = compptr->ac_tbl_no;
|
nuclear@26
|
325 if (cinfo->progressive_mode) {
|
nuclear@26
|
326 /* Progressive mode: only DC or only AC tables are used in one scan;
|
nuclear@26
|
327 * furthermore, Huffman coding of DC refinement uses no table at all.
|
nuclear@26
|
328 * We emit 0 for unused field(s); this is recommended by the P&M text
|
nuclear@26
|
329 * but does not seem to be specified in the standard.
|
nuclear@26
|
330 */
|
nuclear@26
|
331 if (cinfo->Ss == 0) {
|
nuclear@26
|
332 ta = 0; /* DC scan */
|
nuclear@26
|
333 if (cinfo->Ah != 0 && !cinfo->arith_code)
|
nuclear@26
|
334 td = 0; /* no DC table either */
|
nuclear@26
|
335 } else {
|
nuclear@26
|
336 td = 0; /* AC scan */
|
nuclear@26
|
337 }
|
nuclear@26
|
338 }
|
nuclear@26
|
339 emit_byte(cinfo, (td << 4) + ta);
|
nuclear@26
|
340 }
|
nuclear@26
|
341
|
nuclear@26
|
342 emit_byte(cinfo, cinfo->Ss);
|
nuclear@26
|
343 emit_byte(cinfo, cinfo->Se);
|
nuclear@26
|
344 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
|
nuclear@26
|
345 }
|
nuclear@26
|
346
|
nuclear@26
|
347
|
nuclear@26
|
348 LOCAL(void)
|
nuclear@26
|
349 emit_jfif_app0 (j_compress_ptr cinfo)
|
nuclear@26
|
350 /* Emit a JFIF-compliant APP0 marker */
|
nuclear@26
|
351 {
|
nuclear@26
|
352 /*
|
nuclear@26
|
353 * Length of APP0 block (2 bytes)
|
nuclear@26
|
354 * Block ID (4 bytes - ASCII "JFIF")
|
nuclear@26
|
355 * Zero byte (1 byte to terminate the ID string)
|
nuclear@26
|
356 * Version Major, Minor (2 bytes - major first)
|
nuclear@26
|
357 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
|
nuclear@26
|
358 * Xdpu (2 bytes - dots per unit horizontal)
|
nuclear@26
|
359 * Ydpu (2 bytes - dots per unit vertical)
|
nuclear@26
|
360 * Thumbnail X size (1 byte)
|
nuclear@26
|
361 * Thumbnail Y size (1 byte)
|
nuclear@26
|
362 */
|
nuclear@26
|
363
|
nuclear@26
|
364 emit_marker(cinfo, M_APP0);
|
nuclear@26
|
365
|
nuclear@26
|
366 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
|
nuclear@26
|
367
|
nuclear@26
|
368 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
|
nuclear@26
|
369 emit_byte(cinfo, 0x46);
|
nuclear@26
|
370 emit_byte(cinfo, 0x49);
|
nuclear@26
|
371 emit_byte(cinfo, 0x46);
|
nuclear@26
|
372 emit_byte(cinfo, 0);
|
nuclear@26
|
373 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
|
nuclear@26
|
374 emit_byte(cinfo, cinfo->JFIF_minor_version);
|
nuclear@26
|
375 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
|
nuclear@26
|
376 emit_2bytes(cinfo, (int) cinfo->X_density);
|
nuclear@26
|
377 emit_2bytes(cinfo, (int) cinfo->Y_density);
|
nuclear@26
|
378 emit_byte(cinfo, 0); /* No thumbnail image */
|
nuclear@26
|
379 emit_byte(cinfo, 0);
|
nuclear@26
|
380 }
|
nuclear@26
|
381
|
nuclear@26
|
382
|
nuclear@26
|
383 LOCAL(void)
|
nuclear@26
|
384 emit_adobe_app14 (j_compress_ptr cinfo)
|
nuclear@26
|
385 /* Emit an Adobe APP14 marker */
|
nuclear@26
|
386 {
|
nuclear@26
|
387 /*
|
nuclear@26
|
388 * Length of APP14 block (2 bytes)
|
nuclear@26
|
389 * Block ID (5 bytes - ASCII "Adobe")
|
nuclear@26
|
390 * Version Number (2 bytes - currently 100)
|
nuclear@26
|
391 * Flags0 (2 bytes - currently 0)
|
nuclear@26
|
392 * Flags1 (2 bytes - currently 0)
|
nuclear@26
|
393 * Color transform (1 byte)
|
nuclear@26
|
394 *
|
nuclear@26
|
395 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
|
nuclear@26
|
396 * now in circulation seem to use Version = 100, so that's what we write.
|
nuclear@26
|
397 *
|
nuclear@26
|
398 * We write the color transform byte as 1 if the JPEG color space is
|
nuclear@26
|
399 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
|
nuclear@26
|
400 * whether the encoder performed a transformation, which is pretty useless.
|
nuclear@26
|
401 */
|
nuclear@26
|
402
|
nuclear@26
|
403 emit_marker(cinfo, M_APP14);
|
nuclear@26
|
404
|
nuclear@26
|
405 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
|
nuclear@26
|
406
|
nuclear@26
|
407 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
|
nuclear@26
|
408 emit_byte(cinfo, 0x64);
|
nuclear@26
|
409 emit_byte(cinfo, 0x6F);
|
nuclear@26
|
410 emit_byte(cinfo, 0x62);
|
nuclear@26
|
411 emit_byte(cinfo, 0x65);
|
nuclear@26
|
412 emit_2bytes(cinfo, 100); /* Version */
|
nuclear@26
|
413 emit_2bytes(cinfo, 0); /* Flags0 */
|
nuclear@26
|
414 emit_2bytes(cinfo, 0); /* Flags1 */
|
nuclear@26
|
415 switch (cinfo->jpeg_color_space) {
|
nuclear@26
|
416 case JCS_YCbCr:
|
nuclear@26
|
417 emit_byte(cinfo, 1); /* Color transform = 1 */
|
nuclear@26
|
418 break;
|
nuclear@26
|
419 case JCS_YCCK:
|
nuclear@26
|
420 emit_byte(cinfo, 2); /* Color transform = 2 */
|
nuclear@26
|
421 break;
|
nuclear@26
|
422 default:
|
nuclear@26
|
423 emit_byte(cinfo, 0); /* Color transform = 0 */
|
nuclear@26
|
424 break;
|
nuclear@26
|
425 }
|
nuclear@26
|
426 }
|
nuclear@26
|
427
|
nuclear@26
|
428
|
nuclear@26
|
429 /*
|
nuclear@26
|
430 * These routines allow writing an arbitrary marker with parameters.
|
nuclear@26
|
431 * The only intended use is to emit COM or APPn markers after calling
|
nuclear@26
|
432 * write_file_header and before calling write_frame_header.
|
nuclear@26
|
433 * Other uses are not guaranteed to produce desirable results.
|
nuclear@26
|
434 * Counting the parameter bytes properly is the caller's responsibility.
|
nuclear@26
|
435 */
|
nuclear@26
|
436
|
nuclear@26
|
437 METHODDEF(void)
|
nuclear@26
|
438 write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
|
nuclear@26
|
439 /* Emit an arbitrary marker header */
|
nuclear@26
|
440 {
|
nuclear@26
|
441 if (datalen > (unsigned int) 65533) /* safety check */
|
nuclear@26
|
442 ERREXIT(cinfo, JERR_BAD_LENGTH);
|
nuclear@26
|
443
|
nuclear@26
|
444 emit_marker(cinfo, (JPEG_MARKER) marker);
|
nuclear@26
|
445
|
nuclear@26
|
446 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
|
nuclear@26
|
447 }
|
nuclear@26
|
448
|
nuclear@26
|
449 METHODDEF(void)
|
nuclear@26
|
450 write_marker_byte (j_compress_ptr cinfo, int val)
|
nuclear@26
|
451 /* Emit one byte of marker parameters following write_marker_header */
|
nuclear@26
|
452 {
|
nuclear@26
|
453 emit_byte(cinfo, val);
|
nuclear@26
|
454 }
|
nuclear@26
|
455
|
nuclear@26
|
456
|
nuclear@26
|
457 /*
|
nuclear@26
|
458 * Write datastream header.
|
nuclear@26
|
459 * This consists of an SOI and optional APPn markers.
|
nuclear@26
|
460 * We recommend use of the JFIF marker, but not the Adobe marker,
|
nuclear@26
|
461 * when using YCbCr or grayscale data. The JFIF marker should NOT
|
nuclear@26
|
462 * be used for any other JPEG colorspace. The Adobe marker is helpful
|
nuclear@26
|
463 * to distinguish RGB, CMYK, and YCCK colorspaces.
|
nuclear@26
|
464 * Note that an application can write additional header markers after
|
nuclear@26
|
465 * jpeg_start_compress returns.
|
nuclear@26
|
466 */
|
nuclear@26
|
467
|
nuclear@26
|
468 METHODDEF(void)
|
nuclear@26
|
469 write_file_header (j_compress_ptr cinfo)
|
nuclear@26
|
470 {
|
nuclear@26
|
471 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
|
nuclear@26
|
472
|
nuclear@26
|
473 emit_marker(cinfo, M_SOI); /* first the SOI */
|
nuclear@26
|
474
|
nuclear@26
|
475 /* SOI is defined to reset restart interval to 0 */
|
nuclear@26
|
476 marker->last_restart_interval = 0;
|
nuclear@26
|
477
|
nuclear@26
|
478 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
|
nuclear@26
|
479 emit_jfif_app0(cinfo);
|
nuclear@26
|
480 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
|
nuclear@26
|
481 emit_adobe_app14(cinfo);
|
nuclear@26
|
482 }
|
nuclear@26
|
483
|
nuclear@26
|
484
|
nuclear@26
|
485 /*
|
nuclear@26
|
486 * Write frame header.
|
nuclear@26
|
487 * This consists of DQT and SOFn markers.
|
nuclear@26
|
488 * Note that we do not emit the SOF until we have emitted the DQT(s).
|
nuclear@26
|
489 * This avoids compatibility problems with incorrect implementations that
|
nuclear@26
|
490 * try to error-check the quant table numbers as soon as they see the SOF.
|
nuclear@26
|
491 */
|
nuclear@26
|
492
|
nuclear@26
|
493 METHODDEF(void)
|
nuclear@26
|
494 write_frame_header (j_compress_ptr cinfo)
|
nuclear@26
|
495 {
|
nuclear@26
|
496 int ci, prec;
|
nuclear@26
|
497 boolean is_baseline;
|
nuclear@26
|
498 jpeg_component_info *compptr;
|
nuclear@26
|
499
|
nuclear@26
|
500 /* Emit DQT for each quantization table.
|
nuclear@26
|
501 * Note that emit_dqt() suppresses any duplicate tables.
|
nuclear@26
|
502 */
|
nuclear@26
|
503 prec = 0;
|
nuclear@26
|
504 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@26
|
505 ci++, compptr++) {
|
nuclear@26
|
506 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
|
nuclear@26
|
507 }
|
nuclear@26
|
508 /* now prec is nonzero iff there are any 16-bit quant tables. */
|
nuclear@26
|
509
|
nuclear@26
|
510 /* Check for a non-baseline specification.
|
nuclear@26
|
511 * Note we assume that Huffman table numbers won't be changed later.
|
nuclear@26
|
512 */
|
nuclear@26
|
513 if (cinfo->arith_code || cinfo->progressive_mode ||
|
nuclear@26
|
514 cinfo->data_precision != 8) {
|
nuclear@26
|
515 is_baseline = FALSE;
|
nuclear@26
|
516 } else {
|
nuclear@26
|
517 is_baseline = TRUE;
|
nuclear@26
|
518 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
|
nuclear@26
|
519 ci++, compptr++) {
|
nuclear@26
|
520 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
|
nuclear@26
|
521 is_baseline = FALSE;
|
nuclear@26
|
522 }
|
nuclear@26
|
523 if (prec && is_baseline) {
|
nuclear@26
|
524 is_baseline = FALSE;
|
nuclear@26
|
525 /* If it's baseline except for quantizer size, warn the user */
|
nuclear@26
|
526 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
|
nuclear@26
|
527 }
|
nuclear@26
|
528 }
|
nuclear@26
|
529
|
nuclear@26
|
530 /* Emit the proper SOF marker */
|
nuclear@26
|
531 if (cinfo->arith_code) {
|
nuclear@26
|
532 emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
|
nuclear@26
|
533 } else {
|
nuclear@26
|
534 if (cinfo->progressive_mode)
|
nuclear@26
|
535 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
|
nuclear@26
|
536 else if (is_baseline)
|
nuclear@26
|
537 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
|
nuclear@26
|
538 else
|
nuclear@26
|
539 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
|
nuclear@26
|
540 }
|
nuclear@26
|
541 }
|
nuclear@26
|
542
|
nuclear@26
|
543
|
nuclear@26
|
544 /*
|
nuclear@26
|
545 * Write scan header.
|
nuclear@26
|
546 * This consists of DHT or DAC markers, optional DRI, and SOS.
|
nuclear@26
|
547 * Compressed data will be written following the SOS.
|
nuclear@26
|
548 */
|
nuclear@26
|
549
|
nuclear@26
|
550 METHODDEF(void)
|
nuclear@26
|
551 write_scan_header (j_compress_ptr cinfo)
|
nuclear@26
|
552 {
|
nuclear@26
|
553 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
|
nuclear@26
|
554 int i;
|
nuclear@26
|
555 jpeg_component_info *compptr;
|
nuclear@26
|
556
|
nuclear@26
|
557 if (cinfo->arith_code) {
|
nuclear@26
|
558 /* Emit arith conditioning info. We may have some duplication
|
nuclear@26
|
559 * if the file has multiple scans, but it's so small it's hardly
|
nuclear@26
|
560 * worth worrying about.
|
nuclear@26
|
561 */
|
nuclear@26
|
562 emit_dac(cinfo);
|
nuclear@26
|
563 } else {
|
nuclear@26
|
564 /* Emit Huffman tables.
|
nuclear@26
|
565 * Note that emit_dht() suppresses any duplicate tables.
|
nuclear@26
|
566 */
|
nuclear@26
|
567 for (i = 0; i < cinfo->comps_in_scan; i++) {
|
nuclear@26
|
568 compptr = cinfo->cur_comp_info[i];
|
nuclear@26
|
569 if (cinfo->progressive_mode) {
|
nuclear@26
|
570 /* Progressive mode: only DC or only AC tables are used in one scan */
|
nuclear@26
|
571 if (cinfo->Ss == 0) {
|
nuclear@26
|
572 if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
|
nuclear@26
|
573 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
|
nuclear@26
|
574 } else {
|
nuclear@26
|
575 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
|
nuclear@26
|
576 }
|
nuclear@26
|
577 } else {
|
nuclear@26
|
578 /* Sequential mode: need both DC and AC tables */
|
nuclear@26
|
579 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
|
nuclear@26
|
580 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
|
nuclear@26
|
581 }
|
nuclear@26
|
582 }
|
nuclear@26
|
583 }
|
nuclear@26
|
584
|
nuclear@26
|
585 /* Emit DRI if required --- note that DRI value could change for each scan.
|
nuclear@26
|
586 * We avoid wasting space with unnecessary DRIs, however.
|
nuclear@26
|
587 */
|
nuclear@26
|
588 if (cinfo->restart_interval != marker->last_restart_interval) {
|
nuclear@26
|
589 emit_dri(cinfo);
|
nuclear@26
|
590 marker->last_restart_interval = cinfo->restart_interval;
|
nuclear@26
|
591 }
|
nuclear@26
|
592
|
nuclear@26
|
593 emit_sos(cinfo);
|
nuclear@26
|
594 }
|
nuclear@26
|
595
|
nuclear@26
|
596
|
nuclear@26
|
597 /*
|
nuclear@26
|
598 * Write datastream trailer.
|
nuclear@26
|
599 */
|
nuclear@26
|
600
|
nuclear@26
|
601 METHODDEF(void)
|
nuclear@26
|
602 write_file_trailer (j_compress_ptr cinfo)
|
nuclear@26
|
603 {
|
nuclear@26
|
604 emit_marker(cinfo, M_EOI);
|
nuclear@26
|
605 }
|
nuclear@26
|
606
|
nuclear@26
|
607
|
nuclear@26
|
608 /*
|
nuclear@26
|
609 * Write an abbreviated table-specification datastream.
|
nuclear@26
|
610 * This consists of SOI, DQT and DHT tables, and EOI.
|
nuclear@26
|
611 * Any table that is defined and not marked sent_table = TRUE will be
|
nuclear@26
|
612 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
|
nuclear@26
|
613 */
|
nuclear@26
|
614
|
nuclear@26
|
615 METHODDEF(void)
|
nuclear@26
|
616 write_tables_only (j_compress_ptr cinfo)
|
nuclear@26
|
617 {
|
nuclear@26
|
618 int i;
|
nuclear@26
|
619
|
nuclear@26
|
620 emit_marker(cinfo, M_SOI);
|
nuclear@26
|
621
|
nuclear@26
|
622 for (i = 0; i < NUM_QUANT_TBLS; i++) {
|
nuclear@26
|
623 if (cinfo->quant_tbl_ptrs[i] != NULL)
|
nuclear@26
|
624 (void) emit_dqt(cinfo, i);
|
nuclear@26
|
625 }
|
nuclear@26
|
626
|
nuclear@26
|
627 if (! cinfo->arith_code) {
|
nuclear@26
|
628 for (i = 0; i < NUM_HUFF_TBLS; i++) {
|
nuclear@26
|
629 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
|
nuclear@26
|
630 emit_dht(cinfo, i, FALSE);
|
nuclear@26
|
631 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
|
nuclear@26
|
632 emit_dht(cinfo, i, TRUE);
|
nuclear@26
|
633 }
|
nuclear@26
|
634 }
|
nuclear@26
|
635
|
nuclear@26
|
636 emit_marker(cinfo, M_EOI);
|
nuclear@26
|
637 }
|
nuclear@26
|
638
|
nuclear@26
|
639
|
nuclear@26
|
640 /*
|
nuclear@26
|
641 * Initialize the marker writer module.
|
nuclear@26
|
642 */
|
nuclear@26
|
643
|
nuclear@26
|
644 GLOBAL(void)
|
nuclear@26
|
645 jinit_marker_writer (j_compress_ptr cinfo)
|
nuclear@26
|
646 {
|
nuclear@26
|
647 my_marker_ptr marker;
|
nuclear@26
|
648
|
nuclear@26
|
649 /* Create the subobject */
|
nuclear@26
|
650 marker = (my_marker_ptr)
|
nuclear@26
|
651 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@26
|
652 SIZEOF(my_marker_writer));
|
nuclear@26
|
653 cinfo->marker = (struct jpeg_marker_writer *) marker;
|
nuclear@26
|
654 /* Initialize method pointers */
|
nuclear@26
|
655 marker->pub.write_file_header = write_file_header;
|
nuclear@26
|
656 marker->pub.write_frame_header = write_frame_header;
|
nuclear@26
|
657 marker->pub.write_scan_header = write_scan_header;
|
nuclear@26
|
658 marker->pub.write_file_trailer = write_file_trailer;
|
nuclear@26
|
659 marker->pub.write_tables_only = write_tables_only;
|
nuclear@26
|
660 marker->pub.write_marker_header = write_marker_header;
|
nuclear@26
|
661 marker->pub.write_marker_byte = write_marker_byte;
|
nuclear@26
|
662 /* Initialize private state */
|
nuclear@26
|
663 marker->last_restart_interval = 0;
|
nuclear@26
|
664 }
|