`
westice
  • 浏览: 114374 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

可以解压jpeg啦!

阅读更多

终于可以解压jpeg的格式了.

在网上找了一堆方法,现在来总结一下,用的第三方库libjpeg.

首先要装载库文件libjpeg,我已经用dev-cpp的make制好了一个.a的库文件, 和.lib一样的用.还有几个头文件都需要.

 

然后就是调函数,(照抄网上的):

//插入头文件
//网上说这是C写的库,如果调用不了库函数,在编译器选项中选择好库文件就行了
extern "C" 
{
  #include "jpeglib.h"
}
 
 /**************************************************
                 读取jpeg 
                 
                 **************************************************/
		
                 PAINTSTRUCT ps;
                 HDC hdc=BeginPaint(hwnd,&ps);
/span>HDC ui_hdc_jpg=CreateCompatibleDC(hdc);//创建一个兼容的画架
                 //1、声明并初始化解压缩对象,同时制定错误信息管理器
                 struct jpeg_decompress_struct cinfo;
                 struct jpeg_error_mgr jerr;

                 cinfo.err = jpeg_std_error(&jerr);
                 jpeg_create_decompress(&cinfo);
                 
                 //打开图像文件 
                 FILE *f = fopen("pic2.jpg","rb");
                 jpeg_stdio_src(&cinfo, f);
                 
  //在wm_paint消息下使用画布 
                //读取信息头 
                 jpeg_read_header(&cinfo, TRUE);
                 short jpgwidth=cinfo.image_width;
                 short jpgheight=cinfo.image_height;
                 short jpg_components=cinfo.num_components;
                 //创建一个位图句柄,以便在上面画点
                 HBITMAP himage3=(HBITMAP)LoadImage(hinstance,"pic1.bmp",IMAGE_BITMAP,jpgwidth,jpgheight,LR_LOADFROMFILE);

                 //根据信息头设定缓冲区的大小 
                 BYTE* data = new BYTE [jpgwidth*jpgheight*jpg_components];
                 //开始解压缩 
                 jpeg_start_decompress(&cinfo);
                 
                 //开始读取压缩信息 
                 JSAMPROW row_pointer[1];
                 while (cinfo.output_scanline < jpgheight)
                 {
                     unsigned int index=(jpgheight-cinfo.output_scanline-1)*jpgwidth*jpg_components;  
                     //unsigned int index=(cinfo.output_scanline)*jpgwidth*jpg_components;  
                     row_pointer[0]=&data[index];
                     jpeg_read_scanlines(&cinfo,row_pointer ,1);
                 }
                 jpeg_finish_decompress(&cinfo);
                 //释放资源 ,关闭文件 
                 jpeg_destroy_decompress(&cinfo);
                 fclose(f);
                 ////////////////////////////////////////////////////////
                 ///////////////////////////////////////////////////////

 SelectObject(ui_hdc_jpg,himage3);

 

              //一个一个地往画架上画点
               for(int row=0;row<jpgheight;row++)
                 {
                     for(int col=0;col<jpgwidth;col++)
                     {
                         long rowcol_translate=row*(jpgwidth*jpg_components)+col*jpg_components;    
                         unsigned char r=data[rowcol_translate];
                         unsigned char g=data[rowcol_translate+1];
                         unsigned char b=data[rowcol_translate+2];
                         COLORREF rgb=RGB(r,g,b); 
                         
                         //先出来的是最底下的像素 ,所有要从下往上画点才行
                         SetPixel(ui_hdc_jpg,col,jpgheight-row-1,rgb);
                         
                     }    
                 }

//贴在画布上
BitBlt(hdc,0,0,jpgwidth,jpgheight,ui_hdc_jpg,0,0,SRCCOPY);
//释放资源
DeleteDC(ui_hdc_jpg);
EndPaint(hwnd,&ps);
 
分享到:
评论
1 楼 javafound 2010-03-28  

相关推荐

Global site tag (gtag.js) - Google Analytics