Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/vncdll/winvnc/libjpeg/jcmarker.c
Views: 11784
/*1* jcmarker.c2*3* Copyright (C) 1991-1998, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This file contains routines to write JPEG datastream markers.8*/910#define JPEG_INTERNALS11#include "jinclude.h"12#include "jpeglib.h"131415typedef enum { /* JPEG marker codes */16M_SOF0 = 0xc0,17M_SOF1 = 0xc1,18M_SOF2 = 0xc2,19M_SOF3 = 0xc3,2021M_SOF5 = 0xc5,22M_SOF6 = 0xc6,23M_SOF7 = 0xc7,2425M_JPG = 0xc8,26M_SOF9 = 0xc9,27M_SOF10 = 0xca,28M_SOF11 = 0xcb,2930M_SOF13 = 0xcd,31M_SOF14 = 0xce,32M_SOF15 = 0xcf,3334M_DHT = 0xc4,3536M_DAC = 0xcc,3738M_RST0 = 0xd0,39M_RST1 = 0xd1,40M_RST2 = 0xd2,41M_RST3 = 0xd3,42M_RST4 = 0xd4,43M_RST5 = 0xd5,44M_RST6 = 0xd6,45M_RST7 = 0xd7,4647M_SOI = 0xd8,48M_EOI = 0xd9,49M_SOS = 0xda,50M_DQT = 0xdb,51M_DNL = 0xdc,52M_DRI = 0xdd,53M_DHP = 0xde,54M_EXP = 0xdf,5556M_APP0 = 0xe0,57M_APP1 = 0xe1,58M_APP2 = 0xe2,59M_APP3 = 0xe3,60M_APP4 = 0xe4,61M_APP5 = 0xe5,62M_APP6 = 0xe6,63M_APP7 = 0xe7,64M_APP8 = 0xe8,65M_APP9 = 0xe9,66M_APP10 = 0xea,67M_APP11 = 0xeb,68M_APP12 = 0xec,69M_APP13 = 0xed,70M_APP14 = 0xee,71M_APP15 = 0xef,7273M_JPG0 = 0xf0,74M_JPG13 = 0xfd,75M_COM = 0xfe,7677M_TEM = 0x01,7879M_ERROR = 0x10080} JPEG_MARKER;818283/* Private state */8485typedef struct {86struct jpeg_marker_writer pub; /* public fields */8788unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */89} my_marker_writer;9091typedef my_marker_writer * my_marker_ptr;929394/*95* Basic output routines.96*97* Note that we do not support suspension while writing a marker.98* Therefore, an application using suspension must ensure that there is99* enough buffer space for the initial markers (typ. 600-700 bytes) before100* calling jpeg_start_compress, and enough space to write the trailing EOI101* (a few bytes) before calling jpeg_finish_compress. Multipass compression102* modes are not supported at all with suspension, so those two are the only103* points where markers will be written.104*/105106LOCAL(void)107emit_byte (j_compress_ptr cinfo, int val)108/* Emit a byte */109{110struct jpeg_destination_mgr * dest = cinfo->dest;111112*(dest->next_output_byte)++ = (JOCTET) val;113if (--dest->free_in_buffer == 0) {114if (! (*dest->empty_output_buffer) (cinfo))115ERREXIT(cinfo, JERR_CANT_SUSPEND);116}117}118119120LOCAL(void)121emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)122/* Emit a marker code */123{124emit_byte(cinfo, 0xFF);125emit_byte(cinfo, (int) mark);126}127128129LOCAL(void)130emit_2bytes (j_compress_ptr cinfo, int value)131/* Emit a 2-byte integer; these are always MSB first in JPEG files */132{133emit_byte(cinfo, (value >> 8) & 0xFF);134emit_byte(cinfo, value & 0xFF);135}136137138/*139* Routines to write specific marker types.140*/141142LOCAL(int)143emit_dqt (j_compress_ptr cinfo, int index)144/* Emit a DQT marker */145/* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */146{147JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];148int prec;149int i;150151if (qtbl == NULL)152ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);153154prec = 0;155for (i = 0; i < DCTSIZE2; i++) {156if (qtbl->quantval[i] > 255)157prec = 1;158}159160if (! qtbl->sent_table) {161emit_marker(cinfo, M_DQT);162163emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);164165emit_byte(cinfo, index + (prec<<4));166167for (i = 0; i < DCTSIZE2; i++) {168/* The table entries must be emitted in zigzag order. */169unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];170if (prec)171emit_byte(cinfo, (int) (qval >> 8));172emit_byte(cinfo, (int) (qval & 0xFF));173}174175qtbl->sent_table = TRUE;176}177178return prec;179}180181182LOCAL(void)183emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)184/* Emit a DHT marker */185{186JHUFF_TBL * htbl;187int length, i;188189if (is_ac) {190htbl = cinfo->ac_huff_tbl_ptrs[index];191index += 0x10; /* output index has AC bit set */192} else {193htbl = cinfo->dc_huff_tbl_ptrs[index];194}195196if (htbl == NULL)197ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);198199if (! htbl->sent_table) {200emit_marker(cinfo, M_DHT);201202length = 0;203for (i = 1; i <= 16; i++)204length += htbl->bits[i];205206emit_2bytes(cinfo, length + 2 + 1 + 16);207emit_byte(cinfo, index);208209for (i = 1; i <= 16; i++)210emit_byte(cinfo, htbl->bits[i]);211212for (i = 0; i < length; i++)213emit_byte(cinfo, htbl->huffval[i]);214215htbl->sent_table = TRUE;216}217}218219220LOCAL(void)221emit_dac (j_compress_ptr cinfo)222/* Emit a DAC marker */223/* Since the useful info is so small, we want to emit all the tables in */224/* one DAC marker. Therefore this routine does its own scan of the table. */225{226#ifdef C_ARITH_CODING_SUPPORTED227char dc_in_use[NUM_ARITH_TBLS];228char ac_in_use[NUM_ARITH_TBLS];229int length, i;230jpeg_component_info *compptr;231232for (i = 0; i < NUM_ARITH_TBLS; i++)233dc_in_use[i] = ac_in_use[i] = 0;234235for (i = 0; i < cinfo->comps_in_scan; i++) {236compptr = cinfo->cur_comp_info[i];237dc_in_use[compptr->dc_tbl_no] = 1;238ac_in_use[compptr->ac_tbl_no] = 1;239}240241length = 0;242for (i = 0; i < NUM_ARITH_TBLS; i++)243length += dc_in_use[i] + ac_in_use[i];244245emit_marker(cinfo, M_DAC);246247emit_2bytes(cinfo, length*2 + 2);248249for (i = 0; i < NUM_ARITH_TBLS; i++) {250if (dc_in_use[i]) {251emit_byte(cinfo, i);252emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));253}254if (ac_in_use[i]) {255emit_byte(cinfo, i + 0x10);256emit_byte(cinfo, cinfo->arith_ac_K[i]);257}258}259#endif /* C_ARITH_CODING_SUPPORTED */260}261262263LOCAL(void)264emit_dri (j_compress_ptr cinfo)265/* Emit a DRI marker */266{267emit_marker(cinfo, M_DRI);268269emit_2bytes(cinfo, 4); /* fixed length */270271emit_2bytes(cinfo, (int) cinfo->restart_interval);272}273274275LOCAL(void)276emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)277/* Emit a SOF marker */278{279int ci;280jpeg_component_info *compptr;281282emit_marker(cinfo, code);283284emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */285286/* Make sure image isn't bigger than SOF field can handle */287if ((long) cinfo->image_height > 65535L ||288(long) cinfo->image_width > 65535L)289ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);290291emit_byte(cinfo, cinfo->data_precision);292emit_2bytes(cinfo, (int) cinfo->image_height);293emit_2bytes(cinfo, (int) cinfo->image_width);294295emit_byte(cinfo, cinfo->num_components);296297for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;298ci++, compptr++) {299emit_byte(cinfo, compptr->component_id);300emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);301emit_byte(cinfo, compptr->quant_tbl_no);302}303}304305306LOCAL(void)307emit_sos (j_compress_ptr cinfo)308/* Emit a SOS marker */309{310int i, td, ta;311jpeg_component_info *compptr;312313emit_marker(cinfo, M_SOS);314315emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */316317emit_byte(cinfo, cinfo->comps_in_scan);318319for (i = 0; i < cinfo->comps_in_scan; i++) {320compptr = cinfo->cur_comp_info[i];321emit_byte(cinfo, compptr->component_id);322td = compptr->dc_tbl_no;323ta = compptr->ac_tbl_no;324if (cinfo->progressive_mode) {325/* Progressive mode: only DC or only AC tables are used in one scan;326* furthermore, Huffman coding of DC refinement uses no table at all.327* We emit 0 for unused field(s); this is recommended by the P&M text328* but does not seem to be specified in the standard.329*/330if (cinfo->Ss == 0) {331ta = 0; /* DC scan */332if (cinfo->Ah != 0 && !cinfo->arith_code)333td = 0; /* no DC table either */334} else {335td = 0; /* AC scan */336}337}338emit_byte(cinfo, (td << 4) + ta);339}340341emit_byte(cinfo, cinfo->Ss);342emit_byte(cinfo, cinfo->Se);343emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);344}345346347LOCAL(void)348emit_jfif_app0 (j_compress_ptr cinfo)349/* Emit a JFIF-compliant APP0 marker */350{351/*352* Length of APP0 block (2 bytes)353* Block ID (4 bytes - ASCII "JFIF")354* Zero byte (1 byte to terminate the ID string)355* Version Major, Minor (2 bytes - major first)356* Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)357* Xdpu (2 bytes - dots per unit horizontal)358* Ydpu (2 bytes - dots per unit vertical)359* Thumbnail X size (1 byte)360* Thumbnail Y size (1 byte)361*/362363emit_marker(cinfo, M_APP0);364365emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */366367emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */368emit_byte(cinfo, 0x46);369emit_byte(cinfo, 0x49);370emit_byte(cinfo, 0x46);371emit_byte(cinfo, 0);372emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */373emit_byte(cinfo, cinfo->JFIF_minor_version);374emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */375emit_2bytes(cinfo, (int) cinfo->X_density);376emit_2bytes(cinfo, (int) cinfo->Y_density);377emit_byte(cinfo, 0); /* No thumbnail image */378emit_byte(cinfo, 0);379}380381382LOCAL(void)383emit_adobe_app14 (j_compress_ptr cinfo)384/* Emit an Adobe APP14 marker */385{386/*387* Length of APP14 block (2 bytes)388* Block ID (5 bytes - ASCII "Adobe")389* Version Number (2 bytes - currently 100)390* Flags0 (2 bytes - currently 0)391* Flags1 (2 bytes - currently 0)392* Color transform (1 byte)393*394* Although Adobe TN 5116 mentions Version = 101, all the Adobe files395* now in circulation seem to use Version = 100, so that's what we write.396*397* We write the color transform byte as 1 if the JPEG color space is398* YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with399* whether the encoder performed a transformation, which is pretty useless.400*/401402emit_marker(cinfo, M_APP14);403404emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */405406emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */407emit_byte(cinfo, 0x64);408emit_byte(cinfo, 0x6F);409emit_byte(cinfo, 0x62);410emit_byte(cinfo, 0x65);411emit_2bytes(cinfo, 100); /* Version */412emit_2bytes(cinfo, 0); /* Flags0 */413emit_2bytes(cinfo, 0); /* Flags1 */414switch (cinfo->jpeg_color_space) {415case JCS_YCbCr:416emit_byte(cinfo, 1); /* Color transform = 1 */417break;418case JCS_YCCK:419emit_byte(cinfo, 2); /* Color transform = 2 */420break;421default:422emit_byte(cinfo, 0); /* Color transform = 0 */423break;424}425}426427428/*429* These routines allow writing an arbitrary marker with parameters.430* The only intended use is to emit COM or APPn markers after calling431* write_file_header and before calling write_frame_header.432* Other uses are not guaranteed to produce desirable results.433* Counting the parameter bytes properly is the caller's responsibility.434*/435436METHODDEF(void)437write_marker_header (j_compress_ptr cinfo, int marker, unsigned int datalen)438/* Emit an arbitrary marker header */439{440if (datalen > (unsigned int) 65533) /* safety check */441ERREXIT(cinfo, JERR_BAD_LENGTH);442443emit_marker(cinfo, (JPEG_MARKER) marker);444445emit_2bytes(cinfo, (int) (datalen + 2)); /* total length */446}447448METHODDEF(void)449write_marker_byte (j_compress_ptr cinfo, int val)450/* Emit one byte of marker parameters following write_marker_header */451{452emit_byte(cinfo, val);453}454455456/*457* Write datastream header.458* This consists of an SOI and optional APPn markers.459* We recommend use of the JFIF marker, but not the Adobe marker,460* when using YCbCr or grayscale data. The JFIF marker should NOT461* be used for any other JPEG colorspace. The Adobe marker is helpful462* to distinguish RGB, CMYK, and YCCK colorspaces.463* Note that an application can write additional header markers after464* jpeg_start_compress returns.465*/466467METHODDEF(void)468write_file_header (j_compress_ptr cinfo)469{470my_marker_ptr marker = (my_marker_ptr) cinfo->marker;471472emit_marker(cinfo, M_SOI); /* first the SOI */473474/* SOI is defined to reset restart interval to 0 */475marker->last_restart_interval = 0;476477if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */478emit_jfif_app0(cinfo);479if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */480emit_adobe_app14(cinfo);481}482483484/*485* Write frame header.486* This consists of DQT and SOFn markers.487* Note that we do not emit the SOF until we have emitted the DQT(s).488* This avoids compatibility problems with incorrect implementations that489* try to error-check the quant table numbers as soon as they see the SOF.490*/491492METHODDEF(void)493write_frame_header (j_compress_ptr cinfo)494{495int ci, prec;496boolean is_baseline;497jpeg_component_info *compptr;498499/* Emit DQT for each quantization table.500* Note that emit_dqt() suppresses any duplicate tables.501*/502prec = 0;503for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;504ci++, compptr++) {505prec += emit_dqt(cinfo, compptr->quant_tbl_no);506}507/* now prec is nonzero iff there are any 16-bit quant tables. */508509/* Check for a non-baseline specification.510* Note we assume that Huffman table numbers won't be changed later.511*/512if (cinfo->arith_code || cinfo->progressive_mode ||513cinfo->data_precision != 8) {514is_baseline = FALSE;515} else {516is_baseline = TRUE;517for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;518ci++, compptr++) {519if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)520is_baseline = FALSE;521}522if (prec && is_baseline) {523is_baseline = FALSE;524/* If it's baseline except for quantizer size, warn the user */525TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);526}527}528529/* Emit the proper SOF marker */530if (cinfo->arith_code) {531emit_sof(cinfo, M_SOF9); /* SOF code for arithmetic coding */532} else {533if (cinfo->progressive_mode)534emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */535else if (is_baseline)536emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */537else538emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */539}540}541542543/*544* Write scan header.545* This consists of DHT or DAC markers, optional DRI, and SOS.546* Compressed data will be written following the SOS.547*/548549METHODDEF(void)550write_scan_header (j_compress_ptr cinfo)551{552my_marker_ptr marker = (my_marker_ptr) cinfo->marker;553int i;554jpeg_component_info *compptr;555556if (cinfo->arith_code) {557/* Emit arith conditioning info. We may have some duplication558* if the file has multiple scans, but it's so small it's hardly559* worth worrying about.560*/561emit_dac(cinfo);562} else {563/* Emit Huffman tables.564* Note that emit_dht() suppresses any duplicate tables.565*/566for (i = 0; i < cinfo->comps_in_scan; i++) {567compptr = cinfo->cur_comp_info[i];568if (cinfo->progressive_mode) {569/* Progressive mode: only DC or only AC tables are used in one scan */570if (cinfo->Ss == 0) {571if (cinfo->Ah == 0) /* DC needs no table for refinement scan */572emit_dht(cinfo, compptr->dc_tbl_no, FALSE);573} else {574emit_dht(cinfo, compptr->ac_tbl_no, TRUE);575}576} else {577/* Sequential mode: need both DC and AC tables */578emit_dht(cinfo, compptr->dc_tbl_no, FALSE);579emit_dht(cinfo, compptr->ac_tbl_no, TRUE);580}581}582}583584/* Emit DRI if required --- note that DRI value could change for each scan.585* We avoid wasting space with unnecessary DRIs, however.586*/587if (cinfo->restart_interval != marker->last_restart_interval) {588emit_dri(cinfo);589marker->last_restart_interval = cinfo->restart_interval;590}591592emit_sos(cinfo);593}594595596/*597* Write datastream trailer.598*/599600METHODDEF(void)601write_file_trailer (j_compress_ptr cinfo)602{603emit_marker(cinfo, M_EOI);604}605606607/*608* Write an abbreviated table-specification datastream.609* This consists of SOI, DQT and DHT tables, and EOI.610* Any table that is defined and not marked sent_table = TRUE will be611* emitted. Note that all tables will be marked sent_table = TRUE at exit.612*/613614METHODDEF(void)615write_tables_only (j_compress_ptr cinfo)616{617int i;618619emit_marker(cinfo, M_SOI);620621for (i = 0; i < NUM_QUANT_TBLS; i++) {622if (cinfo->quant_tbl_ptrs[i] != NULL)623(void) emit_dqt(cinfo, i);624}625626if (! cinfo->arith_code) {627for (i = 0; i < NUM_HUFF_TBLS; i++) {628if (cinfo->dc_huff_tbl_ptrs[i] != NULL)629emit_dht(cinfo, i, FALSE);630if (cinfo->ac_huff_tbl_ptrs[i] != NULL)631emit_dht(cinfo, i, TRUE);632}633}634635emit_marker(cinfo, M_EOI);636}637638639/*640* Initialize the marker writer module.641*/642643GLOBAL(void)644jinit_marker_writer (j_compress_ptr cinfo)645{646my_marker_ptr marker;647648/* Create the subobject */649marker = (my_marker_ptr)650(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,651SIZEOF(my_marker_writer));652cinfo->marker = (struct jpeg_marker_writer *) marker;653/* Initialize method pointers */654marker->pub.write_file_header = write_file_header;655marker->pub.write_frame_header = write_frame_header;656marker->pub.write_scan_header = write_scan_header;657marker->pub.write_file_trailer = write_file_trailer;658marker->pub.write_tables_only = write_tables_only;659marker->pub.write_marker_header = write_marker_header;660marker->pub.write_marker_byte = write_marker_byte;661/* Initialize private state */662marker->last_restart_interval = 0;663}664665666