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