关闭

jpeg转bmp

208人阅读 评论(0) 收藏 举报
解jpeg压缩用到jpeglib.h库
ubuntu安装jpeglib.h命令为audo apt-get install libjpeg62-dev
 
bmp.h
typedef struct tagBITMAPFILEHEADER
{
 unsigned char bfType[2];
 unsigned char bfSize[4];
 unsigned char bfReserved1[2];
 unsigned char bfReserved2[2];
 unsigned char bfOffBits[4];
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER
{
 unsigned char biSize[4];
 unsigned char biWidth[4];
 unsigned char biHeight[4];
 unsigned char biPlanes[2];
 unsigned char biBitCount[2];
 unsigned char biCompression[4];
 unsigned char biSizeImage[4];
 unsigned char biXPelsPerMeter[4];
 unsigned char biYPelsPerMeter[4];
 unsigned char biClrUsed[4];
 unsigned char biClrImportant[4];
}BITMAPINFOHEADER;
 
main.c
#include
#include
#include "bmp.h"
#include
#include
#include
#include
char * jpgFileName = "test.jpg";
char * bmpFileName = "output.bmp";
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;
long width = 0;
long height = 0;
unsigned char * RGB_Ptr = NULL;
long getSize(int hByte,int lByte)
{
 long result = (hByte/16)*(16*16*16);
 result += (hByte%16)*(16*16);
 result += (lByte/16)*(16);
 result += (lByte%16)*(1);
 return result;
}
unsigned char * LongToByte(unsigned long value)
{ //用法LongToByte(2359350)[i]);
 unsigned char result[5];
 unsigned long cacheVal = value;
 int i=0;
 char *p;
 for(i=0;i<4;i++)
 {
  result[i]=(unsigned char)(cacheVal%16);
  cacheVal = cacheVal/16;
  result[i]+=(unsigned char)(cacheVal%16)*16;
  cacheVal = cacheVal/16;
 }
  result[4] = '\0';
  p=result;
    return p;
}
int main()
{
  FILE * f1 = NULL;
  FILE * f2 = NULL;
 unsigned char * bmpDataBuffer = NULL;
 struct jpeg_decompress_struct cinfo;
 struct jpeg_error_mgr jerr;
 cinfo.err = jpeg_std_error(&jerr);
 jpeg_create_decompress(&cinfo);
 f1 = fopen(jpgFileName,"rb");
 if(f1 == NULL)
 {
  printf("输入文件未找到!!!\n");
  return 0;
 }
 jpeg_stdio_src(&cinfo,f1);
 jpeg_read_header(&cinfo,1);
 {
  width = cinfo.image_width;
  height = cinfo.image_height;
  if(cinfo.num_components != 3)
  {
   printf("不是彩色图\n");
   return 0;
  }
  printf(" Width: %ld ; Height: %ld ; Components: %d\n",width,height,cinfo.num_components);
 }
 unsigned char *lineData =NULL;
  JSAMPARRAY buffer;
  jpeg_start_decompress(&cinfo);
  bmpDataBuffer = malloc(width*height*3);
  lineData = malloc(width*3);
  buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE,(width*3),1);
  while(cinfo.output_scanline < cinfo.output_height)
  {
   int j=0;
   jpeg_read_scanlines(&cinfo,buffer,1);
   for(j=0;j<(width*3);j++)
   {
    bmpDataBuffer[(height - cinfo.output_scanline)*(width*3)+j] = buffer[0][j+2]; //R
   j++;
   bmpDataBuffer[(height - cinfo.output_scanline)*(width*3)+j] = buffer[0][j]; //G
   j++;
   bmpDataBuffer[(height - cinfo.output_scanline)*(width*3)+j] = buffer[0][j-2]; //B
  }
  }
 jpeg_finish_decompress(&cinfo);
 jpeg_destroy_decompress(&cinfo);
 f2=fopen(bmpFileName,"wb");
 int i=0;
 unsigned long fileSize=0;
 memset(&fileHeader,0,sizeof(fileHeader));
 fileHeader.bfType[0]=0x42;
 fileHeader.bfType[1]=0x4D;
 fileSize=width*height*3+54;
 for(i=0;i<4;i++)
 {
  fileHeader.bfSize[i] = LongToByte(fileSize)[i];
  //printf("%x ",fileHeader.bfSize[i]);
 }
 for(i=0;i<4;i++)
 {
  fileHeader.bfOffBits[i] = LongToByte(54)[i];
 }
 fwrite(&fileHeader,sizeof(fileHeader),1,f2);
 //构建bmp信息头
 for(i=0;i<4;i++)
 {
    infoHeader.biSize[i] = LongToByte(40)[i];
 }
 for(i=0;i<4;i++)
 {
  infoHeader.biWidth[i] = LongToByte(width)[i];
 }
 for(i=0;i<4;i++)
 {
  infoHeader.biHeight[i] = LongToByte(height)[i];
 }
 infoHeader.biPlanes[0] = 0x01;
 infoHeader.biPlanes[1] = 0x00;
 infoHeader.biBitCount[0] = 0x18;
 infoHeader.biBitCount[1] = 0x00;
 for(i=0;i<4;i++)
 {
  infoHeader.biCompression[i] = 0;
 }
 for(i=0;i<4;i++)
 {
  infoHeader.biSizeImage[i] = LongToByte((width*height))[i];
 }
 fwrite(&infoHeader,sizeof(infoHeader),1,f2);
 fwrite(bmpDataBuffer,width*height*3,1,f2);
 fclose(f2);
 
 fclose(f1);
 return 0;
}
编译 gcc main.c -o leaf -ljpeg
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(1001) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:37258次
    • 积分:450
    • 等级:
    • 排名:千里之外
    • 原创:90篇
    • 转载:10篇
    • 译文:0篇
    • 评论:0条
    文章分类