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 |
"
|
|
12 |
|
2
|
13 |
ImageReader subclass:#JPEGReader
|
528
|
14 |
instanceVariableNames:'jpeg_decompress_struct jpeg_error_mgr_struct decompressBuffer
|
|
15 |
colorComponents forceGrayscale forceDitherMode'
|
199
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
259
|
18 |
category:'Graphics-Images-Support'
|
2
|
19 |
!
|
|
20 |
|
522
|
21 |
!JPEGReader primitiveDefinitions!
|
|
22 |
%{
|
|
23 |
|
|
24 |
/*
|
|
25 |
* includes, defines, structure definitions
|
|
26 |
* and typedefs come here.
|
|
27 |
*/
|
|
28 |
|
528
|
29 |
#include <stdio.h>
|
|
30 |
|
|
31 |
#ifndef NO_TESTING
|
|
32 |
# include "../../support/libjpeg-6a/jpeglib.h"
|
|
33 |
# include "../../support/libjpeg-6a/jerror.h"
|
|
34 |
#else
|
|
35 |
# include <jpeglib.h>
|
|
36 |
# include <jerror.h>
|
|
37 |
#endif
|
|
38 |
|
|
39 |
%}
|
|
40 |
! !
|
|
41 |
|
|
42 |
!JPEGReader primitiveFunctions!
|
|
43 |
%{
|
|
44 |
|
|
45 |
/*
|
|
46 |
* any local C (helper) functions
|
|
47 |
* come here (please, define as static)
|
|
48 |
*/
|
|
49 |
|
|
50 |
/*
|
|
51 |
* Optional progress monitor: display a percent-done figure on stderr.
|
|
52 |
*/
|
|
53 |
|
|
54 |
#ifdef PROGRESS_REPORT
|
|
55 |
|
|
56 |
void
|
|
57 |
JPG_progress_monitor (j_common_ptr cinfo)
|
|
58 |
{
|
|
59 |
cd_progress_ptr prog = (cd_progress_ptr) cinfo->progress;
|
|
60 |
int total_passes = prog->pub.total_passes + prog->total_extra_passes;
|
|
61 |
int percent_done = (int) (prog->pub.pass_counter*100L/prog->pub.pass_limit);
|
|
62 |
|
|
63 |
if (percent_done != prog->percent_done) {
|
|
64 |
prog->percent_done = percent_done;
|
|
65 |
if (total_passes > 1) {
|
|
66 |
fprintf(stderr, "\rPass %d/%d: %3d%% ",
|
|
67 |
prog->pub.completed_passes + prog->completed_extra_passes + 1,
|
|
68 |
total_passes, percent_done);
|
|
69 |
} else {
|
|
70 |
fprintf(stderr, "\r %3d%% ", percent_done);
|
|
71 |
}
|
|
72 |
fflush(stderr);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
|
|
77 |
void
|
|
78 |
JPG_start_progress_monitor (j_common_ptr cinfo, cd_progress_ptr progress)
|
|
79 |
{
|
|
80 |
/* Enable progress display, unless trace output is on */
|
|
81 |
if (cinfo->err->trace_level == 0) {
|
|
82 |
progress->pub.progress_monitor = progress_monitor;
|
|
83 |
progress->completed_extra_passes = 0;
|
|
84 |
progress->total_extra_passes = 0;
|
|
85 |
progress->percent_done = -1;
|
|
86 |
cinfo->progress = &progress->pub;
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
|
|
91 |
void
|
|
92 |
JPG_end_progress_monitor (j_common_ptr cinfo)
|
|
93 |
{
|
|
94 |
/* Clear away progress display */
|
|
95 |
if (cinfo->err->trace_level == 0) {
|
|
96 |
fprintf(stderr, "\r \r");
|
|
97 |
fflush(stderr);
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
#endif
|
522
|
102 |
|
|
103 |
%}
|
|
104 |
! !
|
|
105 |
|
21
|
106 |
!JPEGReader class methodsFor:'documentation'!
|
|
107 |
|
|
108 |
copyright
|
|
109 |
"
|
|
110 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
28
|
111 |
All Rights Reserved
|
16
|
112 |
|
21
|
113 |
This software is furnished under a license and may be used
|
|
114 |
only in accordance with the terms of that license and with the
|
|
115 |
inclusion of the above copyright notice. This software may not
|
|
116 |
be provided or otherwise made available to, or used by, any
|
|
117 |
other person. No title to or ownership of the software is
|
|
118 |
hereby transferred.
|
|
119 |
"
|
|
120 |
!
|
16
|
121 |
|
21
|
122 |
documentation
|
|
123 |
"
|
|
124 |
Reader for JPEG images.
|
199
|
125 |
|
532
|
126 |
This uses the libpeg library to read the image
|
199
|
127 |
|
|
128 |
Only reading of files is supported.
|
|
129 |
|
|
130 |
[See also:]
|
234
|
131 |
Image Form Icon
|
199
|
132 |
BlitImageReader FaceReader GIFReader PBMReader PCXReader
|
210
|
133 |
ST80FormReader SunRasterReader TargaReader TIFFReader WindowsIconReader
|
209
|
134 |
XBMReader XPMReader XWDReader
|
21
|
135 |
"
|
|
136 |
! !
|
16
|
137 |
|
28
|
138 |
!JPEGReader class methodsFor:'initialization'!
|
|
139 |
|
|
140 |
initialize
|
199
|
141 |
"install myself in the Image classes fileFormat table
|
398
|
142 |
for the `.jpg' and '.jpeg' extensions."
|
199
|
143 |
|
398
|
144 |
Image addReader:self suffix:'jpg'.
|
|
145 |
Image addReader:self suffix:'jpeg'.
|
199
|
146 |
|
398
|
147 |
"Modified: 1.2.1997 / 15:01:55 / cg"
|
28
|
148 |
! !
|
|
149 |
|
522
|
150 |
!JPEGReader class methodsFor:'testing'!
|
|
151 |
|
|
152 |
isValidImageFile:aFileName
|
|
153 |
"return true, if aFileName contains a JPG image.
|
|
154 |
Only look at the files name here ..."
|
|
155 |
|
|
156 |
(aFileName asLowercase endsWith:'.jpg') ifTrue:[^ true].
|
|
157 |
(aFileName asLowercase endsWith:'.jpeg') ifTrue:[^ true].
|
|
158 |
^ false
|
|
159 |
|
|
160 |
"Created: 11.4.1997 / 16:26:25 / cg"
|
2
|
161 |
! !
|
199
|
162 |
|
528
|
163 |
!JPEGReader methodsFor:'accessing'!
|
|
164 |
|
|
165 |
forceDitherMode:something
|
|
166 |
"set the dither mode, to one of #none or #ordered"
|
|
167 |
|
|
168 |
forceDitherMode := something.
|
|
169 |
|
|
170 |
"Modified: 16.4.1997 / 20:34:59 / cg"
|
|
171 |
!
|
|
172 |
|
|
173 |
forceGrayscale:something
|
|
174 |
"set the forceGrayscale mode; if true, grayScale images are
|
|
175 |
returned, even if the input contains a color image."
|
|
176 |
|
|
177 |
forceGrayscale := something.
|
|
178 |
|
|
179 |
"Created: 16.4.1997 / 20:33:44 / cg"
|
|
180 |
"Modified: 16.4.1997 / 20:34:29 / cg"
|
|
181 |
! !
|
|
182 |
|
|
183 |
!JPEGReader methodsFor:'private'!
|
|
184 |
|
|
185 |
create_jpeg_decompress_struct
|
532
|
186 |
|errMgrStructSize decompressStructSize fp|
|
528
|
187 |
|
|
188 |
fp := inStream filePointer.
|
|
189 |
fp isNil ifTrue:[
|
|
190 |
self error:'can only read from an external stream'.
|
|
191 |
^ self.
|
|
192 |
].
|
|
193 |
|
|
194 |
%{
|
532
|
195 |
errMgrStructSize = __MKSMALLINT(sizeof(struct jpeg_error_mgr));
|
|
196 |
decompressStructSize = __MKSMALLINT(sizeof(struct jpeg_decompress_struct));
|
528
|
197 |
%}.
|
|
198 |
|
532
|
199 |
jpeg_error_mgr_struct := ExternalBytes unprotectedNew:errMgrStructSize.
|
|
200 |
jpeg_decompress_struct := ExternalBytes unprotectedNew:decompressStructSize.
|
528
|
201 |
|
|
202 |
%{
|
|
203 |
struct jpeg_decompress_struct *cinfoPtr;
|
|
204 |
struct jpeg_error_mgr *jerrPtr;
|
|
205 |
OBJ j_e_m = __INST(jpeg_error_mgr_struct);
|
|
206 |
OBJ j_d_s = __INST(jpeg_decompress_struct);
|
|
207 |
FILE *f = __FILEVal(fp);
|
|
208 |
|
|
209 |
if (__isExternalBytes(j_d_s)
|
|
210 |
&& __isExternalBytes(j_e_m)) {
|
|
211 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s));
|
|
212 |
jerrPtr = (struct jpeg_error_mgr *)(__externalBytesAddress(j_e_m));
|
|
213 |
|
|
214 |
/*
|
|
215 |
* Initialize the JPEG decompression object with default error handling.
|
|
216 |
*/
|
|
217 |
cinfoPtr->err = jpeg_std_error(jerrPtr);
|
|
218 |
|
|
219 |
jpeg_create_decompress(cinfoPtr);
|
|
220 |
#if 0
|
|
221 |
/*
|
|
222 |
* Insert custom COM marker processor.
|
|
223 |
*/
|
|
224 |
jpeg_set_marker_processor(cinfoPtr, JPEG_COM, COM_handler);
|
|
225 |
#endif
|
|
226 |
cinfoPtr->err->trace_level = 0;
|
|
227 |
|
|
228 |
#if 0
|
|
229 |
/* colors setting */
|
|
230 |
cinfoPtr->desired_number_of_colors = val;
|
|
231 |
cinfoPtr->quantize_colors = TRUE;
|
|
232 |
#endif
|
|
233 |
#if 0
|
|
234 |
/* dct setting */
|
|
235 |
cinfoPtr->dct_method = JDCT_ISLOW;
|
|
236 |
or: cinfoPtr->dct_method = JDCT_IFAST;
|
|
237 |
or: cinfoPtr->dct_method = JDCT_FLOAT;
|
|
238 |
#endif
|
|
239 |
|
|
240 |
/* dither setting */
|
|
241 |
cinfoPtr->dither_mode = JDITHER_FS;
|
|
242 |
if (__INST(forceDitherMode) == @symbol(none)) {
|
|
243 |
cinfoPtr->dither_mode = JDITHER_NONE;
|
|
244 |
} else {
|
|
245 |
if (__INST(forceDitherMode) == @symbol(ordered)) {
|
|
246 |
cinfoPtr->dither_mode = JDITHER_ORDERED;
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
#if 0
|
|
251 |
/* fast setting */
|
|
252 |
cinfoPtr->two_pass_quantize = FALSE;
|
|
253 |
cinfoPtr->dither_mode = JDITHER_ORDERED;
|
|
254 |
cinfoPtr->desired_number_of_colors = 216;
|
|
255 |
cinfoPtr->dct_method = JDCT_FASTEST;
|
|
256 |
cinfoPtr->do_fancy_upsampling = FALSE;
|
|
257 |
#endif
|
|
258 |
|
|
259 |
if (__INST(forceGrayscale) == true) {
|
|
260 |
/* grayscale setting */
|
|
261 |
cinfoPtr->out_color_space = JCS_GRAYSCALE;
|
|
262 |
}
|
|
263 |
|
|
264 |
#if 0
|
|
265 |
/* maxmemory setting */
|
|
266 |
cinfoPtr->mem->max_memory_to_use = lval * 1000L;
|
|
267 |
#endif
|
|
268 |
#if 0
|
|
269 |
/* nosmooth setting */
|
|
270 |
cinfoPtr->do_fancy_upsampling = FALSE;
|
|
271 |
#endif
|
|
272 |
#if 0
|
|
273 |
/* onepass setting */
|
|
274 |
cinfoPtr->two_pass_quantize = FALSE;
|
|
275 |
#endif
|
|
276 |
|
|
277 |
/* Specify data source for decompression */
|
|
278 |
jpeg_stdio_src(cinfoPtr, f);
|
|
279 |
|
|
280 |
/* Read file header, set default decompression parameters */
|
|
281 |
(void) jpeg_read_header(cinfoPtr, TRUE);
|
|
282 |
|
|
283 |
/* Calculate output image dimensions so we can allocate space */
|
|
284 |
jpeg_calc_output_dimensions(cinfoPtr);
|
|
285 |
|
|
286 |
__INST(width) = __MKSMALLINT(cinfoPtr->output_width);
|
|
287 |
__INST(height) = __MKSMALLINT(cinfoPtr->output_height);
|
|
288 |
__INST(colorComponents) = __MKSMALLINT(cinfoPtr->output_components);
|
|
289 |
|
|
290 |
printf("colorComponents: %d\n", cinfoPtr->output_components);
|
|
291 |
|
|
292 |
#if 0
|
|
293 |
/* could now set additional values in cinfo
|
|
294 |
* (colormap)
|
|
295 |
*/
|
|
296 |
#endif
|
|
297 |
}
|
|
298 |
%}
|
|
299 |
|
|
300 |
!
|
|
301 |
|
|
302 |
decompressChunkInto:aByteArray startingAt:index
|
|
303 |
%{
|
|
304 |
struct jpeg_decompress_struct *cinfoPtr;
|
|
305 |
char *rowPtr;
|
|
306 |
OBJ j_d_s = __INST(jpeg_decompress_struct);
|
|
307 |
int num_scanlines;
|
|
308 |
char *rowPointers[4];
|
|
309 |
|
|
310 |
if (__isExternalBytes(j_d_s)
|
|
311 |
&& __isByteArray(aByteArray)) {
|
|
312 |
cinfoPtr = (struct jpeg_decompress_struct *)(__externalBytesAddress(j_d_s));
|
|
313 |
rowPtr = (char *)(__ByteArrayInstPtr(aByteArray)->ba_element);
|
|
314 |
rowPtr += __intVal(index) - 1;
|
|
315 |
|
|
316 |
rowPointers[0] = rowPtr;
|
|
317 |
rowPointers[1] = NULL;
|
|
318 |
rowPointers[2] = NULL;
|
|
319 |
rowPointers[3] = NULL;
|
|
320 |
|
|
321 |
if (cinfoPtr->output_scanline < cinfoPtr->output_height) {
|
|
322 |
num_scanlines = jpeg_read_scanlines(cinfoPtr,
|
|
323 |
rowPointers,
|
|
324 |
1);
|
|
325 |
RETURN (__MKSMALLINT(num_scanlines));
|
|
326 |
}
|
|
327 |
RETURN (__MKSMALLINT(0));
|
|
328 |
}
|
|
329 |
%}.
|
|
330 |
self halt.
|
|
331 |
!
|
|
332 |
|
|
333 |
finish_decompress
|
|
334 |
%{
|
|
335 |
struct jpeg_decompress *cinfoPtr;
|
|
336 |
OBJ j_d_s = __INST(jpeg_decompress_struct);
|
|
337 |
|
|
338 |
if (__isExternalBytes(j_d_s)) {
|
|
339 |
cinfoPtr = (struct jpeg_decompress *)(__externalBytesAddress(j_d_s));
|
|
340 |
|
|
341 |
/* Start decompressor */
|
|
342 |
(void) jpeg_finish_decompress(cinfoPtr);
|
|
343 |
(void) jpeg_destroy_decompress(cinfoPtr);
|
|
344 |
}
|
|
345 |
%}
|
|
346 |
|
|
347 |
|
|
348 |
!
|
|
349 |
|
|
350 |
start_decompress
|
|
351 |
%{
|
|
352 |
struct jpeg_decompress *cinfoPtr;
|
|
353 |
OBJ j_d_s = __INST(jpeg_decompress_struct);
|
|
354 |
|
|
355 |
if (__isExternalBytes(j_d_s)) {
|
|
356 |
cinfoPtr = (struct jpeg_decompress *)(__externalBytesAddress(j_d_s));
|
|
357 |
|
|
358 |
/* Start decompressor */
|
|
359 |
(void) jpeg_start_decompress(cinfoPtr);
|
|
360 |
}
|
|
361 |
%}
|
|
362 |
! !
|
|
363 |
|
|
364 |
!JPEGReader methodsFor:'reading from stream'!
|
|
365 |
|
|
366 |
fromStream:aStream
|
|
367 |
"read a JPG image from a stream"
|
|
368 |
|
|
369 |
|dataIdx bytesPerRow|
|
|
370 |
|
|
371 |
inStream := aStream.
|
|
372 |
|
|
373 |
self create_jpeg_decompress_struct.
|
|
374 |
self start_decompress.
|
|
375 |
|
|
376 |
data := ByteArray uninitializedNew:(width * height * colorComponents).
|
|
377 |
dataIdx := 1.
|
|
378 |
bytesPerRow := colorComponents * width.
|
|
379 |
|
|
380 |
[(self decompressChunkInto:data startingAt:dataIdx) ~~ 0] whileTrue:[
|
|
381 |
"/ got a row in the buffer ...
|
|
382 |
dataIdx := dataIdx + bytesPerRow
|
|
383 |
].
|
|
384 |
|
|
385 |
self finish_decompress.
|
|
386 |
|
|
387 |
colorComponents == 3 ifTrue:[
|
|
388 |
photometric := #rgb.
|
|
389 |
samplesPerPixel := 3.
|
|
390 |
bitsPerSample := #(8 8 8).
|
|
391 |
] ifFalse:[
|
|
392 |
self halt:'unexpected value'
|
|
393 |
].
|
|
394 |
|
|
395 |
"
|
|
396 |
JPEGReader fromFile:'../../support/libjpeg-6a/testimg.jpg'
|
|
397 |
"
|
|
398 |
"
|
|
399 |
|stream reader|
|
|
400 |
|
|
401 |
stream := '../../support/libjpeg-6a/testimg.jpg' asFilename readStream.
|
|
402 |
reader := JPEGReader new.
|
|
403 |
reader forceGrayscale:true.
|
|
404 |
reader forceDitherMode:#ordered.
|
|
405 |
reader fromStream:stream.
|
|
406 |
^ reader image
|
|
407 |
"
|
|
408 |
|
|
409 |
"Modified: 16.4.1997 / 20:36:12 / cg"
|
|
410 |
! !
|
|
411 |
|
199
|
412 |
!JPEGReader class methodsFor:'documentation'!
|
|
413 |
|
|
414 |
version
|
532
|
415 |
^ '$Header: /cvs/stx/stx/libview2/JPEGReader.st,v 1.21 1997-04-16 21:15:24 cg Exp $'
|
199
|
416 |
! !
|
|
417 |
JPEGReader initialize!
|