fpdfapi_jcmarker.c revision ee451cb395940862dad63c85adfe8f2fd55e864c
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if !defined(_FX_JPEG_TURBO_)
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * jcmarker.c
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 1991-1998, Thomas G. Lane.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This file is part of the Independent JPEG Group's software.
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * For conditions of distribution and use, see the accompanying README file.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) * This file contains routines to write JPEG datastream markers.
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch */
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define JPEG_INTERNALS
13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "jinclude.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jpeglib.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef enum {			/* JPEG marker codes */
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  M_SOF0  = 0xc0,
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  M_SOF1  = 0xc1,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  M_SOF2  = 0xc2,
21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_SOF3  = 0xc3,
22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  M_SOF5  = 0xc5,
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  M_SOF6  = 0xc6,
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  M_SOF7  = 0xc7,
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  M_JPG   = 0xc8,
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  M_SOF9  = 0xc9,
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  M_SOF10 = 0xca,
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  M_SOF11 = 0xcb,
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  M_SOF13 = 0xcd,
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  M_SOF14 = 0xce,
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  M_SOF15 = 0xcf,
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_DHT   = 0xc4,
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_DAC   = 0xcc,
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST0  = 0xd0,
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST1  = 0xd1,
42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST2  = 0xd2,
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST3  = 0xd3,
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST4  = 0xd4,
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST5  = 0xd5,
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST6  = 0xd6,
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_RST7  = 0xd7,
48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  M_SOI   = 0xd8,
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  M_EOI   = 0xd9,
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  M_SOS   = 0xda,
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  M_DQT   = 0xdb,
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  M_DNL   = 0xdc,
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_DRI   = 0xdd,
55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  M_DHP   = 0xde,
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  M_EXP   = 0xdf,
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  M_APP0  = 0xe0,
5968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  M_APP1  = 0xe1,
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP2  = 0xe2,
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP3  = 0xe3,
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP4  = 0xe4,
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP5  = 0xe5,
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  M_APP6  = 0xe6,
65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP7  = 0xe7,
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP8  = 0xe8,
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP9  = 0xe9,
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  M_APP10 = 0xea,
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP11 = 0xeb,
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP12 = 0xec,
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  M_APP13 = 0xed,
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  M_APP14 = 0xee,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  M_APP15 = 0xef,
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  M_JPG0  = 0xf0,
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  M_JPG13 = 0xfd,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  M_COM   = 0xfe,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  M_TEM   = 0x01,
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  M_ERROR = 0x100
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} JPEG_MARKER;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)/* Private state */
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdochtypedef struct {
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  struct jpeg_marker_writer pub; /* public fields */
897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
91f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} my_marker_writer;
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)typedef my_marker_writer * my_marker_ptr;
94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)/*
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * Basic output routines.
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch *
99868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * Note that we do not support suspension while writing a marker.
100b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) * Therefore, an application using suspension must ensure that there is
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) * enough buffer space for the initial markers (typ. 600-700 bytes) before
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) * calling jpeg_start_compress, and enough space to write the trailing EOI
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch * modes are not supported at all with suspension, so those two are the only
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * points where markers will be written.
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LOCAL(void)
109f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)emit_byte (j_compress_ptr cinfo, int val)
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/* Emit a byte */
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch{
112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  struct jpeg_destination_mgr * dest = cinfo->dest;
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *(dest->next_output_byte)++ = (JOCTET) val;
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (--dest->free_in_buffer == 0) {
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (! (*dest->empty_output_buffer) (cinfo))
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ERREXIT(cinfo, JERR_CANT_SUSPEND);
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
122116680a4aac90f2aa7413d9095a592090648e557Ben MurdochLOCAL(void)
123f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/* Emit a marker code */
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles){
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  emit_byte(cinfo, 0xFF);
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  emit_byte(cinfo, (int) mark);
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
130
131LOCAL(void)
132emit_2bytes (j_compress_ptr cinfo, int value)
133/* Emit a 2-byte integer; these are always MSB first in JPEG files */
134{
135  emit_byte(cinfo, (value >> 8) & 0xFF);
136  emit_byte(cinfo, value & 0xFF);
137}
138
139
140/*
141 * Routines to write specific marker types.
142 */
143
144LOCAL(int)
145emit_dqt (j_compress_ptr cinfo, int index)
146/* Emit a DQT marker */
147/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
148{
149  JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
150  int prec;
151  int i;
152
153  if (qtbl == NULL)
154    ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
155
156  prec = 0;
157  for (i = 0; i < DCTSIZE2; i++) {
158    if (qtbl->quantval[i] > 255)
159      prec = 1;
160  }
161
162  if (! qtbl->sent_table) {
163    emit_marker(cinfo, M_DQT);
164
165    emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
166
167    emit_byte(cinfo, index + (prec<<4));
168
169    for (i = 0; i < DCTSIZE2; i++) {
170      /* The table entries must be emitted in zigzag order. */
171      unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
172      if (prec)
173	emit_byte(cinfo, (int) (qval >> 8));
174      emit_byte(cinfo, (int) (qval & 0xFF));
175    }
176
177    qtbl->sent_table = TRUE;
178  }
179
180  return prec;
181}
182
183
184LOCAL(void)
185emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
186/* Emit a DHT marker */
187{
188  JHUFF_TBL * htbl;
189  int length, i;
190
191  if (is_ac) {
192    htbl = cinfo->ac_huff_tbl_ptrs[index];
193    index += 0x10;		/* output index has AC bit set */
194  } else {
195    htbl = cinfo->dc_huff_tbl_ptrs[index];
196  }
197
198  if (htbl == NULL)
199    ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
200
201  if (! htbl->sent_table) {
202    emit_marker(cinfo, M_DHT);
203
204    length = 0;
205    for (i = 1; i <= 16; i++)
206      length += htbl->bits[i];
207
208    emit_2bytes(cinfo, length + 2 + 1 + 16);
209    emit_byte(cinfo, index);
210
211    for (i = 1; i <= 16; i++)
212      emit_byte(cinfo, htbl->bits[i]);
213
214    for (i = 0; i < length; i++)
215      emit_byte(cinfo, htbl->huffval[i]);
216
217    htbl->sent_table = TRUE;
218  }
219}
220
221
222LOCAL(void)
223emit_dac (j_compress_ptr cinfo)
224/* Emit a DAC marker */
225/* Since the useful info is so small, we want to emit all the tables in */
226/* one DAC marker.  Therefore this routine does its own scan of the table. */
227{
228#ifdef C_ARITH_CODING_SUPPORTED
229  char dc_in_use[NUM_ARITH_TBLS];
230  char ac_in_use[NUM_ARITH_TBLS];
231  int length, i;
232  jpeg_component_info *compptr;
233
234  for (i = 0; i < NUM_ARITH_TBLS; i++)
235    dc_in_use[i] = ac_in_use[i] = 0;
236
237  for (i = 0; i < cinfo->comps_in_scan; i++) {
238    compptr = cinfo->cur_comp_info[i];
239    dc_in_use[compptr->dc_tbl_no] = 1;
240    ac_in_use[compptr->ac_tbl_no] = 1;
241  }
242
243  length = 0;
244  for (i = 0; i < NUM_ARITH_TBLS; i++)
245    length += dc_in_use[i] + ac_in_use[i];
246
247  emit_marker(cinfo, M_DAC);
248
249  emit_2bytes(cinfo, length*2 + 2);
250
251  for (i = 0; i < NUM_ARITH_TBLS; i++) {
252    if (dc_in_use[i]) {
253      emit_byte(cinfo, i);
254      emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
255    }
256    if (ac_in_use[i]) {
257      emit_byte(cinfo, i + 0x10);
258      emit_byte(cinfo, cinfo->arith_ac_K[i]);
259    }
260  }
261#endif /* C_ARITH_CODING_SUPPORTED */
262}
263
264
265LOCAL(void)
266emit_dri (j_compress_ptr cinfo)
267/* Emit a DRI marker */
268{
269  emit_marker(cinfo, M_DRI);
270
271  emit_2bytes(cinfo, 4);	/* fixed length */
272
273  emit_2bytes(cinfo, (int) cinfo->restart_interval);
274}
275
276
277LOCAL(void)
278emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
279/* Emit a SOF marker */
280{
281  int ci;
282  jpeg_component_info *compptr;
283
284  emit_marker(cinfo, code);
285
286  emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
287
288  /* Make sure image isn't bigger than SOF field can handle */
289  if ((long) cinfo->image_height > 65535L ||
290      (long) cinfo->image_width > 65535L)
291    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
292
293  emit_byte(cinfo, cinfo->data_precision);
294  emit_2bytes(cinfo, (int) cinfo->image_height);
295  emit_2bytes(cinfo, (int) cinfo->image_width);
296
297  emit_byte(cinfo, cinfo->num_components);
298
299  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
300       ci++, compptr++) {
301    emit_byte(cinfo, compptr->component_id);
302    emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
303    emit_byte(cinfo, compptr->quant_tbl_no);
304  }
305}
306
307
308LOCAL(void)
309emit_sos (j_compress_ptr cinfo)
310/* Emit a SOS marker */
311{
312  int i, td, ta;
313  jpeg_component_info *compptr;
314
315  emit_marker(cinfo, M_SOS);
316
317  emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
318
319  emit_byte(cinfo, cinfo->comps_in_scan);
320
321  for (i = 0; i < cinfo->comps_in_scan; i++) {
322    compptr = cinfo->cur_comp_info[i];
323    emit_byte(cinfo, compptr->component_id);
324    td = compptr->dc_tbl_no;
325    ta = compptr->ac_tbl_no;
326    if (cinfo->progressive_mode) {
327      /* Progressive mode: only DC or only AC tables are used in one scan;
328       * furthermore, Huffman coding of DC refinement uses no table at all.
329       * We emit 0 for unused field(s); this is recommended by the P&M text
330       * but does not seem to be specified in the standard.
331       */
332      if (cinfo->Ss == 0) {
333	ta = 0;			/* DC scan */
334	if (cinfo->Ah != 0 && !cinfo->arith_code)
335	  td = 0;		/* no DC table either */
336      } else {
337	td = 0;			/* AC scan */
338      }
339    }
340    emit_byte(cinfo, (td << 4) + ta);
341  }
342
343  emit_byte(cinfo, cinfo->Ss);
344  emit_byte(cinfo, cinfo->Se);
345  emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
346}
347
348
349LOCAL(void)
350emit_jfif_app0 (j_compress_ptr cinfo)
351/* Emit a JFIF-compliant APP0 marker */
352{
353  /*
354   * Length of APP0 block	(2 bytes)
355   * Block ID			(4 bytes - ASCII "JFIF")
356   * Zero byte			(1 byte to terminate the ID string)
357   * Version Major, Minor	(2 bytes - major first)
358   * Units			(1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
359   * Xdpu			(2 bytes - dots per unit horizontal)
360   * Ydpu			(2 bytes - dots per unit vertical)
361   * Thumbnail X size		(1 byte)
362   * Thumbnail Y size		(1 byte)
363   */
364
365  emit_marker(cinfo, M_APP0);
366
367  emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
368
369  emit_byte(cinfo, 0x4A);	/* Identifier: ASCII "JFIF" */
370  emit_byte(cinfo, 0x46);
371  emit_byte(cinfo, 0x49);
372  emit_byte(cinfo, 0x46);
373  emit_byte(cinfo, 0);
374  emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
375  emit_byte(cinfo, cinfo->JFIF_minor_version);
376  emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
377  emit_2bytes(cinfo, (int) cinfo->X_density);
378  emit_2bytes(cinfo, (int) cinfo->Y_density);
379  emit_byte(cinfo, 0);		/* No thumbnail image */
380  emit_byte(cinfo, 0);
381}
382
383
384LOCAL(void)
385emit_adobe_app14 (j_compress_ptr cinfo)
386/* Emit an Adobe APP14 marker */
387{
388  /*
389   * Length of APP14 block	(2 bytes)
390   * Block ID			(5 bytes - ASCII "Adobe")
391   * Version Number		(2 bytes - currently 100)
392   * Flags0			(2 bytes - currently 0)
393   * Flags1			(2 bytes - currently 0)
394   * Color transform		(1 byte)
395   *
396   * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
397   * now in circulation seem to use Version = 100, so that's what we write.
398   *
399   * We write the color transform byte as 1 if the JPEG color space is
400   * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
401   * whether the encoder performed a transformation, which is pretty useless.
402   */
403
404  emit_marker(cinfo, M_APP14);
405
406  emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
407
408  emit_byte(cinfo, 0x41);	/* Identifier: ASCII "Adobe" */
409  emit_byte(cinfo, 0x64);
410  emit_byte(cinfo, 0x6F);
411  emit_byte(cinfo, 0x62);
412  emit_byte(cinfo, 0x65);
413  emit_2bytes(cinfo, 100);	/* Version */
414  emit_2bytes(cinfo, 0);	/* Flags0 */
415  emit_2bytes(cinfo, 0);	/* Flags1 */
416  switch (cinfo->jpeg_color_space) {
417  case JCS_YCbCr:
418    emit_byte(cinfo, 1);	/* Color transform = 1 */
419    break;
420  case JCS_YCCK:
421    emit_byte(cinfo, 2);	/* Color transform = 2 */
422    break;
423  default:
424    emit_byte(cinfo, 0);	/* Color transform = 0 */
425    break;
426  }
427}
428
429
430/*
431 * These routines allow writing an arbitrary marker with parameters.
432 * The only intended use is to emit COM or APPn markers after calling
433 * write_file_header and before calling write_frame_header.
434 * Other uses are not guaranteed to produce desirable results.
435 * Counting the parameter bytes properly is the caller's responsibility.
436 */
437
438METHODDEF(void)
439write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
440/* Emit an arbitrary marker header */
441{
442  if (datalen > (unsigned int) 65533)		/* safety check */
443    ERREXIT(cinfo, JERR_BAD_LENGTH);
444
445  emit_marker(cinfo, (JPEG_MARKER) marker);
446
447  emit_2bytes(cinfo, (int) (datalen + 2));	/* total length */
448}
449
450METHODDEF(void)
451write_marker_byte (j_compress_ptr cinfo, int val)
452/* Emit one byte of marker parameters following write_marker_header */
453{
454  emit_byte(cinfo, val);
455}
456
457
458/*
459 * Write datastream header.
460 * This consists of an SOI and optional APPn markers.
461 * We recommend use of the JFIF marker, but not the Adobe marker,
462 * when using YCbCr or grayscale data.  The JFIF marker should NOT
463 * be used for any other JPEG colorspace.  The Adobe marker is helpful
464 * to distinguish RGB, CMYK, and YCCK colorspaces.
465 * Note that an application can write additional header markers after
466 * jpeg_start_compress returns.
467 */
468
469METHODDEF(void)
470write_file_header (j_compress_ptr cinfo)
471{
472  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
473
474  emit_marker(cinfo, M_SOI);	/* first the SOI */
475
476  /* SOI is defined to reset restart interval to 0 */
477  marker->last_restart_interval = 0;
478
479  if (cinfo->write_JFIF_header)	/* next an optional JFIF APP0 */
480    emit_jfif_app0(cinfo);
481  if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
482    emit_adobe_app14(cinfo);
483}
484
485
486/*
487 * Write frame header.
488 * This consists of DQT and SOFn markers.
489 * Note that we do not emit the SOF until we have emitted the DQT(s).
490 * This avoids compatibility problems with incorrect implementations that
491 * try to error-check the quant table numbers as soon as they see the SOF.
492 */
493
494METHODDEF(void)
495write_frame_header (j_compress_ptr cinfo)
496{
497  int ci, prec;
498  boolean is_baseline;
499  jpeg_component_info *compptr;
500
501  /* Emit DQT for each quantization table.
502   * Note that emit_dqt() suppresses any duplicate tables.
503   */
504  prec = 0;
505  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
506       ci++, compptr++) {
507    prec += emit_dqt(cinfo, compptr->quant_tbl_no);
508  }
509  /* now prec is nonzero iff there are any 16-bit quant tables. */
510
511  /* Check for a non-baseline specification.
512   * Note we assume that Huffman table numbers won't be changed later.
513   */
514  if (cinfo->arith_code || cinfo->progressive_mode ||
515      cinfo->data_precision != 8) {
516    is_baseline = FALSE;
517  } else {
518    is_baseline = TRUE;
519    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
520	 ci++, compptr++) {
521      if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
522	is_baseline = FALSE;
523    }
524    if (prec && is_baseline) {
525      is_baseline = FALSE;
526      /* If it's baseline except for quantizer size, warn the user */
527      TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
528    }
529  }
530
531  /* Emit the proper SOF marker */
532  if (cinfo->arith_code) {
533    emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
534  } else {
535    if (cinfo->progressive_mode)
536      emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
537    else if (is_baseline)
538      emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
539    else
540      emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
541  }
542}
543
544
545/*
546 * Write scan header.
547 * This consists of DHT or DAC markers, optional DRI, and SOS.
548 * Compressed data will be written following the SOS.
549 */
550
551METHODDEF(void)
552write_scan_header (j_compress_ptr cinfo)
553{
554  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
555  int i;
556  jpeg_component_info *compptr;
557
558  if (cinfo->arith_code) {
559    /* Emit arith conditioning info.  We may have some duplication
560     * if the file has multiple scans, but it's so small it's hardly
561     * worth worrying about.
562     */
563    emit_dac(cinfo);
564  } else {
565    /* Emit Huffman tables.
566     * Note that emit_dht() suppresses any duplicate tables.
567     */
568    for (i = 0; i < cinfo->comps_in_scan; i++) {
569      compptr = cinfo->cur_comp_info[i];
570      if (cinfo->progressive_mode) {
571	/* Progressive mode: only DC or only AC tables are used in one scan */
572	if (cinfo->Ss == 0) {
573	  if (cinfo->Ah == 0)	/* DC needs no table for refinement scan */
574	    emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
575	} else {
576	  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
577	}
578      } else {
579	/* Sequential mode: need both DC and AC tables */
580	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
581	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
582      }
583    }
584  }
585
586  /* Emit DRI if required --- note that DRI value could change for each scan.
587   * We avoid wasting space with unnecessary DRIs, however.
588   */
589  if (cinfo->restart_interval != marker->last_restart_interval) {
590    emit_dri(cinfo);
591    marker->last_restart_interval = cinfo->restart_interval;
592  }
593
594  emit_sos(cinfo);
595}
596
597
598/*
599 * Write datastream trailer.
600 */
601
602METHODDEF(void)
603write_file_trailer (j_compress_ptr cinfo)
604{
605  emit_marker(cinfo, M_EOI);
606}
607
608
609/*
610 * Write an abbreviated table-specification datastream.
611 * This consists of SOI, DQT and DHT tables, and EOI.
612 * Any table that is defined and not marked sent_table = TRUE will be
613 * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
614 */
615
616METHODDEF(void)
617write_tables_only (j_compress_ptr cinfo)
618{
619  int i;
620
621  emit_marker(cinfo, M_SOI);
622
623  for (i = 0; i < NUM_QUANT_TBLS; i++) {
624    if (cinfo->quant_tbl_ptrs[i] != NULL)
625      (void) emit_dqt(cinfo, i);
626  }
627
628  if (! cinfo->arith_code) {
629    for (i = 0; i < NUM_HUFF_TBLS; i++) {
630      if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
631	emit_dht(cinfo, i, FALSE);
632      if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
633	emit_dht(cinfo, i, TRUE);
634    }
635  }
636
637  emit_marker(cinfo, M_EOI);
638}
639
640
641/*
642 * Initialize the marker writer module.
643 */
644
645GLOBAL(void)
646jinit_marker_writer (j_compress_ptr cinfo)
647{
648  my_marker_ptr marker;
649
650  /* Create the subobject */
651  marker = (my_marker_ptr)
652    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
653				SIZEOF(my_marker_writer));
654  cinfo->marker = (struct jpeg_marker_writer *) marker;
655  /* Initialize method pointers */
656  marker->pub.write_file_header = write_file_header;
657  marker->pub.write_frame_header = write_frame_header;
658  marker->pub.write_scan_header = write_scan_header;
659  marker->pub.write_file_trailer = write_file_trailer;
660  marker->pub.write_tables_only = write_tables_only;
661  marker->pub.write_marker_header = write_marker_header;
662  marker->pub.write_marker_byte = write_marker_byte;
663  /* Initialize private state */
664  marker->last_restart_interval = 0;
665}
666
667#endif //_FX_JPEG_TURBO_
668