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.
11 #define JPEG_INTERNALS
16 typedef enum { /* JPEG marker codes */
87 struct jpeg_marker_writer pub; /* public fields */
89 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
92 typedef my_marker_writer * my_marker_ptr;
96 * Basic output routines.
98 * Note that we do not support suspension while writing a marker.
99 * Therefore, an application using suspension must ensure that there is
100 * enough buffer space for the initial markers (typ. 600-700 bytes) before
101 * calling jpeg_start_compress, and enough space to write the trailing EOI
102 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
103 * modes are not supported at all with suspension, so those two are the only
104 * points where markers will be written.
108 emit_byte (j_compress_ptr cinfo, int val)
111 struct jpeg_destination_mgr * dest = cinfo->dest;
113 *(dest->next_output_byte)++ = (JOCTET) val;
114 if (--dest->free_in_buffer == 0) {
115 if (! (*dest->empty_output_buffer) (cinfo))
116 ERREXIT(cinfo, JERR_CANT_SUSPEND);
122 emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
123 /* Emit a marker code */
125 emit_byte(cinfo, 0xFF);
126 emit_byte(cinfo, (int) mark);
131 emit_2bytes (j_compress_ptr cinfo, int value)
132 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
134 emit_byte(cinfo, (value >> 8) & 0xFF);
135 emit_byte(cinfo, value & 0xFF);
140 * Routines to write specific marker types.
144 emit_dqt (j_compress_ptr cinfo, int index)
145 /* Emit a DQT marker */
146 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
148 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
153 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
156 for (i = 0; i < DCTSIZE2; i++) {
157 if (qtbl->quantval[i] > 255)
161 if (! qtbl->sent_table) {
162 emit_marker(cinfo, M_DQT);
164 emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
166 emit_byte(cinfo, index + (prec<<4));
168 for (i = 0; i < DCTSIZE2; i++) {
169 /* The table entries must be emitted in zigzag order. */
170 unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
172 emit_byte(cinfo, (int) (qval >> 8));
173 emit_byte(cinfo, (int) (qval & 0xFF));
176 qtbl->sent_table = TRUE;
184 emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
185 /* Emit a DHT marker */
191 htbl = cinfo->ac_huff_tbl_ptrs[index];
192 index += 0x10; /* output index has AC bit set */
194 htbl = cinfo->dc_huff_tbl_ptrs[index];
198 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
200 if (! htbl->sent_table) {
201 emit_marker(cinfo, M_DHT);
204 for (i = 1; i <= 16; i++)
205 length += htbl->bits[i];
207 emit_2bytes(cinfo, length + 2 + 1 + 16);
208 emit_byte(cinfo, index);
210 for (i = 1; i <= 16; i++)
211 emit_byte(cinfo, htbl->bits[i]);
213 for (i = 0; i < length; i++)
214 emit_byte(cinfo, htbl->huffval[i]);
216 htbl->sent_table = TRUE;
222 emit_dac (j_compress_ptr cinfo)
223 /* Emit a DAC marker */
224 /* Since the useful info is so small, we want to emit all the tables in */
225 /* one DAC marker. Therefore this routine does its own scan of the table. */
227 #ifdef C_ARITH_CODING_SUPPORTED
228 char dc_in_use[NUM_ARITH_TBLS];
229 char ac_in_use[NUM_ARITH_TBLS];
231 jpeg_component_info *compptr;
233 for (i = 0; i < NUM_ARITH_TBLS; i++)
234 dc_in_use[i] = ac_in_use[i] = 0;
236 for (i = 0; i < cinfo->comps_in_scan; i++) {
237 compptr = cinfo->cur_comp_info[i];
238 dc_in_use[compptr->dc_tbl_no] = 1;
239 ac_in_use[compptr->ac_tbl_no] = 1;
243 for (i = 0; i < NUM_ARITH_TBLS; i++)
244 length += dc_in_use[i] + ac_in_use[i];
246 emit_marker(cinfo, M_DAC);
248 emit_2bytes(cinfo, length*2 + 2);
250 for (i = 0; i < NUM_ARITH_TBLS; i++) {
253 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
256 emit_byte(cinfo, i + 0x10);
257 emit_byte(cinfo, cinfo->arith_ac_K[i]);
262 #endif /* C_ARITH_CODING_SUPPORTED */
267 emit_dri (j_compress_ptr cinfo)
268 /* Emit a DRI marker */
270 emit_marker(cinfo, M_DRI);
272 emit_2bytes(cinfo, 4); /* fixed length */
274 emit_2bytes(cinfo, (int) cinfo->restart_interval);
279 emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
280 /* Emit a SOF marker */
283 jpeg_component_info *compptr;
285 emit_marker(cinfo, code);
287 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
289 /* Make sure image isn't bigger than SOF field can handle */
290 if ((long) cinfo->image_height > 65535L ||
291 (long) cinfo->image_width > 65535L)
292 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
294 emit_byte(cinfo, cinfo->data_precision);
295 emit_2bytes(cinfo, (int) cinfo->image_height);
296 emit_2bytes(cinfo, (int) cinfo->image_width);
298 emit_byte(cinfo, cinfo->num_components);
300 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
302 emit_byte(cinfo, compptr->component_id);
303 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
304 emit_byte(cinfo, compptr->quant_tbl_no);
310 emit_sos (j_compress_ptr cinfo)
311 /* Emit a SOS marker */
314 jpeg_component_info *compptr;
316 emit_marker(cinfo, M_SOS);
318 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
320 emit_byte(cinfo, cinfo->comps_in_scan);
322 for (i = 0; i < cinfo->comps_in_scan; i++) {
323 compptr = cinfo->cur_comp_info[i];
324 emit_byte(cinfo, compptr->component_id);
325 td = compptr->dc_tbl_no;
326 ta = compptr->ac_tbl_no;
327 if (cinfo->process == JPROC_PROGRESSIVE) {
328 /* Progressive mode: only DC or only AC tables are used in one scan;
329 * furthermore, Huffman coding of DC refinement uses no table at all.
330 * We emit 0 for unused field(s); this is recommended by the P&M text
331 * but does not seem to be specified in the standard.
333 if (cinfo->Ss == 0) {
334 ta = 0; /* DC scan */
335 if (cinfo->Ah != 0 && !cinfo->arith_code)
336 td = 0; /* no DC table either */
338 td = 0; /* AC scan */
341 emit_byte(cinfo, (td << 4) + ta);
344 emit_byte(cinfo, cinfo->Ss);
345 emit_byte(cinfo, cinfo->Se);
346 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
351 emit_jfif_app0 (j_compress_ptr cinfo)
352 /* Emit a JFIF-compliant APP0 marker */
355 * Length of APP0 block (2 bytes)
356 * Block ID (4 bytes - ASCII "JFIF")
357 * Zero byte (1 byte to terminate the ID string)
358 * Version Major, Minor (2 bytes - major first)
359 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
360 * Xdpu (2 bytes - dots per unit horizontal)
361 * Ydpu (2 bytes - dots per unit vertical)
362 * Thumbnail X size (1 byte)
363 * Thumbnail Y size (1 byte)
366 emit_marker(cinfo, M_APP0);
368 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
370 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
371 emit_byte(cinfo, 0x46);
372 emit_byte(cinfo, 0x49);
373 emit_byte(cinfo, 0x46);
375 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
376 emit_byte(cinfo, cinfo->JFIF_minor_version);
377 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
378 emit_2bytes(cinfo, (int) cinfo->X_density);
379 emit_2bytes(cinfo, (int) cinfo->Y_density);
380 emit_byte(cinfo, 0); /* No thumbnail image */
386 emit_adobe_app14 (j_compress_ptr cinfo)
387 /* Emit an Adobe APP14 marker */
390 * Length of APP14 block (2 bytes)
391 * Block ID (5 bytes - ASCII "Adobe")
392 * Version Number (2 bytes - currently 100)
393 * Flags0 (2 bytes - currently 0)
394 * Flags1 (2 bytes - currently 0)
395 * Color transform (1 byte)
397 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
398 * now in circulation seem to use Version = 100, so that's what we write.
400 * We write the color transform byte as 1 if the JPEG color space is
401 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
402 * whether the encoder performed a transformation, which is pretty useless.
405 emit_marker(cinfo, M_APP14);
407 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
409 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
410 emit_byte(cinfo, 0x64);
411 emit_byte(cinfo, 0x6F);
412 emit_byte(cinfo, 0x62);
413 emit_byte(cinfo, 0x65);
414 emit_2bytes(cinfo, 100); /* Version */
415 emit_2bytes(cinfo, 0); /* Flags0 */
416 emit_2bytes(cinfo, 0); /* Flags1 */
417 switch (cinfo->jpeg_color_space) {
419 emit_byte(cinfo, 1); /* Color transform = 1 */
422 emit_byte(cinfo, 2); /* Color transform = 2 */
425 emit_byte(cinfo, 0); /* Color transform = 0 */
432 * These routines allow writing an arbitrary marker with parameters.
433 * The only intended use is to emit COM or APPn markers after calling
434 * write_file_header and before calling write_frame_header.
435 * Other uses are not guaranteed to produce desirable results.
436 * Counting the parameter bytes properly is the caller's responsibility.
440 write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
441 /* Emit an arbitrary marker header */
443 if (datalen > (unsigned int) 65533) /* safety check */
444 ERREXIT(cinfo, JERR_BAD_LENGTH);
446 emit_marker(cinfo, (JPEG_MARKER) marker);
448 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
452 write_marker_byte (j_compress_ptr cinfo, int val)
453 /* Emit one byte of marker parameters following write_marker_header */
455 emit_byte(cinfo, val);
460 * Write datastream header.
461 * This consists of an SOI and optional APPn markers.
462 * We recommend use of the JFIF marker, but not the Adobe marker,
463 * when using YCbCr or grayscale data. The JFIF marker should NOT
464 * be used for any other JPEG colorspace. The Adobe marker is helpful
465 * to distinguish RGB, CMYK, and YCCK colorspaces.
466 * Note that an application can write additional header markers after
467 * jpeg_start_compress returns.
471 write_file_header (j_compress_ptr cinfo)
473 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
475 emit_marker(cinfo, M_SOI); /* first the SOI */
477 /* SOI is defined to reset restart interval to 0 */
478 marker->last_restart_interval = 0;
480 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
481 emit_jfif_app0(cinfo);
482 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
483 emit_adobe_app14(cinfo);
488 * Write frame header.
489 * This consists of DQT and SOFn markers.
490 * Note that we do not emit the SOF until we have emitted the DQT(s).
491 * This avoids compatibility problems with incorrect implementations that
492 * try to error-check the quant table numbers as soon as they see the SOF.
496 write_frame_header (j_compress_ptr cinfo)
500 jpeg_component_info *compptr;
503 if (cinfo->process != JPROC_LOSSLESS) {
504 /* Emit DQT for each quantization table.
505 * Note that emit_dqt() suppresses any duplicate tables.
507 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
509 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
511 /* now prec is nonzero iff there are any 16-bit quant tables. */
514 /* Check for a non-baseline specification.
515 * Note we assume that Huffman table numbers won't be changed later.
517 if (cinfo->arith_code || cinfo->process != JPROC_SEQUENTIAL ||
518 cinfo->data_precision != 8) {
522 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
524 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
527 if (prec && is_baseline) {
529 /* If it's baseline except for quantizer size, warn the user */
530 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
534 /* Emit the proper SOF marker */
535 if (cinfo->arith_code) {
536 if (cinfo->process == JPROC_PROGRESSIVE)
537 emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
538 else if (cinfo->process == JPROC_LOSSLESS)
539 emit_sof(cinfo, M_SOF11); /* SOF code for lossless arithmetic */
541 emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */
543 if (cinfo->process == JPROC_PROGRESSIVE)
544 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
545 else if (cinfo->process == JPROC_LOSSLESS)
546 emit_sof(cinfo, M_SOF3); /* SOF code for lossless Huffman */
547 else if (is_baseline)
548 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
550 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
557 * This consists of DHT or DAC markers, optional DRI, and SOS.
558 * Compressed data will be written following the SOS.
562 write_scan_header (j_compress_ptr cinfo)
564 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
566 jpeg_component_info *compptr;
568 if (cinfo->arith_code) {
569 /* Emit arith conditioning info. We may have some duplication
570 * if the file has multiple scans, but it's so small it's hardly
571 * worth worrying about.
575 /* Emit Huffman tables.
576 * Note that emit_dht() suppresses any duplicate tables.
578 for (i = 0; i < cinfo->comps_in_scan; i++) {
579 compptr = cinfo->cur_comp_info[i];
580 if (cinfo->process == JPROC_PROGRESSIVE) {
581 /* Progressive mode: only DC or only AC tables are used in one scan */
582 if (cinfo->Ss == 0) {
583 if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
584 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
586 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
588 } else if (cinfo->process == JPROC_LOSSLESS) {
589 /* Lossless mode: only DC tables are used */
590 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
592 /* Sequential mode: need both DC and AC tables */
593 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
594 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
599 /* Emit DRI if required --- note that DRI value could change for each scan.
600 * We avoid wasting space with unnecessary DRIs, however.
602 if (cinfo->restart_interval != marker->last_restart_interval) {
604 marker->last_restart_interval = cinfo->restart_interval;
612 * Write datastream trailer.
616 write_file_trailer (j_compress_ptr cinfo)
618 emit_marker(cinfo, M_EOI);
623 * Write an abbreviated table-specification datastream.
624 * This consists of SOI, DQT and DHT tables, and EOI.
625 * Any table that is defined and not marked sent_table = TRUE will be
626 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
630 write_tables_only (j_compress_ptr cinfo)
634 emit_marker(cinfo, M_SOI);
636 for (i = 0; i < NUM_QUANT_TBLS; i++) {
637 if (cinfo->quant_tbl_ptrs[i] != NULL)
638 (void) emit_dqt(cinfo, i);
641 if (! cinfo->arith_code) {
642 for (i = 0; i < NUM_HUFF_TBLS; i++) {
643 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
644 emit_dht(cinfo, i, FALSE);
645 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
646 emit_dht(cinfo, i, TRUE);
650 emit_marker(cinfo, M_EOI);
655 * Initialize the marker writer module.
659 jinit_marker_writer (j_compress_ptr cinfo)
661 my_marker_ptr marker;
663 /* Create the subobject */
664 marker = (my_marker_ptr)
665 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
666 SIZEOF(my_marker_writer));
667 cinfo->marker = (struct jpeg_marker_writer *) marker;
668 /* Initialize method pointers */
669 marker->pub.write_file_header = write_file_header;
670 marker->pub.write_frame_header = write_frame_header;
671 marker->pub.write_scan_header = write_scan_header;
672 marker->pub.write_file_trailer = write_file_trailer;
673 marker->pub.write_tables_only = write_tables_only;
674 marker->pub.write_marker_header = write_marker_header;
675 marker->pub.write_marker_byte = write_marker_byte;
676 /* Initialize private state */
677 marker->last_restart_interval = 0;