ijg库(http://www.ijg.org/)是用于处理jpeg解码和压缩的库,最新版本为2014发布的版本,可以在官网中下载jpegsr9a.zip
使用vs中个nmake 进行编译,对于这个版本的库,在编译的时候需要注意这几个点:
1. 可以在cmd中使用命令进行编译(前提是,将 nmake的路径配置到环境变量中path下了)形如:
设置三个变量:
变量名 变量值
include D:\Program Files\Microsoft Visual Studio 10.0\VC\include\
lib D:\Program Files\Microsoft Visual Studio 10.0\VC\lib\
path D:\Program Files\Microsoft Visual Studio 10.0\VC\bin\
设置好这些变量之后,nmake就可以在cmd中使用了. 进入到ijg源码文件夹中,然后运行
输入 nmake -f makefile.vc setup-v10 编译,
(1)一般情况下,这个版本都会出现一个“无法找到文件 win32.mak”,将
#include<win32.mak>注释掉就可以了(这个注释,!include<win32.mak>)
(2)再次输入上述的命令,会出现ren jconfig.vc jconfig.h 无法找到 返回0x01的情况
这时候,新建一个jconfig.h文件,将jconfig.txt中的文件原封不动的移入到这个新建的文件中即可.
(3)再次运行上述的命令,便可以成功了!
这样就可以生成windows下的vcproject工程文件了,然后使用vs打开jpeg.pxxxx 即可运行生成静态库jpeg.lib,然后取出文件中的 这几个三个头文件
jconfig.h, jpeglib.h,jmorecfg.h 和jpeg.lib就可以了.
#progma comment("lib","jpeg.lib") //使用这条宏引入静态库即可使用:
下面是一个例子:
1 #include<Windows.h>
2 #include <stdio.h>
3 #include "jpeglib.h"
4 #include <setjmp.h>
5
6
7 //struct picture{
8 // JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
9 // int image_height; /* Number of rows in image */
10 // int image_width; /* Number of columns in image */
11 // int quality;
12 // char *destpath;
13 //
14 //};
15
16 JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
17 int image_height; /* Number of rows in image */
18 int image_width; /* Number of columns in image */
19 int quality;
20 char *destpath;
21
22 struct my_error_mgr {
23 struct jpeg_error_mgr pub; /* "public" fields */
24
25 jmp_buf setjmp_buffer; /* for return to caller */
26 };
27
28 typedef struct my_error_mgr * my_error_ptr;
29
30
31 METHODDEF(void)
32 my_error_exit (j_common_ptr cinfo)
33 {
34 my_error_ptr myerr = (my_error_ptr) cinfo->err;
35 (*cinfo->err->output_message) (cinfo);
36 longjmp(myerr->setjmp_buffer, 1);
37 }
38
39
40 GLOBAL(int)
41 read_JPEG_file (char * filename)
42 {
43
44 struct jpeg_decompress_struct cinfo;
45 struct my_error_mgr jerr;
46 FILE * infile; /* source file */
47 JSAMPARRAY buffer; /* Output row buffer */
48 int row_stride; /* physical row width in output buffer */
49
50 if ((infile = fopen(filename, "rb")) == NULL) {
51 fprintf(stderr, "can't open %s\n", filename);
52 return 0;
53 }
54 cinfo.err = jpeg_std_error(&jerr.pub);
55 jerr.pub.error_exit = my_error_exit;
56 if (setjmp(jerr.setjmp_buffer)) {
57 jpeg_destroy_decompress(&cinfo);
58 fclose(infile);
59 return 0;
60 }
61 jpeg_create_decompress(&cinfo);
62 jpeg_stdio_src(&cinfo, infile);
63 (void) jpeg_read_header(&cinfo, TRUE);
64 (void) jpeg_start_decompress(&cinfo);
65 row_stride = cinfo.output_width * cinfo.output_components;
66 buffer = (*cinfo.mem->alloc_sarray)
67 ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
68
69
70
71 //write
72 struct jpeg_compress_struct wcinfo;
73 struct jpeg_error_mgr wjerr;
74 /* More stuff */
75 FILE * outfile; /* target file */
76 JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
77 int wrow_stride; /* physical row width in image buffer */
78
79 wcinfo.err = jpeg_std_error(&wjerr);
80 jpeg_create_compress(&wcinfo);
81 if ((outfile = fopen(destpath, "wb")) == NULL) {
82 fprintf(stderr, "can't open %s\n", destpath);
83 // exit(1);
84 }
85 jpeg_stdio_dest(&wcinfo, outfile);
86 wcinfo.image_width = image_width>cinfo.image_width?cinfo.image_width:image_width; /* image width and height, in pixels */
87 wcinfo.image_height = image_height>cinfo.image_height?cinfo.image_height:image_height;
88 wcinfo.input_components = 3; /* # of color components per pixel */
89 wcinfo.in_color_space = JCS_RGB; /* colorspace of input image */
90 jpeg_set_defaults(&wcinfo);
91 jpeg_set_quality(&wcinfo, quality, TRUE /* limit to baseline-JPEG values */);
92 jpeg_start_compress(&wcinfo, TRUE);
93 wrow_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
94
95
96
97 int cmod=cinfo.image_height/wcinfo.image_height;
98
99 while (cinfo.output_scanline < cinfo.output_height) {
100 (void) jpeg_read_scanlines(&cinfo, buffer, 1);
101 if(cinfo.output_scanline%cmod==0)
102 (void) jpeg_write_scanlines(&wcinfo,buffer, 1);
103 }
104
105
106 (void) jpeg_finish_decompress(&cinfo);
107 jpeg_destroy_decompress(&cinfo);
108 fclose(infile);
109
110
111 jpeg_finish_compress(&wcinfo);
112 jpeg_destroy_compress(&wcinfo);
113 fclose(outfile);
114 return 1;
115 }
116
117 int main(int argc, char * argd[]){
118
119 destpath="D:\\jpeg_123.jpg";
120 image_height =100;
121 image_width=128;
122 quality=100;
123 read_JPEG_file("D:\\123.jpg");
124 // write_JPEG_file("D:\\jpeg_123.jpg",100);
125 system("pause");
126 return 0;
127 }