1 //bmp2jpg.cpp : 定义控制台应用程序的入口点。2 //3 #include "stdafx.h"

4 #include "jpeglib.h"

5 #include "stdlib.h"

6 #pragma comment(lib,"libjpeg.lib")

7 #pragma pack(4) //两字节对齐,否则bmp_fileheader会占16Byte

8

9 /*图像bmp格式文件头结构*/

10 structbmp_fileheader11 {12 unsigned short bfType; //若不对齐,这个会占4Byte

13 unsigned longbfSize;14 unsigned shortbfReverved1;15 unsigned shortbfReverved2;16 unsigned longbfOffBits;17 };18

19 /*图像bmp格式信息头结构*/

20 structbmp_infoheader21 {22 unsigned longbiSize;23 unsigned longbiWidth;24 unsigned longbiHeight;25 unsigned shortbiPlanes;26 unsigned shortbiBitCount;27 unsigned longbiCompression;28 unsigned longbiSizeImage;29 unsigned longbiXPelsPerMeter;30 unsigned longbiYpelsPerMeter;31 unsigned longbiClrUsed;32 unsigned longbiClrImportant;33 };34

35 FILE *input_file ; //输入bmp文件指针

36 FILE *output_file; //输出jpg文件指针

37 bmp_fileheader bmpFh;38 bmp_infoheader bmpIh;39

40 unsigned char *src_buffer;41 unsigned char *dst_buffer;42

43 /*读取图像bmp文件头结构*/

44 voidreadBmpHeader()45 {46 /*读取输入bmp格式图像文件头*/

47 fread(&bmpFh , sizeof(bmp_fileheader) , 1, input_file);48

49 /*读取输入bmp格式图像信息头*/

50 fread(&bmpIh , sizeof(bmp_infoheader), 1, input_file);51 }52

53 /*读取图像bmp数据*/

54 voidreadBmpData()55 {56 fseek(input_file , bmpFh.bfOffBits,SEEK_SET);57 src_buffer = (unsigned char *)malloc(sizeof(unsigned char)*bmpIh.biWidth*bmpIh.biHeight*bmpIh.biBitCount/8);58 if(NULL ==src_buffer)59 {60 printf("malloc memory failed!\n");61 return;62 }63

64 fread(src_buffer , sizeof(unsigned char)*bmpIh.biWidth*bmpIh.biHeight*bmpIh.biBitCount/8,1, input_file);65

66 /*将bmp数据读取到目标huffbuffer*/

67 unsigned long width =bmpIh.biWidth ;68 unsigned long height=bmpIh.biHeight ;69 unsigned short depth= bmpIh.biBitCount/8;70 unsigned char *src_point ;71 unsigned char *dst_point ;72 unsigned long i = 0;73 unsigned long j = 0;74 unsigned long rgb_index = 0;75

76 dst_buffer = (unsigned char *)malloc(sizeof(unsigned char)*width*height*depth);77 if(NULL ==dst_buffer)78 {79 printf("malloc memory failed!\n");80 return;81 }82

83 src_point = src_buffer + width*depth*(height-1);84 dst_point = dst_buffer + width*depth*(height-1);85

86 for(i = 0 ; i < height ; i++)87 {88 for(j = 0 ; j < width*depth ; j+=depth)89 {90 if(depth == 1)91 {92 dst_point[j] =src_point[j];93 }94

95 if(depth == 3)96 {97 dst_point[j+2]=src_point[j+0];98

99 dst_point[j+1]=src_point[j+1];100

101 dst_point[j+0]=src_point[j+2];102 }103

104 }105

106 dst_point -= width*depth ;107 src_point -= width*depth ;108 }109 }110

111 voidsynthese_jpeg()112 {113 structjpeg_compress_struct cinfo;114 structjpeg_error_mgr jerr;115 JSAMPARRAY buffer;116

117 unsigned long width=bmpIh.biWidth;118 unsigned long height=bmpIh.biHeight;119 unsigned short depth=bmpIh.biBitCount/8;120 unsigned char *point;121 cinfo.err=jpeg_std_error(&jerr); //libjpeg各种配置

122 jpeg_create_compress(&cinfo);123 jpeg_stdio_dest(&cinfo,output_file);124 cinfo.image_width=width;125 cinfo.image_height=height;126 cinfo.input_components=depth;127 if (depth==1)128 cinfo.in_color_space=JCS_GRAYSCALE;129 else

130 cinfo.in_color_space=JCS_RGB;131

132 jpeg_set_defaults(&cinfo);133 jpeg_set_quality(&cinfo,20,TRUE); //中间的值为压缩质量,越大质量越好

134 jpeg_start_compress(&cinfo,TRUE);135

136 buffer=(*cinfo.mem->alloc_sarray)137 ((j_common_ptr)&cinfo,JPOOL_IMAGE,width*depth,1);138 point=dst_buffer+width*depth*(height-1);139 while (cinfo.next_scanline

146 jpeg_finish_compress(&cinfo);147 jpeg_destroy_compress(&cinfo);148 }149

150 intmain()151 {152 input_file=fopen("E:/test_pic.bmp","rb");153 if(NULL ==input_file)154 {155 printf("open input file failed!\n");156 }157 output_file=fopen("E:/save_pic.jpg","wb");158 if(NULL ==output_file)159 {160 printf("open output file failed!\n");161 }162

163 readBmpHeader();164 readBmpData();165 synthese_jpeg();166 fclose(input_file);167 fclose(output_file);168

169 free(src_buffer);170 free(dst_buffer);171

172 return 0;173 }

用c语言把图像转成jpg格式,图像格式转换之BMP格式转换为JPG格式(示例代码)相关推荐

  1. python将音频转换成文字_用Python将音频内容转换为文本格式,方言可以吗?

    当对一个或多个人的谈话进行记录时,采用一种高度准确和自动化的方式将口语提取为文本非常有用.转换成文字后,便可以将其用于进一步分析或用作其他功能. 在本教程中,我们将使用称为AssemblyAI(htt ...

  2. android怎么用代码调图像,浅谈android中图片处理之图形变换特效Matrix(四)(示例代码)...

    今天,我们就来谈下android中图片的变形的特效,在上讲博客中我们谈到android中图片中的色彩特效来实现的.改变它的颜色主要通过ColorMatrix类来实现. 现在今天所讲的图片变形的特效主要 ...

  3. c语言中文件的只读属性,C/C++ 文件属性设置(隐藏、只读、加密等)(示例代码)

    GetFileAttributes 获取文件属性函数的返回值 返回字段 返回值 属性类型 FILE_ATTRIBUTE_READONLY 1 只读 FILE_ATTRIBUTE_HIDDEN 2 隐藏 ...

  4. 锦标赛算法c语言,多目标遗传算法 ------ NSGA-II (部分源码解析) 交叉操作 crossover.c(示例代码)...

    遗传算法中的交叉操作是 对NSGA-II  源码分析的  最后一部分, 这一部分也是我 从读该算法源代码和看该算法论文理解偏差最大的  函数模块. 这里,首先提一下,遗传算法的  交叉操作.变异操作都 ...

  5. 下载的音乐如何转成MP3?分享两种将音频转换为MP3格式的方法

    如果您下载了一些音频文件,但是它们不是MP3格式,那么您可能需要将它们转换为MP3格式,以便在各种设备上播放.以下是两种将音频转换为MP3格式的方法. 方法一:使用免费的在线转换工具 有许多免费的在线 ...

  6. msg文件转成html文件,如何将MSG格式的文件转换为PDF格式文件?

    三.如何将MSG格式的文件转换为PDF格式的文件? 想要随时查看MSG格式文件的话,格式转换就成为了理想的途径,下面将教大家将MSG格式的文件转换为PDF格式文件三种方式,一起去学习一下吧. 1.借助 ...

  7. php实现视频转gif,mp4格式如何转换成gif格式

    mp4格式转换成gif格式的方法:首先打开视频转换器,并把需要制作的mp4视频格式添加到软件中:然后点击"输出路径"按钮,设置视频转gif后存储到电脑的路径:接着点击截取新片段按钮 ...

  8. Arduino与ROS通讯的示例代码HelloWorld改写成夏普红外测距传感器数据传输的代码

    改写详情: 其实就是在原来的代码的基础上加了个红外测距传感器的代码,然后把红外测距传感器的double类型的数据用dtostrf函数转换成了char类型的字符串数组.并没有对消息类型进行任何的更改,仅 ...

  9. 图像常见格式及转换(BGR,YUV,NV12,YUV444)

    常见格式 RGB RGB 是最常用于显示器的色彩空间,R(red)是红色通道,G(green)是绿色,B(blue)是蓝色通道.这三种颜色以不同的量进行叠加,就可以显示出五彩缤纷的色彩.RGB 格式里 ...

  10. c语言中实现自动平移,c语言实现图像的旋转与平移

    y y y (4) ty≥height,图象完全移出了屏幕,不用做任何处理. 这种做法利用了位图存储的连续性,即同一行的象素在内存中是相邻的.利用 memcpy函数, 从(x ,y )-x )处,拷 ...

最新文章

  1. python的多行语句可以使用反斜杠_python 为什么不用分号作终止符?
  2. anaconda安装好tensorflow后,无法在jupyter notebook上使用的解决方法
  3. xxx.pch(No such file or directory)
  4. javascript 网页运行代码效果
  5. linux下多线程断点下载工具-axel
  6. tcp程序——回声客户端
  7. 360极速浏览器兼容模式怎么设置在哪
  8. inventor牙距_滚子链计算基础知识 | Inventor 2018 | Autodesk Knowledge Network
  9. SpringMVC快速上手教程及SSM整合案例
  10. 输入两个正整数,求其最大公约数和最小公倍数
  11. HTML文本格式化标签(用来调整文本的格式和排版)
  12. c语言rc,RC低通滤波器中R和C参数选择
  13. php网页设计导航栏代码,总结7种常见的导航条制作实例
  14. 微信公众号申请最新流程
  15. 知识体系结构---备份
  16. anaconda、labelme标注软件安装和使用
  17. 参考文献格式要首行缩进吗_参考文献第二行缩进论文范文 有关毕业论文格式明毕业论文写作资料...
  18. 学废了系列 - WebGIS vs WebGL图形编程
  19. Java和C++基本类型与语法的区别
  20. 建表报错 primary key and distributed by definition incompatible

热门文章

  1. 纯css实现简单的页面视差滚动
  2. mysql聚簇索引abc_索引优化_MySQL开发教程_IT技术个人博客
  3. GDAL(Geospatial Data Abstraction Library )简介
  4. 新买的笔记本电脑分区,笔记本分区分错了怎么重新分
  5. Windows事件ID及解释大全
  6. [转] Redefining the shading languages ecosystem with SPIR-V
  7. C++ float转换int
  8. 阻止事件冒泡和浏览器默认事件
  9. eclipse 自动生成注释 @author
  10. AI 科研入坑指南—中国人民大学赵鑫