📄 imagejpg.cpp
字号:
// ImageJpg.cpp: implementation of the IEDib class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "IEDib.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
// Jpeg libray routine
// Jpeg libray routine
#include "jpeglib.h"
METHODDEF (void)
error_exit (j_common_ptr cinfo)
{
char sz[256];
(cinfo->err->format_message) (cinfo, sz);
strcat (sz, "\n");
}
BOOL IEDib::LoadJPG(LPCTSTR lpszFileName)
{
struct jpeg_decompress_struct dinfo;
struct jpeg_error_mgr jerr;
dinfo.err = jpeg_std_error (&jerr);
jerr.error_exit = error_exit; // Register custom error manager.
FILE* infile; // source file
if ((infile = fopen( (LPSTR)lpszFileName, "rb") ) == NULL)
return FALSE;
jpeg_create_decompress(&dinfo);
try
{
jpeg_stdio_src(&dinfo, infile);
(void) jpeg_read_header(&dinfo, TRUE);
// Choose floating point DCT method.
dinfo.dct_method = JDCT_FLOAT;
jpeg_start_decompress (&dinfo);
if( dinfo.out_color_space == JCS_GRAYSCALE )
{
//DecodeGray(&dinfo);
BYTE* pDst;
int CurLine = 0;
JSAMPARRAY ppDst = &pDst;
int w = dinfo.image_width;
int h = dinfo.image_height;
CreateGrayImage(w, h, 255);
BYTE** ptr = GetPtr();
while (CurLine < h)
{
pDst = ptr[CurLine];
jpeg_read_scanlines(&dinfo, ppDst, 1);
CurLine++;
}
FreePtr(ptr);
}
else
{
//DecodeRGB(&dinfo);
BYTE* pDst;
BYTE* pBuf;
int CurLine = 0;
JSAMPARRAY ppBuf = &pDst;
int w = dinfo.image_width;
int h = dinfo.image_height;
CreateRGBImage(w, h, RGB(255, 255, 0));
RGBBYTE** ptr = GetRGBPtr();
if( dinfo.output_components == 3 )
{
while (CurLine < h)
{
pDst = (BYTE*)ptr[CurLine];
jpeg_read_scanlines(&dinfo, ppBuf, 1);
CurLine++;
}
}
else
{
int row_stride = dinfo.output_width * dinfo.output_components;
pBuf = new BYTE[row_stride];
pDst = pBuf;
while (CurLine < h)
{
jpeg_read_scanlines(&dinfo, ppBuf, 1);
for( int col = 0 ; col < w ; col++ )
{
ptr[CurLine][col].g = pDst[col*4];
ptr[CurLine][col].b = pDst[col*4+1];
ptr[CurLine][col].r = pDst[col*4+2];
}
CurLine++;
}
delete [] pBuf;
}
FreePtr(ptr);
}
jpeg_finish_decompress (&dinfo);
jpeg_destroy_decompress(&dinfo);
fclose( infile );
}
catch(CException)
{
jpeg_abort_decompress(&dinfo);
jpeg_destroy_decompress(&dinfo);
fclose( infile );
return FALSE;
}
return TRUE;
}
BOOL IEDib::SaveJPG(LPCTSTR lpszFileName)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error (&jerr);
jerr.error_exit = error_exit; // Register custom error manager.
FILE* outfile; // source file
if ((outfile = fopen( (LPSTR)lpszFileName, "wb") ) == NULL)
return FALSE;
jpeg_create_compress(&cinfo);
try
{
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = GetWidth(); // image width and height, in pixels
cinfo.image_height = GetHeight();
cinfo.dct_method = JDCT_FLOAT;
if( GetBitCount() == 8 )
{
cinfo.input_components = 1; // # of color components per pixel
cinfo.in_color_space = JCS_GRAYSCALE; // colorspace of input image
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE );
jpeg_start_compress(&cinfo, TRUE);
//EncodeGray(&cinfo);
BYTE* pDst;
int CurLine = 0;
JSAMPARRAY ppDst = &pDst;
int w = GetWidth();
int h = GetHeight();
BYTE** ptr = GetPtr();
while (CurLine < h)
{
pDst = ptr[CurLine];
jpeg_write_scanlines(&cinfo, ppDst, 1);
CurLine++;
}
FreePtr(ptr);
}
else if( GetBitCount() == 24 )
{
cinfo.input_components = 3; // # of color components per pixel
cinfo.in_color_space = JCS_RGB; // colorspace of input image
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, TRUE );
jpeg_start_compress(&cinfo, TRUE);
//EncodeRGB(&cinfo);
BYTE* pDst;
int CurLine = 0;
JSAMPARRAY ppBuf = &pDst;
int w = GetWidth();
int h = GetHeight();
RGBBYTE** ptr = GetRGBPtr();
while (CurLine < h)
{
pDst = (BYTE*)ptr[CurLine];
jpeg_write_scanlines(&cinfo, ppBuf, 1);
CurLine++;
}
FreePtr(ptr);
}
else
{
return FALSE;
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose( outfile );
}
catch(CException)
{
jpeg_abort_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose( outfile );
return FALSE;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -