bmp转jpg(使用libjpeg)

2024-03-16 05:32
文章标签 使用 jpg bmp libjpeg

本文主要是介绍bmp转jpg(使用libjpeg),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转载:http://blog.csdn.net/kangear/article/details/8576917

  jpg压缩原理可以参考这篇文章http://hi.baidu.com/tiandsp/item/f5a2dcde6ef1405bd73aae41,我很早以前转的一篇文章。

  没有使用libjpeg的压缩代码可以看看这篇文章http://hi.baidu.com/tiandsp/item/9b5843c58a3b4474cfd4f841,也是我很早以前转的。

  这次使用libjpeg库压缩和上一篇的解压正好对应起来,有好多函数名称我都是对称的起的,所以结合起来看效果更好。

  和上一篇一样,只能处理24位和8位的图像。

  代码如下:

复制代码
#include <iostream>
#include <stdio.h>
extern "C"{
#include "jpeglib.h"
};
#pragma comment(lib,"jpeg.lib")
using namespace std;#pragma pack(2)        //两字节对齐,否则bmp_fileheader会占16Byte
struct bmp_fileheader
{unsigned short    bfType;        //若不对齐,这个会占4Byteunsigned long    bfSize;unsigned short    bfReverved1;unsigned short    bfReverved2;unsigned long    bfOffBits;
};struct bmp_infoheader
{unsigned long    biSize;unsigned long    biWidth;unsigned long    biHeight;unsigned short    biPlanes;unsigned short    biBitCount;unsigned long    biCompression;unsigned long    biSizeImage;unsigned long    biXPelsPerMeter;unsigned long    biYpelsPerMeter;unsigned long    biClrUsed;unsigned long    biClrImportant;
};FILE *input_file;
FILE *output_file;struct bmp_fileheader bfh;
struct bmp_infoheader bih;unsigned char *src_buffer;
unsigned char *dst_buffer;void read_bmp_header()
{    fread(&bfh,sizeof(struct bmp_fileheader),1,input_file);fread(&bih,sizeof(struct bmp_infoheader),1,input_file);
}void read_bmp_data()
{fseek(input_file,bfh.bfOffBits,SEEK_SET);src_buffer=new unsigned char[bih.biWidth*bih.biHeight*bih.biBitCount/8];fread(src_buffer,sizeof(unsigned char)*bih.biWidth*bih.biHeight*bih.biBitCount/8,1,input_file);unsigned long width=bih.biWidth;unsigned long height=bih.biHeight;unsigned short depth=unsigned short(bih.biBitCount/8);unsigned char *src_point;unsigned char *dst_point;dst_buffer=new unsigned char[width*height*depth];    src_point=src_buffer+width*depth*(height-1);dst_point=dst_buffer+width*depth*(height-1);for (unsigned long i=0;i<height;i++){for (unsigned long j=0;j<width*depth;j+=depth){if (depth==1)        //处理灰度图
            {dst_point[j]=src_point[j];}if (depth==3)        //处理彩色图
            {dst_point[j+2]=src_point[j+0];dst_point[j+1]=src_point[j+1];dst_point[j+0]=src_point[j+2];}}dst_point-=width*depth;src_point-=width*depth;}
}void synthese_jpeg()
{struct jpeg_compress_struct cinfo;struct jpeg_error_mgr jerr;JSAMPARRAY buffer;unsigned long width=bih.biWidth;unsigned long height=bih.biHeight;unsigned short depth=unsigned short(bih.biBitCount/8);unsigned char *point;cinfo.err=jpeg_std_error(&jerr);        //libjpeg各种配置jpeg_create_compress(&cinfo);jpeg_stdio_dest(&cinfo,output_file);cinfo.image_width=width;cinfo.image_height=height;cinfo.input_components=depth;if (depth==1)cinfo.in_color_space=JCS_GRAYSCALE;elsecinfo.in_color_space=JCS_RGB;jpeg_set_defaults(&cinfo);jpeg_set_quality(&cinfo,20,TRUE);    //中间的值为压缩质量,越大质量越好jpeg_start_compress(&cinfo,TRUE);buffer=(*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);point=dst_buffer+width*depth*(height-1);while (cinfo.next_scanline<height){memcpy(*buffer,point,width*depth);jpeg_write_scanlines(&cinfo,buffer,1);point-=width*depth;}jpeg_finish_compress(&cinfo);jpeg_destroy_compress(&cinfo);
}int main()
{input_file=fopen("lena_gray.bmp","rb");output_file=fopen("lena.jpg","wb");read_bmp_header();read_bmp_data();synthese_jpeg();fclose(input_file);fclose(output_file);delete[] src_buffer;delete[] dst_buffer;cout<<"good job."<<endl;cin.get();return 0;
}

这篇关于bmp转jpg(使用libjpeg)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/814448

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

Navicat数据表的数据添加,删除及使用sql完成数据的添加过程

《Navicat数据表的数据添加,删除及使用sql完成数据的添加过程》:本文主要介绍Navicat数据表的数据添加,删除及使用sql完成数据的添加过程,具有很好的参考价值,希望对大家有所帮助,如有... 目录Navicat数据表数据添加,删除及使用sql完成数据添加选中操作的表则出现如下界面,查看左下角从左

python 常见数学公式函数使用详解(最新推荐)

《python常见数学公式函数使用详解(最新推荐)》文章介绍了Python的数学计算工具,涵盖内置函数、math/cmath标准库及numpy/scipy/sympy第三方库,支持从基础算术到复杂数... 目录python 数学公式与函数大全1. 基本数学运算1.1 算术运算1.2 分数与小数2. 数学函数