Debian Codegraph @

gutils/gimagereadjpeg.cpp

raw
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #ifdef _NO_LIBJPEG
31
32 static int a_file_must_define_something=0; /* ANSI says so */
33
34 #else /* We can build with jpeglib - therefore import jpg files */
35
36 #include "basics.h"
37 #include "gimage.h"
38
39 #include <jerror.h>
40 #include <jpeglib.h>
41 #include <setjmp.h>
42 #include <stdio.h>
43 #include <sys/types.h>
44
45 /******************************************************************************/
46
47 struct jpegState {
48 struct jpeg_decompress_struct *cinfo;
49 int state;
50 struct _GImage *base;
51 JSAMPLE *buffer;
52 int scanpos;
53 };
54
55 struct my_error_mgr {
56 struct jpeg_error_mgr pub; /* "public" fields */
57
58 jmp_buf setjmp_buffer; /* for return to caller */
59 int padding[8]; /* On my solaris box jmp_buf is the wrong size */
60 };
61
62 typedef struct my_error_mgr * my_error_ptr;
63
64 METHODDEF(void)
65 my_error_exit (j_common_ptr cinfo)
66 {
67 /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
68 my_error_ptr myerr = (my_error_ptr) cinfo->err;
69
70 /* Always display the message. */
71 /* We could postpone this until after returning, if we chose. */
72 (*cinfo->err->output_message) (cinfo);
73
74 /* Return control to the setjmp point */
75 longjmp(myerr->setjmp_buffer, 1);
76 }
77
78 /* jpeg routines use 24 bit pixels, xvt routines pad out to 32 */
79 static void transferBufferToImage(struct jpegState *js,int ypos) {
80 struct jpeg_decompress_struct *cinfo = js->cinfo;
81 JSAMPLE *pt, *end;
82 Color *ppt;
83
84 ppt = (Color *) (js->base->data+ypos*js->base->bytes_per_line);
85 for ( pt = js->buffer, end = pt+3*cinfo->image_width; pt<end; ) {
86 int r,g,b;
87 r = *(pt++); g= *(pt++); b= *(pt++);
88 *(ppt++) = COLOR_CREATE(r,g,b);
89 }
90 }
91
92 GImage *GImageRead_Jpeg(FILE *infile) {
93 GImage *ret;
94 struct _GImage *base;
95 struct jpeg_decompress_struct cinfo;
96 struct my_error_mgr jerr;
97 JSAMPLE *rows[1];
98 struct jpegState js;
99 int ypos;
100
101
102 cinfo.err = jpeg_std_error(&jerr.pub);
103 jerr.pub.error_exit = my_error_exit;
104 if (setjmp(jerr.setjmp_buffer)) {
105 jpeg_destroy_decompress(&cinfo);
106 return( NULL );
107 }
108
109 jpeg_CreateDecompress(&cinfo,JPEG_LIB_VERSION,(size_t) sizeof(struct jpeg_decompress_struct));
110 jpeg_stdio_src(&cinfo, infile);
111 (void) jpeg_read_header(&cinfo, TRUE);
112
113 if ( cinfo.jpeg_color_space == JCS_GRAYSCALE )
114 cinfo.out_color_space = JCS_RGB;
115 ret = GImageCreate(it_true,cinfo.image_width, cinfo.image_height);
116 if ( ret==NULL ) {
117 jpeg_destroy_decompress(&cinfo);
118 return( NULL );
119 }
120 base = ret->u.image;
121
122 (void) jpeg_start_decompress(&cinfo);
123 rows[0] = (JSAMPLE *) malloc(3*cinfo.image_width);
124 js.cinfo = &cinfo; js.base = base; js.buffer = rows[0];
125 while (cinfo.output_scanline < cinfo.output_height) {
126 ypos = cinfo.output_scanline;
127 (void) jpeg_read_scanlines(&cinfo, rows, 1);
128 transferBufferToImage(&js,ypos);
129 }
130
131 (void) jpeg_finish_decompress(&cinfo);
132 jpeg_destroy_decompress(&cinfo);
133 free(rows[0]);
134
135 return( ret );
136 }
137
138 GImage *GImageReadJpeg(char *filename) {
139 /* Import a jpeg image, else return NULL if error */
140 GImage *ret;
141 FILE * infile; /* source file */
142
143 if ((infile = fopen(filename, "rb")) == NULL) {
144 fprintf(stderr,"Can't open \"%s\"\n", filename);
145 return( NULL );
146 }
147
148 ret = GImageRead_Jpeg(infile);
149 fclose(infile);
150 return( ret );
151 }
152 #endif