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