// bmp2jpg.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "jpeglib.h"
#include "stdlib.h"
#pragma comment(lib,"libjpeg.lib")
#pragma pack(4) //两字节对齐,否则bmp_fileheader会占16Byte /*图像bmp格式文件头结构*/
struct bmp_fileheader
{
unsigned short bfType; //若不对齐,这个会占4Byte
unsigned long bfSize;
unsigned short bfReverved1;
unsigned short bfReverved2;
unsigned long bfOffBits;
}; /*图像bmp格式信息头结构*/
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 ; //输入bmp文件指针
FILE *output_file; //输出jpg文件指针
bmp_fileheader bmpFh;
bmp_infoheader bmpIh; unsigned char *src_buffer;
unsigned char *dst_buffer; /*读取图像bmp文件头结构*/
void readBmpHeader()
{
/*读取输入bmp格式图像文件头*/
fread(&bmpFh , sizeof(bmp_fileheader) , , input_file); /*读取输入bmp格式图像信息头*/
fread(&bmpIh , sizeof(bmp_infoheader), , input_file);
} /*读取图像bmp数据*/
void readBmpData()
{
fseek(input_file , bmpFh.bfOffBits,SEEK_SET);
src_buffer = (unsigned char *)malloc(sizeof(unsigned char)*bmpIh.biWidth*bmpIh.biHeight*bmpIh.biBitCount/);
if(NULL == src_buffer)
{
printf("malloc memory failed!\n");
return ;
} fread(src_buffer , sizeof(unsigned char)*bmpIh.biWidth*bmpIh.biHeight*bmpIh.biBitCount/, , input_file); /*将bmp数据读取到目标huffbuffer*/
unsigned long width = bmpIh.biWidth ;
unsigned long height= bmpIh.biHeight ;
unsigned short depth= bmpIh.biBitCount/ ;
unsigned char *src_point ;
unsigned char *dst_point ;
unsigned long i = ;
unsigned long j = ;
unsigned long rgb_index = ; dst_buffer = (unsigned char *)malloc(sizeof(unsigned char)*width*height*depth);
if(NULL == dst_buffer)
{
printf("malloc memory failed!\n");
return ;
} src_point = src_buffer + width*depth*(height-);
dst_point = dst_buffer + width*depth*(height-); for(i = ; i < height ; i++)
{
for(j = ; j < width*depth ; j+=depth)
{
if(depth == )
{
dst_point[j] = src_point[j];
} if(depth == )
{
dst_point[j+]=src_point[j+]; dst_point[j+]=src_point[j+]; dst_point[j+]=src_point[j+];
} } 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=bmpIh.biWidth;
unsigned long height=bmpIh.biHeight;
unsigned short depth=bmpIh.biBitCount/;
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==)
cinfo.in_color_space=JCS_GRAYSCALE;
else
cinfo.in_color_space=JCS_RGB; jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo,,TRUE); //中间的值为压缩质量,越大质量越好
jpeg_start_compress(&cinfo,TRUE); buffer=(*cinfo.mem->alloc_sarray)
((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,);
point=dst_buffer+width*depth*(height-);
while (cinfo.next_scanline<height)
{
memcpy(*buffer,point,width*depth);
jpeg_write_scanlines(&cinfo,buffer,);
point-=width*depth;
} jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
} int main()
{
input_file=fopen("E:/test_pic.bmp","rb");
if(NULL == input_file)
{
printf("open input file failed!\n");
}
output_file=fopen("E:/save_pic.jpg","wb");
if(NULL == output_file)
{
printf("open output file failed!\n");
} readBmpHeader();
readBmpData();
synthese_jpeg();
fclose(input_file);
fclose(output_file); free(src_buffer);
free(dst_buffer); return ;
}

图像格式转换之BMP格式转换为JPG格式的更多相关文章

  1. webm视频转换 其他视频格式转换为webm格式

    将其他视频格式转换为webm格式 https://files.cnblogs.com/files/bubuchu/html5videoshipingeshizhuanhuanqi.zip

  2. 将windows文本格式转换为UNIX格式

    将windows文本格式转换为UNIX格式 1.使用sed命令来进行转换,如下: sed -e ’s,^M,,g’ textfile 其中^M的输入方法是Ctrl+V, Ctrl+M 对于批量文件的处 ...

  3. elf格式转换为hex格式文件的两种方法

    这周工作终于不太忙了,可以写点笔记总结一下了. 之前的文章如何在Keil-MDK开发环境生成Bin格式文件,介绍了如何在Keil开发环境使用fromelf软件,将生成的axf文件转换为bin文件,这次 ...

  4. Sony索尼数码录音笔MSV格式转换为MP3格式【转】

    本文转载自:http://blog.sina.com.cn/s/blog_4b2c860f0100d78w.html Sony索尼数码录音笔一般存储为WAV格式,有些没有特意修改存储格式的就保存为MS ...

  5. 如何将腾讯视频的qlv格式转换为mp4格式

    基本上每个视频app都会有自己固有的视频播放格式,比如优酷的KUX.爱奇艺的QSV和腾讯的QLV等.而今天我们重点介绍腾讯的QLV格式如何转换为MP4格式,小便也是经过多次的摸索多次的软件试用,发现的 ...

  6. 图片bmp格式转换为jpg格式

    一下代码经过个人测试,可用 注意:将jpg格式的图片重命名为bmp格式,在该代码中是不能转换的,会报空值异常!而且IE10是显示不了这样的图片的 import java.awt.Image; impo ...

  7. 【程序练习】——ini格式转换为xml格式

    ;Configuration of http [http] doamin=www.mysite.com port= cgihome=/cgi-bin   ;Configuration of db [d ...

  8. 【转】将 azw3 格式转换为 mobi 格式并保持原有排版格式

    小伙伴多次向 Kindle 伴侣提出一个问题,那就是通过 Calibre 将排版精美的 azw3 格式电子书转换成 mobi 格式后推送到 Kindle,排版格式会发生很大的变化,比如行距过窄.内嵌字 ...

  9. Photoshop教程,视频MP4格式转换为GIF格式

    转自百度问题 https://zhidao.baidu.com/question/1497485136643778259.html Adobe PhotoShop软件的最bai新du本是可以编辑视zh ...

随机推荐

  1. Python教程 深入条件控制

    while 和 if 条件句中可以使用任意操作,而不仅仅是比较操作. 比较操作符 in 和 not in 校验一个值是否在(或不在)一个序列里.操作符 is 和 is not 比较两个对象是不是同一个 ...

  2. SVN部署与简单使用

    原文发表于cu:2016-05-24 参考文档: http://www.tuicool.com/articles/Yv2iyu7 http://www.centoscn.com/CentosServe ...

  3. 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴

    上一节学习了使用Cocos2d-x播放音乐的方法,但是那种方法一般只适合于播放较大的音乐,而一般比较短小的音乐(如游戏中的打斗.按键音效等)则要通过playEffect来播放.本节使用该方法以及之前学 ...

  4. Spark入门(Python)

    Hadoop是对大数据集进行分布式计算的标准工具,这也是为什么当你穿过机场时能看到”大数据(Big Data)”广告的原因.它已经成为大数据的操作系统,提供了包括工具和技巧在内的丰富生态系统,允许使用 ...

  5. CSS中水平居中设置的几种方式

    1.行内元素: 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的. <body> <div class="t ...

  6. find . -name file -exec echo abc > {} \; fail

    find . -name file -exec echo abc > {} \; fail 应该改用: find . -name file -exec bash -c 'echo abc > ...

  7. 使用命令virsh管理网络设备,创建桥设备 和 使用virt-manager创建虚拟机

    # 1:查看网卡接口 virsh iface-list Name State MAC Address ------------------------------------------------- ...

  8. ProcessList.java和adj值

    简单地讲,adj值决定了在系统资源吃紧的情况下,要先杀掉哪些进程. 在Android的lowmemroykiller机制中,会对于所有进程进行分类,对于每一类别的进程会有其oom_adj值的取值范围, ...

  9. linux下面Zookeeper的单机模式(standalone)

    1.下载 zk下载地址 http://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/ 我用的是http://mirrors.tuna.tsinghua.e ...

  10. ie浏览器升级的正确姿势

    一.版本说明 1.当前IE浏览器分为一下几个版本:IE 6,IE 7,IE 8,IE 9,IE 10,IE 11 2.windows最高支持IE版本win xp:IE 8win 7 :IE 11win ...