blob: 06fe62399ee9faeae107994977c22cfed2bad937 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * jcmarker.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1998, Thomas G. Lane.
DRCa73e8702012-12-31 02:52:30 +00006 * Modifications:
DRCc04bd3c2010-10-10 02:15:56 +00007 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains routines to write JPEG datastream markers.
11 */
12
13#define JPEG_INTERNALS
14#include "jinclude.h"
15#include "jpeglib.h"
DRCc04bd3c2010-10-10 02:15:56 +000016#include "jpegcomp.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017
18
19typedef enum { /* JPEG marker codes */
20 M_SOF0 = 0xc0,
21 M_SOF1 = 0xc1,
22 M_SOF2 = 0xc2,
23 M_SOF3 = 0xc3,
DRC5aa6c9a2013-11-06 05:58:38 +000024
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025 M_SOF5 = 0xc5,
26 M_SOF6 = 0xc6,
27 M_SOF7 = 0xc7,
DRC5aa6c9a2013-11-06 05:58:38 +000028
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000029 M_JPG = 0xc8,
30 M_SOF9 = 0xc9,
31 M_SOF10 = 0xca,
32 M_SOF11 = 0xcb,
DRC5aa6c9a2013-11-06 05:58:38 +000033
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034 M_SOF13 = 0xcd,
35 M_SOF14 = 0xce,
36 M_SOF15 = 0xcf,
DRC5aa6c9a2013-11-06 05:58:38 +000037
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000038 M_DHT = 0xc4,
DRC5aa6c9a2013-11-06 05:58:38 +000039
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040 M_DAC = 0xcc,
DRC5aa6c9a2013-11-06 05:58:38 +000041
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042 M_RST0 = 0xd0,
43 M_RST1 = 0xd1,
44 M_RST2 = 0xd2,
45 M_RST3 = 0xd3,
46 M_RST4 = 0xd4,
47 M_RST5 = 0xd5,
48 M_RST6 = 0xd6,
49 M_RST7 = 0xd7,
DRC5aa6c9a2013-11-06 05:58:38 +000050
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051 M_SOI = 0xd8,
52 M_EOI = 0xd9,
53 M_SOS = 0xda,
54 M_DQT = 0xdb,
55 M_DNL = 0xdc,
56 M_DRI = 0xdd,
57 M_DHP = 0xde,
58 M_EXP = 0xdf,
DRC5aa6c9a2013-11-06 05:58:38 +000059
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000060 M_APP0 = 0xe0,
61 M_APP1 = 0xe1,
62 M_APP2 = 0xe2,
63 M_APP3 = 0xe3,
64 M_APP4 = 0xe4,
65 M_APP5 = 0xe5,
66 M_APP6 = 0xe6,
67 M_APP7 = 0xe7,
68 M_APP8 = 0xe8,
69 M_APP9 = 0xe9,
70 M_APP10 = 0xea,
71 M_APP11 = 0xeb,
72 M_APP12 = 0xec,
73 M_APP13 = 0xed,
74 M_APP14 = 0xee,
75 M_APP15 = 0xef,
DRC5aa6c9a2013-11-06 05:58:38 +000076
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077 M_JPG0 = 0xf0,
78 M_JPG13 = 0xfd,
79 M_COM = 0xfe,
DRC5aa6c9a2013-11-06 05:58:38 +000080
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081 M_TEM = 0x01,
DRC5aa6c9a2013-11-06 05:58:38 +000082
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 M_ERROR = 0x100
84} JPEG_MARKER;
85
86
Thomas G. Lane5ead57a1998-03-27 00:00:00 +000087/* Private state */
88
89typedef struct {
90 struct jpeg_marker_writer pub; /* public fields */
91
92 unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
93} my_marker_writer;
94
95typedef my_marker_writer * my_marker_ptr;
96
97
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000098/*
99 * Basic output routines.
100 *
101 * Note that we do not support suspension while writing a marker.
102 * Therefore, an application using suspension must ensure that there is
103 * enough buffer space for the initial markers (typ. 600-700 bytes) before
104 * calling jpeg_start_compress, and enough space to write the trailing EOI
105 * (a few bytes) before calling jpeg_finish_compress. Multipass compression
106 * modes are not supported at all with suspension, so those two are the only
107 * points where markers will be written.
108 */
109
Thomas G. Lane489583f1996-02-07 00:00:00 +0000110LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111emit_byte (j_compress_ptr cinfo, int val)
112/* Emit a byte */
113{
114 struct jpeg_destination_mgr * dest = cinfo->dest;
115
116 *(dest->next_output_byte)++ = (JOCTET) val;
117 if (--dest->free_in_buffer == 0) {
118 if (! (*dest->empty_output_buffer) (cinfo))
119 ERREXIT(cinfo, JERR_CANT_SUSPEND);
120 }
121}
122
123
Thomas G. Lane489583f1996-02-07 00:00:00 +0000124LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000125emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
126/* Emit a marker code */
127{
128 emit_byte(cinfo, 0xFF);
129 emit_byte(cinfo, (int) mark);
130}
131
132
Thomas G. Lane489583f1996-02-07 00:00:00 +0000133LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000134emit_2bytes (j_compress_ptr cinfo, int value)
135/* Emit a 2-byte integer; these are always MSB first in JPEG files */
136{
137 emit_byte(cinfo, (value >> 8) & 0xFF);
138 emit_byte(cinfo, value & 0xFF);
139}
140
141
142/*
143 * Routines to write specific marker types.
144 */
145
Thomas G. Lane489583f1996-02-07 00:00:00 +0000146LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147emit_dqt (j_compress_ptr cinfo, int index)
148/* Emit a DQT marker */
149/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
150{
151 JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
152 int prec;
153 int i;
154
155 if (qtbl == NULL)
156 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
157
158 prec = 0;
159 for (i = 0; i < DCTSIZE2; i++) {
160 if (qtbl->quantval[i] > 255)
161 prec = 1;
162 }
163
164 if (! qtbl->sent_table) {
165 emit_marker(cinfo, M_DQT);
166
167 emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
168
169 emit_byte(cinfo, index + (prec<<4));
170
171 for (i = 0; i < DCTSIZE2; i++) {
Thomas G. Lane489583f1996-02-07 00:00:00 +0000172 /* The table entries must be emitted in zigzag order. */
173 unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174 if (prec)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000175 emit_byte(cinfo, (int) (qval >> 8));
176 emit_byte(cinfo, (int) (qval & 0xFF));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177 }
178
179 qtbl->sent_table = TRUE;
180 }
181
182 return prec;
183}
184
185
Thomas G. Lane489583f1996-02-07 00:00:00 +0000186LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000187emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
188/* Emit a DHT marker */
189{
190 JHUFF_TBL * htbl;
191 int length, i;
192
193 if (is_ac) {
194 htbl = cinfo->ac_huff_tbl_ptrs[index];
195 index += 0x10; /* output index has AC bit set */
196 } else {
197 htbl = cinfo->dc_huff_tbl_ptrs[index];
198 }
199
200 if (htbl == NULL)
201 ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
202
203 if (! htbl->sent_table) {
204 emit_marker(cinfo, M_DHT);
205
206 length = 0;
207 for (i = 1; i <= 16; i++)
208 length += htbl->bits[i];
209
210 emit_2bytes(cinfo, length + 2 + 1 + 16);
211 emit_byte(cinfo, index);
212
213 for (i = 1; i <= 16; i++)
214 emit_byte(cinfo, htbl->bits[i]);
215
216 for (i = 0; i < length; i++)
217 emit_byte(cinfo, htbl->huffval[i]);
218
219 htbl->sent_table = TRUE;
220 }
221}
222
223
Thomas G. Lane489583f1996-02-07 00:00:00 +0000224LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225emit_dac (j_compress_ptr cinfo)
226/* Emit a DAC marker */
227/* Since the useful info is so small, we want to emit all the tables in */
228/* one DAC marker. Therefore this routine does its own scan of the table. */
229{
230#ifdef C_ARITH_CODING_SUPPORTED
231 char dc_in_use[NUM_ARITH_TBLS];
232 char ac_in_use[NUM_ARITH_TBLS];
233 int length, i;
234 jpeg_component_info *compptr;
DRC5aa6c9a2013-11-06 05:58:38 +0000235
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236 for (i = 0; i < NUM_ARITH_TBLS; i++)
237 dc_in_use[i] = ac_in_use[i] = 0;
DRC5aa6c9a2013-11-06 05:58:38 +0000238
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239 for (i = 0; i < cinfo->comps_in_scan; i++) {
240 compptr = cinfo->cur_comp_info[i];
241 dc_in_use[compptr->dc_tbl_no] = 1;
242 ac_in_use[compptr->ac_tbl_no] = 1;
243 }
DRC5aa6c9a2013-11-06 05:58:38 +0000244
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000245 length = 0;
246 for (i = 0; i < NUM_ARITH_TBLS; i++)
247 length += dc_in_use[i] + ac_in_use[i];
248
249 emit_marker(cinfo, M_DAC);
250
251 emit_2bytes(cinfo, length*2 + 2);
252
253 for (i = 0; i < NUM_ARITH_TBLS; i++) {
254 if (dc_in_use[i]) {
255 emit_byte(cinfo, i);
256 emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
257 }
258 if (ac_in_use[i]) {
259 emit_byte(cinfo, i + 0x10);
260 emit_byte(cinfo, cinfo->arith_ac_K[i]);
261 }
262 }
263#endif /* C_ARITH_CODING_SUPPORTED */
264}
265
266
Thomas G. Lane489583f1996-02-07 00:00:00 +0000267LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000268emit_dri (j_compress_ptr cinfo)
269/* Emit a DRI marker */
270{
271 emit_marker(cinfo, M_DRI);
272
273 emit_2bytes(cinfo, 4); /* fixed length */
274
275 emit_2bytes(cinfo, (int) cinfo->restart_interval);
276}
277
278
Thomas G. Lane489583f1996-02-07 00:00:00 +0000279LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000280emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
281/* Emit a SOF marker */
282{
283 int ci;
284 jpeg_component_info *compptr;
285
286 emit_marker(cinfo, code);
287
288 emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
289
290 /* Make sure image isn't bigger than SOF field can handle */
DRCc04bd3c2010-10-10 02:15:56 +0000291 if ((long) cinfo->_jpeg_height > 65535L ||
292 (long) cinfo->_jpeg_width > 65535L)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
294
295 emit_byte(cinfo, cinfo->data_precision);
DRCc04bd3c2010-10-10 02:15:56 +0000296 emit_2bytes(cinfo, (int) cinfo->_jpeg_height);
297 emit_2bytes(cinfo, (int) cinfo->_jpeg_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000298
299 emit_byte(cinfo, cinfo->num_components);
300
301 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
302 ci++, compptr++) {
303 emit_byte(cinfo, compptr->component_id);
304 emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
305 emit_byte(cinfo, compptr->quant_tbl_no);
306 }
307}
308
309
Thomas G. Lane489583f1996-02-07 00:00:00 +0000310LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311emit_sos (j_compress_ptr cinfo)
312/* Emit a SOS marker */
313{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000314 int i, td, ta;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 jpeg_component_info *compptr;
316
317 emit_marker(cinfo, M_SOS);
318
319 emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
320
321 emit_byte(cinfo, cinfo->comps_in_scan);
322
323 for (i = 0; i < cinfo->comps_in_scan; i++) {
324 compptr = cinfo->cur_comp_info[i];
325 emit_byte(cinfo, compptr->component_id);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000326 td = compptr->dc_tbl_no;
327 ta = compptr->ac_tbl_no;
328 if (cinfo->progressive_mode) {
329 /* Progressive mode: only DC or only AC tables are used in one scan;
330 * furthermore, Huffman coding of DC refinement uses no table at all.
331 * We emit 0 for unused field(s); this is recommended by the P&M text
332 * but does not seem to be specified in the standard.
333 */
334 if (cinfo->Ss == 0) {
335 ta = 0; /* DC scan */
336 if (cinfo->Ah != 0 && !cinfo->arith_code)
337 td = 0; /* no DC table either */
338 } else {
339 td = 0; /* AC scan */
340 }
341 }
342 emit_byte(cinfo, (td << 4) + ta);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343 }
344
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000345 emit_byte(cinfo, cinfo->Ss);
346 emit_byte(cinfo, cinfo->Se);
347 emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348}
349
350
Thomas G. Lane489583f1996-02-07 00:00:00 +0000351LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352emit_jfif_app0 (j_compress_ptr cinfo)
353/* Emit a JFIF-compliant APP0 marker */
354{
355 /*
356 * Length of APP0 block (2 bytes)
357 * Block ID (4 bytes - ASCII "JFIF")
358 * Zero byte (1 byte to terminate the ID string)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000359 * Version Major, Minor (2 bytes - major first)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
361 * Xdpu (2 bytes - dots per unit horizontal)
362 * Ydpu (2 bytes - dots per unit vertical)
363 * Thumbnail X size (1 byte)
364 * Thumbnail Y size (1 byte)
365 */
366
367 emit_marker(cinfo, M_APP0);
368
369 emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
370
371 emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
372 emit_byte(cinfo, 0x46);
373 emit_byte(cinfo, 0x49);
374 emit_byte(cinfo, 0x46);
375 emit_byte(cinfo, 0);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000376 emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
377 emit_byte(cinfo, cinfo->JFIF_minor_version);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000378 emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
379 emit_2bytes(cinfo, (int) cinfo->X_density);
380 emit_2bytes(cinfo, (int) cinfo->Y_density);
381 emit_byte(cinfo, 0); /* No thumbnail image */
382 emit_byte(cinfo, 0);
383}
384
385
Thomas G. Lane489583f1996-02-07 00:00:00 +0000386LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387emit_adobe_app14 (j_compress_ptr cinfo)
388/* Emit an Adobe APP14 marker */
389{
390 /*
391 * Length of APP14 block (2 bytes)
392 * Block ID (5 bytes - ASCII "Adobe")
393 * Version Number (2 bytes - currently 100)
394 * Flags0 (2 bytes - currently 0)
395 * Flags1 (2 bytes - currently 0)
396 * Color transform (1 byte)
397 *
398 * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
399 * now in circulation seem to use Version = 100, so that's what we write.
400 *
401 * We write the color transform byte as 1 if the JPEG color space is
402 * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
403 * whether the encoder performed a transformation, which is pretty useless.
404 */
405
406 emit_marker(cinfo, M_APP14);
407
408 emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
409
410 emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
411 emit_byte(cinfo, 0x64);
412 emit_byte(cinfo, 0x6F);
413 emit_byte(cinfo, 0x62);
414 emit_byte(cinfo, 0x65);
415 emit_2bytes(cinfo, 100); /* Version */
416 emit_2bytes(cinfo, 0); /* Flags0 */
417 emit_2bytes(cinfo, 0); /* Flags1 */
418 switch (cinfo->jpeg_color_space) {
419 case JCS_YCbCr:
420 emit_byte(cinfo, 1); /* Color transform = 1 */
421 break;
422 case JCS_YCCK:
423 emit_byte(cinfo, 2); /* Color transform = 2 */
424 break;
425 default:
426 emit_byte(cinfo, 0); /* Color transform = 0 */
427 break;
428 }
429}
430
431
432/*
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000433 * These routines allow writing an arbitrary marker with parameters.
434 * The only intended use is to emit COM or APPn markers after calling
435 * write_file_header and before calling write_frame_header.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436 * Other uses are not guaranteed to produce desirable results.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000437 * Counting the parameter bytes properly is the caller's responsibility.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438 */
439
Thomas G. Lane489583f1996-02-07 00:00:00 +0000440METHODDEF(void)
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000441write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
442/* Emit an arbitrary marker header */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000444 if (datalen > (unsigned int) 65533) /* safety check */
445 ERREXIT(cinfo, JERR_BAD_LENGTH);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000447 emit_marker(cinfo, (JPEG_MARKER) marker);
448
449 emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */
450}
451
452METHODDEF(void)
453write_marker_byte (j_compress_ptr cinfo, int val)
454/* Emit one byte of marker parameters following write_marker_header */
455{
456 emit_byte(cinfo, val);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000457}
458
459
460/*
461 * Write datastream header.
462 * This consists of an SOI and optional APPn markers.
463 * We recommend use of the JFIF marker, but not the Adobe marker,
464 * when using YCbCr or grayscale data. The JFIF marker should NOT
465 * be used for any other JPEG colorspace. The Adobe marker is helpful
466 * to distinguish RGB, CMYK, and YCCK colorspaces.
467 * Note that an application can write additional header markers after
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000468 * jpeg_start_compress returns.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000469 */
470
Thomas G. Lane489583f1996-02-07 00:00:00 +0000471METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000472write_file_header (j_compress_ptr cinfo)
473{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000474 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
475
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000476 emit_marker(cinfo, M_SOI); /* first the SOI */
477
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000478 /* SOI is defined to reset restart interval to 0 */
479 marker->last_restart_interval = 0;
480
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000481 if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
482 emit_jfif_app0(cinfo);
483 if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
484 emit_adobe_app14(cinfo);
485}
486
487
488/*
489 * Write frame header.
490 * This consists of DQT and SOFn markers.
491 * Note that we do not emit the SOF until we have emitted the DQT(s).
492 * This avoids compatibility problems with incorrect implementations that
493 * try to error-check the quant table numbers as soon as they see the SOF.
494 */
495
Thomas G. Lane489583f1996-02-07 00:00:00 +0000496METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000497write_frame_header (j_compress_ptr cinfo)
498{
499 int ci, prec;
500 boolean is_baseline;
501 jpeg_component_info *compptr;
502
503 /* Emit DQT for each quantization table.
504 * Note that emit_dqt() suppresses any duplicate tables.
505 */
506 prec = 0;
507 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
508 ci++, compptr++) {
509 prec += emit_dqt(cinfo, compptr->quant_tbl_no);
510 }
511 /* now prec is nonzero iff there are any 16-bit quant tables. */
512
513 /* Check for a non-baseline specification.
514 * Note we assume that Huffman table numbers won't be changed later.
515 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000516 if (cinfo->arith_code || cinfo->progressive_mode ||
517 cinfo->data_precision != 8) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000518 is_baseline = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000519 } else {
520 is_baseline = TRUE;
521 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
522 ci++, compptr++) {
523 if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
524 is_baseline = FALSE;
525 }
526 if (prec && is_baseline) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000527 is_baseline = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000528 /* If it's baseline except for quantizer size, warn the user */
529 TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
530 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000531 }
532
533 /* Emit the proper SOF marker */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000534 if (cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000535 emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000536 } else {
537 if (cinfo->progressive_mode)
538 emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
539 else if (is_baseline)
540 emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
541 else
542 emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
543 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000544}
545
546
547/*
548 * Write scan header.
549 * This consists of DHT or DAC markers, optional DRI, and SOS.
550 * Compressed data will be written following the SOS.
551 */
552
Thomas G. Lane489583f1996-02-07 00:00:00 +0000553METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000554write_scan_header (j_compress_ptr cinfo)
555{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000556 my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000557 int i;
558 jpeg_component_info *compptr;
559
560 if (cinfo->arith_code) {
561 /* Emit arith conditioning info. We may have some duplication
562 * if the file has multiple scans, but it's so small it's hardly
563 * worth worrying about.
564 */
565 emit_dac(cinfo);
566 } else {
567 /* Emit Huffman tables.
568 * Note that emit_dht() suppresses any duplicate tables.
569 */
570 for (i = 0; i < cinfo->comps_in_scan; i++) {
571 compptr = cinfo->cur_comp_info[i];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000572 if (cinfo->progressive_mode) {
573 /* Progressive mode: only DC or only AC tables are used in one scan */
574 if (cinfo->Ss == 0) {
575 if (cinfo->Ah == 0) /* DC needs no table for refinement scan */
576 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
577 } else {
578 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
579 }
580 } else {
581 /* Sequential mode: need both DC and AC tables */
582 emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
583 emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
584 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000585 }
586 }
587
588 /* Emit DRI if required --- note that DRI value could change for each scan.
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000589 * We avoid wasting space with unnecessary DRIs, however.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000590 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000591 if (cinfo->restart_interval != marker->last_restart_interval) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000592 emit_dri(cinfo);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000593 marker->last_restart_interval = cinfo->restart_interval;
594 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000595
596 emit_sos(cinfo);
597}
598
599
600/*
601 * Write datastream trailer.
602 */
603
Thomas G. Lane489583f1996-02-07 00:00:00 +0000604METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000605write_file_trailer (j_compress_ptr cinfo)
606{
607 emit_marker(cinfo, M_EOI);
608}
609
610
611/*
612 * Write an abbreviated table-specification datastream.
613 * This consists of SOI, DQT and DHT tables, and EOI.
614 * Any table that is defined and not marked sent_table = TRUE will be
615 * emitted. Note that all tables will be marked sent_table = TRUE at exit.
616 */
617
Thomas G. Lane489583f1996-02-07 00:00:00 +0000618METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000619write_tables_only (j_compress_ptr cinfo)
620{
621 int i;
622
623 emit_marker(cinfo, M_SOI);
624
625 for (i = 0; i < NUM_QUANT_TBLS; i++) {
626 if (cinfo->quant_tbl_ptrs[i] != NULL)
627 (void) emit_dqt(cinfo, i);
628 }
629
630 if (! cinfo->arith_code) {
631 for (i = 0; i < NUM_HUFF_TBLS; i++) {
632 if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
633 emit_dht(cinfo, i, FALSE);
634 if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
635 emit_dht(cinfo, i, TRUE);
636 }
637 }
638
639 emit_marker(cinfo, M_EOI);
640}
641
642
643/*
644 * Initialize the marker writer module.
645 */
646
Thomas G. Lane489583f1996-02-07 00:00:00 +0000647GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000648jinit_marker_writer (j_compress_ptr cinfo)
649{
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000650 my_marker_ptr marker;
651
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000652 /* Create the subobject */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000653 marker = (my_marker_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000654 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000655 SIZEOF(my_marker_writer));
656 cinfo->marker = (struct jpeg_marker_writer *) marker;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000657 /* Initialize method pointers */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000658 marker->pub.write_file_header = write_file_header;
659 marker->pub.write_frame_header = write_frame_header;
660 marker->pub.write_scan_header = write_scan_header;
661 marker->pub.write_file_trailer = write_file_trailer;
662 marker->pub.write_tables_only = write_tables_only;
663 marker->pub.write_marker_header = write_marker_header;
664 marker->pub.write_marker_byte = write_marker_byte;
665 /* Initialize private state */
666 marker->last_restart_interval = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000667}