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