Path: blob/master/src/java.desktop/share/native/libjavajpeg/jcmarker.c
41149 views
/*1* reserved comment block2* DO NOT REMOVE OR ALTER!3*/4/*5* jcmarker.c6*7* Copyright (C) 1991-1998, Thomas G. Lane.8* This file is part of the Independent JPEG Group's software.9* For conditions of distribution and use, see the accompanying README file.10*11* This file contains routines to write JPEG datastream markers.12*/1314#define JPEG_INTERNALS15#include "jinclude.h"16#include "jpeglib.h"171819typedef enum { /* JPEG marker codes */20M_SOF0 = 0xc0,21M_SOF1 = 0xc1,22M_SOF2 = 0xc2,23M_SOF3 = 0xc3,2425M_SOF5 = 0xc5,26M_SOF6 = 0xc6,27M_SOF7 = 0xc7,2829M_JPG = 0xc8,30M_SOF9 = 0xc9,31M_SOF10 = 0xca,32M_SOF11 = 0xcb,3334M_SOF13 = 0xcd,35M_SOF14 = 0xce,36M_SOF15 = 0xcf,3738M_DHT = 0xc4,3940M_DAC = 0xcc,4142M_RST0 = 0xd0,43M_RST1 = 0xd1,44M_RST2 = 0xd2,45M_RST3 = 0xd3,46M_RST4 = 0xd4,47M_RST5 = 0xd5,48M_RST6 = 0xd6,49M_RST7 = 0xd7,5051M_SOI = 0xd8,52M_EOI = 0xd9,53M_SOS = 0xda,54M_DQT = 0xdb,55M_DNL = 0xdc,56M_DRI = 0xdd,57M_DHP = 0xde,58M_EXP = 0xdf,5960M_APP0 = 0xe0,61M_APP1 = 0xe1,62M_APP2 = 0xe2,63M_APP3 = 0xe3,64M_APP4 = 0xe4,65M_APP5 = 0xe5,66M_APP6 = 0xe6,67M_APP7 = 0xe7,68M_APP8 = 0xe8,69M_APP9 = 0xe9,70M_APP10 = 0xea,71M_APP11 = 0xeb,72M_APP12 = 0xec,73M_APP13 = 0xed,74M_APP14 = 0xee,75M_APP15 = 0xef,7677M_JPG0 = 0xf0,78M_JPG13 = 0xfd,79M_COM = 0xfe,8081M_TEM = 0x01,8283M_ERROR = 0x10084} JPEG_MARKER;858687/* Private state */8889typedef struct {90struct jpeg_marker_writer pub; /* public fields */9192unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */93} my_marker_writer;9495typedef my_marker_writer * my_marker_ptr;969798/*99* Basic output routines.100*101* Note that we do not support suspension while writing a marker.102* Therefore, an application using suspension must ensure that there is103* enough buffer space for the initial markers (typ. 600-700 bytes) before104* calling jpeg_start_compress, and enough space to write the trailing EOI105* (a few bytes) before calling jpeg_finish_compress. Multipass compression106* modes are not supported at all with suspension, so those two are the only107* points where markers will be written.108*/109110LOCAL(void)111emit_byte (j_compress_ptr cinfo, int val)112/* Emit a byte */113{114struct jpeg_destination_mgr * dest = cinfo->dest;115116*(dest->next_output_byte)++ = (JOCTET) val;117if (--dest->free_in_buffer == 0) {118if (! (*dest->empty_output_buffer) (cinfo))119ERREXIT(cinfo, JERR_CANT_SUSPEND);120}121}122123124LOCAL(void)125emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)126/* Emit a marker code */127{128emit_byte(cinfo, 0xFF);129emit_byte(cinfo, (int) mark);130}131132133LOCAL(void)134emit_2bytes (j_compress_ptr cinfo, int value)135/* Emit a 2-byte integer; these are always MSB first in JPEG files */136{137emit_byte(cinfo, (value >> 8) & 0xFF);138emit_byte(cinfo, value & 0xFF);139}140141142/*143* Routines to write specific marker types.144*/145146LOCAL(int)147emit_dqt (j_compress_ptr cinfo, int index)148/* Emit a DQT marker */149/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */150{151JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];152int prec;153int i;154155if (qtbl == NULL)156ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);157158prec = 0;159for (i = 0; i < DCTSIZE2; i++) {160if (qtbl->quantval[i] > 255)161prec = 1;162}163164if (! qtbl->sent_table) {165emit_marker(cinfo, M_DQT);166167emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);168169emit_byte(cinfo, index + (prec<<4));170171for (i = 0; i < DCTSIZE2; i++) {172/* The table entries must be emitted in zigzag order. */173unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];174if (prec)175emit_byte(cinfo, (int) (qval >> 8));176emit_byte(cinfo, (int) (qval & 0xFF));177}178179qtbl->sent_table = TRUE;180}181182return prec;183}184185186LOCAL(void)187emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)188/* Emit a DHT marker */189{190JHUFF_TBL * htbl;191int length, i;192193if (is_ac) {194htbl = cinfo->ac_huff_tbl_ptrs[index];195index += 0x10; /* output index has AC bit set */196} else {197htbl = cinfo->dc_huff_tbl_ptrs[index];198}199200if (htbl == NULL)201ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);202203if (! htbl->sent_table) {204emit_marker(cinfo, M_DHT);205206length = 0;207for (i = 1; i <= 16; i++)208length += htbl->bits[i];209210emit_2bytes(cinfo, length + 2 + 1 + 16);211emit_byte(cinfo, index);212213for (i = 1; i <= 16; i++)214emit_byte(cinfo, htbl->bits[i]);215216for (i = 0; i < length; i++)217emit_byte(cinfo, htbl->huffval[i]);218219htbl->sent_table = TRUE;220}221}222223224LOCAL(void)225emit_dac (j_compress_ptr cinfo)226/* Emit a DAC marker */227/* Since the useful info is so small, we want to emit all the tables in */228/* one DAC marker. Therefore this routine does its own scan of the table. */229{230#ifdef C_ARITH_CODING_SUPPORTED231char dc_in_use[NUM_ARITH_TBLS];232char ac_in_use[NUM_ARITH_TBLS];233int length, i;234jpeg_component_info *compptr;235236for (i = 0; i < NUM_ARITH_TBLS; i++)237dc_in_use[i] = ac_in_use[i] = 0;238239for (i = 0; i < cinfo->comps_in_scan; i++) {240compptr = cinfo->cur_comp_info[i];241dc_in_use[compptr->dc_tbl_no] = 1;242ac_in_use[compptr->ac_tbl_no] = 1;243}244245length = 0;246for (i = 0; i < NUM_ARITH_TBLS; i++)247length += dc_in_use[i] + ac_in_use[i];248249emit_marker(cinfo, M_DAC);250251emit_2bytes(cinfo, length*2 + 2);252253for (i = 0; i < NUM_ARITH_TBLS; i++) {254if (dc_in_use[i]) {255emit_byte(cinfo, i);256emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));257}258if (ac_in_use[i]) {259emit_byte(cinfo, i + 0x10);260emit_byte(cinfo, cinfo->arith_ac_K[i]);261}262}263#endif /* C_ARITH_CODING_SUPPORTED */264}265266267LOCAL(void)268emit_dri (j_compress_ptr cinfo)269/* Emit a DRI marker */270{271emit_marker(cinfo, M_DRI);272273emit_2bytes(cinfo, 4); /* fixed length */274275emit_2bytes(cinfo, (int) cinfo->restart_interval);276}277278279LOCAL(void)280emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)281/* Emit a SOF marker */282{283int ci;284jpeg_component_info *compptr;285286emit_marker(cinfo, code);287288emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */289290/* Make sure image isn't bigger than SOF field can handle */291if ((long) cinfo->image_height > 65535L ||292(long) cinfo->image_width > 65535L)293ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);294295emit_byte(cinfo, cinfo->data_precision);296emit_2bytes(cinfo, (int) cinfo->image_height);297emit_2bytes(cinfo, (int) cinfo->image_width);298299emit_byte(cinfo, cinfo->num_components);300301for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;302ci++, compptr++) {303emit_byte(cinfo, compptr->component_id);304emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);305emit_byte(cinfo, compptr->quant_tbl_no);306}307}308309310LOCAL(void)311emit_sos (j_compress_ptr cinfo)312/* Emit a SOS marker */313{314int i, td, ta;315jpeg_component_info *compptr;316317emit_marker(cinfo, M_SOS);318319emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */320321emit_byte(cinfo, cinfo->comps_in_scan);322323for (i = 0; i < cinfo->comps_in_scan; i++) {324compptr = cinfo->cur_comp_info[i];325emit_byte(cinfo, compptr->component_id);326td = compptr->dc_tbl_no;327ta = compptr->ac_tbl_no;328if (cinfo->progressive_mode) {329/* Progressive mode: only DC or only AC tables are used in one scan;330* furthermore, Huffman coding of DC refinement uses no table at all.331* We emit 0 for unused field(s); this is recommended by the P&M text332* but does not seem to be specified in the standard.333*/334if (cinfo->Ss == 0) {335ta = 0; /* DC scan */336if (cinfo->Ah != 0 && !cinfo->arith_code)337td = 0; /* no DC table either */338} else {339td = 0; /* AC scan */340}341}342emit_byte(cinfo, (td << 4) + ta);343}344345emit_byte(cinfo, cinfo->Ss);346emit_byte(cinfo, cinfo->Se);347emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);348}349350351LOCAL(void)352emit_jfif_app0 (j_compress_ptr cinfo)353/* Emit a JFIF-compliant APP0 marker */354{355/*356* Length of APP0 block (2 bytes)357* Block ID (4 bytes - ASCII "JFIF")358* Zero byte (1 byte to terminate the ID string)359* Version Major, Minor (2 bytes - major first)360* Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)361* Xdpu (2 bytes - dots per unit horizontal)362* Ydpu (2 bytes - dots per unit vertical)363* Thumbnail X size (1 byte)364* Thumbnail Y size (1 byte)365*/366367emit_marker(cinfo, M_APP0);368369emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */370371emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */372emit_byte(cinfo, 0x46);373emit_byte(cinfo, 0x49);374emit_byte(cinfo, 0x46);375emit_byte(cinfo, 0);376emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */377emit_byte(cinfo, cinfo->JFIF_minor_version);378emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */379emit_2bytes(cinfo, (int) cinfo->X_density);380emit_2bytes(cinfo, (int) cinfo->Y_density);381emit_byte(cinfo, 0); /* No thumbnail image */382emit_byte(cinfo, 0);383}384385386LOCAL(void)387emit_adobe_app14 (j_compress_ptr cinfo)388/* Emit an Adobe APP14 marker */389{390/*391* Length of APP14 block (2 bytes)392* Block ID (5 bytes - ASCII "Adobe")393* Version Number (2 bytes - currently 100)394* Flags0 (2 bytes - currently 0)395* Flags1 (2 bytes - currently 0)396* Color transform (1 byte)397*398* Although Adobe TN 5116 mentions Version = 101, all the Adobe files399* now in circulation seem to use Version = 100, so that's what we write.400*401* We write the color transform byte as 1 if the JPEG color space is402* YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with403* whether the encoder performed a transformation, which is pretty useless.404*/405406emit_marker(cinfo, M_APP14);407408emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */409410emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */411emit_byte(cinfo, 0x64);412emit_byte(cinfo, 0x6F);413emit_byte(cinfo, 0x62);414emit_byte(cinfo, 0x65);415emit_2bytes(cinfo, 100); /* Version */416emit_2bytes(cinfo, 0); /* Flags0 */417emit_2bytes(cinfo, 0); /* Flags1 */418switch (cinfo->jpeg_color_space) {419case JCS_YCbCr:420emit_byte(cinfo, 1); /* Color transform = 1 */421break;422case JCS_YCCK:423emit_byte(cinfo, 2); /* Color transform = 2 */424break;425default:426emit_byte(cinfo, 0); /* Color transform = 0 */427break;428}429}430431432/*433* These routines allow writing an arbitrary marker with parameters.434* The only intended use is to emit COM or APPn markers after calling435* write_file_header and before calling write_frame_header.436* Other uses are not guaranteed to produce desirable results.437* Counting the parameter bytes properly is the caller's responsibility.438*/439440METHODDEF(void)441write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)442/* Emit an arbitrary marker header */443{444if (datalen > (unsigned int) 65533) /* safety check */445ERREXIT(cinfo, JERR_BAD_LENGTH);446447emit_marker(cinfo, (JPEG_MARKER) marker);448449emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */450}451452METHODDEF(void)453write_marker_byte (j_compress_ptr cinfo, int val)454/* Emit one byte of marker parameters following write_marker_header */455{456emit_byte(cinfo, val);457}458459460/*461* Write datastream header.462* This consists of an SOI and optional APPn markers.463* We recommend use of the JFIF marker, but not the Adobe marker,464* when using YCbCr or grayscale data. The JFIF marker should NOT465* be used for any other JPEG colorspace. The Adobe marker is helpful466* to distinguish RGB, CMYK, and YCCK colorspaces.467* Note that an application can write additional header markers after468* jpeg_start_compress returns.469*/470471METHODDEF(void)472write_file_header (j_compress_ptr cinfo)473{474my_marker_ptr marker = (my_marker_ptr) cinfo->marker;475476emit_marker(cinfo, M_SOI); /* first the SOI */477478/* SOI is defined to reset restart interval to 0 */479marker->last_restart_interval = 0;480481if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */482emit_jfif_app0(cinfo);483if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */484emit_adobe_app14(cinfo);485}486487488/*489* Write frame header.490* This consists of DQT and SOFn markers.491* Note that we do not emit the SOF until we have emitted the DQT(s).492* This avoids compatibility problems with incorrect implementations that493* try to error-check the quant table numbers as soon as they see the SOF.494*/495496METHODDEF(void)497write_frame_header (j_compress_ptr cinfo)498{499int ci, prec;500boolean is_baseline;501jpeg_component_info *compptr;502503/* Emit DQT for each quantization table.504* Note that emit_dqt() suppresses any duplicate tables.505*/506prec = 0;507for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;508ci++, compptr++) {509prec += emit_dqt(cinfo, compptr->quant_tbl_no);510}511/* now prec is nonzero iff there are any 16-bit quant tables. */512513/* Check for a non-baseline specification.514* Note we assume that Huffman table numbers won't be changed later.515*/516if (cinfo->arith_code || cinfo->progressive_mode ||517cinfo->data_precision != 8) {518is_baseline = FALSE;519} else {520is_baseline = TRUE;521for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;522ci++, compptr++) {523if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)524is_baseline = FALSE;525}526if (prec && is_baseline) {527is_baseline = FALSE;528/* If it's baseline except for quantizer size, warn the user */529TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);530}531}532533/* Emit the proper SOF marker */534if (cinfo->arith_code) {535emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */536} else {537if (cinfo->progressive_mode)538emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */539else if (is_baseline)540emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */541else542emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */543}544}545546547/*548* Write scan header.549* This consists of DHT or DAC markers, optional DRI, and SOS.550* Compressed data will be written following the SOS.551*/552553METHODDEF(void)554write_scan_header (j_compress_ptr cinfo)555{556my_marker_ptr marker = (my_marker_ptr) cinfo->marker;557int i;558jpeg_component_info *compptr;559560if (cinfo->arith_code) {561/* Emit arith conditioning info. We may have some duplication562* if the file has multiple scans, but it's so small it's hardly563* worth worrying about.564*/565emit_dac(cinfo);566} else {567/* Emit Huffman tables.568* Note that emit_dht() suppresses any duplicate tables.569*/570for (i = 0; i < cinfo->comps_in_scan; i++) {571compptr = cinfo->cur_comp_info[i];572if (cinfo->progressive_mode) {573/* Progressive mode: only DC or only AC tables are used in one scan */574if (cinfo->Ss == 0) {575if (cinfo->Ah == 0) /* DC needs no table for refinement scan */576emit_dht(cinfo, compptr->dc_tbl_no, FALSE);577} else {578emit_dht(cinfo, compptr->ac_tbl_no, TRUE);579}580} else {581/* Sequential mode: need both DC and AC tables */582emit_dht(cinfo, compptr->dc_tbl_no, FALSE);583emit_dht(cinfo, compptr->ac_tbl_no, TRUE);584}585}586}587588/* Emit DRI if required --- note that DRI value could change for each scan.589* We avoid wasting space with unnecessary DRIs, however.590*/591if (cinfo->restart_interval != marker->last_restart_interval) {592emit_dri(cinfo);593marker->last_restart_interval = cinfo->restart_interval;594}595596emit_sos(cinfo);597}598599600/*601* Write datastream trailer.602*/603604METHODDEF(void)605write_file_trailer (j_compress_ptr cinfo)606{607emit_marker(cinfo, M_EOI);608}609610611/*612* Write an abbreviated table-specification datastream.613* This consists of SOI, DQT and DHT tables, and EOI.614* Any table that is defined and not marked sent_table = TRUE will be615* emitted. Note that all tables will be marked sent_table = TRUE at exit.616*/617618METHODDEF(void)619write_tables_only (j_compress_ptr cinfo)620{621int i;622623emit_marker(cinfo, M_SOI);624625/* Emit DQT for each quantization table.626* Only emit those tables that are actually associated with image components,627* if there are any image components, which will usually not be the case.628* Note that emit_dqt() suppresses any duplicate tables.629*/630if (cinfo->num_components > 0) {631int ci;632jpeg_component_info *compptr;633for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;634ci++, compptr++) {635(void) emit_dqt(cinfo, compptr->quant_tbl_no);636}637} else {638for (i = 0; i < NUM_QUANT_TBLS; i++) {639if (cinfo->quant_tbl_ptrs[i] != NULL)640(void) emit_dqt(cinfo, i);641}642}643644if (! cinfo->arith_code) {645for (i = 0; i < NUM_HUFF_TBLS; i++) {646if (cinfo->dc_huff_tbl_ptrs[i] != NULL)647emit_dht(cinfo, i, FALSE);648if (cinfo->ac_huff_tbl_ptrs[i] != NULL)649emit_dht(cinfo, i, TRUE);650}651}652653emit_marker(cinfo, M_EOI);654}655656657/*658* Initialize the marker writer module.659*/660661GLOBAL(void)662jinit_marker_writer (j_compress_ptr cinfo)663{664my_marker_ptr marker;665666/* Create the subobject */667marker = (my_marker_ptr)668(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,669SIZEOF(my_marker_writer));670cinfo->marker = (struct jpeg_marker_writer *) marker;671/* Initialize method pointers */672marker->pub.write_file_header = write_file_header;673marker->pub.write_frame_header = write_frame_header;674marker->pub.write_scan_header = write_scan_header;675marker->pub.write_file_trailer = write_file_trailer;676marker->pub.write_tables_only = write_tables_only;677marker->pub.write_marker_header = write_marker_header;678marker->pub.write_marker_byte = write_marker_byte;679/* Initialize private state */680marker->last_restart_interval = 0;681}682683684