免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10239 | 回复: 4
打印 上一主题 下一主题

如何将BMP转成JPG [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-10-19 18:20 |只看该作者 |倒序浏览
操作系统:RH9.0
  已经知道RGB的值,可以保存成BMP,但是怎么转成JPG的呢? 那位兄弟知道?万分感谢,急用啊!

论坛徽章:
0
2 [报告]
发表于 2004-10-19 21:32 |只看该作者

如何将BMP转成JPG

已经搞定了,看了libjpeg的源代码
使用libjpeg的静态库,编译的时候增加 -ljpeg
/*
*filename -- jpeg filename
*bits -------- the value of RGB
*width ------jpeg width
*height -----jpeg height
*depth------color components per pixel
*/

  1. #include <jpeglib.h>;

  2. int savejpeg(char *filename, unsigned char *bits, int width, int height, int depth)
  3. {
  4.         struct jpeg_compress_struct cinfo;
  5.         struct jpeg_error_mgr jerr;
  6.         FILE * outfile;                 /* target file */
  7.         JSAMPROW row_pointer[1];        /* pointer to JSAMPLE row[s] */
  8.         int     row_stride;             /* physical row width in image buffer */

  9.         cinfo.err = jpeg_std_error(&jerr);
  10. /* Now we can initialize the JPEG compression object. */
  11.         jpeg_create_compress(&cinfo);

  12.         if ((outfile = fopen(filename, "wb")) == NULL) {
  13.                 fprintf(stderr, "can't open %s\n", filename);
  14.                 return -1;
  15.         }
  16.         jpeg_stdio_dest(&cinfo, outfile);

  17.         cinfo.image_width = width;      /* image width and height, in pixels */
  18.         cinfo.image_height = height;
  19.         cinfo.input_components = depth;         /* # of color components per pixel */
  20.         cinfo.in_color_space = JCS_RGB;         /* colorspace of input image */

  21.         jpeg_set_defaults(&cinfo);
  22.         /* Now you can set any non-default parameters you wish to.
  23.          * Here we just illustrate the use of quality (quantization table) scaling:
  24.          */
  25.         jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE /* limit to baseline-JPEG values */);

  26.         jpeg_start_compress(&cinfo, TRUE);

  27.         row_stride = width * 3; /* JSAMPLEs per row in image_buffer */

  28.         while (cinfo.next_scanline < cinfo.image_height) {
  29.                 row_pointer[0] = & bits[cinfo.next_scanline * row_stride];
  30.                 (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  31.         }

  32.         jpeg_finish_compress(&cinfo);
  33.         fclose(outfile);

  34.         jpeg_destroy_compress(&cinfo);
  35.         return 0;
  36. }
复制代码

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2007-11-22 11:40 |只看该作者
正在学习这个方面的知识,这篇文章太好了。
choc 该用户已被删除
4 [报告]
发表于 2007-11-23 12:41 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2007-11-23 17:51 |只看该作者

回复 #4 choc 的帖子

是不是大端法和小端法的问题?

已经曾经做过一个ARM平台下的项目,图像显示的时候,好像字节序要调整。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP