zoukankan      html  css  js  c++  java
  • (原)调用jpeglib对图像进行压缩

    网址: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的值估计也需要修改,暂时没有考虑。

  • 相关阅读:
    C#学习笔记
    Visual Studio 快捷键
    java 8 中lambda表达式学习
    Spfa算法
    dijkstra算法
    topSort
    并查集--学习详解
    trie树--详解
    POJ1988 并查集的使用
    Mybatis的一级缓存和二级缓存
  • 原文地址:https://www.cnblogs.com/darkknightzh/p/4973828.html
Copyright © 2011-2022 走看看