blob: f4d290b99927dbfafe10caf6e112b9377b937fc9 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmarker.c
3 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +00004 * Copyright (C) 1991-1995, Thomas G. Lane.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00005 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
8 * This file contains routines to write JPEG datastream markers.
9 */
10
11#define JPEG_INTERNALS
12#include "jinclude.h"
13#include "jpeglib.h"
14
15
16typedef enum { /* JPEG marker codes */
17 M_SOF0 = 0xc0,
18 M_SOF1 = 0xc1,
19 M_SOF2 = 0xc2,
20 M_SOF3 = 0xc3,
21
22 M_SOF5 = 0xc5,
23 M_SOF6 = 0xc6,
24 M_SOF7 = 0xc7,
25
26 M_JPG = 0xc8,
27 M_SOF9 = 0xc9,
28 M_SOF10 = 0xca,
29 M_SOF11 = 0xcb,
30
31 M_SOF13 = 0xcd,
32 M_SOF14 = 0xce,
33 M_SOF15 = 0xcf,
34
35 M_DHT = 0xc4,
36
37 M_DAC = 0xcc,
38
39 M_RST0 = 0xd0,
40 M_RST1 = 0xd1,
41 M_RST2 = 0xd2,
42 M_RST3 = 0xd3,
43 M_RST4 = 0xd4,
44 M_RST5 = 0xd5,
45 M_RST6 = 0xd6,
46 M_RST7 = 0xd7,
47
48 M_SOI = 0xd8,
49 M_EOI = 0xd9,
50 M_SOS = 0xda,
51 M_DQT = 0xdb,
52 M_DNL = 0xdc,
53 M_DRI = 0xdd,
54 M_DHP = 0xde,
55 M_EXP = 0xdf,
56
57 M_APP0 = 0xe0,
58 M_APP1 = 0xe1,
59 M_APP2 = 0xe2,
60 M_APP3 = 0xe3,
61 M_APP4 = 0xe4,
62 M_APP5 = 0xe5,
63 M_APP6 = 0xe6,
64 M_APP7 = 0xe7,
65 M_APP8 = 0xe8,
66 M_APP9 = 0xe9,
67 M_APP10 = 0xea,
68 M_APP11 = 0xeb,
69 M_APP12 = 0xec,
70 M_APP13 = 0xed,
71 M_APP14 = 0xee,
72 M_APP15 = 0xef,
73
74 M_JPG0 = 0xf0,
75 M_JPG13 = 0xfd,
76 M_COM = 0xfe,
77
78 M_TEM = 0x01,
79
80 M_ERROR = 0x100
81} JPEG_MARKER;
82
83
84/*
85 * Basic output routines.
86 *
87 * Note that we do not support suspension while writing a marker.
88 * Therefore, an application using suspension must ensure that there is
89 * enough buffer space for the initial markers (typ. 600-700 bytes) before
90 * calling jpeg_start_compress, and enough space to write the trailing EOI
91 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
92 * modes are not supported at all with suspension, so those two are the only
93 * points where markers will be written.
94 */
95
96LOCAL void
97emit_byte (j_compress_ptr cinfo, int val)
98/* Emit a byte */
99{
100 struct jpeg_destination_mgr * dest = cinfo->dest;
101
102 *(dest->next_output_byte)++ = (JOCTET) val;
103 if (--dest->free_in_buffer == 0) {
104 if (! (*dest->empty_output_buffer) (cinfo))
105 ERREXIT(cinfo, JERR_CANT_SUSPEND);
106 }
107}
108
109
110LOCAL void
111emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
112/* Emit a marker code */
113{
114 emit_byte(cinfo, 0xFF);
115 emit_byte(cinfo, (int) mark);
116}
117
118
119LOCAL void
120emit_2bytes (j_compress_ptr cinfo, int value)
121/* Emit a 2-byte integer; these are always MSB first in JPEG files */
122{
123 emit_byte(cinfo, (value >> 8) & 0xFF);
124 emit_byte(cinfo, value & 0xFF);
125}
126
127
128/*
129 * Routines to write specific marker types.
130 */
131
132LOCAL int
133emit_dqt (j_compress_ptr cinfo, int index)
134/* Emit a DQT marker */
135/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
136{
137 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
138 int prec;
139 int i;
140
141 if (qtbl == NULL)
142 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
143
144 prec = 0;
145 for (i = 0; i < DCTSIZE2; i++) {
146 if (qtbl->quantval[i] > 255)
147 prec = 1;
148 }
149
150 if (! qtbl->sent_table) {
151 emit_marker(cinfo, M_DQT);
152
153 emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
154
155 emit_byte(cinfo, index + (prec<<4));
156
157 for (i = 0; i < DCTSIZE2; i++) {
158 if (prec)
159 emit_byte(cinfo, qtbl->quantval[i] >> 8);
160 emit_byte(cinfo, qtbl->quantval[i] & 0xFF);
161 }
162
163 qtbl->sent_table = TRUE;
164 }
165
166 return prec;
167}
168
169
170LOCAL void
171emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
172/* Emit a DHT marker */
173{
174 JHUFF_TBL * htbl;
175 int length, i;
176
177 if (is_ac) {
178 htbl = cinfo->ac_huff_tbl_ptrs[index];
179 index += 0x10; /* output index has AC bit set */
180 } else {
181 htbl = cinfo->dc_huff_tbl_ptrs[index];
182 }
183
184 if (htbl == NULL)
185 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
186
187 if (! htbl->sent_table) {
188 emit_marker(cinfo, M_DHT);
189
190 length = 0;
191 for (i = 1; i <= 16; i++)
192 length += htbl->bits[i];
193
194 emit_2bytes(cinfo, length + 2 + 1 + 16);
195 emit_byte(cinfo, index);
196
197 for (i = 1; i <= 16; i++)
198 emit_byte(cinfo, htbl->bits[i]);
199
200 for (i = 0; i < length; i++)
201 emit_byte(cinfo, htbl->huffval[i]);
202
203 htbl->sent_table = TRUE;
204 }
205}
206
207
208LOCAL void
209emit_dac (j_compress_ptr cinfo)
210/* Emit a DAC marker */
211/* Since the useful info is so small, we want to emit all the tables in */
212/* one DAC marker. Therefore this routine does its own scan of the table. */
213{
214#ifdef C_ARITH_CODING_SUPPORTED
215 char dc_in_use[NUM_ARITH_TBLS];
216 char ac_in_use[NUM_ARITH_TBLS];
217 int length, i;
218 jpeg_component_info *compptr;
219
220 for (i = 0; i < NUM_ARITH_TBLS; i++)
221 dc_in_use[i] = ac_in_use[i] = 0;
222
223 for (i = 0; i < cinfo->comps_in_scan; i++) {
224 compptr = cinfo->cur_comp_info[i];
225 dc_in_use[compptr->dc_tbl_no] = 1;
226 ac_in_use[compptr->ac_tbl_no] = 1;
227 }
228
229 length = 0;
230 for (i = 0; i < NUM_ARITH_TBLS; i++)
231 length += dc_in_use[i] + ac_in_use[i];
232
233 emit_marker(cinfo, M_DAC);
234
235 emit_2bytes(cinfo, length*2 + 2);
236
237 for (i = 0; i < NUM_ARITH_TBLS; i++) {
238 if (dc_in_use[i]) {
239 emit_byte(cinfo, i);
240 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
241 }
242 if (ac_in_use[i]) {
243 emit_byte(cinfo, i + 0x10);
244 emit_byte(cinfo, cinfo->arith_ac_K[i]);
245 }
246 }
247#endif /* C_ARITH_CODING_SUPPORTED */
248}
249
250
251LOCAL void
252emit_dri (j_compress_ptr cinfo)
253/* Emit a DRI marker */
254{
255 emit_marker(cinfo, M_DRI);
256
257 emit_2bytes(cinfo, 4); /* fixed length */
258
259 emit_2bytes(cinfo, (int) cinfo->restart_interval);
260}
261
262
263LOCAL void
264emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
265/* Emit a SOF marker */
266{
267 int ci;
268 jpeg_component_info *compptr;
269
270 emit_marker(cinfo, code);
271
272 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
273
274 /* Make sure image isn't bigger than SOF field can handle */
275 if ((long) cinfo->image_height > 65535L ||
276 (long) cinfo->image_width > 65535L)
277 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
278
279 emit_byte(cinfo, cinfo->data_precision);
280 emit_2bytes(cinfo, (int) cinfo->image_height);
281 emit_2bytes(cinfo, (int) cinfo->image_width);
282
283 emit_byte(cinfo, cinfo->num_components);
284
285 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
286 ci++, compptr++) {
287 emit_byte(cinfo, compptr->component_id);
288 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
289 emit_byte(cinfo, compptr->quant_tbl_no);
290 }
291}
292
293
294LOCAL void
295emit_sos (j_compress_ptr cinfo)
296/* Emit a SOS marker */
297{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000298 int i, td, ta;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000299 jpeg_component_info *compptr;
300
301 emit_marker(cinfo, M_SOS);
302
303 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
304
305 emit_byte(cinfo, cinfo->comps_in_scan);
306
307 for (i = 0; i < cinfo->comps_in_scan; i++) {
308 compptr = cinfo->cur_comp_info[i];
309 emit_byte(cinfo, compptr->component_id);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000310 td = compptr->dc_tbl_no;
311 ta = compptr->ac_tbl_no;
312 if (cinfo->progressive_mode) {
313 /* Progressive mode: only DC or only AC tables are used in one scan;
314 * furthermore, Huffman coding of DC refinement uses no table at all.
315 * We emit 0 for unused field(s); this is recommended by the P&M text
316 * but does not seem to be specified in the standard.
317 */
318 if (cinfo->Ss == 0) {
319 ta = 0; /* DC scan */
320 if (cinfo->Ah != 0 && !cinfo->arith_code)
321 td = 0; /* no DC table either */
322 } else {
323 td = 0; /* AC scan */
324 }
325 }
326 emit_byte(cinfo, (td << 4) + ta);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000327 }
328
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000329 emit_byte(cinfo, cinfo->Ss);
330 emit_byte(cinfo, cinfo->Se);
331 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332}
333
334
335LOCAL void
336emit_jfif_app0 (j_compress_ptr cinfo)
337/* Emit a JFIF-compliant APP0 marker */
338{
339 /*
340 * Length of APP0 block (2 bytes)
341 * Block ID (4 bytes - ASCII "JFIF")
342 * Zero byte (1 byte to terminate the ID string)
343 * Version Major, Minor (2 bytes - 0x01, 0x01)
344 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
345 * Xdpu (2 bytes - dots per unit horizontal)
346 * Ydpu (2 bytes - dots per unit vertical)
347 * Thumbnail X size (1 byte)
348 * Thumbnail Y size (1 byte)
349 */
350
351 emit_marker(cinfo, M_APP0);
352
353 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
354
355 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
356 emit_byte(cinfo, 0x46);
357 emit_byte(cinfo, 0x49);
358 emit_byte(cinfo, 0x46);
359 emit_byte(cinfo, 0);
360 /* We currently emit version code 1.01 since we use no 1.02 features.
361 * This may avoid complaints from some older decoders.
362 */
363 emit_byte(cinfo, 1); /* Major version */
364 emit_byte(cinfo, 1); /* Minor version */
365 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
366 emit_2bytes(cinfo, (int) cinfo->X_density);
367 emit_2bytes(cinfo, (int) cinfo->Y_density);
368 emit_byte(cinfo, 0); /* No thumbnail image */
369 emit_byte(cinfo, 0);
370}
371
372
373LOCAL void
374emit_adobe_app14 (j_compress_ptr cinfo)
375/* Emit an Adobe APP14 marker */
376{
377 /*
378 * Length of APP14 block (2 bytes)
379 * Block ID (5 bytes - ASCII "Adobe")
380 * Version Number (2 bytes - currently 100)
381 * Flags0 (2 bytes - currently 0)
382 * Flags1 (2 bytes - currently 0)
383 * Color transform (1 byte)
384 *
385 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
386 * now in circulation seem to use Version = 100, so that's what we write.
387 *
388 * We write the color transform byte as 1 if the JPEG color space is
389 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
390 * whether the encoder performed a transformation, which is pretty useless.
391 */
392
393 emit_marker(cinfo, M_APP14);
394
395 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
396
397 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
398 emit_byte(cinfo, 0x64);
399 emit_byte(cinfo, 0x6F);
400 emit_byte(cinfo, 0x62);
401 emit_byte(cinfo, 0x65);
402 emit_2bytes(cinfo, 100); /* Version */
403 emit_2bytes(cinfo, 0); /* Flags0 */
404 emit_2bytes(cinfo, 0); /* Flags1 */
405 switch (cinfo->jpeg_color_space) {
406 case JCS_YCbCr:
407 emit_byte(cinfo, 1); /* Color transform = 1 */
408 break;
409 case JCS_YCCK:
410 emit_byte(cinfo, 2); /* Color transform = 2 */
411 break;
412 default:
413 emit_byte(cinfo, 0); /* Color transform = 0 */
414 break;
415 }
416}
417
418
419/*
420 * This routine is exported for possible use by applications.
421 * The intended use is to emit COM or APPn markers after calling
422 * jpeg_start_compress() and before the first jpeg_write_scanlines() call
423 * (hence, after write_file_header but before write_frame_header).
424 * Other uses are not guaranteed to produce desirable results.
425 */
426
427METHODDEF void
428write_any_marker (j_compress_ptr cinfo, int marker,
429 const JOCTET *dataptr, unsigned int datalen)
430/* Emit an arbitrary marker with parameters */
431{
432 if (datalen <= (unsigned int) 65533) { /* safety check */
433 emit_marker(cinfo, (JPEG_MARKER) marker);
434
435 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
436
437 while (datalen--) {
438 emit_byte(cinfo, *dataptr);
439 dataptr++;
440 }
441 }
442}
443
444
445/*
446 * Write datastream header.
447 * This consists of an SOI and optional APPn markers.
448 * We recommend use of the JFIF marker, but not the Adobe marker,
449 * when using YCbCr or grayscale data. The JFIF marker should NOT
450 * be used for any other JPEG colorspace. The Adobe marker is helpful
451 * to distinguish RGB, CMYK, and YCCK colorspaces.
452 * Note that an application can write additional header markers after
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000453 * jpeg_start_compress returns.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000454 */
455
456METHODDEF void
457write_file_header (j_compress_ptr cinfo)
458{
459 emit_marker(cinfo, M_SOI); /* first the SOI */
460
461 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
462 emit_jfif_app0(cinfo);
463 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
464 emit_adobe_app14(cinfo);
465}
466
467
468/*
469 * Write frame header.
470 * This consists of DQT and SOFn markers.
471 * Note that we do not emit the SOF until we have emitted the DQT(s).
472 * This avoids compatibility problems with incorrect implementations that
473 * try to error-check the quant table numbers as soon as they see the SOF.
474 */
475
476METHODDEF void
477write_frame_header (j_compress_ptr cinfo)
478{
479 int ci, prec;
480 boolean is_baseline;
481 jpeg_component_info *compptr;
482
483 /* Emit DQT for each quantization table.
484 * Note that emit_dqt() suppresses any duplicate tables.
485 */
486 prec = 0;
487 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
488 ci++, compptr++) {
489 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
490 }
491 /* now prec is nonzero iff there are any 16-bit quant tables. */
492
493 /* Check for a non-baseline specification.
494 * Note we assume that Huffman table numbers won't be changed later.
495 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000496 if (cinfo->arith_code || cinfo->progressive_mode ||
497 cinfo->data_precision != 8) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000498 is_baseline = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000499 } else {
500 is_baseline = TRUE;
501 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
502 ci++, compptr++) {
503 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
504 is_baseline = FALSE;
505 }
506 if (prec && is_baseline) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000507 is_baseline = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000508 /* If it's baseline except for quantizer size, warn the user */
509 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
510 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000511 }
512
513 /* Emit the proper SOF marker */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000514 if (cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000515 emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000516 } else {
517 if (cinfo->progressive_mode)
518 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
519 else if (is_baseline)
520 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
521 else
522 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
523 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000524}
525
526
527/*
528 * Write scan header.
529 * This consists of DHT or DAC markers, optional DRI, and SOS.
530 * Compressed data will be written following the SOS.
531 */
532
533METHODDEF void
534write_scan_header (j_compress_ptr cinfo)
535{
536 int i;
537 jpeg_component_info *compptr;
538
539 if (cinfo->arith_code) {
540 /* Emit arith conditioning info. We may have some duplication
541 * if the file has multiple scans, but it's so small it's hardly
542 * worth worrying about.
543 */
544 emit_dac(cinfo);
545 } else {
546 /* Emit Huffman tables.
547 * Note that emit_dht() suppresses any duplicate tables.
548 */
549 for (i = 0; i < cinfo->comps_in_scan; i++) {
550 compptr = cinfo->cur_comp_info[i];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000551 if (cinfo->progressive_mode) {
552 /* Progressive mode: only DC or only AC tables are used in one scan */
553 if (cinfo->Ss == 0) {
554 if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
555 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
556 } else {
557 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
558 }
559 } else {
560 /* Sequential mode: need both DC and AC tables */
561 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
562 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
563 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000564 }
565 }
566
567 /* Emit DRI if required --- note that DRI value could change for each scan.
568 * If it doesn't, a tiny amount of space is wasted in multiple-scan files.
569 * We assume DRI will never be nonzero for one scan and zero for a later one.
570 */
571 if (cinfo->restart_interval)
572 emit_dri(cinfo);
573
574 emit_sos(cinfo);
575}
576
577
578/*
579 * Write datastream trailer.
580 */
581
582METHODDEF void
583write_file_trailer (j_compress_ptr cinfo)
584{
585 emit_marker(cinfo, M_EOI);
586}
587
588
589/*
590 * Write an abbreviated table-specification datastream.
591 * This consists of SOI, DQT and DHT tables, and EOI.
592 * Any table that is defined and not marked sent_table = TRUE will be
593 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
594 */
595
596METHODDEF void
597write_tables_only (j_compress_ptr cinfo)
598{
599 int i;
600
601 emit_marker(cinfo, M_SOI);
602
603 for (i = 0; i < NUM_QUANT_TBLS; i++) {
604 if (cinfo->quant_tbl_ptrs[i] != NULL)
605 (void) emit_dqt(cinfo, i);
606 }
607
608 if (! cinfo->arith_code) {
609 for (i = 0; i < NUM_HUFF_TBLS; i++) {
610 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
611 emit_dht(cinfo, i, FALSE);
612 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
613 emit_dht(cinfo, i, TRUE);
614 }
615 }
616
617 emit_marker(cinfo, M_EOI);
618}
619
620
621/*
622 * Initialize the marker writer module.
623 */
624
625GLOBAL void
626jinit_marker_writer (j_compress_ptr cinfo)
627{
628 /* Create the subobject */
629 cinfo->marker = (struct jpeg_marker_writer *)
630 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
631 SIZEOF(struct jpeg_marker_writer));
632 /* Initialize method pointers */
633 cinfo->marker->write_any_marker = write_any_marker;
634 cinfo->marker->write_file_header = write_file_header;
635 cinfo->marker->write_frame_header = write_frame_header;
636 cinfo->marker->write_scan_header = write_scan_header;
637 cinfo->marker->write_file_trailer = write_file_trailer;
638 cinfo->marker->write_tables_only = write_tables_only;
639}