Chinaunix首页 | 论坛 | 博客
  • 博客访问: 116137
  • 博文数量: 29
  • 博客积分: 826
  • 博客等级: 上士
  • 技术积分: 390
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-11 08:30
文章分类
文章存档

2012年(29)

我的朋友

分类: LINUX

2012-07-03 07:02:10

解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
阅读(1979) | 评论(0) | 转发(1) |
0

上一篇:V4L2连续采集帧

下一篇:jpeg解压 压成jpeg

给主人留下些什么吧!~~