1 /*
2  * jcmarker.c
3  *
4  * This file was part of the Independent JPEG Group's software:
5  * Copyright (C) 1991-1998, Thomas G. Lane.
6  * Modified 2003-2010 by Guido Vollbeding.
7  * libjpeg-turbo Modifications:
8  * Copyright (C) 2010, D. R. Commander.
9  * For conditions of distribution and use, see the accompanying README.ijg
10  * file.
11  *
12  * This file contains routines to write JPEG datastream markers.
13  */
14 
15 #define JPEG_INTERNALS
16 #include "jinclude.h"
17 #include "jpeglib.h"
18 #include "jpegcomp.h"
19 
20 
21 typedef enum {                  /* JPEG marker codes */
22   M_SOF0  = 0xc0,
23   M_SOF1  = 0xc1,
24   M_SOF2  = 0xc2,
25   M_SOF3  = 0xc3,
26 
27   M_SOF5  = 0xc5,
28   M_SOF6  = 0xc6,
29   M_SOF7  = 0xc7,
30 
31   M_JPG   = 0xc8,
32   M_SOF9  = 0xc9,
33   M_SOF10 = 0xca,
34   M_SOF11 = 0xcb,
35 
36   M_SOF13 = 0xcd,
37   M_SOF14 = 0xce,
38   M_SOF15 = 0xcf,
39 
40   M_DHT   = 0xc4,
41 
42   M_DAC   = 0xcc,
43 
44   M_RST0  = 0xd0,
45   M_RST1  = 0xd1,
46   M_RST2  = 0xd2,
47   M_RST3  = 0xd3,
48   M_RST4  = 0xd4,
49   M_RST5  = 0xd5,
50   M_RST6  = 0xd6,
51   M_RST7  = 0xd7,
52 
53   M_SOI   = 0xd8,
54   M_EOI   = 0xd9,
55   M_SOS   = 0xda,
56   M_DQT   = 0xdb,
57   M_DNL   = 0xdc,
58   M_DRI   = 0xdd,
59   M_DHP   = 0xde,
60   M_EXP   = 0xdf,
61 
62   M_APP0  = 0xe0,
63   M_APP1  = 0xe1,
64   M_APP2  = 0xe2,
65   M_APP3  = 0xe3,
66   M_APP4  = 0xe4,
67   M_APP5  = 0xe5,
68   M_APP6  = 0xe6,
69   M_APP7  = 0xe7,
70   M_APP8  = 0xe8,
71   M_APP9  = 0xe9,
72   M_APP10 = 0xea,
73   M_APP11 = 0xeb,
74   M_APP12 = 0xec,
75   M_APP13 = 0xed,
76   M_APP14 = 0xee,
77   M_APP15 = 0xef,
78 
79   M_JPG0  = 0xf0,
80   M_JPG13 = 0xfd,
81   M_COM   = 0xfe,
82 
83   M_TEM   = 0x01,
84 
85   M_ERROR = 0x100
86 } JPEG_MARKER;
87 
88 
89 /* Private state */
90 
91 typedef struct {
92   struct jpeg_marker_writer pub; /* public fields */
93 
94   unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
95 } my_marker_writer;
96 
97 typedef my_marker_writer *my_marker_ptr;
98 
99 
100 /*
101  * Basic output routines.
102  *
103  * Note that we do not support suspension while writing a marker.
104  * Therefore, an application using suspension must ensure that there is
105  * enough buffer space for the initial markers (typ. 600-700 bytes) before
106  * calling jpeg_start_compress, and enough space to write the trailing EOI
107  * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
108  * modes are not supported at all with suspension, so those two are the only
109  * points where markers will be written.
110  */
111 
112 LOCAL(void)
emit_byte(j_compress_ptr cinfo,int val)113 emit_byte(j_compress_ptr cinfo, int val)
114 /* Emit a byte */
115 {
116   struct jpeg_destination_mgr *dest = cinfo->dest;
117 
118   *(dest->next_output_byte)++ = (JOCTET)val;
119   if (--dest->free_in_buffer == 0) {
120     if (!(*dest->empty_output_buffer) (cinfo))
121       ERREXIT(cinfo, JERR_CANT_SUSPEND);
122   }
123 }
124 
125 
126 LOCAL(void)
emit_marker(j_compress_ptr cinfo,JPEG_MARKER mark)127 emit_marker(j_compress_ptr cinfo, JPEG_MARKER mark)
128 /* Emit a marker code */
129 {
130   emit_byte(cinfo, 0xFF);
131   emit_byte(cinfo, (int)mark);
132 }
133 
134 
135 LOCAL(void)
emit_2bytes(j_compress_ptr cinfo,int value)136 emit_2bytes(j_compress_ptr cinfo, int value)
137 /* Emit a 2-byte integer; these are always MSB first in JPEG files */
138 {
139   emit_byte(cinfo, (value >> 8) & 0xFF);
140   emit_byte(cinfo, value & 0xFF);
141 }
142 
143 
144 /*
145  * Routines to write specific marker types.
146  */
147 
148 LOCAL(int)
emit_dqt(j_compress_ptr cinfo,int index)149 emit_dqt(j_compress_ptr cinfo, int index)
150 /* Emit a DQT marker */
151 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
152 {
153   JQUANT_TBL *qtbl = cinfo->quant_tbl_ptrs[index];
154   int prec;
155   int i;
156 
157   if (qtbl == NULL)
158     ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
159 
160   prec = 0;
161   for (i = 0; i < DCTSIZE2; i++) {
162     if (qtbl->quantval[i] > 255)
163       prec = 1;
164   }
165 
166   if (!qtbl->sent_table) {
167     emit_marker(cinfo, M_DQT);
168 
169     emit_2bytes(cinfo, prec ? DCTSIZE2 * 2 + 1 + 2 : DCTSIZE2 + 1 + 2);
170 
171     emit_byte(cinfo, index + (prec << 4));
172 
173     for (i = 0; i < DCTSIZE2; i++) {
174       /* The table entries must be emitted in zigzag order. */
175       unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
176       if (prec)
177         emit_byte(cinfo, (int)(qval >> 8));
178       emit_byte(cinfo, (int)(qval & 0xFF));
179     }
180 
181     qtbl->sent_table = TRUE;
182   }
183 
184   return prec;
185 }
186 
187 LOCAL(int)
emit_multi_dqt(j_compress_ptr cinfo)188 emit_multi_dqt (j_compress_ptr cinfo)
189 /* Emits a DQT marker containing all quantization tables */
190 /* Returns number of emitted 16-bit tables, or -1 for failed for baseline checking. */
191 {
192   int prec[MAX_COMPONENTS];
193   int seen[MAX_COMPONENTS] = { 0 };
194   int fin_prec = 0;
195   int ci;
196   int size = 0;
197 
198   if (cinfo->master->compress_profile == JCP_FASTEST)
199     return -1;
200 
201   for (ci = 0; ci < cinfo->num_components; ci++) {
202     int tbl_num = cinfo->comp_info[ci].quant_tbl_no;
203     int i;
204     JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[tbl_num];
205 
206     if (qtbl == NULL || qtbl->sent_table == TRUE)
207       return -1;
208 
209     prec[ci] = 0;
210     for (i = 0; i < DCTSIZE2; i++)
211       prec[ci] = !!(prec[ci] + (qtbl->quantval[i] > 255));
212 
213     fin_prec += prec[ci];
214   }
215 
216   emit_marker(cinfo, M_DQT);
217 
218   for (ci = 0; ci < cinfo->num_components; ci++) {
219     int tbl_num = cinfo->comp_info[ci].quant_tbl_no;
220 
221     if (!seen[tbl_num]) {
222       size += DCTSIZE2 * (prec[ci] + 1) + 1;
223       seen[tbl_num] = 1;
224     }
225   }
226   size += 2;
227 
228   emit_2bytes(cinfo, size);
229 
230   for (ci = 0; ci < cinfo->num_components; ci++) {
231     int tbl_num = cinfo->comp_info[ci].quant_tbl_no;
232     int i;
233     JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[tbl_num];
234 
235     if (qtbl->sent_table == TRUE)
236         continue;
237 
238     emit_byte(cinfo, tbl_num + (prec[ci] << 4));
239 
240     for (i = 0; i < DCTSIZE2; i++) {
241       unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
242 
243       if (prec[ci])
244         emit_byte(cinfo, (int) (qval >> 8));
245       emit_byte(cinfo, (int) (qval & 0xFF));
246     }
247 
248     qtbl->sent_table = TRUE;
249   }
250 
251   return fin_prec;
252 }
253 
254 LOCAL(void)
emit_dht(j_compress_ptr cinfo,int index,boolean is_ac)255 emit_dht(j_compress_ptr cinfo, int index, boolean is_ac)
256 /* Emit a DHT marker */
257 {
258   JHUFF_TBL *htbl;
259   int length, i;
260 
261   if (is_ac) {
262     htbl = cinfo->ac_huff_tbl_ptrs[index];
263     index += 0x10;              /* output index has AC bit set */
264   } else {
265     htbl = cinfo->dc_huff_tbl_ptrs[index];
266   }
267 
268   if (htbl == NULL)
269     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
270 
271   if (!htbl->sent_table) {
272     emit_marker(cinfo, M_DHT);
273 
274     length = 0;
275     for (i = 1; i <= 16; i++)
276       length += htbl->bits[i];
277 
278     emit_2bytes(cinfo, length + 2 + 1 + 16);
279     emit_byte(cinfo, index);
280 
281     for (i = 1; i <= 16; i++)
282       emit_byte(cinfo, htbl->bits[i]);
283 
284     for (i = 0; i < length; i++)
285       emit_byte(cinfo, htbl->huffval[i]);
286 
287     htbl->sent_table = TRUE;
288   }
289 }
290 
291 LOCAL(boolean)
emit_multi_dht(j_compress_ptr cinfo)292 emit_multi_dht (j_compress_ptr cinfo)
293 /* Emit all DHT markers */
294 /* Returns FALSE on failure, TRUE otherwise. */
295 {
296   int i, j;
297   int length = 2;
298   int dclens[NUM_HUFF_TBLS] = { 0 };
299   int aclens[NUM_HUFF_TBLS] = { 0 };
300   JHUFF_TBL *dcseen[NUM_HUFF_TBLS] = { NULL };
301   JHUFF_TBL *acseen[NUM_HUFF_TBLS] = { NULL };
302 
303   if (cinfo->master->compress_profile == JCP_FASTEST)
304     return 0;
305 
306   /* Calclate the total length. */
307   for (i = 0; i < cinfo->comps_in_scan; i++) {
308     jpeg_component_info *compptr = cinfo->cur_comp_info[i];
309     int dcidx = compptr->dc_tbl_no;
310     int acidx = compptr->ac_tbl_no;
311     JHUFF_TBL *dctbl = cinfo->dc_huff_tbl_ptrs[dcidx];
312     JHUFF_TBL *actbl = cinfo->ac_huff_tbl_ptrs[acidx];
313     int seen = 0;
314 
315     /* Handle DC table lenghts */
316     if (cinfo->Ss == 0 && cinfo->Ah == 0) {
317       if (dctbl == NULL)
318         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dcidx);
319 
320       if (dctbl->sent_table)
321           continue;
322 
323       for (j = 0; j < NUM_HUFF_TBLS; j++)
324           seen += (dctbl == dcseen[j]);
325       if (seen)
326           continue;
327       dcseen[i] = dctbl;
328 
329       for (j = 1; j <= 16; j++)
330         dclens[i] += dctbl->bits[j];
331       length += dclens[i] + 16 + 1;
332     }
333 
334     /* Handle AC table lengths */
335     if (cinfo->Se) {
336       if (actbl == NULL)
337         ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, acidx + 0x10);
338 
339       if (actbl->sent_table)
340           continue;
341 
342       seen = 0;
343       for (j = 0; j < NUM_HUFF_TBLS; j++)
344           seen += (actbl == acseen[j]);
345       if (seen)
346           continue;
347       acseen[i] = actbl;
348 
349       for (j = 1; j <= 16; j++)
350         aclens[i] += actbl->bits[j];
351       length += aclens[i] + 16 + 1;
352 
353     }
354   }
355 
356   /* Make sure we can fit it all into one DHT marker */
357   if (length > (1 << 16) - 1)
358     return FALSE;
359 
360   emit_marker(cinfo, M_DHT);
361   emit_2bytes(cinfo, length);
362 
363   for (i = 0; i < cinfo->comps_in_scan; i++) {
364     jpeg_component_info *compptr = cinfo->cur_comp_info[i];
365     int dcidx = compptr->dc_tbl_no;
366     int acidx = compptr->ac_tbl_no;
367     JHUFF_TBL *dctbl = cinfo->dc_huff_tbl_ptrs[dcidx];
368     JHUFF_TBL *actbl = cinfo->ac_huff_tbl_ptrs[acidx];
369 
370     acidx += 0x10;
371 
372     /* DC */
373     if (cinfo->Ss == 0 && cinfo->Ah == 0 && !dctbl->sent_table) {
374       emit_byte(cinfo, dcidx);
375 
376       for (j = 1; j <= 16; j++)
377         emit_byte(cinfo, dctbl->bits[j]);
378 
379       for (j = 0; j < dclens[i]; j++)
380         emit_byte(cinfo, dctbl->huffval[j]);
381 
382       dctbl->sent_table = TRUE;
383     }
384 
385     if (cinfo->Se && !actbl->sent_table) {
386       emit_byte(cinfo, acidx);
387 
388       for (j = 1; j <= 16; j++)
389         emit_byte(cinfo, actbl->bits[j]);
390 
391       for (j = 0; j < aclens[i]; j++)
392         emit_byte(cinfo, actbl->huffval[j]);
393 
394       actbl->sent_table = TRUE;
395     }
396   }
397 
398   return TRUE;
399 }
400 
401 LOCAL(void)
emit_dac(j_compress_ptr cinfo)402 emit_dac(j_compress_ptr cinfo)
403 /* Emit a DAC marker */
404 /* Since the useful info is so small, we want to emit all the tables in */
405 /* one DAC marker.  Therefore this routine does its own scan of the table. */
406 {
407 #ifdef C_ARITH_CODING_SUPPORTED
408   char dc_in_use[NUM_ARITH_TBLS];
409   char ac_in_use[NUM_ARITH_TBLS];
410   int length, i;
411   jpeg_component_info *compptr;
412 
413   for (i = 0; i < NUM_ARITH_TBLS; i++)
414     dc_in_use[i] = ac_in_use[i] = 0;
415 
416   for (i = 0; i < cinfo->comps_in_scan; i++) {
417     compptr = cinfo->cur_comp_info[i];
418     /* DC needs no table for refinement scan */
419     if (cinfo->Ss == 0 && cinfo->Ah == 0)
420       dc_in_use[compptr->dc_tbl_no] = 1;
421     /* AC needs no table when not present */
422     if (cinfo->Se)
423       ac_in_use[compptr->ac_tbl_no] = 1;
424   }
425 
426   length = 0;
427   for (i = 0; i < NUM_ARITH_TBLS; i++)
428     length += dc_in_use[i] + ac_in_use[i];
429 
430   if (length) {
431     emit_marker(cinfo, M_DAC);
432 
433     emit_2bytes(cinfo, length * 2 + 2);
434 
435     for (i = 0; i < NUM_ARITH_TBLS; i++) {
436       if (dc_in_use[i]) {
437         emit_byte(cinfo, i);
438         emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i] << 4));
439       }
440       if (ac_in_use[i]) {
441         emit_byte(cinfo, i + 0x10);
442         emit_byte(cinfo, cinfo->arith_ac_K[i]);
443       }
444     }
445   }
446 #endif /* C_ARITH_CODING_SUPPORTED */
447 }
448 
449 
450 LOCAL(void)
emit_dri(j_compress_ptr cinfo)451 emit_dri(j_compress_ptr cinfo)
452 /* Emit a DRI marker */
453 {
454   emit_marker(cinfo, M_DRI);
455 
456   emit_2bytes(cinfo, 4);        /* fixed length */
457 
458   emit_2bytes(cinfo, (int)cinfo->restart_interval);
459 }
460 
461 
462 LOCAL(void)
emit_sof(j_compress_ptr cinfo,JPEG_MARKER code)463 emit_sof(j_compress_ptr cinfo, JPEG_MARKER code)
464 /* Emit a SOF marker */
465 {
466   int ci;
467   jpeg_component_info *compptr;
468 
469   emit_marker(cinfo, code);
470 
471   emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
472 
473   /* Make sure image isn't bigger than SOF field can handle */
474   if ((long)cinfo->_jpeg_height > 65535L || (long)cinfo->_jpeg_width > 65535L)
475     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)65535);
476 
477   emit_byte(cinfo, cinfo->data_precision);
478   emit_2bytes(cinfo, (int)cinfo->_jpeg_height);
479   emit_2bytes(cinfo, (int)cinfo->_jpeg_width);
480 
481   emit_byte(cinfo, cinfo->num_components);
482 
483   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
484        ci++, compptr++) {
485     emit_byte(cinfo, compptr->component_id);
486     emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
487     emit_byte(cinfo, compptr->quant_tbl_no);
488   }
489 }
490 
491 
492 LOCAL(void)
emit_sos(j_compress_ptr cinfo)493 emit_sos(j_compress_ptr cinfo)
494 /* Emit a SOS marker */
495 {
496   int i, td, ta;
497   jpeg_component_info *compptr;
498 
499   emit_marker(cinfo, M_SOS);
500 
501   emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
502 
503   emit_byte(cinfo, cinfo->comps_in_scan);
504 
505   for (i = 0; i < cinfo->comps_in_scan; i++) {
506     compptr = cinfo->cur_comp_info[i];
507     emit_byte(cinfo, compptr->component_id);
508 
509     /* We emit 0 for unused field(s); this is recommended by the P&M text
510      * but does not seem to be specified in the standard.
511      */
512 
513     /* DC needs no table for refinement scan */
514     td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
515     /* AC needs no table when not present */
516     ta = cinfo->Se ? compptr->ac_tbl_no : 0;
517 
518     emit_byte(cinfo, (td << 4) + ta);
519   }
520 
521   emit_byte(cinfo, cinfo->Ss);
522   emit_byte(cinfo, cinfo->Se);
523   emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
524 }
525 
526 
527 LOCAL(void)
emit_jfif_app0(j_compress_ptr cinfo)528 emit_jfif_app0(j_compress_ptr cinfo)
529 /* Emit a JFIF-compliant APP0 marker */
530 {
531   /*
532    * Length of APP0 block       (2 bytes)
533    * Block ID                   (4 bytes - ASCII "JFIF")
534    * Zero byte                  (1 byte to terminate the ID string)
535    * Version Major, Minor       (2 bytes - major first)
536    * Units                      (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
537    * Xdpu                       (2 bytes - dots per unit horizontal)
538    * Ydpu                       (2 bytes - dots per unit vertical)
539    * Thumbnail X size           (1 byte)
540    * Thumbnail Y size           (1 byte)
541    */
542 
543   emit_marker(cinfo, M_APP0);
544 
545   emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
546 
547   emit_byte(cinfo, 0x4A);       /* Identifier: ASCII "JFIF" */
548   emit_byte(cinfo, 0x46);
549   emit_byte(cinfo, 0x49);
550   emit_byte(cinfo, 0x46);
551   emit_byte(cinfo, 0);
552   emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
553   emit_byte(cinfo, cinfo->JFIF_minor_version);
554   emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
555   emit_2bytes(cinfo, (int)cinfo->X_density);
556   emit_2bytes(cinfo, (int)cinfo->Y_density);
557   emit_byte(cinfo, 0);          /* No thumbnail image */
558   emit_byte(cinfo, 0);
559 }
560 
561 
562 LOCAL(void)
emit_adobe_app14(j_compress_ptr cinfo)563 emit_adobe_app14(j_compress_ptr cinfo)
564 /* Emit an Adobe APP14 marker */
565 {
566   /*
567    * Length of APP14 block      (2 bytes)
568    * Block ID                   (5 bytes - ASCII "Adobe")
569    * Version Number             (2 bytes - currently 100)
570    * Flags0                     (2 bytes - currently 0)
571    * Flags1                     (2 bytes - currently 0)
572    * Color transform            (1 byte)
573    *
574    * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
575    * now in circulation seem to use Version = 100, so that's what we write.
576    *
577    * We write the color transform byte as 1 if the JPEG color space is
578    * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
579    * whether the encoder performed a transformation, which is pretty useless.
580    */
581 
582   emit_marker(cinfo, M_APP14);
583 
584   emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
585 
586   emit_byte(cinfo, 0x41);       /* Identifier: ASCII "Adobe" */
587   emit_byte(cinfo, 0x64);
588   emit_byte(cinfo, 0x6F);
589   emit_byte(cinfo, 0x62);
590   emit_byte(cinfo, 0x65);
591   emit_2bytes(cinfo, 100);      /* Version */
592   emit_2bytes(cinfo, 0);        /* Flags0 */
593   emit_2bytes(cinfo, 0);        /* Flags1 */
594   switch (cinfo->jpeg_color_space) {
595   case JCS_YCbCr:
596     emit_byte(cinfo, 1);        /* Color transform = 1 */
597     break;
598   case JCS_YCCK:
599     emit_byte(cinfo, 2);        /* Color transform = 2 */
600     break;
601   default:
602     emit_byte(cinfo, 0);        /* Color transform = 0 */
603     break;
604   }
605 }
606 
607 
608 /*
609  * These routines allow writing an arbitrary marker with parameters.
610  * The only intended use is to emit COM or APPn markers after calling
611  * write_file_header and before calling write_frame_header.
612  * Other uses are not guaranteed to produce desirable results.
613  * Counting the parameter bytes properly is the caller's responsibility.
614  */
615 
616 METHODDEF(void)
write_marker_header(j_compress_ptr cinfo,int marker,unsigned int datalen)617 write_marker_header(j_compress_ptr cinfo, int marker, unsigned int datalen)
618 /* Emit an arbitrary marker header */
619 {
620   if (datalen > (unsigned int)65533)            /* safety check */
621     ERREXIT(cinfo, JERR_BAD_LENGTH);
622 
623   emit_marker(cinfo, (JPEG_MARKER)marker);
624 
625   emit_2bytes(cinfo, (int)(datalen + 2));       /* total length */
626 }
627 
628 METHODDEF(void)
write_marker_byte(j_compress_ptr cinfo,int val)629 write_marker_byte(j_compress_ptr cinfo, int val)
630 /* Emit one byte of marker parameters following write_marker_header */
631 {
632   emit_byte(cinfo, val);
633 }
634 
635 
636 /*
637  * Write datastream header.
638  * This consists of an SOI and optional APPn markers.
639  * We recommend use of the JFIF marker, but not the Adobe marker,
640  * when using YCbCr or grayscale data.  The JFIF marker should NOT
641  * be used for any other JPEG colorspace.  The Adobe marker is helpful
642  * to distinguish RGB, CMYK, and YCCK colorspaces.
643  * Note that an application can write additional header markers after
644  * jpeg_start_compress returns.
645  */
646 
647 METHODDEF(void)
write_file_header(j_compress_ptr cinfo)648 write_file_header(j_compress_ptr cinfo)
649 {
650   my_marker_ptr marker = (my_marker_ptr)cinfo->marker;
651 
652   emit_marker(cinfo, M_SOI);    /* first the SOI */
653 
654   /* SOI is defined to reset restart interval to 0 */
655   marker->last_restart_interval = 0;
656 
657   if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
658     emit_jfif_app0(cinfo);
659   if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
660     emit_adobe_app14(cinfo);
661 }
662 
663 
664 /*
665  * Write frame header.
666  * This consists of DQT and SOFn markers.
667  * Note that we do not emit the SOF until we have emitted the DQT(s).
668  * This avoids compatibility problems with incorrect implementations that
669  * try to error-check the quant table numbers as soon as they see the SOF.
670  */
671 
672 METHODDEF(void)
write_frame_header(j_compress_ptr cinfo)673 write_frame_header(j_compress_ptr cinfo)
674 {
675   int ci, prec;
676   boolean is_baseline;
677   jpeg_component_info *compptr;
678 
679   /* Emit DQT for each quantization table.
680    * Note that emit_dqt() suppresses any duplicate tables.
681    */
682   prec = emit_multi_dqt(cinfo);
683   if (prec == -1) {
684   prec = 0;
685   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
686        ci++, compptr++) {
687     prec += emit_dqt(cinfo, compptr->quant_tbl_no);
688   }
689   }
690   /* now prec is nonzero iff there are any 16-bit quant tables. */
691 
692   /* Check for a non-baseline specification.
693    * Note we assume that Huffman table numbers won't be changed later.
694    */
695   if (cinfo->arith_code || cinfo->progressive_mode ||
696       cinfo->data_precision != 8) {
697     is_baseline = FALSE;
698   } else {
699     is_baseline = TRUE;
700     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
701          ci++, compptr++) {
702       if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
703         is_baseline = FALSE;
704     }
705     if (prec && is_baseline) {
706       is_baseline = FALSE;
707       /* If it's baseline except for quantizer size, warn the user */
708       TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
709     }
710   }
711 
712   /* Emit the proper SOF marker */
713   if (cinfo->arith_code) {
714     if (cinfo->progressive_mode)
715       emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
716     else
717       emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
718   } else {
719     if (cinfo->progressive_mode)
720       emit_sof(cinfo, M_SOF2);  /* SOF code for progressive Huffman */
721     else if (is_baseline)
722       emit_sof(cinfo, M_SOF0);  /* SOF code for baseline implementation */
723     else
724       emit_sof(cinfo, M_SOF1);  /* SOF code for non-baseline Huffman file */
725   }
726 }
727 
728 
729 /*
730  * Write scan header.
731  * This consists of DHT or DAC markers, optional DRI, and SOS.
732  * Compressed data will be written following the SOS.
733  */
734 
735 METHODDEF(void)
write_scan_header(j_compress_ptr cinfo)736 write_scan_header(j_compress_ptr cinfo)
737 {
738   my_marker_ptr marker = (my_marker_ptr)cinfo->marker;
739   int i;
740   jpeg_component_info *compptr;
741 
742   if (cinfo->arith_code) {
743     /* Emit arith conditioning info.  We may have some duplication
744      * if the file has multiple scans, but it's so small it's hardly
745      * worth worrying about.
746      */
747     emit_dac(cinfo);
748   } else {
749     /* Emit Huffman tables.
750      * Note that emit_dht() suppresses any duplicate tables.
751      */
752     if (!emit_multi_dht(cinfo)) {
753     for (i = 0; i < cinfo->comps_in_scan; i++) {
754       compptr = cinfo->cur_comp_info[i];
755       /* DC needs no table for refinement scan */
756       if (cinfo->Ss == 0 && cinfo->Ah == 0)
757         emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
758       /* AC needs no table when not present */
759       if (cinfo->Se)
760         emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
761     }
762   }
763   }
764 
765   /* Emit DRI if required --- note that DRI value could change for each scan.
766    * We avoid wasting space with unnecessary DRIs, however.
767    */
768   if (cinfo->restart_interval != marker->last_restart_interval) {
769     emit_dri(cinfo);
770     marker->last_restart_interval = cinfo->restart_interval;
771   }
772 
773   emit_sos(cinfo);
774 }
775 
776 
777 /*
778  * Write datastream trailer.
779  */
780 
781 METHODDEF(void)
write_file_trailer(j_compress_ptr cinfo)782 write_file_trailer(j_compress_ptr cinfo)
783 {
784   emit_marker(cinfo, M_EOI);
785 }
786 
787 
788 /*
789  * Write an abbreviated table-specification datastream.
790  * This consists of SOI, DQT and DHT tables, and EOI.
791  * Any table that is defined and not marked sent_table = TRUE will be
792  * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
793  */
794 
795 METHODDEF(void)
write_tables_only(j_compress_ptr cinfo)796 write_tables_only(j_compress_ptr cinfo)
797 {
798   int i;
799 
800   emit_marker(cinfo, M_SOI);
801 
802   for (i = 0; i < NUM_QUANT_TBLS; i++) {
803     if (cinfo->quant_tbl_ptrs[i] != NULL)
804       (void)emit_dqt(cinfo, i);
805   }
806 
807   if (!cinfo->arith_code) {
808     for (i = 0; i < NUM_HUFF_TBLS; i++) {
809       if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
810         emit_dht(cinfo, i, FALSE);
811       if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
812         emit_dht(cinfo, i, TRUE);
813     }
814   }
815 
816   emit_marker(cinfo, M_EOI);
817 }
818 
819 
820 /*
821  * Initialize the marker writer module.
822  */
823 
824 GLOBAL(void)
jinit_marker_writer(j_compress_ptr cinfo)825 jinit_marker_writer(j_compress_ptr cinfo)
826 {
827   my_marker_ptr marker;
828 
829   /* Create the subobject */
830   marker = (my_marker_ptr)
831     (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
832                                 sizeof(my_marker_writer));
833   cinfo->marker = (struct jpeg_marker_writer *)marker;
834   /* Initialize method pointers */
835   marker->pub.write_file_header = write_file_header;
836   marker->pub.write_frame_header = write_frame_header;
837   marker->pub.write_scan_header = write_scan_header;
838   marker->pub.write_file_trailer = write_file_trailer;
839   marker->pub.write_tables_only = write_tables_only;
840   marker->pub.write_marker_header = write_marker_header;
841   marker->pub.write_marker_byte = write_marker_byte;
842   /* Initialize private state */
843   marker->last_restart_interval = 0;
844 }
845