📄 ximajpg.cpp
字号:
/*
* File: ximajpg.cpp
* Purpose: Platform Independent JPEG Image Class Loader and Writer
* 07/Aug/2001 Davide Pizzolato - www.xdp.it
* CxImage version 5.99a 08/Feb/2004
*/
#include "ximajpg.h"
#if CXIMAGE_SUPPORT_JPG
#include "ximaiter.h"
#include <setjmp.h>
struct ima_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
char* buffer; /* error message <CSC>*/
};
typedef ima_error_mgr *ima_error_ptr;
////////////////////////////////////////////////////////////////////////////////
// Here's the routine that will replace the standard error_exit method:
////////////////////////////////////////////////////////////////////////////////
static void
ima_jpeg_error_exit (j_common_ptr cinfo)
{
/* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
ima_error_ptr myerr = (ima_error_ptr) cinfo->err;
/* Create the message */
myerr->pub.format_message (cinfo, myerr->buffer);
/* Send it to stderr, adding a newline */
/* Return control to the setjmp point */
longjmp(myerr->setjmp_buffer, 1);
}
////////////////////////////////////////////////////////////////////////////////
CxImageJPG::CxImageJPG(): CxImage(CXIMAGE_FORMAT_JPG)
{
#if CXIMAGEJPG_SUPPORT_EXIF
memset(&m_exifinfo, 0, sizeof(EXIFINFO));
#endif
}
////////////////////////////////////////////////////////////////////////////////
#if CXIMAGEJPG_SUPPORT_EXIF
bool CxImageJPG::DecodeExif(CxFile * hFile)
{
long pos=hFile->Tell();
CxExifInfo exif(&m_exifinfo);
exif.DecodeExif(hFile);
hFile->Seek(pos,SEEK_SET);
return exif.m_exifinfo->IsExif;
}
#endif //CXIMAGEJPG_SUPPORT_EXIF
////////////////////////////////////////////////////////////////////////////////
bool CxImageJPG::Decode(CxFile * hFile)
{
bool is_exif = false;
#if CXIMAGEJPG_SUPPORT_EXIF
is_exif = DecodeExif(hFile);
#endif
CImageIterator iter(this);
/* 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;
/* We use our private extension JPEG error handler. <CSC> */
struct ima_error_mgr jerr;
jerr.buffer=info.szLastError;
/* More stuff */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
/* In this example we want to open the input file before doing anything else,
* so that the setjmp() error recovery below can assume the file is open.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to read binary files.
*/
/* Step 1: allocate and initialize JPEG decompression object */
/* We set up the normal JPEG error routines, then override error_exit. */
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = ima_jpeg_error_exit;
/* Establish the setjmp return context for my_error_exit to use. */
if (setjmp(jerr.setjmp_buffer)) {
/* If we get here, the JPEG code has signaled an error.
* We need to clean up the JPEG object, close the input file, and return.
*/
jpeg_destroy_decompress(&cinfo);
return 0;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
/* Step 2: specify data source (eg, a file) */
//jpeg_stdio_src(&cinfo, infile);
CxFileJpg src(hFile);
cinfo.src = &src;
/* Step 3: read file parameters with jpeg_read_header() */
(void) jpeg_read_header(&cinfo, TRUE);
/* Step 4 <chupeev> handle decoder options*/
if ((GetCodecOption() & DECODE_GRAYSCALE) != 0)
cinfo.out_color_space = JCS_GRAYSCALE;
if ((GetCodecOption() & DECODE_QUANTIZE) != 0) {
cinfo.quantize_colors = TRUE;
cinfo.desired_number_of_colors = info.nQuality;
}
if ((GetCodecOption() & DECODE_DITHER) != 0)
cinfo.dither_mode = m_nDither;
if ((GetCodecOption() & DECODE_ONEPASS) != 0)
cinfo.two_pass_quantize = FALSE;
if ((GetCodecOption() & DECODE_NOSMOOTH) != 0)
cinfo.do_fancy_upsampling = FALSE;
//<DP>: Load true color images as RGB (no quantize)
/* Step 4: set parameters for decompression */
/* if (cinfo.jpeg_color_space!=JCS_GRAYSCALE) {
* cinfo.quantize_colors = TRUE;
* cinfo.desired_number_of_colors = 128;
*}
*/ //</DP>
// Set the scale <ignacio>
cinfo.scale_denom = info.nScale;
// Borrowed the idea from GIF implementation <ignacio>
if (info.nEscape == -1) {
// Return output dimensions only
jpeg_calc_output_dimensions(&cinfo);
head.biWidth = cinfo.output_width;
head.biHeight = cinfo.output_height;
jpeg_destroy_decompress(&cinfo);
return true;
}
/* Step 5: Start decompressor */
jpeg_start_decompress(&cinfo);
/* We may need to do some setup of our own at this point before reading
* the data. After jpeg_start_decompress() we have the correct scaled
* output image dimensions available, as well as the output colormap
* if we asked for color quantization.
*/
//Create the image using output dimensions <ignacio>
//Create(cinfo.image_width, cinfo.image_height, 8*cinfo.output_components, CXIMAGE_FORMAT_JPG);
Create(cinfo.output_width, cinfo.output_height, 8*cinfo.output_components, CXIMAGE_FORMAT_JPG);
if (!pDib) longjmp(jerr.setjmp_buffer, 1); //<DP> check if the image has been created
if (is_exif){
#if CXIMAGEJPG_SUPPORT_EXIF
if ((m_exifinfo.Xresolution != 0.0) && (m_exifinfo.ResolutionUnit != 0))
SetXDPI((long)(m_exifinfo.Xresolution/m_exifinfo.ResolutionUnit));
if ((m_exifinfo.Yresolution != 0.0) && (m_exifinfo.ResolutionUnit != 0))
SetYDPI((long)(m_exifinfo.Yresolution/m_exifinfo.ResolutionUnit));
#endif
} else {
if (cinfo.density_unit==2){
SetXDPI((long)floor(cinfo.X_density * 254.0 / 10000.0 + 0.5));
SetYDPI((long)floor(cinfo.Y_density * 254.0 / 10000.0 + 0.5));
} else {
SetXDPI(cinfo.X_density);
SetYDPI(cinfo.Y_density);
}
}
if (cinfo.out_color_space==JCS_GRAYSCALE){
SetGrayPalette();
head.biClrUsed =256;
} else {
if (cinfo.quantize_colors==TRUE){
SetPalette(cinfo.actual_number_of_colors, cinfo.colormap[0], cinfo.colormap[1], cinfo.colormap[2]);
head.biClrUsed=cinfo.actual_number_of_colors;
} else {
head.biClrUsed=0;
}
}
/* JSAMPLEs per row in output buffer */
row_stride = cinfo.output_width * cinfo.output_components;
/* Make a one-row-high sample array that will go away when done with image */
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
/* Step 6: while (scan lines remain to be read) */
/* jpeg_read_scanlines(...); */
/* Here we use the library's state variable cinfo.output_scanline as the
* loop counter, so that we don't have to keep track ourselves.
*/
iter.Upset();
while (cinfo.output_scanline < cinfo.output_height) {
if (info.nEscape) longjmp(jerr.setjmp_buffer, 1); // <vho> - cancel decoding
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
// info.nProgress = (long)(100*cinfo.output_scanline/cinfo.output_height);
//<DP> Step 6a: CMYK->RGB */
if ((cinfo.num_components==4)&&(cinfo.quantize_colors==FALSE)){
BYTE k,*dst,*src;
dst=iter.GetRow();
src=buffer[0];
for(long x3=0,x4=0; x3<(long)info.dwEffWidth && x4<row_stride; x3+=3, x4+=4){
k=src[x4+3];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -