Path: blob/master/3rdparty/libjpeg-turbo/src/jcmarker.c
16337 views
/*1* jcmarker.c2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1998, Thomas G. Lane.5* Modified 2003-2010 by Guido Vollbeding.6* libjpeg-turbo Modifications:7* Copyright (C) 2010, D. R. Commander.8* For conditions of distribution and use, see the accompanying README.ijg9* file.10*11* This file contains routines to write JPEG datastream markers.12*/1314#define JPEG_INTERNALS15#include "jinclude.h"16#include "jpeglib.h"17#include "jpegcomp.h"181920typedef enum { /* JPEG marker codes */21M_SOF0 = 0xc0,22M_SOF1 = 0xc1,23M_SOF2 = 0xc2,24M_SOF3 = 0xc3,2526M_SOF5 = 0xc5,27M_SOF6 = 0xc6,28M_SOF7 = 0xc7,2930M_JPG = 0xc8,31M_SOF9 = 0xc9,32M_SOF10 = 0xca,33M_SOF11 = 0xcb,3435M_SOF13 = 0xcd,36M_SOF14 = 0xce,37M_SOF15 = 0xcf,3839M_DHT = 0xc4,4041M_DAC = 0xcc,4243M_RST0 = 0xd0,44M_RST1 = 0xd1,45M_RST2 = 0xd2,46M_RST3 = 0xd3,47M_RST4 = 0xd4,48M_RST5 = 0xd5,49M_RST6 = 0xd6,50M_RST7 = 0xd7,5152M_SOI = 0xd8,53M_EOI = 0xd9,54M_SOS = 0xda,55M_DQT = 0xdb,56M_DNL = 0xdc,57M_DRI = 0xdd,58M_DHP = 0xde,59M_EXP = 0xdf,6061M_APP0 = 0xe0,62M_APP1 = 0xe1,63M_APP2 = 0xe2,64M_APP3 = 0xe3,65M_APP4 = 0xe4,66M_APP5 = 0xe5,67M_APP6 = 0xe6,68M_APP7 = 0xe7,69M_APP8 = 0xe8,70M_APP9 = 0xe9,71M_APP10 = 0xea,72M_APP11 = 0xeb,73M_APP12 = 0xec,74M_APP13 = 0xed,75M_APP14 = 0xee,76M_APP15 = 0xef,7778M_JPG0 = 0xf0,79M_JPG13 = 0xfd,80M_COM = 0xfe,8182M_TEM = 0x01,8384M_ERROR = 0x10085} JPEG_MARKER;868788/* Private state */8990typedef struct {91struct jpeg_marker_writer pub; /* public fields */9293unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */94} my_marker_writer;9596typedef my_marker_writer *my_marker_ptr;979899/*100* Basic output routines.101*102* Note that we do not support suspension while writing a marker.103* Therefore, an application using suspension must ensure that there is104* enough buffer space for the initial markers (typ. 600-700 bytes) before105* calling jpeg_start_compress, and enough space to write the trailing EOI106* (a few bytes) before calling jpeg_finish_compress. Multipass compression107* modes are not supported at all with suspension, so those two are the only108* points where markers will be written.109*/110111LOCAL(void)112emit_byte (j_compress_ptr cinfo, int val)113/* Emit a byte */114{115struct jpeg_destination_mgr *dest = cinfo->dest;116117*(dest->next_output_byte)++ = (JOCTET) val;118if (--dest->free_in_buffer == 0) {119if (! (*dest->empty_output_buffer) (cinfo))120ERREXIT(cinfo, JERR_CANT_SUSPEND);121}122}123124125LOCAL(void)126emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)127/* Emit a marker code */128{129emit_byte(cinfo, 0xFF);130emit_byte(cinfo, (int) mark);131}132133134LOCAL(void)135emit_2bytes (j_compress_ptr cinfo, int value)136/* Emit a 2-byte integer; these are always MSB first in JPEG files */137{138emit_byte(cinfo, (value >> 8) & 0xFF);139emit_byte(cinfo, value & 0xFF);140}141142143/*144* Routines to write specific marker types.145*/146147LOCAL(int)148emit_dqt (j_compress_ptr cinfo, int index)149/* Emit a DQT marker */150/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */151{152JQUANT_TBL *qtbl = cinfo->quant_tbl_ptrs[index];153int prec;154int i;155156if (qtbl == NULL)157ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);158159prec = 0;160for (i = 0; i < DCTSIZE2; i++) {161if (qtbl->quantval[i] > 255)162prec = 1;163}164165if (! qtbl->sent_table) {166emit_marker(cinfo, M_DQT);167168emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);169170emit_byte(cinfo, index + (prec<<4));171172for (i = 0; i < DCTSIZE2; i++) {173/* The table entries must be emitted in zigzag order. */174unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];175if (prec)176emit_byte(cinfo, (int) (qval >> 8));177emit_byte(cinfo, (int) (qval & 0xFF));178}179180qtbl->sent_table = TRUE;181}182183return prec;184}185186187LOCAL(void)188emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)189/* Emit a DHT marker */190{191JHUFF_TBL *htbl;192int length, i;193194if (is_ac) {195htbl = cinfo->ac_huff_tbl_ptrs[index];196index += 0x10; /* output index has AC bit set */197} else {198htbl = cinfo->dc_huff_tbl_ptrs[index];199}200201if (htbl == NULL)202ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);203204if (! htbl->sent_table) {205emit_marker(cinfo, M_DHT);206207length = 0;208for (i = 1; i <= 16; i++)209length += htbl->bits[i];210211emit_2bytes(cinfo, length + 2 + 1 + 16);212emit_byte(cinfo, index);213214for (i = 1; i <= 16; i++)215emit_byte(cinfo, htbl->bits[i]);216217for (i = 0; i < length; i++)218emit_byte(cinfo, htbl->huffval[i]);219220htbl->sent_table = TRUE;221}222}223224225LOCAL(void)226emit_dac (j_compress_ptr cinfo)227/* Emit a DAC marker */228/* Since the useful info is so small, we want to emit all the tables in */229/* one DAC marker. Therefore this routine does its own scan of the table. */230{231#ifdef C_ARITH_CODING_SUPPORTED232char dc_in_use[NUM_ARITH_TBLS];233char ac_in_use[NUM_ARITH_TBLS];234int length, i;235jpeg_component_info *compptr;236237for (i = 0; i < NUM_ARITH_TBLS; i++)238dc_in_use[i] = ac_in_use[i] = 0;239240for (i = 0; i < cinfo->comps_in_scan; i++) {241compptr = cinfo->cur_comp_info[i];242/* DC needs no table for refinement scan */243if (cinfo->Ss == 0 && cinfo->Ah == 0)244dc_in_use[compptr->dc_tbl_no] = 1;245/* AC needs no table when not present */246if (cinfo->Se)247ac_in_use[compptr->ac_tbl_no] = 1;248}249250length = 0;251for (i = 0; i < NUM_ARITH_TBLS; i++)252length += dc_in_use[i] + ac_in_use[i];253254if (length) {255emit_marker(cinfo, M_DAC);256257emit_2bytes(cinfo, length*2 + 2);258259for (i = 0; i < NUM_ARITH_TBLS; i++) {260if (dc_in_use[i]) {261emit_byte(cinfo, i);262emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));263}264if (ac_in_use[i]) {265emit_byte(cinfo, i + 0x10);266emit_byte(cinfo, cinfo->arith_ac_K[i]);267}268}269}270#endif /* C_ARITH_CODING_SUPPORTED */271}272273274LOCAL(void)275emit_dri (j_compress_ptr cinfo)276/* Emit a DRI marker */277{278emit_marker(cinfo, M_DRI);279280emit_2bytes(cinfo, 4); /* fixed length */281282emit_2bytes(cinfo, (int) cinfo->restart_interval);283}284285286LOCAL(void)287emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)288/* Emit a SOF marker */289{290int ci;291jpeg_component_info *compptr;292293emit_marker(cinfo, code);294295emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */296297/* Make sure image isn't bigger than SOF field can handle */298if ((long) cinfo->_jpeg_height > 65535L ||299(long) cinfo->_jpeg_width > 65535L)300ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);301302emit_byte(cinfo, cinfo->data_precision);303emit_2bytes(cinfo, (int) cinfo->_jpeg_height);304emit_2bytes(cinfo, (int) cinfo->_jpeg_width);305306emit_byte(cinfo, cinfo->num_components);307308for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;309ci++, compptr++) {310emit_byte(cinfo, compptr->component_id);311emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);312emit_byte(cinfo, compptr->quant_tbl_no);313}314}315316317LOCAL(void)318emit_sos (j_compress_ptr cinfo)319/* Emit a SOS marker */320{321int i, td, ta;322jpeg_component_info *compptr;323324emit_marker(cinfo, M_SOS);325326emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */327328emit_byte(cinfo, cinfo->comps_in_scan);329330for (i = 0; i < cinfo->comps_in_scan; i++) {331compptr = cinfo->cur_comp_info[i];332emit_byte(cinfo, compptr->component_id);333334/* We emit 0 for unused field(s); this is recommended by the P&M text335* but does not seem to be specified in the standard.336*/337338/* DC needs no table for refinement scan */339td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;340/* AC needs no table when not present */341ta = cinfo->Se ? compptr->ac_tbl_no : 0;342343emit_byte(cinfo, (td << 4) + ta);344}345346emit_byte(cinfo, cinfo->Ss);347emit_byte(cinfo, cinfo->Se);348emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);349}350351352LOCAL(void)353emit_jfif_app0 (j_compress_ptr cinfo)354/* Emit a JFIF-compliant APP0 marker */355{356/*357* Length of APP0 block (2 bytes)358* Block ID (4 bytes - ASCII "JFIF")359* Zero byte (1 byte to terminate the ID string)360* Version Major, Minor (2 bytes - major first)361* Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)362* Xdpu (2 bytes - dots per unit horizontal)363* Ydpu (2 bytes - dots per unit vertical)364* Thumbnail X size (1 byte)365* Thumbnail Y size (1 byte)366*/367368emit_marker(cinfo, M_APP0);369370emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */371372emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */373emit_byte(cinfo, 0x46);374emit_byte(cinfo, 0x49);375emit_byte(cinfo, 0x46);376emit_byte(cinfo, 0);377emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */378emit_byte(cinfo, cinfo->JFIF_minor_version);379emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */380emit_2bytes(cinfo, (int) cinfo->X_density);381emit_2bytes(cinfo, (int) cinfo->Y_density);382emit_byte(cinfo, 0); /* No thumbnail image */383emit_byte(cinfo, 0);384}385386387LOCAL(void)388emit_adobe_app14 (j_compress_ptr cinfo)389/* Emit an Adobe APP14 marker */390{391/*392* Length of APP14 block (2 bytes)393* Block ID (5 bytes - ASCII "Adobe")394* Version Number (2 bytes - currently 100)395* Flags0 (2 bytes - currently 0)396* Flags1 (2 bytes - currently 0)397* Color transform (1 byte)398*399* Although Adobe TN 5116 mentions Version = 101, all the Adobe files400* now in circulation seem to use Version = 100, so that's what we write.401*402* We write the color transform byte as 1 if the JPEG color space is403* YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with404* whether the encoder performed a transformation, which is pretty useless.405*/406407emit_marker(cinfo, M_APP14);408409emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */410411emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */412emit_byte(cinfo, 0x64);413emit_byte(cinfo, 0x6F);414emit_byte(cinfo, 0x62);415emit_byte(cinfo, 0x65);416emit_2bytes(cinfo, 100); /* Version */417emit_2bytes(cinfo, 0); /* Flags0 */418emit_2bytes(cinfo, 0); /* Flags1 */419switch (cinfo->jpeg_color_space) {420case JCS_YCbCr:421emit_byte(cinfo, 1); /* Color transform = 1 */422break;423case JCS_YCCK:424emit_byte(cinfo, 2); /* Color transform = 2 */425break;426default:427emit_byte(cinfo, 0); /* Color transform = 0 */428break;429}430}431432433/*434* These routines allow writing an arbitrary marker with parameters.435* The only intended use is to emit COM or APPn markers after calling436* write_file_header and before calling write_frame_header.437* Other uses are not guaranteed to produce desirable results.438* Counting the parameter bytes properly is the caller's responsibility.439*/440441METHODDEF(void)442write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)443/* Emit an arbitrary marker header */444{445if (datalen > (unsigned int) 65533) /* safety check */446ERREXIT(cinfo, JERR_BAD_LENGTH);447448emit_marker(cinfo, (JPEG_MARKER) marker);449450emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */451}452453METHODDEF(void)454write_marker_byte (j_compress_ptr cinfo, int val)455/* Emit one byte of marker parameters following write_marker_header */456{457emit_byte(cinfo, val);458}459460461/*462* Write datastream header.463* This consists of an SOI and optional APPn markers.464* We recommend use of the JFIF marker, but not the Adobe marker,465* when using YCbCr or grayscale data. The JFIF marker should NOT466* be used for any other JPEG colorspace. The Adobe marker is helpful467* to distinguish RGB, CMYK, and YCCK colorspaces.468* Note that an application can write additional header markers after469* jpeg_start_compress returns.470*/471472METHODDEF(void)473write_file_header (j_compress_ptr cinfo)474{475my_marker_ptr marker = (my_marker_ptr) cinfo->marker;476477emit_marker(cinfo, M_SOI); /* first the SOI */478479/* SOI is defined to reset restart interval to 0 */480marker->last_restart_interval = 0;481482if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */483emit_jfif_app0(cinfo);484if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */485emit_adobe_app14(cinfo);486}487488489/*490* Write frame header.491* This consists of DQT and SOFn markers.492* Note that we do not emit the SOF until we have emitted the DQT(s).493* This avoids compatibility problems with incorrect implementations that494* try to error-check the quant table numbers as soon as they see the SOF.495*/496497METHODDEF(void)498write_frame_header (j_compress_ptr cinfo)499{500int ci, prec;501boolean is_baseline;502jpeg_component_info *compptr;503504/* Emit DQT for each quantization table.505* Note that emit_dqt() suppresses any duplicate tables.506*/507prec = 0;508for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;509ci++, compptr++) {510prec += emit_dqt(cinfo, compptr->quant_tbl_no);511}512/* now prec is nonzero iff there are any 16-bit quant tables. */513514/* Check for a non-baseline specification.515* Note we assume that Huffman table numbers won't be changed later.516*/517if (cinfo->arith_code || cinfo->progressive_mode ||518cinfo->data_precision != 8) {519is_baseline = FALSE;520} else {521is_baseline = TRUE;522for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;523ci++, compptr++) {524if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)525is_baseline = FALSE;526}527if (prec && is_baseline) {528is_baseline = FALSE;529/* If it's baseline except for quantizer size, warn the user */530TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);531}532}533534/* Emit the proper SOF marker */535if (cinfo->arith_code) {536if (cinfo->progressive_mode)537emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */538else539emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */540} else {541if (cinfo->progressive_mode)542emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */543else if (is_baseline)544emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */545else546emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */547}548}549550551/*552* Write scan header.553* This consists of DHT or DAC markers, optional DRI, and SOS.554* Compressed data will be written following the SOS.555*/556557METHODDEF(void)558write_scan_header (j_compress_ptr cinfo)559{560my_marker_ptr marker = (my_marker_ptr) cinfo->marker;561int i;562jpeg_component_info *compptr;563564if (cinfo->arith_code) {565/* Emit arith conditioning info. We may have some duplication566* if the file has multiple scans, but it's so small it's hardly567* worth worrying about.568*/569emit_dac(cinfo);570} else {571/* Emit Huffman tables.572* Note that emit_dht() suppresses any duplicate tables.573*/574for (i = 0; i < cinfo->comps_in_scan; i++) {575compptr = cinfo->cur_comp_info[i];576/* DC needs no table for refinement scan */577if (cinfo->Ss == 0 && cinfo->Ah == 0)578emit_dht(cinfo, compptr->dc_tbl_no, FALSE);579/* AC needs no table when not present */580if (cinfo->Se)581emit_dht(cinfo, compptr->ac_tbl_no, TRUE);582}583}584585/* Emit DRI if required --- note that DRI value could change for each scan.586* We avoid wasting space with unnecessary DRIs, however.587*/588if (cinfo->restart_interval != marker->last_restart_interval) {589emit_dri(cinfo);590marker->last_restart_interval = cinfo->restart_interval;591}592593emit_sos(cinfo);594}595596597/*598* Write datastream trailer.599*/600601METHODDEF(void)602write_file_trailer (j_compress_ptr cinfo)603{604emit_marker(cinfo, M_EOI);605}606607608/*609* Write an abbreviated table-specification datastream.610* This consists of SOI, DQT and DHT tables, and EOI.611* Any table that is defined and not marked sent_table = TRUE will be612* emitted. Note that all tables will be marked sent_table = TRUE at exit.613*/614615METHODDEF(void)616write_tables_only (j_compress_ptr cinfo)617{618int i;619620emit_marker(cinfo, M_SOI);621622for (i = 0; i < NUM_QUANT_TBLS; i++) {623if (cinfo->quant_tbl_ptrs[i] != NULL)624(void) emit_dqt(cinfo, i);625}626627if (! cinfo->arith_code) {628for (i = 0; i < NUM_HUFF_TBLS; i++) {629if (cinfo->dc_huff_tbl_ptrs[i] != NULL)630emit_dht(cinfo, i, FALSE);631if (cinfo->ac_huff_tbl_ptrs[i] != NULL)632emit_dht(cinfo, i, TRUE);633}634}635636emit_marker(cinfo, M_EOI);637}638639640/*641* Initialize the marker writer module.642*/643644GLOBAL(void)645jinit_marker_writer (j_compress_ptr cinfo)646{647my_marker_ptr marker;648649/* Create the subobject */650marker = (my_marker_ptr)651(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,652sizeof(my_marker_writer));653cinfo->marker = (struct jpeg_marker_writer *) marker;654/* Initialize method pointers */655marker->pub.write_file_header = write_file_header;656marker->pub.write_frame_header = write_frame_header;657marker->pub.write_scan_header = write_scan_header;658marker->pub.write_file_trailer = write_file_trailer;659marker->pub.write_tables_only = write_tables_only;660marker->pub.write_marker_header = write_marker_header;661marker->pub.write_marker_byte = write_marker_byte;662/* Initialize private state */663marker->last_restart_interval = 0;664}665666667