// 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. vue关于img src动态赋值问题

    解决方法: 加个require()就可以了 <img :src="require('../assets/images/'+imgsrc+'.png')"/>

  2. 高可用Kubernetes集群-6. 部署kube-apiserver

    八.部署kube-apiserver 接下来3章节是部署Kube-Master相关的服务,包含:kube-apiserver,kube-controller-manager,kube-schedule ...

  3. 多主机Docker容器的VLAN划分

    原文发表于cu:2016-06-06 参考文档: Docker网络的4种模式,pipework/ovs的简单使用等:http://www.infoq.com/cn/articles/docker-ne ...

  4. 数据挖掘学习笔记——kaggle 数据预处理

    预处理 1. 删除缺失值 a. 删除行即样本(对于样本如果输出变量存在缺失的则直接删除该行,因为无法用该样本训练) b. 删除列,即特征(采用这种删除方式,应保证训练集和验证集都应当删除相同的特征) ...

  5. 《Learning scikit-learn Machine Learning in Python》chapter1

    前言 由于实验原因,准备入坑 python 机器学习,而 python 机器学习常用的包就是 scikit-learn ,准备先了解一下这个工具.在这里搜了有 scikit-learn 关键字的书,找 ...

  6. Python:print()函数的几个常用参数

    1.参数sep:设置输出字符产之间的字符串.默认是空格 name='Tomwenxing' age=' job='student' print(name,age,job) print(name,age ...

  7. 软工1816 · Alpha冲刺(5/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 后端代码复审 福大各个食堂的菜品口味量化.属性标记 组织前后端线下协作 接下来 ...

  8. 404 Note Found· 第七次作业 - 需求分析报告

    目录 组队后的团队项目的整体计划安排 项目logo及思维导图 项目logo 思维导图 产品思维导图 产品思维导图-引导 产品思维导图-后端数据处理.存储 产品思维导图-短信识别 产品思维导图-智能分析 ...

  9. 项目Beta冲刺(团队)第一天

    1.今天解决的进度 成员 进度 陈家权 回复界面设计,由于成员变动加上和其他成员距离较远,服务器404 赖晓连 改进Alpha版本页面没能及时更新的问题 雷晶 获取提问问题时间更新到数据库 林巧娜 今 ...

  10. Swift-switch使用注意点

    1.swift后面的()可以省略 2.case后面的额break可以省略 3.如果想产生case穿透使用fallthrough 4.case后面可以判断多个条件","分割 5.sw ...