// 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. Unity学习笔记(2): 如何使物体消失

    Unity使物体消失的三种方法 GameObject.Destroy(gameobjcet); //在下一帧破坏物体 gameObject.SetActive(false); //Unity2017新 ...

  2. Siki_Unity_2-7_Stealth秘密行动

    Unity 2-7 Stealth秘密行动 Abstract:向量运算:Animation动画:Navigation寻路系统:Mecanim动画系统 任务1&2&3:游戏介绍 & ...

  3. OpenCV学习笔记——疑问

    vec3b:表示每一个Vec3b对象中,可以存储3个char(字符型)数据,比如可以用这样的对象,去存储RGB图像中的一个像素点.typedef Vec<uchar, 3> Vec3b; ...

  4. mininet实验 脚本实现控制交换机行为

    写在前面 本文参考 通过这个实验,我学习到了另一种下流表的方式. 下流表有两种方式(我目前了解): 通过controller下发. 通过OvS提供的API直接向OvS交换机下流表. 本实验脚本已经把相 ...

  5. hihocoder #1032 : 最长回文子串 Manacher算法

    题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...

  6. 【技术向】rainmeter的设计与发现

    我们在大学期间所学的那点代码知识还远远不够,于是我就自己寻找到了一款简单易懂的软件,来丰富我的代码知识. 这款软件叫rainmeter,中文叫做雨滴,是一款可以修改桌面的软件.它可以将桌面上更改出硬盘 ...

  7. java 队列的使用(转载)

    转载声明:http://blog.csdn.net/lzy_lizhiyang/article/details/48311925 先我们要知道使用队列的目的是什么?一般情况下,如果是一些及时消息的处理 ...

  8. shader language学习(1)——shader language简介背景

    shader language,称为着色语言,shade在英语是阴影.颜色深浅的意思.shader language基于物体本身属性和光照条件,计算美格橡塑的颜色值. 实际上这种解释具有明显的时代局限 ...

  9. 【Leetcode】179. Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  10. 超强汇总!110 道 Python 面试笔试题

    https://mp.weixin.qq.com/s/hDQrimihoaHSbrtjLybZLA 今天给大家分享了110道面试题,其中大部分是巩固基本python知识点,希望刚刚入手python,对 ...