4 * Copyright (C) 1991-1998, Thomas G. Lane.
5 * Modified 2003-2010 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 */
88 struct jpeg_marker_writer pub
; /* public fields */
90 unsigned int last_restart_interval
; /* last DRI value emitted; 0 after SOI */
93 typedef my_marker_writer
* my_marker_ptr
;
97 * Basic output routines.
99 * Note that we do not support suspension while writing a marker.
100 * Therefore, an application using suspension must ensure that there is
101 * enough buffer space for the initial markers (typ. 600-700 bytes) before
102 * calling jpeg_start_compress, and enough space to write the trailing EOI
103 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
104 * modes are not supported at all with suspension, so those two are the only
105 * points where markers will be written.
109 emit_byte (j_compress_ptr cinfo
, int val
)
112 struct jpeg_destination_mgr
* dest
= cinfo
->dest
;
114 *(dest
->next_output_byte
)++ = (JOCTET
) val
;
115 if (--dest
->free_in_buffer
== 0) {
116 if (! (*dest
->empty_output_buffer
) (cinfo
))
117 ERREXIT(cinfo
, JERR_CANT_SUSPEND
);
123 emit_marker (j_compress_ptr cinfo
, JPEG_MARKER mark
)
124 /* Emit a marker code */
126 emit_byte(cinfo
, 0xFF);
127 emit_byte(cinfo
, (int) mark
);
132 emit_2bytes (j_compress_ptr cinfo
, int value
)
133 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
135 emit_byte(cinfo
, (value
>> 8) & 0xFF);
136 emit_byte(cinfo
, value
& 0xFF);
141 * Routines to write specific marker types.
145 emit_dqt (j_compress_ptr cinfo
, int index
)
146 /* Emit a DQT marker */
147 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
149 JQUANT_TBL
* qtbl
= cinfo
->quant_tbl_ptrs
[index
];
154 ERREXIT1(cinfo
, JERR_NO_QUANT_TABLE
, index
);
157 for (i
= 0; i
<= cinfo
->lim_Se
; i
++) {
158 if (qtbl
->quantval
[cinfo
->natural_order
[i
]] > 255)
162 if (! qtbl
->sent_table
) {
163 emit_marker(cinfo
, M_DQT
);
166 prec
? cinfo
->lim_Se
* 2 + 2 + 1 + 2 : cinfo
->lim_Se
+ 1 + 1 + 2);
168 emit_byte(cinfo
, index
+ (prec
<<4));
170 for (i
= 0; i
<= cinfo
->lim_Se
; i
++) {
171 /* The table entries must be emitted in zigzag order. */
172 unsigned int qval
= qtbl
->quantval
[cinfo
->natural_order
[i
]];
174 emit_byte(cinfo
, (int) (qval
>> 8));
175 emit_byte(cinfo
, (int) (qval
& 0xFF));
178 qtbl
->sent_table
= TRUE
;
186 emit_dht (j_compress_ptr cinfo
, int index
, boolean is_ac
)
187 /* Emit a DHT marker */
193 htbl
= cinfo
->ac_huff_tbl_ptrs
[index
];
194 index
+= 0x10; /* output index has AC bit set */
196 htbl
= cinfo
->dc_huff_tbl_ptrs
[index
];
200 ERREXIT1(cinfo
, JERR_NO_HUFF_TABLE
, index
);
202 if (! htbl
->sent_table
) {
203 emit_marker(cinfo
, M_DHT
);
206 for (i
= 1; i
<= 16; i
++)
207 length
+= htbl
->bits
[i
];
209 emit_2bytes(cinfo
, length
+ 2 + 1 + 16);
210 emit_byte(cinfo
, index
);
212 for (i
= 1; i
<= 16; i
++)
213 emit_byte(cinfo
, htbl
->bits
[i
]);
215 for (i
= 0; i
< length
; i
++)
216 emit_byte(cinfo
, htbl
->huffval
[i
]);
218 htbl
->sent_table
= TRUE
;
224 emit_dac (j_compress_ptr cinfo
)
225 /* Emit a DAC marker */
226 /* Since the useful info is so small, we want to emit all the tables in */
227 /* one DAC marker. Therefore this routine does its own scan of the table. */
229 #ifdef C_ARITH_CODING_SUPPORTED
230 char dc_in_use
[NUM_ARITH_TBLS
];
231 char ac_in_use
[NUM_ARITH_TBLS
];
233 jpeg_component_info
*compptr
;
235 for (i
= 0; i
< NUM_ARITH_TBLS
; i
++)
236 dc_in_use
[i
] = ac_in_use
[i
] = 0;
238 for (i
= 0; i
< cinfo
->comps_in_scan
; i
++) {
239 compptr
= cinfo
->cur_comp_info
[i
];
240 /* DC needs no table for refinement scan */
241 if (cinfo
->Ss
== 0 && cinfo
->Ah
== 0)
242 dc_in_use
[compptr
->dc_tbl_no
] = 1;
243 /* AC needs no table when not present */
245 ac_in_use
[compptr
->ac_tbl_no
] = 1;
249 for (i
= 0; i
< NUM_ARITH_TBLS
; i
++)
250 length
+= dc_in_use
[i
] + ac_in_use
[i
];
253 emit_marker(cinfo
, M_DAC
);
255 emit_2bytes(cinfo
, length
*2 + 2);
257 for (i
= 0; i
< NUM_ARITH_TBLS
; i
++) {
260 emit_byte(cinfo
, cinfo
->arith_dc_L
[i
] + (cinfo
->arith_dc_U
[i
]<<4));
263 emit_byte(cinfo
, i
+ 0x10);
264 emit_byte(cinfo
, cinfo
->arith_ac_K
[i
]);
268 #endif /* C_ARITH_CODING_SUPPORTED */
273 emit_dri (j_compress_ptr cinfo
)
274 /* Emit a DRI marker */
276 emit_marker(cinfo
, M_DRI
);
278 emit_2bytes(cinfo
, 4); /* fixed length */
280 emit_2bytes(cinfo
, (int) cinfo
->restart_interval
);
285 emit_sof (j_compress_ptr cinfo
, JPEG_MARKER code
)
286 /* Emit a SOF marker */
289 jpeg_component_info
*compptr
;
291 emit_marker(cinfo
, code
);
293 emit_2bytes(cinfo
, 3 * cinfo
->num_components
+ 2 + 5 + 1); /* length */
295 /* Make sure image isn't bigger than SOF field can handle */
296 if ((long) cinfo
->jpeg_height
> 65535L ||
297 (long) cinfo
->jpeg_width
> 65535L)
298 ERREXIT1(cinfo
, JERR_IMAGE_TOO_BIG
, (unsigned int) 65535);
300 emit_byte(cinfo
, cinfo
->data_precision
);
301 emit_2bytes(cinfo
, (int) cinfo
->jpeg_height
);
302 emit_2bytes(cinfo
, (int) cinfo
->jpeg_width
);
304 emit_byte(cinfo
, cinfo
->num_components
);
306 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
308 emit_byte(cinfo
, compptr
->component_id
);
309 emit_byte(cinfo
, (compptr
->h_samp_factor
<< 4) + compptr
->v_samp_factor
);
310 emit_byte(cinfo
, compptr
->quant_tbl_no
);
316 emit_sos (j_compress_ptr cinfo
)
317 /* Emit a SOS marker */
320 jpeg_component_info
*compptr
;
322 emit_marker(cinfo
, M_SOS
);
324 emit_2bytes(cinfo
, 2 * cinfo
->comps_in_scan
+ 2 + 1 + 3); /* length */
326 emit_byte(cinfo
, cinfo
->comps_in_scan
);
328 for (i
= 0; i
< cinfo
->comps_in_scan
; i
++) {
329 compptr
= cinfo
->cur_comp_info
[i
];
330 emit_byte(cinfo
, compptr
->component_id
);
332 /* We emit 0 for unused field(s); this is recommended by the P&M text
333 * but does not seem to be specified in the standard.
336 /* DC needs no table for refinement scan */
337 td
= cinfo
->Ss
== 0 && cinfo
->Ah
== 0 ? compptr
->dc_tbl_no
: 0;
338 /* AC needs no table when not present */
339 ta
= cinfo
->Se
? compptr
->ac_tbl_no
: 0;
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_pseudo_sos (j_compress_ptr cinfo
)
352 /* Emit a pseudo SOS marker */
354 emit_marker(cinfo
, M_SOS
);
356 emit_2bytes(cinfo
, 2 + 1 + 3); /* length */
358 emit_byte(cinfo
, 0); /* Ns */
360 emit_byte(cinfo
, 0); /* Ss */
361 emit_byte(cinfo
, cinfo
->block_size
* cinfo
->block_size
- 1); /* Se */
362 emit_byte(cinfo
, 0); /* Ah/Al */
367 emit_jfif_app0 (j_compress_ptr cinfo
)
368 /* Emit a JFIF-compliant APP0 marker */
371 * Length of APP0 block (2 bytes)
372 * Block ID (4 bytes - ASCII "JFIF")
373 * Zero byte (1 byte to terminate the ID string)
374 * Version Major, Minor (2 bytes - major first)
375 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
376 * Xdpu (2 bytes - dots per unit horizontal)
377 * Ydpu (2 bytes - dots per unit vertical)
378 * Thumbnail X size (1 byte)
379 * Thumbnail Y size (1 byte)
382 emit_marker(cinfo
, M_APP0
);
384 emit_2bytes(cinfo
, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
386 emit_byte(cinfo
, 0x4A); /* Identifier: ASCII "JFIF" */
387 emit_byte(cinfo
, 0x46);
388 emit_byte(cinfo
, 0x49);
389 emit_byte(cinfo
, 0x46);
391 emit_byte(cinfo
, cinfo
->JFIF_major_version
); /* Version fields */
392 emit_byte(cinfo
, cinfo
->JFIF_minor_version
);
393 emit_byte(cinfo
, cinfo
->density_unit
); /* Pixel size information */
394 emit_2bytes(cinfo
, (int) cinfo
->X_density
);
395 emit_2bytes(cinfo
, (int) cinfo
->Y_density
);
396 emit_byte(cinfo
, 0); /* No thumbnail image */
402 emit_adobe_app14 (j_compress_ptr cinfo
)
403 /* Emit an Adobe APP14 marker */
406 * Length of APP14 block (2 bytes)
407 * Block ID (5 bytes - ASCII "Adobe")
408 * Version Number (2 bytes - currently 100)
409 * Flags0 (2 bytes - currently 0)
410 * Flags1 (2 bytes - currently 0)
411 * Color transform (1 byte)
413 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
414 * now in circulation seem to use Version = 100, so that's what we write.
416 * We write the color transform byte as 1 if the JPEG color space is
417 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
418 * whether the encoder performed a transformation, which is pretty useless.
421 emit_marker(cinfo
, M_APP14
);
423 emit_2bytes(cinfo
, 2 + 5 + 2 + 2 + 2 + 1); /* length */
425 emit_byte(cinfo
, 0x41); /* Identifier: ASCII "Adobe" */
426 emit_byte(cinfo
, 0x64);
427 emit_byte(cinfo
, 0x6F);
428 emit_byte(cinfo
, 0x62);
429 emit_byte(cinfo
, 0x65);
430 emit_2bytes(cinfo
, 100); /* Version */
431 emit_2bytes(cinfo
, 0); /* Flags0 */
432 emit_2bytes(cinfo
, 0); /* Flags1 */
433 switch (cinfo
->jpeg_color_space
) {
435 emit_byte(cinfo
, 1); /* Color transform = 1 */
438 emit_byte(cinfo
, 2); /* Color transform = 2 */
441 emit_byte(cinfo
, 0); /* Color transform = 0 */
448 * These routines allow writing an arbitrary marker with parameters.
449 * The only intended use is to emit COM or APPn markers after calling
450 * write_file_header and before calling write_frame_header.
451 * Other uses are not guaranteed to produce desirable results.
452 * Counting the parameter bytes properly is the caller's responsibility.
456 write_marker_header (j_compress_ptr cinfo
, int marker
, unsigned int datalen
)
457 /* Emit an arbitrary marker header */
459 if (datalen
> (unsigned int) 65533) /* safety check */
460 ERREXIT(cinfo
, JERR_BAD_LENGTH
);
462 emit_marker(cinfo
, (JPEG_MARKER
) marker
);
464 emit_2bytes(cinfo
, (int) (datalen
+ 2)); /* total length */
468 write_marker_byte (j_compress_ptr cinfo
, int val
)
469 /* Emit one byte of marker parameters following write_marker_header */
471 emit_byte(cinfo
, val
);
476 * Write datastream header.
477 * This consists of an SOI and optional APPn markers.
478 * We recommend use of the JFIF marker, but not the Adobe marker,
479 * when using YCbCr or grayscale data. The JFIF marker should NOT
480 * be used for any other JPEG colorspace. The Adobe marker is helpful
481 * to distinguish RGB, CMYK, and YCCK colorspaces.
482 * Note that an application can write additional header markers after
483 * jpeg_start_compress returns.
487 write_file_header (j_compress_ptr cinfo
)
489 my_marker_ptr marker
= (my_marker_ptr
) cinfo
->marker
;
491 emit_marker(cinfo
, M_SOI
); /* first the SOI */
493 /* SOI is defined to reset restart interval to 0 */
494 marker
->last_restart_interval
= 0;
496 if (cinfo
->write_JFIF_header
) /* next an optional JFIF APP0 */
497 emit_jfif_app0(cinfo
);
498 if (cinfo
->write_Adobe_marker
) /* next an optional Adobe APP14 */
499 emit_adobe_app14(cinfo
);
504 * Write frame header.
505 * This consists of DQT and SOFn markers, and a conditional pseudo SOS marker.
506 * Note that we do not emit the SOF until we have emitted the DQT(s).
507 * This avoids compatibility problems with incorrect implementations that
508 * try to error-check the quant table numbers as soon as they see the SOF.
512 write_frame_header (j_compress_ptr cinfo
)
516 jpeg_component_info
*compptr
;
518 /* Emit DQT for each quantization table.
519 * Note that emit_dqt() suppresses any duplicate tables.
522 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
524 prec
+= emit_dqt(cinfo
, compptr
->quant_tbl_no
);
526 /* now prec is nonzero iff there are any 16-bit quant tables. */
528 /* Check for a non-baseline specification.
529 * Note we assume that Huffman table numbers won't be changed later.
531 if (cinfo
->arith_code
|| cinfo
->progressive_mode
||
532 cinfo
->data_precision
!= 8 || cinfo
->block_size
!= DCTSIZE
) {
536 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
538 if (compptr
->dc_tbl_no
> 1 || compptr
->ac_tbl_no
> 1)
541 if (prec
&& is_baseline
) {
543 /* If it's baseline except for quantizer size, warn the user */
544 TRACEMS(cinfo
, 0, JTRC_16BIT_TABLES
);
548 /* Emit the proper SOF marker */
549 if (cinfo
->arith_code
) {
550 if (cinfo
->progressive_mode
)
551 emit_sof(cinfo
, M_SOF10
); /* SOF code for progressive arithmetic */
553 emit_sof(cinfo
, M_SOF9
); /* SOF code for sequential arithmetic */
555 if (cinfo
->progressive_mode
)
556 emit_sof(cinfo
, M_SOF2
); /* SOF code for progressive Huffman */
557 else if (is_baseline
)
558 emit_sof(cinfo
, M_SOF0
); /* SOF code for baseline implementation */
560 emit_sof(cinfo
, M_SOF1
); /* SOF code for non-baseline Huffman file */
563 /* Check to emit pseudo SOS marker */
564 if (cinfo
->progressive_mode
&& cinfo
->block_size
!= DCTSIZE
)
565 emit_pseudo_sos(cinfo
);
571 * This consists of DHT or DAC markers, optional DRI, and SOS.
572 * Compressed data will be written following the SOS.
576 write_scan_header (j_compress_ptr cinfo
)
578 my_marker_ptr marker
= (my_marker_ptr
) cinfo
->marker
;
580 jpeg_component_info
*compptr
;
582 if (cinfo
->arith_code
) {
583 /* Emit arith conditioning info. We may have some duplication
584 * if the file has multiple scans, but it's so small it's hardly
585 * worth worrying about.
589 /* Emit Huffman tables.
590 * Note that emit_dht() suppresses any duplicate tables.
592 for (i
= 0; i
< cinfo
->comps_in_scan
; i
++) {
593 compptr
= cinfo
->cur_comp_info
[i
];
594 /* DC needs no table for refinement scan */
595 if (cinfo
->Ss
== 0 && cinfo
->Ah
== 0)
596 emit_dht(cinfo
, compptr
->dc_tbl_no
, FALSE
);
597 /* AC needs no table when not present */
599 emit_dht(cinfo
, compptr
->ac_tbl_no
, TRUE
);
603 /* Emit DRI if required --- note that DRI value could change for each scan.
604 * We avoid wasting space with unnecessary DRIs, however.
606 if (cinfo
->restart_interval
!= marker
->last_restart_interval
) {
608 marker
->last_restart_interval
= cinfo
->restart_interval
;
616 * Write datastream trailer.
620 write_file_trailer (j_compress_ptr cinfo
)
622 emit_marker(cinfo
, M_EOI
);
627 * Write an abbreviated table-specification datastream.
628 * This consists of SOI, DQT and DHT tables, and EOI.
629 * Any table that is defined and not marked sent_table = TRUE will be
630 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
634 write_tables_only (j_compress_ptr cinfo
)
638 emit_marker(cinfo
, M_SOI
);
640 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++) {
641 if (cinfo
->quant_tbl_ptrs
[i
] != NULL
)
642 (void) emit_dqt(cinfo
, i
);
645 if (! cinfo
->arith_code
) {
646 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
647 if (cinfo
->dc_huff_tbl_ptrs
[i
] != NULL
)
648 emit_dht(cinfo
, i
, FALSE
);
649 if (cinfo
->ac_huff_tbl_ptrs
[i
] != NULL
)
650 emit_dht(cinfo
, i
, TRUE
);
654 emit_marker(cinfo
, M_EOI
);
659 * Initialize the marker writer module.
663 jinit_marker_writer (j_compress_ptr cinfo
)
665 my_marker_ptr marker
;
667 /* Create the subobject */
668 marker
= (my_marker_ptr
)
669 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
670 SIZEOF(my_marker_writer
));
671 cinfo
->marker
= (struct jpeg_marker_writer
*) marker
;
672 /* Initialize method pointers */
673 marker
->pub
.write_file_header
= write_file_header
;
674 marker
->pub
.write_frame_header
= write_frame_header
;
675 marker
->pub
.write_scan_header
= write_scan_header
;
676 marker
->pub
.write_file_trailer
= write_file_trailer
;
677 marker
->pub
.write_tables_only
= write_tables_only
;
678 marker
->pub
.write_marker_header
= write_marker_header
;
679 marker
->pub
.write_marker_byte
= write_marker_byte
;
680 /* Initialize private state */
681 marker
->last_restart_interval
= 0;