前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图像处理基础(六)-libjpeg常用算法

图像处理基础(六)-libjpeg常用算法

作者头像
Pulsar-V
发布2019-03-12 17:05:13
8150
发布2019-03-12 17:05:13
举报
文章被收录于专栏:Pulsar-V

bmp文件转化为bgr数据

代码语言:javascript
复制
void bmpfile_to_bgr(char *bmp_file,BYTE **rgb,int *size,int *w,int *h,int *bit)
{
    FILE *fp = fopen(bmp_file,"rb");
    if(fp == NULL) return;
    BITMAPFILEHEADER bmpheader;
    BITMAPINFOHEADER bmpinfo;
    fread(&bmpheader,sizeof(BITMAPFILEHEADER),1,fp);
    fread(&bmpinfo,sizeof(BITMAPINFOHEADER),1,fp);
    *rgb = new BYTE[bmpinfo.biSizeImage];
    fread(*rgb,bmpinfo.biSizeImage,1,fp);
    *size = bmpinfo.biSizeImage;
    *w = bmpinfo.biWidth;
    *h = abs(bmpinfo.biHeight);
    *bit = bmpinfo.biBitCount;
    fclose(fp);
}

调用

代码语言:javascript
复制
BYTE *bgr1 = NULL;
int size=0,w=0,h=0,bit=0;
bmpfile_to_bgr("test.bmp",&bgr1,&size,&w,&h,&bit);
free(bgr1);

bgr数据转化为bmp文件

代码语言:javascript
复制
void bgr_to_bmpfile(char *bmp_file,BYTE *rgb,int size,int w,int h,int bit)
{
    FILE *fp = fopen(bmp_file,"wb+");
    if(fp == NULL) return;
 
    BITMAPFILEHEADER bmpheader;
    BITMAPINFOHEADER bmpinfo;
 
    bmpheader.bfType = 0x4d42;
    bmpheader.bfReserved1 = 0;
    bmpheader.bfReserved2 = 0;
    bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmpheader.bfSize = bmpheader.bfOffBits + size;
 
    bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
    bmpinfo.biWidth = w;
    bmpinfo.biHeight = h;
    bmpinfo.biPlanes = 1;
    bmpinfo.biBitCount = bit;
    bmpinfo.biCompression = BI_RGB;
    bmpinfo.biSizeImage = (w*bit+31)/32*4*h;
    bmpinfo.biXPelsPerMeter = 0;
    bmpinfo.biYPelsPerMeter = 0;
    bmpinfo.biClrUsed = 0;
    bmpinfo.biClrImportant = 0;
         
    fwrite(&bmpheader, sizeof(BITMAPFILEHEADER), 1, fp);
    fwrite(&bmpinfo, sizeof(BITMAPINFOHEADER), 1, fp);
    fwrite(rgb, size, 1, fp);
 
    fclose(fp);
}

调用

代码语言:javascript
复制
bgr_to_bmpfile("test.bmp",bgr1,size,w,h,bit);

翻转r-b分量

bmp的分量是以bgr保存的,但是libjpeg使用的是rgb,所以需要翻转br分量

代码语言:javascript
复制
void bgr_flip_rgb(BYTE *bgr,int size,int bit)
{
    int i = 0;
    for(;i<size;i+=bit/8){
        BYTE b = bgr[i];
        bgr[i] = bgr[i+2];
        bgr[i+2] = b;
    }
}

jpg文件转jpg内存数据

代码语言:javascript
复制
void jpgfile_to_jpgmem(char *jpg_file,BYTE **jpg,int *size)
{
    FILE *fp = fopen(jpg_file,"rb");
    if(fp == NULL) return;
     
    fseek(fp,0,SEEK_END);
    int length = ftell(fp);
    fseek(fp,0,SEEK_SET);
     
    *jpg = new BYTE[length];
    fread(*jpg,length,1,fp);
    *size = length;
     
    fclose(fp);
}

调用

代码语言:javascript
复制
BYTE *jpg = NULL;
int size = 0;
jpgfile_to_jpgmem("Tulips.jpg",&jpg,&size);
free(jpg);

jpg内存数据转jpg文件

代码语言:javascript
复制
void jpgmem_to_jpgfile(char *jpg_file,BYTE *jpg,int size)
{
    FILE *fp = fopen(jpg_file,"wb+");
    if(fp == NULL) return;
    fwrite(jpg,size,1,fp);
    fclose(fp);
}

bgr数据转jpg内存数据

代码语言:javascript
复制
void bgr_to_jpgmem(BYTE *rgb,int size,int w,int h,int bit,BYTE **jpg,int *j_size)
{
    struct jpeg_error_mgr jerr;
    jpeg_std_error(&jerr);
     
    struct jpeg_compress_struct cinfo;
    cinfo.err = &jerr;
    jpeg_create_compress(&cinfo);
     
    jpeg_mem_dest(&cinfo,jpg,(unsigned long*)j_size);
     
    cinfo.image_width = w;
    cinfo.image_height = h;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
     
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, 100, TRUE);
    jpeg_start_compress(&cinfo,TRUE);
    int row_stride = cinfo.image_width*cinfo.input_components;
     
    JSAMPROW row_pointer[1];
    while(cinfo.next_scanline < cinfo.image_height)
    {
        row_pointer[0] = & rgb[(cinfo.image_height-cinfo.next_scanline-1)*row_stride];
        (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
}

bgr数据转为jpg文件

代码语言:javascript
复制
void bgr_to_jpgfile(BYTE *rgb,int size,int w,int h,int bit,char *jpgfile)
{
    struct jpeg_error_mgr jerr;
    jpeg_std_error(&jerr);
     
    struct jpeg_compress_struct cinfo;
    cinfo.err = &jerr;
    jpeg_create_compress(&cinfo);
     
    FILE *fp = fopen(jpgfile,"wb+");
    jpeg_stdio_dest(&cinfo,fp);
    //jpeg_mem_dest(&cinfo,jpg,(unsigned long*)j_size);
     
    cinfo.image_width = w;
    cinfo.image_height = h;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
     
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, 100, TRUE);
    jpeg_start_compress(&cinfo,TRUE);
    int row_stride = cinfo.image_width*cinfo.input_components;
     
    JSAMPROW row_pointer[1];
    while(cinfo.next_scanline < cinfo.image_height)
    {
        //row_pointer[0] = & rgb[cinfo.next_scanline * row_stride];
        row_pointer[0] = & rgb[(cinfo.image_height-cinfo.next_scanline-1)*row_stride];
        (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
     
    fclose(fp);
}

jpg内存数据->bgr数据

代码语言:javascript
复制
void jpgmem_to_bgr(BYTE *jpg,int size,BYTE **bgr,int *b_size,int *w,int *h)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    cinfo.err = jpeg_std_error(&jerr);
     
    jpeg_create_decompress(&cinfo);
    jpeg_mem_src(&cinfo,jpg,size);
     
    jpeg_read_header(&cinfo,TRUE);
    jpeg_start_decompress(&cinfo);
     
    unsigned long width = cinfo.output_width;
    unsigned long height = cinfo.output_height;
    unsigned short depth = cinfo.output_components;
     
    *w = width;
    *h = height;
    *b_size = width*height*depth;
    *bgr = (BYTE*)malloc(width*height*depth);
    memset(*bgr,0,width*height*depth);
     
    JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, width*depth,1);
     
    BYTE *point = (*bgr)+(height-cinfo.output_scanline-1)*(width*depth);
    while(cinfo.output_scanline<height)
    {
        jpeg_read_scanlines(&cinfo, buffer, 1);
        memcpy(point, *buffer, width*depth);
        point -= width*depth;
    }
     
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
}

调用

代码语言:javascript
复制
BYTE *jpg = NULL;
int j_size = 0;
jpgfile_to_jpgmem("Tulips.jpg",&jpg,&j_size);
 
BYTE *bgr = NULL;
int size,w,h,bit=24;
jpgmem_to_bgr(jpg,j_size,&bgr,&size,&w,&h);
bgr_flip_rgb(bgr,size,bit);
bgr_to_bmpfile("Tulips2.bmp",bgr,size,w,h,bit);
free(bgr);
 
free(jpg);
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/07/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • bmp文件转化为bgr数据
    • 调用
    • bgr数据转化为bmp文件
      • 调用
      • 翻转r-b分量
      • jpg文件转jpg内存数据
        • 调用
        • jpg内存数据转jpg文件
        • bgr数据转jpg内存数据
        • bgr数据转为jpg文件
        • jpg内存数据->bgr数据
          • 调用
          相关产品与服务
          图像处理
          图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档