使用jpeglib,实现jpg和bmp互转
2011年10月08日
到http://www.ijg.org下载库文件
1、头文件
#include "afx.h"
#include
extern "C" {
#include "jpeglib.h"
}
#pragma comment( lib , "libjpeg.lib" )
int ByteAlign( int nBits )
{
int nAlignBytes = ( nBits + 31 ) / 32 * 4;
return nAlignBytes;
}
extern "C" int __declspec(dllexport) __stdcall JpegFromBMP24(CString &strBmpName,CString &strJpgName,int iCompressed=80); //int nWidth, int nHeight, int nLineBytes, LPBYTE &pJpgOut);
extern "C" bool __declspec(dllexport) __stdcall JpegToBMP(char *input, char *output);
2、cpp文件
#include "JpgFile.h"
int setBmpHeader(BITMAPINFOHEADER &BMIH, BITMAPFILEHEADER &bmfh,struct jpeg_decompress_struct *info)
{
memset(&BMIH, 0, sizeof(BITMAPINFOHEADER)) ;
BMIH.biSize = sizeof(BITMAPINFOHEADER) ;
BMIH.biBitCount = 24 ;
BMIH.biPlanes = 1 ;
BMIH.biCompression = BI_RGB;
BMIH.biWidth =info->output_width;
BMIH.biHeight = 0-info->output_height;//高度是负数。表示bmp的反转
BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * info->output_height;//BMIH.biWidth*cinfo.output_height*3;//((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * BMIH.biHeight;
int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
LONG lImageSize = BMIH.biSizeImage ;
LONG lFileSize = nBitsOffset + lImageSize ;
bmfh.bfType = 'B'+('M'bmWidth;//位图宽度
int nHeight=pBitMap->bmHeight;//位图高度
int nSrcColorBits = (int)(pBitMap->bmBitsPixel);//每个颜色多少位表示
int nSrcBitsPiexel = 0;
switch( nSrcColorBits)
{
case 16:
nSrcBitsPiexel = 2;
break;
case 24:
nSrcBitsPiexel = 3; //每个像素3个字节
break;
case 32:
nSrcBitsPiexel = 4;
break;
default:
nSrcBitsPiexel = 3;
break;
}
int nLineBytes = ByteAlign( nWidth * nSrcBitsPiexel * 8 );//字节对齐 每行多少字节
struct jpeg_compress_struct jcs;//申请压缩结构
struct jpeg_error_mgr jsrcerr;
BYTE *pRGB24In =(BYTE*)pBitMap->bmBits;//位图的数据内容
if (nWidth == 0) return 0;
if (nHeight == 0) return 0;
/* initialize the JPEG compression object. */
jcs.err = jpeg_std_error(&jsrcerr);
jpeg_create_compress(&jcs);//建立压缩
unsigned long lsize = 0;
BYTE* lpJpgData = NULL;
BYTE* pJpgOut=NULL;
jpeg_mem_dest(&jcs, &lpJpgData, &lsize); //初始化jcs
jcs.image_width = nWidth; /* image widthPix and height, in pixels */
jcs.image_height = nHeight;
jcs.input_components = 3; /* # of color components per pixel */
jcs.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&jcs);
int nCompressed = iCompressed; /*[0-100], if 0, quality is best, but size is biggest*/
jpeg_set_quality(&jcs, nCompressed, TRUE /* limit to baseline-JPEG values */);
jpeg_start_compress(&jcs, TRUE);//开始压缩
while (jcs.next_scanline BMP header */
fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), out);
fwrite(&BMIH, 1, sizeof(BITMAPINFOHEADER), out);
/* SAMPLEs per row in output buffer */
row_width = cinfo.output_width * cinfo.output_components;
/* calculate number of padding */
num_padding = cinfo.output_width%4;
/* set buffer */
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_width, 1);
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
convertColor((char *)buffer[0], cinfo.output_width);
fwrite(buffer[0], 1, row_width, out);
fwrite("0", 1, num_padding, out);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(in);
fclose(out);
return TRUE;
}
2011年10月08日
到http://www.ijg.org下载库文件
1、头文件
#include "afx.h"
#include
extern "C" {
#include "jpeglib.h"
}
#pragma comment( lib , "libjpeg.lib" )
int ByteAlign( int nBits )
{
int nAlignBytes = ( nBits + 31 ) / 32 * 4;
return nAlignBytes;
}
extern "C" int __declspec(dllexport) __stdcall JpegFromBMP24(CString &strBmpName,CString &strJpgName,int iCompressed=80); //int nWidth, int nHeight, int nLineBytes, LPBYTE &pJpgOut);
extern "C" bool __declspec(dllexport) __stdcall JpegToBMP(char *input, char *output);
2、cpp文件
#include "JpgFile.h"
int setBmpHeader(BITMAPINFOHEADER &BMIH, BITMAPFILEHEADER &bmfh,struct jpeg_decompress_struct *info)
{
memset(&BMIH, 0, sizeof(BITMAPINFOHEADER)) ;
BMIH.biSize = sizeof(BITMAPINFOHEADER) ;
BMIH.biBitCount = 24 ;
BMIH.biPlanes = 1 ;
BMIH.biCompression = BI_RGB;
BMIH.biWidth =info->output_width;
BMIH.biHeight = 0-info->output_height;//高度是负数。表示bmp的反转
BMIH.biSizeImage = ((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * info->output_height;//BMIH.biWidth*cinfo.output_height*3;//((((BMIH.biWidth * BMIH.biBitCount) + 31) & ~31) >> 3) * BMIH.biHeight;
int nBitsOffset = sizeof(BITMAPFILEHEADER) + BMIH.biSize;
LONG lImageSize = BMIH.biSizeImage ;
LONG lFileSize = nBitsOffset + lImageSize ;
bmfh.bfType = 'B'+('M'bmWidth;//位图宽度
int nHeight=pBitMap->bmHeight;//位图高度
int nSrcColorBits = (int)(pBitMap->bmBitsPixel);//每个颜色多少位表示
int nSrcBitsPiexel = 0;
switch( nSrcColorBits)
{
case 16:
nSrcBitsPiexel = 2;
break;
case 24:
nSrcBitsPiexel = 3; //每个像素3个字节
break;
case 32:
nSrcBitsPiexel = 4;
break;
default:
nSrcBitsPiexel = 3;
break;
}
int nLineBytes = ByteAlign( nWidth * nSrcBitsPiexel * 8 );//字节对齐 每行多少字节
struct jpeg_compress_struct jcs;//申请压缩结构
struct jpeg_error_mgr jsrcerr;
BYTE *pRGB24In =(BYTE*)pBitMap->bmBits;//位图的数据内容
if (nWidth == 0) return 0;
if (nHeight == 0) return 0;
/* initialize the JPEG compression object. */
jcs.err = jpeg_std_error(&jsrcerr);
jpeg_create_compress(&jcs);//建立压缩
unsigned long lsize = 0;
BYTE* lpJpgData = NULL;
BYTE* pJpgOut=NULL;
jpeg_mem_dest(&jcs, &lpJpgData, &lsize); //初始化jcs
jcs.image_width = nWidth; /* image widthPix and height, in pixels */
jcs.image_height = nHeight;
jcs.input_components = 3; /* # of color components per pixel */
jcs.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&jcs);
int nCompressed = iCompressed; /*[0-100], if 0, quality is best, but size is biggest*/
jpeg_set_quality(&jcs, nCompressed, TRUE /* limit to baseline-JPEG values */);
jpeg_start_compress(&jcs, TRUE);//开始压缩
while (jcs.next_scanline BMP header */
fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), out);
fwrite(&BMIH, 1, sizeof(BITMAPINFOHEADER), out);
/* SAMPLEs per row in output buffer */
row_width = cinfo.output_width * cinfo.output_components;
/* calculate number of padding */
num_padding = cinfo.output_width%4;
/* set buffer */
buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_width, 1);
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, buffer, 1);
convertColor((char *)buffer[0], cinfo.output_width);
fwrite(buffer[0], 1, row_width, out);
fwrite("0", 1, num_padding, out);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(in);
fclose(out);
return TRUE;
}