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