1 /*
2  * jcmarker.c
3  *
4  * Copyright (C) 1991-1994, 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.
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 
16 typedef 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 
96 LOCAL void
emit_byte(j_compress_ptr cinfo,int val)97 emit_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 
110 LOCAL void
emit_marker(j_compress_ptr cinfo,JPEG_MARKER mark)111 emit_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 
119 LOCAL void
emit_2bytes(j_compress_ptr cinfo,int value)120 emit_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 
132 LOCAL int
emit_dqt(j_compress_ptr cinfo,int index)133 emit_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 
170 LOCAL void
emit_dht(j_compress_ptr cinfo,int index,boolean is_ac)171 emit_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 
208 LOCAL void
emit_dac(j_compress_ptr cinfo)209 emit_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 
251 LOCAL void
emit_dri(j_compress_ptr cinfo)252 emit_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 
263 LOCAL void
emit_sof(j_compress_ptr cinfo,JPEG_MARKER code)264 emit_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 
294 LOCAL void
emit_sos(j_compress_ptr cinfo)295 emit_sos (j_compress_ptr cinfo)
296 /* Emit a SOS marker */
297 {
298   int i;
299   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);
310     emit_byte(cinfo, (compptr->dc_tbl_no << 4) + compptr->ac_tbl_no);
311   }
312 
313   emit_byte(cinfo, 0);		/* Spectral selection start */
314   emit_byte(cinfo, DCTSIZE2-1);	/* Spectral selection end */
315   emit_byte(cinfo, 0);		/* Successive approximation */
316 }
317 
318 
319 LOCAL void
emit_jfif_app0(j_compress_ptr cinfo)320 emit_jfif_app0 (j_compress_ptr cinfo)
321 /* Emit a JFIF-compliant APP0 marker */
322 {
323   /*
324    * Length of APP0 block	(2 bytes)
325    * Block ID			(4 bytes - ASCII "JFIF")
326    * Zero byte			(1 byte to terminate the ID string)
327    * Version Major, Minor	(2 bytes - 0x01, 0x01)
328    * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
329    * Xdpu			(2 bytes - dots per unit horizontal)
330    * Ydpu			(2 bytes - dots per unit vertical)
331    * Thumbnail X size		(1 byte)
332    * Thumbnail Y size		(1 byte)
333    */
334 
335   emit_marker(cinfo, M_APP0);
336 
337   emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
338 
339   emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
340   emit_byte(cinfo, 0x46);
341   emit_byte(cinfo, 0x49);
342   emit_byte(cinfo, 0x46);
343   emit_byte(cinfo, 0);
344   /* We currently emit version code 1.01 since we use no 1.02 features.
345    * This may avoid complaints from some older decoders.
346    */
347   emit_byte(cinfo, 1);		/* Major version */
348   emit_byte(cinfo, 1);		/* Minor version */
349   emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
350   emit_2bytes(cinfo, (int) cinfo->X_density);
351   emit_2bytes(cinfo, (int) cinfo->Y_density);
352   emit_byte(cinfo, 0);		/* No thumbnail image */
353   emit_byte(cinfo, 0);
354 }
355 
356 
357 LOCAL void
emit_adobe_app14(j_compress_ptr cinfo)358 emit_adobe_app14 (j_compress_ptr cinfo)
359 /* Emit an Adobe APP14 marker */
360 {
361   /*
362    * Length of APP14 block	(2 bytes)
363    * Block ID			(5 bytes - ASCII "Adobe")
364    * Version Number		(2 bytes - currently 100)
365    * Flags0			(2 bytes - currently 0)
366    * Flags1			(2 bytes - currently 0)
367    * Color transform		(1 byte)
368    *
369    * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
370    * now in circulation seem to use Version = 100, so that's what we write.
371    *
372    * We write the color transform byte as 1 if the JPEG color space is
373    * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
374    * whether the encoder performed a transformation, which is pretty useless.
375    */
376 
377   emit_marker(cinfo, M_APP14);
378 
379   emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
380 
381   emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
382   emit_byte(cinfo, 0x64);
383   emit_byte(cinfo, 0x6F);
384   emit_byte(cinfo, 0x62);
385   emit_byte(cinfo, 0x65);
386   emit_2bytes(cinfo, 100);	/* Version */
387   emit_2bytes(cinfo, 0);	/* Flags0 */
388   emit_2bytes(cinfo, 0);	/* Flags1 */
389   switch (cinfo->jpeg_color_space) {
390   case JCS_YCbCr:
391     emit_byte(cinfo, 1);	/* Color transform = 1 */
392     break;
393   case JCS_YCCK:
394     emit_byte(cinfo, 2);	/* Color transform = 2 */
395     break;
396   default:
397     emit_byte(cinfo, 0);	/* Color transform = 0 */
398     break;
399   }
400 }
401 
402 
403 /*
404  * This routine is exported for possible use by applications.
405  * The intended use is to emit COM or APPn markers after calling
406  * jpeg_start_compress() and before the first jpeg_write_scanlines() call
407  * (hence, after write_file_header but before write_frame_header).
408  * Other uses are not guaranteed to produce desirable results.
409  */
410 
411 METHODDEF void
write_any_marker(j_compress_ptr cinfo,int marker,const JOCTET * dataptr,unsigned int datalen)412 write_any_marker (j_compress_ptr cinfo, int marker,
413 		  const JOCTET *dataptr, unsigned int datalen)
414 /* Emit an arbitrary marker with parameters */
415 {
416   if (datalen <= (unsigned int) 65533) { /* safety check */
417     emit_marker(cinfo, (JPEG_MARKER) marker);
418 
419     emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
420 
421     while (datalen--) {
422       emit_byte(cinfo, *dataptr);
423       dataptr++;
424     }
425   }
426 }
427 
428 
429 /*
430  * Write datastream header.
431  * This consists of an SOI and optional APPn markers.
432  * We recommend use of the JFIF marker, but not the Adobe marker,
433  * when using YCbCr or grayscale data.  The JFIF marker should NOT
434  * be used for any other JPEG colorspace.  The Adobe marker is helpful
435  * to distinguish RGB, CMYK, and YCCK colorspaces.
436  * Note that an application can write additional header markers after
437  * jpeg_start_decompress returns.
438  */
439 
440 METHODDEF void
write_file_header(j_compress_ptr cinfo)441 write_file_header (j_compress_ptr cinfo)
442 {
443   emit_marker(cinfo, M_SOI);	/* first the SOI */
444 
445   if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
446     emit_jfif_app0(cinfo);
447   if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
448     emit_adobe_app14(cinfo);
449 }
450 
451 
452 /*
453  * Write frame header.
454  * This consists of DQT and SOFn markers.
455  * Note that we do not emit the SOF until we have emitted the DQT(s).
456  * This avoids compatibility problems with incorrect implementations that
457  * try to error-check the quant table numbers as soon as they see the SOF.
458  */
459 
460 METHODDEF void
write_frame_header(j_compress_ptr cinfo)461 write_frame_header (j_compress_ptr cinfo)
462 {
463   int ci, prec;
464   boolean is_baseline;
465   jpeg_component_info *compptr;
466 
467   /* Emit DQT for each quantization table.
468    * Note that emit_dqt() suppresses any duplicate tables.
469    */
470   prec = 0;
471   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
472        ci++, compptr++) {
473     prec += emit_dqt(cinfo, compptr->quant_tbl_no);
474   }
475   /* now prec is nonzero iff there are any 16-bit quant tables. */
476 
477   /* Check for a non-baseline specification.
478    * Note we assume that Huffman table numbers won't be changed later.
479    */
480   is_baseline = TRUE;
481   if (cinfo->arith_code || (cinfo->data_precision != 8))
482     is_baseline = FALSE;
483   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
484        ci++, compptr++) {
485     if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
486       is_baseline = FALSE;
487   }
488   if (prec && is_baseline) {
489     is_baseline = FALSE;
490     /* If it's baseline except for quantizer size, warn the user */
491     TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
492   }
493 
494   /* Emit the proper SOF marker */
495   if (cinfo->arith_code)
496     emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
497   else if (is_baseline)
498     emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
499   else
500     emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
501 }
502 
503 
504 /*
505  * Write scan header.
506  * This consists of DHT or DAC markers, optional DRI, and SOS.
507  * Compressed data will be written following the SOS.
508  */
509 
510 METHODDEF void
write_scan_header(j_compress_ptr cinfo)511 write_scan_header (j_compress_ptr cinfo)
512 {
513   int i;
514   jpeg_component_info *compptr;
515 
516   if (cinfo->arith_code) {
517     /* Emit arith conditioning info.  We may have some duplication
518      * if the file has multiple scans, but it's so small it's hardly
519      * worth worrying about.
520      */
521     emit_dac(cinfo);
522   } else {
523     /* Emit Huffman tables.
524      * Note that emit_dht() suppresses any duplicate tables.
525      */
526     for (i = 0; i < cinfo->comps_in_scan; i++) {
527       compptr = cinfo->cur_comp_info[i];
528       emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
529       emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
530     }
531   }
532 
533   /* Emit DRI if required --- note that DRI value could change for each scan.
534    * If it doesn't, a tiny amount of space is wasted in multiple-scan files.
535    * We assume DRI will never be nonzero for one scan and zero for a later one.
536    */
537   if (cinfo->restart_interval)
538     emit_dri(cinfo);
539 
540   emit_sos(cinfo);
541 }
542 
543 
544 /*
545  * Write datastream trailer.
546  */
547 
548 METHODDEF void
write_file_trailer(j_compress_ptr cinfo)549 write_file_trailer (j_compress_ptr cinfo)
550 {
551   emit_marker(cinfo, M_EOI);
552 }
553 
554 
555 /*
556  * Write an abbreviated table-specification datastream.
557  * This consists of SOI, DQT and DHT tables, and EOI.
558  * Any table that is defined and not marked sent_table = TRUE will be
559  * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
560  */
561 
562 METHODDEF void
write_tables_only(j_compress_ptr cinfo)563 write_tables_only (j_compress_ptr cinfo)
564 {
565   int i;
566 
567   emit_marker(cinfo, M_SOI);
568 
569   for (i = 0; i < NUM_QUANT_TBLS; i++) {
570     if (cinfo->quant_tbl_ptrs[i] != NULL)
571       (void) emit_dqt(cinfo, i);
572   }
573 
574   if (! cinfo->arith_code) {
575     for (i = 0; i < NUM_HUFF_TBLS; i++) {
576       if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
577 	emit_dht(cinfo, i, FALSE);
578       if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
579 	emit_dht(cinfo, i, TRUE);
580     }
581   }
582 
583   emit_marker(cinfo, M_EOI);
584 }
585 
586 
587 /*
588  * Initialize the marker writer module.
589  */
590 
591 GLOBAL void
jinit_marker_writer(j_compress_ptr cinfo)592 jinit_marker_writer (j_compress_ptr cinfo)
593 {
594   /* Create the subobject */
595   cinfo->marker = (struct jpeg_marker_writer *)
596     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
597 				SIZEOF(struct jpeg_marker_writer));
598   /* Initialize method pointers */
599   cinfo->marker->write_any_marker = write_any_marker;
600   cinfo->marker->write_file_header = write_file_header;
601   cinfo->marker->write_frame_header = write_frame_header;
602   cinfo->marker->write_scan_header = write_scan_header;
603   cinfo->marker->write_file_trailer = write_file_trailer;
604   cinfo->marker->write_tables_only = write_tables_only;
605 }
606