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 */
85 struct jpeg_marker_writer pub
; /* public fields */
87 unsigned int last_restart_interval
; /* last DRI value emitted; 0 after SOI */
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.
106 emit_byte (j_compress_ptr cinfo
, int16 val
)
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
);
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
);
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.
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
];
151 ERREXIT1(cinfo
, JERR_NO_QUANT_TABLE
, index
);
154 for (i
= 0; i
< DCTSIZE2
; i
++) {
155 if (qtbl
->quantval
[i
] > 255)
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
]];
170 emit_byte(cinfo
, (int16
) (qval
>> 8));
171 emit_byte(cinfo
, (int16
) (qval
& 0xFF));
174 qtbl
->sent_table
= TRUE
;
182 emit_dht (j_compress_ptr cinfo
, int16 index
, boolean is_ac
)
183 /* Emit a DHT marker */
189 htbl
= cinfo
->ac_huff_tbl_ptrs
[index
];
190 index
+= 0x10; /* output index has AC bit set */
192 htbl
= cinfo
->dc_huff_tbl_ptrs
[index
];
196 ERREXIT1(cinfo
, JERR_NO_HUFF_TABLE
, index
);
198 if (! htbl
->sent_table
) {
199 emit_marker(cinfo
, M_DHT
);
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
;
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
];
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;
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
++) {
251 emit_byte(cinfo
, cinfo
->arith_dc_L
[i
] + (cinfo
->arith_dc_U
[i
]<<4));
254 emit_byte(cinfo
, i
+ 0x10);
255 emit_byte(cinfo
, cinfo
->arith_ac_K
[i
]);
258 #endif /* C_ARITH_CODING_SUPPORTED */
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
);
275 emit_sof (j_compress_ptr cinfo
, JPEG_MARKER code
)
276 /* Emit a SOF marker */
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
;
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
);
306 emit_sos (j_compress_ptr cinfo
)
307 /* Emit a SOS marker */
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 */
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
);
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);
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 */
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
) {
415 emit_byte(cinfo
, 1); /* Color transform = 1 */
418 emit_byte(cinfo
, 2); /* Color transform = 2 */
421 emit_byte(cinfo
, 0); /* Color transform = 0 */
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.
436 write_marker_header (j_compress_ptr cinfo
, int16 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 */
448 write_marker_byte (j_compress_ptr cinfo
, int16 val
)
449 /* Emit one byte of marker parameters following write_marker_header */
451 emit_byte(cinfo
, 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.
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.
492 write_frame_header (j_compress_ptr cinfo
)
496 jpeg_component_info
*compptr
;
498 /* Emit DQT for each quantization table.
499 * Note that emit_dqt() suppresses any duplicate tables.
502 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
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) {
516 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
518 if (compptr
->dc_tbl_no
> 1 || compptr
->ac_tbl_no
> 1)
521 if (prec
&& is_baseline
) {
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 */
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 */
537 emit_sof(cinfo
, M_SOF1
); /* SOF code for non-baseline Huffman file */
544 * This consists of DHT or DAC markers, optional DRI, and SOS.
545 * Compressed data will be written following the SOS.
549 write_scan_header (j_compress_ptr cinfo
)
551 my_marker_ptr marker
= (my_marker_ptr
) cinfo
->marker
;
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.
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
);
573 emit_dht(cinfo
, compptr
->ac_tbl_no
, TRUE
);
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
) {
588 marker
->last_restart_interval
= cinfo
->restart_interval
;
596 * Write datastream trailer.
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.
614 write_tables_only (j_compress_ptr cinfo
)
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.
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;