winjpg.c
来自「MinGUI 可视化程序代码」· C语言 代码 · 共 300 行
C
300 行
/******************************************************************************** Copyright 2006 National ASIC Center, All right Reserved** FILE NAME: winjpg.c* PROGRAMMER: ming.c* Date of Creation: 2006/08/8** DESCRIPTION:** NOTE:** FUNCTIONS LIST:* -----------------------------------------------------------------------------** -----------------------------------------------------------------------------** MODIFICATION HISTORY* LastModify 2006/10/30******************************************************************************/#include "mingui.h"
//---------------------------------------------------------------------------
#if(JPEG_SUPPORT==true)
//---------------------------------------------------------------------------
#include "jpeglib.h"
#include "jerror.h"
/* Expanded data source object for stdio input */
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
STREAM *stream; /* source stream */
BYTE * buffer; /* start of buffer */
BOOL start_of_file; /* have we gotten any data yet? */
} my_source_mgr;
#define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
typedef my_source_mgr * my_src_ptr;
//---------------------------------------------------------------------------
/*
* Initialize source --- called by jpeg_read_header
* before any data is actually read.
*/
static void init_source (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
/* We reset the empty-input-file flag for each image,
* but we don't clear the input buffer.
* This is correct behavior for reading a series of images from one source.
*/
src->start_of_file = TRUE;
}
/*
* Fill the input buffer --- called whenever buffer is emptied.
*/
static BOOL fill_input_buffer (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
size_t nbytes;
nbytes = StreamRead ( src->buffer,INPUT_BUF_SIZE,(STREAM *)src->stream);
if (nbytes <= 0)
{ if (src->start_of_file) /* Treat empty input file as fatal error */
ERREXIT(cinfo, JERR_INPUT_EMPTY);
WARNMS(cinfo, JWRN_JPEG_EOF);
/* Insert a fake EOI marker */
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
src->start_of_file = FALSE;
return TRUE;
}
/*
* Skip data --- used to skip over a potentially large amount of
* uninteresting data (such as an APPn marker).
*/
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
/* Just a dumb implementation for now. Could use fseek() except
* it doesn't work on pipes. Not clear that being smart is worth
* any trouble anyway --- large skips are infrequent.
*/
if (num_bytes > 0) {
while (num_bytes > (long) src->pub.bytes_in_buffer) {
num_bytes -= (long) src->pub.bytes_in_buffer;
(void) fill_input_buffer(cinfo);
/* note we assume that fill_input_buffer will never return FALSE,
* so suspension need not be handled.
*/
}
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
}
/*
* An additional method that can be provided by data source modules is the
* resync_to_restart method for error recovery in the presence of RST markers.
* For the moment, this source module just uses the default resync method
* provided by the JPEG library. That method assumes that no backtracking
* is possible.
*/
/*
* Terminate source --- called by jpeg_finish_decompress
* after all data has been read. Often a no-op.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
static void term_source (j_decompress_ptr cinfo)
{
/* no work necessary here */
}
/*
* Prepare for input from a stdio stream.
* The caller must have already opened the stream, and is responsible
* for closing it after finishing decompression.
*/
//---------------------------------------------------------------------------
void my_jpeg_data_src (j_decompress_ptr cinfo, STREAM *stream)
{
my_src_ptr src;
/* The source object and input buffer are made permanent so that a series
* of JPEG images can be read from the same file by calling jpeg_stdio_src
* only before the first one. (If we discarded the buffer at the end of
* one image, we'd likely lose the start of the next one.)
* This makes it unsafe to use this manager and a different source
* manager serially with the same JPEG object. Caveat programmer.
*/
if (cinfo->src == NULL) /* first time for this JPEG object? */
{ cinfo->src = (struct jpeg_source_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(my_source_mgr));
src = (my_src_ptr) cinfo->src;
src->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
INPUT_BUF_SIZE * sizeof(JOCTET));
}
src = (my_src_ptr) cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = term_source;
src->stream=stream;
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
src->pub.next_input_byte = NULL; /* until buffer loaded */
}
int CM_DecodeJPG(TImageHead *imgHead, STREAM *stream)
{ int ret = 0; /* image load error*/
unsigned char magic[5];
/* This struct contains the JPEG decompression parameters
* and pointers to working space
* (which is allocated as needed by the JPEG library).
*/
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
if(StreamRead(magic,2,stream)!=2)
return 0; /* not JPEG image*/
if (magic[0] != 0xFF || magic[1] != 0xD8)
return 0; /* not JPEG image*/
StreamRead (magic, 4, stream);
StreamRead (magic, 4, stream);
magic [4] = '\0';
if (strncmp((char *)magic, "JFIF", 4) != 0 && strncmp((char *)magic, "Exif", 4) != 0)
return 0; /* not JPEG image*/
StreamSeek(stream, -10, SEEK_CUR);
/* Step 1: allocate and initialize JPEG decompression object */
/* We set up the normal JPEG error routines. */
cinfo.err = jpeg_std_error (&jerr);
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress (&cinfo);
/* Step 2: specify data source */
my_jpeg_data_src (&cinfo, stream);
/* Step 3: read file parameters with jpeg_read_header() */
jpeg_read_header (&cinfo, TRUE);
/* Step 4: set parameters for decompression */
cinfo.out_color_space = JCS_RGB;
cinfo.quantize_colors = FALSE;
#if (BITS_PER_PIXEL <= 8)
{ RGBQUAD rgbColor;
int i;
cinfo.quantize_colors = TRUE;
/* Get system palette */
cinfo.actual_number_of_colors = 0x01 << BITS_PER_PIXEL;
/* Allocate jpeg colormap space */
cinfo.colormap = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE,
(JDIMENSION)cinfo.actual_number_of_colors, (JDIMENSION)3);
imgHead->transPal=(PIXEL *)GetMem(cinfo.actual_number_of_colors*sizeof(PIXEL));
imgHead->palsize=cinfo.actual_number_of_colors;
/* Set colormap from system palette */
for(i = 0; i < cinfo.actual_number_of_colors; ++i)
{ PixelMapToColor(i,&rgbColor);
cinfo.colormap[0][i] = rgbColor.b;
cinfo.colormap[1][i] = rgbColor.g;
cinfo.colormap[2][i] = rgbColor.r;
imgHead->transPal[i]=i;
}
}
#endif
jpeg_calc_output_dimensions (&cinfo);
ret = 0;//ERR_BMP_MEM;
imgHead->bpp = cinfo.output_components * 8;
imgHead->width = cinfo.output_width;
imgHead->height = cinfo.output_height;
imgHead->pitch = ( (imgHead->width*imgHead->bpp+31)>>3 ) & ~0x3;
imgHead->compression=0;
imgHead->bits = (BYTE *)GetMem(imgHead->pitch * imgHead->height);
if (!imgHead->bits) goto err;
/* Step 5: Start decompressor */
jpeg_start_decompress (&cinfo);
/* Step 6: while (scan lines remain to be read) */
while (cinfo.output_scanline < cinfo.output_height)
{ JSAMPROW rowptr[1];
BYTE* bits;
bits = imgHead->bits + cinfo.output_scanline * imgHead->pitch;
rowptr[0] = (JSAMPROW)(bits);
jpeg_read_scanlines (&cinfo, rowptr, 1);
}
ret = TRUE;
err:
/* Step 7: Finish decompression */
jpeg_finish_decompress (&cinfo);
/* Step 8: Release JPEG decompression object */
jpeg_destroy_decompress (&cinfo);
/* May want to check to see whether any corrupt-data
* warnings occurred (test whether jerr.pub.num_warnings is nonzero).
*/
return ret;
}
//---------------------------------------------------------------------------
#else
int CM_DecodeJPG(TImageHead *imgHead, STREAM *stream)
{ return 0;
}
//---------------------------------------------------------------------------
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?