blob: b1c1e4581e5c6c566563894197fd493dc5f0dd6a [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmarker.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1998, Thomas G. Lane.
DRCc04bd3c2010-10-10 02:15:56 +00005 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00006 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains routines to write JPEG datastream markers.
10 */
11
12#define JPEG_INTERNALS
13#include "jinclude.h"
14#include "jpeglib.h"
DRCc04bd3c2010-10-10 02:15:56 +000015#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000016
17
18typedef enum { /* JPEG marker codes */
19 M_SOF0 = 0xc0,
20 M_SOF1 = 0xc1,
21 M_SOF2 = 0xc2,
22 M_SOF3 = 0xc3,
23
24 M_SOF5 = 0xc5,
25 M_SOF6 = 0xc6,
26 M_SOF7 = 0xc7,
27
28 M_JPG = 0xc8,
29 M_SOF9 = 0xc9,
30 M_SOF10 = 0xca,
31 M_SOF11 = 0xcb,
32
33 M_SOF13 = 0xcd,
34 M_SOF14 = 0xce,
35 M_SOF15 = 0xcf,
36
37 M_DHT = 0xc4,
38
39 M_DAC = 0xcc,
40
41 M_RST0 = 0xd0,
42 M_RST1 = 0xd1,
43 M_RST2 = 0xd2,
44 M_RST3 = 0xd3,
45 M_RST4 = 0xd4,
46 M_RST5 = 0xd5,
47 M_RST6 = 0xd6,
48 M_RST7 = 0xd7,
49
50 M_SOI = 0xd8,
51 M_EOI = 0xd9,
52 M_SOS = 0xda,
53 M_DQT = 0xdb,
54 M_DNL = 0xdc,
55 M_DRI = 0xdd,
56 M_DHP = 0xde,
57 M_EXP = 0xdf,
58
59 M_APP0 = 0xe0,
60 M_APP1 = 0xe1,
61 M_APP2 = 0xe2,
62 M_APP3 = 0xe3,
63 M_APP4 = 0xe4,
64 M_APP5 = 0xe5,
65 M_APP6 = 0xe6,
66 M_APP7 = 0xe7,
67 M_APP8 = 0xe8,
68 M_APP9 = 0xe9,
69 M_APP10 = 0xea,
70 M_APP11 = 0xeb,
71 M_APP12 = 0xec,
72 M_APP13 = 0xed,
73 M_APP14 = 0xee,
74 M_APP15 = 0xef,
75
76 M_JPG0 = 0xf0,
77 M_JPG13 = 0xfd,
78 M_COM = 0xfe,
79
80 M_TEM = 0x01,
81
82 M_ERROR = 0x100
83} JPEG_MARKER;
84
85
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000086/* Private state */
87
88typedef struct {
89 struct jpeg_marker_writer pub; /* public fields */
90
91 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
92} my_marker_writer;
93
94typedef my_marker_writer * my_marker_ptr;
95
96
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000097/*
98 * Basic output routines.
99 *
100 * Note that we do not support suspension while writing a marker.
101 * Therefore, an application using suspension must ensure that there is
102 * enough buffer space for the initial markers (typ. 600-700 bytes) before
103 * calling jpeg_start_compress, and enough space to write the trailing EOI
104 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
105 * modes are not supported at all with suspension, so those two are the only
106 * points where markers will be written.
107 */
108
Thomas G. Lane489583f1996-02-07 00:00:00 +0000109LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110emit_byte (j_compress_ptr cinfo, int val)
111/* Emit a byte */
112{
113 struct jpeg_destination_mgr * dest = cinfo->dest;
114
115 *(dest->next_output_byte)++ = (JOCTET) val;
116 if (--dest->free_in_buffer == 0) {
117 if (! (*dest->empty_output_buffer) (cinfo))
118 ERREXIT(cinfo, JERR_CANT_SUSPEND);
119 }
120}
121
122
Thomas G. Lane489583f1996-02-07 00:00:00 +0000123LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000124emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
125/* Emit a marker code */
126{
127 emit_byte(cinfo, 0xFF);
128 emit_byte(cinfo, (int) mark);
129}
130
131
Thomas G. Lane489583f1996-02-07 00:00:00 +0000132LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000133emit_2bytes (j_compress_ptr cinfo, int value)
134/* Emit a 2-byte integer; these are always MSB first in JPEG files */
135{
136 emit_byte(cinfo, (value >> 8) & 0xFF);
137 emit_byte(cinfo, value & 0xFF);
138}
139
140
141/*
142 * Routines to write specific marker types.
143 */
144
Thomas G. Lane489583f1996-02-07 00:00:00 +0000145LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146emit_dqt (j_compress_ptr cinfo, int index)
147/* Emit a DQT marker */
148/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
149{
150 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
151 int prec;
152 int i;
153
154 if (qtbl == NULL)
155 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
156
157 prec = 0;
158 for (i = 0; i < DCTSIZE2; i++) {
159 if (qtbl->quantval[i] > 255)
160 prec = 1;
161 }
162
163 if (! qtbl->sent_table) {
164 emit_marker(cinfo, M_DQT);
165
166 emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
167
168 emit_byte(cinfo, index + (prec<<4));
169
170 for (i = 0; i < DCTSIZE2; i++) {
Thomas G. Lane489583f1996-02-07 00:00:00 +0000171 /* The table entries must be emitted in zigzag order. */
172 unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000173 if (prec)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000174 emit_byte(cinfo, (int) (qval >> 8));
175 emit_byte(cinfo, (int) (qval & 0xFF));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000176 }
177
178 qtbl->sent_table = TRUE;
179 }
180
181 return prec;
182}
183
184
Thomas G. Lane489583f1996-02-07 00:00:00 +0000185LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
187/* Emit a DHT marker */
188{
189 JHUFF_TBL * htbl;
190 int length, i;
191
192 if (is_ac) {
193 htbl = cinfo->ac_huff_tbl_ptrs[index];
194 index += 0x10; /* output index has AC bit set */
195 } else {
196 htbl = cinfo->dc_huff_tbl_ptrs[index];
197 }
198
199 if (htbl == NULL)
200 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
201
202 if (! htbl->sent_table) {
203 emit_marker(cinfo, M_DHT);
204
205 length = 0;
206 for (i = 1; i <= 16; i++)
207 length += htbl->bits[i];
208
209 emit_2bytes(cinfo, length + 2 + 1 + 16);
210 emit_byte(cinfo, index);
211
212 for (i = 1; i <= 16; i++)
213 emit_byte(cinfo, htbl->bits[i]);
214
215 for (i = 0; i < length; i++)
216 emit_byte(cinfo, htbl->huffval[i]);
217
218 htbl->sent_table = TRUE;
219 }
220}
221
222
Thomas G. Lane489583f1996-02-07 00:00:00 +0000223LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000224emit_dac (j_compress_ptr cinfo)
225/* Emit a DAC marker */
226/* Since the useful info is so small, we want to emit all the tables in */
227/* one DAC marker. Therefore this routine does its own scan of the table. */
228{
229#ifdef C_ARITH_CODING_SUPPORTED
230 char dc_in_use[NUM_ARITH_TBLS];
231 char ac_in_use[NUM_ARITH_TBLS];
232 int length, i;
233 jpeg_component_info *compptr;
234
235 for (i = 0; i < NUM_ARITH_TBLS; i++)
236 dc_in_use[i] = ac_in_use[i] = 0;
237
238 for (i = 0; i < cinfo->comps_in_scan; i++) {
239 compptr = cinfo->cur_comp_info[i];
240 dc_in_use[compptr->dc_tbl_no] = 1;
241 ac_in_use[compptr->ac_tbl_no] = 1;
242 }
243
244 length = 0;
245 for (i = 0; i < NUM_ARITH_TBLS; i++)
246 length += dc_in_use[i] + ac_in_use[i];
247
248 emit_marker(cinfo, M_DAC);
249
250 emit_2bytes(cinfo, length*2 + 2);
251
252 for (i = 0; i < NUM_ARITH_TBLS; i++) {
253 if (dc_in_use[i]) {
254 emit_byte(cinfo, i);
255 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
256 }
257 if (ac_in_use[i]) {
258 emit_byte(cinfo, i + 0x10);
259 emit_byte(cinfo, cinfo->arith_ac_K[i]);
260 }
261 }
262#endif /* C_ARITH_CODING_SUPPORTED */
263}
264
265
Thomas G. Lane489583f1996-02-07 00:00:00 +0000266LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000267emit_dri (j_compress_ptr cinfo)
268/* Emit a DRI marker */
269{
270 emit_marker(cinfo, M_DRI);
271
272 emit_2bytes(cinfo, 4); /* fixed length */
273
274 emit_2bytes(cinfo, (int) cinfo->restart_interval);
275}
276
277
Thomas G. Lane489583f1996-02-07 00:00:00 +0000278LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000279emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
280/* Emit a SOF marker */
281{
282 int ci;
283 jpeg_component_info *compptr;
284
285 emit_marker(cinfo, code);
286
287 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
288
289 /* Make sure image isn't bigger than SOF field can handle */
DRCc04bd3c2010-10-10 02:15:56 +0000290 if ((long) cinfo->_jpeg_height > 65535L ||
291 (long) cinfo->_jpeg_width > 65535L)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000292 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
293
294 emit_byte(cinfo, cinfo->data_precision);
DRCc04bd3c2010-10-10 02:15:56 +0000295 emit_2bytes(cinfo, (int) cinfo->_jpeg_height);
296 emit_2bytes(cinfo, (int) cinfo->_jpeg_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000297
298 emit_byte(cinfo, cinfo->num_components);
299
300 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
301 ci++, compptr++) {
302 emit_byte(cinfo, compptr->component_id);
303 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
304 emit_byte(cinfo, compptr->quant_tbl_no);
305 }
306}
307
308
Thomas G. Lane489583f1996-02-07 00:00:00 +0000309LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310emit_sos (j_compress_ptr cinfo)
311/* Emit a SOS marker */
312{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000313 int i, td, ta;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 jpeg_component_info *compptr;
315
316 emit_marker(cinfo, M_SOS);
317
318 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
319
320 emit_byte(cinfo, cinfo->comps_in_scan);
321
322 for (i = 0; i < cinfo->comps_in_scan; i++) {
323 compptr = cinfo->cur_comp_info[i];
324 emit_byte(cinfo, compptr->component_id);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000325 td = compptr->dc_tbl_no;
326 ta = compptr->ac_tbl_no;
327 if (cinfo->progressive_mode) {
328 /* Progressive mode: only DC or only AC tables are used in one scan;
329 * furthermore, Huffman coding of DC refinement uses no table at all.
330 * We emit 0 for unused field(s); this is recommended by the P&M text
331 * but does not seem to be specified in the standard.
332 */
333 if (cinfo->Ss == 0) {
334 ta = 0; /* DC scan */
335 if (cinfo->Ah != 0 && !cinfo->arith_code)
336 td = 0; /* no DC table either */
337 } else {
338 td = 0; /* AC scan */
339 }
340 }
341 emit_byte(cinfo, (td << 4) + ta);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342 }
343
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000344 emit_byte(cinfo, cinfo->Ss);
345 emit_byte(cinfo, cinfo->Se);
346 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000347}
348
349
Thomas G. Lane489583f1996-02-07 00:00:00 +0000350LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000351emit_jfif_app0 (j_compress_ptr cinfo)
352/* Emit a JFIF-compliant APP0 marker */
353{
354 /*
355 * Length of APP0 block (2 bytes)
356 * Block ID (4 bytes - ASCII "JFIF")
357 * Zero byte (1 byte to terminate the ID string)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000358 * Version Major, Minor (2 bytes - major first)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
360 * Xdpu (2 bytes - dots per unit horizontal)
361 * Ydpu (2 bytes - dots per unit vertical)
362 * Thumbnail X size (1 byte)
363 * Thumbnail Y size (1 byte)
364 */
365
366 emit_marker(cinfo, M_APP0);
367
368 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
369
370 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
371 emit_byte(cinfo, 0x46);
372 emit_byte(cinfo, 0x49);
373 emit_byte(cinfo, 0x46);
374 emit_byte(cinfo, 0);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000375 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
376 emit_byte(cinfo, cinfo->JFIF_minor_version);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000377 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
378 emit_2bytes(cinfo, (int) cinfo->X_density);
379 emit_2bytes(cinfo, (int) cinfo->Y_density);
380 emit_byte(cinfo, 0); /* No thumbnail image */
381 emit_byte(cinfo, 0);
382}
383
384
Thomas G. Lane489583f1996-02-07 00:00:00 +0000385LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000386emit_adobe_app14 (j_compress_ptr cinfo)
387/* Emit an Adobe APP14 marker */
388{
389 /*
390 * Length of APP14 block (2 bytes)
391 * Block ID (5 bytes - ASCII "Adobe")
392 * Version Number (2 bytes - currently 100)
393 * Flags0 (2 bytes - currently 0)
394 * Flags1 (2 bytes - currently 0)
395 * Color transform (1 byte)
396 *
397 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
398 * now in circulation seem to use Version = 100, so that's what we write.
399 *
400 * We write the color transform byte as 1 if the JPEG color space is
401 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
402 * whether the encoder performed a transformation, which is pretty useless.
403 */
404
405 emit_marker(cinfo, M_APP14);
406
407 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
408
409 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
410 emit_byte(cinfo, 0x64);
411 emit_byte(cinfo, 0x6F);
412 emit_byte(cinfo, 0x62);
413 emit_byte(cinfo, 0x65);
414 emit_2bytes(cinfo, 100); /* Version */
415 emit_2bytes(cinfo, 0); /* Flags0 */
416 emit_2bytes(cinfo, 0); /* Flags1 */
417 switch (cinfo->jpeg_color_space) {
418 case JCS_YCbCr:
419 emit_byte(cinfo, 1); /* Color transform = 1 */
420 break;
421 case JCS_YCCK:
422 emit_byte(cinfo, 2); /* Color transform = 2 */
423 break;
424 default:
425 emit_byte(cinfo, 0); /* Color transform = 0 */
426 break;
427 }
428}
429
430
431/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000432 * These routines allow writing an arbitrary marker with parameters.
433 * The only intended use is to emit COM or APPn markers after calling
434 * write_file_header and before calling write_frame_header.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435 * Other uses are not guaranteed to produce desirable results.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000436 * Counting the parameter bytes properly is the caller's responsibility.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000437 */
438
Thomas G. Lane489583f1996-02-07 00:00:00 +0000439METHODDEF(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000440write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
441/* Emit an arbitrary marker header */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000442{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000443 if (datalen > (unsigned int) 65533) /* safety check */
444 ERREXIT(cinfo, JERR_BAD_LENGTH);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000445
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000446 emit_marker(cinfo, (JPEG_MARKER) marker);
447
448 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
449}
450
451METHODDEF(void)
452write_marker_byte (j_compress_ptr cinfo, int val)
453/* Emit one byte of marker parameters following write_marker_header */
454{
455 emit_byte(cinfo, val);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000456}
457
458
459/*
460 * Write datastream header.
461 * This consists of an SOI and optional APPn markers.
462 * We recommend use of the JFIF marker, but not the Adobe marker,
463 * when using YCbCr or grayscale data. The JFIF marker should NOT
464 * be used for any other JPEG colorspace. The Adobe marker is helpful
465 * to distinguish RGB, CMYK, and YCCK colorspaces.
466 * Note that an application can write additional header markers after
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000467 * jpeg_start_compress returns.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000468 */
469
Thomas G. Lane489583f1996-02-07 00:00:00 +0000470METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000471write_file_header (j_compress_ptr cinfo)
472{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000473 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
474
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475 emit_marker(cinfo, M_SOI); /* first the SOI */
476
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000477 /* SOI is defined to reset restart interval to 0 */
478 marker->last_restart_interval = 0;
479
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000480 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
481 emit_jfif_app0(cinfo);
482 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
483 emit_adobe_app14(cinfo);
484}
485
486
487/*
488 * Write frame header.
489 * This consists of DQT and SOFn markers.
490 * Note that we do not emit the SOF until we have emitted the DQT(s).
491 * This avoids compatibility problems with incorrect implementations that
492 * try to error-check the quant table numbers as soon as they see the SOF.
493 */
494
Thomas G. Lane489583f1996-02-07 00:00:00 +0000495METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496write_frame_header (j_compress_ptr cinfo)
497{
498 int ci, prec;
499 boolean is_baseline;
500 jpeg_component_info *compptr;
501
502 /* Emit DQT for each quantization table.
503 * Note that emit_dqt() suppresses any duplicate tables.
504 */
505 prec = 0;
506 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
507 ci++, compptr++) {
508 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
509 }
510 /* now prec is nonzero iff there are any 16-bit quant tables. */
511
512 /* Check for a non-baseline specification.
513 * Note we assume that Huffman table numbers won't be changed later.
514 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000515 if (cinfo->arith_code || cinfo->progressive_mode ||
516 cinfo->data_precision != 8) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000517 is_baseline = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000518 } else {
519 is_baseline = TRUE;
520 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
521 ci++, compptr++) {
522 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
523 is_baseline = FALSE;
524 }
525 if (prec && is_baseline) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000526 is_baseline = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000527 /* If it's baseline except for quantizer size, warn the user */
528 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
529 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000530 }
531
532 /* Emit the proper SOF marker */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000533 if (cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000534 emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000535 } else {
536 if (cinfo->progressive_mode)
537 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
538 else if (is_baseline)
539 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
540 else
541 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
542 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000543}
544
545
546/*
547 * Write scan header.
548 * This consists of DHT or DAC markers, optional DRI, and SOS.
549 * Compressed data will be written following the SOS.
550 */
551
Thomas G. Lane489583f1996-02-07 00:00:00 +0000552METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000553write_scan_header (j_compress_ptr cinfo)
554{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000555 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000556 int i;
557 jpeg_component_info *compptr;
558
559 if (cinfo->arith_code) {
560 /* Emit arith conditioning info. We may have some duplication
561 * if the file has multiple scans, but it's so small it's hardly
562 * worth worrying about.
563 */
564 emit_dac(cinfo);
565 } else {
566 /* Emit Huffman tables.
567 * Note that emit_dht() suppresses any duplicate tables.
568 */
569 for (i = 0; i < cinfo->comps_in_scan; i++) {
570 compptr = cinfo->cur_comp_info[i];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000571 if (cinfo->progressive_mode) {
572 /* Progressive mode: only DC or only AC tables are used in one scan */
573 if (cinfo->Ss == 0) {
574 if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
575 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
576 } else {
577 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
578 }
579 } else {
580 /* Sequential mode: need both DC and AC tables */
581 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
582 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
583 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000584 }
585 }
586
587 /* Emit DRI if required --- note that DRI value could change for each scan.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000588 * We avoid wasting space with unnecessary DRIs, however.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000589 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000590 if (cinfo->restart_interval != marker->last_restart_interval) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000591 emit_dri(cinfo);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000592 marker->last_restart_interval = cinfo->restart_interval;
593 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000594
595 emit_sos(cinfo);
596}
597
598
599/*
600 * Write datastream trailer.
601 */
602
Thomas G. Lane489583f1996-02-07 00:00:00 +0000603METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000604write_file_trailer (j_compress_ptr cinfo)
605{
606 emit_marker(cinfo, M_EOI);
607}
608
609
610/*
611 * Write an abbreviated table-specification datastream.
612 * This consists of SOI, DQT and DHT tables, and EOI.
613 * Any table that is defined and not marked sent_table = TRUE will be
614 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
615 */
616
Thomas G. Lane489583f1996-02-07 00:00:00 +0000617METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000618write_tables_only (j_compress_ptr cinfo)
619{
620 int i;
621
622 emit_marker(cinfo, M_SOI);
623
624 for (i = 0; i < NUM_QUANT_TBLS; i++) {
625 if (cinfo->quant_tbl_ptrs[i] != NULL)
626 (void) emit_dqt(cinfo, i);
627 }
628
629 if (! cinfo->arith_code) {
630 for (i = 0; i < NUM_HUFF_TBLS; i++) {
631 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
632 emit_dht(cinfo, i, FALSE);
633 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
634 emit_dht(cinfo, i, TRUE);
635 }
636 }
637
638 emit_marker(cinfo, M_EOI);
639}
640
641
642/*
643 * Initialize the marker writer module.
644 */
645
Thomas G. Lane489583f1996-02-07 00:00:00 +0000646GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000647jinit_marker_writer (j_compress_ptr cinfo)
648{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000649 my_marker_ptr marker;
650
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000651 /* Create the subobject */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000652 marker = (my_marker_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000653 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000654 SIZEOF(my_marker_writer));
655 cinfo->marker = (struct jpeg_marker_writer *) marker;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000656 /* Initialize method pointers */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000657 marker->pub.write_file_header = write_file_header;
658 marker->pub.write_frame_header = write_frame_header;
659 marker->pub.write_scan_header = write_scan_header;
660 marker->pub.write_file_trailer = write_file_trailer;
661 marker->pub.write_tables_only = write_tables_only;
662 marker->pub.write_marker_header = write_marker_header;
663 marker->pub.write_marker_byte = write_marker_byte;
664 /* Initialize private state */
665 marker->last_restart_interval = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000666}