(原)调用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 Files\Microsoft SDKs\Windows\v6.0A\Include\win32.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的程序:
void GeneJpegFile(const char* jpegFileName, unsigned char* inputData,
int nWidth, int nHeight, int nChannel, int nQuality)
{
/* This struct contains the JPEG compression parameters and pointers to
* working space (which is allocated as needed by the JPEG library).
* It is possible to have several such structures, representing multiple
* compression/decompression processes, in existence at once. We refer
* to any one struct (and its associated working data) as a "JPEG object".
*/
struct jpeg_compress_struct cinfo; /* This struct represents a JPEG error handler. It is declared separately
* because applications often want to supply a specialized error handler
* (see the second half of this file for an example). But here we just
* take the easy way out and use the standard error handler, which will
* print a message on stderr and call exit() if compression fails.
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
struct jpeg_error_mgr jerr; /* More stuff */
FILE *outfile; /* target file */
JSAMPROW row_pointer[]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */ /* Step 1: allocate and initialize JPEG compression object */ /* We have to set up the error handler first, in case the initialization
* step fails. (Unlikely, but it could happen if you are out of memory.)
* This routine fills in the contents of struct jerr, and returns jerr's
* address which we place into the link field in cinfo.
*/
cinfo.err = jpeg_std_error(&jerr); /* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo); /* Now we can initialize the JPEG compression object. */ /* Step 2: specify data destination (eg, a file) */
/* Note: steps 2 and 3 can be done in either order. */ /* Here we use the library-supplied code to send compressed data to a
* stdio stream. You can also write your own code to do something else.
* VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
* requires it in order to write binary files.
*/
if ((outfile = fopen(jpegFileName, "wb")) == NULL)
{
fprintf(stderr, "can't open %s\n", jpegFileName);
return;
}
jpeg_stdio_dest(&cinfo, outfile); /* Step 3: set parameters for compression */ /* First we supply a description of the input image.
* Four fields of the cinfo struct must be filled in:
*/
cinfo.image_width = nWidth; /* image width and height, in pixels */
cinfo.image_height = nHeight;
cinfo.input_components = nChannel; /* # of color components per pixel */ if (nChannel == )
{
cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */
}
else if (nChannel == )
{
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
} /* Now use the library's routine to set default compression parameters.
* (You must set at least cinfo.in_color_space before calling this,
* since the defaults depend on the source color space.)
*/
jpeg_set_defaults(&cinfo); // Now you can set any non-default parameters you wish to.
// Here we just illustrate the use of quality (quantization table) scaling:
jpeg_set_quality(&cinfo, nQuality, TRUE); /* limit to baseline-JPEG values */ /* Step 4: Start compressor */ /* TRUE ensures that we will write a complete interchange-JPEG file.
* Pass TRUE unless you are very sure of what you're doing.
*/
jpeg_start_compress(&cinfo, TRUE); /* Step 5: while (scan lines remain to be written) */
/* jpeg_write_scanlines(...); */ /* Here we use the library's state variable cinfo.next_scanline as the
* loop counter, so that we don't have to keep track ourselves.
* To keep things simple, we pass one scanline per call; you can pass
* more if you wish, though.
*/
row_stride = nWidth * nChannel; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height)
{
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_pointer[] = &inputData[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, );
} /* Step 6: Finish compression */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo); /* After finish_compress, we can close the output file. */
fclose(outfile);
}
说明:
1. 灰度图像的话,缓冲区大小就是width*height.
2. RGB图像的话,缓冲区大小是width*height*3,同时,像素排列顺序是RGB RGB RGB RGB…
3. 其他格式的话,cinfo.in_color_space需要改成相应的格式,且row_stride的值估计也需要修改,暂时没有考虑。
(原)调用jpeglib对图像进行压缩的更多相关文章
- 使用方向变换(directional transform)图像分块压缩感知
论文的思路是先介绍分块压缩感知BCS,然后介绍使用投影和硬阈值方法的迭代投影方法PL,接着将PL与维纳滤波器结合形成SPL(平滑PL),并且介绍了稀疏表示的几种基,提出了两种效果较好的稀疏基:CT与D ...
- C#调用7z实现文件的压缩与解压
1.关于7z 首先在这里先介绍一下7z压缩软件,7z是一种主流的 压缩格式,它拥有极高的压缩比.在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式.主要有以下特点: 来源且模块化的组 ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- Android 调用jepg库进行图片压缩,保持图片不失真
1. 浅谈为什么Android和iOS图片质量差距那么大? 首先来说,作为一个安卓狗,机器当然用的是安卓的手机.现在的安卓手机大多数都会以高清拍照,动不动就几千万柔光相机来吸引各种买家.买来后,拍照发 ...
- 图像jpeg压缩
图像分割 8X8 颜色空间转换RGB->YCbCr 3个8X8的矩阵 离散余弦变换:(Discrete cosine transform),简称DCT. DCT转换后的数组中第一个是一个直线数据 ...
- C++调用Matlab引擎 图像读写与处理 (知识+代码篇)
准备知识 之 Matlab Engine 执行命令 /* Execute matlab statement */ int engEvalString(Engine* ep, const char* s ...
- C# - WinFrm应用程序调用SharpZipLib实现文件的压缩和解压缩
前言 本篇主要记录:VS2019 WinFrm桌面应用程序调用SharpZipLib,实现文件的简单压缩和解压缩功能. SharpZipLib 开源地址戳这里. 准备工作 搭建WinFrm前台界面 添 ...
- swift 图像的压缩上传
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [Str ...
- C# 如何进行图像的压缩
从网上找的非常有效.图片3M到500k private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCode ...
随机推荐
- Truncate Table user
Truncate Table 百科名片 Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用. 目录 语法 参数 注释 示例 ...
- 混入模式(max-in)实现继承
混入模式并不是一种复制完整的对象,而是从多个对象中复制出任意的成员并将这些成员组合成一个新的对象. 实现如下: function mix(){ var arg,prop,child = {}; for ...
- maven项目依赖被改为文件夹时如何改回lib
如图
- Linux - Reset a MySQL root password
Use the following steps to reset a MySQL root password by using the command line interface. Stop the ...
- Tomcat 6.0.32 +Spring dbcp datasource关闭Tomcat出现严重异常
异常如下: 信息: Pausing Coyote HTTP/ -- :: org.apache.catalina.core.StandardService stop 信息: Stopping serv ...
- 关于hash
http://rapheal.iteye.com/blog/1142955 关于javascript hash
- BZOJ 1068 (区间DP)
题意:字符串的压缩,f[l][r][0]代表还没M,f[l][r][1]代表有M. #include<cstdio> #include<cmath> #include<c ...
- SIM卡基础,各管脚意义,封装定义
1. SIM简介 SIM卡(Subscriber Identity Module).即用户识别模块,是一张符合GSM规范的"智慧卡".SIM卡可以插入任何一部符合GSM规范的移动电 ...
- OpenSSl 加密解密 示例(终于有编程实践了)
OPenSSl的加密主要有三个重要的函数.看懂下面的代码就基本上知道该如何使用openssL来加密了. 不过注意,要先将libssl.so.1.0和libcrypto.so.1.0文件复制到执行的文件 ...
- 关于PowerShell中的命令的别名
cmdlets的别名,有利于使用传统的cmd的方式 或者使用 bash的方式的人员,更加方便的使用使用频率较高的命令. 以下是从 别名中获取的内置的别名. PS C:\> Get-Alias * ...