author | Claus Gittinger <cg@exept.de> |
Tue, 28 May 1996 21:12:57 +0200 | |
changeset 748 | 818c6312acd1 |
parent 746 | f5479d603f64 |
child 757 | 6c7dc7e05e93 |
permissions | -rw-r--r-- |
0 | 1 |
" |
6 | 2 |
COPYRIGHT (c) 1991 by Claus Gittinger |
72 | 3 |
All Rights Reserved |
0 | 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 |
|
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
14 |
instanceVariableNames:'bytes width height bitsPerSample samplesPerPixel colorMap |
305
4ece2bdf1011
handle StaticColor visual (OS/2 server has this)
Claus Gittinger <cg@exept.de>
parents:
286
diff
changeset
|
15 |
photometric device deviceForm monoDeviceForm fullColorDeviceForm' |
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
16 |
classVariableNames:'Lobby DitherAlgorithm NumberOfDitherColors |
305
4ece2bdf1011
handle StaticColor visual (OS/2 server has this)
Claus Gittinger <cg@exept.de>
parents:
286
diff
changeset
|
17 |
CollectGarbageWhenRunningOutOfColors FileFormats' |
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
18 |
poolDictionaries:'' |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
19 |
category:'Graphics-Images' |
0 | 20 |
! |
21 |
||
22 |
!Image class methodsFor:'documentation'! |
|
23 |
||
46 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1991 by Claus Gittinger |
|
72 | 27 |
All Rights Reserved |
46 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
0 | 38 |
documentation |
39 |
" |
|
46 | 40 |
this class provides (some time in the future) representation |
41 |
for all kinds of images (monochrome, greyscale and color) |
|
592 | 42 |
and will finally replace Form. |
622 | 43 |
For now (only ;-) depths of 1, 2, 4, 8, 16 and 24 are supported. |
592 | 44 |
|
45 |
An Image keeps all of its information in a device independent way, |
|
46 |
but may be associated to a device. |
|
47 |
The data held keeps all information which was originally present, |
|
48 |
even if the display-device has lower resolution. |
|
46 | 49 |
Therefore, it is possible to process and manipulate images without loosing |
50 |
color information - even on low color resolution displays. |
|
51 |
||
52 |
Usually, you get a device specific representation of the image by |
|
592 | 53 |
sending the |
54 |
'on:aDevice' |
|
55 |
message to it, which will create a (possibly dithered) device-form, |
|
56 |
representing the image using the currently available colors. |
|
46 | 57 |
|
286 | 58 |
In rare cases, an explicit monochrome representation of the image is needed |
46 | 59 |
(X servers take monochrome icons only), this can be created by sending |
592 | 60 |
it the message |
61 |
'monochromeOn:aDevice'. |
|
0 | 62 |
|
46 | 63 |
Also, it is planned to generate another hi-color resolution version, |
286 | 64 |
which uses its own colormap and allows the use of all 256 colors on an 8bit display |
65 |
(this is not currently implemented). |
|
66 |
||
67 |
An images pixel interpretation is controlled by the photometric instance variable |
|
68 |
and (if that is #palette) a colorMap. |
|
69 |
This is in contrast to the ST-80 way, where all of this info is in the colorMap |
|
70 |
(and using specialized colorMaps for b&w / greyScale images). |
|
71 |
This may change in future versions for more application compatibility. |
|
72 |
||
73 |
To convert pictures from/to external file-formats, image readers are used |
|
592 | 74 |
which have the file-format knowledge built in. |
75 |
There are readers for most common formats available |
|
286 | 76 |
(see ImageReader and especially subclasses such as TIFFReader, GIFReader etc.). |
77 |
||
78 |
File formats are handled by subclasses of ImageReader, which understand |
|
79 |
a specific format. You can add more readers, by adding an association |
|
80 |
such as ('.jpg' -> JPEGReader) to the class variable 'FileFormats' |
|
81 |
(see the classes #initialize method, which dsets up some default, and the |
|
82 |
patches/display.rc files, which add more). |
|
83 |
||
84 |
Some algorithms used here (especially dithering & color allocation) are |
|
46 | 85 |
experimental and far from being perfect (some are very slow). |
592 | 86 |
For now, the most common cases have been optimized and perform reasonably |
87 |
fast - however, with uncommon depth/visualType combinations, you may |
|
88 |
run into slower fallback methods ... |
|
89 |
||
90 |
Much more work is needed if you plan to do heavy image processing and will |
|
91 |
(may) be done in the future if there is a demand for it ... |
|
92 |
Dithering can be controlled by the DitherAlgorithm classVariable: |
|
2 | 93 |
|
46 | 94 |
DitherAlgorithm: |
0 | 95 |
|
46 | 96 |
nil a simple threshold algorithm |
592 | 97 |
(i.e. for mono, p<0.5 -> black, p>=0.5 -> white) |
0 | 98 |
|
46 | 99 |
#pattern patterned dither |
592 | 100 |
(for p, take dithered color to fill pixel; |
101 |
uses dithering in color-class) |
|
0 | 102 |
|
46 | 103 |
#error error diffusion dither (Floyd-Steinberg) |
592 | 104 |
planned - not yet implemented. |
105 |
||
106 |
||
107 |
||
108 |
[instance variables:] |
|
109 |
||
110 |
width <Integer> the width in pixels |
|
111 |
height <Integer> the height in pixels |
|
112 |
bytes <ByteArray> the full image information |
|
113 |
photometric <Symbol> #rgb, #palette, #blackIs0 or #whiteIs0 |
|
114 |
samplesPerPixel <Integer> the number of planes |
|
115 |
bitsPerSample <Array> the number of bits per plane |
|
116 |
||
117 |
colorMap <Array> only if photometric is #pseudocolor; |
|
118 |
maps pixel values to r/g/b values. |
|
119 |
||
120 |
device <Workstation> the device on which deviceForm, |
|
121 |
monoDeviceForm and lowResDeviceForm are |
|
122 |
||
123 |
deviceForm <Form> the device form which gives the best |
|
124 |
possible aproximation of the image on |
|
125 |
device using standard colors. |
|
126 |
||
127 |
monoDeviceForm <Form> the device form which gives a monochrome |
|
128 |
aproximation of the image on device. |
|
129 |
||
130 |
fullColorDeviceForm <Form> the device form which gives the best |
|
131 |
possible aproximation of the image on |
|
132 |
device using private colors. |
|
133 |
||
134 |
[class variables:] |
|
135 |
||
136 |
Lobby <Registry> keeps track of known images |
|
137 |
(for resource freeing with garbage collector) |
|
138 |
||
139 |
DitherAlgorithm <Symbol> defines how to dither |
|
140 |
||
141 |
NumberOfDitherColors <Integer> defines, how many dither colors to use |
|
142 |
||
143 |
FileFormats <Dictionary> associates filename extensions to |
|
144 |
image reader classes (now set-up in startup-file) |
|
145 |
||
146 |
CollectGarbageWhenRunningOutOfColors |
|
147 |
<Boolean> if true, and we run out of available |
|
148 |
device colors during creation of a |
|
149 |
device image, collect garbage for |
|
150 |
possible image reclamation. |
|
151 |
If false, proceed immediately. |
|
152 |
Default is true. |
|
158 | 153 |
|
46 | 154 |
todo: |
592 | 155 |
there is currently no mask stored/available in the image itself; currently masks |
156 |
have to be stored as separate bitmaps. |
|
157 |
(which is bad for image-file formats, which provide a mask) |
|
158 |
Thus, currently, all images are completely opaque. |
|
159 |
||
160 |
[See also:] |
|
161 |
Form Icon ImageReader |
|
611 | 162 |
|
163 |
[author:] |
|
164 |
Claus Gittinger |
|
0 | 165 |
" |
649 | 166 |
! |
167 |
||
168 |
examples |
|
169 |
" |
|
170 |
reading from a file (many formats are supported): |
|
171 |
[exBegin] |
|
172 |
(Image fromFile:'bitmaps/claus.gif') inspect |
|
173 |
[exEnd] |
|
174 |
[exBegin] |
|
175 |
(Image fromFile:'bitmaps/SmalltalkX.xbm') inspect |
|
176 |
[exEnd] |
|
177 |
[exBegin] |
|
178 |
(Image fromFile:'bitmaps/okSmily_up.bmp') inspect |
|
179 |
[exEnd] |
|
180 |
||
181 |
storing (only a few formats are currently supported): |
|
182 |
[exBegin] |
|
183 |
|img| |
|
184 |
||
185 |
img := Image fromFile:'bitmaps/okSmily_up.bmp'. |
|
186 |
img saveOn:'myImage.tiff'. |
|
187 |
(Image fromFile:'myImage.tiff') inspect |
|
188 |
[exEnd] |
|
189 |
magnifying (any factor): |
|
190 |
[exBegin] |
|
191 |
((Image fromFile:'bitmaps/claus.gif') |
|
192 |
magnifiedTo:(48@48)) |
|
193 |
inspect |
|
194 |
[exEnd] |
|
195 |
[exBegin] |
|
196 |
((Image fromFile:'bitmaps/claus.gif') |
|
197 |
magnifiedBy:0.7) |
|
198 |
inspect |
|
199 |
[exEnd] |
|
200 |
rotating (currently, only multiples of 90 degrees are supported): |
|
201 |
[exBegin] |
|
202 |
((Image fromFile:'bitmaps/claus.gif') |
|
203 |
rotated:90) |
|
204 |
inspect |
|
205 |
[exEnd] |
|
206 |
[exBegin] |
|
207 |
(((Image fromFile:'bitmaps/claus.gif') |
|
208 |
magnifiedBy:0.3@0.7) rotated:270) |
|
209 |
inspect |
|
210 |
[exEnd] |
|
211 |
" |
|
0 | 212 |
! ! |
213 |
||
283 | 214 |
!Image class methodsFor:'initialization'! |
215 |
||
216 |
fileFormats |
|
217 |
"return the collection of supported file formats. |
|
218 |
The returned dictionary maps file-extensions to image reader classes." |
|
219 |
||
220 |
FileFormats isNil ifTrue:[ |
|
221 |
self initializeFileFormatTable |
|
222 |
]. |
|
223 |
^ FileFormats |
|
224 |
||
225 |
" |
|
226 |
Image fileFormats |
|
227 |
" |
|
100 | 228 |
! |
229 |
||
283 | 230 |
flushDeviceImages |
231 |
"simply unassign all pictures from their device" |
|
232 |
||
233 |
Lobby do:[:anImage | |
|
234 |
anImage restored |
|
235 |
] |
|
0 | 236 |
! |
237 |
||
238 |
initialize |
|
2 | 239 |
"initialize class constants" |
240 |
||
0 | 241 |
"setup tracker of known pictures" |
24 | 242 |
Lobby isNil ifTrue:[ |
748 | 243 |
Lobby := Registry new. |
244 |
ObjectMemory addDependent:self. |
|
245 |
||
246 |
"define algorithm to use for dithering - currently only nil or #pattern supported" |
|
247 |
DitherAlgorithm := #pattern. "will be changed to error as soon as implemented" |
|
248 |
(Display notNil and:[Display hasGrayscales]) ifFalse:[ |
|
249 |
NumberOfDitherColors := 64 |
|
250 |
] ifTrue:[ |
|
251 |
"as far as I remember, this is about the number of grey values, the eye can distinguish" |
|
252 |
NumberOfDitherColors := 100 |
|
253 |
]. |
|
254 |
||
255 |
"define reader classes" |
|
256 |
FileFormats isNil ifTrue:[ |
|
257 |
self initializeFileFormatTable |
|
258 |
]. |
|
259 |
||
260 |
CollectGarbageWhenRunningOutOfColors := false |
|
386 | 261 |
] |
748 | 262 |
|
263 |
"Modified: 28.5.1996 / 21:12:30 / cg" |
|
12 | 264 |
! |
265 |
||
24 | 266 |
initializeFileFormatTable |
46 | 267 |
"initialize a default table to map from file extension to reader class. |
268 |
The mapping here is a default needed for proper operation of ST/X; |
|
269 |
see the 'smalltalk.rc' startup file for a real (full) map." |
|
24 | 270 |
|
271 |
FileFormats := Dictionary new. |
|
272 |
FileFormats at:'.xbm' put:XBMReader. |
|
273 |
FileFormats at:'.tiff' put:TIFFReader. |
|
274 |
FileFormats at:'.gif' put:GIFReader. |
|
72 | 275 |
"/ FileFormats at:'.img' put:IMGReader. |
276 |
"/ FileFormats at:'.icon' put:SunRasterReader. |
|
24 | 277 |
|
72 | 278 |
" |
279 |
Image initializeFileFormatTable |
|
280 |
" |
|
24 | 281 |
! |
282 |
||
0 | 283 |
update:something |
284 |
"flush all device specific stuff when restarted from a snapshot" |
|
285 |
||
286 |
(something == #restarted) ifTrue:[ |
|
72 | 287 |
self flushDeviceImages |
0 | 288 |
] |
289 |
! ! |
|
290 |
||
283 | 291 |
!Image class methodsFor:'instance creation'! |
292 |
||
293 |
extent:ext depth:d bits:bits |
|
294 |
"ST-80 compatibility; assume 32-bit padding" |
|
295 |
||
296 |
^ self extent:ext depth:d bits:bits pad:32 |
|
24 | 297 |
! |
298 |
||
283 | 299 |
extent:ext depth:d bits:bits pad:padding |
300 |
"ST-80 compatibility" |
|
301 |
||
302 |
^ self width:ext x height:ext y depth:d fromArray:bits pad:padding |
|
24 | 303 |
! |
304 |
||
283 | 305 |
extent:ext depth:d palette:aColormap bits:bits pad:padding |
306 |
"ST-80 compatibility" |
|
307 |
||
308 |
^ self width:ext x height:ext y depth:d fromArray:bits pad:padding |
|
309 |
! |
|
310 |
||
311 |
extent:ext fromArray:bits offset:offset |
|
312 |
"ST-80 compatibility" |
|
313 |
||
314 |
^ self width:ext x height:ext y fromArray:bits |
|
89 | 315 |
! |
316 |
||
317 |
fromForm:aForm |
|
318 |
"create & return an Image given a form" |
|
319 |
||
320 |
|cls| |
|
321 |
||
322 |
cls := self. |
|
323 |
cls == Image ifTrue:[ |
|
324 |
cls := self implementorForDepth:aForm depth |
|
325 |
]. |
|
326 |
^ (cls new) fromForm:aForm. |
|
327 |
||
328 |
" |
|
329 |
|f| |
|
330 |
||
331 |
f := Form width:16 height:16. |
|
332 |
f clear. |
|
333 |
f displayLineFromX:0 y:0 toX:15 y:15. |
|
334 |
f inspect. |
|
335 |
(Image fromForm:f) inspect |
|
336 |
" |
|
337 |
! |
|
338 |
||
339 |
fromImage:anImage |
|
340 |
"create & return an Image given another image. This can be used to |
|
341 |
convert an image to another depth." |
|
342 |
||
343 |
(self == Image or:[anImage class == self]) ifTrue:[^ anImage]. |
|
344 |
^ self new fromImage:anImage. |
|
345 |
||
346 |
" |
|
347 |
|i1 i8| |
|
348 |
||
349 |
i1 := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
350 |
i8 := Depth8Image fromImage:i1. |
|
351 |
i8 inspect |
|
352 |
" |
|
353 |
! |
|
354 |
||
191 | 355 |
fromSubImage:anImage in:aRectangle |
356 |
"create & return an Image from a rectangular area in another image. |
|
357 |
This can also be used to get a subimage in another depth." |
|
358 |
||
359 |
^ self new fromSubImage:anImage in:aRectangle. |
|
360 |
||
361 |
" |
|
362 |
|i1 i8| |
|
363 |
||
364 |
i1 := Image fromFile:'bitmaps/garfield.gif'. |
|
365 |
i8 := Depth8Image fromSubImage:i1 in:(0@0 corner:20@20). |
|
366 |
i8 inspect |
|
367 |
" |
|
650 | 368 |
" |
369 |
|i1 i8| |
|
370 |
||
371 |
i1 := Image fromFile:'bitmaps/claus.gif'. |
|
372 |
i8 := Depth8Image fromSubImage:i1 in:(70@50 extent:50@50). |
|
373 |
i8 inspect |
|
374 |
" |
|
191 | 375 |
|
376 |
"Created: 20.9.1995 / 01:05:43 / claus" |
|
650 | 377 |
"Modified: 7.5.1996 / 19:28:17 / cg" |
191 | 378 |
! |
379 |
||
283 | 380 |
new |
381 |
"create a new image. Redefined to set the photometric to |
|
382 |
greyScale with black being 0 as default." |
|
383 |
||
384 |
^ super new photometric:#blackIs0 |
|
385 |
! |
|
386 |
||
89 | 387 |
width:w height:h |
388 |
"create a new image, given width, height. Assume a depth of 1." |
|
389 |
||
390 |
|cls| |
|
391 |
||
392 |
cls := self. |
|
393 |
cls == Image ifTrue:[ |
|
394 |
cls := self implementorForDepth:1 |
|
395 |
]. |
|
396 |
^ cls new width:w height:h depth:1 |
|
397 |
! |
|
398 |
||
82 | 399 |
width:w height:h depth:d |
89 | 400 |
"create a new image, given width, height and depth" |
401 |
||
191 | 402 |
^ (self implementorForDepth:d) new |
403 |
width:w height:h depth:d |
|
82 | 404 |
! |
405 |
||
406 |
width:w height:h depth:d fromArray:pixelData |
|
89 | 407 |
"create a new image, given width, height, depth and data. |
408 |
Data must be a ByteArray containing correctly aligned bits for the specified |
|
134 | 409 |
depth (8-bit padded)." |
82 | 410 |
|
411 |
^ (self implementorForDepth:d) new width:w height:h depth:d fromArray:pixelData |
|
412 |
||
413 |
" |
|
414 |
Image width:8 |
|
415 |
height:8 |
|
416 |
depth:1 |
|
89 | 417 |
fromArray:#[2r11001100 |
82 | 418 |
2r00110011 |
419 |
2r11001100 |
|
420 |
2r00110011 |
|
421 |
2r11001100 |
|
422 |
2r00110011 |
|
423 |
2r11001100 |
|
89 | 424 |
2r00110011]. |
82 | 425 |
" |
426 |
||
427 |
" |
|
428 |
Image width:8 |
|
429 |
height:8 |
|
430 |
depth:2 |
|
89 | 431 |
fromArray:#[4r1100 4r1100 |
82 | 432 |
4r0011 4r0011 |
433 |
4r1100 4r1100 |
|
434 |
4r0011 4r0011 |
|
435 |
4r1100 4r1100 |
|
436 |
4r0011 4r0011 |
|
437 |
4r1100 4r1100 |
|
89 | 438 |
4r0011 4r0011]. |
439 |
" |
|
440 |
||
441 |
" |
|
442 |
Image width:8 |
|
443 |
height:8 |
|
444 |
depth:4 |
|
445 |
fromArray:#[4r0001 4r0001 |
|
446 |
4r0011 4r0011 |
|
447 |
4r1100 4r1100 |
|
448 |
4r0011 4r0011 |
|
449 |
4r1100 4r1100 |
|
450 |
4r0011 4r0011 |
|
451 |
4r1100 4r1100 |
|
452 |
4r0011 4r0011]. |
|
82 | 453 |
" |
132 | 454 |
! |
455 |
||
134 | 456 |
width:w height:h depth:d fromArray:pixelData pad:padding |
457 |
"create a new image, given width, height, depth and data. |
|
458 |
Data must be a ByteArray containing correctly aligned bits for the specified |
|
459 |
depth." |
|
460 |
||
461 |
|img newBits srcRowBytes dstRowBytes srcIndex dstIndex| |
|
462 |
||
463 |
img := (self implementorForDepth:d) new width:w height:h depth:d . |
|
464 |
||
465 |
padding ~~ 8 ifTrue:[ |
|
466 |
"must repad; ST/X uses byte padding, while ST-80 uses longword |
|
467 |
padding. This is stupid, and may be changed in ST/X with future versions. |
|
468 |
" |
|
469 |
dstRowBytes := img bytesPerRow. |
|
144 | 470 |
srcRowBytes := ((w * d + padding - 1) bitShift:-5) bitShift:2. |
134 | 471 |
|
472 |
newBits := ByteArray new:(dstRowBytes * h). |
|
473 |
srcIndex := 1. |
|
474 |
dstIndex := 1. |
|
475 |
||
476 |
1 to:h do:[:row | |
|
477 |
newBits replaceFrom:dstIndex |
|
478 |
to:(dstIndex + dstRowBytes - 1) |
|
479 |
with:pixelData |
|
480 |
startingAt:srcIndex. |
|
481 |
srcIndex := srcIndex + srcRowBytes. |
|
482 |
dstIndex := dstIndex + dstRowBytes. |
|
483 |
]. |
|
484 |
] ifFalse:[ |
|
485 |
newBits := pixelData |
|
486 |
]. |
|
487 |
img bits:newBits. |
|
488 |
^ img |
|
489 |
! |
|
490 |
||
283 | 491 |
width:w height:h fromArray:anArray |
492 |
"create a new image, given width, height. Assume a depth of 1 of the |
|
493 |
receiving class is Image. |
|
494 |
Data must be a ByteArray containing correctly aligned bits for depth 1 |
|
495 |
(i.e. 8 bits per byte)." |
|
496 |
||
497 |
|cls d| |
|
498 |
||
499 |
cls := self. |
|
500 |
cls == Image ifTrue:[ |
|
501 |
cls := self implementorForDepth:1. |
|
502 |
d := 1. |
|
503 |
] ifFalse:[ |
|
504 |
d := cls imageDepth |
|
505 |
]. |
|
506 |
^ cls new width:w height:h depth:d fromArray:anArray |
|
191 | 507 |
|
508 |
" |
|
283 | 509 |
Image width:8 |
510 |
height:8 |
|
511 |
fromArray:#[2r11001100 |
|
512 |
2r00110011 |
|
513 |
2r11001100 |
|
514 |
2r00110011 |
|
515 |
2r11001100 |
|
516 |
2r00110011 |
|
517 |
2r11001100 |
|
518 |
2r00110011]. |
|
191 | 519 |
" |
283 | 520 |
! ! |
521 |
||
650 | 522 |
!Image class methodsFor:'file reading'! |
523 |
||
524 |
fromFile:aFileName |
|
525 |
"read an image from a file - this methods tries to find |
|
526 |
out the file format itself (by the extension and by contents) |
|
527 |
and lets the appropriate reader read the file." |
|
528 |
||
529 |
|image name nm inStream suffixLen| |
|
530 |
||
531 |
" |
|
532 |
before trying each reader, check if file is readable |
|
533 |
" |
|
534 |
name := aFileName. |
|
535 |
inStream := Smalltalk systemFileStreamFor:name. |
|
536 |
inStream isNil ifTrue:[ |
|
537 |
inStream := Smalltalk bitmapFileStreamFor:name. |
|
538 |
inStream isNil ifTrue:[ |
|
539 |
('IMAGE: ' , aFileName , ' does not exist or is not readable') infoPrintNL. |
|
540 |
^ nil |
|
541 |
]. |
|
542 |
name := 'bitmaps/' , name. |
|
543 |
]. |
|
544 |
inStream close. |
|
545 |
||
546 |
" |
|
547 |
get the imageReader class from the files extension |
|
548 |
" |
|
549 |
nm := name. |
|
550 |
(name endsWith:'.Z') ifTrue:[ |
|
551 |
suffixLen := 2 |
|
552 |
] ifFalse:[ |
|
553 |
(name endsWith:'.gz') ifTrue:[ |
|
554 |
suffixLen := 3 |
|
555 |
] ifFalse:[ |
|
556 |
suffixLen := 0 |
|
557 |
] |
|
558 |
]. |
|
559 |
suffixLen ~~ 0 ifTrue:[ |
|
560 |
nm := name copyWithoutLast:suffixLen |
|
561 |
]. |
|
562 |
||
563 |
" |
|
564 |
ask the corresponding readerclass first |
|
565 |
" |
|
566 |
FileFormats keysAndValuesDo:[:suffix :readerClass | |
|
567 |
(nm endsWith:suffix) ifTrue:[ |
|
568 |
readerClass notNil ifTrue:[ |
|
569 |
image := readerClass fromFile:name. |
|
570 |
image notNil ifTrue:[^ image]. |
|
571 |
] |
|
572 |
] |
|
573 |
]. |
|
574 |
||
575 |
" |
|
576 |
no known extension - ask all readers if they know |
|
577 |
this format ... |
|
578 |
" |
|
579 |
FileFormats do:[:readerClass | |
|
580 |
readerClass notNil ifTrue:[ |
|
581 |
(readerClass isValidImageFile:name) ifTrue:[ |
|
582 |
^ readerClass fromFile:name |
|
583 |
] |
|
584 |
] |
|
585 |
]. |
|
586 |
||
587 |
" |
|
588 |
nope - unknown format |
|
589 |
" |
|
590 |
'IMAGE: unknown image file format: ' infoPrint. aFileName infoPrintNL. |
|
591 |
^ nil |
|
592 |
||
593 |
" |
|
594 |
Image fromFile:'bitmaps/dano.tiff' |
|
595 |
Image fromFile:'bitmaps/test.fax' |
|
596 |
Image fromFile:'bitmaps/voice.tiff' |
|
597 |
Image fromFile:'voice.tiff' |
|
598 |
||
599 |
Image fromFile:'../fileIn/bitmaps/claus.gif' |
|
600 |
Image fromFile:'../fileIn/bitmaps/garfield.gif' |
|
601 |
||
602 |
Image fromFile:'../fileIn/bitmaps/founders.im8' |
|
603 |
Image fromFile:'../goodies/faces/next.com/steve.face' |
|
604 |
||
605 |
Image fromFile:'/LocalLibrary/Images/OS2/dos3.ico' |
|
606 |
Image fromFile:'bitmaps/globe1.xbm' |
|
607 |
Image fromFile:'bitmaps/globe1.xbm.Z' |
|
608 |
Image fromFile:'bitmaps/hello_world.icon' |
|
609 |
" |
|
610 |
||
611 |
"Modified: 7.3.1996 / 19:18:13 / cg" |
|
612 |
! ! |
|
613 |
||
283 | 614 |
!Image class methodsFor:'misc'! |
615 |
||
616 |
dither:aSymbol |
|
617 |
"define how to dither - #pattern, #error or none; |
|
618 |
error diffusion dithering is currently not implemented, |
|
619 |
pattern dither is currently very slow." |
|
620 |
||
621 |
DitherAlgorithm := aSymbol |
|
622 |
||
623 |
"Image dither:#pattern" |
|
624 |
"Image dither:#error" |
|
625 |
"Image dither:nil" |
|
626 |
! |
|
627 |
||
628 |
numberOfDitherColors:n |
|
629 |
"define how many colors (i.e. patterns) to use when |
|
630 |
doing a pattern dither" |
|
631 |
||
632 |
NumberOfDitherColors := n |
|
633 |
! ! |
|
634 |
||
635 |
!Image class methodsFor:'queries'! |
|
636 |
||
637 |
imageDepth |
|
579 | 638 |
"return the depth of images represented by instances of |
639 |
this class. |
|
640 |
Must be redefined in concrete subclasses" |
|
641 |
||
642 |
^ self shouldNotImplement |
|
643 |
||
644 |
"Modified: 20.4.1996 / 23:40:41 / cg" |
|
191 | 645 |
! |
646 |
||
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
647 |
implementorForDepth:depth |
283 | 648 |
"return the class, which best implements images of depth" |
649 |
||
650 |
depth == 1 ifTrue:[^ Depth1Image]. |
|
651 |
depth == 2 ifTrue:[^ Depth2Image]. |
|
652 |
depth == 4 ifTrue:[^ Depth4Image]. |
|
653 |
depth == 8 ifTrue:[^ Depth8Image]. |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
654 |
depth == 15 ifTrue:[^ Depth16Image]. "/ kludge for 15bit XFree |
283 | 655 |
depth == 16 ifTrue:[^ Depth16Image]. |
656 |
depth == 24 ifTrue:[^ Depth24Image]. |
|
657 |
^ self |
|
658 |
! ! |
|
659 |
||
660 |
!Image class methodsFor:'screen capture'! |
|
661 |
||
662 |
fromScreen |
|
663 |
"return an image of the full screen" |
|
664 |
||
665 |
|display| |
|
666 |
||
667 |
display := Screen current. |
|
668 |
^ self fromScreen:(0@0 corner:(display width @ display height)) |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
669 |
|
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
670 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
671 |
Image fromScreen |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
672 |
" |
283 | 673 |
! |
674 |
||
675 |
fromScreen:aRectangle |
|
676 |
"return an image of a part of the screen" |
|
677 |
||
678 |
^ self fromScreen:aRectangle on:Screen current |
|
679 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
680 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
681 |
Image fromScreen:(0@0 corner:100@100) |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
682 |
" |
283 | 683 |
! |
684 |
||
685 |
fromScreen:aRectangle on:aDisplay |
|
686 |
"return an image of a part of the screen, which may be on |
|
687 |
another Display." |
|
688 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
689 |
|depth vis img| |
283 | 690 |
|
691 |
depth := aDisplay depth. |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
692 |
|
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
693 |
"/ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
694 |
"/ for truecolor displays, return a Depth24Image |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
695 |
"/ (must do this for depth15 & depth16 displays, since |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
696 |
"/ Depth16Image has no way to specify r/g/b masks ...) |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
697 |
"/ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
698 |
vis := aDisplay visualType. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
699 |
(vis == #TrueColor or:[vis == #DirectColor]) ifTrue:[ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
700 |
depth > 8 ifTrue:[ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
701 |
depth := 24. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
702 |
] |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
703 |
]. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
704 |
|
283 | 705 |
img := (self implementorForDepth: depth) new. |
706 |
^ img fromScreen:aRectangle on:aDisplay |
|
707 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
708 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
709 |
Image fromScreen:(0@0 corner:100@100) |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
710 |
" |
283 | 711 |
! |
712 |
||
713 |
fromScreenArea |
|
714 |
"return an image of a part of the screen; |
|
715 |
let user specify screen area." |
|
716 |
||
717 |
^ self fromScreen:(Rectangle fromUser) |
|
718 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
719 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
720 |
Image fromScreenArea |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
721 |
" |
283 | 722 |
! |
723 |
||
724 |
fromUser |
|
725 |
"return an image of a part of the screen; let user specify screen area. |
|
726 |
Same as fromScreenArea, for ST-80 compatibility" |
|
727 |
||
728 |
^ self fromScreenArea |
|
729 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
730 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
731 |
Image fromUser |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
732 |
" |
283 | 733 |
! |
734 |
||
735 |
fromView:aView |
|
736 |
"return an image taken from a views contents as currently |
|
737 |
on the screen. The returned image has the same depth and photometric |
|
738 |
as the Display. Notice, that for invisible or partial covered |
|
739 |
views, the returned Image is NOT correct. You may want to raise |
|
740 |
the view before using this method." |
|
741 |
||
742 |
|org dev| |
|
743 |
||
744 | 744 |
dev := aView graphicsDevice. |
283 | 745 |
org := dev translatePoint:(0@0) |
744 | 746 |
from:(aView id) |
747 |
to:dev rootWindowId. |
|
283 | 748 |
^ self fromScreen:(org extent:aView extent) on:dev |
191 | 749 |
|
750 |
" |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
751 |
Image fromView:(NewLauncher allInstances first topView) |
283 | 752 |
Image fromView:(SystemBrowser allInstances first topView) |
191 | 753 |
" |
744 | 754 |
|
755 |
"Modified: 28.5.1996 / 20:23:32 / cg" |
|
157 | 756 |
! ! |
757 |
||
0 | 758 |
!Image methodsFor:'accessing'! |
759 |
||
592 | 760 |
bitsPerSample |
761 |
"return the number of bits per sample. |
|
762 |
The return value is an array of bits-per-plane." |
|
763 |
||
764 |
^ bitsPerSample |
|
765 |
! |
|
766 |
||
767 |
colorMap |
|
768 |
"return the colormap" |
|
769 |
||
770 |
^ colorMap |
|
771 |
! |
|
772 |
||
773 |
colorMap:anArrayOfArrays |
|
774 |
"set the colorMap" |
|
775 |
||
776 |
colorMap := anArrayOfArrays. |
|
777 |
photometric isNil ifTrue:[photometric := #palette]. |
|
778 |
deviceForm notNil ifTrue:[self release] |
|
779 |
||
780 |
"Modified: 31.8.1995 / 03:05:59 / claus" |
|
781 |
"Modified: 23.4.1996 / 10:57:32 / cg" |
|
782 |
! |
|
783 |
||
784 |
depth |
|
785 |
"return the depth of the image" |
|
786 |
||
787 |
^ self bitsPerPixel |
|
788 |
! |
|
789 |
||
790 |
device |
|
791 |
"return the device, the receiver is associated with. |
|
792 |
Return nil, if the image is unassigned." |
|
793 |
||
794 |
^ device |
|
795 |
! |
|
796 |
||
797 |
extent |
|
798 |
"return the images extent" |
|
799 |
||
800 |
^ width@height |
|
801 |
! |
|
802 |
||
803 |
fullColorId |
|
804 |
"return the id of the full color image on the device. |
|
805 |
Return nil, if the image is unassigned." |
|
806 |
||
807 |
fullColorDeviceForm isNil ifTrue:[^ nil]. |
|
808 |
^ fullColorDeviceForm id |
|
809 |
! |
|
810 |
||
728 | 811 |
graphicsDevice |
812 |
"same as #device - for ST-80 compatibility" |
|
813 |
||
814 |
^ device |
|
815 |
! |
|
744 | 816 |
|
592 | 817 |
height |
818 |
"return the height of the image" |
|
819 |
||
820 |
^ height |
|
821 |
! |
|
822 |
||
823 |
id |
|
824 |
"return the id of the image on the device. |
|
825 |
Return nil, if the image is unassigned." |
|
826 |
||
827 |
deviceForm isNil ifTrue:[^ nil]. |
|
828 |
^ deviceForm id |
|
829 |
! |
|
830 |
||
831 |
monochromeId |
|
832 |
"return the id of the monochrome image on the device. |
|
833 |
Return nil, if the image is unassigned." |
|
834 |
||
835 |
monoDeviceForm isNil ifTrue:[^ nil]. |
|
836 |
^ monoDeviceForm id |
|
837 |
! |
|
838 |
||
839 |
palette |
|
840 |
"return the colormap; ST-80 compatibility" |
|
841 |
||
842 |
^ colorMap |
|
843 |
! |
|
844 |
||
845 |
palette:aColormap |
|
846 |
"set the colormap; ST-80 compatibility" |
|
847 |
||
848 |
colorMap := aColormap |
|
849 |
||
850 |
"Created: 1.2.1996 / 15:09:25 / cg" |
|
851 |
! |
|
852 |
||
853 |
photometric |
|
854 |
"return the photometric, a symbol such as #palette, #rgb etc." |
|
855 |
||
856 |
^ photometric |
|
857 |
! |
|
858 |
||
859 |
samplesPerPixel |
|
860 |
"return the number of samples per pixel in the image. |
|
861 |
The return value is an array of bits-per-plane." |
|
862 |
||
863 |
^ samplesPerPixel |
|
864 |
! |
|
865 |
||
866 |
width |
|
867 |
"return the width of the image" |
|
868 |
||
869 |
^ width |
|
870 |
! ! |
|
871 |
||
872 |
!Image methodsFor:'accessing - pixels'! |
|
873 |
||
0 | 874 |
at:aPoint |
2 | 875 |
"retrieve the pixel at aPoint; return a color. |
876 |
Pixels start at 0@0 for upper left pixel, end at |
|
81 | 877 |
(width-1)@(height-1) for lower right pixel. |
2 | 878 |
You should not use this method for image-processing, its |
81 | 879 |
very slow ... |
880 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
0 | 881 |
|
882 |
^ self atX:aPoint x y:aPoint y |
|
883 |
! |
|
884 |
||
283 | 885 |
at:aPoint put:aColor |
886 |
"set the pixel at aPoint to aColor. |
|
887 |
Pixels start at 0@0 for the upper left pixel, end at |
|
888 |
(width-1)@(height-1) for lower right pixel. |
|
889 |
You should not use this method for image-processing, its |
|
890 |
very slow ... |
|
891 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
892 |
||
893 |
^ self atX:aPoint x y:aPoint y put:aColor |
|
894 |
! |
|
895 |
||
896 |
atPoint:aPoint |
|
897 |
"ST-80 compatibility: return the pixelValue at:aPoint." |
|
898 |
||
899 |
^ self valueAtX:aPoint x y:aPoint y |
|
900 |
! |
|
901 |
||
0 | 902 |
atX:x y:y |
2 | 903 |
"retrieve a pixel at x/y; return a color. |
81 | 904 |
Pixels start at 0@0 for upper left pixel, end at |
905 |
(width-1)@(height-1) for lower right pixel. |
|
2 | 906 |
You should not use this method for image-processing, its |
81 | 907 |
very slow ... |
908 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
2 | 909 |
|
89 | 910 |
|pixel maxPixel r g b| |
911 |
||
912 |
pixel := self valueAtX:x y:y. |
|
913 |
photometric == #blackIs0 ifTrue:[ |
|
746 | 914 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
915 |
^ Color gray:(pixel * (100 / maxPixel)). |
|
89 | 916 |
]. |
917 |
photometric == #whiteIs0 ifTrue:[ |
|
746 | 918 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
919 |
^ Color gray:100 - (pixel * (100 / maxPixel)). |
|
89 | 920 |
]. |
921 |
photometric == #palette ifTrue:[ |
|
746 | 922 |
^ colorMap at:(pixel + 1) |
89 | 923 |
]. |
924 |
photometric == #rgb ifTrue:[ |
|
746 | 925 |
r := (pixel bitShift:16) bitAnd:16rFF. |
926 |
g := (pixel bitShift:8) bitAnd:16rFF. |
|
927 |
b := pixel bitAnd:16rFF. |
|
928 |
^ Color red:r / 255 * 100 |
|
929 |
green:g / 255 * 100 |
|
930 |
blue:b / 255 * 100 |
|
89 | 931 |
]. |
932 |
self error:'invalid photometric' |
|
746 | 933 |
|
934 |
"Modified: 28.5.1996 / 20:54:59 / cg" |
|
2 | 935 |
! |
936 |
||
937 |
atX:x y:y put:aColor |
|
938 |
"set the pixel at x/y to aColor. |
|
81 | 939 |
Pixels start at 0@0 for the upper left pixel, end at |
940 |
(width-1)@(height-1) for the lower right pixel. |
|
89 | 941 |
This method checks if the color can be stored in the image. |
942 |
(i.e. if the receiver is a palette image, the color must be present in there). |
|
943 |
You should not use this method for image-processing, it is very slow ... |
|
81 | 944 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
945 |
||
89 | 946 |
|pixel maxPixel| |
947 |
||
948 |
photometric == #whiteIs0 ifTrue:[ |
|
949 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
|
950 |
pixel := maxPixel - (aColor brightness * maxPixel) rounded. |
|
951 |
] ifFalse:[ |
|
952 |
photometric == #blackIs0 ifTrue:[ |
|
953 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
|
954 |
pixel := (aColor brightness * maxPixel) rounded. |
|
955 |
] ifFalse:[ |
|
956 |
photometric ~~ #palette ifTrue:[ |
|
957 |
self error:'format not supported'. |
|
958 |
^ nil |
|
959 |
]. |
|
960 |
pixel := colorMap indexOf:aColor. |
|
961 |
pixel == 0 ifTrue:[ |
|
962 |
" |
|
963 |
the color to be stored is not in the images colormap |
|
964 |
" |
|
965 |
self error:'invalid color' |
|
966 |
]. |
|
967 |
pixel := pixel - 1 |
|
968 |
] |
|
969 |
]. |
|
970 |
self atX:x y:y putValue:pixel. |
|
81 | 971 |
! |
972 |
||
283 | 973 |
atX:x y:y putValue:aPixelValue |
974 |
"set the pixel at x/y to aPixelValue. |
|
975 |
The interpretation of the pixelValue depends on the photometric |
|
976 |
and the colormap. (see also: Image>>atX:y:put:) |
|
977 |
Pixels start at 0@0 for the upper left pixel, end at |
|
978 |
(width-1) @ (height-1) for the lower right pixel. |
|
979 |
You should not use this method for image-processing, its |
|
980 |
very slow ... |
|
981 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
982 |
||
983 |
^ self subclassResponsibility |
|
984 |
! |
|
985 |
||
986 |
bits |
|
987 |
"return the raw image data; depending on the photometric, |
|
988 |
this has to be interpreted as monochrome, greyscale, |
|
989 |
palette or rgb data. It is also packed to be dense, so |
|
990 |
a 4 bitPerSample palette image will store 2 pixels per byte." |
|
991 |
||
992 |
^ bytes |
|
993 |
! |
|
994 |
||
995 |
data |
|
996 |
"for backward compatibility - will vanish" |
|
997 |
||
998 |
^ bytes |
|
999 |
! |
|
1000 |
||
1001 |
data:aByteArray |
|
1002 |
"for backward compatibility - will vanish" |
|
1003 |
||
1004 |
bytes := aByteArray |
|
1005 |
! |
|
1006 |
||
81 | 1007 |
valueAt:aPoint |
1008 |
"retrieve the pixelValue at aPoint; return an integer number. |
|
1009 |
Pixels start at 0@0 for upper left pixel, end at |
|
1010 |
width-1@height-1 for lower right pixel. |
|
1011 |
The returned numbers interpretation depends on the photometric |
|
1012 |
and the colormap. (see also Image>>at: and Image>>atX:y:) |
|
2 | 1013 |
You should not use this method for image-processing, its |
81 | 1014 |
very slow ... |
1015 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
1016 |
||
1017 |
^ self valueAtX:aPoint x y:aPoint y |
|
1018 |
! |
|
1019 |
||
1020 |
valueAtX:x y:y |
|
1021 |
"retrieve the pixelValue at aPoint; return an integer number. |
|
1022 |
Pixels start at 0/0 for upper left pixel, and end at |
|
1023 |
width-1@height-1 for lower right pixel. |
|
1024 |
The returned numbers interpretation depends on the photometric |
|
1025 |
and the colormap. (see also Image>>at: and Image>>atX:y:) |
|
1026 |
You should not use this method for image-processing of |
|
1027 |
big images, its very slow ... |
|
1028 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
2 | 1029 |
|
1030 |
^ self subclassResponsibility |
|
592 | 1031 |
! ! |
1032 |
||
1033 |
!Image methodsFor:'accessing - private'! |
|
1034 |
||
1035 |
bits:aByteArray |
|
1036 |
"set the raw data. |
|
1037 |
||
1038 |
This interface is only to be used when initializing |
|
1039 |
instances or by image readers. Calling for a change of an |
|
1040 |
existing image may confuse later pixel interpretation." |
|
1041 |
||
1042 |
bytes := aByteArray |
|
1043 |
||
1044 |
"Modified: 23.4.1996 / 11:08:28 / cg" |
|
2 | 1045 |
! |
1046 |
||
592 | 1047 |
bitsPerSample:aNumber |
1048 |
"set the number of bits per sample. |
|
1049 |
||
1050 |
This interface is only to be used when initializing |
|
1051 |
instances or by image readers. Calling for a change of an |
|
1052 |
existing image may confuse later pixel interpretation." |
|
1053 |
||
1054 |
bitsPerSample := aNumber |
|
1055 |
||
1056 |
"Modified: 23.4.1996 / 11:08:31 / cg" |
|
1057 |
! |
|
1058 |
||
1059 |
depth:d |
|
1060 |
"set the depth of the image. |
|
1061 |
||
1062 |
This interface is only to be used when initializing |
|
1063 |
instances or by image readers. Calling for a change of an |
|
1064 |
existing image may confuse later pixel interpretation." |
|
1065 |
||
1066 |
d == 24 ifTrue:[ |
|
1067 |
samplesPerPixel := 3. |
|
1068 |
bitsPerSample := #(8 8 8) |
|
1069 |
] ifFalse:[ |
|
1070 |
samplesPerPixel := 1. |
|
1071 |
bitsPerSample := Array with:d |
|
1072 |
] |
|
1073 |
||
1074 |
"Modified: 23.4.1996 / 11:08:34 / cg" |
|
1075 |
! |
|
1076 |
||
1077 |
extent:anExtent |
|
1078 |
"set the images extent. |
|
1079 |
||
1080 |
This interface is only to be used when initializing |
|
1081 |
instances or by image readers. Calling for a change of an |
|
1082 |
existing image may confuse later pixel interpretation." |
|
1083 |
||
1084 |
width := anExtent x. |
|
1085 |
height := anExtent y |
|
1086 |
||
1087 |
"Modified: 23.4.1996 / 11:08:38 / cg" |
|
1088 |
! |
|
1089 |
||
1090 |
height:aNumber |
|
1091 |
"set the height of the image. |
|
1092 |
||
1093 |
This interface is only to be used when initializing |
|
1094 |
instances or by image readers. Calling for a change of an |
|
1095 |
existing image may confuse later pixel interpretation." |
|
1096 |
||
1097 |
height := aNumber |
|
1098 |
||
1099 |
"Modified: 23.4.1996 / 11:08:40 / cg" |
|
1100 |
! |
|
1101 |
||
1102 |
photometric:aSymbol |
|
1103 |
"set the photometric interpretation of the pixel values. |
|
1104 |
The argument, aSymbol is one of: |
|
1105 |
#blackIs0, #whiteIs0, #palette, #rgb |
|
1106 |
See TIFF documentation, from which the photometric concept is borrowed. |
|
1107 |
||
1108 |
This interface is only to be used when initializing |
|
1109 |
instances or by image readers. Calling for a change of an |
|
1110 |
existing image may confuse later pixel interpretation." |
|
1111 |
||
1112 |
photometric := aSymbol |
|
1113 |
||
1114 |
"Modified: 23.4.1996 / 11:08:08 / cg" |
|
1115 |
! |
|
1116 |
||
1117 |
samplesPerPixel:aNumber |
|
1118 |
"set the array of samples per pixel. |
|
1119 |
||
1120 |
This interface is only to be used when initializing |
|
1121 |
instances or by image readers. Calling for a change of an |
|
1122 |
existing image may confuse later pixel interpretation." |
|
1123 |
||
1124 |
samplesPerPixel := aNumber |
|
1125 |
||
1126 |
"Modified: 23.4.1996 / 11:08:45 / cg" |
|
81 | 1127 |
! |
1128 |
||
283 | 1129 |
width:aNumber |
592 | 1130 |
"set the width of the image. |
1131 |
||
1132 |
This interface is only to be used when initializing |
|
1133 |
instances or by image readers. Calling for a change of an |
|
1134 |
existing image may confuse later pixel interpretation." |
|
283 | 1135 |
|
1136 |
width := aNumber |
|
592 | 1137 |
|
1138 |
"Modified: 23.4.1996 / 11:08:48 / cg" |
|
100 | 1139 |
! |
1140 |
||
283 | 1141 |
width:w height:h |
592 | 1142 |
"set the width and height of the image. |
1143 |
||
1144 |
This interface is only to be used when initializing |
|
1145 |
instances or by image readers. Calling for a change of an |
|
1146 |
existing image may confuse later pixel interpretation." |
|
283 | 1147 |
|
1148 |
width := w. |
|
1149 |
height := h |
|
592 | 1150 |
|
1151 |
"Modified: 23.4.1996 / 11:08:53 / cg" |
|
81 | 1152 |
! |
2 | 1153 |
|
283 | 1154 |
width:w height:h depth:d |
592 | 1155 |
"set the width, height and depth of the image. |
1156 |
||
1157 |
This interface is only to be used when initializing |
|
1158 |
instances or by image readers. Calling for a change of an |
|
1159 |
existing image may confuse later pixel interpretation." |
|
283 | 1160 |
|
1161 |
width := w. |
|
1162 |
height := h. |
|
1163 |
self depth:d. |
|
592 | 1164 |
|
1165 |
"Modified: 23.4.1996 / 11:08:56 / cg" |
|
89 | 1166 |
! |
1167 |
||
283 | 1168 |
width:w height:h depth:d fromArray:bits |
592 | 1169 |
"set the width, height, depth and pixels of the image. |
1170 |
||
1171 |
This interface is only to be used when initializing |
|
1172 |
instances or by image readers. Calling for a change of an |
|
1173 |
existing image may confuse later pixel interpretation." |
|
283 | 1174 |
|
1175 |
width := w. |
|
1176 |
height := h. |
|
1177 |
self depth:d. |
|
1178 |
bytes := bits |
|
592 | 1179 |
|
1180 |
"Modified: 23.4.1996 / 11:08:59 / cg" |
|
1181 |
! |
|
1182 |
||
1183 |
width:w height:h photometric:p samplesPerPixel:spp bitsPerSample:bps colorMap:cm bits:pixels |
|
1184 |
"set all relevant internal state of the image. |
|
1185 |
||
1186 |
This interface is only to be used when initializing |
|
1187 |
instances or by image readers. Calling for a change of an |
|
1188 |
existing image may confuse later pixel interpretation." |
|
1189 |
||
1190 |
width := w. |
|
1191 |
height := h. |
|
1192 |
photometric := p. |
|
1193 |
samplesPerPixel := spp. |
|
1194 |
bitsPerSample := bps. |
|
1195 |
colorMap := cm. |
|
1196 |
bytes := pixels. |
|
1197 |
||
1198 |
"Created: 23.4.1996 / 11:06:40 / cg" |
|
1199 |
"Modified: 23.4.1996 / 11:09:02 / cg" |
|
0 | 1200 |
! ! |
1201 |
||
283 | 1202 |
!Image methodsFor:'binary storage'! |
1203 |
||
1204 |
readBinaryContentsFrom: stream manager: manager |
|
1205 |
"read a binary representation of an image from stream. |
|
1206 |
Redefined to flush any device data." |
|
1207 |
||
1208 |
super readBinaryContentsFrom: stream manager: manager. |
|
1209 |
device := nil. |
|
1210 |
deviceForm := nil. |
|
1211 |
monoDeviceForm := nil. |
|
1212 |
fullColorDeviceForm := nil. |
|
0 | 1213 |
! |
1214 |
||
283 | 1215 |
storeBinaryDefinitionOn: stream manager: manager |
1216 |
"store a binary representation of the receiver on stream. |
|
592 | 1217 |
This is an internal interface for binary storage mechanism. |
283 | 1218 |
Redefined to not store the device form (which is recreated at |
1219 |
load time anyway)" |
|
1220 |
||
1221 |
|tDevice tDeviceForm tMonoDeviceForm tFullColorDeviceForm| |
|
1222 |
||
1223 |
tDevice := device. |
|
1224 |
tDeviceForm := deviceForm. |
|
1225 |
tMonoDeviceForm := monoDeviceForm. |
|
1226 |
tFullColorDeviceForm := fullColorDeviceForm. |
|
1227 |
||
1228 |
device := nil. |
|
1229 |
deviceForm := nil. |
|
1230 |
monoDeviceForm := nil. |
|
1231 |
fullColorDeviceForm := nil. |
|
1232 |
||
1233 |
super storeBinaryDefinitionOn: stream manager: manager. |
|
1234 |
||
1235 |
device := tDevice. |
|
1236 |
deviceForm := tDeviceForm. |
|
1237 |
monoDeviceForm := tMonoDeviceForm. |
|
1238 |
fullColorDeviceForm := tFullColorDeviceForm. |
|
592 | 1239 |
|
1240 |
"Modified: 23.4.1996 / 09:30:50 / cg" |
|
0 | 1241 |
! ! |
1242 |
||
1243 |
!Image methodsFor:'converting'! |
|
1244 |
||
283 | 1245 |
asCachedImage |
592 | 1246 |
"return the receiver associated to the current screens device. |
1247 |
For ST-80 compatibility |
|
1248 |
(ST/X uses Image for both device- and nonDevice-images)" |
|
283 | 1249 |
|
1250 |
^ self on:Screen current |
|
592 | 1251 |
|
1252 |
"Modified: 23.4.1996 / 11:10:32 / cg" |
|
0 | 1253 |
! |
1254 |
||
1255 |
asFormOn:aDevice |
|
72 | 1256 |
"get a device form, with best possible approximation. |
1257 |
remember it in case someone asks again." |
|
1258 |
||
1259 |
|form| |
|
1260 |
||
1261 |
((aDevice == device) and:[deviceForm notNil]) ifTrue:[^ deviceForm]. |
|
0 | 1262 |
|
1263 |
(photometric == #palette) ifTrue:[ |
|
72 | 1264 |
form := self paletteImageAsFormOn:aDevice |
1265 |
] ifFalse:[ |
|
1266 |
(photometric == #rgb) ifTrue:[ |
|
1267 |
form := self rgbImageAsFormOn:aDevice |
|
1268 |
] ifFalse:[ |
|
1269 |
form := self greyImageAsFormOn:aDevice |
|
1270 |
] |
|
0 | 1271 |
]. |
1272 |
||
72 | 1273 |
(device isNil or:[aDevice == device]) ifTrue:[ |
1274 |
"remember this form in the receiver ..." |
|
1275 |
||
1276 |
form notNil ifTrue:[ |
|
1277 |
deviceForm := form. |
|
1278 |
device isNil ifTrue:[ |
|
1279 |
device := aDevice. |
|
1280 |
Lobby register:self |
|
1281 |
] ifFalse:[ |
|
162 | 1282 |
Lobby registerChange:self |
72 | 1283 |
]. |
1284 |
" |
|
1285 |
can save space, by not keeping the images data-bits |
|
1286 |
twice (here and in the device form) |
|
1287 |
" |
|
1288 |
form forgetBits |
|
1289 |
] |
|
0 | 1290 |
]. |
1291 |
||
72 | 1292 |
^ form |
0 | 1293 |
! |
1294 |
||
1295 |
asMonochromeFormOn:aDevice |
|
1296 |
"get a monochrome device form" |
|
1297 |
||
72 | 1298 |
|form| |
1299 |
||
1300 |
((aDevice == device) and:[monoDeviceForm notNil]) ifTrue:[^ monoDeviceForm]. |
|
1301 |
||
0 | 1302 |
(photometric == #palette) ifTrue:[ |
72 | 1303 |
form := self paletteImageAsMonoFormOn:aDevice |
1304 |
] ifFalse:[ |
|
1305 |
(photometric == #rgb) ifTrue:[ |
|
1306 |
form := self rgbImageAsMonoFormOn:aDevice |
|
1307 |
] ifFalse:[ |
|
1308 |
form := self greyImageAsMonoFormOn:aDevice |
|
1309 |
] |
|
0 | 1310 |
]. |
1311 |
||
72 | 1312 |
(device isNil or:[aDevice == device]) ifTrue:[ |
1313 |
"remember this form in the receiver ..." |
|
1314 |
||
1315 |
form notNil ifTrue:[ |
|
1316 |
monoDeviceForm := form. |
|
1317 |
device isNil ifTrue:[ |
|
1318 |
device := aDevice. |
|
1319 |
Lobby register:self |
|
1320 |
] ifFalse:[ |
|
162 | 1321 |
Lobby registerChange:self |
72 | 1322 |
]. |
1323 |
" |
|
1324 |
can save space, by not keeping the images data-bits |
|
1325 |
twice (here and in the device form) |
|
1326 |
" |
|
1327 |
form forgetBits |
|
1328 |
] |
|
0 | 1329 |
]. |
1330 |
||
72 | 1331 |
^ form |
89 | 1332 |
! |
1333 |
||
283 | 1334 |
fromForm:aForm |
592 | 1335 |
"setup the receiver from a form" |
283 | 1336 |
|
1337 |
|map c0 c1| |
|
1338 |
||
1339 |
width := aForm width. |
|
1340 |
height := aForm height. |
|
1341 |
bytes := aForm bits. |
|
1342 |
bitsPerSample := self bitsPerSample. |
|
1343 |
samplesPerPixel := self samplesPerPixel. |
|
1344 |
map := aForm colorMap. |
|
1345 |
||
1346 |
aForm depth == 1 ifTrue:[ |
|
592 | 1347 |
map isNil ifTrue:[ |
1348 |
photometric := #whiteIs0 |
|
1349 |
] ifFalse:[ |
|
1350 |
c0 := map at:1. |
|
1351 |
c1 := map at:2. |
|
1352 |
((c0 = Color white) |
|
1353 |
and:[c1 = Color black]) ifTrue:[ |
|
1354 |
photometric := #whiteIs0 |
|
1355 |
] ifFalse:[ |
|
1356 |
((c0 = Color black) |
|
1357 |
and:[c1 = Color white]) ifTrue:[ |
|
1358 |
photometric := #blackIs0 |
|
1359 |
] ifFalse:[ |
|
1360 |
photometric := #palette. |
|
1361 |
colorMap := Array with:c0 with:c1. |
|
1362 |
] |
|
1363 |
] |
|
1364 |
] |
|
283 | 1365 |
] ifFalse:[ |
592 | 1366 |
map notNil ifTrue:[ |
1367 |
photometric := #palette. |
|
1368 |
colorMap := map copy. |
|
1369 |
] ifFalse:[ |
|
1370 |
" |
|
1371 |
photometric stays at default |
|
1372 |
(which is rgb for d24, greyscale for others) |
|
1373 |
" |
|
1374 |
] |
|
283 | 1375 |
]. |
592 | 1376 |
|
1377 |
"Modified: 23.4.1996 / 11:11:00 / cg" |
|
283 | 1378 |
! |
1379 |
||
89 | 1380 |
fromImage:anImage |
1381 |
"setup the receiver from another image. |
|
1382 |
Color precision may be lost, if conversion is from a higher depth |
|
191 | 1383 |
image. |
1384 |
WARNING: |
|
1385 |
This implementation is a slow fallback (the loop over the |
|
89 | 1386 |
source pixels is very slow). If this method is used heavily, you |
1387 |
may want to redefine it in concrete subclasses for common source images." |
|
1388 |
||
1389 |
width := anImage width. |
|
1390 |
height := anImage height. |
|
1391 |
bytes := ByteArray uninitializedNew:(self bytesPerRow * height). |
|
1392 |
bitsPerSample := self bitsPerSample. |
|
1393 |
samplesPerPixel := self samplesPerPixel. |
|
191 | 1394 |
self colormapFromImage:anImage. |
89 | 1395 |
anImage colorsFromX:0 y:0 toX:(width-1) y:(height-1) do:[:x :y :clr | |
1396 |
self atX:x y:y put:clr |
|
1397 |
]. |
|
1398 |
||
1399 |
" |
|
1400 |
|i i2 i4 i8 i24| |
|
1401 |
||
1402 |
i := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
1403 |
i inspect. |
|
1404 |
i2 := Depth2Image fromImage:i. |
|
1405 |
i2 inspect. |
|
1406 |
i4 := Depth4Image fromImage:i. |
|
1407 |
i4 inspect. |
|
1408 |
i8 := Depth8Image fromImage:i. |
|
1409 |
i8 inspect. |
|
1410 |
i24 := Depth24Image fromImage:i. |
|
1411 |
i24 inspect. |
|
1412 |
" |
|
191 | 1413 |
|
1414 |
"Created: 20.9.1995 / 00:59:03 / claus" |
|
1415 |
! |
|
1416 |
||
1417 |
fromSubImage:anImage in:aRectangle |
|
1418 |
"setup the receiver from another image, extracting a rectangular |
|
1419 |
area. Color precision may be lost, if conversion is from a higher depth |
|
1420 |
image. |
|
1421 |
WARNING: |
|
1422 |
This implementation is a slow fallback (the loop over the |
|
1423 |
source pixels is very slow). If this method is used heavily, you |
|
1424 |
may want to redefine it in concrete subclasses for the common case of |
|
1425 |
of creating a subImage with the same depth & palette." |
|
1426 |
||
1427 |
|x0 y0| |
|
1428 |
||
1429 |
width := aRectangle width + 1. |
|
1430 |
height := aRectangle height + 1. |
|
1431 |
bytes := ByteArray uninitializedNew:(self bytesPerRow * height). |
|
1432 |
bitsPerSample := self bitsPerSample. |
|
1433 |
samplesPerPixel := self samplesPerPixel. |
|
1434 |
self colormapFromImage:anImage. |
|
1435 |
x0 := aRectangle left. |
|
1436 |
y0 := aRectangle top. |
|
1437 |
((photometric == anImage photometric) |
|
1438 |
and:[self bitsPerPixel == anImage bitsPerPixel |
|
1439 |
and:[colorMap = anImage colorMap]]) ifTrue:[ |
|
1440 |
"/ can do it by value |
|
1441 |
anImage valuesFromX:x0 y:y0 |
|
1442 |
toX:aRectangle right y:aRectangle bottom |
|
1443 |
do:[:x :y :pixelValue | |
|
1444 |
self atX:x-x0 y:y-y0 putValue:pixelValue. |
|
1445 |
] |
|
1446 |
] ifFalse:[ |
|
1447 |
"/ must do it by colors |
|
1448 |
anImage colorsFromX:x0 y:y0 |
|
1449 |
toX:aRectangle right y:aRectangle bottom |
|
1450 |
do:[:x :y :clr | |
|
1451 |
self atX:x-x0 y:y-y0 put:clr. |
|
1452 |
] |
|
1453 |
]. |
|
1454 |
||
1455 |
" |
|
1456 |
|i i2 i4 i8 i24| |
|
1457 |
||
1458 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1459 |
i inspect. |
|
1460 |
i4 := Depth4Image fromSubImage:i in:(300@160 corner:340@200). |
|
1461 |
i4 inspect. |
|
1462 |
i8 := Depth8Image fromSubImage:i in:(300@160 corner:340@200). |
|
1463 |
i8 inspect. |
|
1464 |
i24 := Depth24Image fromSubImage:i in:(300@160 corner:340@200). |
|
1465 |
i24 inspect. |
|
1466 |
" |
|
1467 |
||
1468 |
"Created: 20.9.1995 / 01:06:02 / claus" |
|
1469 |
"Modified: 20.9.1995 / 10:15:37 / claus" |
|
89 | 1470 |
! |
1471 |
||
283 | 1472 |
monochromeOn:aDevice |
1473 |
"return a monochrome device image of the receiver for aDevice. |
|
1474 |
(monochrome, even if device supports colors)" |
|
1475 |
||
1476 |
((aDevice == device) and:[monoDeviceForm notNil]) ifTrue:[^ self]. |
|
1477 |
(device notNil and:[aDevice ~~ device]) ifTrue:[ |
|
1478 |
"oops, I am already accociated to another device |
|
1479 |
- need a copy ... |
|
1480 |
" |
|
1481 |
^ self copy monochromeOn:aDevice |
|
1482 |
]. |
|
1483 |
monoDeviceForm := self asMonochromeFormOn:aDevice. |
|
1484 |
! |
|
1485 |
||
1486 |
on:aDevice |
|
1487 |
"return an image with the same pixels as the receiver, but |
|
1488 |
associated to aDevice. If the receiver is not yet bound to |
|
1489 |
a device, this will be the receiver. Otherwise, a new image |
|
1490 |
is returned." |
|
1491 |
||
1492 |
((aDevice == device) and:[deviceForm notNil]) ifTrue:[^ self]. |
|
1493 |
(device notNil and:[aDevice ~~ device]) ifTrue:[ |
|
1494 |
"oops, I am already accociated to another device |
|
1495 |
- need a copy ... |
|
1496 |
" |
|
1497 |
^ self copy on:aDevice |
|
1498 |
]. |
|
1499 |
deviceForm := self asFormOn:aDevice. |
|
1500 |
device := aDevice |
|
1501 |
! ! |
|
1502 |
||
1503 |
!Image methodsFor:'converting greyscale images'! |
|
1504 |
||
1505 |
greyImageAsFormOn:aDevice |
|
1506 |
"return a thresholded grey-deviceForm from the grey image." |
|
1507 |
||
1508 |
|pictureDepth nPlanes f| |
|
1509 |
||
1510 |
nPlanes := samplesPerPixel. |
|
1511 |
(nPlanes == 2) ifTrue:[ |
|
386 | 1512 |
'IMAGE: alpha plane ignored' errorPrintNL. |
1513 |
nPlanes := 1 |
|
283 | 1514 |
]. |
1515 |
||
1516 |
pictureDepth := bitsPerSample at:1. |
|
1517 |
||
1518 |
"monochrome is very easy ..." |
|
1519 |
||
1520 |
(pictureDepth == 1) ifTrue:[ |
|
386 | 1521 |
^ Form width:width height:height fromArray:bytes on:aDevice |
283 | 1522 |
]. |
1523 |
||
1524 |
(aDevice visualType == #StaticGray) ifTrue:[ |
|
386 | 1525 |
(aDevice depth == pictureDepth) ifTrue:[ |
1526 |
||
1527 |
"greyscale is easy, if the depths match" |
|
1528 |
||
1529 |
f := Form width:width height:height depth:pictureDepth on:aDevice. |
|
1530 |
f isNil ifTrue:[^ nil]. |
|
1531 |
f initGC. |
|
1532 |
||
1533 |
"if device has white at the opposite corner ..." |
|
1534 |
((aDevice blackpixel == 0) == (photometric == #blackIs0)) ifFalse:[ |
|
1535 |
"have to invert bits" |
|
1536 |
f function:#copyInverted |
|
1537 |
]. |
|
1538 |
aDevice drawBits:bytes depth:pictureDepth width:width height:height |
|
1539 |
x:0 y:0 |
|
1540 |
into:(f id) |
|
1541 |
x:0 y:0 width:width height:height with:(f gcId). |
|
1542 |
^ f |
|
1543 |
]. |
|
1544 |
||
1545 |
"the image has more greylevels than the display - dither" |
|
283 | 1546 |
|
1547 |
" |
|
1548 |
coming soon ... |
|
386 | 1549 |
DitherAlgorithm == #error ifTrue:[ |
1550 |
^ self greyImageAsErrorDitheredGreyFormOn:aDevice |
|
1551 |
]. |
|
283 | 1552 |
" |
386 | 1553 |
DitherAlgorithm == #pattern ifTrue:[ |
1554 |
^ self greyImageAsPatternDitheredGreyFormOn:aDevice |
|
1555 |
]. |
|
1556 |
||
1557 |
"no dither, simply cut off information" |
|
1558 |
(aDevice depth == 1) ifTrue:[ |
|
1559 |
"for monochrome, there is a special method to do this" |
|
1560 |
^ self greyImageAsMonoFormOn:aDevice |
|
1561 |
]. |
|
1562 |
"the general case, will take as many bits from the image |
|
1563 |
as possible for the device" |
|
1564 |
^ self greyImageAsGreyFormOn:aDevice |
|
283 | 1565 |
]. |
1566 |
||
1567 |
||
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1568 |
(aDevice visualType == #PseudoColor |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1569 |
or:[aDevice visualType == #GrayScale |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1570 |
or:[aDevice visualType == #StaticColor]]) ifTrue:[ |
386 | 1571 |
^ self greyImageAsPseudoFormOn:aDevice |
283 | 1572 |
]. |
1573 |
||
1574 |
(aDevice visualType == #TrueColor) ifTrue:[ |
|
386 | 1575 |
^ self greyImageAsTrueColorFormOn:aDevice |
283 | 1576 |
]. |
1577 |
||
1578 |
self error:'cannot convert this format'. |
|
1579 |
^ nil |
|
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1580 |
|
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1581 |
"Modified: 11.12.1995 / 18:55:01 / cg" |
283 | 1582 |
! |
1583 |
||
1584 |
greyImageAsGreyFormOn:aDevice |
|
1585 |
"return an 8-bit Form from the grey image" |
|
1586 |
||
1587 |
|wideBits pictureDepth f map nplanes ncells |
|
1588 |
inverse |
|
1589 |
mapSize "{ Class: SmallInteger }" |
|
1590 |
oldValue "{ Class: SmallInteger }" |
|
1591 |
newValue "{ Class: SmallInteger }" |
|
1592 |
shift "{ Class: SmallInteger }" |
|
1593 |
shift2 "{ Class: SmallInteger }" |
|
1594 |
shift3 "{ Class: SmallInteger }" |
|
1595 |
shift4 "{ Class: SmallInteger }" | |
|
1596 |
||
1597 |
(aDevice depth == 8) ifFalse:[ |
|
1598 |
'IMAGE: non-8 plane displays not supported' errorPrintNL. |
|
1599 |
^ self greyImageAsMonoFormOn:aDevice |
|
1600 |
]. |
|
1601 |
||
1602 |
pictureDepth := bitsPerSample at:1. |
|
1603 |
||
1604 |
wideBits := ByteArray uninitializedNew:(width * height). |
|
1605 |
||
1606 |
map := ByteArray uninitializedNew:(1 bitShift:pictureDepth). |
|
1607 |
||
1608 |
"find the real number of server-planes" |
|
1609 |
nplanes := 8. |
|
1610 |
ncells := 256. |
|
1611 |
[aDevice ncells < ncells] whileTrue:[ |
|
1612 |
nplanes := nplanes - 1. |
|
1613 |
ncells := ncells // 2 |
|
1614 |
]. |
|
1615 |
||
1616 |
"prepare translation table" |
|
1617 |
||
1618 |
shift := nplanes - pictureDepth. |
|
1619 |
shift2 := shift - pictureDepth. |
|
1620 |
shift3 := shift2 - pictureDepth. |
|
1621 |
shift4 := shift3 - pictureDepth. |
|
1622 |
||
1623 |
inverse := aDevice blackpixel ~~ 0. |
|
1624 |
photometric == #blackIs0 ifFalse:[ |
|
1625 |
inverse := inverse not |
|
1626 |
]. |
|
1627 |
mapSize := map size. |
|
1628 |
1 to:mapSize do:[:index | |
|
1629 |
oldValue := index - 1. |
|
1630 |
newValue := oldValue bitShift:shift. |
|
1631 |
newValue := newValue bitOr:(oldValue bitShift:shift2). |
|
1632 |
newValue := newValue bitOr:(oldValue bitShift:shift3). |
|
1633 |
newValue := newValue bitOr:(oldValue bitShift:shift4). |
|
1634 |
inverse ifTrue:[ |
|
1635 |
map at:(map size - index + 1) put:newValue |
|
89 | 1636 |
] ifFalse:[ |
283 | 1637 |
map at:index put:newValue |
1638 |
] |
|
1639 |
]. |
|
1640 |
bytes expandPixels:pictureDepth |
|
1641 |
width:width |
|
1642 |
height:height |
|
1643 |
into:wideBits |
|
1644 |
mapping:map. |
|
1645 |
||
1646 |
f := Form width:width height:height depth:8 on:aDevice. |
|
1647 |
f isNil ifTrue:[^ nil]. |
|
1648 |
f initGC. |
|
1649 |
aDevice drawBits:wideBits depth:8 width:width height:height |
|
1650 |
x:0 y:0 |
|
1651 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
1652 |
^ f |
|
1653 |
! |
|
1654 |
||
1655 |
greyImageAsMonoFormOn:aDevice |
|
1656 |
"return a (thresholded) monochrome Form from the image." |
|
1657 |
||
1658 |
^ self subclassResponsibility |
|
1659 |
! |
|
1660 |
||
1661 |
greyImageAsPatternDitheredGreyFormOn:aDevice |
|
1662 |
"return a dithered greyForm from the grey image. |
|
1663 |
Works for any source/destination depths, but very very slow |
|
1664 |
since each pixel is processed individually. |
|
1665 |
Usually redefined by subclasses for more performance" |
|
1666 |
||
1667 |
|f last |
|
1668 |
x0 "{Class: SmallInteger }" |
|
1669 |
w "{Class: SmallInteger }" |
|
1670 |
h "{Class: SmallInteger }" |
|
1671 |
run "{Class: SmallInteger }" | |
|
1672 |
||
694 | 1673 |
Transcript showCR:'slow dithering ..'. Transcript endEntry. |
283 | 1674 |
|
1675 |
w := width - 1. |
|
1676 |
h := height - 1. |
|
1677 |
||
1678 |
"draw each pixel using dither color (let others do the dithering) |
|
1679 |
although the code is simple, its very slow" |
|
1680 |
||
1681 |
f := Form width:width height:height depth:aDevice depth on:aDevice. |
|
1682 |
f isNil ifTrue:[^ nil]. |
|
1683 |
f initGC. |
|
1684 |
||
1685 |
0 to:h do:[:dstY | |
|
694 | 1686 |
x0 := 0. |
1687 |
run := 0. |
|
1688 |
last := nil. |
|
1689 |
self atY:dstY from:0 to:w do:[:x :srcColor | |
|
1690 |
|dstColor| |
|
1691 |
||
1692 |
srcColor ~~ last ifTrue:[ |
|
1693 |
(run ~~ 0) ifTrue:[ |
|
1694 |
f fillRectangleX:x0 y:dstY width:run height:1. |
|
1695 |
]. |
|
1696 |
run := 0. |
|
746 | 1697 |
dstColor := Color gray:(srcColor grayIntensity). |
694 | 1698 |
f paint:dstColor. |
1699 |
last := srcColor. |
|
1700 |
x0 := x |
|
1701 |
]. |
|
1702 |
run := run + 1 |
|
1703 |
]. |
|
1704 |
f fillRectangleX:x0 y:dstY width:run height:1. |
|
283 | 1705 |
]. |
1706 |
^ f |
|
694 | 1707 |
|
746 | 1708 |
"Modified: 28.5.1996 / 20:55:02 / cg" |
283 | 1709 |
! |
1710 |
||
1711 |
greyImageAsPseudoFormOn:aDevice |
|
1712 |
"return an 8-bit pseudo Form from the grey image" |
|
1713 |
||
1714 |
|wideBits pictureDepth f map |
|
1715 |
colorMap usedColors nUsed aColor |
|
1716 |
nColors "{ Class: SmallInteger }" |
|
1717 |
range id| |
|
1718 |
||
1719 |
pictureDepth := bitsPerSample at:1. |
|
1720 |
||
1721 |
(#(2 4 8) includes:pictureDepth) ifFalse:[ |
|
746 | 1722 |
self error:'currently only depth-2, 4 or 8 supported'. |
1723 |
^ nil |
|
283 | 1724 |
]. |
1725 |
||
1726 |
wideBits := ByteArray uninitializedNew:(width * height). |
|
1727 |
||
1728 |
(pictureDepth == 8) ifTrue:[ |
|
746 | 1729 |
"for 8bits, we scan for used colors first; |
1730 |
to avoid allocating too many colors" |
|
1731 |
||
1732 |
usedColors := bytes usedValues. |
|
1733 |
nUsed := usedColors max + 1. |
|
1734 |
||
1735 |
colorMap := Array new:nUsed. |
|
1736 |
photometric == #blackIs0 ifTrue:[ |
|
1737 |
usedColors do:[:grey | |
|
1738 |
colorMap at:(grey + 1) put:(Color gray:(100.0 / 256.0 * grey)) |
|
1739 |
] |
|
1740 |
] ifFalse:[ |
|
1741 |
usedColors do:[:grey | |
|
1742 |
colorMap at:(grey + 1) put:(Color gray:(100 - (100.0 / 256.0 * grey))) |
|
1743 |
] |
|
1744 |
] |
|
89 | 1745 |
] ifFalse:[ |
746 | 1746 |
nColors := (1 bitShift:pictureDepth). |
1747 |
colorMap := Array new:nColors. |
|
1748 |
range := 100 / (nColors - 1) asFloat. |
|
1749 |
photometric == #blackIs0 ifTrue:[ |
|
1750 |
1 to:nColors do:[:i | |
|
1751 |
colorMap at:i put:(Color gray:(i - 1) * range). |
|
1752 |
]. |
|
1753 |
] ifFalse:[ |
|
1754 |
1 to:nColors do:[:i | |
|
1755 |
colorMap at:(nColors - i + 1) put:(Color gray:(i - 1) * range). |
|
1756 |
]. |
|
1757 |
]. |
|
283 | 1758 |
]. |
1759 |
||
1760 |
"XXX should reduce 8->6->4->2 planes, if not all colors could be allocated" |
|
1761 |
||
1762 |
"setup the translation map" |
|
1763 |
map := ByteArray uninitializedNew:256. |
|
1764 |
nColors := colorMap size. |
|
1765 |
1 to:nColors do:[:i | |
|
746 | 1766 |
aColor := colorMap at:i. |
1767 |
aColor notNil ifTrue:[ |
|
1768 |
aColor := aColor on:aDevice. |
|
1769 |
colorMap at:i put:aColor. |
|
1770 |
id := aColor colorId. |
|
1771 |
id notNil ifTrue:[ |
|
1772 |
map at:i put:id |
|
1773 |
] ifFalse:[ |
|
1774 |
map at:i put:0 |
|
1775 |
] |
|
1776 |
] |
|
89 | 1777 |
]. |
283 | 1778 |
|
1779 |
"expand & translate" |
|
1780 |
bytes expandPixels:pictureDepth |
|
746 | 1781 |
width:width |
1782 |
height:height |
|
1783 |
into:wideBits |
|
1784 |
mapping:map. |
|
283 | 1785 |
|
1786 |
f := Form width:width height:height depth:8 on:aDevice. |
|
1787 |
f isNil ifTrue:[^ nil]. |
|
1788 |
f colorMap:colorMap. |
|
1789 |
f initGC. |
|
1790 |
aDevice drawBits:wideBits depth:8 width:width height:height |
|
746 | 1791 |
x:0 y:0 |
1792 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
283 | 1793 |
^ f |
746 | 1794 |
|
1795 |
"Modified: 28.5.1996 / 20:55:57 / cg" |
|
0 | 1796 |
! |
1797 |
||
283 | 1798 |
greyImageAsTrueColorFormOn:aDevice |
1799 |
"return a true-color device-form for the grey-image receiver. |
|
1800 |
TODO: the pixel loops ought to be implemented as inline primitive code ..." |
|
1801 |
||
1802 |
|depth |
|
1803 |
myDepth "{ Class: SmallInteger }" |
|
1804 |
nColors "{ Class: SmallInteger }" |
|
1805 |
colorValues |
|
1806 |
scaleDown scaleRed scaleGreen scaleBlue redShift blueShift greenShift |
|
1807 |
form imageBitsdestIndex |
|
1808 |
bestFormat usedDeviceDepth usedDeviceBitsPerPixel imageBits destIndex |
|
1809 |
nPixels| |
|
1810 |
||
1811 |
"/ this is a slow fallback method; this ought to be |
|
1812 |
"/ redefined in DepthxImage for more performance. |
|
1813 |
||
1814 |
depth := aDevice depth. |
|
1815 |
myDepth := self depth. |
|
1816 |
myDepth > 12 ifTrue:[ |
|
1817 |
self error:'unsupported trueColor depth in greyImageAsTrueColorFormOn:'. |
|
1818 |
^ nil |
|
0 | 1819 |
]. |
1820 |
||
283 | 1821 |
"/ compute scale to map from my pixels into devices range |
1822 |
||
1823 |
scaleDown := 1 bitShift:myDepth. |
|
1824 |
scaleRed := (1 bitShift:aDevice bitsRed). |
|
1825 |
scaleGreen := (1 bitShift:aDevice bitsGreen). |
|
1826 |
scaleBlue := (1 bitShift:aDevice bitsBlue). |
|
1827 |
redShift := aDevice shiftRed. |
|
1828 |
greenShift := aDevice shiftGreen. |
|
1829 |
blueShift := aDevice shiftBlue. |
|
1830 |
||
1831 |
nColors := (1 bitShift:myDepth). |
|
1832 |
colorValues := Array new:nColors. |
|
1833 |
1 to:nColors do:[:i | |
|
1834 |
|v gv bv rv nv| |
|
1835 |
||
1836 |
"/ scale down to 0..1 |
|
1837 |
v := (i-1) / scaleDown. |
|
1838 |
rv := (v * scaleRed) rounded. |
|
1839 |
gv := (v * scaleGreen) rounded. |
|
1840 |
bv := (v * scaleBlue) rounded. |
|
1841 |
nv := rv bitShift:redShift. |
|
1842 |
nv := nv bitOr:(gv bitShift:greenShift). |
|
1843 |
nv := nv bitOr:(bv bitShift:blueShift). |
|
1844 |
colorValues at:i put:nv |
|
0 | 1845 |
]. |
283 | 1846 |
photometric == #whiteIs0 ifTrue:[ |
1847 |
"/ reverse the order; 0 is brightest |
|
1848 |
colorValues reverse |
|
1849 |
]. |
|
193 | 1850 |
|
1851 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
1852 |
usedDeviceDepth := bestFormat at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
1853 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
193 | 1854 |
|
283 | 1855 |
"/ for now, only support some depths |
1856 |
||
1857 |
usedDeviceBitsPerPixel == 16 ifTrue:[ |
|
1858 |
imageBits := ByteArray uninitializedNew:(width * height * 2). |
|
1859 |
||
1860 |
"/ now, walk over the image and replace |
|
1861 |
"/ colorMap indices by color values in the bits array |
|
1862 |
||
1863 |
destIndex := 1. |
|
1864 |
0 to:height-1 do:[:y | |
|
1865 |
0 to:width-1 do:[:x | |
|
1866 |
|greyValue| |
|
1867 |
||
1868 |
greyValue := self valueAtX:x y:y. |
|
1869 |
imageBits wordAt:destIndex put:(colorValues at:greyValue + 1) MSB:true. |
|
1870 |
destIndex := destIndex + 2. |
|
1871 |
] |
|
1872 |
] |
|
1873 |
] ifFalse:[ |
|
1874 |
usedDeviceBitsPerPixel == 32 ifTrue:[ |
|
1875 |
imageBits := ByteArray uninitializedNew:(width * height * 4). |
|
1876 |
||
1877 |
"/ now, walk over the image and replace |
|
1878 |
"/ colorMap indices by color values in the bits array |
|
1879 |
||
1880 |
destIndex := 1. |
|
1881 |
0 to:height-1 do:[:y | |
|
1882 |
0 to:width-1 do:[:x | |
|
1883 |
|greyValue| |
|
1884 |
||
1885 |
greyValue := self valueAtX:x y:y. |
|
1886 |
imageBits doubleWordAt:destIndex put:(colorValues at:greyValue + 1) MSB:true. |
|
1887 |
destIndex := destIndex + 4. |
|
1888 |
] |
|
1889 |
] |
|
1890 |
] |
|
193 | 1891 |
]. |
1892 |
||
283 | 1893 |
imageBits isNil ifTrue:[ |
1894 |
'IMAGE: unimplemented trueColor depth on greyImageAsTrueColorFormOn:' errorPrintNL. |
|
1895 |
^ self paletteImageAsMonoFormOn:aDevice |
|
1896 |
]. |
|
1897 |
||
1898 |
form := |
|
193 | 1899 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
283 | 1900 |
form isNil ifTrue:[^ nil]. |
193 | 1901 |
form initGC. |
1902 |
||
1903 |
form |
|
283 | 1904 |
copyBitsFrom:imageBits bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
193 | 1905 |
width:width height:height |
1906 |
x:0 y:0 toX:0 y:0. |
|
1907 |
||
1908 |
^ form |
|
1909 |
||
283 | 1910 |
"Created: 20.10.1995 / 22:05:10 / cg" |
1911 |
"Modified: 21.10.1995 / 19:30:37 / cg" |
|
0 | 1912 |
! ! |
1913 |
||
1914 |
!Image methodsFor:'converting palette images'! |
|
1915 |
||
283 | 1916 |
paletteImageAs2PlaneFormOn:aDevice |
1917 |
"return a 2-bit grey-deviceForm from the palette image" |
|
1918 |
||
1919 |
^ self subclassResponsibility |
|
1920 |
! |
|
1921 |
||
0 | 1922 |
paletteImageAsFormOn:aDevice |
72 | 1923 |
"return a device-form for the palette-image receiver" |
1924 |
||
380 | 1925 |
|type ddepth| |
193 | 1926 |
|
379 | 1927 |
(ddepth := aDevice depth) == 1 ifTrue:[ |
1928 |
^ self paletteImageAsMonoFormOn:aDevice |
|
1929 |
]. |
|
1930 |
||
193 | 1931 |
((type := aDevice visualType) == #StaticGray) ifTrue:[ |
386 | 1932 |
ddepth == 8 ifTrue:[ |
1933 |
^ self paletteImageAsGreyFormOn:aDevice |
|
1934 |
]. |
|
1935 |
||
1936 |
DitherAlgorithm == #pattern ifTrue:[ |
|
1937 |
^ self paletteImageAsPatternDitheredGreyFormOn:aDevice |
|
1938 |
]. |
|
1939 |
||
1940 |
ddepth == 2 ifTrue:[ |
|
1941 |
^ self paletteImageAs2PlaneFormOn:aDevice |
|
1942 |
]. |
|
1943 |
||
1944 |
^ self paletteImageAsMonoFormOn:aDevice |
|
0 | 1945 |
]. |
193 | 1946 |
(type == #TrueColor) ifTrue:[ |
386 | 1947 |
^ self paletteImageAsTrueColorFormOn:aDevice |
193 | 1948 |
]. |
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1949 |
(type == #PseudoColor) ifTrue:[ |
386 | 1950 |
^ self paletteImageAsPseudoFormOn:aDevice |
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1951 |
]. |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1952 |
(type == #StaticColor) ifTrue:[ |
386 | 1953 |
^ self paletteImageAsPseudoFormOn:aDevice |
193 | 1954 |
]. |
1955 |
"/ dump fallback: every device should implement b&w images ... |
|
1956 |
^ self paletteImageAsMonoFormOn:aDevice |
|
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1957 |
|
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
1958 |
"Modified: 11.12.1995 / 16:34:52 / cg" |
0 | 1959 |
! |
1960 |
||
283 | 1961 |
paletteImageAsGreyFormOn:aDevice |
1962 |
"return an 8-bit grey-deviceForm from the palette image" |
|
1963 |
||
1964 |
^ self subclassResponsibility |
|
1965 |
! |
|
1966 |
||
0 | 1967 |
paletteImageAsMonoFormOn:aDevice |
72 | 1968 |
"return a 1-bit mono-deviceForm from the palette image" |
0 | 1969 |
|
2 | 1970 |
^ self subclassResponsibility |
0 | 1971 |
! |
1972 |
||
283 | 1973 |
paletteImageAsPatternDitheredGreyFormOn:aDevice |
1974 |
"return a dithered grey-deviceForm from the palette image." |
|
0 | 1975 |
|
2 | 1976 |
^ self subclassResponsibility |
0 | 1977 |
! |
1978 |
||
1979 |
paletteImageAsPseudoFormOn:aDevice |
|
72 | 1980 |
"return a pseudo-deviceForm from the palette image." |
0 | 1981 |
|
89 | 1982 |
|tempImage d temp8| |
1983 |
||
1984 |
d := self depth. |
|
1985 |
(#(1 2 4 8) includes:d) ifTrue:[ |
|
1986 |
" |
|
1987 |
fallback code for some depth's: |
|
1988 |
create a temporary Depth8Image and use its conversion method |
|
1989 |
" |
|
1990 |
temp8 := ByteArray uninitializedNew:(width * height). |
|
1991 |
||
1992 |
bytes expandPixels:d |
|
1993 |
width:width |
|
1994 |
height:height |
|
1995 |
into:temp8 |
|
1996 |
mapping:nil. |
|
1997 |
||
1998 |
tempImage := Image width:width height:height depth:8 fromArray:temp8. |
|
1999 |
tempImage colorMap:colorMap. |
|
2000 |
^ tempImage paletteImageAsPseudoFormOn:aDevice |
|
2001 |
]. |
|
2 | 2002 |
^ self subclassResponsibility |
0 | 2003 |
! |
2004 |
||
193 | 2005 |
paletteImageAsTrueColorFormOn:aDevice |
2006 |
"return a true-color device-form for the palette-image receiver." |
|
2007 |
||
209 | 2008 |
|depth |
2009 |
myDepth "{ Class: SmallInteger }" |
|
2010 |
nColors "{ Class: SmallInteger }" |
|
2011 |
colorValues |
|
193 | 2012 |
scaleRed scaleGreen scaleBlue redShift greenShift blueShift |
194 | 2013 |
form imageBits bestFormat usedDeviceDepth usedDeviceBitsPerPixel |
2014 |
destIndex ok| |
|
193 | 2015 |
|
2016 |
"/ this is a slow fallback method; this ought to be |
|
2017 |
"/ redefined in DepthxImage for more performance. |
|
2018 |
||
2019 |
depth := aDevice depth. |
|
2020 |
myDepth := self bitsPerPixel. |
|
2021 |
myDepth > 12 ifTrue:[ |
|
2022 |
'IMAGE: depth > 12 not supported' errorPrintNL. |
|
2023 |
^ nil |
|
2024 |
]. |
|
2025 |
||
2026 |
"/ gather r/g/b values for all colors in the map ... |
|
2027 |
||
2028 |
nColors := colorMap size. |
|
2029 |
||
2030 |
"/ precompute scales to map from 0..100 into devices range |
|
2031 |
"/ (this may be different for the individual components) |
|
2032 |
||
2033 |
scaleRed := ((1 bitShift:aDevice bitsRed) - 1) / 100. |
|
2034 |
scaleGreen := ((1 bitShift:aDevice bitsGreen) - 1) / 100. |
|
2035 |
scaleBlue := ((1 bitShift:aDevice bitsBlue) - 1) / 100. |
|
2036 |
redShift := aDevice shiftRed. |
|
2037 |
greenShift := aDevice shiftGreen. |
|
2038 |
blueShift := aDevice shiftBlue. |
|
2039 |
||
2040 |
colorValues := Array uninitializedNew:nColors. |
|
2041 |
||
2042 |
1 to:nColors do:[:index | |
|
209 | 2043 |
|clr rv gv bv v "{ Class: SmallInteger }" | |
193 | 2044 |
|
2045 |
clr := colorMap at:index. |
|
2046 |
clr notNil ifTrue:[ |
|
2047 |
rv := (clr red * scaleRed) rounded. |
|
2048 |
gv := (clr green * scaleGreen) rounded. |
|
2049 |
bv := (clr blue * scaleBlue) rounded. |
|
2050 |
||
2051 |
v := rv bitShift:redShift. |
|
2052 |
v := v bitOr:(gv bitShift:greenShift). |
|
2053 |
v := v bitOr:(bv bitShift:blueShift). |
|
2054 |
colorValues at:index put:v. |
|
2055 |
"/ clr print. ' ' print. |
|
2056 |
"/ rv print. ' ' print. gv print. ' ' print. bv print. ' ' print. |
|
2057 |
"/ ' -> ' print. v printNL. |
|
2058 |
||
2059 |
] |
|
2060 |
]. |
|
2061 |
||
2062 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2063 |
usedDeviceDepth := bestFormat at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2064 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
193 | 2065 |
|
2066 |
"/ for now, only support some depths |
|
2067 |
||
2068 |
usedDeviceBitsPerPixel == 16 ifTrue:[ |
|
2069 |
imageBits := ByteArray uninitializedNew:(width * height * 2). |
|
2070 |
||
2071 |
"/ now, walk over the image and replace |
|
2072 |
"/ colorMap indices by color values in the bits array |
|
2073 |
||
2074 |
ok := false. |
|
194 | 2075 |
|
2076 |
%{ /* OPTIONAL */ |
|
2077 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
|
193 | 2078 |
&& __isArray(colorValues) |
2079 |
&& __isByteArray(_INST(bytes)) |
|
2080 |
&& (myDepth == __MKSMALLINT(8)) |
|
2081 |
&& __isByteArray(imageBits)) { |
|
209 | 2082 |
int x, y, w, h, nPix; |
193 | 2083 |
|
2084 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
2085 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
2086 |
OBJ *ap = __ArrayInstPtr(colorValues)->a_element; |
|
2087 |
||
194 | 2088 |
w = __intVal(_INST(width)); |
209 | 2089 |
h = __intVal(_INST(height)); |
2090 |
nPix = w * h; |
|
2091 |
while (nPix > 0) { |
|
2092 |
unsigned idx, v; |
|
2093 |
OBJ clr; |
|
2094 |
||
2095 |
idx = *srcPtr; |
|
2096 |
clr = ap[idx]; |
|
2097 |
v = __intVal(clr); |
|
193 | 2098 |
#ifdef MSBFIRST |
209 | 2099 |
((short *)dstPtr)[0] = v; |
193 | 2100 |
#else |
209 | 2101 |
dstPtr[0] = (v>>8) & 0xFF; |
2102 |
dstPtr[1] = (v) & 0xFF; |
|
193 | 2103 |
#endif |
209 | 2104 |
dstPtr += 2; |
2105 |
srcPtr += 1; |
|
2106 |
nPix--; |
|
193 | 2107 |
} |
2108 |
ok = true; |
|
2109 |
} |
|
2110 |
%}. |
|
2111 |
ok ifFalse:[ |
|
209 | 2112 |
"/ this fallback is only executed if some type is not |
194 | 2113 |
"/ what the primitive expects (for example, if the bytes-instvar |
209 | 2114 |
"/ is not a ByteArray) |
2115 |
"/ or (more important) the source depth is not 8. |
|
2116 |
"/ The code below uses the (depth independent) but slower |
|
2117 |
"/ #valueAtX:y: to fetch pixel values. |
|
193 | 2118 |
destIndex := 1. |
2119 |
0 to:height-1 do:[:y | |
|
2120 |
0 to:width-1 do:[:x | |
|
2121 |
|colorIndex| |
|
2122 |
||
2123 |
colorIndex := self valueAtX:x y:y. |
|
2124 |
imageBits wordAt:destIndex put:(colorValues at:colorIndex + 1) MSB:true. |
|
2125 |
destIndex := destIndex + 2. |
|
2126 |
] |
|
2127 |
] |
|
2128 |
] |
|
2129 |
] ifFalse:[ |
|
2130 |
usedDeviceBitsPerPixel == 32 ifTrue:[ |
|
2131 |
imageBits := ByteArray uninitializedNew:(width * height * 4). |
|
2132 |
||
2133 |
"/ now, walk over the image and replace |
|
2134 |
"/ colorMap indices by color values in the bits array |
|
2135 |
||
2136 |
ok := false. |
|
194 | 2137 |
|
2138 |
%{ /* OPTIONAL */ |
|
2139 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
|
193 | 2140 |
&& __isArray(colorValues) |
2141 |
&& __isByteArray(_INST(bytes)) |
|
2142 |
&& (myDepth == __MKSMALLINT(8)) |
|
2143 |
&& __isByteArray(imageBits)) { |
|
209 | 2144 |
int x, y, w, h, nPix; |
193 | 2145 |
|
2146 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
2147 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
2148 |
OBJ *ap = __ArrayInstPtr(colorValues)->a_element; |
|
2149 |
||
194 | 2150 |
w = __intVal(_INST(width)); |
209 | 2151 |
h = __intVal(_INST(height)); |
2152 |
nPix = w * h; |
|
2153 |
while (nPix > 0) { |
|
2154 |
unsigned idx, v; |
|
2155 |
OBJ clr; |
|
2156 |
||
2157 |
idx = *srcPtr; |
|
2158 |
clr = ap[idx]; |
|
2159 |
v = __intVal(clr); |
|
193 | 2160 |
#ifdef MSBFIRST |
209 | 2161 |
((long *)dstPtr)[0] = v; |
193 | 2162 |
#else |
209 | 2163 |
dstPtr[0] = (v>>24) & 0xFF; |
2164 |
dstPtr[1] = (v>>16) & 0xFF; |
|
2165 |
dstPtr[2] = (v>>8) & 0xFF; |
|
2166 |
dstPtr[3] = (v) & 0xFF; |
|
193 | 2167 |
#endif |
209 | 2168 |
dstPtr += 4; |
2169 |
srcPtr += 1; |
|
2170 |
nPix--; |
|
193 | 2171 |
} |
2172 |
ok = true; |
|
2173 |
} |
|
2174 |
%}. |
|
2175 |
ok ifFalse:[ |
|
209 | 2176 |
"/ this fallback is only executed if some type is not |
194 | 2177 |
"/ what the primitive expects (for example, if the bytes-instvar |
209 | 2178 |
"/ is not a ByteArray) |
2179 |
"/ or (more important) the source depth is not 8. |
|
2180 |
"/ The code below uses the (depth independent) but slower |
|
2181 |
"/ #valueAtX:y: to fetch pixel values. |
|
194 | 2182 |
|
193 | 2183 |
destIndex := 1. |
2184 |
0 to:height-1 do:[:y | |
|
2185 |
0 to:width-1 do:[:x | |
|
2186 |
|colorIndex| |
|
2187 |
||
2188 |
colorIndex := self valueAtX:x y:y. |
|
2189 |
imageBits doubleWordAt:destIndex put:(colorValues at:colorIndex + 1) MSB:true. |
|
2190 |
destIndex := destIndex + 4. |
|
2191 |
] |
|
2192 |
] |
|
2193 |
] |
|
2194 |
] |
|
2195 |
]. |
|
2196 |
||
2197 |
imageBits isNil ifTrue:[ |
|
2198 |
'IMAGE: unimplemented trueColor depth in paletteImageAsTrueColorFormOn:' errorPrintNL. |
|
2199 |
^ self paletteImageAsMonoFormOn:aDevice |
|
2200 |
]. |
|
2201 |
||
2202 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
|
2203 |
form isNil ifTrue:[^ nil]. |
|
2204 |
form initGC. |
|
2205 |
||
2206 |
form |
|
2207 |
copyBitsFrom:imageBits bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
|
2208 |
width:width height:height |
|
2209 |
x:0 y:0 toX:0 y:0. |
|
2210 |
||
2211 |
^ form |
|
2212 |
||
2213 |
"Created: 20.10.1995 / 22:05:10 / cg" |
|
2214 |
"Modified: 21.10.1995 / 19:30:26 / cg" |
|
0 | 2215 |
! ! |
2216 |
||
283 | 2217 |
!Image methodsFor:'converting rgb images'! |
2218 |
||
2219 |
rgbImageAs2PlaneFormOn:aDevice |
|
2220 |
"return a 2-bit device form for aDevice from the rgb picture, |
|
2221 |
using a threshold algorithm. |
|
2222 |
(i.e. grey value < 0.25 -> black // 0.25..0.5 -> darkgrey // |
|
2223 |
0.5 .. 0.75 -> lightgrey // > 0.75 -> white)." |
|
2224 |
||
2225 |
^ self subclassResponsibility |
|
0 | 2226 |
! |
2227 |
||
283 | 2228 |
rgbImageAs8BitGreyFormOn:aDevice |
2229 |
"return an 8-bit greyForm from the rgb picture" |
|
0 | 2230 |
|
2 | 2231 |
^ self subclassResponsibility |
0 | 2232 |
! |
2233 |
||
283 | 2234 |
rgbImageAsFormOn:aDevice |
2235 |
"convert am rgb image to a device-form on aDevice. |
|
2236 |
Return the device-form." |
|
2237 |
||
2238 |
|visual| |
|
2239 |
||
2240 |
visual := aDevice visualType. |
|
2241 |
(visual == #StaticGray) ifTrue:[ |
|
2242 |
^ self rgbImageAsGreyFormOn:aDevice |
|
0 | 2243 |
]. |
283 | 2244 |
(visual == #TrueColor) ifTrue:[ |
2245 |
^ self rgbImageAsTrueColorFormOn:aDevice |
|
0 | 2246 |
]. |
283 | 2247 |
^ self rgbImageAsPseudoFormOn:aDevice |
0 | 2248 |
! |
2249 |
||
283 | 2250 |
rgbImageAsGreyFormOn:aDevice |
2251 |
"convert an rgb image to a grey device-form on aDevice |
|
2252 |
(for greyscale displays)" |
|
2253 |
||
2254 |
|deviceDepth| |
|
2255 |
||
2256 |
deviceDepth := aDevice depth. |
|
2257 |
||
2258 |
"I have specially tuned methods for monochrome" |
|
2259 |
(deviceDepth == 1) ifTrue:[ |
|
2260 |
DitherAlgorithm == #error ifTrue:[ |
|
2261 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
2262 |
]. |
|
2263 |
DitherAlgorithm == #pattern ifTrue:[ |
|
2264 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
2265 |
]. |
|
2266 |
^ self rgbImageAsMonoFormOn:aDevice |
|
0 | 2267 |
]. |
2268 |
||
283 | 2269 |
"and for 2plane greyscale (i.e. NeXTs)" |
2270 |
(deviceDepth == 2) ifTrue:[ |
|
2271 |
DitherAlgorithm == #error ifTrue:[ |
|
2272 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
72 | 2273 |
]. |
283 | 2274 |
DitherAlgorithm == #pattern ifTrue:[ |
2275 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
2276 |
]. |
|
2277 |
^ self rgbImageAs2PlaneFormOn:aDevice |
|
0 | 2278 |
]. |
2279 |
||
283 | 2280 |
(deviceDepth == 8) ifTrue:[ |
2281 |
^ self rgbImageAs8BitGreyFormOn:aDevice |
|
0 | 2282 |
]. |
2283 |
||
283 | 2284 |
"mhmh need another converter ... |
2285 |
till then we do:" |
|
2286 |
DitherAlgorithm == #error ifTrue:[ |
|
2287 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
2288 |
]. |
|
2289 |
DitherAlgorithm == #pattern ifTrue:[ |
|
2290 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
2291 |
]. |
|
2292 |
^ self rgbImageAsMonoFormOn:aDevice |
|
2293 |
! |
|
2294 |
||
2295 |
rgbImageAsMonoFormOn:aDevice |
|
2296 |
"return a 1-bit monochrome form for aDevice from the rgb picture, |
|
2297 |
using a threshold algorithm. |
|
2298 |
(i.e. grey value < 0.5 -> black, grey value >= 0.5 -> white)." |
|
2299 |
||
2300 |
^ self subclassResponsibility |
|
193 | 2301 |
! |
2302 |
||
283 | 2303 |
rgbImageAsPatternDitheredGreyFormOn:aDevice |
2304 |
"return a dithered greyForm for aDevice from the palette picture. |
|
2305 |
works for any destination depth. |
|
2306 |
A slow algorithm, using draw into the form (which indirectly does |
|
2307 |
the dither) - should be rewritten." |
|
2308 |
||
2309 |
^ self subclassResponsibility |
|
2310 |
! |
|
2311 |
||
2312 |
rgbImageAsPseudoFormOn:aDevice |
|
2313 |
"return a pseudocolor form from the rgb-picture" |
|
2314 |
||
2315 |
^ self subclassResponsibility |
|
2316 |
! |
|
2317 |
||
2318 |
rgbImageAsTrueColorFormOn:aDevice |
|
2319 |
"return a truecolor form from the rgb-picture." |
|
2320 |
||
2321 |
|bestFormat usedDeviceDepth usedDeviceBitsPerPixel depth |
|
2322 |
form| |
|
193 | 2323 |
|
2324 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2325 |
usedDeviceDepth := bestFormat at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2326 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
193 | 2327 |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
2328 |
"/ kludge for 15bit XFree servers (should return 16 here - does not) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
2329 |
usedDeviceBitsPerPixel == 15 ifTrue:[ |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
2330 |
usedDeviceBitsPerPixel := 16 |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
2331 |
]. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
2332 |
|
283 | 2333 |
"/ |
2334 |
"/ only the trivial case, where the depths match |
|
2335 |
"/ is handled here; conversions are supposed to |
|
2336 |
"/ be done in concrete subclasses (see D24Image) |
|
2337 |
"/ |
|
2338 |
self bitsPerPixel == usedDeviceBitsPerPixel ifFalse:[ |
|
2339 |
'IMAGE: unimplemented trueColor depth in rgbImageAsTrueColorFormOn:' errorPrintNL. |
|
2340 |
^ self rgbImageAsMonoFormOn:aDevice |
|
193 | 2341 |
]. |
2342 |
||
283 | 2343 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
2344 |
form isNil ifTrue:[ |
|
2345 |
'IMAGE: display bitmap creation failed' errorPrintNL. |
|
2346 |
^ nil |
|
193 | 2347 |
]. |
2348 |
form initGC. |
|
2349 |
||
2350 |
form |
|
283 | 2351 |
copyBitsFrom:bytes bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
193 | 2352 |
width:width height:height |
2353 |
x:0 y:0 toX:0 y:0. |
|
2354 |
||
2355 |
^ form |
|
2356 |
||
283 | 2357 |
"Modified: 21.10.1995 / 19:30:11 / cg" |
2358 |
! ! |
|
2359 |
||
2360 |
!Image methodsFor:'copying'! |
|
2361 |
||
2362 |
postCopy |
|
592 | 2363 |
"redefined to also copy the pixels and the colorMap |
2364 |
and clear out any device handles in the copy." |
|
2365 |
||
283 | 2366 |
bytes := bytes copy. |
2367 |
colorMap := colorMap deepCopy. |
|
2368 |
device := deviceForm := monoDeviceForm := fullColorDeviceForm := nil |
|
592 | 2369 |
|
2370 |
"Modified: 23.4.1996 / 11:12:03 / cg" |
|
283 | 2371 |
! ! |
2372 |
||
2373 |
!Image methodsFor:'displaying'! |
|
2374 |
||
2375 |
displayOn:aGC at:aPoint |
|
592 | 2376 |
"draw the receiver in the graphicsContext, aGC. |
283 | 2377 |
Smalltalk-80 compatibility" |
2378 |
||
672 | 2379 |
self displayOn:aGC x:aPoint x y:aPoint y. |
2380 |
||
2381 |
"Modified: 12.5.1996 / 20:16:38 / cg" |
|
2382 |
! |
|
2383 |
||
2384 |
displayOn:aGC x:x y:y |
|
2385 |
"draw the receiver in the graphicsContext, aGC. |
|
2386 |
Smalltalk-80 compatibility" |
|
2387 |
||
2388 |
aGC displayForm:self x:x y:y. |
|
592 | 2389 |
|
2390 |
"Modified: 23.4.1996 / 11:12:31 / cg" |
|
672 | 2391 |
"Created: 12.5.1996 / 20:14:31 / cg" |
283 | 2392 |
! ! |
2393 |
||
2394 |
!Image methodsFor:'enumerating'! |
|
2395 |
||
2396 |
atY:y from:x1 to:x2 do:aBlock |
|
2397 |
"perform aBlock for each pixel from x1 to x2 in row y. |
|
2398 |
The block is passed the color at each pixel. |
|
2399 |
The code here provides a generic and slow implementation, and |
|
2400 |
should be redefined in concrete subclasses, to avoid some processing |
|
2401 |
when going from pixel to pixel (i.e. the byte-index and mask computations |
|
2402 |
and also the color allocation)." |
|
2403 |
||
2404 |
|xStart "{Class: SmallInteger }" |
|
2405 |
xEnd "{Class: SmallInteger }"| |
|
2406 |
||
2407 |
xStart := x1. |
|
2408 |
xEnd := x2. |
|
2409 |
xStart to:xEnd do:[:xRun | |
|
2410 |
aBlock value:(self atX:xRun y:y) |
|
2411 |
] |
|
2412 |
! |
|
2413 |
||
2414 |
colorsFromX:xStart y:yStart toX:xEnd y:yEnd do:aBlock |
|
2415 |
"perform aBlock for each color in a rectangular area of the image. |
|
2416 |
Notice, that x and y coordinates start at 0@0 for the upper left corner. |
|
2417 |
The block is passed the x and y coordinates and pixelValue at each pixel. |
|
2418 |
The code here provides a generic and slow implementation, and |
|
2419 |
should be redefined in concrete subclasses, to avoid some processing |
|
2420 |
when going from pixel to pixel (i.e. the byte-index and mask computations, |
|
2421 |
and especially, the color allocations)." |
|
2422 |
||
2423 |
|xS "{Class: SmallInteger }" |
|
2424 |
xE "{Class: SmallInteger }"| |
|
2425 |
||
2426 |
xS := xStart. |
|
2427 |
xE := xEnd. |
|
2428 |
yStart to:yEnd do:[:yRun | |
|
2429 |
self atY:yRun from:xStart to:xEnd do:[:xRun :color | |
|
2430 |
aBlock value:xRun value:yRun value:color |
|
2431 |
] |
|
2432 |
] |
|
2433 |
! |
|
2434 |
||
2435 |
valueAtY:y from:x1 to:x2 do:aBlock |
|
2436 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
|
2437 |
Notice, that x and y coordinates start at 0@0 for the upper left corner. |
|
2438 |
The block is passed the x coordinate and the pixelValue at each pixel. |
|
2439 |
(see also Image>>atY:from:to:do:). |
|
2440 |
The code here provides a generic and slow implementation, and |
|
2441 |
should be redefined in concrete subclasses, to avoid some processing |
|
2442 |
when going from pixel to pixel (i.e. the byte-index and mask computations)." |
|
2443 |
||
2444 |
|xStart "{Class: SmallInteger }" |
|
2445 |
xEnd "{Class: SmallInteger }"| |
|
2446 |
||
2447 |
xStart := x1. |
|
2448 |
xEnd := x2. |
|
2449 |
xStart to:xEnd do:[:xRun | |
|
692 | 2450 |
aBlock value:xRun value:(self valueAtX:xRun y:y) |
283 | 2451 |
] |
692 | 2452 |
|
2453 |
"Modified: 17.5.1996 / 12:03:59 / cg" |
|
283 | 2454 |
! |
2455 |
||
2456 |
valuesFromX:xStart y:yStart toX:xEnd y:yEnd do:aBlock |
|
2457 |
"perform aBlock for each pixelValue in a rectangular area of the image. |
|
2458 |
Notice, that x and y coordinates start at 0@0 for the upper left corner. |
|
2459 |
The block is passed the x and y coordinates and pixelValue at each pixel. |
|
2460 |
The code here provides a generic and slow implementation, and |
|
2461 |
should be redefined in concrete subclasses, to avoid some processing |
|
2462 |
when going from pixel to pixel (i.e. the byte-index and mask computations)." |
|
2463 |
||
2464 |
|xS "{Class: SmallInteger }" |
|
2465 |
xE "{Class: SmallInteger }"| |
|
2466 |
||
2467 |
xS := xStart. |
|
2468 |
xE := xEnd. |
|
2469 |
yStart to:yEnd do:[:yRun | |
|
2470 |
self valueAtY:yRun from:xStart to:xEnd do:[:xRun :pixel | |
|
2471 |
aBlock value:xRun value:yRun value:pixel |
|
2472 |
] |
|
2473 |
] |
|
0 | 2474 |
! ! |
2475 |
||
2476 |
!Image methodsFor:'image manipulations'! |
|
2477 |
||
283 | 2478 |
colorMapProcessing:aBlock |
2479 |
"a helper for all kinds of colormap manipulations. |
|
592 | 2480 |
The argument, aBlock is called for every colormap entry, |
2481 |
and the returned value will replace the original entry in the map. |
|
283 | 2482 |
This will fail for non-palette images. |
592 | 2483 |
See examples in Image>>copyWithColorMapProcessing:" |
283 | 2484 |
|
2485 |
|nColors "{ Class: SmallInteger }"| |
|
2486 |
||
2487 |
colorMap isNil ifTrue:[ |
|
592 | 2488 |
^ self error:'image has no colormap' |
283 | 2489 |
]. |
2490 |
||
2491 |
nColors := colorMap size. |
|
2492 |
1 to:nColors do:[:index | |
|
592 | 2493 |
|clr| |
2494 |
||
2495 |
clr := colorMap at:index. |
|
2496 |
clr notNil ifTrue:[ |
|
2497 |
colorMap at:index put:(aBlock value:clr) |
|
2498 |
] |
|
283 | 2499 |
] |
592 | 2500 |
|
2501 |
"Modified: 23.4.1996 / 11:13:55 / cg" |
|
283 | 2502 |
! |
2503 |
||
89 | 2504 |
copyWithColorMapProcessing:aBlock |
81 | 2505 |
"a helper to create & return new images based on the receiver with |
89 | 2506 |
some colorMap processing. The receiver is copied, and the copied images |
2507 |
colormap is modified by replacing entries with the result of the processing block, |
|
2508 |
which is called with the original color values. The block is supposed to return |
|
2509 |
a color." |
|
81 | 2510 |
|
2511 |
|newImage| |
|
2512 |
||
2513 |
newImage := self copy. |
|
2514 |
newImage colorMap isNil ifTrue:[ |
|
2515 |
self error:'no colormap in image'. |
|
2516 |
^ nil |
|
2517 |
]. |
|
2518 |
||
2519 |
" |
|
2520 |
the code below manipulates the colormap. |
|
2521 |
For non-palette images, special code is required |
|
2522 |
" |
|
89 | 2523 |
newImage colorMapProcessing:aBlock. |
81 | 2524 |
^ newImage |
2525 |
||
2526 |
" |
|
2527 |
leave red component only: |
|
2528 |
||
2529 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 2530 |
copyWithColorMapProcessing:[:clr | Color red:(clr red) green:0 blue:0] |
81 | 2531 |
" |
89 | 2532 |
|
81 | 2533 |
" |
2534 |
make it reddish: |
|
2535 |
||
2536 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 2537 |
copyWithColorMapProcessing:[:clr | Color red:((clr red * 2) min:100) green:clr green blue:clr blue] |
81 | 2538 |
" |
89 | 2539 |
|
81 | 2540 |
" |
2541 |
invert: |
|
2542 |
||
2543 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 2544 |
copyWithColorMapProcessing:[:clr | Color red:(100 - clr red) green:(100 - clr green) blue:(100 - clr green)] |
81 | 2545 |
" |
89 | 2546 |
|
81 | 2547 |
" |
2548 |
lighter: |
|
2549 |
||
2550 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 2551 |
copyWithColorMapProcessing:[:clr | |r g b| |
2552 |
r := clr red. g := clr green. b := clr blue. |
|
2553 |
Color red:(r + (100-r//2)) |
|
2554 |
green:(g + (100-g//2)) |
|
2555 |
blue:(b + (100-b//2))] |
|
81 | 2556 |
" |
89 | 2557 |
|
81 | 2558 |
" |
2559 |
darker: |
|
2560 |
||
2561 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 2562 |
copyWithColorMapProcessing:[:clr | Color red:(clr red//2) green:(clr green // 2) blue:(clr blue // 2)] |
81 | 2563 |
" |
2564 |
! |
|
2565 |
||
283 | 2566 |
darkened |
2567 |
"return a new image which is slightly darker than the receiver. |
|
2568 |
The receiver must be a palette image (currently). |
|
592 | 2569 |
CAVEAT: Need an argument, which specifies by how much it should be darker." |
283 | 2570 |
|
2571 |
^ self |
|
592 | 2572 |
copyWithColorMapProcessing:[:clr | |
2573 |
Color red:(clr red // 2) |
|
2574 |
green:(clr green // 2) |
|
2575 |
blue:(clr blue // 2)] |
|
283 | 2576 |
|
2577 |
" |
|
2578 |
(Image fromFile:'bitmaps/claus.gif') inspect |
|
2579 |
(Image fromFile:'bitmaps/claus.gif') darkened inspect |
|
2580 |
" |
|
592 | 2581 |
|
2582 |
"Modified: 23.4.1996 / 11:14:34 / cg" |
|
283 | 2583 |
! |
2584 |
||
2585 |
flipHorizontal |
|
2586 |
"inplace horizontal flip" |
|
2587 |
||
2588 |
|w "{Class: SmallInteger }" |
|
2589 |
h "{Class: SmallInteger }" |
|
2590 |
c2 "{Class: SmallInteger }" |
|
2591 |
value | |
|
2592 |
||
2593 |
w := width - 1. |
|
2594 |
h := height - 1. |
|
2595 |
||
2596 |
0 to:h do:[:row | |
|
2597 |
c2 := w. |
|
2598 |
0 to:(w // 2) do:[:col | |
|
2599 |
value := self valueAtX:col y:row. |
|
2600 |
self atX:col y:row putValue:(self valueAtX:c2 y:row). |
|
2601 |
self atX:c2 y:row putValue:value. |
|
2602 |
c2 := c2 - 1. |
|
2603 |
] |
|
81 | 2604 |
]. |
283 | 2605 |
"flush device info" |
2606 |
self restored |
|
2607 |
! |
|
2608 |
||
2609 |
flipVertical |
|
2610 |
"inplace vertical flip" |
|
2611 |
||
2612 |
|h "{Class: SmallInteger }" |
|
2613 |
bytesPerRow "{Class: SmallInteger }" |
|
2614 |
buffer |
|
2615 |
indexLow "{Class: SmallInteger }" |
|
2616 |
indexHi "{Class: SmallInteger }"| |
|
2617 |
||
2618 |
bytesPerRow := self bytesPerRow. |
|
2619 |
buffer := ByteArray new:bytesPerRow. |
|
2620 |
||
2621 |
h := height - 1. |
|
2622 |
||
2623 |
indexLow := 1. |
|
2624 |
indexHi := bytesPerRow * h + 1. |
|
2625 |
||
2626 |
0 to:(h // 2) do:[:row | |
|
2627 |
buffer replaceFrom:1 to:bytesPerRow with:bytes startingAt:indexLow. |
|
2628 |
bytes replaceFrom:indexLow to:(indexLow + bytesPerRow - 1) with:bytes startingAt:indexHi. |
|
2629 |
bytes replaceFrom:indexHi to:(indexHi + bytesPerRow - 1) with:buffer startingAt:1. |
|
2630 |
indexLow := indexLow + bytesPerRow. |
|
2631 |
indexHi := indexHi - bytesPerRow. |
|
2632 |
]. |
|
2633 |
"flush device info" |
|
2634 |
self restored |
|
2635 |
! |
|
2636 |
||
2637 |
hardMagnifiedBy:scalePoint |
|
2638 |
"return a new image magnified by scalePoint, aPoint. |
|
2639 |
This is the general magnification method, handling non-integral values. |
|
2640 |
It is slower than the integral magnification method." |
|
2641 |
||
2642 |
|mX |
|
2643 |
mY |
|
2644 |
newWidth "{ Class: SmallInteger }" |
|
2645 |
newHeight "{ Class: SmallInteger }" |
|
2646 |
w "{ Class: SmallInteger }" |
|
2647 |
h "{ Class: SmallInteger }" |
|
2648 |
newImage newBits bitsPerPixel newBytesPerRow |
|
2649 |
value srcRow| |
|
2650 |
||
2651 |
mX := scalePoint x. |
|
2652 |
mY := scalePoint y. |
|
2653 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil]. |
|
2654 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
|
2655 |
||
2656 |
newWidth := (width * mX) truncated. |
|
2657 |
newHeight := (height * mY) truncated. |
|
2658 |
||
2659 |
bitsPerPixel := self depth. |
|
2660 |
newBytesPerRow := ((newWidth * bitsPerPixel) + 7) // 8. |
|
2661 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * newHeight). |
|
2662 |
||
2663 |
newImage := self species new. |
|
2664 |
newImage bits:newBits. |
|
2665 |
newImage width:newWidth. |
|
2666 |
newImage height:newHeight. |
|
2667 |
newImage photometric:photometric. |
|
2668 |
newImage samplesPerPixel:samplesPerPixel. |
|
2669 |
newImage bitsPerSample:bitsPerSample. |
|
2670 |
newImage colorMap:colorMap copy. |
|
2671 |
||
2672 |
"walk over destination image fetching pixels from source image" |
|
2673 |
||
2674 |
w := newWidth - 1. |
|
2675 |
h := newHeight - 1. |
|
2676 |
||
2677 |
0 to:h do:[:row | |
|
2678 |
srcRow := (row // mY). |
|
2679 |
0 to:w do:[:col | |
|
2680 |
value := self valueAtX:(col // mX) y:srcRow. |
|
2681 |
newImage atX:col y:row putValue:value. |
|
89 | 2682 |
] |
283 | 2683 |
]. |
2684 |
||
2685 |
^ newImage |
|
2686 |
||
2687 |
"((Image fromFile:'bitmaps/claus.gif') magnifiedBy:0.5@0.5)" |
|
2688 |
! |
|
2689 |
||
2690 |
hardRotated:degrees |
|
592 | 2691 |
"raise an error - rotation by arbitrary angles is not yet implemented" |
2692 |
||
283 | 2693 |
self error:'not yet implemented' |
592 | 2694 |
|
2695 |
"Modified: 23.4.1996 / 11:15:33 / cg" |
|
81 | 2696 |
! |
2697 |
||
54 | 2698 |
lightened |
89 | 2699 |
"return a new image which is slightly brighter than the receiver. |
2700 |
The receiver must be a palette image (currently). |
|
592 | 2701 |
CAVEAT: Need an argument, which specifies by how much it should be lighter." |
54 | 2702 |
|
81 | 2703 |
^ self |
592 | 2704 |
copyWithColorMapProcessing:[:clr | |r g b| |
2705 |
r := clr red. |
|
2706 |
g := clr green. |
|
2707 |
b := clr blue. |
|
2708 |
Color red:(r + (100-r//2)) |
|
2709 |
green:(g + (100-g//2)) |
|
2710 |
blue:(b + (100-b//2))] |
|
81 | 2711 |
|
2712 |
" |
|
2713 |
(Image fromFile:'bitmaps/claus.gif') inspect |
|
2714 |
(Image fromFile:'bitmaps/claus.gif') lightened inspect |
|
2715 |
(Image fromFile:'bitmaps/claus.gif') darkened inspect |
|
2716 |
(Image fromFile:'bitmaps/claus.gif') darkened darkened inspect |
|
2717 |
" |
|
592 | 2718 |
|
2719 |
"Modified: 23.4.1996 / 11:14:41 / cg" |
|
54 | 2720 |
! |
2721 |
||
134 | 2722 |
magnifiedBy:scale |
81 | 2723 |
"return a new image magnified by scalePoint, aPoint. |
72 | 2724 |
If non-integral magnify is asked for, pass the work on to 'hardMagnifyBy:' |
2725 |
while simple (integral) magnifications are handled here." |
|
0 | 2726 |
|
81 | 2727 |
|scalePoint mX mY |
46 | 2728 |
magX "{ Class: SmallInteger }" "new version of stc can find this out itself..." |
2729 |
magY "{ Class: SmallInteger }" |
|
2730 |
srcOffset "{ Class: SmallInteger }" |
|
2731 |
dstOffset "{ Class: SmallInteger }" |
|
2732 |
w "{ Class: SmallInteger }" |
|
2733 |
h "{ Class: SmallInteger }" |
|
2734 |
first |
|
81 | 2735 |
newWidth newHeight newImage newBits |
2736 |
bitsPerPixel newBytesPerRow oldBytesPerRow| |
|
0 | 2737 |
|
81 | 2738 |
scalePoint := scale asPoint. |
2739 |
mX := scalePoint asPoint x. |
|
2740 |
mY := scalePoint asPoint y. |
|
46 | 2741 |
((mX <= 0) or:[mY <= 0]) ifTrue:[^ nil]. |
0 | 2742 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
2743 |
||
12 | 2744 |
((mX isMemberOf:SmallInteger) and:[mY isMemberOf:SmallInteger]) ifFalse:[ |
134 | 2745 |
^ self hardMagnifiedBy:scalePoint |
0 | 2746 |
]. |
2747 |
||
2 | 2748 |
bitsPerPixel := self depth. |
0 | 2749 |
oldBytesPerRow := ((width * bitsPerPixel) + 7) // 8. |
2750 |
||
46 | 2751 |
w := width. |
2752 |
h := height. |
|
2753 |
magX := mX. |
|
2754 |
magY := mY. |
|
2755 |
||
2756 |
newWidth := w * mX. |
|
2757 |
newHeight := h * mY. |
|
0 | 2758 |
newBytesPerRow := ((newWidth * bitsPerPixel) + 7) // 8. |
2759 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * newHeight). |
|
2760 |
||
2761 |
newImage := self species new. |
|
2 | 2762 |
newImage bits:newBits. |
0 | 2763 |
newImage width:newWidth. |
2764 |
newImage height:newHeight. |
|
2765 |
newImage photometric:photometric. |
|
2766 |
newImage samplesPerPixel:samplesPerPixel. |
|
2767 |
newImage bitsPerSample:bitsPerSample. |
|
2768 |
newImage colorMap:colorMap copy. |
|
2769 |
||
2770 |
mX = 1 ifTrue:[ |
|
72 | 2771 |
"expand rows only" |
2772 |
srcOffset := 1. |
|
2773 |
dstOffset := 1. |
|
0 | 2774 |
|
72 | 2775 |
1 to:h do:[:row | |
2776 |
1 to:mY do:[:i | |
|
2777 |
newBits replaceFrom:dstOffset |
|
2778 |
to:(dstOffset + oldBytesPerRow - 1) |
|
2779 |
with:bytes |
|
2780 |
startingAt:srcOffset. |
|
2781 |
dstOffset := dstOffset + newBytesPerRow |
|
2782 |
]. |
|
2783 |
srcOffset := srcOffset + oldBytesPerRow. |
|
2784 |
]. |
|
0 | 2785 |
] ifFalse:[ |
72 | 2786 |
"expand cols" |
2787 |
(mX > 1) ifTrue:[ |
|
2788 |
dstOffset := 1. |
|
2789 |
srcOffset := 1. |
|
2790 |
1 to:h do:[:row | |
|
2791 |
self magnifyRowFrom:bytes |
|
2792 |
offset:srcOffset |
|
2793 |
into:newBits |
|
2794 |
offset:dstOffset |
|
2795 |
factor:mX. |
|
0 | 2796 |
|
72 | 2797 |
first := dstOffset. |
2798 |
dstOffset := dstOffset + newBytesPerRow. |
|
2799 |
" and copy for row expansion " |
|
2800 |
2 to:mY do:[:i | |
|
2801 |
newBits replaceFrom:dstOffset |
|
2802 |
to:(dstOffset + newBytesPerRow - 1) |
|
2803 |
with:newBits |
|
2804 |
startingAt:first. |
|
2805 |
dstOffset := dstOffset + newBytesPerRow |
|
2806 |
]. |
|
2807 |
srcOffset := srcOffset + oldBytesPerRow. |
|
2808 |
]. |
|
2809 |
] |
|
0 | 2810 |
]. |
2811 |
^ newImage |
|
2812 |
||
134 | 2813 |
"((Image fromFile:'bitmaps/claus.gif') magnifiedBy:1@2)" |
2 | 2814 |
! |
2815 |
||
154 | 2816 |
magnifiedPreservingRatioTo:anExtent |
2817 |
"return a new image magnified to fit into anExtent, |
|
2818 |
preserving the receivers width/height ratio. |
|
2819 |
(i.e. not distorting the image). |
|
2820 |
See also #magnifiedTo: and #magnifiedBy:" |
|
2821 |
||
2822 |
|rX rY| |
|
2823 |
||
2824 |
rX := anExtent x / self width. |
|
2825 |
rY := anExtent y / self height. |
|
2826 |
^ self magnifiedBy:(rX min:rY) |
|
2827 |
||
2828 |
" |
|
2829 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedPreservingRatioTo:100@100) |
|
2830 |
||
2831 |
in contrast to: |
|
2832 |
||
2833 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedTo:100@100) |
|
2834 |
" |
|
2835 |
! |
|
2836 |
||
134 | 2837 |
magnifiedTo:anExtent |
154 | 2838 |
"return a new image magnified to have the size specified by extent. |
2839 |
This may distort the image if the arguments ratio is not the images ratio. |
|
2840 |
See also #magnifiedPreservingRatioTo: and #magnifiedBy:" |
|
89 | 2841 |
|
134 | 2842 |
^ self magnifiedBy:(anExtent / self extent) |
89 | 2843 |
|
2844 |
" |
|
134 | 2845 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedTo:100@100) |
154 | 2846 |
|
2847 |
in contrast to: |
|
2848 |
||
2849 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedPreservingRatioTo:100@100) |
|
89 | 2850 |
" |
2851 |
! |
|
2852 |
||
12 | 2853 |
rotated:degrees |
2854 |
"return a new image from the old one, by rotating the image |
|
46 | 2855 |
degrees clockwise. |
592 | 2856 |
CAVEAT: Currently, only rotation by a multiple of 90 degrees is implemented." |
12 | 2857 |
|
46 | 2858 |
|w "{Class: SmallInteger }" |
2859 |
h "{Class: SmallInteger }" |
|
12 | 2860 |
c2 "{Class: SmallInteger }" |
692 | 2861 |
r2 "{Class: SmallInteger }" |
2862 |
nW "{Class: SmallInteger }" |
|
2863 |
nH "{Class: SmallInteger }" |
|
24 | 2864 |
newImage newBits newBytesPerRow d| |
12 | 2865 |
|
2866 |
d := degrees. |
|
2867 |
[d < 0] whileTrue:[d := d + 360]. |
|
2868 |
d >= 360 ifTrue:[d := d \\ 360]. |
|
46 | 2869 |
d := d truncated. |
2870 |
d == 0 ifTrue:[^ self]. |
|
2871 |
((d ~~ 90) and:[(d ~~ 270) and:[d ~~ 180]]) ifTrue:[ |
|
592 | 2872 |
^ self hardRotated:d |
12 | 2873 |
]. |
2874 |
||
692 | 2875 |
d == 180 ifTrue:[ |
2876 |
nW := width. |
|
2877 |
nH := height. |
|
2878 |
] ifFalse:[ |
|
2879 |
nW := height. |
|
2880 |
nH := width. |
|
2881 |
]. |
|
2882 |
||
2883 |
newBytesPerRow := ((nW * self depth) + 7) // 8. |
|
2884 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * nH). |
|
12 | 2885 |
|
2886 |
newImage := self species new. |
|
2887 |
newImage bits:newBits. |
|
692 | 2888 |
newImage width:nW. |
2889 |
newImage height:nH. |
|
12 | 2890 |
newImage photometric:photometric. |
2891 |
newImage samplesPerPixel:samplesPerPixel. |
|
2892 |
newImage bitsPerSample:bitsPerSample. |
|
2893 |
newImage colorMap:colorMap copy. |
|
2894 |
||
2895 |
w := width - 1. |
|
2896 |
h := height - 1. |
|
2897 |
||
2898 |
d == 90 ifTrue:[ |
|
592 | 2899 |
0 to:h do:[:row | |
2900 |
c2 := h-row. |
|
692 | 2901 |
self valueAtY:row from:0 to:w do:[:col :pixel | |
2902 |
newImage atX:c2 y:col putValue:pixel. |
|
592 | 2903 |
] |
692 | 2904 |
"/ 0 to:w do:[:col | |
2905 |
"/ newImage atX:c2 y:col putValue:(self valueAtX:col y:row). |
|
2906 |
"/ ] |
|
592 | 2907 |
] |
12 | 2908 |
]. |
46 | 2909 |
d == 180 ifTrue:[ |
592 | 2910 |
0 to:h do:[:row | |
692 | 2911 |
r2 := h - row. |
2912 |
self valueAtY:row from:0 to:w do:[:col :pixel | |
|
2913 |
newImage atX:(w-col) y:r2 putValue:pixel. |
|
592 | 2914 |
] |
692 | 2915 |
"/ 0 to:w do:[:col | |
2916 |
"/ newImage atX:(w-col) y:r2 putValue:(self valueAtX:col y:row). |
|
2917 |
"/ ] |
|
592 | 2918 |
] |
46 | 2919 |
]. |
12 | 2920 |
d == 270 ifTrue:[ |
592 | 2921 |
0 to:h do:[:row | |
692 | 2922 |
self valueAtY:row from:0 to:w do:[:col :pixel | |
2923 |
newImage atX:row y:(w-col) putValue:pixel. |
|
592 | 2924 |
] |
692 | 2925 |
"/ 0 to:w do:[:col | |
2926 |
"/ newImage atX:row y:(w-col) putValue:(self valueAtX:col y:row). |
|
2927 |
"/ ] |
|
592 | 2928 |
] |
12 | 2929 |
]. |
2930 |
^ newImage |
|
592 | 2931 |
|
692 | 2932 |
" |
2933 |
|i| |
|
2934 |
||
2935 |
i := Image fromFile:'claus.gif'. |
|
2936 |
i inspect. |
|
2937 |
(i rotated:90) inspect. |
|
2938 |
(i rotated:180) inspect. |
|
2939 |
(i rotated:270) inspect. |
|
2940 |
" |
|
2941 |
||
2942 |
"Modified: 17.5.1996 / 12:06:34 / cg" |
|
2943 |
! |
|
2944 |
||
2945 |
withPixelFunctionApplied:pixelFunctionBlock |
|
2946 |
"return a new image from the old one, by applying a pixel processor |
|
2947 |
(read `Behond photography, by Gerard J. Holzmann; |
|
2948 |
ISBM 0-13-074410-7) |
|
2949 |
See blurred / oilPointed as examples ...)" |
|
2950 |
||
2951 |
|w "{Class: SmallInteger }" |
|
2952 |
h "{Class: SmallInteger }" |
|
2953 |
newImage newBits newBytesPerRow d| |
|
2954 |
||
2955 |
newBytesPerRow := ((width * self depth) + 7) // 8. |
|
2956 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * height). |
|
2957 |
||
2958 |
newImage := self species new. |
|
2959 |
newImage bits:newBits. |
|
2960 |
newImage width:width. |
|
2961 |
newImage height:height. |
|
2962 |
newImage photometric:photometric. |
|
2963 |
newImage samplesPerPixel:samplesPerPixel. |
|
2964 |
newImage bitsPerSample:bitsPerSample. |
|
2965 |
newImage colorMap:colorMap copy. |
|
2966 |
||
2967 |
w := width - 1. |
|
2968 |
h := height - 1. |
|
2969 |
||
2970 |
0 to:h do:[:y | |
|
2971 |
0 to:w do:[:x | |
|
2972 |
newImage atX:x y:y put:(pixelFunctionBlock |
|
2973 |
value:self |
|
2974 |
value:x |
|
2975 |
value:y) |
|
2976 |
] |
|
2977 |
]. |
|
2978 |
^ newImage |
|
2979 |
||
2980 |
" |
|
2981 |
|i| |
|
2982 |
||
2983 |
i := Image fromFile:'claus.gif'. |
|
2984 |
i inspect. |
|
2985 |
(i withPixelFunctionApplied:[:oldImage :x :y | |
|
2986 |
((x between:100 and:200) |
|
2987 |
and:[y between:100 and:200]) ifTrue:[ |
|
2988 |
oldImage atX:x y:y |
|
2989 |
] ifFalse:[ |
|
2990 |
Color black. |
|
2991 |
] |
|
2992 |
]) inspect. |
|
2993 |
" |
|
2994 |
||
2995 |
"Modified: 17.5.1996 / 12:31:44 / cg" |
|
283 | 2996 |
! ! |
2997 |
||
2998 |
!Image methodsFor:'inspecting'! |
|
2999 |
||
3000 |
inspectorClass |
|
3001 |
"redefined to launch an ImageInspector |
|
3002 |
(instead of the default InspectorView)." |
|
3003 |
||
3004 |
^ ImageInspectorView |
|
3005 |
! ! |
|
3006 |
||
3007 |
!Image methodsFor:'instance release'! |
|
3008 |
||
3009 |
disposed |
|
3010 |
"some Image has been collected - nothing to do" |
|
12 | 3011 |
! |
3012 |
||
283 | 3013 |
release |
3014 |
"release device resources" |
|
3015 |
||
3016 |
device := nil. |
|
3017 |
deviceForm := nil. |
|
3018 |
monoDeviceForm := nil. |
|
3019 |
fullColorDeviceForm := nil. |
|
3020 |
Lobby unregister:self |
|
46 | 3021 |
! |
3022 |
||
283 | 3023 |
restored |
3024 |
"flush device specifics after a snapin or binary restore" |
|
3025 |
||
3026 |
self release |
|
3027 |
! ! |
|
3028 |
||
592 | 3029 |
!Image methodsFor:'obsolete'! |
3030 |
||
3031 |
magnifyBy:scale |
|
3032 |
"obsolete: has been renamed to magnifiedBy: for ST-80 compatibility |
|
3033 |
and name consistency ..." |
|
3034 |
||
3035 |
self obsoleteMethodWarning. |
|
3036 |
^ self magnifiedBy:scale |
|
3037 |
! ! |
|
3038 |
||
283 | 3039 |
!Image methodsFor:'pixel copying'! |
3040 |
||
3041 |
copyFrom:anImage x:srcX y:srcY toX:dstX y:dstY width:w height:h |
|
3042 |
"replace a rectangulat area by pixels from another image. |
|
3043 |
WARNING: |
|
3044 |
This implementation is a slow fallback (the loop over the |
|
3045 |
source pixels is very slow). If this method is used heavily, you |
|
3046 |
may want to redefine it in concrete subclasses for the common case of |
|
3047 |
of copying from an Image with the same depth & palette." |
|
3048 |
||
3049 |
|dX dY| |
|
3050 |
||
3051 |
dX := srcX-dstX. |
|
3052 |
dY := srcY-dstY. |
|
3053 |
((photometric == anImage photometric) |
|
3054 |
and:[self bitsPerPixel == anImage bitsPerPixel |
|
3055 |
and:[colorMap = anImage colorMap]]) ifTrue:[ |
|
3056 |
"/ can loop over values |
|
3057 |
anImage valuesFromX:srcX y:srcY |
|
3058 |
toX:srcX+w-1 y:srcY+h-1 |
|
3059 |
do:[:x :y :pixelValue | |
|
3060 |
self atX:x-dX y:y-dY putValue:pixelValue. |
|
3061 |
] |
|
3062 |
] ifFalse:[ |
|
3063 |
"/ must loop over colors |
|
3064 |
anImage colorsFromX:srcX y:srcY |
|
3065 |
toX:srcX+w-1 y:srcY+h-1 |
|
3066 |
do:[:x :y :clr | |
|
3067 |
self atX:x-dX y:y-dY put:clr. |
|
3068 |
] |
|
3069 |
] |
|
3070 |
||
3071 |
" |
|
3072 |
|i1 i8 i4| |
|
3073 |
||
3074 |
i8 := Image fromFile:'bitmaps/garfield.gif'. |
|
3075 |
i8 inspect. |
|
3076 |
i1 := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
3077 |
i1 inspect. |
|
3078 |
||
3079 |
i4 := Depth4Image fromImage:i8. |
|
3080 |
i4 copyFrom:i1 x:0 y:0 toX:20 y:20 width:30 height:30. |
|
3081 |
i4 inspect. |
|
3082 |
" |
|
3083 |
||
3084 |
"Created: 20.9.1995 / 10:14:01 / claus" |
|
3085 |
"Modified: 20.9.1995 / 10:25:31 / claus" |
|
3086 |
! |
|
3087 |
||
3088 |
subImageIn:aRectangle |
|
3089 |
"create and return a new image consisting of a subArea of myself" |
|
3090 |
||
3091 |
^ self class fromSubImage:self in:aRectangle |
|
3092 |
||
3093 |
" |
|
3094 |
|i| |
|
3095 |
||
3096 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
3097 |
i inspect. |
|
3098 |
(i subImageIn:(300@160 corner:340@200)) inspect |
|
3099 |
" |
|
3100 |
||
3101 |
"Created: 20.9.1995 / 01:24:20 / claus" |
|
3102 |
! ! |
|
3103 |
||
3104 |
!Image methodsFor:'printing & storing'! |
|
3105 |
||
3106 |
storeOn:aStream |
|
592 | 3107 |
"append a printed representation of the receiver to aStream, |
3108 |
from which a copy of it can be reconstructed." |
|
3109 |
||
283 | 3110 |
aStream nextPutAll:'(' , self class name , ' new)'. |
3111 |
aStream nextPutAll:' width: '. width storeOn:aStream. |
|
3112 |
aStream nextPutAll:'; height: '. height storeOn:aStream. |
|
3113 |
aStream nextPutAll:'; photometric:('. photometric storeOn:aStream. |
|
3114 |
aStream nextPutAll:'); bitsPerSample:('. bitsPerSample storeOn:aStream. |
|
3115 |
aStream nextPutAll:'); samplesPerPixel:('. samplesPerPixel storeOn:aStream. |
|
3116 |
aStream nextPutAll:'); bits:('. bytes storeOn:aStream. aStream nextPutAll:') '. |
|
3117 |
colorMap notNil ifTrue:[ |
|
592 | 3118 |
aStream nextPutAll:'; colorMap:('. |
3119 |
colorMap storeOn:aStream. |
|
3120 |
aStream nextPutAll:')'. |
|
283 | 3121 |
]. |
3122 |
aStream nextPutAll:'; yourself' |
|
592 | 3123 |
|
3124 |
"Modified: 23.4.1996 / 11:18:05 / cg" |
|
0 | 3125 |
! ! |
3126 |
||
3127 |
!Image methodsFor:'private'! |
|
3128 |
||
193 | 3129 |
bestSupportedImageFormatFor:aDevice |
3130 |
"scan through the image formats as supported by aDevice, |
|
3131 |
and return the best to use when the receiver is to be represented |
|
3132 |
on it. The best format is the one with the same number or more bits per |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3133 |
pixel. Here, the smallest format found which can represent enough pixels is taken." |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3134 |
|
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3135 |
|bestDeviceDepth bestDeviceBitsPerPixel bestInfo maxInfo myDepth maxDepth maxBitsPerPixel | |
193 | 3136 |
|
3137 |
myDepth := self bitsPerPixel. |
|
3138 |
maxBitsPerPixel := 0. |
|
3139 |
||
3140 |
aDevice supportedImageFormats do:[:entry | |
|
3141 |
|deviceImageDepth deviceImageBitsPerPixel| |
|
3142 |
||
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3143 |
deviceImageDepth := entry at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3144 |
deviceImageBitsPerPixel := entry at:#bitsPerPixel. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3145 |
|
193 | 3146 |
deviceImageBitsPerPixel > maxBitsPerPixel ifTrue:[ |
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3147 |
maxInfo := entry. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3148 |
"/ maxBitsPerPixel := deviceImageBitsPerPixel. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3149 |
"/ maxDepth := deviceImageDepth. |
193 | 3150 |
]. |
3151 |
deviceImageDepth >= myDepth ifTrue:[ |
|
3152 |
deviceImageDepth == myDepth ifTrue:[ |
|
3153 |
"/ take the better one ... |
|
3154 |
(bestDeviceDepth isNil |
|
3155 |
or:[(bestDeviceBitsPerPixel ~~ bestDeviceDepth) |
|
3156 |
and:[deviceImageDepth == deviceImageBitsPerPixel]]) ifTrue:[ |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3157 |
bestInfo := entry. |
193 | 3158 |
bestDeviceDepth := deviceImageDepth. |
3159 |
bestDeviceBitsPerPixel := deviceImageBitsPerPixel. |
|
3160 |
] |
|
3161 |
] ifFalse:[ |
|
3162 |
"/ take the next-larger depth |
|
3163 |
(bestDeviceDepth isNil |
|
3164 |
or:[deviceImageBitsPerPixel < bestDeviceBitsPerPixel]) ifTrue:[ |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3165 |
bestInfo := entry. |
193 | 3166 |
bestDeviceDepth := deviceImageDepth. |
3167 |
bestDeviceBitsPerPixel := deviceImageBitsPerPixel. |
|
3168 |
] |
|
3169 |
] |
|
3170 |
]. |
|
3171 |
]. |
|
3172 |
bestDeviceDepth isNil ifTrue:[ |
|
3173 |
maxBitsPerPixel == 0 ifTrue:[ |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3174 |
"/ |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3175 |
"/ oops - nothing appropriate |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3176 |
"/ |
193 | 3177 |
bestDeviceDepth := bestDeviceBitsPerPixel := aDevice depth. |
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3178 |
bestInfo := IdentityDictionary new. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3179 |
bestInfo at:#depth put:bestDeviceDepth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3180 |
bestInfo at:#bitsPerPixel put:bestDeviceDepth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3181 |
bestInfo at:#padding put:32. |
193 | 3182 |
] ifFalse:[ |
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3183 |
bestInfo := maxInfo. |
193 | 3184 |
] |
3185 |
]. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3186 |
^ bestInfo |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3187 |
|
193 | 3188 |
"Modified: 21.10.1995 / 03:52:45 / cg" |
283 | 3189 |
! |
3190 |
||
3191 |
colormapFromImage:anImage |
|
3192 |
"setup the receivers colormap from another image. |
|
3193 |
Color precision may be lost, if conversion is from a higher depth |
|
3194 |
image. This does not convert any pixel values; it is non-public helper |
|
3195 |
for fromImage:/fromSubImake:" |
|
3196 |
||
3197 |
samplesPerPixel == 3 ifTrue:[ |
|
3198 |
photometric := #rgb |
|
3199 |
] ifFalse:[ |
|
3200 |
photometric := anImage photometric. |
|
3201 |
photometric == #palette ifTrue:[ |
|
3202 |
colorMap := anImage colorMap copy. |
|
3203 |
" |
|
3204 |
must compress the colormap, if source image has higher depth |
|
3205 |
than myself. |
|
3206 |
" |
|
3207 |
anImage bitsPerPixel > self bitsPerPixel ifTrue:[ |
|
3208 |
" |
|
3209 |
get used colors are extracted into our colorMap |
|
3210 |
(the at-put below will set the pixelValue according the |
|
3211 |
new colorIndex |
|
3212 |
" |
|
3213 |
colorMap := anImage usedColors asArray. |
|
3214 |
colorMap size > (1 bitShift:self bitsPerPixel) ifTrue:[ |
|
3215 |
'IMAGE: possibly too many colors in image' errorPrintNL |
|
3216 |
] |
|
3217 |
] |
|
3218 |
] |
|
3219 |
]. |
|
3220 |
||
3221 |
"Created: 20.9.1995 / 00:58:42 / claus" |
|
3222 |
! |
|
3223 |
||
3224 |
magnifyRowFrom:srcBytes offset:srcStart pixels:oldPixels |
|
3225 |
into:dstBytes offset:dstStart factor:mX |
|
3226 |
||
3227 |
"magnify a single pixel row - can only magnify by integer factors, |
|
3228 |
can only magnify 1,2,4,8 and 24 bit-per-pixel images. But this is done fast." |
|
3229 |
||
3230 |
^ self subclassResponsibility |
|
2 | 3231 |
! ! |
0 | 3232 |
|
283 | 3233 |
!Image methodsFor:'queries'! |
3234 |
||
3235 |
averageColor |
|
3236 |
"return the average color of the image. |
|
3237 |
This usually only makes sense for textures and patterns |
|
3238 |
(i.e. to compute shadow & light colors for viewBackgrounds). |
|
3239 |
Notice, that for the above purpose, it is usually ok to process |
|
3240 |
a subImage - i.e. use Image>>averageColorIn: on a smaller rectangle" |
|
3241 |
||
3242 |
^ self averageColorIn:(0@0 corner:(width-1)@(height-1)) |
|
3243 |
! |
|
3244 |
||
3245 |
averageColorIn:aRectangle |
|
3246 |
"return the images average color in an area. |
|
3247 |
The implementation below is slow - so you may want to |
|
3248 |
create tuned versions for DepthXImage if you plan to do |
|
3249 |
heavy image processing ... |
|
3250 |
(also, creating tuned versions of the enumeration messages helps a lot)" |
|
3251 |
||
3252 |
|x0 "{ Class:SmallInteger }" |
|
3253 |
y0 "{ Class:SmallInteger }" |
|
3254 |
x1 "{ Class:SmallInteger }" |
|
3255 |
y1 "{ Class:SmallInteger }" |
|
3256 |
sumRed sumGreen sumBlue n| |
|
3257 |
||
3258 |
sumRed := sumGreen := sumBlue := 0. |
|
3259 |
y0 := aRectangle top. |
|
3260 |
y1 := aRectangle bottom. |
|
3261 |
x0 := aRectangle left. |
|
3262 |
x1 := aRectangle right. |
|
3263 |
||
3264 |
self colorsFromX:x0 y:y0 toX:x1 y:y1 do:[:x :y :colorAtXY | |
|
3265 |
sumRed := sumRed + colorAtXY red. |
|
3266 |
sumGreen := sumGreen + colorAtXY green. |
|
3267 |
sumBlue := sumBlue + colorAtXY blue. |
|
3268 |
]. |
|
3269 |
n := (x1 - x0 + 1) * (y1 - y0 + 1). |
|
3270 |
^ Color red:(sumRed / n) green:(sumGreen / n) blue:(sumBlue / n) |
|
3271 |
! |
|
3272 |
||
3273 |
bitsPerPixel |
|
3274 |
"return the number of bits per pixel" |
|
3275 |
||
3276 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i]) |
|
3277 |
! |
|
3278 |
||
3279 |
bitsPerRow |
|
3280 |
"return the number of bits in one scanline of the image" |
|
3281 |
||
3282 |
^ width * (self bitsPerPixel). |
|
3283 |
! |
|
3284 |
||
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3285 |
bounds |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3286 |
"return my bounds (added to make images usable as VisualComponents)" |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3287 |
|
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3288 |
^ Rectangle left:0 top:0 width:width height:height |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3289 |
|
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3290 |
"Created: 13.5.1996 / 10:27:06 / cg" |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3291 |
! |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3292 |
|
283 | 3293 |
brightness |
3294 |
"return the brightness of the image. |
|
3295 |
This usually only makes sense for textures and patterns |
|
3296 |
(i.e. to compute shadow & light colors for viewBackgrounds). |
|
3297 |
Notice, that for the above purpose, only a subimage is inspected here" |
|
3298 |
||
3299 |
^ (self averageColorIn:(0@0 corner:7@7)) brightness |
|
3300 |
! |
|
3301 |
||
3302 |
bytesPerRow |
|
3303 |
"return the number of bytes in one scanline of the image" |
|
3304 |
||
3305 |
|bitsPerRow bytesPerRow| |
|
3306 |
||
3307 |
bitsPerRow := width * (self bitsPerPixel). |
|
3308 |
bytesPerRow := bitsPerRow // 8. |
|
3309 |
((bitsPerRow \\ 8) ~~ 0) ifTrue:[ |
|
3310 |
bytesPerRow := bytesPerRow + 1 |
|
3311 |
]. |
|
3312 |
^ bytesPerRow |
|
3313 |
! |
|
3314 |
||
674 | 3315 |
heightOn:aGC |
3316 |
"return my height, if displayed on aGC; |
|
3317 |
since my height is independent of the device (the number of pixels), |
|
3318 |
return the pixel-height" |
|
3319 |
||
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3320 |
^ height |
674 | 3321 |
|
3322 |
"Created: 12.5.1996 / 21:35:06 / cg" |
|
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3323 |
"Modified: 13.5.1996 / 10:26:36 / cg" |
674 | 3324 |
! |
3325 |
||
283 | 3326 |
isImage |
3327 |
"return true, if the receiver is some kind of image; |
|
3328 |
true is returned here - the method is redefined from Object." |
|
3329 |
||
3330 |
^ true |
|
3331 |
! |
|
3332 |
||
3333 |
isImageOrForm |
|
3334 |
"return true, if the receiver is some kind of image or form; |
|
3335 |
true is returned here - the method is redefined from Object." |
|
3336 |
||
3337 |
^ true |
|
3338 |
! |
|
3339 |
||
3340 |
usedColors |
|
3341 |
"return a collection of colors used in the receiver." |
|
3342 |
||
3343 |
|usedValues max| |
|
3344 |
||
3345 |
usedValues := self usedValues asArray. |
|
3346 |
photometric ~~ #palette ifTrue:[ |
|
746 | 3347 |
max := (1 bitShift:self depth) - 1. |
3348 |
^ usedValues collect:[:val | (Color gray:(100 * val / max ))] |
|
283 | 3349 |
]. |
3350 |
||
3351 |
^ usedValues collect:[:val | (colorMap at:val+1)] |
|
3352 |
||
3353 |
" |
|
3354 |
(Image fromFile:'bitmaps/garfield.gif') usedColors |
|
3355 |
(Image fromFile:'bitmaps/SBrowser.xbm') usedColors |
|
3356 |
(Image fromFile:'ttt.tiff') usedColors |
|
3357 |
" |
|
746 | 3358 |
|
3359 |
"Modified: 28.5.1996 / 20:55:10 / cg" |
|
283 | 3360 |
! |
3361 |
||
3362 |
usedValues |
|
3363 |
"return a collection of color values used in the receiver. |
|
3364 |
Notice, that the interpretation of the pixels depends on the photometric |
|
3365 |
of the image. |
|
3366 |
This is a general and therefore slow implementation; subclasses |
|
3367 |
may want to redefine this method for more performance." |
|
3368 |
||
3369 |
|set| |
|
3370 |
||
3371 |
set := IdentitySet new. |
|
3372 |
self valuesFromX:0 y:0 toX:(self width-1) y:(self height-1) do:[:x :y :pixel | |
|
3373 |
set add:pixel |
|
3374 |
]. |
|
3375 |
^ set |
|
3376 |
||
3377 |
" |
|
3378 |
(Image fromFile:'bitmaps/garfield.gif') usedValues |
|
3379 |
(Image fromFile:'bitmaps/SBrowser.xbm') usedValues |
|
3380 |
(Image fromFile:'ttt.tiff') usedValues |
|
3381 |
" |
|
674 | 3382 |
! |
3383 |
||
3384 |
widthOn:aGC |
|
3385 |
"return my width, if displayed on aGC; |
|
3386 |
since my width is independent of the device (the number of pixels), |
|
3387 |
return the pixel-width" |
|
3388 |
||
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3389 |
^ width |
674 | 3390 |
|
3391 |
"Created: 12.5.1996 / 21:34:59 / cg" |
|
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
3392 |
"Modified: 13.5.1996 / 10:26:42 / cg" |
283 | 3393 |
! ! |
3394 |
||
3395 |
!Image methodsFor:'saving on file'! |
|
3396 |
||
3397 |
saveOn:aFileName |
|
3398 |
"save the image in a aFileName. The suffix of the filename |
|
3399 |
controls the format. Currently, not all formats may be supported |
|
3400 |
(see ImageReader subclasses implementing save:onFile:)" |
|
3401 |
||
3402 |
" |
|
3403 |
from the extension, get the imageReader class |
|
3404 |
(which should know how to write images as well) |
|
3405 |
" |
|
3406 |
FileFormats associationsDo:[:a | |
|
3407 |
(aFileName endsWith:(a key)) ifTrue:[ |
|
3408 |
^ (a value) save:self onFile:aFileName |
|
3409 |
] |
|
3410 |
]. |
|
3411 |
||
3412 |
" |
|
3413 |
no known extension - could ask user for the format here. |
|
3414 |
currently default to tiff format. |
|
3415 |
" |
|
3416 |
'IMAGE: unknown extension - cannot figure out format - using tiff' errorPrintNL. |
|
3417 |
^ self saveOn:aFileName using:TIFFReader |
|
89 | 3418 |
! |
3419 |
||
283 | 3420 |
saveOn:aFileName using:readerClass |
3421 |
"save the receiver using the representation class" |
|
3422 |
||
3423 |
readerClass save:self onFile:aFileName |
|
3424 |
||
3425 |
" |
|
3426 |
anImage saveOn:'myImage' using:TIFFReader |
|
3427 |
anImage saveOn:'myImage' using:XBMReader |
|
3428 |
" |
|
0 | 3429 |
! ! |
283 | 3430 |
|
3431 |
!Image methodsFor:'screen capture'! |
|
3432 |
||
3433 |
fromScreen:aRectangle |
|
3434 |
"read an image from the display screen" |
|
3435 |
||
3436 |
^ self fromScreen:aRectangle on:Screen current |
|
3437 |
! |
|
3438 |
||
3439 |
fromScreen:aRectangle on:aDevice |
|
3440 |
"read an image from aDevices display screen. |
|
3441 |
Since I have no other displays, only the MonoChrome, StaticGrey |
|
3442 |
and PseudoColor cases have been tested ... |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3443 |
(especially True- and DirectColor may be wrong). |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3444 |
Late note: 24bit rgb now also works." |
283 | 3445 |
|
3446 |
|depth visType |
|
3447 |
x "{ Class: SmallInteger }" |
|
3448 |
y "{ Class: SmallInteger }" |
|
3449 |
w "{ Class: SmallInteger }" |
|
3450 |
h "{ Class: SmallInteger }" |
|
3451 |
dstIndex "{ Class: SmallInteger }" |
|
3452 |
srcIndex "{ Class: SmallInteger }" |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3453 |
srcRow "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3454 |
dstRow "{ Class: SmallInteger }" |
283 | 3455 |
inData tmpData usedPixels mapSize |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3456 |
map bitsPerPixel bytesPerLine byteOrder spaceBitsPerPixel |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3457 |
info bitsPerPixelIn bytesPerLineIn curs cid rootView |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3458 |
maskR "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3459 |
maskG "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3460 |
maskB "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3461 |
shR "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3462 |
shG "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3463 |
shB "{ Class: SmallInteger }" |
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3464 |
shR2 "{ Class: SmallInteger }" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3465 |
shG2 "{ Class: SmallInteger }" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3466 |
shB2 "{ Class: SmallInteger }" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3467 |
r "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3468 |
g "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3469 |
b "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3470 |
word| |
283 | 3471 |
|
3472 |
curs := Cursor sourceForm:(Form fromFile:'Camera.xbm') |
|
518 | 3473 |
maskForm:(Form fromFile:'Camera_m.xbm') |
3474 |
hotSpot:16@16. |
|
283 | 3475 |
curs notNil ifTrue:[ |
518 | 3476 |
cid := (curs on:aDevice) id |
283 | 3477 |
]. |
3478 |
||
3479 |
" |
|
3480 |
get some attributes of the display device |
|
3481 |
" |
|
3482 |
visType := aDevice visualType. |
|
3483 |
depth := aDevice depth. |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3484 |
|
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3485 |
"/ kludge for 15bit XFree server |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3486 |
depth == 15 ifTrue:[ |
518 | 3487 |
depth := 16 |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3488 |
]. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3489 |
|
283 | 3490 |
(visType == #StaticGray) ifTrue:[ |
518 | 3491 |
(aDevice blackpixel == 0) ifTrue:[ |
3492 |
photometric := #blackIs0 |
|
3493 |
] ifFalse:[ |
|
3494 |
photometric := #whiteIs0 |
|
3495 |
]. |
|
3496 |
samplesPerPixel := 1. |
|
3497 |
bitsPerPixel := depth. |
|
3498 |
bitsPerSample := Array with:bitsPerPixel. |
|
283 | 3499 |
] ifFalse:[ |
518 | 3500 |
((visType == #PseudoColor) or:[(visType == #StaticColor) or:[visType == #GrayScale]]) ifTrue:[ |
3501 |
photometric := #palette. |
|
3502 |
samplesPerPixel := 1. |
|
3503 |
bitsPerPixel := depth. |
|
3504 |
bitsPerSample := Array with:bitsPerPixel. |
|
3505 |
] ifFalse:[ |
|
3506 |
((visType == #TrueColor) or:[visType == #DirectColor]) ifTrue:[ |
|
3507 |
photometric := #rgb. |
|
3508 |
samplesPerPixel := 3. |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3509 |
"/ bitsPerPixel := depth. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3510 |
"/ bitsPerSample := Array with:aDevice bitsRed |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3511 |
"/ with:aDevice bitsGreen |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3512 |
"/ with:aDevice bitsBlue |
518 | 3513 |
bitsPerPixel := 24. |
3514 |
bitsPerSample := #(8 8 8). |
|
3515 |
] ifFalse:[ |
|
3516 |
self error:'screen visual not supported'. |
|
3517 |
^ nil |
|
3518 |
] |
|
3519 |
] |
|
283 | 3520 |
]. |
3521 |
||
3522 |
" |
|
3523 |
dont know yet, how the display pads; assume worst case, |
|
3524 |
offering enough space for 32 bit padding |
|
3525 |
" |
|
3526 |
w := width := aRectangle width. |
|
3527 |
h := height := aRectangle height. |
|
3528 |
x := aRectangle left. |
|
3529 |
y := aRectangle top. |
|
3530 |
||
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3531 |
" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3532 |
a kludge: we dont know in advance how much we are going to need |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3533 |
(its too late when info is present ...) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3534 |
" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3535 |
spaceBitsPerPixel := bitsPerPixel. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3536 |
(bitsPerPixel > 8) ifTrue:[ |
518 | 3537 |
spaceBitsPerPixel := 16. |
3538 |
(bitsPerPixel > 16) ifTrue:[ |
|
3539 |
spaceBitsPerPixel := 32. |
|
3540 |
(bitsPerPixel > 32) ifTrue:[ |
|
3541 |
spaceBitsPerPixel := bitsPerPixel. |
|
3542 |
] |
|
3543 |
] |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3544 |
]. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3545 |
|
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3546 |
bytesPerLine := (w * spaceBitsPerPixel + 31) // 32 * 4. |
283 | 3547 |
inData := ByteArray uninitializedNew:(bytesPerLine * height). |
3548 |
||
3549 |
" |
|
3550 |
actually have to grabServer ... but thats not yet available |
|
3551 |
" |
|
728 | 3552 |
rootView := aDevice rootView. |
283 | 3553 |
aDevice setActivePointerGrab:rootView. |
3554 |
aDevice grabPointerIn:rootView id |
|
518 | 3555 |
withCursor:cid pointerMode:#async keyboardMode:#sync confineTo:nil. |
283 | 3556 |
|
3557 |
" |
|
3558 |
get the pixels |
|
3559 |
" |
|
3560 |
info := aDevice getBitsFrom:rootView id x:x y:y width:w height:h into:inData. |
|
3561 |
||
3562 |
" |
|
396
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
3563 |
check if byteorder is what I like (msbFirst) |
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
3564 |
" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3565 |
byteOrder := info at:#byteOrder. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3566 |
bitsPerPixelIn := info at:#bitsPerPixel. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3567 |
byteOrder ~~ #msbFirst ifTrue:[ |
518 | 3568 |
bitsPerPixelIn == 16 ifTrue:[ |
3569 |
"/ must swap bytes |
|
3570 |
inData swapBytes |
|
3571 |
] ifFalse:[ |
|
3572 |
bitsPerPixelIn == 32 ifTrue:[ |
|
3573 |
"/ must swap longs |
|
3574 |
inData swapLongs |
|
3575 |
] |
|
3576 |
] |
|
396
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
3577 |
]. |
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
3578 |
|
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
3579 |
" |
283 | 3580 |
check, if the devices padding is different .. |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3581 |
or if the bitsPerPixels are different |
283 | 3582 |
" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3583 |
bytesPerLineIn := (info at:#bytesPerLine). "what I got" |
283 | 3584 |
bytesPerLine := (w * bitsPerPixel + 7) // 8. "what I want" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3585 |
|
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3586 |
((bytesPerLine ~~ bytesPerLineIn) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3587 |
or:[bitsPerPixelIn ~~ bitsPerPixel]) ifTrue:[ |
518 | 3588 |
tmpData := inData. |
3589 |
inData := ByteArray uninitializedNew:(bytesPerLine * height). |
|
3590 |
||
3591 |
srcRow := 1. |
|
3592 |
dstRow := 1. |
|
3593 |
||
3594 |
bitsPerPixelIn ~~ bitsPerPixel ifTrue:[ |
|
3595 |
"/ for now, only 32 -> 24 is supported |
|
3596 |
||
3597 |
maskR := (1 bitShift:aDevice bitsRed) - 1. |
|
3598 |
maskG := (1 bitShift:aDevice bitsGreen) - 1. |
|
3599 |
maskB := (1 bitShift:aDevice bitsBlue) - 1. |
|
3600 |
shR := aDevice shiftRed negated. |
|
3601 |
shG := aDevice shiftGreen negated. |
|
3602 |
shB := aDevice shiftBlue negated. |
|
3603 |
shR2 := (8 - aDevice bitsRed). |
|
3604 |
shG2 := (8 - aDevice bitsGreen). |
|
3605 |
shB2 := (8 - aDevice bitsBlue). |
|
3606 |
||
3607 |
((bitsPerPixelIn == 32) and:[bitsPerPixel == 24]) ifTrue:[ |
|
3608 |
"/ 'reformatting 32->24...' printNL. |
|
3609 |
1 to:h do:[:hi | |
|
3610 |
srcIndex := srcRow. |
|
3611 |
dstIndex := dstRow. |
|
3612 |
||
3613 |
1 to:w do:[:wi | |
|
3614 |
word := tmpData doubleWordAt:srcIndex MSB:true. |
|
3615 |
r := (word bitShift:shR) bitAnd:maskR. |
|
3616 |
g := (word bitShift:shG) bitAnd:maskG. |
|
3617 |
b := (word bitShift:shB) bitAnd:maskB. |
|
3618 |
||
3619 |
inData at:dstIndex put:r. |
|
3620 |
inData at:dstIndex+1 put:g. |
|
3621 |
inData at:dstIndex+2 put:b. |
|
3622 |
srcIndex := srcIndex + 4. |
|
3623 |
dstIndex := dstIndex + 3. |
|
3624 |
]. |
|
3625 |
dstRow := dstRow + bytesPerLine. |
|
3626 |
srcRow := srcRow + bytesPerLineIn |
|
3627 |
] |
|
3628 |
] ifFalse:[ |
|
3629 |
((bitsPerPixelIn == 16) and:[bitsPerPixel == 24]) ifTrue:[ |
|
3630 |
"/ 'reformatting 16->24...' printNL. |
|
3631 |
1 to:h do:[:hi | |
|
3632 |
srcIndex := srcRow. |
|
3633 |
dstIndex := dstRow. |
|
3634 |
||
3635 |
1 to:w do:[:wi | |
|
3636 |
word := tmpData wordAt:srcIndex MSB:true. |
|
3637 |
r := (word bitShift:shR) bitAnd:maskR. |
|
3638 |
g := (word bitShift:shG) bitAnd:maskG. |
|
3639 |
b := (word bitShift:shB) bitAnd:maskB. |
|
3640 |
||
3641 |
inData at:dstIndex put:(r bitShift:shR2). |
|
3642 |
inData at:dstIndex+1 put:(g bitShift:shG2). |
|
3643 |
inData at:dstIndex+2 put:(b bitShift:shB2). |
|
3644 |
srcIndex := srcIndex + 2. |
|
3645 |
dstIndex := dstIndex + 3. |
|
3646 |
]. |
|
3647 |
dstRow := dstRow + bytesPerLine. |
|
3648 |
srcRow := srcRow + bytesPerLineIn |
|
3649 |
] |
|
3650 |
] ifFalse:[ |
|
3651 |
('IMAGE: unsupported depth combination: ' , bitsPerPixelIn printString , ' -> ' , |
|
3652 |
bitsPerPixel printString) errorPrintNL. |
|
3653 |
^ nil |
|
3654 |
] |
|
3655 |
]. |
|
3656 |
] ifFalse:[ |
|
3657 |
" |
|
3658 |
repad in the buffer |
|
3659 |
" |
|
3660 |
1 to:h do:[:hi | |
|
3661 |
inData replaceFrom:dstRow to:(dstRow + bytesPerLine - 1) |
|
3662 |
with:tmpData startingAt:srcRow. |
|
3663 |
dstRow := dstRow + bytesPerLine. |
|
3664 |
srcRow := srcRow + bytesPerLineIn |
|
3665 |
] |
|
3666 |
] |
|
283 | 3667 |
] ifFalse:[ |
518 | 3668 |
(bytesPerLine * height) ~~ inData size ifTrue:[ |
3669 |
tmpData := inData. |
|
3670 |
inData := ByteArray uninitializedNew:(bytesPerLine * height). |
|
3671 |
inData replaceFrom:1 to:bytesPerLine * height with:tmpData startingAt:1 |
|
3672 |
] |
|
283 | 3673 |
]. |
3674 |
bytes := inData. |
|
3675 |
||
3676 |
"info printNL." |
|
3677 |
||
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3678 |
((visType == #StaticGray) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3679 |
or:[visType == #TrueColor |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3680 |
or:[visType == #DirectColor]]) ifTrue:[ |
518 | 3681 |
" |
3682 |
were done, the pixel values are the rgb/grey values |
|
3683 |
" |
|
283 | 3684 |
] ifFalse:[ |
518 | 3685 |
" |
3686 |
what we have now are the color numbers - still need the r/g/b values. |
|
3687 |
find out, which colors are in the picture |
|
3688 |
" |
|
3689 |
usedPixels := inData usedValues. |
|
3690 |
mapSize := usedPixels max + 1. |
|
3691 |
||
3692 |
"get the palette" |
|
3693 |
map := Array new:mapSize. |
|
3694 |
usedPixels do:[:colorIndex | |
|
3695 |
|i| |
|
3696 |
||
3697 |
i := colorIndex + 1. |
|
3698 |
aDevice getRGBFrom:colorIndex into:[:r :g :b | |
|
3699 |
map at:i put:(Color red:r green:g blue:b) |
|
3700 |
] |
|
3701 |
]. |
|
3702 |
colorMap := map. |
|
283 | 3703 |
]. |
3704 |
||
3705 |
aDevice ungrabPointer. |
|
3706 |
||
3707 |
" |
|
3708 |
(Image new) fromScreen:((0 @ 0) corner:(100 @ 100)) on:Display |
|
3709 |
(Image new) fromScreen:((0 @ 0) corner:(500 @ 500)) on:Display |
|
3710 |
" |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
3711 |
|
518 | 3712 |
"Modified: 7.3.1996 / 19:17:33 / cg" |
283 | 3713 |
! ! |
3714 |
||
3715 |
!Image class methodsFor:'documentation'! |
|
3716 |
||
3717 |
version |
|
748 | 3718 |
^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.76 1996-05-28 19:12:57 cg Exp $' |
650 | 3719 |
! ! |
283 | 3720 |
Image initialize! |