author | Jan Vrany <jan.vrany@labware.com> |
Tue, 09 Aug 2022 21:18:05 +0100 | |
branch | jv |
changeset 4471 | df7515191c90 |
parent 4213 | 8127ef0ff47d |
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 |
|
3564 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
2 | 16 |
ImageReader subclass:#JPEGReader |
3564 | 17 |
instanceVariableNames:'jpeg_decompress_struct jpeg_compress_struct jpeg_error_mgr_struct |
18 |
colorComponents forceGrayscale forceDitherMode app1SegmentHandler |
|
19 |
compressQuality bytesPerRow' |
|
3692 | 20 |
classVariableNames:'DefaultCompressQuality ErrorPrinting' |
199 | 21 |
poolDictionaries:'' |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1698
diff
changeset
|
22 |
category:'Graphics-Images-Readers' |
2 | 23 |
! |
24 |
||
522 | 25 |
!JPEGReader primitiveDefinitions! |
26 |
%{ |
|
27 |
||
28 |
/* |
|
29 |
* includes, defines, structure definitions |
|
30 |
* and typedefs come here. |
|
31 |
*/ |
|
32 |
||
528 | 33 |
#include <stdio.h> |
34 |
||
542 | 35 |
#ifdef sunos |
36 |
# include <sys/types.h> |
|
528 | 37 |
#endif |
38 |
||
542 | 39 |
#include <jpeglib.h> |
40 |
#include <jerror.h> |
|
41 |
||
534 | 42 |
struct my_error_mgr { |
43 |
struct jpeg_error_mgr pub; |
|
44 |
jmp_buf setjmp_buffer; |
|
45 |
}; |
|
46 |
||
528 | 47 |
%} |
48 |
! ! |
|
49 |
||
50 |
!JPEGReader primitiveFunctions! |
|
51 |
%{ |
|
52 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
53 |
/* |
528 | 54 |
* any local C (helper) functions |
55 |
* come here (please, define as static) |
|
56 |
*/ |
|
57 |
||
3085 | 58 |
static void |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
59 |
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
|
60 |
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
|
61 |
{ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
62 |
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
|
63 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
64 |
if (@global(ErrorPrinting) == true) { |
2283 | 65 |
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
|
66 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
67 |
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
|
68 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
69 |
|
3085 | 70 |
static void |
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
{ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
74 |
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
|
75 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
76 |
if (@global(ErrorPrinting) == true) { |
2283 | 77 |
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
|
78 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
79 |
/* Create the message */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
80 |
(*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
|
81 |
|
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
82 |
/* Send it to stderr, adding a newline */ |
2283 | 83 |
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
|
84 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
85 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
86 |
|
528 | 87 |
/* |
88 |
* Optional progress monitor: display a percent-done figure on stderr. |
|
89 |
*/ |
|
90 |
||
91 |
#ifdef PROGRESS_REPORT |
|
92 |
||
93 |
void |
|
94 |
JPG_progress_monitor (j_common_ptr cinfo) |
|
95 |
{ |
|
96 |
cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress; |
|
97 |
int total_passes = prog->pub.total_passes + prog->total_extra_passes; |
|
98 |
int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit); |
|
99 |
||
100 |
if (percent_done != prog->percent_done) { |
|
101 |
prog->percent_done = percent_done; |
|
102 |
if (total_passes > 1) { |
|
2283 | 103 |
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
|
104 |
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
|
105 |
total_passes, percent_done); |
528 | 106 |
} else { |
2283 | 107 |
console_fprintf("\r %3d%% ", percent_done); |
528 | 108 |
} |
2283 | 109 |
console_fflush(stderr); |
528 | 110 |
} |
111 |
} |
|
112 |
||
113 |
||
114 |
void |
|
115 |
JPG_start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress) |
|
116 |
{ |
|
117 |
/* Enable progress display, unless trace output is on */ |
|
118 |
if (cinfo->err->trace_level == 0) { |
|
119 |
progress->pub.progress_monitor = progress_monitor; |
|
120 |
progress->completed_extra_passes = 0; |
|
121 |
progress->total_extra_passes = 0; |
|
122 |
progress->percent_done = -1; |
|
123 |
cinfo->progress = &progress->pub; |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
||
128 |
void |
|
129 |
JPG_end_progress_monitor (j_common_ptr cinfo) |
|
130 |
{ |
|
131 |
/* Clear away progress display */ |
|
132 |
if (cinfo->err->trace_level == 0) { |
|
2283 | 133 |
console_fprintf(stderr, "\r \r"); |
528 | 134 |
} |
135 |
} |
|
136 |
||
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
137 |
#endif /* PROGRESS_REPORT */ |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
138 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
139 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
140 |
/* fetch a byte from the stream */ |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
141 |
unsigned int |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
142 |
JPG_jpeg_getc (j_decompress_ptr cinfo) |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
143 |
/* Read next byte */ |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
144 |
{ |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
145 |
struct jpeg_source_mgr *datasrc = cinfo->src; |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
146 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
147 |
if (datasrc->bytes_in_buffer == 0) { |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
148 |
if (! (*datasrc->fill_input_buffer) (cinfo)) |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
149 |
return -1; |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
150 |
} |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
151 |
datasrc->bytes_in_buffer--; |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
152 |
return (*datasrc->next_input_byte++) & 0xFF; |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
153 |
} |
522 | 154 |
|
155 |
%} |
|
156 |
! ! |
|
157 |
||
21 | 158 |
!JPEGReader class methodsFor:'documentation'! |
159 |
||
160 |
copyright |
|
161 |
" |
|
162 |
COPYRIGHT (c) 1993 by Claus Gittinger |
|
28 | 163 |
All Rights Reserved |
16 | 164 |
|
21 | 165 |
This software is furnished under a license and may be used |
166 |
only in accordance with the terms of that license and with the |
|
167 |
inclusion of the above copyright notice. This software may not |
|
168 |
be provided or otherwise made available to, or used by, any |
|
169 |
other person. No title to or ownership of the software is |
|
170 |
hereby transferred. |
|
171 |
" |
|
172 |
! |
|
16 | 173 |
|
21 | 174 |
documentation |
175 |
" |
|
176 |
Reader for JPEG images. |
|
199 | 177 |
|
532 | 178 |
This uses the libpeg library to read the image |
199 | 179 |
|
3564 | 180 |
Only writing of depth24 images is currently supported. |
199 | 181 |
|
182 |
[See also:] |
|
3564 | 183 |
Image Form Icon |
184 |
BlitImageReader FaceReader GIFReader PBMReader PCXReader |
|
185 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader |
|
186 |
XBMReader XPMReader XWDReader |
|
187 |
" |
|
188 |
! |
|
189 |
||
190 |
examples |
|
191 |
" |
|
192 |
good quality: |
|
193 |
[exBegin] |
|
194 |
|i i24 i2| |
|
195 |
||
196 |
Transcript printf:'gif original %d\n' with:( '../../goodies/bitmaps/gifImages/garfield.gif' asFilename fileSize ). |
|
197 |
i := Image fromFile:'../../goodies/bitmaps/gifImages/garfield.gif'. |
|
198 |
i24 := Depth24Image fromImage:i. |
|
199 |
JPEGReader save:i24 onFile:'test100.jpg'. |
|
200 |
i2 := Image fromFile:'test100.jpg'. |
|
201 |
Transcript printf:'jpg 100%% %d\n' with:( 'test100.jpg' asFilename fileSize ). |
|
202 |
i2 inspect |
|
203 |
[exEnd] |
|
204 |
||
205 |
||
206 |
normal quality: |
|
207 |
[exBegin] |
|
208 |
|i i24 i2| |
|
209 |
||
210 |
Transcript printf:'gif original %d\n' with:( '../../goodies/bitmaps/gifImages/garfield.gif' asFilename fileSize ). |
|
211 |
i := Image fromFile:'../../goodies/bitmaps/gifImages/garfield.gif'. |
|
212 |
i24 := Depth24Image fromImage:i. |
|
213 |
JPEGReader save:i24 onFile:'test80.jpg'. |
|
214 |
i2 := Image fromFile:'test80.jpg'. |
|
215 |
Transcript printf:'jpg 80%% %d\n' with:( 'test80.jpg' asFilename fileSize ). |
|
216 |
i2 inspect |
|
217 |
[exEnd] |
|
218 |
||
219 |
||
220 |
low quality: |
|
221 |
[exBegin] |
|
222 |
|i i24 i2| |
|
223 |
||
224 |
i := Image fromFile:'../../goodies/bitmaps/gifImages/garfield.gif'. |
|
225 |
i24 := Depth24Image fromImage:i. |
|
226 |
JPEGReader new save:i24 onStream:('test50.jpg' asFilename writeStream) compressQuality:50. |
|
227 |
i2 := Image fromFile:'test50.jpg'. |
|
228 |
Transcript printf:'jpg 50%% %d\n' with:( 'test50.jpg' asFilename fileSize ). |
|
229 |
i2 inspect |
|
230 |
[exEnd] |
|
231 |
||
232 |
bad quality: |
|
233 |
[exBegin] |
|
234 |
|i i24 i2| |
|
235 |
||
236 |
i := Image fromFile:'../../goodies/bitmaps/gifImages/garfield.gif'. |
|
237 |
i24 := Depth24Image fromImage:i. |
|
238 |
JPEGReader new save:i24 onStream:('test20.jpg' asFilename writeStream) compressQuality:20. |
|
239 |
i2 := Image fromFile:'test20.jpg'. |
|
240 |
Transcript printf:'jpg 20%% %d\n' with:( 'test20.jpg' asFilename fileSize ). |
|
241 |
i2 inspect |
|
242 |
[exEnd] |
|
243 |
||
244 |
very bad quality: |
|
245 |
[exBegin] |
|
246 |
|i i24 i2| |
|
247 |
||
248 |
i := Image fromFile:'../../goodies/bitmaps/gifImages/garfield.gif'. |
|
249 |
i24 := Depth24Image fromImage:i. |
|
250 |
JPEGReader new save:i24 onStream:('test10.jpg' asFilename writeStream) compressQuality:10. |
|
251 |
i2 := Image fromFile:'test10.jpg'. |
|
252 |
Transcript printf:'jpg 10%% %d\n' with:( 'test10.jpg' asFilename fileSize ). |
|
253 |
i2 inspect |
|
254 |
[exEnd] |
|
21 | 255 |
" |
256 |
! ! |
|
16 | 257 |
|
28 | 258 |
!JPEGReader class methodsFor:'initialization'! |
259 |
||
260 |
initialize |
|
199 | 261 |
"install myself in the Image classes fileFormat table |
398 | 262 |
for the `.jpg' and '.jpeg' extensions." |
199 | 263 |
|
647 | 264 |
MIMETypes defineImageType:'image/jpeg' suffix:'jpg' reader:self. |
265 |
MIMETypes defineImageType:nil suffix:'jpeg' reader:self. |
|
199 | 266 |
|
398 | 267 |
"Modified: 1.2.1997 / 15:01:55 / cg" |
28 | 268 |
! ! |
269 |
||
3564 | 270 |
!JPEGReader class methodsFor:'defaults'! |
271 |
||
272 |
defaultCompressQuality |
|
3575 | 273 |
^ DefaultCompressQuality ? 85 |
274 |
! |
|
275 |
||
276 |
defaultCompressQuality:percent |
|
277 |
DefaultCompressQuality := percent. |
|
3564 | 278 |
! ! |
279 |
||
522 | 280 |
!JPEGReader class methodsFor:'testing'! |
281 |
||
3572 | 282 |
canRepresent:anImage |
283 |
"return true, if anImage can be represented in my file format. |
|
284 |
For now, only d24 images are supported." |
|
285 |
||
286 |
anImage depth == 24 ifTrue:[^ true]. |
|
287 |
('JPEGReader [info]: image depth is not 24.') infoPrintCR. |
|
288 |
^ false. |
|
289 |
! |
|
290 |
||
2768 | 291 |
isValidImageFile:aFilenameOrString |
522 | 292 |
"return true, if aFileName contains a JPG image. |
3345 | 293 |
Only look at the file's name here ..." |
522 | 294 |
|
3557 | 295 |
^ (MIMETypes mimeTypeForSuffix:aFilenameOrString asFilename suffix) = 'image/jpeg' |
522 | 296 |
|
297 |
"Created: 11.4.1997 / 16:26:25 / cg" |
|
2768 | 298 |
|
299 |
" |
|
3557 | 300 |
self isValidImageFile:'xxx.jpg' |
2768 | 301 |
" |
2 | 302 |
! ! |
199 | 303 |
|
528 | 304 |
!JPEGReader methodsFor:'accessing'! |
305 |
||
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
306 |
app1SegmentHandler:aBlock |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
307 |
"set a handler block for app1 segment data (geolocation in exif format)" |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
308 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
309 |
app1SegmentHandler := aBlock |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
310 |
! |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
311 |
|
3576 | 312 |
compressQuality:compressQualityOrNilForDefault |
313 |
compressQuality := compressQualityOrNilForDefault |
|
314 |
! |
|
315 |
||
528 | 316 |
forceDitherMode:something |
317 |
"set the dither mode, to one of #none or #ordered" |
|
318 |
||
319 |
forceDitherMode := something. |
|
320 |
||
321 |
"Modified: 16.4.1997 / 20:34:59 / cg" |
|
322 |
! |
|
323 |
||
324 |
forceGrayscale:something |
|
325 |
"set the forceGrayscale mode; if true, grayScale images are |
|
326 |
returned, even if the input contains a color image." |
|
327 |
||
328 |
forceGrayscale := something. |
|
329 |
||
330 |
"Created: 16.4.1997 / 20:33:44 / cg" |
|
331 |
"Modified: 16.4.1997 / 20:34:29 / cg" |
|
332 |
! ! |
|
333 |
||
334 |
!JPEGReader methodsFor:'private'! |
|
335 |
||
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
336 |
app1SegmentCallback |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
337 |
"return a callback function which invokes the app1SegmentHandlerBlock if defined. |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
338 |
This will be called to handle the exif segment, containing geolocation tags. |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
339 |
Return nil, if there is no handler blcok defined" |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
340 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
341 |
|cb| |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
342 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
343 |
app1SegmentHandler isNil ifTrue:[^ nil]. |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
344 |
|
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
345 |
cb := ExternalFunctionCallback new. |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
346 |
cb returnType:#bool argumentTypes:#(pointer). |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
347 |
cb generateClosure. |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
348 |
cb action:[:args | self fetchApp1SegmentData. true]. |
3060 | 349 |
^ cb code. "can be passed to C." |
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
350 |
! |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
351 |
|
3564 | 352 |
create_jpeg_compress_struct |
353 |
|errMgrStructSize compressStructSize fp errorOccurred| |
|
354 |
||
355 |
self assert:(photometric == #rgb). |
|
356 |
self assert:(samplesPerPixel == 3). |
|
357 |
self assert:(bitsPerSample asArray = #(8 8 8)). |
|
358 |
||
359 |
fp := outStream filePointer. |
|
360 |
fp isNil ifTrue:[ |
|
361 |
self error:'can only write to an external stream'. |
|
362 |
^ false. |
|
363 |
]. |
|
364 |
||
365 |
%{ |
|
366 |
errMgrStructSize = __mkSmallInteger(sizeof(struct my_error_mgr)); |
|
367 |
compressStructSize = __mkSmallInteger(sizeof(struct jpeg_compress_struct)); |
|
368 |
%}. |
|
369 |
||
370 |
jpeg_error_mgr_struct := ExternalBytes unprotectedNew:errMgrStructSize. |
|
371 |
jpeg_compress_struct := ExternalBytes unprotectedNew:compressStructSize. |
|
372 |
errorOccurred := false. |
|
373 |
||
374 |
%{ /* STACK: 400000 */ |
|
375 |
struct jpeg_compress_struct *cinfoPtr; |
|
376 |
struct my_error_mgr *jerrPtr; |
|
377 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
|
378 |
OBJ j_c_s = __INST(jpeg_compress_struct); |
|
379 |
FILE *f = __FILEVal(fp); |
|
380 |
char *outBuffer; |
|
381 |
||
382 |
if (__isExternalBytesLike(j_c_s) |
|
383 |
&& __isExternalBytesLike(j_e_m)) { |
|
384 |
cinfoPtr = (struct jpeg_compress_struct *)(__externalBytesAddress(j_c_s)); |
|
385 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
|
386 |
||
387 |
/* |
|
388 |
* Initialize the JPEG decompression object with default error handling. |
|
389 |
*/ |
|
390 |
cinfoPtr->err = jpeg_std_error(&jerrPtr->pub); |
|
391 |
||
392 |
/* |
|
393 |
* prepare to handle errors smoothly ... |
|
394 |
*/ |
|
395 |
jerrPtr->pub.error_exit = my_error_exit; |
|
396 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
397 |
/* |
|
398 |
* error occurred ... |
|
399 |
*/ |
|
400 |
jpeg_destroy_compress(cinfoPtr); |
|
401 |
RETURN (false); |
|
402 |
} |
|
403 |
||
404 |
/* |
|
405 |
* use my message print function |
|
406 |
*/ |
|
407 |
jerrPtr->pub.output_message = my_output_message; |
|
408 |
||
409 |
jpeg_create_compress(cinfoPtr); |
|
410 |
||
411 |
/* Specify data destination for compression */ |
|
412 |
jpeg_stdio_dest(cinfoPtr, f); |
|
413 |
||
414 |
cinfoPtr->image_width = __intVal(__INST(width)); |
|
415 |
cinfoPtr->image_height = __intVal(__INST(height)); |
|
416 |
cinfoPtr->input_components = __intVal(__INST(samplesPerPixel)); |
|
417 |
cinfoPtr->in_color_space = JCS_RGB; |
|
418 |
||
419 |
jpeg_set_defaults(cinfoPtr); |
|
420 |
/*set the quality [0..100] */ |
|
421 |
jpeg_set_quality (cinfoPtr, __intVal(__INST(compressQuality)), 1); |
|
422 |
jpeg_start_compress(cinfoPtr, 1); |
|
423 |
||
424 |
||
425 |
cinfoPtr->err->trace_level = 0; |
|
426 |
} |
|
427 |
%}. |
|
428 |
^ true |
|
429 |
! |
|
430 |
||
528 | 431 |
create_jpeg_decompress_struct |
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
432 |
|errMgrStructSize decompressStructSize fp errorOccurred app1SegmentCallbackFunction| |
528 | 433 |
|
434 |
fp := inStream filePointer. |
|
435 |
fp isNil ifTrue:[ |
|
3557 | 436 |
self error:'can only read from an external stream'. |
437 |
^ false. |
|
528 | 438 |
]. |
439 |
||
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
440 |
app1SegmentCallbackFunction := self app1SegmentCallback. |
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
441 |
|
528 | 442 |
%{ |
2050 | 443 |
errMgrStructSize = __mkSmallInteger(sizeof(struct my_error_mgr)); |
444 |
decompressStructSize = __mkSmallInteger(sizeof(struct jpeg_decompress_struct)); |
|
528 | 445 |
%}. |
446 |
||
532 | 447 |
jpeg_error_mgr_struct := ExternalBytes unprotectedNew:errMgrStructSize. |
448 |
jpeg_decompress_struct := ExternalBytes unprotectedNew:decompressStructSize. |
|
534 | 449 |
errorOccurred := false. |
528 | 450 |
|
1698 | 451 |
%{ /* STACK: 400000 */ |
528 | 452 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 453 |
struct my_error_mgr *jerrPtr; |
528 | 454 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
455 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
|
456 |
FILE *f = __FILEVal(fp); |
|
457 |
||
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
458 |
if (__isExternalBytesLike(j_d_s) |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
459 |
&& __isExternalBytesLike(j_e_m)) { |
3557 | 460 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
461 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
|
528 | 462 |
|
3557 | 463 |
/* |
464 |
* Initialize the JPEG decompression object with default error handling. |
|
465 |
*/ |
|
466 |
cinfoPtr->err = jpeg_std_error(&jerrPtr->pub); |
|
528 | 467 |
|
3557 | 468 |
/* |
469 |
* prepare to handle errors smoothly ... |
|
470 |
*/ |
|
471 |
jerrPtr->pub.error_exit = my_error_exit; |
|
472 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
473 |
/* |
|
474 |
* error occurred ... |
|
475 |
*/ |
|
476 |
jpeg_destroy_decompress(cinfoPtr); |
|
477 |
RETURN (false); |
|
478 |
} |
|
534 | 479 |
|
3557 | 480 |
/* |
481 |
* use my message print function |
|
482 |
*/ |
|
483 |
jerrPtr->pub.output_message = my_output_message; |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
484 |
|
3557 | 485 |
jpeg_create_decompress(cinfoPtr); |
528 | 486 |
#if 0 |
3557 | 487 |
/* |
488 |
* Insert custom COM marker processor. |
|
489 |
*/ |
|
490 |
jpeg_set_marker_processor(cinfoPtr, JPEG_COM, COM_handler); |
|
528 | 491 |
#endif |
3557 | 492 |
#if 0 |
493 |
if (app1SegmentCallbackFunction != nil) { |
|
494 |
jpeg_marker_parser_method cb = NULL; |
|
2971
07bd4e98036e
hooks to extract Exif (Geolocation) data.
Claus Gittinger <cg@exept.de>
parents:
2768
diff
changeset
|
495 |
|
3557 | 496 |
if (__isExternalFunction(app1SegmentCallbackFunction)) { |
497 |
cb = (jpeg_marker_parser_method)__externalFunctionVal(app1SegmentCallbackFunction); |
|
498 |
} else if (__isExternalAddress(app1SegmentCallbackFunction)) { |
|
499 |
cb = (jpeg_marker_parser_method)__externalAddressVal(app1SegmentCallbackFunction); |
|
500 |
} else { |
|
501 |
/* ignore, but should report an error... */ |
|
502 |
} |
|
503 |
jpeg_set_marker_processor(cinfoPtr, JPEG_APP0+1, cb); |
|
504 |
} |
|
505 |
#endif |
|
506 |
/* Specify data source for decompression */ |
|
507 |
jpeg_stdio_src(cinfoPtr, f); |
|
3562 | 508 |
# if JPEG_LIB_VERSION_MAJOR >= 9 |
3557 | 509 |
jpeg_save_markers (cinfoPtr, JPEG_APP0, 0xffff); |
510 |
jpeg_save_markers (cinfoPtr, JPEG_APP0+1, 0xffff); |
|
511 |
jpeg_save_markers (cinfoPtr, JPEG_APP0+13, 0xffff); |
|
3560 | 512 |
#endif |
3557 | 513 |
/* Read file header, set default decompression parameters */ |
514 |
(void) jpeg_read_header(cinfoPtr, TRUE); |
|
3086 | 515 |
|
3557 | 516 |
cinfoPtr->err->trace_level = 0; |
528 | 517 |
|
518 |
#if 0 |
|
3557 | 519 |
/* colors setting */ |
520 |
cinfoPtr->desired_number_of_colors = val; |
|
521 |
cinfoPtr->quantize_colors = TRUE; |
|
528 | 522 |
#endif |
523 |
#if 0 |
|
3557 | 524 |
/* dct setting */ |
525 |
cinfoPtr->dct_method = JDCT_ISLOW; |
|
526 |
or: cinfoPtr->dct_method = JDCT_IFAST; |
|
527 |
or: cinfoPtr->dct_method = JDCT_FLOAT; |
|
528 | 528 |
#endif |
529 |
||
3557 | 530 |
/* dither setting */ |
531 |
cinfoPtr->dither_mode = JDITHER_FS; |
|
532 |
if (__INST(forceDitherMode) == @symbol(none)) { |
|
533 |
cinfoPtr->dither_mode = JDITHER_NONE; |
|
534 |
} else { |
|
535 |
if (__INST(forceDitherMode) == @symbol(ordered)) { |
|
536 |
cinfoPtr->dither_mode = JDITHER_ORDERED; |
|
537 |
} |
|
538 |
} |
|
528 | 539 |
|
540 |
#if 0 |
|
3557 | 541 |
/* fast setting */ |
542 |
cinfoPtr->two_pass_quantize = FALSE; |
|
543 |
cinfoPtr->dither_mode = JDITHER_ORDERED; |
|
544 |
cinfoPtr->desired_number_of_colors = 216; |
|
545 |
cinfoPtr->dct_method = JDCT_FASTEST; |
|
546 |
cinfoPtr->do_fancy_upsampling = FALSE; |
|
528 | 547 |
#endif |
548 |
||
3557 | 549 |
if (__INST(forceGrayscale) == true) { |
550 |
/* grayscale setting */ |
|
551 |
cinfoPtr->out_color_space = JCS_GRAYSCALE; |
|
552 |
} |
|
528 | 553 |
|
554 |
#if 0 |
|
3557 | 555 |
/* maxmemory setting */ |
556 |
cinfoPtr->mem->max_memory_to_use = lval * 1000L; |
|
528 | 557 |
#endif |
534 | 558 |
|
528 | 559 |
#if 0 |
3557 | 560 |
/* nosmooth setting */ |
561 |
cinfoPtr->do_fancy_upsampling = FALSE; |
|
528 | 562 |
#endif |
534 | 563 |
|
528 | 564 |
#if 0 |
3557 | 565 |
/* onepass setting */ |
566 |
cinfoPtr->two_pass_quantize = FALSE; |
|
528 | 567 |
#endif |
568 |
||
3557 | 569 |
/* Calculate output image dimensions so we can allocate space */ |
570 |
jpeg_calc_output_dimensions(cinfoPtr); |
|
571 |
||
572 |
__INST(width) = __mkSmallInteger(cinfoPtr->output_width); |
|
573 |
__INST(height) = __mkSmallInteger(cinfoPtr->output_height); |
|
574 |
__INST(colorComponents) = __mkSmallInteger(cinfoPtr->output_components); |
|
575 |
||
576 |
/* could now set additional values in cinfo |
|
577 |
* (colormap) |
|
578 |
*/ |
|
579 |
||
580 |
} |
|
581 |
%}. |
|
582 |
^ true |
|
583 |
! |
|
584 |
||
528 | 585 |
decompressChunkInto:aByteArray startingAt:index |
1698 | 586 |
%{ /* STACK: 400000 */ |
528 | 587 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 588 |
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
|
589 |
char *rowPtr = NULL; |
528 | 590 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
534 | 591 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
528 | 592 |
int num_scanlines; |
3083 | 593 |
JSAMPROW rowPointers[4]; |
528 | 594 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
595 |
if (__isByteArray(aByteArray)) { |
3557 | 596 |
rowPtr = (char *)(__ByteArrayInstPtr(aByteArray)->ba_element); |
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
597 |
} else if (__isExternalBytesLike(aByteArray)) { |
3557 | 598 |
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
|
599 |
} |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
600 |
|
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
601 |
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
|
602 |
&& (rowPtr != NULL) |
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
603 |
&& __isExternalBytesLike(j_e_m)) { |
3557 | 604 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
605 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
|
528 | 606 |
|
3557 | 607 |
rowPtr += __intVal(index) - 1; |
528 | 608 |
|
3557 | 609 |
rowPointers[0] = rowPtr; |
610 |
rowPointers[1] = NULL; |
|
611 |
rowPointers[2] = NULL; |
|
612 |
rowPointers[3] = NULL; |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
613 |
|
3557 | 614 |
if (cinfoPtr->output_scanline < cinfoPtr->output_height) { |
615 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
616 |
/* |
|
617 |
* error occurred ... |
|
618 |
*/ |
|
619 |
jpeg_destroy_decompress(cinfoPtr); |
|
620 |
RETURN (__mkSmallInteger(-1)); |
|
621 |
} |
|
622 |
num_scanlines = jpeg_read_scanlines(cinfoPtr, rowPointers, 1); |
|
623 |
RETURN (__mkSmallInteger(num_scanlines)); |
|
624 |
} |
|
625 |
RETURN (__mkSmallInteger(0)); |
|
528 | 626 |
} |
627 |
%}. |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
628 |
self halt:'bad arguments'. |
528 | 629 |
! |
630 |
||
2972 | 631 |
extractApp1DataFrom:data |
632 |
(data startsWith:#[16r45 16r78 16r69 16r66 16r0] " = 'Exif' + 0-byte" ) ifTrue:[ |
|
3557 | 633 |
self extractExifDataFrom:data. |
2972 | 634 |
]. |
635 |
||
636 |
"Created: / 12-12-2011 / 21:22:23 / cg" |
|
637 |
! |
|
638 |
||
639 |
extractExifDataFrom:data |
|
3098 | 640 |
self halt:'ignored exif data' |
2972 | 641 |
|
642 |
"Created: / 12-12-2011 / 21:22:32 / cg" |
|
643 |
! |
|
644 |
||
645 |
fetchApp1SegmentData |
|
646 |
|byte1 byte2 count| |
|
647 |
||
648 |
byte1 := self jpeg_getc. |
|
649 |
byte2 := self jpeg_getc. |
|
650 |
count := (byte1 bitShift:8) + byte2. "/ msb first |
|
651 |
count := count - 2. "/ count itself is included |
|
652 |
data := ByteArray new:count. |
|
653 |
1 to: count do:[:i | |
|
3557 | 654 |
data at:i put:(self jpeg_getc). |
2972 | 655 |
]. |
3098 | 656 |
self breakPoint:#cg. |
2972 | 657 |
app1SegmentHandler value:data. |
658 |
! |
|
659 |
||
3564 | 660 |
finish_compress |
661 |
%{ /* STACK: 400000 */ |
|
662 |
struct jpeg_compress_struct *cinfoPtr; |
|
663 |
struct my_error_mgr *jerrPtr; |
|
664 |
OBJ j_c_s = __INST(jpeg_compress_struct); |
|
665 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
|
666 |
||
667 |
if (__isExternalBytesLike(j_c_s) |
|
668 |
&& __isExternalBytesLike(j_e_m)) { |
|
669 |
cinfoPtr = (struct jpeg_compress_struct *)(__externalBytesAddress(j_c_s)); |
|
670 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
|
671 |
||
672 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
673 |
jpeg_destroy_compress(cinfoPtr); |
|
674 |
RETURN (false); |
|
675 |
} |
|
676 |
||
677 |
/* finish decompressor */ |
|
678 |
(void) jpeg_finish_compress(cinfoPtr); |
|
679 |
(void) jpeg_destroy_compress(cinfoPtr); |
|
680 |
RETURN (true); |
|
681 |
} |
|
682 |
%} |
|
683 |
! |
|
684 |
||
528 | 685 |
finish_decompress |
1698 | 686 |
%{ /* STACK: 400000 */ |
536 | 687 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 688 |
struct my_error_mgr *jerrPtr; |
528 | 689 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
534 | 690 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
528 | 691 |
|
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
692 |
if (__isExternalBytesLike(j_d_s) |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
693 |
&& __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
|
694 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
534 | 695 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
528 | 696 |
|
534 | 697 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
698 |
jpeg_destroy_decompress(cinfoPtr); |
|
699 |
RETURN (false); |
|
700 |
} |
|
701 |
||
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
702 |
/* finish decompressor */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
703 |
(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
|
704 |
(void) jpeg_destroy_decompress(cinfoPtr); |
534 | 705 |
RETURN (true); |
528 | 706 |
} |
707 |
%} |
|
708 |
! |
|
709 |
||
535 | 710 |
get_error_message |
711 |
%{ |
|
3084 | 712 |
j_common_ptr cinfoPtr; |
535 | 713 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
714 |
char buffer[JMSG_LENGTH_MAX+1]; |
|
715 |
||
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
716 |
if (__isExternalBytesLike(j_d_s)) { |
3557 | 717 |
cinfoPtr = (j_common_ptr)(__externalBytesAddress(j_d_s)); |
535 | 718 |
|
3557 | 719 |
(*cinfoPtr->err->format_message) (cinfoPtr, buffer); |
720 |
buffer[JMSG_LENGTH_MAX] = '\0'; |
|
721 |
RETURN ( __MKSTRING(buffer)); |
|
535 | 722 |
} |
723 |
%}. |
|
724 |
^ nil |
|
725 |
! |
|
726 |
||
2972 | 727 |
jpeg_getc |
728 |
%{ |
|
729 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
|
730 |
||
731 |
if (__isExternalBytesLike(j_d_s)) { |
|
732 |
struct jpeg_decompress_struct *cinfoPtr; |
|
733 |
int byte; |
|
734 |
||
735 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
|
736 |
||
737 |
byte = JPG_jpeg_getc (cinfoPtr); |
|
738 |
RETURN( __MKSMALLINT(byte) ); |
|
739 |
} |
|
740 |
%}. |
|
741 |
self primitiveFailed |
|
742 |
! |
|
743 |
||
528 | 744 |
start_decompress |
1698 | 745 |
%{ /* STACK: 400000 */ |
536 | 746 |
struct jpeg_decompress_struct *cinfoPtr; |
534 | 747 |
struct my_error_mgr *jerrPtr; |
528 | 748 |
OBJ j_d_s = __INST(jpeg_decompress_struct); |
534 | 749 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
528 | 750 |
|
1747
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
751 |
if (__isExternalBytesLike(j_d_s) |
90a5fcae4b4b
also handle subclasses of externalBytes
Claus Gittinger <cg@exept.de>
parents:
1745
diff
changeset
|
752 |
&& __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
|
753 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s)); |
534 | 754 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
755 |
||
756 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
757 |
jpeg_destroy_decompress(cinfoPtr); |
|
758 |
RETURN (false); |
|
759 |
} |
|
528 | 760 |
|
1061
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
761 |
/* Start decompressor */ |
e487ecc3d506
define my own error printing function (req'd to avoid bad
Claus Gittinger <cg@exept.de>
parents:
951
diff
changeset
|
762 |
(void) jpeg_start_decompress(cinfoPtr); |
534 | 763 |
RETURN (true); |
528 | 764 |
} |
765 |
%} |
|
766 |
! ! |
|
767 |
||
1805 | 768 |
!JPEGReader methodsFor:'reading'! |
528 | 769 |
|
770 |
fromStream:aStream |
|
3692 | 771 |
"read a JPG image from a stream. |
772 |
For now, we can only read from a stdio-FILE with libjpeg |
|
773 |
(need to write a mem-reader to read from a Smalltalk stream). |
|
774 |
Therefore, any internal stream data is copied to a temporary file first, |
|
775 |
and libjpg asked to decompress from there. |
|
776 |
This should be fixed, if jpeg reading is a bottleneck in your app." |
|
528 | 777 |
|
3692 | 778 |
|dataIdx bytesPerRow returnCode pos1 pos2 ok s img| |
528 | 779 |
|
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
780 |
aStream isExternalStream ifFalse:[ |
3692 | 781 |
"/ 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
|
782 |
|
3692 | 783 |
s := FileStream newTemporary binary. |
784 |
[ |
|
785 |
pos1 := aStream position. |
|
786 |
aStream copyToEndInto:s. |
|
787 |
aStream position:pos1. |
|
788 |
"/ s nextPutAll:aStream contents. |
|
789 |
s reset. |
|
790 |
img := self fromStream:s. |
|
791 |
] ensure:[ |
|
792 |
"/ leave the internal stream positioned correctly. |
|
793 |
"/ sigh: jpgreader seems to read too much (buffer-filling?); |
|
794 |
"/ so the position is usually beyond the actual end of the single image. |
|
795 |
"/ for now, you have to use workarounds (such as scanning for SOI markers) |
|
796 |
"/ to get individual images from a sequence. |
|
797 |
pos2 := s position. |
|
798 |
s close. |
|
799 |
s fileName delete. |
|
800 |
]. |
|
801 |
img notNil ifTrue:[ |
|
802 |
aStream position:(pos1 + pos2). |
|
803 |
]. |
|
804 |
^ img |
|
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
805 |
]. |
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
806 |
|
528 | 807 |
inStream := aStream. |
669
d38a796ad10b
care for files with 128 byte header before actual image
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
808 |
pos1 := inStream position. |
528 | 809 |
|
2972 | 810 |
"/ to extract app1 data (for example, exif geolocation tags), |
811 |
"/ uncomment the following and implement the exif tag reader... |
|
2973
b708ff319811
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2972
diff
changeset
|
812 |
"/ Warning: this does not yet work as expected. |
2974
9d47730e05e0
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2973
diff
changeset
|
813 |
"/ app1SegmentHandler := [:data | self extractApp1DataFrom:data]. |
895
ebac67edf08e
check for inStream being an external stream.
Claus Gittinger <cg@exept.de>
parents:
669
diff
changeset
|
814 |
|
535 | 815 |
(self create_jpeg_decompress_struct not |
816 |
or:[self start_decompress not]) ifTrue:[ |
|
3692 | 817 |
ok := false. |
669
d38a796ad10b
care for files with 128 byte header before actual image
Claus Gittinger <cg@exept.de>
parents:
647
diff
changeset
|
818 |
|
3692 | 819 |
"/ if there was no SOI marker, |
820 |
"/ try again, skipping first 128 bytes |
|
821 |
"/ (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
|
822 |
|
3692 | 823 |
inStream position:pos1. |
824 |
((inStream nextByte ~~ 16rFF) |
|
825 |
or:[inStream nextByte ~~ 16rD8]) ifTrue:[ |
|
826 |
inStream position:pos1 + 128. |
|
827 |
((inStream nextByte == 16rFF) |
|
828 |
and:[inStream nextByte == 16rD8]) ifTrue:[ |
|
829 |
inStream position:pos1 + 128. |
|
830 |
ok := self create_jpeg_decompress_struct |
|
831 |
and:[self start_decompress] |
|
832 |
]. |
|
833 |
]. |
|
834 |
ok ifFalse:[ |
|
835 |
'JPEGReader [info]: ' infoPrint. self get_error_message infoPrintCR. |
|
836 |
^ nil |
|
837 |
] |
|
535 | 838 |
]. |
528 | 839 |
|
840 |
data := ByteArray uninitializedNew:(width * height * colorComponents). |
|
841 |
dataIdx := 1. |
|
842 |
bytesPerRow := colorComponents * width. |
|
843 |
||
534 | 844 |
[(returnCode := self decompressChunkInto:data startingAt:dataIdx) > 0] whileTrue:[ |
3692 | 845 |
"/ got a row in the buffer ... |
846 |
dataIdx := dataIdx + bytesPerRow |
|
2282 | 847 |
]. |
534 | 848 |
returnCode < 0 ifTrue:[ |
3692 | 849 |
'JPEGReader [info]: ' infoPrint. self get_error_message infoPrintCR. |
850 |
^ nil |
|
534 | 851 |
]. |
528 | 852 |
|
535 | 853 |
(self finish_decompress) ifFalse:[ |
3692 | 854 |
'JPEGReader [info]: ' infoPrint. self get_error_message infoPrintCR. |
855 |
^ nil |
|
535 | 856 |
]. |
528 | 857 |
|
858 |
colorComponents == 3 ifTrue:[ |
|
3692 | 859 |
photometric := #rgb. |
860 |
samplesPerPixel := 3. |
|
861 |
bitsPerSample := #(8 8 8). |
|
528 | 862 |
] ifFalse:[ |
3692 | 863 |
photometric := #blackIs0. |
864 |
samplesPerPixel := 1. |
|
865 |
bitsPerSample := #(8). |
|
528 | 866 |
]. |
867 |
||
868 |
" |
|
3086 | 869 |
JPEGReader fromFile:'../../support/libjpeg-9/testimg.jpg' |
528 | 870 |
" |
3191 | 871 |
|
872 |
" |
|
873 |
|stream| |
|
874 |
stream := #[] readWriteStream. |
|
875 |
'../../support/libjpeg-9/testimg.jpg' asFilename copyToStream:stream. |
|
876 |
JPEGReader fromStream:stream |
|
877 |
" |
|
878 |
||
528 | 879 |
" |
880 |
|stream reader| |
|
881 |
||
3060 | 882 |
stream := '../../support/libjpeg-9/testimg.jpg' asFilename readStream. |
528 | 883 |
reader := JPEGReader new. |
884 |
reader forceGrayscale:true. |
|
885 |
reader forceDitherMode:#ordered. |
|
886 |
reader fromStream:stream. |
|
887 |
^ reader image |
|
888 |
" |
|
889 |
||
2974
9d47730e05e0
comment/format in: #fromStream:
Claus Gittinger <cg@exept.de>
parents:
2973
diff
changeset
|
890 |
"Modified: / 12-12-2011 / 21:34:48 / cg" |
528 | 891 |
! ! |
892 |
||
3564 | 893 |
!JPEGReader methodsFor:'writing'! |
894 |
||
895 |
compressScanlines |
|
896 |
%{ |
|
897 |
unsigned char *rowPtr; |
|
898 |
int bpr = __intVal(__INST(bytesPerRow)); |
|
899 |
int row; |
|
900 |
int __height = __intVal(__INST(height)); |
|
901 |
struct jpeg_compress_struct *cinfoPtr; |
|
902 |
struct my_error_mgr *jerrPtr; |
|
903 |
OBJ j_c_s = __INST(jpeg_compress_struct); |
|
904 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct); |
|
905 |
OBJ __data = __INST(data); |
|
3559 | 906 |
|
3564 | 907 |
if (__isByteArray(__data)) { |
908 |
rowPtr = (char *)(__ByteArrayInstPtr(__data)->ba_element); |
|
909 |
} else if (__isExternalBytesLike(__data)) { |
|
910 |
rowPtr = __externalBytesAddress(__data); |
|
911 |
} |
|
912 |
||
913 |
if (__isExternalBytesLike(j_c_s) |
|
914 |
&& __isExternalBytesLike(j_e_m)) { |
|
915 |
cinfoPtr = (struct jpeg_compress_struct *)(__externalBytesAddress(j_c_s)); |
|
916 |
jerrPtr = (struct my_error_mgr *)(__externalBytesAddress(j_e_m)); |
|
917 |
||
918 |
if (setjmp(jerrPtr->setjmp_buffer)) { |
|
919 |
jpeg_destroy_compress(cinfoPtr); |
|
920 |
RETURN (false); |
|
921 |
} |
|
922 |
||
923 |
for (row=0; row<__height; row++) { |
|
924 |
jpeg_write_scanlines(cinfoPtr, &rowPtr, 1); |
|
925 |
rowPtr += bpr; |
|
926 |
} |
|
927 |
} |
|
928 |
%} |
|
929 |
! |
|
3557 | 930 |
|
3575 | 931 |
save:anImage onFile:fileName |
932 |
anImage depth ~~ 24 ifTrue:[ |
|
933 |
Error handle:[:ex | |
|
934 |
^ Image cannotRepresentImageSignal |
|
935 |
raiseWith:anImage |
|
936 |
errorString:('failed to convert image to depth24: ', ex description). |
|
937 |
"/ ex return. |
|
938 |
] do:[ |
|
939 |
^ self save:(Depth24Image fromImage:anImage) onFile:fileName |
|
940 |
]. |
|
941 |
]. |
|
942 |
^ super save:anImage onFile:fileName. |
|
943 |
! |
|
944 |
||
3557 | 945 |
save:image onStream:aStream |
946 |
"save image in JPG-file-format onto aStream" |
|
947 |
||
948 |
image depth ~~ 24 ifTrue:[ |
|
3575 | 949 |
^ Image cannotRepresentImageSignal |
950 |
raiseWith:image |
|
951 |
errorString:('JPG (currently) only supports depth24 images'). |
|
3557 | 952 |
]. |
953 |
||
954 |
outStream := aStream. |
|
955 |
outStream binary. |
|
956 |
||
957 |
"/ mask := image mask. |
|
958 |
"/ mask notNil ifTrue:[ |
|
959 |
"/ self assignTransparentPixelIn:image |
|
960 |
"/ ]. |
|
961 |
||
962 |
width := image width. |
|
963 |
height := image height. |
|
964 |
photometric := image photometric. |
|
965 |
samplesPerPixel := image samplesPerPixel. |
|
966 |
bitsPerSample := image bitsPerSample. |
|
967 |
colorMap := image colorMap. |
|
968 |
bytesPerRow := image bytesPerRow. |
|
969 |
data := image bits. |
|
3575 | 970 |
compressQuality isNil ifTrue:[ |
971 |
compressQuality := self class defaultCompressQuality. |
|
972 |
]. |
|
3557 | 973 |
|
974 |
(self create_jpeg_compress_struct not) ifTrue:[ |
|
3575 | 975 |
self error:'JPG: setup error'. |
3557 | 976 |
]. |
977 |
||
978 |
self compressScanlines. |
|
979 |
self finish_compress. |
|
980 |
! ! |
|
981 |
||
199 | 982 |
!JPEGReader class methodsFor:'documentation'! |
983 |
||
984 |
version |
|
3557 | 985 |
^ '$Header$' |
2768 | 986 |
! |
987 |
||
988 |
version_CVS |
|
3557 | 989 |
^ '$Header$' |
199 | 990 |
! ! |
1745
4fa0fad2a463
code cleanup (colorMap handling)
Claus Gittinger <cg@exept.de>
parents:
1698
diff
changeset
|
991 |
|
3060 | 992 |
|
199 | 993 |
JPEGReader initialize! |