source: trunk/third/firefox/jpeg/jcmarker.c @ 21695

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