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