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