author | Stefan Vogel <sv@exept.de> |
Wed, 21 Oct 2009 18:52:38 +0200 | |
changeset 2768 | 38e9b42f2af8 |
parent 2284 | cebd50e9da1c |
child 2971 | 07bd4e98036e |
permissions | -rw-r--r-- |
6 | 1 |
" |
2 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
28 | 3 |
All Rights Reserved |
6 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1558
1ae43779327f
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1094
diff
changeset
|
12 |
"{ Package: 'stx:libview2' }" |
1ae43779327f
#valueNowOrOnUnwindDo: -> #ensure:
Claus Gittinger <cg@exept.de>
parents:
1094
diff
changeset
|
13 |
|
2 | 14 |
ImageReader subclass:#JPEGReader |
537 | 15 |
instanceVariableNames:'jpeg_decompress_struct jpeg_error_mgr_struct colorComponents |
16 |
forceGrayscale forceDitherMode' |
|
576 | 17 |
classVariableNames:'ErrorPrinting' |
199 | 18 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1698
diff
changeset
|
19 |
category:'Graphics-Images-Readers' |
2 | 20 |
! |
21 |
||
522 | 22 |
!JPEGReader primitiveDefinitions! |
23 |
%{ |
|
24 |
||
25 |
/* |
|
26 |
* includes, defines, structure definitions |
|
27 |
* and typedefs come here. |
|
28 |
*/ |
|
29 |
||
528 | 30 |
#include <stdio.h> |
31 |
||
542 | 32 |
#ifdef sunos |
33 |
# include <sys/types.h> |
|
528 | 34 |
#endif |
35 |
||
542 | 36 |
#include <jpeglib.h> |
37 |
#include <jerror.h> |
|
38 |
||
534 | 39 |
struct my_error_mgr { |
40 |
struct jpeg_error_mgr pub; |
|
41 |
jmp_buf setjmp_buffer; |
|
42 |
}; |
|
43 |
||
528 | 44 |
%} |
45 |
! ! |
|
46 |
||
47 |
!JPEGReader primitiveFunctions! |
|
48 |
%{ |
|
49 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
50 |
/* |
528 | 51 |
* any local C (helper) functions |
52 |
* come here (please, define as static) |
|
53 |
*/ |
|
54 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
55 |
static |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
56 |
my_error_exit(cinfo) |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
57 |
j_common_ptr cinfo; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
58 |
{ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
59 |
struct my_error_mgr *myerrPtr = (struct my_error_mgr *) cinfo->err; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
60 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
61 |
if (@global(ErrorPrinting) == true) { |
2283 | 62 |
console_fprintf(stderr, "JPEGReader [warning]: jpeg error\n"); |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
63 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
64 |
longjmp(myerrPtr->setjmp_buffer, 1); |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
65 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
66 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
67 |
static |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
68 |
my_output_message(cinfo) |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
69 |
j_common_ptr cinfo; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
70 |
{ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
71 |
char buffer[JMSG_LENGTH_MAX]; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
72 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
73 |
if (@global(ErrorPrinting) == true) { |
2283 | 74 |
console_fprintf(stderr, "libJPEG [error]: "); |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
75 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
76 |
/* Create the message */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
77 |
(*cinfo->err->format_message) (cinfo, buffer); |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
78 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
79 |
/* Send it to stderr, adding a newline */ |
2283 | 80 |
console_fprintf(stderr, "%s\n", buffer); |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
81 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
82 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
83 |
|
528 | 84 |
/* |
85 |
* Optional progress monitor: display a percent-done figure on stderr. |
|
86 |
*/ |
|
87 |
||
88 |
#ifdef PROGRESS_REPORT |
|
89 |
||
90 |
void |
|
91 |
JPG_progress_monitor (j_common_ptr cinfo) |
|
92 |
{ |
|
93 |
cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress; |
|
94 |
int total_passes = prog->pub.total_passes + prog->total_extra_passes; |
|
95 |
int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit); |
|
96 |
||
97 |
if (percent_done != prog->percent_done) { |
|
98 |
prog->percent_done = percent_done; |
|
99 |
if (total_passes > 1) { |
|
2283 | 100 |
console_fprintf("\rPass %d/%d: %3d%% ", |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
101 |
prog->pub.completed_passes + prog->completed_extra_passes + 1, |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
102 |
total_passes, percent_done); |
528 | 103 |
} else { |
2283 | 104 |
console_fprintf("\r %3d%% ", percent_done); |
528 | 105 |
} |
2283 | 106 |
console_fflush(stderr); |
528 | 107 |
} |
108 |
} |
|
109 |
||
110 |
||
111 |
void |
|
112 |
JPG_start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress) |
|
113 |
{ |
|
114 |
/* Enable progress display, unless trace output is on */ |
|
115 |
if (cinfo->err->trace_level == 0) { |
|
116 |
progress->pub.progress_monitor = progress_monitor; |
|
117 |
progress->completed_extra_passes = 0; |
|
118 |
progress->total_extra_passes = 0; |
|
119 |
progress->percent_done = -1; |
|
120 |
cinfo->progress = &progress->pub; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
||
125 |
void |
|
126 |
JPG_end_progress_monitor (j_common_ptr cinfo) |
|
127 |
{ |
|
128 |
/* Clear away progress display */ |
|
129 |
if (cinfo->err->trace_level == 0) { |
|
2283 | 130 |
console_fprintf(stderr, "\r \r"); |
528 | 131 |
} |
132 |
} |
|
133 |
||
134 |
#endif |
|
522 | 135 |
|
136 |
%} |
|
137 |
! ! |
|
138 |
||
21 | 139 |
!JPEGReader class methodsFor:'documentation'! |
140 |
||
141 |
copyright |
|
142 |
" |
|
143 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
28 | 144 |
All Rights Reserved |
16 | 145 |
|
21 | 146 |
This software is furnished under a license and may be used |
147 |
only in accordance with the terms of that license and with the |
|
148 |
inclusion of the above copyright notice. This software may not |
|
149 |
be provided or otherwise made available to, or used by, any |
|
150 |
other person. No title to or ownership of the software is |
|
151 |
hereby transferred. |
|
152 |
" |
|
153 |
! |
|
16 | 154 |
|
21 | 155 |
documentation |
156 |
" |
|
157 |
Reader for JPEG images. |
|
199 | 158 |
|
532 | 159 |
This uses the libpeg library to read the image |
199 | 160 |
|
161 |
Only reading of files is supported. |
|
162 |
||
163 |
[See also:] |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
164 |
Image Form Icon |
2282 | 165 |
BlitImageReader FaceReader GIFReader PBMReader PCXReader |
166 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
167 |
XBMReader XPMReader XWDReader |
|
21 | 168 |
" |
169 |
! ! |
|
16 | 170 |
|
28 | 171 |
!JPEGReader class methodsFor:'initialization'! |
172 |
||
173 |
initialize |
|
199 | 174 |
"install myself in the Image classes fileFormat table |
398 | 175 |
for the `.jpg' and '.jpeg' extensions." |
199 | 176 |
|
647 | 177 |
MIMETypes defineImageType:'image/jpeg' suffix:'jpg' reader:self. |
178 |
MIMETypes defineImageType:nil suffix:'jpeg' reader:self. |
|
199 | 179 |
|
398 | 180 |
"Modified: 1.2.1997 / 15:01:55 / cg" |
28 | 181 |
! ! |
182 |
||
522 | 183 |
!JPEGReader class methodsFor:'testing'! |
184 |
||
2768 | 185 |
isValidImageFile:aFilenameOrString |
522 | 186 |
"return true, if aFileName contains a JPG image. |
187 |
Only look at the files name here ..." |
|
188 |
||
2768 | 189 |
|suffix| |
190 |
||
191 |
suffix := aFilenameOrString asFilename suffix asLowercase. |
|
192 |
^ suffix = 'jpg' or:[suffix = 'jpeg']. |
|
522 | 193 |
|
194 |
"Created: 11.4.1997 / 16:26:25 / cg" |
|
2768 | 195 |
|
196 |
" |
|
197 |
self isValidImageFile:'xxx.jpg' |
|
198 |
" |
|
2 | 199 |
! ! |
199 | 200 |
|
528 | 201 |
!JPEGReader methodsFor:'accessing'! |
202 |
||
203 |
forceDitherMode:something |
|
204 |
"set the dither mode, to one of #none or #ordered" |
|
205 |
||
206 |
forceDitherMode := something. |
|
207 |
||
208 |
"Modified: 16.4.1997 / 20:34:59 / cg" |
|
209 |
! |
|
210 |
||
211 |
forceGrayscale:something |
|
212 |
"set the forceGrayscale mode; if true, grayScale images are |
|
213 |
returned, even if the input contains a color image." |
|
214 |
||
215 |
forceGrayscale := something. |
|
216 |
||
217 |
"Created: 16.4.1997 / 20:33:44 / cg" |
|
218 |
"Modified: 16.4.1997 / 20:34:29 / cg" |
|
219 |
! ! |
|
220 |
||
221 |
!JPEGReader methodsFor:'private'! |
|
222 |
||
223 |
create_jpeg_decompress_struct |
|
534 | 224 |
|errMgrStructSize decompressStructSize fp errorOccurred| |
528 | 225 |
|
226 |
fp := inStream filePointer. |
|
227 |
fp isNil ifTrue:[ |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
228 |
self error:'can only read from an external stream'. |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
229 |
^ self. |
528 | 230 |
]. |
231 |
||
232 |
%{ |
|
2050 | 233 |
errMgrStructSize = __mkSmallInteger(sizeof(struct my_error_mgr)); |
234 |
decompressStructSize = __mkSmallInteger(sizeof(struct jpeg_decompress_struct)); |
|
528 | 235 |
%}. |
236 |
||
532 | 237 |
jpeg_error_mgr_struct := ExternalBytes unprotectedNew:errMgrStructSize. |
238 |
jpeg_decompress_struct := ExternalBytes unprotectedNew:decompressStructSize. |
|
534 | 239 |
errorOccurred := false. |
528 | 240 |
|
1698 | 241 |
%{ /* STACK: 400000 */ |
528 | 242 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 243 |
struct my_error_mgr *jerrPtr; |
528 | 244 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
245 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
|
246 |
FILE *f = __FILEVal(fp); |
|
247 |
||
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
248 |
if (__isExternalBytesLike(j_d_s) |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
249 |
&& __isExternalBytesLike(j_e_m)) { |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
250 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
251 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
528 | 252 |
|
2282 | 253 |
/* |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
254 |
* Initialize the JPEG decompression object with default error handling. |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
255 |
*/ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
256 |
cinfoPtr->err = jpeg_std_error(jerrPtr); |
528 | 257 |
|
534 | 258 |
/* |
259 |
* prepare to handle errors smoothly ... |
|
260 |
*/ |
|
261 |
jerrPtr->pub.error_exit = my_error_exit; |
|
262 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
2282 | 263 |
/* |
534 | 264 |
* error occurred ... |
265 |
*/ |
|
266 |
jpeg_destroy_decompress(cinfoPtr); |
|
267 |
RETURN (false); |
|
268 |
} |
|
269 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
270 |
/* |
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
271 |
* use my message print function |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
272 |
*/ |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
273 |
jerrPtr->pub.output_message = my_output_message; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
274 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
275 |
jpeg_create_decompress(cinfoPtr); |
528 | 276 |
#if 0 |
2282 | 277 |
/* |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
278 |
* Insert custom COM marker processor. |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
279 |
*/ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
280 |
jpeg_set_marker_processor(cinfoPtr, JPEG_COM, COM_handler); |
528 | 281 |
#endif |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
282 |
cinfoPtr->err->trace_level = 0; |
528 | 283 |
|
284 |
#if 0 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
285 |
/* colors setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
286 |
cinfoPtr->desired_number_of_colors = val; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
287 |
cinfoPtr->quantize_colors = TRUE; |
528 | 288 |
#endif |
289 |
#if 0 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
290 |
/* dct setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
291 |
cinfoPtr->dct_method = JDCT_ISLOW; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
292 |
or: cinfoPtr->dct_method = JDCT_IFAST; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
293 |
or: cinfoPtr->dct_method = JDCT_FLOAT; |
528 | 294 |
#endif |
295 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
296 |
/* dither setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
297 |
cinfoPtr->dither_mode = JDITHER_FS; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
298 |
if (__INST(forceDitherMode) == @symbol(none)) { |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
299 |
cinfoPtr->dither_mode = JDITHER_NONE; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
300 |
} else { |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
301 |
if (__INST(forceDitherMode) == @symbol(ordered)) { |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
302 |
cinfoPtr->dither_mode = JDITHER_ORDERED; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
303 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
304 |
} |
528 | 305 |
|
306 |
#if 0 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
307 |
/* fast setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
308 |
cinfoPtr->two_pass_quantize = FALSE; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
309 |
cinfoPtr->dither_mode = JDITHER_ORDERED; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
310 |
cinfoPtr->desired_number_of_colors = 216; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
311 |
cinfoPtr->dct_method = JDCT_FASTEST; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
312 |
cinfoPtr->do_fancy_upsampling = FALSE; |
528 | 313 |
#endif |
314 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
315 |
if (__INST(forceGrayscale) == true) { |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
316 |
/* grayscale setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
317 |
cinfoPtr->out_color_space = JCS_GRAYSCALE; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
318 |
} |
528 | 319 |
|
320 |
#if 0 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
321 |
/* maxmemory setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
322 |
cinfoPtr->mem->max_memory_to_use = lval * 1000L; |
528 | 323 |
#endif |
534 | 324 |
|
528 | 325 |
#if 0 |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
326 |
/* nosmooth setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
327 |
cinfoPtr->do_fancy_upsampling = FALSE; |
528 | 328 |
#endif |
534 | 329 |
|
528 | 330 |
#if 0 |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
331 |
/* onepass setting */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
332 |
cinfoPtr->two_pass_quantize = FALSE; |
528 | 333 |
#endif |
334 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
335 |
/* Specify data source for decompression */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
336 |
jpeg_stdio_src(cinfoPtr, f); |
528 | 337 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
338 |
/* Read file header, set default decompression parameters */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
339 |
(void) jpeg_read_header(cinfoPtr, TRUE); |
528 | 340 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
341 |
/* Calculate output image dimensions so we can allocate space */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
342 |
jpeg_calc_output_dimensions(cinfoPtr); |
528 | 343 |
|
2050 | 344 |
__INST(width) = __mkSmallInteger(cinfoPtr->output_width); |
345 |
__INST(height) = __mkSmallInteger(cinfoPtr->output_height); |
|
346 |
__INST(colorComponents) = __mkSmallInteger(cinfoPtr->output_components); |
|
528 | 347 |
|
348 |
#if 0 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
349 |
/* could now set additional values in cinfo |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
350 |
* (colormap) |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
351 |
*/ |
528 | 352 |
#endif |
534 | 353 |
|
528 | 354 |
} |
534 | 355 |
%}. |
356 |
^ true |
|
528 | 357 |
! |
358 |
||
359 |
decompressChunkInto:aByteArray startingAt:index |
|
1698 | 360 |
%{ /* STACK: 400000 */ |
528 | 361 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 362 |
struct my_error_mgr *jerrPtr; |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
363 |
char *rowPtr = NULL; |
528 | 364 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
534 | 365 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
528 | 366 |
int num_scanlines; |
367 |
char *rowPointers[4]; |
|
368 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
369 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
370 |
if (__isByteArray(aByteArray)) { |
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
371 |
rowPtr = (char *)(__ByteArrayInstPtr(aByteArray)->ba_element); |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
372 |
} else if (__isExternalBytesLike(aByteArray)) { |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
373 |
rowPtr = __externalBytesAddress(aByteArray); |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
374 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
375 |
|
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
376 |
if (__isExternalBytesLike(j_d_s) |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
377 |
&& (rowPtr != NULL) |
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
378 |
&& __isExternalBytesLike(j_e_m)) { |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
379 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
380 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
528 | 381 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
382 |
rowPtr += __intVal(index) - 1; |
528 | 383 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
384 |
rowPointers[0] = rowPtr; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
385 |
rowPointers[1] = NULL; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
386 |
rowPointers[2] = NULL; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
387 |
rowPointers[3] = NULL; |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
388 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
389 |
if (cinfoPtr->output_scanline < cinfoPtr->output_height) { |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
390 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
2282 | 391 |
/* |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
392 |
* error occurred ... |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
393 |
*/ |
534 | 394 |
jpeg_destroy_decompress(cinfoPtr); |
2050 | 395 |
RETURN (__mkSmallInteger(-1)); |
534 | 396 |
} |
2282 | 397 |
num_scanlines = jpeg_read_scanlines(cinfoPtr, |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
398 |
rowPointers, |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
399 |
1); |
2050 | 400 |
RETURN (__mkSmallInteger(num_scanlines)); |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
401 |
} |
2050 | 402 |
RETURN (__mkSmallInteger(0)); |
528 | 403 |
} |
404 |
%}. |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
405 |
self halt:'bad arguments'. |
528 | 406 |
! |
407 |
||
408 |
finish_decompress |
|
1698 | 409 |
%{ /* STACK: 400000 */ |
536 | 410 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 411 |
struct my_error_mgr *jerrPtr; |
528 | 412 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
534 | 413 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
528 | 414 |
|
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
415 |
if (__isExternalBytesLike(j_d_s) |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
416 |
&& __isExternalBytesLike(j_e_m)) { |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
417 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
534 | 418 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
528 | 419 |
|
534 | 420 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
421 |
jpeg_destroy_decompress(cinfoPtr); |
|
422 |
RETURN (false); |
|
423 |
} |
|
424 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
425 |
/* finish decompressor */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
426 |
(void) jpeg_finish_decompress(cinfoPtr); |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
427 |
(void) jpeg_destroy_decompress(cinfoPtr); |
534 | 428 |
RETURN (true); |
528 | 429 |
} |
430 |
%} |
|
431 |
! |
|
432 |
||
535 | 433 |
get_error_message |
434 |
%{ |
|
536 | 435 |
struct jpeg_decompress_struct *cinfoPtr; |
535 | 436 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
437 |
char buffer[JMSG_LENGTH_MAX+1]; |
|
438 |
||
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
439 |
if (__isExternalBytesLike(j_d_s)) { |
536 | 440 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
535 | 441 |
|
442 |
(*cinfoPtr->err->format_message) (cinfoPtr, buffer); |
|
443 |
buffer[JMSG_LENGTH_MAX] = '\0'; |
|
444 |
RETURN ( __MKSTRING(buffer)); |
|
445 |
} |
|
446 |
%}. |
|
447 |
^ nil |
|
448 |
! |
|
449 |
||
528 | 450 |
start_decompress |
1698 | 451 |
%{ /* STACK: 400000 */ |
536 | 452 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 453 |
struct my_error_mgr *jerrPtr; |
528 | 454 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
534 | 455 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
528 | 456 |
|
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
457 |
if (__isExternalBytesLike(j_d_s) |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
458 |
&& __isExternalBytesLike(j_e_m)) { |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
459 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
534 | 460 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
461 |
||
462 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
463 |
jpeg_destroy_decompress(cinfoPtr); |
|
464 |
RETURN (false); |
|
465 |
} |
|
528 | 466 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
467 |
/* Start decompressor */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
468 |
(void) jpeg_start_decompress(cinfoPtr); |
534 | 469 |
RETURN (true); |
528 | 470 |
} |
471 |
%} |
|
472 |
! ! |
|
473 |
||
1805 | 474 |
!JPEGReader methodsFor:'reading'! |
528 | 475 |
|
476 |
fromStream:aStream |
|
477 |
"read a JPG image from a stream" |
|
478 |
||
951
de8417214dfd
read from an internal stream (save to tempfile, then read)
Claus Gittinger <cg@exept.de>
parents:
895
diff
changeset
|
479 |
|dataIdx bytesPerRow returnCode pos1 ok tmpFile s| |
528 | 480 |
|
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
481 |
aStream isExternalStream ifFalse:[ |
2768 | 482 |
"/ libJpeg can only handle real OS-streams |
951
de8417214dfd
read from an internal stream (save to tempfile, then read)
Claus Gittinger <cg@exept.de>
parents:
895
diff
changeset
|
483 |
|
2768 | 484 |
tmpFile := Filename newTemporary. |
485 |
[ |
|
486 |
s := tmpFile writeStream binary. |
|
487 |
s nextPutAll:aStream contents. |
|
488 |
s close. |
|
489 |
s := tmpFile readStream binary. |
|
490 |
^ self fromStream:s. |
|
491 |
] ensure:[ |
|
492 |
s notNil ifTrue:[s close]. |
|
493 |
tmpFile delete. |
|
494 |
]. |
|
2282 | 495 |
|
2768 | 496 |
"/ 'JPEGReader [info]: can only read from real streams' infoPrintCR. |
497 |
"/ ^ nil |
|
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
498 |
]. |
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
499 |
|
528 | 500 |
inStream := aStream. |
669
d38a796ad10b
care for files with 128 byte header before actual image
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
501 |
pos1 := inStream position. |
528 | 502 |
|
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
503 |
|
535 | 504 |
(self create_jpeg_decompress_struct not |
505 |
or:[self start_decompress not]) ifTrue:[ |
|
2768 | 506 |
ok := false. |
669
d38a796ad10b
care for files with 128 byte header before actual image
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
507 |
|
2768 | 508 |
"/ if there was no SOI marker, |
509 |
"/ try again, skipping first 128 bytes |
|
510 |
"/ (seems to be generated by some jpg writers) |
|
669
d38a796ad10b
care for files with 128 byte header before actual image
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
511 |
|
2768 | 512 |
inStream position:pos1. |
513 |
((inStream nextByte ~~ 16rFF) |
|
514 |
or:[inStream nextByte ~~ 16rD8]) ifTrue:[ |
|
515 |
inStream position:pos1 + 128. |
|
516 |
((inStream nextByte == 16rFF) |
|
517 |
and:[inStream nextByte == 16rD8]) ifTrue:[ |
|
518 |
inStream position:pos1 + 128. |
|
519 |
ok := self create_jpeg_decompress_struct |
|
520 |
and:[self start_decompress] |
|
521 |
]. |
|
522 |
]. |
|
523 |
ok ifFalse:[ |
|
524 |
'JPEGReader [info]: ' infoPrint. self get_error_message infoPrintCR. |
|
525 |
^ nil |
|
526 |
] |
|
535 | 527 |
]. |
528 | 528 |
|
529 |
data := ByteArray uninitializedNew:(width * height * colorComponents). |
|
530 |
dataIdx := 1. |
|
531 |
bytesPerRow := colorComponents * width. |
|
532 |
||
534 | 533 |
[(returnCode := self decompressChunkInto:data startingAt:dataIdx) > 0] whileTrue:[ |
2768 | 534 |
"/ got a row in the buffer ... |
535 |
dataIdx := dataIdx + bytesPerRow |
|
2282 | 536 |
]. |
534 | 537 |
returnCode < 0 ifTrue:[ |
2768 | 538 |
'JPEGReader [info]: ' infoPrint. self get_error_message infoPrintCR. |
539 |
^ nil |
|
534 | 540 |
]. |
528 | 541 |
|
535 | 542 |
(self finish_decompress) ifFalse:[ |
2768 | 543 |
'JPEGReader [info]: ' infoPrint. self get_error_message infoPrintCR. |
544 |
^ nil |
|
535 | 545 |
]. |
528 | 546 |
|
547 |
colorComponents == 3 ifTrue:[ |
|
2768 | 548 |
photometric := #rgb. |
549 |
samplesPerPixel := 3. |
|
550 |
bitsPerSample := #(8 8 8). |
|
528 | 551 |
] ifFalse:[ |
2768 | 552 |
photometric := #blackIs0. |
553 |
samplesPerPixel := 1. |
|
554 |
bitsPerSample := #(8). |
|
528 | 555 |
]. |
556 |
||
557 |
" |
|
2768 | 558 |
JPEGReader fromFile:'../../support/libjpeg-7/testimg.jpg' |
528 | 559 |
" |
560 |
" |
|
561 |
|stream reader| |
|
562 |
||
2768 | 563 |
stream := '../../support/libjpeg-7/testimg.jpg' asFilename readStream. |
528 | 564 |
reader := JPEGReader new. |
565 |
reader forceGrayscale:true. |
|
566 |
reader forceDitherMode:#ordered. |
|
567 |
reader fromStream:stream. |
|
568 |
^ reader image |
|
569 |
" |
|
570 |
||
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
571 |
"Modified: / 4.4.1998 / 18:48:46 / cg" |
528 | 572 |
! ! |
573 |
||
199 | 574 |
!JPEGReader class methodsFor:'documentation'! |
575 |
||
576 |
version |
|
2768 | 577 |
^ '$Header: /cvs/stx/stx/libview2/JPEGReader.st,v 1.50 2009-10-21 16:52:38 stefan Exp $' |
578 |
! |
|
579 |
||
580 |
version_CVS |
|
581 |
^ '$Header: /cvs/stx/stx/libview2/JPEGReader.st,v 1.50 2009-10-21 16:52:38 stefan Exp $' |
|
199 | 582 |
! ! |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1698
diff
changeset
|
583 |
|
199 | 584 |
JPEGReader initialize! |