0
|
1 |
"
|
6
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
0
|
3 |
All Rights Reserved
|
|
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 |
|
|
13 |
Object subclass:#Image
|
2
|
14 |
instanceVariableNames:'bytes width height
|
|
15 |
bitsPerSample samplesPerPixel
|
|
16 |
colorMap photometric
|
0
|
17 |
device deviceForm monoDeviceForm
|
|
18 |
fullColorDeviceForm'
|
|
19 |
classVariableNames:'lobby
|
|
20 |
DitherAlgorithm NumberOfDitherColors
|
|
21 |
FileFormats'
|
|
22 |
poolDictionaries:''
|
|
23 |
category:'Graphics-Display Objects'
|
|
24 |
!
|
|
25 |
|
|
26 |
Image comment:'
|
|
27 |
|
6
|
28 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
0
|
29 |
All Rights Reserved
|
|
30 |
|
6
|
31 |
$Header: /cvs/stx/stx/libview/Image.st,v 1.4 1993-10-13 02:43:36 claus Exp $
|
0
|
32 |
written Summer 91 by claus
|
|
33 |
'!
|
|
34 |
|
|
35 |
!Image class methodsFor:'documentation'!
|
|
36 |
|
|
37 |
documentation
|
|
38 |
"
|
|
39 |
this class provides (some time in the future) representation
|
|
40 |
for all kinds of images (monochrome, greyscale and color)
|
2
|
41 |
and will finally replace Form - its still under construction.
|
0
|
42 |
|
|
43 |
An Image keeps all info in a device independent way, but may get
|
|
44 |
associated to a device. The data held keeps all information which
|
|
45 |
was originally present, even if the display-device has lower resolution.
|
|
46 |
Therefore, it is even on low color resolution displays possible, to
|
|
47 |
process and manipulate images without loosing color information.
|
|
48 |
|
|
49 |
Usually, you get a device specific representation of the image by
|
2
|
50 |
sending an image the 'on:aDevice' message, which will create
|
0
|
51 |
a (possibly) dithered form representing the image using the currently
|
|
52 |
available colors.
|
|
53 |
Sometimes, a monochrome representation is needed (X servers take monochrome
|
|
54 |
icons only), this will be created by 'image>>monochromeOn:aDevice'.
|
2
|
55 |
Also, it is planned to generate another hi-color resolution version,
|
|
56 |
which needs its own colormap to be installed and allows use of all
|
0
|
57 |
256 colors on an 8bit display (not currently implemented).
|
|
58 |
|
|
59 |
To convert pictures from/to external file-formats, readers are used
|
|
60 |
which have the file-format knowledge (see TIFFReader, GIFReader etc.).
|
|
61 |
|
|
62 |
The algorithms in here (especially dithering & color allocation) are
|
|
63 |
experimental and far from beeing perfect (some are very slow).
|
|
64 |
Much more work is needed and will be done in the near future ...
|
|
65 |
Dithering is done as:
|
2
|
66 |
|
0
|
67 |
DitherAlgorithm:
|
|
68 |
|
|
69 |
nil a simple threshold algorithm
|
|
70 |
(i.e. for mono, p<0.5 -> black, p>=0.5 -> white)
|
|
71 |
|
|
72 |
#pattern patterned dither
|
|
73 |
(for p, take dithered color to fill pixel;
|
|
74 |
uses dithering in color-class)
|
|
75 |
|
|
76 |
#error error diffusion dither (Floyd-Steinberg)
|
|
77 |
planned
|
|
78 |
|
|
79 |
File formats are handled by subclasses of ImageReader, which understand
|
|
80 |
a specific format. You can add more readers, by adding an association
|
2
|
81 |
such as ('.jpg' -> JPEGReader) to the class variable 'FileFormats' (see
|
|
82 |
Image initialize.
|
0
|
83 |
|
|
84 |
instance variables:
|
|
85 |
|
|
86 |
width <Integer> the width in pixels
|
|
87 |
height <Integer> the height in pixels
|
2
|
88 |
bytes <ByteArray> the full image information
|
0
|
89 |
photometric <Symbol> #rgb, #palette, #blackIs0 or #whiteIs0
|
|
90 |
samplesPerPixel <Integer> the number of planes
|
|
91 |
bitsPerSample <Array> the number of bits per plane
|
|
92 |
colorMap <Array> an Array of 3 arrays containing red,
|
|
93 |
green and blue values (0..255 biased)
|
|
94 |
device <Workstation> the device on which deviceForm,
|
|
95 |
monoDeviceForm and lowResDeviceForm are
|
|
96 |
deviceForm <Form> the device form which gives the best
|
|
97 |
possible aproximation of the image on
|
|
98 |
device using standard colors.
|
|
99 |
monoDeviceForm <Form> the device form which gives a monochrome
|
|
100 |
aproximation of the image on device.
|
|
101 |
fullColorDeviceForm <Form> the device form which gives the best
|
|
102 |
possible aproximation of the image on
|
|
103 |
device using private colors.
|
|
104 |
|
|
105 |
class variables:
|
|
106 |
|
|
107 |
lobby <Registry> keeps track of known images
|
|
108 |
DitherAlgorithm <Symbol> defines how to dither
|
|
109 |
NumberOfDitherColors <Integer> defines, how many dither colors to use
|
|
110 |
FileFormats <Dictionary> associates filename extensions to
|
|
111 |
image reader classes
|
|
112 |
"
|
|
113 |
! !
|
|
114 |
|
|
115 |
!Image class methodsFor:'misc'!
|
|
116 |
|
|
117 |
dither:aSymbol
|
2
|
118 |
"define how to dither - #pattern, #error or none;
|
0
|
119 |
error diffusion dithering is currently not implemented,
|
|
120 |
pattern dither is currently very slow."
|
|
121 |
|
|
122 |
DitherAlgorithm := aSymbol
|
|
123 |
|
|
124 |
"Image dither:#pattern"
|
|
125 |
"Image dither:#error"
|
|
126 |
"Image dither:nil"
|
|
127 |
!
|
|
128 |
|
|
129 |
numberOfDitherColors:n
|
|
130 |
"define how many colors (i.e. patterns) to use when
|
|
131 |
doing a pattern dither (good values are:)"
|
|
132 |
|
|
133 |
NumberOfDitherColors := n
|
|
134 |
! !
|
|
135 |
|
|
136 |
!Image class methodsFor:'initialization'!
|
|
137 |
|
|
138 |
initialize
|
2
|
139 |
"initialize class constants"
|
|
140 |
|
0
|
141 |
"setup tracker of known pictures"
|
|
142 |
lobby isNil ifTrue:[
|
|
143 |
lobby := Registry new.
|
|
144 |
ObjectMemory addDependent:self.
|
|
145 |
].
|
2
|
146 |
|
|
147 |
"define algorithm to use for dithering - currently only nil or #pattern supported"
|
0
|
148 |
DitherAlgorithm := #pattern. "will be changed to error as soon as implemented"
|
2
|
149 |
(Display notNil and:[Display hasGreyscales]) ifFalse:[
|
|
150 |
NumberOfDitherColors := 64
|
|
151 |
] ifTrue:[
|
|
152 |
"as far as I remember, this is about the number of grey values, the eye can distinguish"
|
|
153 |
NumberOfDitherColors := 100
|
|
154 |
].
|
0
|
155 |
|
2
|
156 |
"define reader classes"
|
0
|
157 |
FileFormats := Dictionary new.
|
|
158 |
FileFormats at:'.xbm' put:XBMReader.
|
|
159 |
FileFormats at:'.tiff' put:TIFFReader.
|
2
|
160 |
FileFormats at:'.tif' put:TIFFReader.
|
0
|
161 |
FileFormats at:'.gif' put:GIFReader.
|
|
162 |
FileFormats at:'.img' put:IMGReader.
|
|
163 |
FileFormats at:'.pcx' put:PCXReader.
|
|
164 |
FileFormats at:'.mac' put:MacPaintReader.
|
|
165 |
FileFormats at:'.im8' put:SunRasterReader.
|
2
|
166 |
FileFormats at:'.icon' put:SunRasterReader.
|
0
|
167 |
FileFormats at:'.face' put:FaceReader.
|
|
168 |
FileFormats at:'.g3' put:G3FileReader.
|
2
|
169 |
FileFormats at:'.bmp' put:WindowsIconReader.
|
0
|
170 |
FileFormats at:'.ico' put:WindowsIconReader.
|
2
|
171 |
FileFormats at:'.jpg' put:JPEGReader.
|
|
172 |
FileFormats at:'.jpeg' put:JPEGReader.
|
|
173 |
FileFormats at:'.ppm' put:PBMReader.
|
|
174 |
FileFormats at:'.pbm' put:PBMReader.
|
|
175 |
FileFormats at:'.pgm' put:PBMReader.
|
0
|
176 |
!
|
|
177 |
|
|
178 |
flushDeviceImages
|
|
179 |
"simply unassign all pictures from their device"
|
|
180 |
|
|
181 |
lobby contentsDo:[:anImage |
|
|
182 |
anImage restored
|
|
183 |
]
|
|
184 |
!
|
|
185 |
|
|
186 |
update:something
|
|
187 |
"flush all device specific stuff when restarted from a snapshot"
|
|
188 |
|
|
189 |
(something == #restarted) ifTrue:[
|
|
190 |
self flushDeviceImages
|
|
191 |
]
|
|
192 |
! !
|
|
193 |
|
2
|
194 |
!Image class methodsFor:'screen capture'!
|
0
|
195 |
|
2
|
196 |
fromScreen
|
|
197 |
"return an image of the full screen"
|
|
198 |
|
|
199 |
^ self fromScreen:(0@0 corner:(Display width@Display height))
|
0
|
200 |
!
|
|
201 |
|
2
|
202 |
fromScreen:aRectangle
|
|
203 |
"return an image of a part of the screen"
|
|
204 |
|
|
205 |
|depth img|
|
|
206 |
|
|
207 |
depth := Display depth.
|
|
208 |
img := (self implementorForDepth: depth) new.
|
|
209 |
^ img fromScreen:aRectangle
|
|
210 |
|
|
211 |
"Image fromScreen:(0@0 corner:100@100)"
|
0
|
212 |
! !
|
|
213 |
|
|
214 |
!Image class methodsFor:'reading from file'!
|
|
215 |
|
|
216 |
fromFile:aFileName
|
2
|
217 |
|readerClass image|
|
|
218 |
|
|
219 |
FileFormats associationsDo:[:assoc |
|
|
220 |
(aFileName endsWith:(assoc key)) ifTrue:[
|
|
221 |
readerClass := assoc value.
|
|
222 |
readerClass notNil ifTrue:[
|
|
223 |
image := readerClass fromFile:aFileName.
|
|
224 |
image notNil ifTrue:[^ image].
|
|
225 |
]
|
0
|
226 |
]
|
|
227 |
].
|
|
228 |
"no known extension - ask all readers if they know
|
|
229 |
this format ..."
|
|
230 |
|
2
|
231 |
FileFormats associationsDo:[:assoc |
|
|
232 |
readerClass := assoc value.
|
|
233 |
readerClass notNil ifTrue:[
|
|
234 |
(readerClass isValidImageFile:aFileName) ifTrue:[
|
|
235 |
^ readerClass fromFile:aFileName
|
|
236 |
]
|
0
|
237 |
]
|
|
238 |
].
|
|
239 |
|
|
240 |
"nope - unknown format"
|
2
|
241 |
'unknown image file format' printNewline.
|
|
242 |
^ nil
|
0
|
243 |
|
|
244 |
"Image fromFile:'bitmaps/dano.tiff'"
|
|
245 |
"Image fromFile:'bitmaps/test.fax'"
|
|
246 |
"Image fromFile:'bitmaps/voice.tiff'"
|
|
247 |
"Image fromFile:'voice.tiff'"
|
|
248 |
|
|
249 |
"Image fromFile:'../fileIn/bitmaps/claus.gif'"
|
|
250 |
"Image fromFile:'../fileIn/bitmaps/garfield.gif'"
|
|
251 |
|
|
252 |
"Image fromFile:'../fileIn/bitmaps/founders.im8'"
|
|
253 |
"Image fromFile:'../goodies/faces/next.com/steve.face'"
|
|
254 |
|
|
255 |
"Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico'"
|
|
256 |
"Image fromFile:'bitmaps/globe1.xbm'"
|
2
|
257 |
"Image fromFile:'bitmaps/hello_world.icon'"
|
|
258 |
! !
|
|
259 |
|
|
260 |
!Image class methodsFor:'queries'!
|
|
261 |
|
|
262 |
implementorForDepth: depth
|
|
263 |
"return the class, which best implements images of depth"
|
|
264 |
|
|
265 |
depth == 1 ifTrue:[^ Depth1Image].
|
|
266 |
depth == 2 ifTrue:[^ Depth2Image].
|
|
267 |
depth == 4 ifTrue:[^ Depth4Image].
|
|
268 |
depth == 8 ifTrue:[^ Depth8Image].
|
|
269 |
depth == 24 ifTrue:[^ Depth24Image].
|
|
270 |
^ self
|
|
271 |
! !
|
|
272 |
|
|
273 |
!Image methodsFor:'instance release'!
|
|
274 |
|
|
275 |
restored
|
|
276 |
device := nil.
|
|
277 |
deviceForm := nil.
|
|
278 |
monoDeviceForm := nil.
|
|
279 |
fullColorDeviceForm := nil
|
|
280 |
!
|
|
281 |
|
|
282 |
disposed
|
|
283 |
"some Image has been collected - nothing to do"
|
|
284 |
! !
|
|
285 |
|
|
286 |
!Image methodsFor:'inpecting'!
|
|
287 |
|
|
288 |
inspect
|
|
289 |
"redefined to launch an ImageInspector on the receiver
|
|
290 |
(instead of the default InspectorView)."
|
|
291 |
|
|
292 |
ImageInspectorView isNil ifTrue:[
|
|
293 |
super inspect
|
|
294 |
] ifFalse:[
|
|
295 |
ImageInspectorView openOn:self
|
|
296 |
]
|
0
|
297 |
! !
|
|
298 |
|
|
299 |
!Image methodsFor:'accessing'!
|
|
300 |
|
|
301 |
device
|
|
302 |
"return the device, the receiver is associated with.
|
|
303 |
Return nil, if the image is unassigned."
|
|
304 |
|
|
305 |
^ device
|
|
306 |
!
|
|
307 |
|
|
308 |
id
|
|
309 |
"return the id of the image on the device.
|
|
310 |
Return nil, if the image is unassigned."
|
|
311 |
|
|
312 |
deviceForm isNil ifTrue:[^ nil].
|
|
313 |
^ deviceForm id
|
|
314 |
!
|
|
315 |
|
|
316 |
monochromeId
|
|
317 |
"return the id of the monochrome image on the device.
|
|
318 |
Return nil, if the image is unassigned."
|
|
319 |
|
|
320 |
monoDeviceForm isNil ifTrue:[^ nil].
|
|
321 |
^ monoDeviceForm id
|
|
322 |
!
|
|
323 |
|
|
324 |
fullColorId
|
|
325 |
"return the id of the full color image on the device.
|
|
326 |
Return nil, if the image is unassigned."
|
|
327 |
|
|
328 |
fullColorDeviceForm isNil ifTrue:[^ nil].
|
|
329 |
^ fullColorDeviceForm id
|
|
330 |
!
|
|
331 |
|
|
332 |
width
|
|
333 |
"return the width of the image"
|
|
334 |
|
|
335 |
^ width
|
|
336 |
!
|
|
337 |
|
|
338 |
width:aNumber
|
|
339 |
"set the width of the image"
|
|
340 |
|
|
341 |
width := aNumber
|
|
342 |
!
|
|
343 |
|
|
344 |
height
|
|
345 |
"return the height of the image"
|
|
346 |
|
|
347 |
^ height
|
|
348 |
!
|
|
349 |
|
|
350 |
height:aNumber
|
|
351 |
"set the height of the image"
|
|
352 |
|
|
353 |
height := aNumber
|
|
354 |
!
|
|
355 |
|
|
356 |
samplesPerPixel
|
|
357 |
"return the number of samples per pixel in the image.
|
|
358 |
The return value is an array of bits-per-plane."
|
|
359 |
|
|
360 |
^ samplesPerPixel
|
|
361 |
!
|
|
362 |
|
|
363 |
samplesPerPixel:aNumber
|
|
364 |
"set the array of samples per pixel"
|
|
365 |
|
|
366 |
samplesPerPixel := aNumber
|
|
367 |
!
|
|
368 |
|
|
369 |
depth
|
|
370 |
"return the depth of the image"
|
|
371 |
|
2
|
372 |
^ self bitsPerPixel
|
0
|
373 |
!
|
|
374 |
|
|
375 |
bitsPerSample
|
2
|
376 |
"return the number of bits per sample.
|
|
377 |
The return value is an array of bits-per-plane."
|
0
|
378 |
|
|
379 |
^ bitsPerSample
|
|
380 |
!
|
|
381 |
|
|
382 |
bitsPerSample:aNumber
|
|
383 |
bitsPerSample := aNumber
|
|
384 |
!
|
|
385 |
|
2
|
386 |
bitsPerPixel
|
|
387 |
"return the number of bits per pixel"
|
|
388 |
|
|
389 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i])
|
|
390 |
!
|
|
391 |
|
|
392 |
bitsPerRow
|
|
393 |
"return the number of bits in one scanline of the image"
|
|
394 |
|
|
395 |
^ width * (self bitsPerPixel).
|
|
396 |
!
|
|
397 |
|
|
398 |
bytesPerRow
|
|
399 |
"return the number of bytes in one scanline of the image"
|
|
400 |
|
|
401 |
|bitsPerRow bytesPerRow|
|
|
402 |
|
|
403 |
bitsPerRow := width * (self bitsPerPixel).
|
|
404 |
bytesPerRow := bitsPerRow // 8.
|
|
405 |
((bitsPerRow \\ 8) ~~ 0) ifTrue:[
|
|
406 |
bytesPerRow := bytesPerRow + 1
|
|
407 |
].
|
|
408 |
^ bytesPerRow
|
|
409 |
!
|
|
410 |
|
0
|
411 |
data
|
2
|
412 |
"for backward compatibility - will vanish"
|
|
413 |
|
|
414 |
^ bytes
|
|
415 |
!
|
|
416 |
|
|
417 |
data:aByteArray
|
|
418 |
"for backward compatibility - will vanish"
|
|
419 |
|
|
420 |
bytes := aByteArray
|
|
421 |
!
|
|
422 |
|
|
423 |
bits:aByteArray
|
|
424 |
"set the raw data"
|
|
425 |
|
|
426 |
bytes := aByteArray
|
|
427 |
!
|
|
428 |
|
|
429 |
bits
|
0
|
430 |
"return the raw image data; depending on the photometric,
|
|
431 |
this has to be interpreted as monochrome, greyscale,
|
|
432 |
palette or rgb data. It is also packed to be dense, so
|
|
433 |
a 4 bitPerSample palette image will store 2 pixels per byte."
|
|
434 |
|
2
|
435 |
^ bytes
|
0
|
436 |
!
|
|
437 |
|
|
438 |
photometric
|
|
439 |
"return the photometric, a symbol such as #palette, #rgb etc."
|
|
440 |
|
|
441 |
^ photometric
|
|
442 |
!
|
|
443 |
|
|
444 |
photometric:aSymbol
|
|
445 |
"set the photometric"
|
|
446 |
|
|
447 |
photometric := aSymbol
|
|
448 |
!
|
|
449 |
|
|
450 |
colorMap:anArrayOfArrays
|
|
451 |
colorMap := anArrayOfArrays
|
|
452 |
!
|
|
453 |
|
|
454 |
colorMap
|
|
455 |
"return the colormap"
|
|
456 |
|
|
457 |
^ colorMap
|
|
458 |
!
|
|
459 |
|
|
460 |
at:aPoint
|
2
|
461 |
"retrieve the pixel at aPoint; return a color.
|
|
462 |
Pixels start at 0@0 for upper left pixel, end at
|
|
463 |
width-1@height-1 for lower right pixel.
|
|
464 |
You should not use this method for image-processing, its
|
|
465 |
very slow ..."
|
0
|
466 |
|
|
467 |
^ self atX:aPoint x y:aPoint y
|
|
468 |
!
|
|
469 |
|
2
|
470 |
valueAt:aPoint
|
|
471 |
"retrieve the pixel at aPoint; return a pixel value.
|
|
472 |
Pixels start at 0@0 for upper left pixel, end at
|
|
473 |
width-1@height-1 for lower right pixel.
|
|
474 |
You should not use this method for image-processing, its
|
|
475 |
very slow ..."
|
|
476 |
|
|
477 |
^ self valueAtX:aPoint x y:aPoint y
|
|
478 |
!
|
|
479 |
|
0
|
480 |
atX:x y:y
|
2
|
481 |
"retrieve a pixel at x/y; return a color.
|
|
482 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
483 |
x = width-1, y=height-1 for lower right pixel.
|
|
484 |
You should not use this method for image-processing, its
|
|
485 |
very slow ..."
|
|
486 |
|
|
487 |
^ self subclassResponsibility
|
|
488 |
!
|
|
489 |
|
|
490 |
valueAtX:x y:y
|
|
491 |
"retrieve the pixel at aPoint; return a pixel value.
|
|
492 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
493 |
width-1@height-1 for lower right pixel.
|
|
494 |
You should not use this method for image-processing, its
|
|
495 |
very slow ..."
|
|
496 |
|
|
497 |
^ self subclassResponsibility
|
|
498 |
!
|
|
499 |
|
|
500 |
at:aPoint put:aColor
|
|
501 |
"set the pixel at aPoint to aColor.
|
|
502 |
Pixels start at 0@0 for upper left pixel, end at
|
|
503 |
width-1@height-1 for lower right pixel.
|
|
504 |
You should not use this method for image-processing, its
|
|
505 |
very slow ..."
|
|
506 |
|
|
507 |
^ self atX:aPoint x y:aPoint y put:aColor
|
|
508 |
!
|
|
509 |
|
|
510 |
atX:x y:y put:aColor
|
|
511 |
"set the pixel at x/y to aColor.
|
|
512 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
513 |
x = width-1, y=height-1 for lower right pixel.
|
|
514 |
You should not use this method for image-processing, its
|
|
515 |
very slow ..."
|
|
516 |
|
|
517 |
^ self subclassResponsibility
|
|
518 |
!
|
|
519 |
|
|
520 |
atX:x y:y putValue:aPixelValue
|
|
521 |
"set the pixel at x/y to aPixelValue.
|
|
522 |
Pixels start at x=0 , y=0 for upper left pixel, end at
|
|
523 |
x = width-1, y=height-1 for lower right pixel.
|
|
524 |
You should not use this method for image-processing, its
|
|
525 |
very slow ..."
|
|
526 |
|
|
527 |
^ self subclassResponsibility
|
|
528 |
!
|
|
529 |
|
|
530 |
atY:y from:x1 to:x2 do:aBlock
|
|
531 |
"perform aBlock for each pixel from x1 to x2 in row y.
|
|
532 |
The block is passed the color at each pixel.
|
|
533 |
This method allows slighly faster processing of an
|
|
534 |
image than using atX:y:, since some processing can be
|
|
535 |
avoided when going from pixel to pixel. However, for
|
|
536 |
real image processing, specialized methods should be written."
|
|
537 |
|
|
538 |
^ self subclassResponsibility.
|
0
|
539 |
! !
|
|
540 |
|
|
541 |
!Image methodsFor:'screen capture'!
|
|
542 |
|
|
543 |
fromScreen:aRectangle
|
|
544 |
"read an image from the display screen"
|
|
545 |
|
|
546 |
^ self fromScreen:aRectangle on:Display
|
|
547 |
!
|
|
548 |
|
|
549 |
fromScreen:aRectangle on:aDevice
|
|
550 |
"read an image from aDevices display screen.
|
|
551 |
Since I have no other displays, only the MonoChrome, StaticGrey
|
|
552 |
and PseudoColor cases have been tested ...
|
|
553 |
(especially True- and DirectColor may be wrong)"
|
|
554 |
|
|
555 |
|depth visType root
|
|
556 |
x "{ Class: SmallInteger }"
|
|
557 |
y "{ Class: SmallInteger }"
|
|
558 |
w "{ Class: SmallInteger }"
|
|
559 |
h "{ Class: SmallInteger }"
|
|
560 |
dstIndex "{ Class: SmallInteger }"
|
|
561 |
srcIndex "{ Class: SmallInteger }"
|
|
562 |
inData tmpData usedColors nUsed
|
|
563 |
rMap gMap bMap bitsPerPixel bytesPerLine
|
|
564 |
info bytesPerLineIn|
|
|
565 |
|
|
566 |
visType := aDevice visualType.
|
|
567 |
depth := aDevice depth.
|
|
568 |
(visType == #StaticGray) ifTrue:[
|
|
569 |
(aDevice blackpixel == 0) ifTrue:[
|
|
570 |
photometric := #blackIs0
|
|
571 |
] ifFalse:[
|
|
572 |
photometric := #whiteIs0
|
|
573 |
].
|
|
574 |
samplesPerPixel := 1.
|
|
575 |
bitsPerPixel := depth.
|
|
576 |
bitsPerSample := Array with:bitsPerPixel.
|
|
577 |
] ifFalse:[
|
|
578 |
((visType == #PseudoColor) or:[visType == #StaticColor]) ifTrue:[
|
|
579 |
photometric := #palette.
|
|
580 |
samplesPerPixel := 1.
|
|
581 |
bitsPerPixel := depth.
|
|
582 |
bitsPerSample := Array with:bitsPerPixel.
|
|
583 |
] ifFalse:[
|
|
584 |
((visType == #TrueColor) or:[visType == #DirectColor]) ifTrue:[
|
|
585 |
photometric := #rgb.
|
|
586 |
samplesPerPixel := 3.
|
|
587 |
bitsPerPixel := 24.
|
|
588 |
bitsPerSample := #(8 8 8)
|
|
589 |
] ifFalse:[
|
|
590 |
self error:'screen visual not supported'.
|
|
591 |
^ nil
|
|
592 |
]
|
|
593 |
]
|
|
594 |
].
|
|
595 |
|
|
596 |
"dont know yet, how display pads, assume worst case"
|
|
597 |
|
|
598 |
w := width := aRectangle width.
|
|
599 |
h := height := aRectangle height.
|
|
600 |
x := aRectangle left.
|
|
601 |
y := aRectangle top.
|
|
602 |
|
|
603 |
bytesPerLine := (w * bitsPerPixel + 31) // 8.
|
|
604 |
inData := ByteArray uninitializedNew:(bytesPerLine * height).
|
|
605 |
|
|
606 |
root := DisplayRootView on:aDevice.
|
|
607 |
info := aDevice getBitsFrom:root id x:x y:y width:w height:h into:inData.
|
|
608 |
|
|
609 |
"if the padding is not correct .."
|
|
610 |
bytesPerLineIn := (info at:3). "what I got"
|
|
611 |
bytesPerLine := (w * bitsPerPixel + 7) // 8. "what I want"
|
|
612 |
(bytesPerLine ~~ bytesPerLineIn) ifTrue:[
|
|
613 |
tmpData := inData.
|
|
614 |
inData := ByteArray uninitializedNew:(bytesPerLine * height).
|
|
615 |
srcIndex := 1.
|
|
616 |
dstIndex := 1.
|
|
617 |
1 to:height do:[:h |
|
|
618 |
inData replaceFrom:dstIndex to:(dstIndex + bytesPerLine - 1)
|
|
619 |
with:tmpData startingAt:srcIndex.
|
|
620 |
dstIndex := dstIndex + bytesPerLine.
|
|
621 |
srcIndex := srcIndex + bytesPerLineIn
|
|
622 |
]
|
|
623 |
].
|
|
624 |
|
|
625 |
"info printNewline. "
|
|
626 |
|
|
627 |
((visType == #StaticGray) or:[visType == #TrueColor]) ifTrue:[
|
|
628 |
"were done, the pixel values are the rgb/grey values"
|
2
|
629 |
bytes := inData.
|
0
|
630 |
^ self
|
|
631 |
].
|
|
632 |
|
|
633 |
"what we have now are the color numbers - still need the r/g/b values"
|
|
634 |
"find out, which colors are in the picture"
|
|
635 |
|
|
636 |
usedColors := inData usedValues.
|
|
637 |
nUsed := usedColors maximumValue + 1.
|
|
638 |
|
|
639 |
"get the palette"
|
|
640 |
rMap := Array new:nUsed.
|
|
641 |
gMap := Array new:nUsed.
|
|
642 |
bMap := Array new:nUsed.
|
|
643 |
usedColors do:[:colorIndex |
|
|
644 |
|i scale|
|
|
645 |
|
|
646 |
i := colorIndex + 1.
|
|
647 |
scale := 255.0 / 100.0.
|
|
648 |
aDevice getRGBFrom:colorIndex into:[:r :g :b |
|
|
649 |
rMap at:i put:(r * scale) rounded.
|
|
650 |
gMap at:i put:(g * scale) rounded.
|
|
651 |
bMap at:i put:(b * scale) rounded
|
|
652 |
]
|
|
653 |
].
|
|
654 |
colorMap := Array with:rMap with:gMap with:bMap.
|
2
|
655 |
bytes := inData.
|
0
|
656 |
|
|
657 |
"Image new fromScreen:((0 @ 0) corner:(100 @ 100)) on:Display"
|
|
658 |
"Image new fromScreen:((0 @ 0) corner:(500 @ 500)) on:Display"
|
|
659 |
! !
|
|
660 |
|
|
661 |
!Image methodsFor:'saving on file'!
|
|
662 |
|
|
663 |
saveOn:aFileName
|
|
664 |
FileFormats associationsDo:[:a |
|
|
665 |
(aFileName endsWith:(a key)) ifTrue:[
|
|
666 |
^ (a value) save:self onFile:aFileName
|
|
667 |
]
|
|
668 |
].
|
2
|
669 |
"no known extension - could ask user for the format here.
|
|
670 |
currently default to tiff format."
|
0
|
671 |
|
2
|
672 |
'unknown extension - could not figure out format - using tiff' printNewline.
|
|
673 |
^ self saveOn:aFileName using:TIFFReader
|
0
|
674 |
!
|
|
675 |
|
|
676 |
saveOn:aFileName using:readerClass
|
|
677 |
"save the receiver using the representation class"
|
|
678 |
|
|
679 |
readerClass save:self onFile:aFileName
|
|
680 |
|
|
681 |
"anImage saveOn:'myImage' using:TIFFReader"
|
|
682 |
! !
|
|
683 |
|
|
684 |
!Image methodsFor:'converting'!
|
|
685 |
|
|
686 |
on:aDevice
|
2
|
687 |
"make the image device dependent for aDevice"
|
|
688 |
|
0
|
689 |
((aDevice == device) and:[deviceForm notNil]) ifTrue:[^ self].
|
|
690 |
deviceForm := self asFormOn:aDevice.
|
|
691 |
device := aDevice
|
|
692 |
!
|
|
693 |
|
|
694 |
monochromeOn:aDevice
|
|
695 |
((aDevice == device) and:[monoDeviceForm notNil]) ifTrue:[^ self].
|
|
696 |
monoDeviceForm := self asMonochromeFormOn:aDevice.
|
|
697 |
device := aDevice
|
|
698 |
!
|
|
699 |
|
|
700 |
asFormOn:aDevice
|
|
701 |
"get a device form, with best possible approximation"
|
|
702 |
|
|
703 |
(photometric == #palette) ifTrue:[
|
|
704 |
^ self paletteImageAsFormOn:aDevice
|
|
705 |
].
|
|
706 |
|
|
707 |
(photometric == #rgb) ifTrue:[
|
|
708 |
^ self rgbImageAsFormOn:aDevice
|
|
709 |
].
|
|
710 |
|
|
711 |
^ self greyImageAsFormOn:aDevice
|
|
712 |
!
|
|
713 |
|
|
714 |
asMonochromeFormOn:aDevice
|
|
715 |
"get a monochrome device form"
|
|
716 |
|
|
717 |
(photometric == #palette) ifTrue:[
|
|
718 |
^ self paletteImageAsMonoFormOn:aDevice
|
|
719 |
].
|
|
720 |
|
|
721 |
(photometric == #rgb) ifTrue:[
|
|
722 |
^ self rgbImageAsMonoFormOn:aDevice
|
|
723 |
].
|
|
724 |
|
|
725 |
^ self greyImageAsMonoFormOn:aDevice
|
|
726 |
! !
|
|
727 |
|
|
728 |
!Image methodsFor:'converting rgb images'!
|
|
729 |
|
|
730 |
rgbImageAsFormOn:aDevice
|
|
731 |
"convert am rgb image to a device form form aDevice.
|
|
732 |
Return the device form."
|
|
733 |
|
|
734 |
|visual|
|
|
735 |
|
|
736 |
visual := aDevice visualType.
|
|
737 |
(visual == #StaticGray) ifTrue:[
|
|
738 |
^ self rgbImageAsGreyFormOn:aDevice
|
|
739 |
].
|
|
740 |
(visual == #TrueColor) ifTrue:[
|
|
741 |
^ self rgbImageAsTrueFormOn:aDevice
|
|
742 |
].
|
|
743 |
^ self rgbImageAsPseudoFormOn:aDevice
|
|
744 |
!
|
|
745 |
|
|
746 |
rgbImageAsGreyFormOn:aDevice
|
|
747 |
"convert an rgb image to a grey image for greyscale displays"
|
|
748 |
|
|
749 |
|deviceDepth|
|
|
750 |
|
|
751 |
deviceDepth := aDevice depth.
|
|
752 |
|
|
753 |
"I have specially tuned methods for monochrome"
|
|
754 |
(deviceDepth == 1) ifTrue:[
|
|
755 |
DitherAlgorithm == #error ifTrue:[
|
|
756 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice
|
|
757 |
].
|
|
758 |
DitherAlgorithm == #pattern ifTrue:[
|
|
759 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice
|
|
760 |
].
|
|
761 |
^ self rgbImageAsMonoFormOn:aDevice
|
|
762 |
].
|
|
763 |
|
|
764 |
"and for 2plane greyscale (i.e. NeXTs)"
|
|
765 |
(deviceDepth == 2) ifTrue:[
|
|
766 |
DitherAlgorithm == #error ifTrue:[
|
|
767 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice
|
|
768 |
].
|
|
769 |
DitherAlgorithm == #pattern ifTrue:[
|
|
770 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice
|
|
771 |
].
|
|
772 |
^ self rgbImageAs2PlaneFormOn:aDevice
|
|
773 |
].
|
|
774 |
|
|
775 |
(deviceDepth == 8) ifTrue:[
|
|
776 |
^ self rgbImageAs8BitGreyFormOn:aDevice
|
|
777 |
].
|
|
778 |
|
|
779 |
"mhmh need another converter ...
|
|
780 |
till then we do:"
|
|
781 |
DitherAlgorithm == #error ifTrue:[
|
|
782 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice
|
|
783 |
].
|
|
784 |
DitherAlgorithm == #pattern ifTrue:[
|
|
785 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice
|
|
786 |
].
|
|
787 |
^ self rgbImageAsMonoFormOn:aDevice
|
|
788 |
!
|
|
789 |
|
|
790 |
rgbImageAsMonoFormOn:aDevice
|
|
791 |
"return a 1-bit monochrome form for aDevice from the rgb picture,
|
|
792 |
using a threshold algorithm.
|
|
793 |
(i.e. grey value < 0.5 -> black, grey value >= 0.5 -> white)."
|
|
794 |
|
2
|
795 |
^ self subclassResponsibility
|
0
|
796 |
!
|
|
797 |
|
|
798 |
rgbImageAsPatternDitheredGreyFormOn:aDevice
|
|
799 |
"return a dithered greyForm for aDevice from the palette picture.
|
|
800 |
works for any destination depth.
|
|
801 |
A slow algorithm, using draw into the form (which indirectly does
|
|
802 |
the dither) - should be rewritten."
|
|
803 |
|
2
|
804 |
^ self subclassResponsibility
|
0
|
805 |
!
|
|
806 |
|
|
807 |
rgbImageAs2PlaneFormOn:aDevice
|
|
808 |
"return a 2-bit device form for aDevice from the rgb picture,
|
|
809 |
using a threshold algorithm.
|
|
810 |
(i.e. grey value < 0.25 -> black // 0.25..0.5 -> darkgrey //
|
|
811 |
0.5 .. 0.75 -> lightgrey // > 0.75 -> white)."
|
|
812 |
|
2
|
813 |
^ self subclassResponsibility
|
0
|
814 |
!
|
|
815 |
|
|
816 |
rgbImageAs8BitGreyFormOn:aDevice
|
|
817 |
"return an 8-bit greyForm from the rgb picture"
|
|
818 |
|
2
|
819 |
^ self subclassResponsibility
|
0
|
820 |
!
|
|
821 |
|
|
822 |
rgbImageAsPseudoFormOn:aDevice
|
|
823 |
"return a pseudocolor form from the rgb-picture"
|
|
824 |
|
2
|
825 |
^ self subclassResponsibility
|
0
|
826 |
! !
|
|
827 |
|
|
828 |
!Image methodsFor:'converting palette images'!
|
|
829 |
|
|
830 |
paletteImageAsFormOn:aDevice
|
|
831 |
(aDevice visualType == #StaticGray) ifTrue:[
|
|
832 |
(aDevice depth == 8) ifTrue:[
|
|
833 |
^ self paletteImageAsGreyFormOn:aDevice
|
|
834 |
].
|
|
835 |
|
|
836 |
DitherAlgorithm == #pattern ifTrue:[
|
|
837 |
^ self paletteImageAsPatternDitheredGreyFormOn:aDevice
|
|
838 |
].
|
|
839 |
|
|
840 |
(aDevice depth == 2) ifTrue:[
|
|
841 |
^ self paletteImageAs2PlaneFormOn:aDevice
|
|
842 |
].
|
|
843 |
|
|
844 |
^ self paletteImageAsMonoFormOn:aDevice
|
|
845 |
].
|
|
846 |
^ self paletteImageAsPseudoFormOn:aDevice
|
|
847 |
!
|
|
848 |
|
|
849 |
paletteImageAsMonoFormOn:aDevice
|
|
850 |
"return a 1-bit monoForm from the palette picture"
|
|
851 |
|
2
|
852 |
^ self subclassResponsibility
|
0
|
853 |
!
|
|
854 |
|
|
855 |
paletteImageAs2PlaneFormOn:aDevice
|
|
856 |
"return a 2-bit greyForm from the palette picture"
|
|
857 |
|
2
|
858 |
^ self subclassResponsibility
|
0
|
859 |
!
|
|
860 |
|
|
861 |
paletteImageAsPseudoFormOn:aDevice
|
2
|
862 |
"return a pseudoForm from the palette picture."
|
0
|
863 |
|
2
|
864 |
^ self subclassResponsibility
|
0
|
865 |
!
|
|
866 |
|
|
867 |
paletteImageAsGreyFormOn:aDevice
|
|
868 |
"return an 8-bit greyForm from the palette picture"
|
|
869 |
|
2
|
870 |
^ self subclassResponsibility
|
|
871 |
!
|
0
|
872 |
|
2
|
873 |
paletteImageAsPatternDitheredGreyFormOn:aDevice
|
|
874 |
"return a dithered greyForm from the palette picture."
|
0
|
875 |
|
2
|
876 |
^ self subclassResponsibility
|
0
|
877 |
! !
|
|
878 |
|
|
879 |
!Image methodsFor:'converting greyscale images'!
|
|
880 |
|
|
881 |
greyImageAsFormOn:aDevice
|
2
|
882 |
"return a thresholded greyForm from the grey picture."
|
|
883 |
|
0
|
884 |
|pictureDepth nPlanes f|
|
|
885 |
|
|
886 |
nPlanes := samplesPerPixel.
|
|
887 |
(nPlanes == 2) ifTrue:[
|
|
888 |
'alpha plane ignored' printNewline.
|
|
889 |
nPlanes := 1
|
|
890 |
].
|
|
891 |
|
|
892 |
pictureDepth := bitsPerSample at:1.
|
|
893 |
|
|
894 |
"monochrome is very easy ..."
|
|
895 |
|
|
896 |
(pictureDepth == 1) ifTrue:[
|
2
|
897 |
^ Form width:width height:height fromArray:bytes on:aDevice
|
0
|
898 |
].
|
|
899 |
|
|
900 |
(aDevice visualType == #StaticGray) ifTrue:[
|
|
901 |
(aDevice depth == pictureDepth) ifTrue:[
|
|
902 |
|
|
903 |
"greyscale is easy, if the depths match"
|
|
904 |
|
|
905 |
f := Form width:width height:height depth:pictureDepth on:aDevice.
|
|
906 |
f isNil ifTrue:[^ nil].
|
|
907 |
f initGC.
|
|
908 |
|
|
909 |
"if device has white at the opposite corner ..."
|
|
910 |
((aDevice blackpixel == 0) == (photometric == #blackIs0)) ifFalse:[
|
|
911 |
"have to invert bits"
|
|
912 |
f function:#copyInverted
|
|
913 |
].
|
2
|
914 |
aDevice drawBits:bytes depth:pictureDepth width:width height:height
|
0
|
915 |
x:0 y:0
|
|
916 |
into:(f id)
|
|
917 |
x:0 y:0 width:width height:height with:(f gcId).
|
|
918 |
^ f
|
|
919 |
].
|
|
920 |
|
|
921 |
"the image has more greylevels than the display - dither"
|
|
922 |
|
|
923 |
"
|
|
924 |
coming soon ...
|
|
925 |
DitherAlgorithm == #error ifTrue:[
|
|
926 |
^ self greyImageAsErrorDitheredGreyFormOn:aDevice
|
|
927 |
].
|
|
928 |
"
|
|
929 |
DitherAlgorithm == #pattern ifTrue:[
|
|
930 |
^ self greyImageAsPatternDitheredGreyFormOn:aDevice
|
|
931 |
].
|
|
932 |
|
|
933 |
"no dither, simply cut off information"
|
|
934 |
(aDevice depth == 1) ifTrue:[
|
|
935 |
"for monochrome, there is a special method to do this"
|
|
936 |
^ self greyImageAsMonoFormOn:aDevice
|
|
937 |
].
|
|
938 |
"the general case, will take as many bits from the image
|
|
939 |
as possible for the device"
|
|
940 |
^ self greyImageAsGreyFormOn:aDevice
|
|
941 |
].
|
|
942 |
|
|
943 |
|
|
944 |
(aDevice visualType == #PseudoColor) ifTrue:[
|
|
945 |
^ self greyImageAsPseudoFormOn:aDevice
|
|
946 |
].
|
|
947 |
|
|
948 |
self error:'cannot convert this format'.
|
|
949 |
^ nil
|
|
950 |
!
|
|
951 |
|
|
952 |
greyImageAsMonoFormOn:aDevice
|
2
|
953 |
"return a (thresholded) monochrome Form from the picture."
|
0
|
954 |
|
2
|
955 |
^ self subclassResponsibility
|
0
|
956 |
!
|
|
957 |
|
|
958 |
greyImageAsPatternDitheredGreyFormOn:aDevice
|
|
959 |
"return a dithered greyForm from the grey picture.
|
2
|
960 |
Works for any source/destination depths, but very very slow
|
|
961 |
since each pixel is processed individually.
|
|
962 |
Usually redefined by subclasses for more performance"
|
0
|
963 |
|
2
|
964 |
|f last
|
|
965 |
x0 "{Class: SmallInteger }"
|
|
966 |
w "{Class: SmallInteger }"
|
|
967 |
h "{Class: SmallInteger }"
|
|
968 |
v "{Class: SmallInteger }"
|
|
969 |
run "{Class: SmallInteger }" |
|
0
|
970 |
|
2
|
971 |
Transcript showCr:'slow dithering ..'. Transcript endEntry.
|
|
972 |
|
|
973 |
w := width - 1.
|
|
974 |
h := height - 1.
|
0
|
975 |
|
2
|
976 |
"draw each pixel using dither color (let others do the dithering)
|
|
977 |
although the code is simple, its very slow"
|
|
978 |
|
|
979 |
f := Form width:width height:height depth:aDevice depth on:aDevice.
|
0
|
980 |
f isNil ifTrue:[^ nil].
|
|
981 |
f initGC.
|
|
982 |
|
2
|
983 |
0 to:h do:[:dstY |
|
|
984 |
x0 := 0.
|
0
|
985 |
run := 0.
|
|
986 |
last := nil.
|
2
|
987 |
self atY:dstY from:0 to:w do:[:x :srcColor |
|
|
988 |
|dstColor|
|
0
|
989 |
|
2
|
990 |
srcColor ~~ last ifTrue:[
|
0
|
991 |
(run ~~ 0) ifTrue:[
|
2
|
992 |
f fillRectangleX:x0 y:dstY width:run height:1.
|
0
|
993 |
].
|
2
|
994 |
run := 0.
|
|
995 |
dstColor := Color grey:(srcColor greyIntensity).
|
|
996 |
f paint:dstColor.
|
|
997 |
last := srcColor.
|
|
998 |
x0 := x
|
0
|
999 |
].
|
2
|
1000 |
run := run + 1
|
0
|
1001 |
].
|
2
|
1002 |
f fillRectangleX:x0 y:dstY width:run height:1.
|
0
|
1003 |
].
|
|
1004 |
^ f
|
|
1005 |
!
|
|
1006 |
|
|
1007 |
greyImageAsGreyFormOn:aDevice
|
|
1008 |
"return an 8-bit Form from the grey picture"
|
|
1009 |
|
|
1010 |
|wideBits pictureDepth f map shift nplanes ncells
|
|
1011 |
inverse oldValue newValue
|
|
1012 |
shift2 shift3 shift4|
|
|
1013 |
|
|
1014 |
(aDevice depth == 8) ifFalse:[
|
|
1015 |
'non-8 plane displays not supported' printNewline.
|
|
1016 |
^ self greyImageAsMonoFormOn:aDevice
|
|
1017 |
].
|
|
1018 |
|
|
1019 |
pictureDepth := bitsPerSample at:1.
|
|
1020 |
|
|
1021 |
wideBits := ByteArray uninitializedNew:(width * height).
|
|
1022 |
|
|
1023 |
map := ByteArray uninitializedNew:(1 bitShift:pictureDepth).
|
|
1024 |
|
|
1025 |
"find the real number of server-planes"
|
|
1026 |
nplanes := 8.
|
|
1027 |
ncells := 256.
|
|
1028 |
[aDevice ncells < ncells] whileTrue:[
|
|
1029 |
nplanes := nplanes - 1.
|
|
1030 |
ncells := ncells // 2
|
|
1031 |
].
|
|
1032 |
|
|
1033 |
"prepare translation table"
|
|
1034 |
|
|
1035 |
shift := nplanes - pictureDepth.
|
|
1036 |
shift2 := shift - pictureDepth.
|
|
1037 |
shift3 := shift2 - pictureDepth.
|
|
1038 |
shift4 := shift3 - pictureDepth.
|
|
1039 |
|
|
1040 |
inverse := aDevice blackpixel ~~ 0.
|
|
1041 |
photometric == #blackIs0 ifFalse:[
|
|
1042 |
inverse := inverse not
|
|
1043 |
].
|
|
1044 |
1 to:(map size) do:[:index |
|
|
1045 |
oldValue := index - 1.
|
|
1046 |
newValue := oldValue bitShift:shift.
|
|
1047 |
newValue := newValue bitOr:(oldValue bitShift:shift2).
|
|
1048 |
newValue := newValue bitOr:(oldValue bitShift:shift3).
|
|
1049 |
newValue := newValue bitOr:(oldValue bitShift:shift4).
|
|
1050 |
inverse ifTrue:[
|
|
1051 |
map at:(map size - index + 1) put:newValue
|
|
1052 |
] ifFalse:[
|
|
1053 |
map at:index put:newValue
|
|
1054 |
]
|
|
1055 |
].
|
2
|
1056 |
bytes expandPixels:pictureDepth
|
0
|
1057 |
width:width
|
|
1058 |
height:height
|
|
1059 |
into:wideBits
|
|
1060 |
mapping:map.
|
|
1061 |
|
|
1062 |
f := Form width:width height:height depth:8 on:aDevice.
|
|
1063 |
f isNil ifTrue:[^ nil].
|
|
1064 |
f initGC.
|
|
1065 |
aDevice drawBits:wideBits depth:8 width:width height:height
|
|
1066 |
x:0 y:0
|
|
1067 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId).
|
|
1068 |
^ f
|
|
1069 |
!
|
|
1070 |
|
|
1071 |
greyImageAsPseudoFormOn:aDevice
|
|
1072 |
"return an 8-bit pseudo Form from the grey picture"
|
|
1073 |
|
|
1074 |
|wideBits pictureDepth f map
|
|
1075 |
colorMap usedColors nUsed aColor nColors range|
|
|
1076 |
|
|
1077 |
pictureDepth := bitsPerSample at:1.
|
|
1078 |
|
|
1079 |
(#(2 4 8) includes:pictureDepth) ifFalse:[
|
|
1080 |
self error:'currently only depth-2, 4 or 8 supported'.
|
|
1081 |
^ nil
|
|
1082 |
].
|
|
1083 |
|
|
1084 |
wideBits := ByteArray uninitializedNew:(width * height).
|
|
1085 |
|
|
1086 |
(pictureDepth == 8) ifTrue:[
|
|
1087 |
"for 8bits, we scan for used colors first;
|
|
1088 |
to avoid allocating too many colors"
|
|
1089 |
|
2
|
1090 |
usedColors := bytes usedValues.
|
0
|
1091 |
nUsed := usedColors maximumValue + 1.
|
|
1092 |
|
|
1093 |
colorMap := Array new:nUsed.
|
|
1094 |
photometric == #blackIs0 ifTrue:[
|
|
1095 |
usedColors do:[:grey |
|
|
1096 |
colorMap at:(grey + 1) put:(Color grey:(100.0 / 256.0 * grey))
|
|
1097 |
]
|
|
1098 |
] ifFalse:[
|
|
1099 |
usedColors do:[:grey |
|
|
1100 |
colorMap at:(grey + 1) put:(Color grey:(100 - (100.0 / 256.0 * grey)))
|
|
1101 |
]
|
|
1102 |
]
|
|
1103 |
] ifFalse:[
|
|
1104 |
nColors := (1 bitShift:pictureDepth).
|
|
1105 |
colorMap := Array new:nColors.
|
|
1106 |
range := 100 / (nColors - 1) asFloat.
|
|
1107 |
1 to:nColors do:[:i |
|
|
1108 |
colorMap at:i put:(Color grey:(i - 1) * range).
|
|
1109 |
].
|
|
1110 |
].
|
|
1111 |
|
|
1112 |
"XXX should reduce 8->6->4->2 planes, if not all colors could be allocated"
|
|
1113 |
|
|
1114 |
"setup the translation map"
|
|
1115 |
map := ByteArray uninitializedNew:256.
|
|
1116 |
1 to:(colorMap size) do:[:i |
|
|
1117 |
aColor := colorMap at:i.
|
|
1118 |
aColor notNil ifTrue:[
|
|
1119 |
map at:i put:(aColor on:aDevice) colorId
|
|
1120 |
]
|
|
1121 |
].
|
|
1122 |
|
|
1123 |
"expand & translate"
|
2
|
1124 |
bytes expandPixels:pictureDepth
|
0
|
1125 |
width:width
|
|
1126 |
height:height
|
|
1127 |
into:wideBits
|
|
1128 |
mapping:map.
|
|
1129 |
|
|
1130 |
f := Form width:width height:height depth:8 on:aDevice.
|
|
1131 |
f isNil ifTrue:[^ nil].
|
|
1132 |
f colorMap:colorMap.
|
|
1133 |
f initGC.
|
|
1134 |
aDevice drawBits:wideBits depth:8 width:width height:height
|
|
1135 |
x:0 y:0
|
|
1136 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId).
|
|
1137 |
^ f
|
|
1138 |
! !
|
|
1139 |
|
|
1140 |
!Image methodsFor:'image manipulations'!
|
|
1141 |
|
|
1142 |
magnifyBy:extent
|
|
1143 |
"return a new image magnified by extent, aPoint.
|
|
1144 |
If non-integral magnify is asked for, pass the work on to 'hardMagnifyBy:'"
|
|
1145 |
|
|
1146 |
|mX mY srcOffset dstOffset dstStep first
|
|
1147 |
newWidth newHeight newImage newBits bitsPerPixel newBytesPerRow oldBytesPerRow|
|
|
1148 |
|
|
1149 |
mX := extent x.
|
|
1150 |
mY := extent y.
|
|
1151 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil].
|
|
1152 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self].
|
|
1153 |
|
|
1154 |
((mX isKindOf:SmallInteger) and:[mY isKindOf:SmallInteger]) ifFalse:[
|
|
1155 |
^ self hardMagnifyBy:extent
|
|
1156 |
].
|
|
1157 |
|
2
|
1158 |
bitsPerPixel := self depth.
|
0
|
1159 |
oldBytesPerRow := ((width * bitsPerPixel) + 7) // 8.
|
|
1160 |
|
|
1161 |
newWidth := width * mX.
|
|
1162 |
newHeight := height * mY.
|
|
1163 |
newBytesPerRow := ((newWidth * bitsPerPixel) + 7) // 8.
|
|
1164 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * newHeight).
|
|
1165 |
|
|
1166 |
newImage := self species new.
|
2
|
1167 |
newImage bits:newBits.
|
0
|
1168 |
newImage width:newWidth.
|
|
1169 |
newImage height:newHeight.
|
|
1170 |
newImage photometric:photometric.
|
|
1171 |
newImage samplesPerPixel:samplesPerPixel.
|
|
1172 |
newImage bitsPerSample:bitsPerSample.
|
|
1173 |
newImage colorMap:colorMap copy.
|
|
1174 |
|
|
1175 |
mX = 1 ifTrue:[
|
|
1176 |
"expand rows only"
|
|
1177 |
srcOffset := 1.
|
|
1178 |
dstOffset := 1.
|
|
1179 |
|
|
1180 |
1 to:height do:[:row |
|
|
1181 |
1 to:mY do:[:i |
|
|
1182 |
newBits replaceFrom:dstOffset to:(dstOffset + oldBytesPerRow - 1)
|
2
|
1183 |
with:bytes startingAt:srcOffset.
|
0
|
1184 |
dstOffset := dstOffset + newBytesPerRow
|
|
1185 |
].
|
|
1186 |
srcOffset := srcOffset + oldBytesPerRow.
|
|
1187 |
].
|
|
1188 |
] ifFalse:[
|
|
1189 |
"expand cols"
|
|
1190 |
(mX > 1) ifTrue:[
|
|
1191 |
dstOffset := 1.
|
|
1192 |
srcOffset := 1.
|
|
1193 |
1 to:height do:[:row |
|
2
|
1194 |
self magnifyRowFrom:bytes offset:srcOffset
|
|
1195 |
into:newBits offset:dstOffset factor:mX.
|
0
|
1196 |
|
|
1197 |
first := dstOffset.
|
|
1198 |
dstOffset := dstOffset + newBytesPerRow.
|
2
|
1199 |
" and copy for row expansion "
|
0
|
1200 |
2 to:mY do:[:i |
|
|
1201 |
newBits replaceFrom:dstOffset to:(dstOffset + newBytesPerRow - 1)
|
|
1202 |
with:newBits startingAt:first.
|
|
1203 |
dstOffset := dstOffset + newBytesPerRow
|
|
1204 |
].
|
|
1205 |
srcOffset := srcOffset + oldBytesPerRow.
|
|
1206 |
].
|
|
1207 |
]
|
|
1208 |
].
|
|
1209 |
^ newImage
|
|
1210 |
|
|
1211 |
"((Image fromFile:'bitmaps/claus.gif') magnifyBy:1@2)"
|
2
|
1212 |
!
|
|
1213 |
|
|
1214 |
hardMagnifyBy:extent
|
|
1215 |
"return a new image magnified by extent, aPoint.
|
|
1216 |
This is the general magnification method, handling non-integral values"
|
|
1217 |
|
|
1218 |
|mX mY
|
|
1219 |
newWidth "{ Class: SmallInteger }"
|
|
1220 |
newHeight "{ Class: SmallInteger }"
|
|
1221 |
w "{ Class: SmallInteger }"
|
|
1222 |
h "{ Class: SmallInteger }"
|
|
1223 |
newImage newBits bitsPerPixel newBytesPerRow
|
|
1224 |
value srcRow|
|
|
1225 |
|
|
1226 |
mX := extent x.
|
|
1227 |
mY := extent y.
|
|
1228 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil].
|
|
1229 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self].
|
|
1230 |
|
|
1231 |
|
|
1232 |
newWidth := (width * mX) truncated.
|
|
1233 |
newHeight := (height * mY) truncated.
|
|
1234 |
|
|
1235 |
bitsPerPixel := self depth.
|
|
1236 |
newBytesPerRow := ((newWidth * bitsPerPixel) + 7) // 8.
|
|
1237 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * newHeight).
|
|
1238 |
|
|
1239 |
newImage := self species new.
|
|
1240 |
newImage bits:newBits.
|
|
1241 |
newImage width:newWidth.
|
|
1242 |
newImage height:newHeight.
|
|
1243 |
newImage photometric:photometric.
|
|
1244 |
newImage samplesPerPixel:samplesPerPixel.
|
|
1245 |
newImage bitsPerSample:bitsPerSample.
|
|
1246 |
newImage colorMap:colorMap copy.
|
|
1247 |
|
|
1248 |
"walk over destination image fetching pixels from source image"
|
|
1249 |
|
|
1250 |
w := newWidth - 1.
|
|
1251 |
h := newHeight - 1.
|
|
1252 |
|
|
1253 |
0 to:h do:[:row |
|
|
1254 |
srcRow := (row // mY).
|
|
1255 |
0 to:w do:[:col |
|
|
1256 |
value := self valueAtX:(col // mX) y:srcRow.
|
|
1257 |
newImage atX:col y:row putValue:value.
|
|
1258 |
]
|
|
1259 |
].
|
|
1260 |
|
|
1261 |
^ newImage
|
|
1262 |
|
|
1263 |
"((Image fromFile:'bitmaps/claus.gif') magnifyBy:0.5@0.5)"
|
0
|
1264 |
! !
|
|
1265 |
|
|
1266 |
!Image methodsFor:'private'!
|
|
1267 |
|
2
|
1268 |
magnifyRowFrom:srcBytes offset:srcStart pixels:oldPixels
|
|
1269 |
into:dstBytes offset:dstStart factor:mX
|
0
|
1270 |
|
2
|
1271 |
"magnify a single pixel row - can only magnify by integer factors,
|
|
1272 |
can only magnify 1,2,4,8 and 24 bit-per-pixel images. But this is done fast."
|
|
1273 |
|
|
1274 |
self subclassResponsibility
|
|
1275 |
! !
|
0
|
1276 |
|
2
|
1277 |
!Image methodsFor: 'binary storage'!
|
|
1278 |
|
|
1279 |
readBinaryContentsFrom: stream manager: manager
|
|
1280 |
"tell the newly restored Image about restoration"
|
|
1281 |
|
|
1282 |
super readBinaryContentsFrom: stream manager: manager.
|
|
1283 |
self restored
|
0
|
1284 |
! !
|