(原)调用jpeglib对图像进行压缩

摘要:
参考网站:http://dev.w3.org/Amaya/libjpeg/example.chttp://www.360doc.com/content/13/0226/15/2036337_268016821.shtml1.先转到官方网站http://www.ijg.org/files/下载源代码jpegsr9a.zip。包括5.打开VS2013开发人员命令提示符。6.定位E:jpeg-9a。在命令行中输入:E:,然后输入:cdjpeg-9a7。输入“nmake-fmakefile.vc”以生成所需的libjpeg。lib函数库。

网址:http://www.cnblogs.com/darkknightzh/p/4973828.html。未经允许,严禁转载。

参考网站:

http://dev.w3.org/Amaya/libjpeg/example.c

http://www.360doc.com/content/13/0226/15/2036337_268016821.shtml

1. 首先去官网http://www.ijg.org/files/下载源码,下载的是jpegsr9a.zip。

2. 解压后放到E盘根目录,“E:jpeg-9a”下会有很多文件。

3. 将“jconfig.vc”改成“jconfig.h”

4. 将“makefile.vc”中第12行

!include <win32.mak>

改成

!include <C:Program FilesMicrosoft SDKsWindowsv6.0AIncludewin32.mak>

5. 打开vs2013的“VS2013 开发人员命令提示”

6. 定位到E:jpeg-9a。在命令行中输入:

E:

之后输入:

cd jpeg-9a

7. 输入” nmake -f makefile.vc” 生成所需要的libjpeg.lib函数库。

8. 使用时,将“jconfig.h”、“jmorecfg.h”、“jpeglib.h”、“libjpeg.lib”四个文件拷贝到对应的文件夹内。

9.  libjpeg.lib是用c语言开发的,
    如果在C++程序里使用,需要用extern "C" { }包含一下。如下:

extern "C" 
{
#include "jpeglib.h"
}

10. 在“解决方案资源管理器”中“属性页“的”连接器-输入-附加依赖项”内,增加“libjpeg.lib”

11. 从缓冲区生成jpg的程序:

  1 void GeneJpegFile(const char* jpegFileName, unsigned char* inputData,
  2     int nWidth, int nHeight, int nChannel, int nQuality)
  3 {
  4     /* This struct contains the JPEG compression parameters and pointers to
  5     * working space (which is allocated as needed by the JPEG library).
  6     * It is possible to have several such structures, representing multiple
  7     * compression/decompression processes, in existence at once.  We refer
  8     * to any one struct (and its associated working data) as a "JPEG object".
  9     */
 10     struct jpeg_compress_struct cinfo;
 11 
 12     /* This struct represents a JPEG error handler.  It is declared separately
 13     * because applications often want to supply a specialized error handler
 14     * (see the second half of this file for an example).  But here we just
 15     * take the easy way out and use the standard error handler, which will
 16     * print a message on stderr and call exit() if compression fails.
 17     * Note that this struct must live as long as the main JPEG parameter
 18     * struct, to avoid dangling-pointer problems.
 19     */
 20     struct jpeg_error_mgr jerr;
 21 
 22     /* More stuff */
 23     FILE *outfile;                  /* target file */
 24     JSAMPROW row_pointer[1];        /* pointer to JSAMPLE row[s] */
 25     int     row_stride;             /* physical row width in image buffer */
 26 
 27     /* Step 1: allocate and initialize JPEG compression object */
 28 
 29     /* We have to set up the error handler first, in case the initialization
 30     * step fails.  (Unlikely, but it could happen if you are out of memory.)
 31     * This routine fills in the contents of struct jerr, and returns jerr's
 32     * address which we place into the link field in cinfo.
 33     */
 34     cinfo.err = jpeg_std_error(&jerr);
 35 
 36     /* Now we can initialize the JPEG compression object. */
 37     jpeg_create_compress(&cinfo);  /* Now we can initialize the JPEG compression object. */
 38 
 39     /* Step 2: specify data destination (eg, a file) */
 40     /* Note: steps 2 and 3 can be done in either order. */
 41 
 42     /* Here we use the library-supplied code to send compressed data to a
 43     * stdio stream.  You can also write your own code to do something else.
 44     * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
 45     * requires it in order to write binary files.
 46     */
 47     if ((outfile = fopen(jpegFileName, "wb")) == NULL)
 48     {
 49         fprintf(stderr, "can't open %s
", jpegFileName);
 50         return;
 51     }
 52     jpeg_stdio_dest(&cinfo, outfile);
 53 
 54     /* Step 3: set parameters for compression */
 55 
 56     /* First we supply a description of the input image.
 57     * Four fields of the cinfo struct must be filled in:
 58     */
 59     cinfo.image_width = nWidth;                /* image width and height, in pixels */
 60     cinfo.image_height = nHeight;
 61     cinfo.input_components = nChannel;         /* # of color components per pixel */
 62 
 63     if (nChannel == 1)
 64     {
 65         cinfo.in_color_space = JCS_GRAYSCALE;  /* colorspace of input image */
 66     }
 67     else if (nChannel == 3)
 68     {
 69         cinfo.in_color_space = JCS_RGB;        /* colorspace of input image */
 70     }
 71     
 72     /* Now use the library's routine to set default compression parameters.
 73     * (You must set at least cinfo.in_color_space before calling this,
 74     * since the defaults depend on the source color space.)
 75     */
 76     jpeg_set_defaults(&cinfo);
 77 
 78     // Now you can set any non-default parameters you wish to.
 79     // Here we just illustrate the use of quality (quantization table) scaling:
 80     jpeg_set_quality(&cinfo, nQuality, TRUE); /* limit to baseline-JPEG values */
 81 
 82     /* Step 4: Start compressor */
 83 
 84     /* TRUE ensures that we will write a complete interchange-JPEG file.
 85     * Pass TRUE unless you are very sure of what you're doing.
 86     */
 87     jpeg_start_compress(&cinfo, TRUE);
 88 
 89     /* Step 5: while (scan lines remain to be written) */
 90     /*           jpeg_write_scanlines(...); */
 91 
 92     /* Here we use the library's state variable cinfo.next_scanline as the
 93     * loop counter, so that we don't have to keep track ourselves.
 94     * To keep things simple, we pass one scanline per call; you can pass
 95     * more if you wish, though.
 96     */
 97     row_stride = nWidth * nChannel; /* JSAMPLEs per row in image_buffer */
 98 
 99     while (cinfo.next_scanline < cinfo.image_height)
100     {
101         /* jpeg_write_scanlines expects an array of pointers to scanlines.
102         * Here the array is only one element long, but you could pass
103         * more than one scanline at a time if that's more convenient.
104         */
105         row_pointer[0] = &inputData[cinfo.next_scanline * row_stride];
106         (void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
107     }
108 
109     /* Step 6: Finish compression */
110     jpeg_finish_compress(&cinfo);
111     jpeg_destroy_compress(&cinfo);
112 
113     /* After finish_compress, we can close the output file. */
114     fclose(outfile);
115 }

说明:

1. 灰度图像的话,缓冲区大小就是width*height.

2. RGB图像的话,缓冲区大小是width*height*3,同时,像素排列顺序是RGB RGB RGB RGB

3. 其他格式的话,cinfo.in_color_space需要改成相应的格式,且row_stride的值估计也需要修改,暂时没有考虑。

免责声明:文章转载自《(原)调用jpeglib对图像进行压缩》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇var_dump()函数输出不完整,有省略号?解决办法MVVM模式下实现拖拽下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

工具软件 PYUV打开raw图片

引自:http://blog.csdn.net/lavenderss/article/details/51495648  [pYUV]如何打开YUV/RGB图片 pYUV工具本身使用起来比较简单,但如果选项设置错误,会导致图像显示失真或错误,让人误以为是图片本身的问题,这里介绍两个比较典型类型的图片打开方式,其他类型图片打开方式触类旁通即可。 1.  ...

渐进式jpeg(progressive jpeg)图片及其相关

最近看有些网站上的jpg格式的图片在呈现的时候,有两种方式,一种是自上而下扫描式的,还有一种就是先是全部的模糊图片,然后逐渐清晰(就像GIF格式的交错显示)。 一、基本JPEG(baseline jpeg)和渐进JPEG 图片的尺寸大小: 张鑫旭个人博客看到: 同一张jpg图片,如果保存为基本式和渐进式那个尺寸更小呢? 根据我拿3终不同风格图片做测试,发现...

ie浏览器不支持cmyk模式下的JPEG图片

  今天分享一个小小的技巧,就是解决颜色模式为印刷格式CMYK四色模式的JPEG图片上传到服务器上不被支持的问题。  如果你用photo shop打开该图片,细心的人就会发现JPG图像标题栏的图片名称后面紧跟着该图像的颜色模式,如果是RGB的就显示RGB,如果是四色模式CMYK的就显示CMYK。而通常网络上使用的基本都是RGB格式。况且IE6.9-ie8...

(转)了解一下,各种图片格式的区别

  在开发过程中,经常涉及到要用到图片,但是图片有很多不同的格式,他们之间有什么区别呢,我们在使用的时候又该如何选择呢?本文介绍和比较几种常见图片文件格式的优缺点,并介绍不同的文件格式对应用程序性能的影响。 有损vs无损 图片文件格式有可能会对图片的文件大小进行不同程度的压缩,图片的压缩分为有损压缩和无损压缩两种。 有损压缩:指在压缩文件大小的过程中,损...

ijg库解码超大型jpeg图片

1. ijg库解码超大型jpeg图片(>100M)的时候,如何避免内存溢出。 采用边解码边压缩的策略,每次解码一行或者若干行图片数据,然后对于这些解码的数据,进行DQT(量化处理,过滤掉高频的数据,保持低频的数据), 这样解码完,也压缩完。 2. ijg库提供给我们的压缩接口都非常单一,仅有文件流操作,也就是仅仅只有从文件(图片)中读取,然后保存到文...

Android 下的EXIF

一.什么是Exif Exif(Exchangeable Image File 可交换图像文件)是一种图象文件格式,它的数据存储与JPEG格式是完全相同的。实际上Exif格式就是在JPEG格式头部插入了数码照片的信息,包括拍 摄时的光圈、快门、白平衡、ISO、焦距、日期时间等各种和拍摄条件以及相机品牌、型号、色彩编码、拍摄时录制的声音以及全球定位系统(GPS...