1 | // |
---|
2 | // "$Id$" |
---|
3 | // |
---|
4 | // Fl_JPEG_Image routines. |
---|
5 | // |
---|
6 | // Copyright 1997-2005 by Easy Software Products. |
---|
7 | // Image support donated by Matthias Melcher, Copyright 2000. |
---|
8 | // |
---|
9 | // This library is free software; you can redistribute it and/or |
---|
10 | // modify it under the terms of the GNU Library General Public |
---|
11 | // License as published by the Free Software Foundation; either |
---|
12 | // version 2 of the License, or (at your option) any later version. |
---|
13 | // |
---|
14 | // This library is distributed in the hope that it will be useful, |
---|
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | // Library General Public License for more details. |
---|
18 | // |
---|
19 | // You should have received a copy of the GNU Library General Public |
---|
20 | // License along with this library; if not, write to the Free Software |
---|
21 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
22 | // USA. |
---|
23 | // |
---|
24 | // Please report all bugs and problems on the following page: |
---|
25 | // |
---|
26 | // http://www.fltk.org/str.php |
---|
27 | // |
---|
28 | // Contents: |
---|
29 | // |
---|
30 | // Fl_JPEG_Image::Fl_JPEG_Image() - Load a JPEG image file. |
---|
31 | // |
---|
32 | |
---|
33 | // |
---|
34 | // Include necessary header files... |
---|
35 | // |
---|
36 | |
---|
37 | #include <FL/Fl_JPEG_Image.H> |
---|
38 | #include <config.h> |
---|
39 | #include <stdio.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <setjmp.h> |
---|
42 | |
---|
43 | |
---|
44 | // Some releases of the Cygwin JPEG libraries don't have a correctly |
---|
45 | // updated header file for the INT32 data type; the following define |
---|
46 | // from Shane Hill seems to be a usable workaround... |
---|
47 | |
---|
48 | #if defined(WIN32) && defined(__CYGWIN__) |
---|
49 | # define XMD_H |
---|
50 | #endif // WIN32 && __CYGWIN__ |
---|
51 | |
---|
52 | |
---|
53 | extern "C" |
---|
54 | { |
---|
55 | #ifdef HAVE_LIBJPEG |
---|
56 | # include <jpeglib.h> |
---|
57 | #endif // HAVE_LIBJPEG |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | // |
---|
62 | // Custom JPEG error handling structure... |
---|
63 | // |
---|
64 | |
---|
65 | #ifdef HAVE_LIBJPEG |
---|
66 | struct fl_jpeg_error_mgr { |
---|
67 | jpeg_error_mgr pub_; // Destination manager... |
---|
68 | jmp_buf errhand_; // Error handler |
---|
69 | }; |
---|
70 | #endif // HAVE_LIBJPEG |
---|
71 | |
---|
72 | |
---|
73 | // |
---|
74 | // Error handler for JPEG files... |
---|
75 | // |
---|
76 | |
---|
77 | #ifdef HAVE_LIBJPEG |
---|
78 | extern "C" { |
---|
79 | static void |
---|
80 | fl_jpeg_error_handler(j_common_ptr dinfo) { // I - Decompressor info |
---|
81 | longjmp(((fl_jpeg_error_mgr *)(dinfo->err))->errhand_, 1); |
---|
82 | } |
---|
83 | |
---|
84 | static void |
---|
85 | fl_jpeg_output_handler(j_common_ptr) { // I - Decompressor info (not used) |
---|
86 | } |
---|
87 | } |
---|
88 | #endif // HAVE_LIBJPEG |
---|
89 | |
---|
90 | |
---|
91 | // |
---|
92 | // 'Fl_JPEG_Image::Fl_JPEG_Image()' - Load a JPEG image file. |
---|
93 | // |
---|
94 | |
---|
95 | Fl_JPEG_Image::Fl_JPEG_Image(const char *jpeg) // I - File to load |
---|
96 | : Fl_RGB_Image(0,0,0) { |
---|
97 | #ifdef HAVE_LIBJPEG |
---|
98 | FILE *fp; // File pointer |
---|
99 | jpeg_decompress_struct dinfo; // Decompressor info |
---|
100 | fl_jpeg_error_mgr jerr; // Error handler info |
---|
101 | JSAMPROW row; // Sample row pointer |
---|
102 | |
---|
103 | // the following variables are pointers allocating some private space that |
---|
104 | // is not reset by 'setjmp()' |
---|
105 | char* max_finish_decompress_err; // count errors and give up afer a while |
---|
106 | char* max_destroy_decompress_err; // to avoid recusion and deadlock |
---|
107 | |
---|
108 | // Clear data... |
---|
109 | alloc_array = 0; |
---|
110 | array = (uchar *)0; |
---|
111 | |
---|
112 | // Open the image file... |
---|
113 | if ((fp = fopen(jpeg, "rb")) == NULL) return; |
---|
114 | |
---|
115 | // Setup the decompressor info and read the header... |
---|
116 | dinfo.err = jpeg_std_error((jpeg_error_mgr *)&jerr); |
---|
117 | jerr.pub_.error_exit = fl_jpeg_error_handler; |
---|
118 | jerr.pub_.output_message = fl_jpeg_output_handler; |
---|
119 | |
---|
120 | // Setup error loop variables |
---|
121 | max_finish_decompress_err = (char*)malloc(1); // allocate space on the frame for error counters |
---|
122 | max_destroy_decompress_err = (char*)malloc(1); // otherwise, the variables are reset on the longjmp |
---|
123 | *max_finish_decompress_err=10; |
---|
124 | *max_destroy_decompress_err=10; |
---|
125 | |
---|
126 | if (setjmp(jerr.errhand_)) |
---|
127 | { |
---|
128 | // JPEG error handling... |
---|
129 | // if any of the cleanup routines hits another error, we would end up |
---|
130 | // in a loop. So instead, we decrement max_err for some upper cleanup limit. |
---|
131 | if ( ((*max_finish_decompress_err)-- > 0) && array) |
---|
132 | jpeg_finish_decompress(&dinfo); |
---|
133 | if ( (*max_destroy_decompress_err)-- > 0) |
---|
134 | jpeg_destroy_decompress(&dinfo); |
---|
135 | |
---|
136 | fclose(fp); |
---|
137 | |
---|
138 | w(0); |
---|
139 | h(0); |
---|
140 | d(0); |
---|
141 | |
---|
142 | if (array) { |
---|
143 | delete[] (uchar *)array; |
---|
144 | array = 0; |
---|
145 | alloc_array = 0; |
---|
146 | } |
---|
147 | |
---|
148 | free(max_destroy_decompress_err); |
---|
149 | free(max_finish_decompress_err); |
---|
150 | |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | jpeg_create_decompress(&dinfo); |
---|
155 | jpeg_stdio_src(&dinfo, fp); |
---|
156 | jpeg_read_header(&dinfo, 1); |
---|
157 | |
---|
158 | dinfo.quantize_colors = (boolean)FALSE; |
---|
159 | dinfo.out_color_space = JCS_RGB; |
---|
160 | dinfo.out_color_components = 3; |
---|
161 | dinfo.output_components = 3; |
---|
162 | |
---|
163 | jpeg_calc_output_dimensions(&dinfo); |
---|
164 | |
---|
165 | w(dinfo.output_width); |
---|
166 | h(dinfo.output_height); |
---|
167 | d(dinfo.output_components); |
---|
168 | |
---|
169 | array = new uchar[w() * h() * d()]; |
---|
170 | alloc_array = 1; |
---|
171 | |
---|
172 | jpeg_start_decompress(&dinfo); |
---|
173 | |
---|
174 | while (dinfo.output_scanline < dinfo.output_height) { |
---|
175 | row = (JSAMPROW)(array + |
---|
176 | dinfo.output_scanline * dinfo.output_width * |
---|
177 | dinfo.output_components); |
---|
178 | jpeg_read_scanlines(&dinfo, &row, (JDIMENSION)1); |
---|
179 | } |
---|
180 | |
---|
181 | jpeg_finish_decompress(&dinfo); |
---|
182 | jpeg_destroy_decompress(&dinfo); |
---|
183 | |
---|
184 | free(max_destroy_decompress_err); |
---|
185 | free(max_finish_decompress_err); |
---|
186 | |
---|
187 | fclose(fp); |
---|
188 | #endif // HAVE_LIBJPEG |
---|
189 | } |
---|
190 | |
---|
191 | // |
---|
192 | // End of "$Id$". |
---|
193 | // |
---|