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