图像格式转换之BMP格式转换为JPG格式
// 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格式的更多相关文章
- webm视频转换 其他视频格式转换为webm格式
将其他视频格式转换为webm格式 https://files.cnblogs.com/files/bubuchu/html5videoshipingeshizhuanhuanqi.zip
- 将windows文本格式转换为UNIX格式
将windows文本格式转换为UNIX格式 1.使用sed命令来进行转换,如下: sed -e ’s,^M,,g’ textfile 其中^M的输入方法是Ctrl+V, Ctrl+M 对于批量文件的处 ...
- elf格式转换为hex格式文件的两种方法
这周工作终于不太忙了,可以写点笔记总结一下了. 之前的文章如何在Keil-MDK开发环境生成Bin格式文件,介绍了如何在Keil开发环境使用fromelf软件,将生成的axf文件转换为bin文件,这次 ...
- Sony索尼数码录音笔MSV格式转换为MP3格式【转】
本文转载自:http://blog.sina.com.cn/s/blog_4b2c860f0100d78w.html Sony索尼数码录音笔一般存储为WAV格式,有些没有特意修改存储格式的就保存为MS ...
- 如何将腾讯视频的qlv格式转换为mp4格式
基本上每个视频app都会有自己固有的视频播放格式,比如优酷的KUX.爱奇艺的QSV和腾讯的QLV等.而今天我们重点介绍腾讯的QLV格式如何转换为MP4格式,小便也是经过多次的摸索多次的软件试用,发现的 ...
- 图片bmp格式转换为jpg格式
一下代码经过个人测试,可用 注意:将jpg格式的图片重命名为bmp格式,在该代码中是不能转换的,会报空值异常!而且IE10是显示不了这样的图片的 import java.awt.Image; impo ...
- 【程序练习】——ini格式转换为xml格式
;Configuration of http [http] doamin=www.mysite.com port= cgihome=/cgi-bin ;Configuration of db [d ...
- 【转】将 azw3 格式转换为 mobi 格式并保持原有排版格式
小伙伴多次向 Kindle 伴侣提出一个问题,那就是通过 Calibre 将排版精美的 azw3 格式电子书转换成 mobi 格式后推送到 Kindle,排版格式会发生很大的变化,比如行距过窄.内嵌字 ...
- Photoshop教程,视频MP4格式转换为GIF格式
转自百度问题 https://zhidao.baidu.com/question/1497485136643778259.html Adobe PhotoShop软件的最bai新du本是可以编辑视zh ...
随机推荐
- 2.0 flume、sqoop、oozie/Azkaban
在一个完整的大数据处理系统中,除了hdfs+mapreduce+hive组成分析系统的核心之外,还需要数据采集.结果数据导出.任务调度等不可或缺的辅助系统,而这些辅助工具在hadoop生态体系中都有便 ...
- Redis的数据类型以及每种数据类型的使用场景
人就是很奇怪的动物,很简单的问题往往大家都容易忽略,当我们在使用分布式缓存Redis的时候,一个最简单的问题Redis的数据类型以及每种数据类型的使用场景是什么? 是不是觉得这个问题很基础?我也这么觉 ...
- eclipse提示找不到dubbo.xsb报错
需要下载一个dubbo.xsb文件到本地,并在eclipse中配置 下载路径:下载链接 下载方法: a).带开链接 b).点击[Raw]按钮 c). 右键->另存为 在eclipse中配置xsb ...
- Paper Reading - Mind’s Eye: A Recurrent Visual Representation for Image Caption Generation ( CVPR 2015 )
Link of the Paper: https://ieeexplore.ieee.org/document/7298856/ A Correlative Paper: Learning a Rec ...
- Heavy Cargo POJ 2263 (Floyd传递闭包)
Description Big Johnsson Trucks Inc. is a company specialized in manufacturing big trucks. Their lat ...
- LeetCode 36. Valid Sudoku (C++)
题目: Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according t ...
- 【Alpha】阶段第一次Scrum Meeting
[Alpha]阶段第一次Scrum Meeting 工作情况 团队成员 今日已完成任务 明日待完成任务 刘峻辰 后端接口开发 测试接口,修正bug 赵智源 撰写测试方案书 部署实际任务和编写测试样例 ...
- 第七次JAVA语言笔记
- CentOS7 修改分辨率
1. 修改文件: vi /boot/grub2/grub.cfg 2. 在linux16 开头的哪一行 增加 vga=0x341 修改为1024x768 3. 重启..
- javascript之彻底理解valueOf, toString
参与运算的都是简单类型(一般就字符串和数字), 复杂类型是不参与运算的. ***当对象(非简单类型)用作键时,会先调用toString()方法把对象转化成字符串 var a = {}, b = ...