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