nuclear@2: /* nuclear@2: * jcmarker.c nuclear@2: * nuclear@2: * Copyright (C) 1991-1998, Thomas G. Lane. nuclear@2: * This file is part of the Independent JPEG Group's software. nuclear@2: * For conditions of distribution and use, see the accompanying README file. nuclear@2: * nuclear@2: * This file contains routines to write JPEG datastream markers. nuclear@2: */ nuclear@2: nuclear@2: #define JPEG_INTERNALS nuclear@2: #include "jinclude.h" nuclear@2: #include "jpeglib.h" nuclear@2: nuclear@2: nuclear@2: typedef enum { /* JPEG marker codes */ nuclear@2: M_SOF0 = 0xc0, nuclear@2: M_SOF1 = 0xc1, nuclear@2: M_SOF2 = 0xc2, nuclear@2: M_SOF3 = 0xc3, nuclear@2: nuclear@2: M_SOF5 = 0xc5, nuclear@2: M_SOF6 = 0xc6, nuclear@2: M_SOF7 = 0xc7, nuclear@2: nuclear@2: M_JPG = 0xc8, nuclear@2: M_SOF9 = 0xc9, nuclear@2: M_SOF10 = 0xca, nuclear@2: M_SOF11 = 0xcb, nuclear@2: nuclear@2: M_SOF13 = 0xcd, nuclear@2: M_SOF14 = 0xce, nuclear@2: M_SOF15 = 0xcf, nuclear@2: nuclear@2: M_DHT = 0xc4, nuclear@2: nuclear@2: M_DAC = 0xcc, nuclear@2: nuclear@2: M_RST0 = 0xd0, nuclear@2: M_RST1 = 0xd1, nuclear@2: M_RST2 = 0xd2, nuclear@2: M_RST3 = 0xd3, nuclear@2: M_RST4 = 0xd4, nuclear@2: M_RST5 = 0xd5, nuclear@2: M_RST6 = 0xd6, nuclear@2: M_RST7 = 0xd7, nuclear@2: nuclear@2: M_SOI = 0xd8, nuclear@2: M_EOI = 0xd9, nuclear@2: M_SOS = 0xda, nuclear@2: M_DQT = 0xdb, nuclear@2: M_DNL = 0xdc, nuclear@2: M_DRI = 0xdd, nuclear@2: M_DHP = 0xde, nuclear@2: M_EXP = 0xdf, nuclear@2: nuclear@2: M_APP0 = 0xe0, nuclear@2: M_APP1 = 0xe1, nuclear@2: M_APP2 = 0xe2, nuclear@2: M_APP3 = 0xe3, nuclear@2: M_APP4 = 0xe4, nuclear@2: M_APP5 = 0xe5, nuclear@2: M_APP6 = 0xe6, nuclear@2: M_APP7 = 0xe7, nuclear@2: M_APP8 = 0xe8, nuclear@2: M_APP9 = 0xe9, nuclear@2: M_APP10 = 0xea, nuclear@2: M_APP11 = 0xeb, nuclear@2: M_APP12 = 0xec, nuclear@2: M_APP13 = 0xed, nuclear@2: M_APP14 = 0xee, nuclear@2: M_APP15 = 0xef, nuclear@2: nuclear@2: M_JPG0 = 0xf0, nuclear@2: M_JPG13 = 0xfd, nuclear@2: M_COM = 0xfe, nuclear@2: nuclear@2: M_TEM = 0x01, nuclear@2: nuclear@2: M_ERROR = 0x100 nuclear@2: } JPEG_MARKER; nuclear@2: nuclear@2: nuclear@2: /* Private state */ nuclear@2: nuclear@2: typedef struct { nuclear@2: struct jpeg_marker_writer pub; /* public fields */ nuclear@2: nuclear@2: unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */ nuclear@2: } my_marker_writer; nuclear@2: nuclear@2: typedef my_marker_writer * my_marker_ptr; nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Basic output routines. nuclear@2: * nuclear@2: * Note that we do not support suspension while writing a marker. nuclear@2: * Therefore, an application using suspension must ensure that there is nuclear@2: * enough buffer space for the initial markers (typ. 600-700 bytes) before nuclear@2: * calling jpeg_start_compress, and enough space to write the trailing EOI nuclear@2: * (a few bytes) before calling jpeg_finish_compress. Multipass compression nuclear@2: * modes are not supported at all with suspension, so those two are the only nuclear@2: * points where markers will be written. nuclear@2: */ nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_byte (j_compress_ptr cinfo, int val) nuclear@2: /* Emit a byte */ nuclear@2: { nuclear@2: struct jpeg_destination_mgr * dest = cinfo->dest; nuclear@2: nuclear@2: *(dest->next_output_byte)++ = (JOCTET) val; nuclear@2: if (--dest->free_in_buffer == 0) { nuclear@2: if (! (*dest->empty_output_buffer) (cinfo)) nuclear@2: ERREXIT(cinfo, JERR_CANT_SUSPEND); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark) nuclear@2: /* Emit a marker code */ nuclear@2: { nuclear@2: emit_byte(cinfo, 0xFF); nuclear@2: emit_byte(cinfo, (int) mark); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_2bytes (j_compress_ptr cinfo, int value) nuclear@2: /* Emit a 2-byte integer; these are always MSB first in JPEG files */ nuclear@2: { nuclear@2: emit_byte(cinfo, (value >> 8) & 0xFF); nuclear@2: emit_byte(cinfo, value & 0xFF); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Routines to write specific marker types. nuclear@2: */ nuclear@2: nuclear@2: LOCAL(int) nuclear@2: emit_dqt (j_compress_ptr cinfo, int index) nuclear@2: /* Emit a DQT marker */ nuclear@2: /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */ nuclear@2: { nuclear@2: JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index]; nuclear@2: int prec; nuclear@2: int i; nuclear@2: nuclear@2: if (qtbl == NULL) nuclear@2: ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index); nuclear@2: nuclear@2: prec = 0; nuclear@2: for (i = 0; i < DCTSIZE2; i++) { nuclear@2: if (qtbl->quantval[i] > 255) nuclear@2: prec = 1; nuclear@2: } nuclear@2: nuclear@2: if (! qtbl->sent_table) { nuclear@2: emit_marker(cinfo, M_DQT); nuclear@2: nuclear@2: emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2); nuclear@2: nuclear@2: emit_byte(cinfo, index + (prec<<4)); nuclear@2: nuclear@2: for (i = 0; i < DCTSIZE2; i++) { nuclear@2: /* The table entries must be emitted in zigzag order. */ nuclear@2: unsigned int qval = qtbl->quantval[jpeg_natural_order[i]]; nuclear@2: if (prec) nuclear@2: emit_byte(cinfo, (int) (qval >> 8)); nuclear@2: emit_byte(cinfo, (int) (qval & 0xFF)); nuclear@2: } nuclear@2: nuclear@2: qtbl->sent_table = TRUE; nuclear@2: } nuclear@2: nuclear@2: return prec; nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_dht (j_compress_ptr cinfo, int index, boolean is_ac) nuclear@2: /* Emit a DHT marker */ nuclear@2: { nuclear@2: JHUFF_TBL * htbl; nuclear@2: int length, i; nuclear@2: nuclear@2: if (is_ac) { nuclear@2: htbl = cinfo->ac_huff_tbl_ptrs[index]; nuclear@2: index += 0x10; /* output index has AC bit set */ nuclear@2: } else { nuclear@2: htbl = cinfo->dc_huff_tbl_ptrs[index]; nuclear@2: } nuclear@2: nuclear@2: if (htbl == NULL) nuclear@2: ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index); nuclear@2: nuclear@2: if (! htbl->sent_table) { nuclear@2: emit_marker(cinfo, M_DHT); nuclear@2: nuclear@2: length = 0; nuclear@2: for (i = 1; i <= 16; i++) nuclear@2: length += htbl->bits[i]; nuclear@2: nuclear@2: emit_2bytes(cinfo, length + 2 + 1 + 16); nuclear@2: emit_byte(cinfo, index); nuclear@2: nuclear@2: for (i = 1; i <= 16; i++) nuclear@2: emit_byte(cinfo, htbl->bits[i]); nuclear@2: nuclear@2: for (i = 0; i < length; i++) nuclear@2: emit_byte(cinfo, htbl->huffval[i]); nuclear@2: nuclear@2: htbl->sent_table = TRUE; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_dac (j_compress_ptr cinfo) nuclear@2: /* Emit a DAC marker */ nuclear@2: /* Since the useful info is so small, we want to emit all the tables in */ nuclear@2: /* one DAC marker. Therefore this routine does its own scan of the table. */ nuclear@2: { nuclear@2: #ifdef C_ARITH_CODING_SUPPORTED nuclear@2: char dc_in_use[NUM_ARITH_TBLS]; nuclear@2: char ac_in_use[NUM_ARITH_TBLS]; nuclear@2: int length, i; nuclear@2: jpeg_component_info *compptr; nuclear@2: nuclear@2: for (i = 0; i < NUM_ARITH_TBLS; i++) nuclear@2: dc_in_use[i] = ac_in_use[i] = 0; nuclear@2: nuclear@2: for (i = 0; i < cinfo->comps_in_scan; i++) { nuclear@2: compptr = cinfo->cur_comp_info[i]; nuclear@2: dc_in_use[compptr->dc_tbl_no] = 1; nuclear@2: ac_in_use[compptr->ac_tbl_no] = 1; nuclear@2: } nuclear@2: nuclear@2: length = 0; nuclear@2: for (i = 0; i < NUM_ARITH_TBLS; i++) nuclear@2: length += dc_in_use[i] + ac_in_use[i]; nuclear@2: nuclear@2: emit_marker(cinfo, M_DAC); nuclear@2: nuclear@2: emit_2bytes(cinfo, length*2 + 2); nuclear@2: nuclear@2: for (i = 0; i < NUM_ARITH_TBLS; i++) { nuclear@2: if (dc_in_use[i]) { nuclear@2: emit_byte(cinfo, i); nuclear@2: emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4)); nuclear@2: } nuclear@2: if (ac_in_use[i]) { nuclear@2: emit_byte(cinfo, i + 0x10); nuclear@2: emit_byte(cinfo, cinfo->arith_ac_K[i]); nuclear@2: } nuclear@2: } nuclear@2: #endif /* C_ARITH_CODING_SUPPORTED */ nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_dri (j_compress_ptr cinfo) nuclear@2: /* Emit a DRI marker */ nuclear@2: { nuclear@2: emit_marker(cinfo, M_DRI); nuclear@2: nuclear@2: emit_2bytes(cinfo, 4); /* fixed length */ nuclear@2: nuclear@2: emit_2bytes(cinfo, (int) cinfo->restart_interval); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_sof (j_compress_ptr cinfo, JPEG_MARKER code) nuclear@2: /* Emit a SOF marker */ nuclear@2: { nuclear@2: int ci; nuclear@2: jpeg_component_info *compptr; nuclear@2: nuclear@2: emit_marker(cinfo, code); nuclear@2: nuclear@2: emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */ nuclear@2: nuclear@2: /* Make sure image isn't bigger than SOF field can handle */ nuclear@2: if ((long) cinfo->image_height > 65535L || nuclear@2: (long) cinfo->image_width > 65535L) nuclear@2: ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535); nuclear@2: nuclear@2: emit_byte(cinfo, cinfo->data_precision); nuclear@2: emit_2bytes(cinfo, (int) cinfo->image_height); nuclear@2: emit_2bytes(cinfo, (int) cinfo->image_width); nuclear@2: nuclear@2: emit_byte(cinfo, cinfo->num_components); nuclear@2: nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: emit_byte(cinfo, compptr->component_id); nuclear@2: emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor); nuclear@2: emit_byte(cinfo, compptr->quant_tbl_no); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_sos (j_compress_ptr cinfo) nuclear@2: /* Emit a SOS marker */ nuclear@2: { nuclear@2: int i, td, ta; nuclear@2: jpeg_component_info *compptr; nuclear@2: nuclear@2: emit_marker(cinfo, M_SOS); nuclear@2: nuclear@2: emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */ nuclear@2: nuclear@2: emit_byte(cinfo, cinfo->comps_in_scan); nuclear@2: nuclear@2: for (i = 0; i < cinfo->comps_in_scan; i++) { nuclear@2: compptr = cinfo->cur_comp_info[i]; nuclear@2: emit_byte(cinfo, compptr->component_id); nuclear@2: td = compptr->dc_tbl_no; nuclear@2: ta = compptr->ac_tbl_no; nuclear@2: if (cinfo->progressive_mode) { nuclear@2: /* Progressive mode: only DC or only AC tables are used in one scan; nuclear@2: * furthermore, Huffman coding of DC refinement uses no table at all. nuclear@2: * We emit 0 for unused field(s); this is recommended by the P&M text nuclear@2: * but does not seem to be specified in the standard. nuclear@2: */ nuclear@2: if (cinfo->Ss == 0) { nuclear@2: ta = 0; /* DC scan */ nuclear@2: if (cinfo->Ah != 0 && !cinfo->arith_code) nuclear@2: td = 0; /* no DC table either */ nuclear@2: } else { nuclear@2: td = 0; /* AC scan */ nuclear@2: } nuclear@2: } nuclear@2: emit_byte(cinfo, (td << 4) + ta); nuclear@2: } nuclear@2: nuclear@2: emit_byte(cinfo, cinfo->Ss); nuclear@2: emit_byte(cinfo, cinfo->Se); nuclear@2: emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_jfif_app0 (j_compress_ptr cinfo) nuclear@2: /* Emit a JFIF-compliant APP0 marker */ nuclear@2: { nuclear@2: /* nuclear@2: * Length of APP0 block (2 bytes) nuclear@2: * Block ID (4 bytes - ASCII "JFIF") nuclear@2: * Zero byte (1 byte to terminate the ID string) nuclear@2: * Version Major, Minor (2 bytes - major first) nuclear@2: * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm) nuclear@2: * Xdpu (2 bytes - dots per unit horizontal) nuclear@2: * Ydpu (2 bytes - dots per unit vertical) nuclear@2: * Thumbnail X size (1 byte) nuclear@2: * Thumbnail Y size (1 byte) nuclear@2: */ nuclear@2: nuclear@2: emit_marker(cinfo, M_APP0); nuclear@2: nuclear@2: emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */ nuclear@2: nuclear@2: emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */ nuclear@2: emit_byte(cinfo, 0x46); nuclear@2: emit_byte(cinfo, 0x49); nuclear@2: emit_byte(cinfo, 0x46); nuclear@2: emit_byte(cinfo, 0); nuclear@2: emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */ nuclear@2: emit_byte(cinfo, cinfo->JFIF_minor_version); nuclear@2: emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */ nuclear@2: emit_2bytes(cinfo, (int) cinfo->X_density); nuclear@2: emit_2bytes(cinfo, (int) cinfo->Y_density); nuclear@2: emit_byte(cinfo, 0); /* No thumbnail image */ nuclear@2: emit_byte(cinfo, 0); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: LOCAL(void) nuclear@2: emit_adobe_app14 (j_compress_ptr cinfo) nuclear@2: /* Emit an Adobe APP14 marker */ nuclear@2: { nuclear@2: /* nuclear@2: * Length of APP14 block (2 bytes) nuclear@2: * Block ID (5 bytes - ASCII "Adobe") nuclear@2: * Version Number (2 bytes - currently 100) nuclear@2: * Flags0 (2 bytes - currently 0) nuclear@2: * Flags1 (2 bytes - currently 0) nuclear@2: * Color transform (1 byte) nuclear@2: * nuclear@2: * Although Adobe TN 5116 mentions Version = 101, all the Adobe files nuclear@2: * now in circulation seem to use Version = 100, so that's what we write. nuclear@2: * nuclear@2: * We write the color transform byte as 1 if the JPEG color space is nuclear@2: * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with nuclear@2: * whether the encoder performed a transformation, which is pretty useless. nuclear@2: */ nuclear@2: nuclear@2: emit_marker(cinfo, M_APP14); nuclear@2: nuclear@2: emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */ nuclear@2: nuclear@2: emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */ nuclear@2: emit_byte(cinfo, 0x64); nuclear@2: emit_byte(cinfo, 0x6F); nuclear@2: emit_byte(cinfo, 0x62); nuclear@2: emit_byte(cinfo, 0x65); nuclear@2: emit_2bytes(cinfo, 100); /* Version */ nuclear@2: emit_2bytes(cinfo, 0); /* Flags0 */ nuclear@2: emit_2bytes(cinfo, 0); /* Flags1 */ nuclear@2: switch (cinfo->jpeg_color_space) { nuclear@2: case JCS_YCbCr: nuclear@2: emit_byte(cinfo, 1); /* Color transform = 1 */ nuclear@2: break; nuclear@2: case JCS_YCCK: nuclear@2: emit_byte(cinfo, 2); /* Color transform = 2 */ nuclear@2: break; nuclear@2: default: nuclear@2: emit_byte(cinfo, 0); /* Color transform = 0 */ nuclear@2: break; nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * These routines allow writing an arbitrary marker with parameters. nuclear@2: * The only intended use is to emit COM or APPn markers after calling nuclear@2: * write_file_header and before calling write_frame_header. nuclear@2: * Other uses are not guaranteed to produce desirable results. nuclear@2: * Counting the parameter bytes properly is the caller's responsibility. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen) nuclear@2: /* Emit an arbitrary marker header */ nuclear@2: { nuclear@2: if (datalen > (unsigned int) 65533) /* safety check */ nuclear@2: ERREXIT(cinfo, JERR_BAD_LENGTH); nuclear@2: nuclear@2: emit_marker(cinfo, (JPEG_MARKER) marker); nuclear@2: nuclear@2: emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */ nuclear@2: } nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_marker_byte (j_compress_ptr cinfo, int val) nuclear@2: /* Emit one byte of marker parameters following write_marker_header */ nuclear@2: { nuclear@2: emit_byte(cinfo, val); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Write datastream header. nuclear@2: * This consists of an SOI and optional APPn markers. nuclear@2: * We recommend use of the JFIF marker, but not the Adobe marker, nuclear@2: * when using YCbCr or grayscale data. The JFIF marker should NOT nuclear@2: * be used for any other JPEG colorspace. The Adobe marker is helpful nuclear@2: * to distinguish RGB, CMYK, and YCCK colorspaces. nuclear@2: * Note that an application can write additional header markers after nuclear@2: * jpeg_start_compress returns. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_file_header (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_marker_ptr marker = (my_marker_ptr) cinfo->marker; nuclear@2: nuclear@2: emit_marker(cinfo, M_SOI); /* first the SOI */ nuclear@2: nuclear@2: /* SOI is defined to reset restart interval to 0 */ nuclear@2: marker->last_restart_interval = 0; nuclear@2: nuclear@2: if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */ nuclear@2: emit_jfif_app0(cinfo); nuclear@2: if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */ nuclear@2: emit_adobe_app14(cinfo); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Write frame header. nuclear@2: * This consists of DQT and SOFn markers. nuclear@2: * Note that we do not emit the SOF until we have emitted the DQT(s). nuclear@2: * This avoids compatibility problems with incorrect implementations that nuclear@2: * try to error-check the quant table numbers as soon as they see the SOF. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_frame_header (j_compress_ptr cinfo) nuclear@2: { nuclear@2: int ci, prec; nuclear@2: boolean is_baseline; nuclear@2: jpeg_component_info *compptr; nuclear@2: nuclear@2: /* Emit DQT for each quantization table. nuclear@2: * Note that emit_dqt() suppresses any duplicate tables. nuclear@2: */ nuclear@2: prec = 0; nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: prec += emit_dqt(cinfo, compptr->quant_tbl_no); nuclear@2: } nuclear@2: /* now prec is nonzero iff there are any 16-bit quant tables. */ nuclear@2: nuclear@2: /* Check for a non-baseline specification. nuclear@2: * Note we assume that Huffman table numbers won't be changed later. nuclear@2: */ nuclear@2: if (cinfo->arith_code || cinfo->progressive_mode || nuclear@2: cinfo->data_precision != 8) { nuclear@2: is_baseline = FALSE; nuclear@2: } else { nuclear@2: is_baseline = TRUE; nuclear@2: for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; nuclear@2: ci++, compptr++) { nuclear@2: if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1) nuclear@2: is_baseline = FALSE; nuclear@2: } nuclear@2: if (prec && is_baseline) { nuclear@2: is_baseline = FALSE; nuclear@2: /* If it's baseline except for quantizer size, warn the user */ nuclear@2: TRACEMS(cinfo, 0, JTRC_16BIT_TABLES); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* Emit the proper SOF marker */ nuclear@2: if (cinfo->arith_code) { nuclear@2: emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */ nuclear@2: } else { nuclear@2: if (cinfo->progressive_mode) nuclear@2: emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */ nuclear@2: else if (is_baseline) nuclear@2: emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */ nuclear@2: else nuclear@2: emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */ nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Write scan header. nuclear@2: * This consists of DHT or DAC markers, optional DRI, and SOS. nuclear@2: * Compressed data will be written following the SOS. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_scan_header (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_marker_ptr marker = (my_marker_ptr) cinfo->marker; nuclear@2: int i; nuclear@2: jpeg_component_info *compptr; nuclear@2: nuclear@2: if (cinfo->arith_code) { nuclear@2: /* Emit arith conditioning info. We may have some duplication nuclear@2: * if the file has multiple scans, but it's so small it's hardly nuclear@2: * worth worrying about. nuclear@2: */ nuclear@2: emit_dac(cinfo); nuclear@2: } else { nuclear@2: /* Emit Huffman tables. nuclear@2: * Note that emit_dht() suppresses any duplicate tables. nuclear@2: */ nuclear@2: for (i = 0; i < cinfo->comps_in_scan; i++) { nuclear@2: compptr = cinfo->cur_comp_info[i]; nuclear@2: if (cinfo->progressive_mode) { nuclear@2: /* Progressive mode: only DC or only AC tables are used in one scan */ nuclear@2: if (cinfo->Ss == 0) { nuclear@2: if (cinfo->Ah == 0) /* DC needs no table for refinement scan */ nuclear@2: emit_dht(cinfo, compptr->dc_tbl_no, FALSE); nuclear@2: } else { nuclear@2: emit_dht(cinfo, compptr->ac_tbl_no, TRUE); nuclear@2: } nuclear@2: } else { nuclear@2: /* Sequential mode: need both DC and AC tables */ nuclear@2: emit_dht(cinfo, compptr->dc_tbl_no, FALSE); nuclear@2: emit_dht(cinfo, compptr->ac_tbl_no, TRUE); nuclear@2: } nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: /* Emit DRI if required --- note that DRI value could change for each scan. nuclear@2: * We avoid wasting space with unnecessary DRIs, however. nuclear@2: */ nuclear@2: if (cinfo->restart_interval != marker->last_restart_interval) { nuclear@2: emit_dri(cinfo); nuclear@2: marker->last_restart_interval = cinfo->restart_interval; nuclear@2: } nuclear@2: nuclear@2: emit_sos(cinfo); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Write datastream trailer. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_file_trailer (j_compress_ptr cinfo) nuclear@2: { nuclear@2: emit_marker(cinfo, M_EOI); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Write an abbreviated table-specification datastream. nuclear@2: * This consists of SOI, DQT and DHT tables, and EOI. nuclear@2: * Any table that is defined and not marked sent_table = TRUE will be nuclear@2: * emitted. Note that all tables will be marked sent_table = TRUE at exit. nuclear@2: */ nuclear@2: nuclear@2: METHODDEF(void) nuclear@2: write_tables_only (j_compress_ptr cinfo) nuclear@2: { nuclear@2: int i; nuclear@2: nuclear@2: emit_marker(cinfo, M_SOI); nuclear@2: nuclear@2: for (i = 0; i < NUM_QUANT_TBLS; i++) { nuclear@2: if (cinfo->quant_tbl_ptrs[i] != NULL) nuclear@2: (void) emit_dqt(cinfo, i); nuclear@2: } nuclear@2: nuclear@2: if (! cinfo->arith_code) { nuclear@2: for (i = 0; i < NUM_HUFF_TBLS; i++) { nuclear@2: if (cinfo->dc_huff_tbl_ptrs[i] != NULL) nuclear@2: emit_dht(cinfo, i, FALSE); nuclear@2: if (cinfo->ac_huff_tbl_ptrs[i] != NULL) nuclear@2: emit_dht(cinfo, i, TRUE); nuclear@2: } nuclear@2: } nuclear@2: nuclear@2: emit_marker(cinfo, M_EOI); nuclear@2: } nuclear@2: nuclear@2: nuclear@2: /* nuclear@2: * Initialize the marker writer module. nuclear@2: */ nuclear@2: nuclear@2: GLOBAL(void) nuclear@2: jinit_marker_writer (j_compress_ptr cinfo) nuclear@2: { nuclear@2: my_marker_ptr marker; nuclear@2: nuclear@2: /* Create the subobject */ nuclear@2: marker = (my_marker_ptr) nuclear@2: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, nuclear@2: SIZEOF(my_marker_writer)); nuclear@2: cinfo->marker = (struct jpeg_marker_writer *) marker; nuclear@2: /* Initialize method pointers */ nuclear@2: marker->pub.write_file_header = write_file_header; nuclear@2: marker->pub.write_frame_header = write_frame_header; nuclear@2: marker->pub.write_scan_header = write_scan_header; nuclear@2: marker->pub.write_file_trailer = write_file_trailer; nuclear@2: marker->pub.write_tables_only = write_tables_only; nuclear@2: marker->pub.write_marker_header = write_marker_header; nuclear@2: marker->pub.write_marker_byte = write_marker_byte; nuclear@2: /* Initialize private state */ nuclear@2: marker->last_restart_interval = 0; nuclear@2: }