4 * Copyright (C) 1991-1998, Thomas G. Lane.
5 * Modified 2003-2012 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
9 * This file contains routines to write JPEG datastream markers.
12 #define JPEG_INTERNALS
17 typedef enum { /* JPEG marker codes */
89 struct jpeg_marker_writer pub; /* public fields */
91 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
94 typedef my_marker_writer * my_marker_ptr;
98 * Basic output routines.
100 * Note that we do not support suspension while writing a marker.
101 * Therefore, an application using suspension must ensure that there is
102 * enough buffer space for the initial markers (typ. 600-700 bytes) before
103 * calling jpeg_start_compress, and enough space to write the trailing EOI
104 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
105 * modes are not supported at all with suspension, so those two are the only
106 * points where markers will be written.
110 emit_byte (j_compress_ptr cinfo, int val)
113 struct jpeg_destination_mgr * dest = cinfo->dest;
115 *(dest->next_output_byte)++ = (JOCTET) val;
116 if (--dest->free_in_buffer == 0) {
117 if (! (*dest->empty_output_buffer) (cinfo))
118 ERREXIT(cinfo, JERR_CANT_SUSPEND);
124 emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
125 /* Emit a marker code */
127 emit_byte(cinfo, 0xFF);
128 emit_byte(cinfo, (int) mark);
133 emit_2bytes (j_compress_ptr cinfo, int value)
134 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
136 emit_byte(cinfo, (value >> 8) & 0xFF);
137 emit_byte(cinfo, value & 0xFF);
142 * Routines to write specific marker types.
146 emit_dqt (j_compress_ptr cinfo, int index)
147 /* Emit a DQT marker */
148 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
150 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
155 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
158 for (i = 0; i <= cinfo->lim_Se; i++) {
159 if (qtbl->quantval[cinfo->natural_order[i]] > 255)
163 if (! qtbl->sent_table) {
164 emit_marker(cinfo, M_DQT);
167 prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2);
169 emit_byte(cinfo, index + (prec<<4));
171 for (i = 0; i <= cinfo->lim_Se; i++) {
172 /* The table entries must be emitted in zigzag order. */
173 unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
175 emit_byte(cinfo, (int) (qval >> 8));
176 emit_byte(cinfo, (int) (qval & 0xFF));
179 qtbl->sent_table = TRUE;
187 emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
188 /* Emit a DHT marker */
194 htbl = cinfo->ac_huff_tbl_ptrs[index];
195 index += 0x10; /* output index has AC bit set */
197 htbl = cinfo->dc_huff_tbl_ptrs[index];
201 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
203 if (! htbl->sent_table) {
204 emit_marker(cinfo, M_DHT);
207 for (i = 1; i <= 16; i++)
208 length += htbl->bits[i];
210 emit_2bytes(cinfo, length + 2 + 1 + 16);
211 emit_byte(cinfo, index);
213 for (i = 1; i <= 16; i++)
214 emit_byte(cinfo, htbl->bits[i]);
216 for (i = 0; i < length; i++)
217 emit_byte(cinfo, htbl->huffval[i]);
219 htbl->sent_table = TRUE;
225 emit_dac (j_compress_ptr cinfo)
226 /* Emit a DAC marker */
227 /* Since the useful info is so small, we want to emit all the tables in */
228 /* one DAC marker. Therefore this routine does its own scan of the table. */
230 #ifdef C_ARITH_CODING_SUPPORTED
231 char dc_in_use[NUM_ARITH_TBLS];
232 char ac_in_use[NUM_ARITH_TBLS];
234 jpeg_component_info *compptr;
236 for (i = 0; i < NUM_ARITH_TBLS; i++)
237 dc_in_use[i] = ac_in_use[i] = 0;
239 for (i = 0; i < cinfo->comps_in_scan; i++) {
240 compptr = cinfo->cur_comp_info[i];
241 /* DC needs no table for refinement scan */
242 if (cinfo->Ss == 0 && cinfo->Ah == 0)
243 dc_in_use[compptr->dc_tbl_no] = 1;
244 /* AC needs no table when not present */
246 ac_in_use[compptr->ac_tbl_no] = 1;
250 for (i = 0; i < NUM_ARITH_TBLS; i++)
251 length += dc_in_use[i] + ac_in_use[i];
254 emit_marker(cinfo, M_DAC);
256 emit_2bytes(cinfo, length*2 + 2);
258 for (i = 0; i < NUM_ARITH_TBLS; i++) {
261 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
264 emit_byte(cinfo, i + 0x10);
265 emit_byte(cinfo, cinfo->arith_ac_K[i]);
269 #endif /* C_ARITH_CODING_SUPPORTED */
274 emit_dri (j_compress_ptr cinfo)
275 /* Emit a DRI marker */
277 emit_marker(cinfo, M_DRI);
279 emit_2bytes(cinfo, 4); /* fixed length */
281 emit_2bytes(cinfo, (int) cinfo->restart_interval);
286 emit_lse_ict (j_compress_ptr cinfo)
287 /* Emit an LSE inverse color transform specification marker */
289 /* Support only 1 transform */
290 if (cinfo->color_transform != JCT_SUBTRACT_GREEN ||
291 cinfo->num_components < 3)
292 ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
294 emit_marker(cinfo, M_JPG8);
296 emit_2bytes(cinfo, 24); /* fixed length */
298 emit_byte(cinfo, 0x0D); /* ID inverse transform specification */
299 emit_2bytes(cinfo, MAXJSAMPLE); /* MAXTRANS */
300 emit_byte(cinfo, 3); /* Nt=3 */
301 emit_byte(cinfo, cinfo->comp_info[1].component_id);
302 emit_byte(cinfo, cinfo->comp_info[0].component_id);
303 emit_byte(cinfo, cinfo->comp_info[2].component_id);
304 emit_byte(cinfo, 0x80); /* F1: CENTER1=1, NORM1=0 */
305 emit_2bytes(cinfo, 0); /* A(1,1)=0 */
306 emit_2bytes(cinfo, 0); /* A(1,2)=0 */
307 emit_byte(cinfo, 0); /* F2: CENTER2=0, NORM2=0 */
308 emit_2bytes(cinfo, 1); /* A(2,1)=1 */
309 emit_2bytes(cinfo, 0); /* A(2,2)=0 */
310 emit_byte(cinfo, 0); /* F3: CENTER3=0, NORM3=0 */
311 emit_2bytes(cinfo, 1); /* A(3,1)=1 */
312 emit_2bytes(cinfo, 0); /* A(3,2)=0 */
317 emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
318 /* Emit a SOF marker */
321 jpeg_component_info *compptr;
323 emit_marker(cinfo, code);
325 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
327 /* Make sure image isn't bigger than SOF field can handle */
328 if ((long) cinfo->jpeg_height > 65535L ||
329 (long) cinfo->jpeg_width > 65535L)
330 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
332 emit_byte(cinfo, cinfo->data_precision);
333 emit_2bytes(cinfo, (int) cinfo->jpeg_height);
334 emit_2bytes(cinfo, (int) cinfo->jpeg_width);
336 emit_byte(cinfo, cinfo->num_components);
338 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
340 emit_byte(cinfo, compptr->component_id);
341 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
342 emit_byte(cinfo, compptr->quant_tbl_no);
348 emit_sos (j_compress_ptr cinfo)
349 /* Emit a SOS marker */
352 jpeg_component_info *compptr;
354 emit_marker(cinfo, M_SOS);
356 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
358 emit_byte(cinfo, cinfo->comps_in_scan);
360 for (i = 0; i < cinfo->comps_in_scan; i++) {
361 compptr = cinfo->cur_comp_info[i];
362 emit_byte(cinfo, compptr->component_id);
364 /* We emit 0 for unused field(s); this is recommended by the P&M text
365 * but does not seem to be specified in the standard.
368 /* DC needs no table for refinement scan */
369 td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
370 /* AC needs no table when not present */
371 ta = cinfo->Se ? compptr->ac_tbl_no : 0;
373 emit_byte(cinfo, (td << 4) + ta);
376 emit_byte(cinfo, cinfo->Ss);
377 emit_byte(cinfo, cinfo->Se);
378 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
383 emit_pseudo_sos (j_compress_ptr cinfo)
384 /* Emit a pseudo SOS marker */
386 emit_marker(cinfo, M_SOS);
388 emit_2bytes(cinfo, 2 + 1 + 3); /* length */
390 emit_byte(cinfo, 0); /* Ns */
392 emit_byte(cinfo, 0); /* Ss */
393 emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1); /* Se */
394 emit_byte(cinfo, 0); /* Ah/Al */
399 emit_jfif_app0 (j_compress_ptr cinfo)
400 /* Emit a JFIF-compliant APP0 marker */
403 * Length of APP0 block (2 bytes)
404 * Block ID (4 bytes - ASCII "JFIF")
405 * Zero byte (1 byte to terminate the ID string)
406 * Version Major, Minor (2 bytes - major first)
407 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
408 * Xdpu (2 bytes - dots per unit horizontal)
409 * Ydpu (2 bytes - dots per unit vertical)
410 * Thumbnail X size (1 byte)
411 * Thumbnail Y size (1 byte)
414 emit_marker(cinfo, M_APP0);
416 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
418 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
419 emit_byte(cinfo, 0x46);
420 emit_byte(cinfo, 0x49);
421 emit_byte(cinfo, 0x46);
423 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
424 emit_byte(cinfo, cinfo->JFIF_minor_version);
425 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
426 emit_2bytes(cinfo, (int) cinfo->X_density);
427 emit_2bytes(cinfo, (int) cinfo->Y_density);
428 emit_byte(cinfo, 0); /* No thumbnail image */
434 emit_adobe_app14 (j_compress_ptr cinfo)
435 /* Emit an Adobe APP14 marker */
438 * Length of APP14 block (2 bytes)
439 * Block ID (5 bytes - ASCII "Adobe")
440 * Version Number (2 bytes - currently 100)
441 * Flags0 (2 bytes - currently 0)
442 * Flags1 (2 bytes - currently 0)
443 * Color transform (1 byte)
445 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
446 * now in circulation seem to use Version = 100, so that's what we write.
448 * We write the color transform byte as 1 if the JPEG color space is
449 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
450 * whether the encoder performed a transformation, which is pretty useless.
453 emit_marker(cinfo, M_APP14);
455 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
457 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
458 emit_byte(cinfo, 0x64);
459 emit_byte(cinfo, 0x6F);
460 emit_byte(cinfo, 0x62);
461 emit_byte(cinfo, 0x65);
462 emit_2bytes(cinfo, 100); /* Version */
463 emit_2bytes(cinfo, 0); /* Flags0 */
464 emit_2bytes(cinfo, 0); /* Flags1 */
465 switch (cinfo->jpeg_color_space) {
467 emit_byte(cinfo, 1); /* Color transform = 1 */
470 emit_byte(cinfo, 2); /* Color transform = 2 */
473 emit_byte(cinfo, 0); /* Color transform = 0 */
480 * These routines allow writing an arbitrary marker with parameters.
481 * The only intended use is to emit COM or APPn markers after calling
482 * write_file_header and before calling write_frame_header.
483 * Other uses are not guaranteed to produce desirable results.
484 * Counting the parameter bytes properly is the caller's responsibility.
488 write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
489 /* Emit an arbitrary marker header */
491 if (datalen > (unsigned int) 65533) /* safety check */
492 ERREXIT(cinfo, JERR_BAD_LENGTH);
494 emit_marker(cinfo, (JPEG_MARKER) marker);
496 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
500 write_marker_byte (j_compress_ptr cinfo, int val)
501 /* Emit one byte of marker parameters following write_marker_header */
503 emit_byte(cinfo, val);
508 * Write datastream header.
509 * This consists of an SOI and optional APPn markers.
510 * We recommend use of the JFIF marker, but not the Adobe marker,
511 * when using YCbCr or grayscale data. The JFIF marker should NOT
512 * be used for any other JPEG colorspace. The Adobe marker is helpful
513 * to distinguish RGB, CMYK, and YCCK colorspaces.
514 * Note that an application can write additional header markers after
515 * jpeg_start_compress returns.
519 write_file_header (j_compress_ptr cinfo)
521 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
523 emit_marker(cinfo, M_SOI); /* first the SOI */
525 /* SOI is defined to reset restart interval to 0 */
526 marker->last_restart_interval = 0;
528 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
529 emit_jfif_app0(cinfo);
530 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
531 emit_adobe_app14(cinfo);
536 * Write frame header.
537 * This consists of DQT and SOFn markers,
538 * a conditional LSE marker and a conditional pseudo SOS marker.
539 * Note that we do not emit the SOF until we have emitted the DQT(s).
540 * This avoids compatibility problems with incorrect implementations that
541 * try to error-check the quant table numbers as soon as they see the SOF.
545 write_frame_header (j_compress_ptr cinfo)
549 jpeg_component_info *compptr;
551 /* Emit DQT for each quantization table.
552 * Note that emit_dqt() suppresses any duplicate tables.
555 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
557 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
559 /* now prec is nonzero iff there are any 16-bit quant tables. */
561 /* Check for a non-baseline specification.
562 * Note we assume that Huffman table numbers won't be changed later.
564 if (cinfo->arith_code || cinfo->progressive_mode ||
565 cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) {
569 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
571 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
574 if (prec && is_baseline) {
576 /* If it's baseline except for quantizer size, warn the user */
577 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
581 /* Emit the proper SOF marker */
582 if (cinfo->arith_code) {
583 if (cinfo->progressive_mode)
584 emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
586 emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */
588 if (cinfo->progressive_mode)
589 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
590 else if (is_baseline)
591 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
593 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
596 /* Check to emit LSE inverse color transform specification marker */
597 if (cinfo->color_transform)
600 /* Check to emit pseudo SOS marker */
601 if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE)
602 emit_pseudo_sos(cinfo);
608 * This consists of DHT or DAC markers, optional DRI, and SOS.
609 * Compressed data will be written following the SOS.
613 write_scan_header (j_compress_ptr cinfo)
615 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
617 jpeg_component_info *compptr;
619 if (cinfo->arith_code) {
620 /* Emit arith conditioning info. We may have some duplication
621 * if the file has multiple scans, but it's so small it's hardly
622 * worth worrying about.
626 /* Emit Huffman tables.
627 * Note that emit_dht() suppresses any duplicate tables.
629 for (i = 0; i < cinfo->comps_in_scan; i++) {
630 compptr = cinfo->cur_comp_info[i];
631 /* DC needs no table for refinement scan */
632 if (cinfo->Ss == 0 && cinfo->Ah == 0)
633 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
634 /* AC needs no table when not present */
636 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
640 /* Emit DRI if required --- note that DRI value could change for each scan.
641 * We avoid wasting space with unnecessary DRIs, however.
643 if (cinfo->restart_interval != marker->last_restart_interval) {
645 marker->last_restart_interval = cinfo->restart_interval;
653 * Write datastream trailer.
657 write_file_trailer (j_compress_ptr cinfo)
659 emit_marker(cinfo, M_EOI);
664 * Write an abbreviated table-specification datastream.
665 * This consists of SOI, DQT and DHT tables, and EOI.
666 * Any table that is defined and not marked sent_table = TRUE will be
667 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
671 write_tables_only (j_compress_ptr cinfo)
675 emit_marker(cinfo, M_SOI);
677 for (i = 0; i < NUM_QUANT_TBLS; i++) {
678 if (cinfo->quant_tbl_ptrs[i] != NULL)
679 (void) emit_dqt(cinfo, i);
682 if (! cinfo->arith_code) {
683 for (i = 0; i < NUM_HUFF_TBLS; i++) {
684 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
685 emit_dht(cinfo, i, FALSE);
686 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
687 emit_dht(cinfo, i, TRUE);
691 emit_marker(cinfo, M_EOI);
696 * Initialize the marker writer module.
700 jinit_marker_writer (j_compress_ptr cinfo)
702 my_marker_ptr marker;
704 /* Create the subobject */
705 marker = (my_marker_ptr)
706 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
707 SIZEOF(my_marker_writer));
708 cinfo->marker = &marker->pub;
709 /* Initialize method pointers */
710 marker->pub.write_file_header = write_file_header;
711 marker->pub.write_frame_header = write_frame_header;
712 marker->pub.write_scan_header = write_scan_header;
713 marker->pub.write_file_trailer = write_file_trailer;
714 marker->pub.write_tables_only = write_tables_only;
715 marker->pub.write_marker_header = write_marker_header;
716 marker->pub.write_marker_byte = write_marker_byte;
717 /* Initialize private state */
718 marker->last_restart_interval = 0;