8 * Copyright (C) 1991-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains routines to write JPEG datastream markers.
15 #define JPEG_INTERNALS
20 typedef enum { /* JPEG marker codes */
91 struct jpeg_marker_writer pub
; /* public fields */
93 unsigned int last_restart_interval
; /* last DRI value emitted; 0 after SOI */
96 typedef my_marker_writer
* my_marker_ptr
;
100 * Basic output routines.
102 * Note that we do not support suspension while writing a marker.
103 * Therefore, an application using suspension must ensure that there is
104 * enough buffer space for the initial markers (typ. 600-700 bytes) before
105 * calling jpeg_start_compress, and enough space to write the trailing EOI
106 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
107 * modes are not supported at all with suspension, so those two are the only
108 * points where markers will be written.
112 emit_byte (j_compress_ptr cinfo
, int val
)
115 struct jpeg_destination_mgr
* dest
= cinfo
->dest
;
117 *(dest
->next_output_byte
)++ = (JOCTET
) val
;
118 if (--dest
->free_in_buffer
== 0) {
119 if (! (*dest
->empty_output_buffer
) (cinfo
))
120 ERREXIT(cinfo
, JERR_CANT_SUSPEND
);
126 emit_marker (j_compress_ptr cinfo
, JPEG_MARKER mark
)
127 /* Emit a marker code */
129 emit_byte(cinfo
, 0xFF);
130 emit_byte(cinfo
, (int) mark
);
135 emit_2bytes (j_compress_ptr cinfo
, int value
)
136 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
138 emit_byte(cinfo
, (value
>> 8) & 0xFF);
139 emit_byte(cinfo
, value
& 0xFF);
144 * Routines to write specific marker types.
148 emit_dqt (j_compress_ptr cinfo
, int index
)
149 /* Emit a DQT marker */
150 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
152 JQUANT_TBL
* qtbl
= cinfo
->quant_tbl_ptrs
[index
];
157 ERREXIT1(cinfo
, JERR_NO_QUANT_TABLE
, index
);
160 for (i
= 0; i
< DCTSIZE2
; i
++) {
161 if (qtbl
->quantval
[i
] > 255)
165 if (! qtbl
->sent_table
) {
166 emit_marker(cinfo
, M_DQT
);
168 emit_2bytes(cinfo
, prec
? DCTSIZE2
*2 + 1 + 2 : DCTSIZE2
+ 1 + 2);
170 emit_byte(cinfo
, index
+ (prec
<<4));
172 for (i
= 0; i
< DCTSIZE2
; i
++) {
173 /* The table entries must be emitted in zigzag order. */
174 unsigned int qval
= qtbl
->quantval
[jpeg_natural_order
[i
]];
176 emit_byte(cinfo
, (int) (qval
>> 8));
177 emit_byte(cinfo
, (int) (qval
& 0xFF));
180 qtbl
->sent_table
= TRUE
;
188 emit_dht (j_compress_ptr cinfo
, int index
, boolean is_ac
)
189 /* Emit a DHT marker */
195 htbl
= cinfo
->ac_huff_tbl_ptrs
[index
];
196 index
+= 0x10; /* output index has AC bit set */
198 htbl
= cinfo
->dc_huff_tbl_ptrs
[index
];
202 ERREXIT1(cinfo
, JERR_NO_HUFF_TABLE
, index
);
204 if (! htbl
->sent_table
) {
205 emit_marker(cinfo
, M_DHT
);
208 for (i
= 1; i
<= 16; i
++)
209 length
+= htbl
->bits
[i
];
211 emit_2bytes(cinfo
, length
+ 2 + 1 + 16);
212 emit_byte(cinfo
, index
);
214 for (i
= 1; i
<= 16; i
++)
215 emit_byte(cinfo
, htbl
->bits
[i
]);
217 for (i
= 0; i
< length
; i
++)
218 emit_byte(cinfo
, htbl
->huffval
[i
]);
220 htbl
->sent_table
= TRUE
;
226 emit_dac (j_compress_ptr cinfo
)
227 /* Emit a DAC marker */
228 /* Since the useful info is so small, we want to emit all the tables in */
229 /* one DAC marker. Therefore this routine does its own scan of the table. */
231 #ifdef C_ARITH_CODING_SUPPORTED
232 char dc_in_use
[NUM_ARITH_TBLS
];
233 char ac_in_use
[NUM_ARITH_TBLS
];
235 jpeg_component_info
*compptr
;
237 for (i
= 0; i
< NUM_ARITH_TBLS
; i
++)
238 dc_in_use
[i
] = ac_in_use
[i
] = 0;
240 for (i
= 0; i
< cinfo
->comps_in_scan
; i
++) {
241 compptr
= cinfo
->cur_comp_info
[i
];
242 dc_in_use
[compptr
->dc_tbl_no
] = 1;
243 ac_in_use
[compptr
->ac_tbl_no
] = 1;
247 for (i
= 0; i
< NUM_ARITH_TBLS
; i
++)
248 length
+= dc_in_use
[i
] + ac_in_use
[i
];
250 emit_marker(cinfo
, M_DAC
);
252 emit_2bytes(cinfo
, length
*2 + 2);
254 for (i
= 0; i
< NUM_ARITH_TBLS
; i
++) {
257 emit_byte(cinfo
, cinfo
->arith_dc_L
[i
] + (cinfo
->arith_dc_U
[i
]<<4));
260 emit_byte(cinfo
, i
+ 0x10);
261 emit_byte(cinfo
, cinfo
->arith_ac_K
[i
]);
264 #endif /* C_ARITH_CODING_SUPPORTED */
269 emit_dri (j_compress_ptr cinfo
)
270 /* Emit a DRI marker */
272 emit_marker(cinfo
, M_DRI
);
274 emit_2bytes(cinfo
, 4); /* fixed length */
276 emit_2bytes(cinfo
, (int) cinfo
->restart_interval
);
281 emit_sof (j_compress_ptr cinfo
, JPEG_MARKER code
)
282 /* Emit a SOF marker */
285 jpeg_component_info
*compptr
;
287 emit_marker(cinfo
, code
);
289 emit_2bytes(cinfo
, 3 * cinfo
->num_components
+ 2 + 5 + 1); /* length */
291 /* Make sure image isn't bigger than SOF field can handle */
292 if ((long) cinfo
->image_height
> 65535L ||
293 (long) cinfo
->image_width
> 65535L)
294 ERREXIT1(cinfo
, JERR_IMAGE_TOO_BIG
, (unsigned int) 65535);
296 emit_byte(cinfo
, cinfo
->data_precision
);
297 emit_2bytes(cinfo
, (int) cinfo
->image_height
);
298 emit_2bytes(cinfo
, (int) cinfo
->image_width
);
300 emit_byte(cinfo
, cinfo
->num_components
);
302 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
304 emit_byte(cinfo
, compptr
->component_id
);
305 emit_byte(cinfo
, (compptr
->h_samp_factor
<< 4) + compptr
->v_samp_factor
);
306 emit_byte(cinfo
, compptr
->quant_tbl_no
);
312 emit_sos (j_compress_ptr cinfo
)
313 /* Emit a SOS marker */
316 jpeg_component_info
*compptr
;
318 emit_marker(cinfo
, M_SOS
);
320 emit_2bytes(cinfo
, 2 * cinfo
->comps_in_scan
+ 2 + 1 + 3); /* length */
322 emit_byte(cinfo
, cinfo
->comps_in_scan
);
324 for (i
= 0; i
< cinfo
->comps_in_scan
; i
++) {
325 compptr
= cinfo
->cur_comp_info
[i
];
326 emit_byte(cinfo
, compptr
->component_id
);
327 td
= compptr
->dc_tbl_no
;
328 ta
= compptr
->ac_tbl_no
;
329 if (cinfo
->process
== JPROC_PROGRESSIVE
) {
330 /* Progressive mode: only DC or only AC tables are used in one scan;
331 * furthermore, Huffman coding of DC refinement uses no table at all.
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.
335 if (cinfo
->Ss
== 0) {
336 ta
= 0; /* DC scan */
337 if (cinfo
->Ah
!= 0 && !cinfo
->arith_code
)
338 td
= 0; /* no DC table either */
340 td
= 0; /* AC scan */
343 emit_byte(cinfo
, (td
<< 4) + ta
);
346 emit_byte(cinfo
, cinfo
->Ss
);
347 emit_byte(cinfo
, cinfo
->Se
);
348 emit_byte(cinfo
, (cinfo
->Ah
<< 4) + cinfo
->Al
);
353 emit_jfif_app0 (j_compress_ptr cinfo
)
354 /* Emit a JFIF-compliant APP0 marker */
357 * Length of APP0 block (2 bytes)
358 * Block ID (4 bytes - ASCII "JFIF")
359 * Zero byte (1 byte to terminate the ID string)
360 * Version Major, Minor (2 bytes - major first)
361 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
362 * Xdpu (2 bytes - dots per unit horizontal)
363 * Ydpu (2 bytes - dots per unit vertical)
364 * Thumbnail X size (1 byte)
365 * Thumbnail Y size (1 byte)
368 emit_marker(cinfo
, M_APP0
);
370 emit_2bytes(cinfo
, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
372 emit_byte(cinfo
, 0x4A); /* Identifier: ASCII "JFIF" */
373 emit_byte(cinfo
, 0x46);
374 emit_byte(cinfo
, 0x49);
375 emit_byte(cinfo
, 0x46);
377 emit_byte(cinfo
, cinfo
->JFIF_major_version
); /* Version fields */
378 emit_byte(cinfo
, cinfo
->JFIF_minor_version
);
379 emit_byte(cinfo
, cinfo
->density_unit
); /* Pixel size information */
380 emit_2bytes(cinfo
, (int) cinfo
->X_density
);
381 emit_2bytes(cinfo
, (int) cinfo
->Y_density
);
382 emit_byte(cinfo
, 0); /* No thumbnail image */
388 emit_adobe_app14 (j_compress_ptr cinfo
)
389 /* Emit an Adobe APP14 marker */
392 * Length of APP14 block (2 bytes)
393 * Block ID (5 bytes - ASCII "Adobe")
394 * Version Number (2 bytes - currently 100)
395 * Flags0 (2 bytes - currently 0)
396 * Flags1 (2 bytes - currently 0)
397 * Color transform (1 byte)
399 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
400 * now in circulation seem to use Version = 100, so that's what we write.
402 * We write the color transform byte as 1 if the JPEG color space is
403 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
404 * whether the encoder performed a transformation, which is pretty useless.
407 emit_marker(cinfo
, M_APP14
);
409 emit_2bytes(cinfo
, 2 + 5 + 2 + 2 + 2 + 1); /* length */
411 emit_byte(cinfo
, 0x41); /* Identifier: ASCII "Adobe" */
412 emit_byte(cinfo
, 0x64);
413 emit_byte(cinfo
, 0x6F);
414 emit_byte(cinfo
, 0x62);
415 emit_byte(cinfo
, 0x65);
416 emit_2bytes(cinfo
, 100); /* Version */
417 emit_2bytes(cinfo
, 0); /* Flags0 */
418 emit_2bytes(cinfo
, 0); /* Flags1 */
419 switch (cinfo
->jpeg_color_space
) {
421 emit_byte(cinfo
, 1); /* Color transform = 1 */
424 emit_byte(cinfo
, 2); /* Color transform = 2 */
427 emit_byte(cinfo
, 0); /* Color transform = 0 */
434 * These routines allow writing an arbitrary marker with parameters.
435 * The only intended use is to emit COM or APPn markers after calling
436 * write_file_header and before calling write_frame_header.
437 * Other uses are not guaranteed to produce desirable results.
438 * Counting the parameter bytes properly is the caller's responsibility.
442 write_marker_header (j_compress_ptr cinfo
, int marker
, unsigned int datalen
)
443 /* Emit an arbitrary marker header */
445 if (datalen
> (unsigned int) 65533) /* safety check */
446 ERREXIT(cinfo
, JERR_BAD_LENGTH
);
448 emit_marker(cinfo
, (JPEG_MARKER
) marker
);
450 emit_2bytes(cinfo
, (int) (datalen
+ 2)); /* total length */
454 write_marker_byte (j_compress_ptr cinfo
, int val
)
455 /* Emit one byte of marker parameters following write_marker_header */
457 emit_byte(cinfo
, val
);
462 * Write datastream header.
463 * This consists of an SOI and optional APPn markers.
464 * We recommend use of the JFIF marker, but not the Adobe marker,
465 * when using YCbCr or grayscale data. The JFIF marker should NOT
466 * be used for any other JPEG colorspace. The Adobe marker is helpful
467 * to distinguish RGB, CMYK, and YCCK colorspaces.
468 * Note that an application can write additional header markers after
469 * jpeg_start_compress returns.
473 write_file_header (j_compress_ptr cinfo
)
475 my_marker_ptr marker
= (my_marker_ptr
) cinfo
->marker
;
477 emit_marker(cinfo
, M_SOI
); /* first the SOI */
479 /* SOI is defined to reset restart interval to 0 */
480 marker
->last_restart_interval
= 0;
482 if (cinfo
->write_JFIF_header
) /* next an optional JFIF APP0 */
483 emit_jfif_app0(cinfo
);
484 if (cinfo
->write_Adobe_marker
) /* next an optional Adobe APP14 */
485 emit_adobe_app14(cinfo
);
490 * Write frame header.
491 * This consists of DQT and SOFn markers.
492 * Note that we do not emit the SOF until we have emitted the DQT(s).
493 * This avoids compatibility problems with incorrect implementations that
494 * try to error-check the quant table numbers as soon as they see the SOF.
498 write_frame_header (j_compress_ptr cinfo
)
502 jpeg_component_info
*compptr
;
504 if (cinfo
->process
!= JPROC_LOSSLESS
) {
505 /* Emit DQT for each quantization table.
506 * Note that emit_dqt() suppresses any duplicate tables.
509 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
511 prec
+= emit_dqt(cinfo
, compptr
->quant_tbl_no
);
513 /* now prec is nonzero iff there are any 16-bit quant tables. */
516 /* Check for a non-baseline specification.
517 * Note we assume that Huffman table numbers won't be changed later.
519 if (cinfo
->arith_code
|| cinfo
->process
!= JPROC_SEQUENTIAL
||
520 cinfo
->data_precision
!= 8) {
524 for (ci
= 0, compptr
= cinfo
->comp_info
; ci
< cinfo
->num_components
;
526 if (compptr
->dc_tbl_no
> 1 || compptr
->ac_tbl_no
> 1)
529 if (prec
&& is_baseline
) {
531 /* If it's baseline except for quantizer size, warn the user */
532 TRACEMS(cinfo
, 0, JTRC_16BIT_TABLES
);
536 /* Emit the proper SOF marker */
537 if (cinfo
->arith_code
) {
538 emit_sof(cinfo
, M_SOF9
); /* SOF code for arithmetic coding */
540 if (cinfo
->process
== JPROC_PROGRESSIVE
)
541 emit_sof(cinfo
, M_SOF2
); /* SOF code for progressive Huffman */
542 else if (cinfo
->process
== JPROC_LOSSLESS
)
543 emit_sof(cinfo
, M_SOF3
); /* SOF code for lossless Huffman */
544 else if (is_baseline
)
545 emit_sof(cinfo
, M_SOF0
); /* SOF code for baseline implementation */
547 emit_sof(cinfo
, M_SOF1
); /* SOF code for non-baseline Huffman file */
554 * This consists of DHT or DAC markers, optional DRI, and SOS.
555 * Compressed data will be written following the SOS.
559 write_scan_header (j_compress_ptr cinfo
)
561 my_marker_ptr marker
= (my_marker_ptr
) cinfo
->marker
;
563 jpeg_component_info
*compptr
;
565 if (cinfo
->arith_code
) {
566 /* Emit arith conditioning info. We may have some duplication
567 * if the file has multiple scans, but it's so small it's hardly
568 * worth worrying about.
572 /* Emit Huffman tables.
573 * Note that emit_dht() suppresses any duplicate tables.
575 for (i
= 0; i
< cinfo
->comps_in_scan
; i
++) {
576 compptr
= cinfo
->cur_comp_info
[i
];
577 if (cinfo
->process
== JPROC_PROGRESSIVE
) {
578 /* Progressive mode: only DC or only AC tables are used in one scan */
579 if (cinfo
->Ss
== 0) {
580 if (cinfo
->Ah
== 0) /* DC needs no table for refinement scan */
581 emit_dht(cinfo
, compptr
->dc_tbl_no
, FALSE
);
583 emit_dht(cinfo
, compptr
->ac_tbl_no
, TRUE
);
585 } else if (cinfo
->process
== JPROC_LOSSLESS
) {
586 /* Lossless mode: only DC tables are used */
587 emit_dht(cinfo
, compptr
->dc_tbl_no
, FALSE
);
589 /* Sequential mode: need both DC and AC tables */
590 emit_dht(cinfo
, compptr
->dc_tbl_no
, FALSE
);
591 emit_dht(cinfo
, compptr
->ac_tbl_no
, TRUE
);
596 /* Emit DRI if required --- note that DRI value could change for each scan.
597 * We avoid wasting space with unnecessary DRIs, however.
599 if (cinfo
->restart_interval
!= marker
->last_restart_interval
) {
601 marker
->last_restart_interval
= cinfo
->restart_interval
;
609 * Write datastream trailer.
613 write_file_trailer (j_compress_ptr cinfo
)
615 emit_marker(cinfo
, M_EOI
);
620 * Write an abbreviated table-specification datastream.
621 * This consists of SOI, DQT and DHT tables, and EOI.
622 * Any table that is defined and not marked sent_table = TRUE will be
623 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
627 write_tables_only (j_compress_ptr cinfo
)
631 emit_marker(cinfo
, M_SOI
);
633 for (i
= 0; i
< NUM_QUANT_TBLS
; i
++) {
634 if (cinfo
->quant_tbl_ptrs
[i
] != NULL
)
635 (void) emit_dqt(cinfo
, i
);
638 if (! cinfo
->arith_code
) {
639 for (i
= 0; i
< NUM_HUFF_TBLS
; i
++) {
640 if (cinfo
->dc_huff_tbl_ptrs
[i
] != NULL
)
641 emit_dht(cinfo
, i
, FALSE
);
642 if (cinfo
->ac_huff_tbl_ptrs
[i
] != NULL
)
643 emit_dht(cinfo
, i
, TRUE
);
647 emit_marker(cinfo
, M_EOI
);
652 * Initialize the marker writer module.
656 jinit_marker_writer (j_compress_ptr cinfo
)
658 my_marker_ptr marker
;
660 /* Create the subobject */
661 marker
= (my_marker_ptr
)
662 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
663 SIZEOF(my_marker_writer
));
664 cinfo
->marker
= (struct jpeg_marker_writer
*) marker
;
665 /* Initialize method pointers */
666 marker
->pub
.write_file_header
= write_file_header
;
667 marker
->pub
.write_frame_header
= write_frame_header
;
668 marker
->pub
.write_scan_header
= write_scan_header
;
669 marker
->pub
.write_file_trailer
= write_file_trailer
;
670 marker
->pub
.write_tables_only
= write_tables_only
;
671 marker
->pub
.write_marker_header
= write_marker_header
;
672 marker
->pub
.write_marker_byte
= write_marker_byte
;
673 /* Initialize private state */
674 marker
->last_restart_interval
= 0;