JPEG格式
见博客http://hi.baidu.com/loveboa/blog/item/8e2cc9174c490d0cc83d6d42.html非常干净详细
但是Jpg格式太复杂,要按照文件格式一部分一部分来读的话很麻烦,在网上查到了两种库,一个是jpglib一个是ijl,其中jpglib需要包含好多的文件,最后还是选用了ijl,intel jpeg library。
// intput and output filenames
char* input_filename = "GD945281.jpg";
char* output_filename = "output.bmp";
// declare the file handle
HANDLE hbitmapfile;
// Open the bitmap file for output.
hbitmapfile = CreateFile(
output_filename,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
CREATE_ALWAYS,
0, 0);
// create the JPEG structure on the stack
JPEG_CORE_PROPERTIES jcprops;
ZeroMemory(&jcprops, sizeof(JPEG_CORE_PROPERTIES));
if (ijlInit(&jcprops) != IJL_OK)
AfxMessageBox("!");
// intialize the JPEG structure
// set the IJL data source as the input filename
jcprops.JPGFile = input_filename;
// jcprops.JPGSizeBytes=3000*4500;
// read JPEG parameters from the file
if (ijlRead(&jcprops, IJL_JFILE_READPARAMS) != IJL_OK)
AfxMessageBox("!");
// calculate the line offset of the output DIB.
// Windows DIBs are aligned to 4-byte line widths.
int DIBOffset = (jcprops.JPGWidth*3 + 3)/4*4;
// resize the output bitmap file to the actual size the
// bitmap will represent.
SetFilePointer(
hbitmapfile,
DIBOffset * jcprops.JPGHeight + sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER),
0,
FILE_BEGIN);
SetEndOfFile(hbitmapfile);
// map the disk file into an addressable memory region.
HANDLE hFileMappingObject;
hFileMappingObject = CreateFileMapping(
hbitmapfile,
0,
PAGE_READWRITE,
0, 0,
0);
// the memory-mapped file will reside at address
// "bitmapptr"
unsigned char* bitmapptr;
bitmapptr = (unsigned char*) MapViewOfFile(
hFileMappingObject,
FILE_MAP_WRITE,
0, 0, 0);
// write the output bitmap header
BITMAPFILEHEADER *bmfh = (BITMAPFILEHEADER *)bitmapptr;
BITMAPINFOHEADER *bmih = (BITMAPINFOHEADER *)(bitmapptr
+ sizeof(BITMAPFILEHEADER));
bmfh->bfType = 'MB';
bmfh->bfSize = DIBOffset * jcprops.JPGHeight
+ sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfh->bfReserved1 = 0;
bmfh->bfReserved2 = 0;
bmfh->bfOffBits = sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER);
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = jcprops.JPGWidth;
bmih->biHeight = jcprops.JPGHeight;
bmih->biPlanes = 1;
bmih->biBitCount = 24;
bmih->biCompression = BI_RGB;
bmih->biSizeImage = 0;
bmih->biXPelsPerMeter = 1;
bmih->biYPelsPerMeter = 1;
bmih->biClrUsed = 0;
bmih->biClrImportant = 0;
// set up the DIB specification for the JPEG decoder
jcprops.DIBBytes = bitmapptr + sizeof(BITMAPFILEHEADER)
+ sizeof(BITMAPINFOHEADER);
jcprops.DIBWidth = jcprops.JPGWidth;
jcprops.DIBHeight = -(int)jcprops.JPGHeight;
jcprops.DIBColor = IJL_BGR;
jcprops.DIBChannels = 3;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(
jcprops.DIBWidth, jcprops.DIBChannels);
// read data from the JPEG image into the bitmap
ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE);
// clean up and destroy the JPEG Decompressor
ijlFree(&jcprops);
// close the output file
UnmapViewOfFile(bitmapptr);
CloseHandle(hFileMappingObject);
CloseHandle(hbitmapfile);