📄 jpgmisc.c
字号:
/******************************************************************************
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright (c) 2003 Intel Corporation. All Rights Reserved.
//
// Description:
// Intel(R) Integrated Performance Primitives Sample Code JPEG Mixed Code
//
******************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include "sampjpeg.h"
/******************************************************************************
// Name: read_bmp_file
// Description:
// This function parses the BMP file header
// Input Arguments:
// src - File Pointer to the source BMP file
// Output Arguments:
// enc_state - Pointer to the encoder state structure, its content
// will be initialized in this function
// color_mode - Color mode used of this file
// data_size - Raw data size of the BMP file
// Returns:
// SAMPLE_STATUS_NOERR - No error
// SAMPLE_STATUS_NOTSUPPORTED_ERR - Not supported input
// SAMPLE_STATUS_ERR - Other errors in file
******************************************************************************/
sample_status read_bmp_file(FILE *src, jpeg_enc_state *enc_state, int *color_mode,
int *data_size)
{
unsigned char data_buf[16];
int buf_size;
int offset;
int data32;
unsigned short data16;
int tmp;
int compression;
int mask[3];
/* Check if the format is correct */
if ( fread(data_buf, 1, 2, src)!=2) {
return SAMPLE_STATUS_BITSTREAM_ERR;
}
if ((data_buf[0]!='B')||(data_buf[1]!='M')) {
return SAMPLE_STATUS_NOTSUPPORTED_ERR;
}
/* Get the file size */
if(fread(&buf_size, 4, 1, src) != 1) {
return SAMPLE_STATUS_ERR;
}
/* Ingore the reserved bytes */
fseek(src, 4, SEEK_CUR);
/* Read the BMP data offset */
if(fread(&offset, 4, 1, src) != 1) {
return SAMPLE_STATUS_ERR;
}
/* Now begin the bitmap header information */
/* Read the size information */
if ( (fread(&data32, 4, 1, src)!=1)||(0==data32) ) {
return SAMPLE_STATUS_ERR;
}
/* Read the picture width information */
if ( (fread(&data32, 4, 1, src)!=1)||(0==data32) ) {
return SAMPLE_STATUS_ERR;
}
enc_state->width = data32;
/* Read the picture height information */
if ( (fread(&data32, 4, 1, src)!=1)||(0==data32) ) {
return SAMPLE_STATUS_ERR;
}
enc_state->height = data32;
/* Read the plane number */
if ( fread(&data16, 2, 1, src)!=1) {
return SAMPLE_STATUS_ERR;
}
/* Read the pixel format */
if ( (fread(&data16, 2, 1, src)!=1) ) {
return SAMPLE_STATUS_ERR;
}
enc_state->bits_per_pixel=data16;
tmp = enc_state->width * enc_state->bits_per_pixel;
enc_state->step = (((tmp)+31)&(~31))>>3;
*data_size = enc_state->step * enc_state->height;
/* Read the RGB format */
if ((fread(&compression, 4, 1, src)!=1) ) {
return SAMPLE_STATUS_ERR;
}
if ((JPEG_BI_RGB != compression) && (JPEG_BI_BITFIELDS != compression)) {
return SAMPLE_STATUS_NOTSUPPORTED_ERR;
}
/* The following information are all ignored */
/* Image size */
if (fread(&data32, 4, 1, src)!=1) {
return SAMPLE_STATUS_ERR;
}
/* X Pels Per Meter */
if ( fread(&data32, 4, 1, src)!=1 ) {
return SAMPLE_STATUS_ERR;
}
/* Y Pels Per Meter */
if ( fread(&data32, 4, 1, src)!=1 ) {
return SAMPLE_STATUS_ERR;
}
/* Color used */
if ( fread(&data32, 4, 1, src)!=1 ) {
return SAMPLE_STATUS_ERR;
}
/* Color importance */
if ( fread(&data32, 4, 1, src)!=1 ) {
return SAMPLE_STATUS_ERR;
}
/* Translate the color format */
switch (enc_state->bits_per_pixel) {
case 24: /* 24-bit color, B:G:R = 8:8:8 */
*color_mode = JPEG_BGR888;
break;
case 16:
if ( JPEG_BI_BITFIELDS == compression ) {
if ( fread(&mask, 4, 3, src)!=3 ) {
return SAMPLE_STATUS_ERR;
}
if ((0x001F == mask[0]) || (0x03E0 == mask[1]) ||
(0x7C00 == mask[2]) ) {
*color_mode = JPEG_BGR555;
} else if ((0x001F == mask[0]) || ( 0x07E0 == mask[1]) ||
(0xF800==mask[2])) {
*color_mode = JPEG_BGR565;
} else {
return SAMPLE_STATUS_NOTSUPPORTED_ERR;
}
}
break;
default:
return SAMPLE_STATUS_NOTSUPPORTED_ERR;
};
/* Seek the file pointer to the raw data beginning */
fseek(src, offset, SEEK_SET);
return SAMPLE_STATUS_NOERR;
}
/******************************************************************************
// Name: write_head_information
// Description:
// This function write the head information of a JPEG file. Includes:
// marks, picture properties, quantization tables, huffman tables.
// Input Arguments:
// enc_state - Pointer to the encoder state structure, its content
// will be used to get the information.
// Output Arguments:
// dst_bitsream - Output bitstream structure
// Returns:
// None
******************************************************************************/
void write_head_information(sample_bitstream *dst_bitsream,
jpeg_enc_state *enc_state)
{
char *buf;
int len;
Ipp8u *quant_table_lum, *quant_table_chrom;
int i;
int mt;
int tab_index[3];
int v_sample[3], h_sample[3];
buf = (char *)dst_bitsream->bs_cur_byte;
/* Write the SOI marker */
MAKE_MARKER_2B(buf, JPEG_MARKER_SOI);
/* Write APP0 general header information */
len = 2 + 5 + 2 + 1 + 2 + 2 + 1 + 1;
MAKE_MARKER_2B(buf, JPEG_MARKER_APP0);
MAKE_INT16_2B(buf, len);
/* Write format indicator "JFIF" \0 */
MAKE_INT8_1B(buf, 'J');
MAKE_INT8_1B(buf, 'F');
MAKE_INT8_1B(buf, 'I');
MAKE_INT8_1B(buf, 'F');
MAKE_INT8_1B(buf, 0);
/* Write version number */
MAKE_INT16_2B(buf, 0x0102);
/* Write the density units */
MAKE_INT8_1B(buf, 0);
/* Write the aspect ratio */
MAKE_INT16_2B(buf, 0x48);
MAKE_INT16_2B(buf, 0x48);
/* Write the Thumbnail X and Y size */
MAKE_INT8_1B(buf, 0);
MAKE_INT8_1B(buf, 0);
/* Load the quant table */
if(1 == enc_state->quality) {
quant_table_lum = h_lum_quant_table;
quant_table_chrom = h_chrom_quant_table;
} else {
quant_table_lum = l_lum_quant_table;
quant_table_chrom = l_chrom_quant_table;
}
/* Write the quant table for luminance */
MAKE_MARKER_2B(buf, JPEG_MARKER_DQT);
len = 67;
MAKE_INT16_2B(buf, len);
MAKE_INT8_1B(buf, 0);
for (i=0; i<64; i++){
*buf ++ = (Ipp8u)quant_table_lum[zig_zag_tab_index[i]];
}
/* Write the quant table for chrominance */
MAKE_MARKER_2B(buf, JPEG_MARKER_DQT);
len = 67;
MAKE_INT16_2B(buf, len);
MAKE_INT8_1B(buf, 1);
for (i=0; i<64; i++){
*buf ++ = (Ipp8u)quant_table_chrom[zig_zag_tab_index[i]];
}
/* Write the huffman table */
/* Write the DC lumninance huffman table */
MAKE_MARKER_2B(buf, JPEG_MARKER_DHT);
mt = 0;
for (i = 0; i < 16; i++){
mt += lum_dc_huffbits[i];
}
len = 2 + 1 + 16 + mt;
MAKE_INT16_2B(buf, len);
MAKE_INT8_1B(buf, 0);
for (i = 0; i < 16; i++){
MAKE_INT8_1B(buf, lum_dc_huffbits[i]);
}
for (i = 0; i < mt; i++){
MAKE_INT8_1B(buf, lum_dc_huffvalues[i]);
}
/* Write AC luminance huffman table */
MAKE_MARKER_2B(buf, JPEG_MARKER_DHT);
mt = 0;
for (i = 0; i < 16; i++){
mt += lum_ac_huffbits[i];
}
len = 2 + 1 + 16 + mt;
MAKE_INT16_2B(buf, len);
MAKE_INT8_1B(buf, 0x10);
for (i = 0; i < 16; i++){
MAKE_INT8_1B(buf, lum_ac_huffbits[i]);
}
for (i = 0; i < mt; i++){
MAKE_INT8_1B(buf, lum_ac_huffvalues[i]);
}
/* Write DC Chrominance huffman table */
MAKE_MARKER_2B(buf, JPEG_MARKER_DHT);
mt = 0;
for (i = 0; i < 16; i++){
mt += chrom_dc_huffbits[i];
}
len = 2 + 1 + 16 + mt;
MAKE_INT16_2B(buf, len);
MAKE_INT8_1B(buf, 0x01);
for (i = 0; i < 16; i++){
MAKE_INT8_1B(buf, chrom_dc_huffbits[i]);
}
for (i = 0; i < mt; i++){
MAKE_INT8_1B(buf, chrom_dc_huffvalues[i]);
}
/* Write AC Chrominance huffman table */
MAKE_MARKER_2B(buf, JPEG_MARKER_DHT);
mt = 0;
for (i = 0; i < 16; i++){
mt += chrom_ac_huffbits[i];
}
len = 2 + 1 + 16 + mt;
MAKE_INT16_2B(buf, len);
MAKE_INT8_1B(buf, 0x11);
for (i = 0; i < 16; i++){
MAKE_INT8_1B(buf, chrom_ac_huffbits[i]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -