4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1998, Thomas G. Lane.
6 * Modified 2003-2010 by Guido Vollbeding.
7 * Lossless JPEG Modifications:
8 * Copyright (C) 1999, Ken Murchison.
9 * libjpeg-turbo Modifications:
10 * Copyright (C) 2010, 2022, D. R. Commander.
11 * For conditions of distribution and use, see the accompanying README.ijg
14 * This file contains routines to write JPEG datastream markers.
17 #define JPEG_INTERNALS
20 #include "jpegapicomp.h"
23 typedef enum { /* JPEG marker codes */
94 struct jpeg_marker_writer pub; /* public fields */
96 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
99 typedef my_marker_writer *my_marker_ptr;
103 * Basic output routines.
105 * Note that we do not support suspension while writing a marker.
106 * Therefore, an application using suspension must ensure that there is
107 * enough buffer space for the initial markers (typ. 600-700 bytes) before
108 * calling jpeg_start_compress, and enough space to write the trailing EOI
109 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
110 * modes are not supported at all with suspension, so those two are the only
111 * points where markers will be written.
115 emit_byte(j_compress_ptr cinfo, int val)
118 struct jpeg_destination_mgr *dest = cinfo->dest;
120 *(dest->next_output_byte)++ = (JOCTET)val;
121 if (--dest->free_in_buffer == 0) {
122 if (!(*dest->empty_output_buffer) (cinfo))
123 ERREXIT(cinfo, JERR_CANT_SUSPEND);
129 emit_marker(j_compress_ptr cinfo, JPEG_MARKER mark)
130 /* Emit a marker code */
132 emit_byte(cinfo, 0xFF);
133 emit_byte(cinfo, (int)mark);
138 emit_2bytes(j_compress_ptr cinfo, int value)
139 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
141 emit_byte(cinfo, (value >> 8) & 0xFF);
142 emit_byte(cinfo, value & 0xFF);
147 * Routines to write specific marker types.
151 emit_dqt(j_compress_ptr cinfo, int index)
152 /* Emit a DQT marker */
153 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
155 JQUANT_TBL *qtbl = cinfo->quant_tbl_ptrs[index];
160 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
163 for (i = 0; i < DCTSIZE2; i++) {
164 if (qtbl->quantval[i] > 255)
168 if (!qtbl->sent_table) {
169 emit_marker(cinfo, M_DQT);
171 emit_2bytes(cinfo, prec ? DCTSIZE2 * 2 + 1 + 2 : DCTSIZE2 + 1 + 2);
173 emit_byte(cinfo, index + (prec << 4));
175 for (i = 0; i < DCTSIZE2; i++) {
176 /* The table entries must be emitted in zigzag order. */
177 unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
179 emit_byte(cinfo, (int)(qval >> 8));
180 emit_byte(cinfo, (int)(qval & 0xFF));
183 qtbl->sent_table = TRUE;
191 emit_dht(j_compress_ptr cinfo, int index, boolean is_ac)
192 /* Emit a DHT marker */
198 htbl = cinfo->ac_huff_tbl_ptrs[index];
199 index += 0x10; /* output index has AC bit set */
201 htbl = cinfo->dc_huff_tbl_ptrs[index];
205 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
207 if (!htbl->sent_table) {
208 emit_marker(cinfo, M_DHT);
211 for (i = 1; i <= 16; i++)
212 length += htbl->bits[i];
214 emit_2bytes(cinfo, length + 2 + 1 + 16);
215 emit_byte(cinfo, index);
217 for (i = 1; i <= 16; i++)
218 emit_byte(cinfo, htbl->bits[i]);
220 for (i = 0; i < length; i++)
221 emit_byte(cinfo, htbl->huffval[i]);
223 htbl->sent_table = TRUE;
229 emit_dac(j_compress_ptr cinfo)
230 /* Emit a DAC marker */
231 /* Since the useful info is so small, we want to emit all the tables in */
232 /* one DAC marker. Therefore this routine does its own scan of the table. */
234 #ifdef C_ARITH_CODING_SUPPORTED
235 char dc_in_use[NUM_ARITH_TBLS];
236 char ac_in_use[NUM_ARITH_TBLS];
238 jpeg_component_info *compptr;
240 for (i = 0; i < NUM_ARITH_TBLS; i++)
241 dc_in_use[i] = ac_in_use[i] = 0;
243 for (i = 0; i < cinfo->comps_in_scan; i++) {
244 compptr = cinfo->cur_comp_info[i];
245 /* DC needs no table for refinement scan */
246 if (cinfo->Ss == 0 && cinfo->Ah == 0)
247 dc_in_use[compptr->dc_tbl_no] = 1;
248 /* AC needs no table when not present */
250 ac_in_use[compptr->ac_tbl_no] = 1;
254 for (i = 0; i < NUM_ARITH_TBLS; i++)
255 length += dc_in_use[i] + ac_in_use[i];
258 emit_marker(cinfo, M_DAC);
260 emit_2bytes(cinfo, length * 2 + 2);
262 for (i = 0; i < NUM_ARITH_TBLS; i++) {
265 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i] << 4));
268 emit_byte(cinfo, i + 0x10);
269 emit_byte(cinfo, cinfo->arith_ac_K[i]);
273 #endif /* C_ARITH_CODING_SUPPORTED */
278 emit_dri(j_compress_ptr cinfo)
279 /* Emit a DRI marker */
281 emit_marker(cinfo, M_DRI);
283 emit_2bytes(cinfo, 4); /* fixed length */
285 emit_2bytes(cinfo, (int)cinfo->restart_interval);
290 emit_sof(j_compress_ptr cinfo, JPEG_MARKER code)
291 /* Emit a SOF marker */
294 jpeg_component_info *compptr;
296 emit_marker(cinfo, code);
298 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
300 /* Make sure image isn't bigger than SOF field can handle */
301 if ((long)cinfo->_jpeg_height > 65535L || (long)cinfo->_jpeg_width > 65535L)
302 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)65535);
304 emit_byte(cinfo, cinfo->data_precision);
305 emit_2bytes(cinfo, (int)cinfo->_jpeg_height);
306 emit_2bytes(cinfo, (int)cinfo->_jpeg_width);
308 emit_byte(cinfo, cinfo->num_components);
310 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
312 emit_byte(cinfo, compptr->component_id);
313 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
314 emit_byte(cinfo, compptr->quant_tbl_no);
320 emit_sos(j_compress_ptr cinfo)
321 /* Emit a SOS marker */
324 jpeg_component_info *compptr;
326 emit_marker(cinfo, M_SOS);
328 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
330 emit_byte(cinfo, cinfo->comps_in_scan);
332 for (i = 0; i < cinfo->comps_in_scan; i++) {
333 compptr = cinfo->cur_comp_info[i];
334 emit_byte(cinfo, compptr->component_id);
336 /* We emit 0 for unused field(s); this is recommended by the P&M text
337 * but does not seem to be specified in the standard.
340 /* DC needs no table for refinement scan */
341 td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
342 /* AC needs no table when not present */
343 ta = cinfo->Se ? compptr->ac_tbl_no : 0;
345 emit_byte(cinfo, (td << 4) + ta);
348 emit_byte(cinfo, cinfo->Ss);
349 emit_byte(cinfo, cinfo->Se);
350 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
355 emit_jfif_app0(j_compress_ptr cinfo)
356 /* Emit a JFIF-compliant APP0 marker */
359 * Length of APP0 block (2 bytes)
360 * Block ID (4 bytes - ASCII "JFIF")
361 * Zero byte (1 byte to terminate the ID string)
362 * Version Major, Minor (2 bytes - major first)
363 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
364 * Xdpu (2 bytes - dots per unit horizontal)
365 * Ydpu (2 bytes - dots per unit vertical)
366 * Thumbnail X size (1 byte)
367 * Thumbnail Y size (1 byte)
370 emit_marker(cinfo, M_APP0);
372 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
374 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
375 emit_byte(cinfo, 0x46);
376 emit_byte(cinfo, 0x49);
377 emit_byte(cinfo, 0x46);
379 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
380 emit_byte(cinfo, cinfo->JFIF_minor_version);
381 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
382 emit_2bytes(cinfo, (int)cinfo->X_density);
383 emit_2bytes(cinfo, (int)cinfo->Y_density);
384 emit_byte(cinfo, 0); /* No thumbnail image */
390 emit_adobe_app14(j_compress_ptr cinfo)
391 /* Emit an Adobe APP14 marker */
394 * Length of APP14 block (2 bytes)
395 * Block ID (5 bytes - ASCII "Adobe")
396 * Version Number (2 bytes - currently 100)
397 * Flags0 (2 bytes - currently 0)
398 * Flags1 (2 bytes - currently 0)
399 * Color transform (1 byte)
401 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
402 * now in circulation seem to use Version = 100, so that's what we write.
404 * We write the color transform byte as 1 if the JPEG color space is
405 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
406 * whether the encoder performed a transformation, which is pretty useless.
409 emit_marker(cinfo, M_APP14);
411 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
413 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
414 emit_byte(cinfo, 0x64);
415 emit_byte(cinfo, 0x6F);
416 emit_byte(cinfo, 0x62);
417 emit_byte(cinfo, 0x65);
418 emit_2bytes(cinfo, 100); /* Version */
419 emit_2bytes(cinfo, 0); /* Flags0 */
420 emit_2bytes(cinfo, 0); /* Flags1 */
421 switch (cinfo->jpeg_color_space) {
423 emit_byte(cinfo, 1); /* Color transform = 1 */
426 emit_byte(cinfo, 2); /* Color transform = 2 */
429 emit_byte(cinfo, 0); /* Color transform = 0 */
436 * These routines allow writing an arbitrary marker with parameters.
437 * The only intended use is to emit COM or APPn markers after calling
438 * write_file_header and before calling write_frame_header.
439 * Other uses are not guaranteed to produce desirable results.
440 * Counting the parameter bytes properly is the caller's responsibility.
444 write_marker_header(j_compress_ptr cinfo, int marker, unsigned int datalen)
445 /* Emit an arbitrary marker header */
447 if (datalen > (unsigned int)65533) /* safety check */
448 ERREXIT(cinfo, JERR_BAD_LENGTH);
450 emit_marker(cinfo, (JPEG_MARKER)marker);
452 emit_2bytes(cinfo, (int)(datalen + 2)); /* total length */
456 write_marker_byte(j_compress_ptr cinfo, int val)
457 /* Emit one byte of marker parameters following write_marker_header */
459 emit_byte(cinfo, val);
464 * Write datastream header.
465 * This consists of an SOI and optional APPn markers.
466 * We recommend use of the JFIF marker, but not the Adobe marker,
467 * when using YCbCr or grayscale data. The JFIF marker should NOT
468 * be used for any other JPEG colorspace. The Adobe marker is helpful
469 * to distinguish RGB, CMYK, and YCCK colorspaces.
470 * Note that an application can write additional header markers after
471 * jpeg_start_compress returns.
475 write_file_header(j_compress_ptr cinfo)
477 my_marker_ptr marker = (my_marker_ptr)cinfo->marker;
479 emit_marker(cinfo, M_SOI); /* first the SOI */
481 /* SOI is defined to reset restart interval to 0 */
482 marker->last_restart_interval = 0;
484 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
485 emit_jfif_app0(cinfo);
486 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
487 emit_adobe_app14(cinfo);
492 * Write frame header.
493 * This consists of DQT and SOFn markers.
494 * Note that we do not emit the SOF until we have emitted the DQT(s).
495 * This avoids compatibility problems with incorrect implementations that
496 * try to error-check the quant table numbers as soon as they see the SOF.
500 write_frame_header(j_compress_ptr cinfo)
504 jpeg_component_info *compptr;
506 if (!cinfo->master->lossless) {
507 /* Emit DQT for each quantization table.
508 * Note that emit_dqt() suppresses any duplicate tables.
510 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
512 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
514 /* now prec is nonzero iff there are any 16-bit quant tables. */
517 /* Check for a non-baseline specification.
518 * Note we assume that Huffman table numbers won't be changed later.
520 if (cinfo->arith_code || cinfo->progressive_mode ||
521 cinfo->master->lossless || cinfo->data_precision != 8) {
525 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
527 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
530 if (prec && is_baseline) {
532 /* If it's baseline except for quantizer size, warn the user */
533 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
537 /* Emit the proper SOF marker */
538 if (cinfo->arith_code) {
539 if (cinfo->progressive_mode)
540 emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
542 emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */
544 if (cinfo->progressive_mode)
545 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
546 else if (cinfo->master->lossless)
547 emit_sof(cinfo, M_SOF3); /* SOF code for lossless Huffman */
548 else if (is_baseline)
549 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
551 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
558 * This consists of DHT or DAC markers, optional DRI, and SOS.
559 * Compressed data will be written following the SOS.
563 write_scan_header(j_compress_ptr cinfo)
565 my_marker_ptr marker = (my_marker_ptr)cinfo->marker;
567 jpeg_component_info *compptr;
569 if (cinfo->arith_code) {
570 /* Emit arith conditioning info. We may have some duplication
571 * if the file has multiple scans, but it's so small it's hardly
572 * worth worrying about.
576 /* Emit Huffman tables.
577 * Note that emit_dht() suppresses any duplicate tables.
579 for (i = 0; i < cinfo->comps_in_scan; i++) {
580 compptr = cinfo->cur_comp_info[i];
581 /* DC needs no table for refinement scan */
582 if ((cinfo->Ss == 0 && cinfo->Ah == 0) || cinfo->master->lossless)
583 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
584 /* AC needs no table when not present, and lossless mode uses only DC
586 if (cinfo->Se && !cinfo->master->lossless)
587 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
591 /* Emit DRI if required --- note that DRI value could change for each scan.
592 * We avoid wasting space with unnecessary DRIs, however.
594 if (cinfo->restart_interval != marker->last_restart_interval) {
596 marker->last_restart_interval = cinfo->restart_interval;
604 * Write datastream trailer.
608 write_file_trailer(j_compress_ptr cinfo)
610 emit_marker(cinfo, M_EOI);
615 * Write an abbreviated table-specification datastream.
616 * This consists of SOI, DQT and DHT tables, and EOI.
617 * Any table that is defined and not marked sent_table = TRUE will be
618 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
622 write_tables_only(j_compress_ptr cinfo)
626 emit_marker(cinfo, M_SOI);
628 for (i = 0; i < NUM_QUANT_TBLS; i++) {
629 if (cinfo->quant_tbl_ptrs[i] != NULL)
630 (void)emit_dqt(cinfo, i);
633 if (!cinfo->arith_code) {
634 for (i = 0; i < NUM_HUFF_TBLS; i++) {
635 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
636 emit_dht(cinfo, i, FALSE);
637 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
638 emit_dht(cinfo, i, TRUE);
642 emit_marker(cinfo, M_EOI);
647 * Initialize the marker writer module.
651 jinit_marker_writer(j_compress_ptr cinfo)
653 my_marker_ptr marker;
655 /* Create the subobject */
656 marker = (my_marker_ptr)
657 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
658 sizeof(my_marker_writer));
659 cinfo->marker = (struct jpeg_marker_writer *)marker;
660 /* Initialize method pointers */
661 marker->pub.write_file_header = write_file_header;
662 marker->pub.write_frame_header = write_frame_header;
663 marker->pub.write_scan_header = write_scan_header;
664 marker->pub.write_file_trailer = write_file_trailer;
665 marker->pub.write_tables_only = write_tables_only;
666 marker->pub.write_marker_header = write_marker_header;
667 marker->pub.write_marker_byte = write_marker_byte;
668 /* Initialize private state */
669 marker->last_restart_interval = 0;