📄 jpgmisc.c
字号:
for (i = 0; i < mt; i++){
MAKE_INT8_1B(buf, chrom_ac_huffvalues[i]);
}
/* Write SOF infomation */
MAKE_MARKER_2B(buf, JPEG_MARKER_SOF0);
/* Write the length len = 8 + num_component * 3 = 17 */
MAKE_INT16_2B(buf, 17);
MAKE_INT8_1B(buf, 8);
MAKE_INT16_2B(buf, enc_state->height);
MAKE_INT16_2B(buf, enc_state->width);
MAKE_INT8_1B(buf, 3);
tab_index[0] = 0;
tab_index[1] = tab_index[2] = 1;
v_sample[0] = h_sample[0] = 2;
v_sample[1] = h_sample[1] = 1;
v_sample[2] = h_sample[2] = 1;
for (i=0; i<3; i++){
MAKE_INT8_1B(buf, (i+1));
/* Write the V sampling and H sampling */
MAKE_INT8_1B(buf, (h_sample[i]<<4) + (v_sample[i] & 0xff));
/* Write the quant table index */
MAKE_INT8_1B(buf, tab_index[i]);
}
dst_bitsream->bs_cur_byte = (unsigned char *)buf;
}
/******************************************************************************
// Name: write_sos_information
// Description:
// This function write the SOS information of scan. Includes:
// marks, component indicators, and table indicators.
// Output Arguments:
// dst_bitsream - Pointer to the output bitstream structure
// Returns:
// None
******************************************************************************/
void write_sos_information(sample_bitstream *dst_bitstream)
{
int num_component = 3; /* The interleave format has 3 components */
int len;
/* Input the SOS marker */
MAKE_MARKER_2B(dst_bitstream->bs_cur_byte, JPEG_MARKER_SOS);
/* Input the length information */
len = 6 + 2 * num_component;
MAKE_INT16_2B(dst_bitstream->bs_cur_byte, len);
/* Input the number of components */
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, num_component);
/* Input the tablle indicators */
/* Table indicator for the Y blocks */
/* Component indicator */
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 1);
/* DC huff table | AC huff table */
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 0);
/* Table indicator for the Cb blocks */
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 2);
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 0x11);
/* Table indicator for the Cb blocks */
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 3);
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 0x11);
/* Input the spectral start and end number */
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 0);
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 63);
MAKE_INT8_1B(dst_bitstream->bs_cur_byte, 0);
}
/******************************************************************************
// Name: read_jpeg_header
// Description:
// This function read and parse the JPEG file header information
// Input Arguments:
// stream - Pointer to the current stream pointer.
// Output Arguments:
// dec_state - Pointer to the JPEG decoder state. The state elements
// will be set unpon the bitstream header information
// Returns:
// SAMPLE_STATUS_BITSTREAM_ERR - Bitstream error found
// SAMPLE_STATUS_NOTSUPPORTED_ERR - Format not supported
// SAMPLE_STATUS_NOERR - No error found
******************************************************************************/
sample_status read_jpeg_header(char **stream, jpeg_dec_state *dec_state)
{
char *src;
unsigned char data1, data2;
unsigned int length;
char buf[5];
const char format[5] = {'J', 'F', 'I', 'F', 0};
int i;
int num_component;
Ipp8u *quant_table;
src = *stream;
/* Check the SOI marker */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_SOI) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
/* Check the APP0 header */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_APP0) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
for(i = 0; i < 5; i ++) {
buf[i] = *src ++;
if(buf[i] != format[i]) {
return SAMPLE_STATUS_NOTSUPPORTED_ERR;
}
}
/* Skip left part ot the APP0 header */
src += length - 7;
/* Load the quantization table for luminance */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_DQT) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
/* Skip the table indicator */
src ++;
quant_table = (Ipp8u *)SAMPLE_ALIGN8(dec_state->tmp_lum_quant_table);
for (i=0; i< (int)length- 3; i++){
quant_table[zig_zag_tab_index[i]] = *src ++;
}
/* Load the quantization table for chrominance */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_DQT) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
/* Skip the table indicator */
src ++;
quant_table = (Ipp8u *)SAMPLE_ALIGN8(dec_state->tmp_chrom_quant_table);
for (i = 0; i < (int)length - 3; i++){
quant_table[zig_zag_tab_index[i]] = *src ++;
}
/* Load the huffman table */
/* Load the DC huffman table for luminance */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_DHT) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
src ++; /* Skip the table index */
for(i = 0; i < 16; i ++) {
dec_state->lum_dc_huffbits[i] = *src ++;
}
for(i = 0; i < (int)length - 19; i ++) {
dec_state->lum_dc_huffvalues[i] = *src ++;
}
/* Load the AC huffman table for luminance */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_DHT) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
src ++; /* Skip the table index */
for(i = 0; i < 16; i ++) {
dec_state->lum_ac_huffbits[i] = *src ++;
}
for(i = 0; i < (int)length - 19; i ++) {
dec_state->lum_ac_huffvalues[i] = *src ++;
}
/* Load the DC huffman table for chrominance */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_DHT) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
src ++; /* Skip the table index */
for(i = 0; i < 16; i ++) {
dec_state->chrom_dc_huffbits[i] = *src ++;
}
for(i = 0; i < (int)length - 19; i ++) {
dec_state->chrom_dc_huffvalues[i] = *src ++;
}
/* Load the AC huffman table for chrominance */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_DHT) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
src ++; /* Skip the table index */
for(i = 0; i < 16; i ++) {
dec_state->chrom_ac_huffbits[i] = *src ++;
}
for(i = 0; i < (int)length - 19; i ++) {
dec_state->chrom_ac_huffvalues[i] = *src ++;
}
/* Get the SOF information */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_SOF0) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
src ++; /* Skip next byte */
/* Get the width and height of the picture */
data1 = *src ++;
data2 = *src ++;
dec_state->height = ((unsigned int)data1 << 8) + (unsigned int)data2;
data1 = *src ++;
data2 = *src ++;
dec_state->width = ((unsigned int)data1 << 8) + (unsigned int)data2;
/* The output format is BGR888 so step = width * 3 */
dec_state->step = dec_state->width * 3;
num_component = (int)(*src ++);
/* Skip the next information, because the format of the input are limited */
src += num_component * 3;
*stream = src;
return SAMPLE_STATUS_NOERR;
}
/******************************************************************************
// Name: read_sos
// Description:
// This function read and parse the JPEG SOS marker segment information
// Input Arguments:
// stream - Pointer to the current stream pointer.
// Returns:
// SAMPLE_STATUS_BITSTREAM_ERR - Bitstream error found
// SAMPLE_STATUS_NOERR - No error found
******************************************************************************/
sample_status read_sos(char **stream)
{
unsigned char data1, data2;
char *src;
int length;
src = *stream;
/* Read the SOS marker */
data1 = *src ++;
data2 = *src ++;
if(data1 != 0xff && data2 != JPEG_MARKER_SOS) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
/* Read the length of the rest SOS part */
data1 = *src ++;
data2 = *src ++;
length = ((unsigned int)data1 << 8) + (unsigned int)data2;
/* Skip the rest of the information since the input are restricted */
src += length - 2;
*stream = src;
return SAMPLE_STATUS_NOERR;
}
/* EOF */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -