📄 jpegfile.h
字号:
////////////////////////////////////////////////////////////
//
// JpegFile - A C++ class to allow reading and writing of
// RGB and Grayscale JPEG images. (actually, it reads all forms
// that the JPEG lib will decode into RGB or grayscale) and
// writes only RGB and Grayscale.
//
// It is based on a Win32 compilation of the IJG V.6a code.
//
// This will only work on 32-bit Windows systems. I have only
// tried this with Win 95, VC++ 4.1.
//
// This class Copyright 1997, Chris Losinger
// This is free to use and modify provided my name is included.
//
// Comments:
// Thanks to Robert Johnson for discovering a DWORD-alignment bug
// Thanks to Lee Bode for catching a bug in CMfcappView::OnFileGetdimensionsjpg()
//
////////////////////////////////////////////////////////////
//
// General Usage:
//
// #include this file.
// link with jpeglib2.lib
//
// All functions here are static. There is no need to have a JpegFile object.
// There is actually nothing in a JpegFile object anyway.
//
// So, you can do this :
//
// BOOL ok = JpegFile::vertFlipBuf(buf, widthbytes, height);
//
// instead of this :
//
// JpegFile jpgOb;
// BOOL ok = jpgOb.vertFlipBuf(buf, widthbytes, height);
//
/////
//
// Linking usage :
// It is sometimes necessary to set /NODEFAULTLIB:LIBC (or LIBCD) to use this
// class.
//
/////
//
// Error reporting:
// The class generates message boxes in response to JPEG errors.
//
// The JpegFile.cpp fn my_error_exit defines the behavior for
// fatal errors : show msg box, return to caller.
//
// Warnings are handled by jpeglib.lib - which generates msg boxes too.
//
////////////////////////////////////////////////////////////////
/*
////////////////////////////////////////////////////////////////
// Reading Usage :
UINT height;
UINT width;
BYTE *dataBuf;
//read the file
dataBuf=JpegFile::JpegFileToRGB(fileName,
&width,
&height);
if (dataBuf==NULL) {
return;
}
// RGB -> BGR
JpegFile::BGRFromRGB(dataBuf, m_width, m_height);
BYTE *buf;
// create a DWORD aligned buffer from the JpegFile object
buf = JpegFile::MakeDwordAlignedBuf(dataBuf,
width,
height,
&m_widthDW);
// flip that buffer
JpegFile::VertFlipBuf(m_buf, m_widthDW, m_height);
// you now have a buffer ready to be used as a DIB
// be sure to delete [] dataBuf; // !!!!!!!!!!
// delete [] buf;
// Writing Usage
BYTE *tmp=NULL;
// assume buf is a DWORD-aligned BGR buffer, vertically flipped
// as if read from a BMP file.
// un-DWORD-align
tmp=JpegFile::RGBFromDWORDAligned(buf,
widthPix,
widthBytes,
height);
// vertical flip
JpegFile::VertFlipBuf(tmp, widthPix * 3, height);
// reverse BGR
JpegFile::BGRFromRGB(tmp, widthPix, height);
if (tmp==NULL) {
AfxMessageBox("~DWORD Memory Error");
return;
}
// write it
BOOL ok=JpegFile::RGBToJpegFile(fileName,
tmp,
width,
height,
TRUE,
75);
if (!ok) {
AfxMessageBox("Write Error");
}
delete [] tmp;
////////////////////////////////////////////////////////////////*/
class JpegFile
{
public:
////////////////////////////////////////////////////////////////
// read a JPEG file to an RGB buffer - 3 bytes per pixel
// returns a ptr to a buffer .
// caller is responsible for cleanup!!!
// BYTE *buf = JpegFile::JpegFileToRGB(....);
// delete [] buf;
static BOOL JpegFileToRGB(CString fileName, // path to image
BYTE** lpimg); // image height
////////////////////////////////////////////////////////////////
// write a JPEG file from a 3-component, 1-byte per component buffer
static BOOL RGBToJpegFile(CString fileName, // path
BYTE **dataBuf, // RGB buffer
UINT width, // pixels
UINT height, // rows
BOOL color, // TRUE = RGB
// FALSE = Grayscale
int quality); // 0 - 100
////////////////////////////////////////////////////////////////
// fetch width / height of an image
static BOOL GetJPGDimensions(CString fileName,
UINT &width,
UINT &height,
UINT &Depth);
////////////////////////////////////////////////////////////////
// convert RGB to grayscale using luminance calculation
// in-place
static BOOL MakeGrayScale(BYTE *buf, // input buf
UINT widthPix, // width in pixels
UINT height); // height
////////////////////////////////////////////////////////////////
// swap Red and Blue bytes
// in-place
static BOOL BGRFromRGB(BYTE *buf, // input buf
UINT widthPix, // width in pixels
UINT height); // lines
////////////////////////////////////////////////////////////////
// these do nothing
JpegFile(); // creates an empty object
~JpegFile(); // destroys nothing
};
// 新的读取JPG文件的程序
#define M_SOF0 0xc0
#define M_DHT 0xc4
#define M_EOI 0xd9
#define M_SOS 0xda
#define M_DQT 0xdb
#define M_DRI 0xdd
#define M_APP0 0xe0
static int Zig_Zag[8][8]={{0, 1, 5, 6, 14, 15, 27, 28},
{2, 4, 7, 13, 16, 26, 29, 42},
{3, 8, 12, 17, 25, 30, 41, 43},
{9, 11, 18, 24, 37, 40, 44, 53},
{10, 19, 23, 32, 39, 45, 52, 54},
{20, 22, 33, 38, 46, 51, 55, 60},
{21, 34, 37, 47, 50, 56, 59, 61},
{35, 36, 48, 49, 57, 58, 62, 63}};
#define W1 2841 // 2048*sqrt(2)*cos(1*pi/16)
#define W2 2676 // 2048*sqrt(2)*cos(2*pi/16)
#define W3 2408 // 2048*sqrt(2)*cos(3*pi/16)
#define W5 1609 // 2048*sqrt(2)*cos(5*pi/16)
#define W6 1108 // 2048*sqrt(2)*cos(6*pi/16)
#define W7 565 // 2048*sqrt(2)*cos(7*pi/16)
class JpegFile1
{
public:
short SampRate_Y_H, SampRate_Y_V;
short SampRate_Y_H8, SampRate_Y_V8;
short SampRate_U_H, SampRate_U_V;
short SampRate_V_H, SampRate_V_V;
short H_YtoU,V_YtoU,H_YtoV,V_YtoV;
short Y_in_MCU,U_in_MCU,V_in_MCU;
unsigned char *lpJpegBuf;
unsigned char *lp;
short qt_table[3][64];
short comp_num;
BYTE comp_index[3];
BYTE YDcIndex,YAcIndex,UVDcIndex,UVAcIndex;
BYTE HufTabIndex;
short *YQtTable,*UQtTable,*VQtTable;
short code_pos_table[4][16],code_len_table[4][16];
unsigned short code_value_table[4][256];
unsigned short huf_max_value[4][16],huf_min_value[4][16];
short BitPos,CurByte;
short rrun,vvalue;
short MCUBuffer[10*64];
int QtZzMCUBuffer[10*64];
short BlockBuffer[64];
short ycoef,ucoef,vcoef;
BOOL IntervalFlag;
short interval;
int Y[4*64],U[4*64],V[4*64];
DWORD sizei,sizej;
short restart;
long iclip[1024];
long *iclp;
DWORD BufSize, JpegBufSize;
DWORD LineBytes;
BYTE* hJpegBuf;
BYTE** lpImgData;
int funcret;
DWORD ImgWidth, ImgHeight, ImgSize;
BYTE bClipArray[1024], *blpClip;
public:
JpegFile1();// creates an empty object
~JpegFile1();// destroys nothing
public:
BOOL ReadJPGData(BYTE **lpParam);
BOOL OpenJPGFile(CString FlieName);
int InitTag();
int Decode();
void GetYUV(short flag);
void StoreBuffer();
int DecodeMCUBlock();
int HufBlock(BYTE dchufindex,BYTE achufindex);
int DecodeElement();
void IQtIZzMCUComponent(short flag);
void IQtIZzBlock(short *s ,int * d,short flag);
void Fast_IDCT(int * block);
BYTE ReadByte();
void idctrow(int * blk);
void idctcol(int * blk);
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -