// 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. TPO 02 - Desert Formation

    TPO 02 - Desert Formation NOTE: 主要意思(大概就是主谓宾)用粗体标出:重要的其它用斜体: []中的是大致意思,可能与原文有关也可能无关,但不会离题 目的为训练句子/段落 ...

  2. 【转】: 塞尔达组在GDC2017演讲的文字翻译:显示的力量

      塞尔达系列推出新作的时候,美术风格都有明显变化.本作的风格比起写实,笔触轻快变化幅度大是其特征.2011年公开的技术演示中,画面风格要更加写实.最终版则更接近于卡通.5年里到底发生了什么呢? ▲2 ...

  3. 网络流dinic模板,邻接矩阵+链式前向星

    //这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...

  4. 基于preteus的1602液晶显示器的学习(LM016L)

    (证明学过,以示纪念) 所谓1602就是每行可以显示16个字符,可以显示两行.1602液晶在工业中使用比较广泛,其基本都采用的是HD44780控制器,或者兼容该指令集,因此基于HD44780写的控制程 ...

  5. PHP 整数

    实验环境php 5.2.17,参考资料:PHP手册 1.整数溢出 如果整数超出最大范围或者运算结果超出最大范围,都会转变成float <?php $a=2147483647; var_dump( ...

  6. Python 装饰器Decorator(一)

    (一) 装饰器基础知识 什么是Python装饰器?Python里装饰器是一个可调用的对象(函数),其参数是另一个函数(被装饰的函数) 假如有一个名字为somedecorator的装饰器,target是 ...

  7. RESTful源码笔记之RESTful Framework的基本组件

    快速实例 Quickstart 序列化 创建一个序列化类 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之类的表示形式的方式. ...

  8. Beautiful Year(拆分四位数)

    Description It seems like the year of 2013 came only yesterday. Do you know a curious fact? The year ...

  9. windows环境下nginx服务器的安装与配置

    转载至:http://www.cnblogs.com/hxxy2003/archive/2012/09/20/2695254.html nginx服务器是一个高性能的HTTP和反向代理服务器,它以稳定 ...

  10. lintcode-436-最大正方形

    436-最大正方形 在一个二维01矩阵中找到全为1的最大正方形 样例 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 标签 动态规划 爱彼迎 脸书 思路 使用 ...