source: golgotha/src/i4/loaders/jpg/jcmarker.cc @ 608

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