author | Claus Gittinger <cg@exept.de> |
Fri, 07 Jun 1996 19:32:55 +0200 | |
changeset 798 | 31ed4a1d4b4a |
parent 766 | 8c93c7464bdf |
child 800 | 0730bb75c28f |
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 |
|
798 | 633 |
! |
634 |
||
635 |
orderedDitherMatrixOfSize:sz |
|
636 |
sz == 2 ifTrue:[ |
|
637 |
^ #[ |
|
638 |
0 2 |
|
639 |
3 1 |
|
640 |
]. |
|
641 |
]. |
|
642 |
||
643 |
sz == 4 ifTrue:[ |
|
644 |
^ #[ |
|
645 |
0 8 2 10 |
|
646 |
12 4 14 6 |
|
647 |
3 11 1 9 |
|
648 |
15 7 13 5 |
|
649 |
]. |
|
650 |
]. |
|
651 |
||
652 |
sz == 8 ifTrue:[ |
|
653 |
^ #[ |
|
654 |
0 32 8 40 2 34 10 42 |
|
655 |
48 16 56 24 50 18 58 26 |
|
656 |
12 44 4 36 14 46 6 38 |
|
657 |
60 28 52 20 62 30 54 22 |
|
658 |
||
659 |
3 35 11 43 1 33 9 41 |
|
660 |
51 19 59 27 49 17 57 25 |
|
661 |
15 47 7 39 13 45 5 37 |
|
662 |
63 31 55 23 61 29 53 21 |
|
663 |
]. |
|
664 |
]. |
|
665 |
||
666 |
^ nil |
|
667 |
||
668 |
"Created: 7.6.1996 / 14:15:05 / cg" |
|
283 | 669 |
! ! |
670 |
||
671 |
!Image class methodsFor:'queries'! |
|
672 |
||
673 |
imageDepth |
|
579 | 674 |
"return the depth of images represented by instances of |
675 |
this class. |
|
676 |
Must be redefined in concrete subclasses" |
|
677 |
||
678 |
^ self shouldNotImplement |
|
679 |
||
680 |
"Modified: 20.4.1996 / 23:40:41 / cg" |
|
191 | 681 |
! |
682 |
||
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
683 |
implementorForDepth:depth |
283 | 684 |
"return the class, which best implements images of depth" |
685 |
||
686 |
depth == 1 ifTrue:[^ Depth1Image]. |
|
687 |
depth == 2 ifTrue:[^ Depth2Image]. |
|
688 |
depth == 4 ifTrue:[^ Depth4Image]. |
|
689 |
depth == 8 ifTrue:[^ Depth8Image]. |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
690 |
depth == 15 ifTrue:[^ Depth16Image]. "/ kludge for 15bit XFree |
283 | 691 |
depth == 16 ifTrue:[^ Depth16Image]. |
692 |
depth == 24 ifTrue:[^ Depth24Image]. |
|
693 |
^ self |
|
694 |
! ! |
|
695 |
||
696 |
!Image class methodsFor:'screen capture'! |
|
697 |
||
698 |
fromScreen |
|
699 |
"return an image of the full screen" |
|
700 |
||
701 |
|display| |
|
702 |
||
703 |
display := Screen current. |
|
704 |
^ 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
|
705 |
|
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
706 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
707 |
Image fromScreen |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
708 |
" |
283 | 709 |
! |
710 |
||
711 |
fromScreen:aRectangle |
|
712 |
"return an image of a part of the screen" |
|
713 |
||
714 |
^ self fromScreen:aRectangle on:Screen current |
|
715 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
716 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
717 |
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
|
718 |
" |
283 | 719 |
! |
720 |
||
721 |
fromScreen:aRectangle on:aDisplay |
|
722 |
"return an image of a part of the screen, which may be on |
|
723 |
another Display." |
|
724 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
725 |
|depth vis img| |
283 | 726 |
|
727 |
depth := aDisplay depth. |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
728 |
|
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
729 |
"/ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
730 |
"/ 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
|
731 |
"/ (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
|
732 |
"/ 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
|
733 |
"/ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
734 |
vis := aDisplay visualType. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
735 |
(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
|
736 |
depth > 8 ifTrue:[ |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
737 |
depth := 24. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
738 |
] |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
739 |
]. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
740 |
|
283 | 741 |
img := (self implementorForDepth: depth) new. |
742 |
^ img fromScreen:aRectangle on:aDisplay |
|
743 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
744 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
745 |
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
|
746 |
" |
283 | 747 |
! |
748 |
||
749 |
fromScreenArea |
|
750 |
"return an image of a part of the screen; |
|
751 |
let user specify screen area." |
|
752 |
||
753 |
^ self fromScreen:(Rectangle fromUser) |
|
754 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
755 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
756 |
Image fromScreenArea |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
757 |
" |
283 | 758 |
! |
759 |
||
760 |
fromUser |
|
761 |
"return an image of a part of the screen; let user specify screen area. |
|
762 |
Same as fromScreenArea, for ST-80 compatibility" |
|
763 |
||
764 |
^ self fromScreenArea |
|
765 |
||
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
766 |
" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
767 |
Image fromUser |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
768 |
" |
283 | 769 |
! |
770 |
||
771 |
fromView:aView |
|
772 |
"return an image taken from a views contents as currently |
|
773 |
on the screen. The returned image has the same depth and photometric |
|
774 |
as the Display. Notice, that for invisible or partial covered |
|
775 |
views, the returned Image is NOT correct. You may want to raise |
|
776 |
the view before using this method." |
|
777 |
||
778 |
|org dev| |
|
779 |
||
744 | 780 |
dev := aView graphicsDevice. |
283 | 781 |
org := dev translatePoint:(0@0) |
744 | 782 |
from:(aView id) |
783 |
to:dev rootWindowId. |
|
283 | 784 |
^ self fromScreen:(org extent:aView extent) on:dev |
191 | 785 |
|
786 |
" |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
787 |
Image fromView:(NewLauncher allInstances first topView) |
283 | 788 |
Image fromView:(SystemBrowser allInstances first topView) |
191 | 789 |
" |
744 | 790 |
|
791 |
"Modified: 28.5.1996 / 20:23:32 / cg" |
|
157 | 792 |
! ! |
793 |
||
0 | 794 |
!Image methodsFor:'accessing'! |
795 |
||
592 | 796 |
bitsPerSample |
797 |
"return the number of bits per sample. |
|
798 |
The return value is an array of bits-per-plane." |
|
799 |
||
800 |
^ bitsPerSample |
|
801 |
! |
|
802 |
||
803 |
colorMap |
|
804 |
"return the colormap" |
|
805 |
||
806 |
^ colorMap |
|
807 |
! |
|
808 |
||
809 |
colorMap:anArrayOfArrays |
|
810 |
"set the colorMap" |
|
811 |
||
812 |
colorMap := anArrayOfArrays. |
|
813 |
photometric isNil ifTrue:[photometric := #palette]. |
|
814 |
deviceForm notNil ifTrue:[self release] |
|
815 |
||
816 |
"Modified: 31.8.1995 / 03:05:59 / claus" |
|
817 |
"Modified: 23.4.1996 / 10:57:32 / cg" |
|
818 |
! |
|
819 |
||
757 | 820 |
container:aVisualContainer |
821 |
"ignored here - added to allow images to be used like |
|
822 |
VisualComponents (later, Image should inherit from it)" |
|
823 |
||
824 |
"Created: 28.5.1996 / 23:43:49 / cg" |
|
825 |
"Modified: 29.5.1996 / 10:22:23 / cg" |
|
826 |
! |
|
827 |
||
592 | 828 |
depth |
829 |
"return the depth of the image" |
|
830 |
||
831 |
^ self bitsPerPixel |
|
832 |
! |
|
833 |
||
834 |
device |
|
835 |
"return the device, the receiver is associated with. |
|
836 |
Return nil, if the image is unassigned." |
|
837 |
||
838 |
^ device |
|
839 |
! |
|
840 |
||
841 |
extent |
|
842 |
"return the images extent" |
|
843 |
||
844 |
^ width@height |
|
845 |
! |
|
846 |
||
847 |
fullColorId |
|
848 |
"return the id of the full color image on the device. |
|
849 |
Return nil, if the image is unassigned." |
|
850 |
||
851 |
fullColorDeviceForm isNil ifTrue:[^ nil]. |
|
852 |
^ fullColorDeviceForm id |
|
853 |
! |
|
854 |
||
728 | 855 |
graphicsDevice |
856 |
"same as #device - for ST-80 compatibility" |
|
857 |
||
858 |
^ device |
|
859 |
! |
|
744 | 860 |
|
592 | 861 |
height |
862 |
"return the height of the image" |
|
863 |
||
864 |
^ height |
|
865 |
! |
|
866 |
||
867 |
id |
|
868 |
"return the id of the image on the device. |
|
869 |
Return nil, if the image is unassigned." |
|
870 |
||
871 |
deviceForm isNil ifTrue:[^ nil]. |
|
872 |
^ deviceForm id |
|
873 |
! |
|
874 |
||
875 |
monochromeId |
|
876 |
"return the id of the monochrome image on the device. |
|
877 |
Return nil, if the image is unassigned." |
|
878 |
||
879 |
monoDeviceForm isNil ifTrue:[^ nil]. |
|
880 |
^ monoDeviceForm id |
|
881 |
! |
|
882 |
||
883 |
palette |
|
884 |
"return the colormap; ST-80 compatibility" |
|
885 |
||
886 |
^ colorMap |
|
887 |
! |
|
888 |
||
889 |
palette:aColormap |
|
890 |
"set the colormap; ST-80 compatibility" |
|
891 |
||
892 |
colorMap := aColormap |
|
893 |
||
894 |
"Created: 1.2.1996 / 15:09:25 / cg" |
|
895 |
! |
|
896 |
||
897 |
photometric |
|
898 |
"return the photometric, a symbol such as #palette, #rgb etc." |
|
899 |
||
900 |
^ photometric |
|
901 |
! |
|
902 |
||
903 |
samplesPerPixel |
|
904 |
"return the number of samples per pixel in the image. |
|
905 |
The return value is an array of bits-per-plane." |
|
906 |
||
907 |
^ samplesPerPixel |
|
908 |
! |
|
909 |
||
910 |
width |
|
911 |
"return the width of the image" |
|
912 |
||
913 |
^ width |
|
914 |
! ! |
|
915 |
||
916 |
!Image methodsFor:'accessing - pixels'! |
|
917 |
||
0 | 918 |
at:aPoint |
2 | 919 |
"retrieve the pixel at aPoint; return a color. |
920 |
Pixels start at 0@0 for upper left pixel, end at |
|
81 | 921 |
(width-1)@(height-1) for lower right pixel. |
2 | 922 |
You should not use this method for image-processing, its |
81 | 923 |
very slow ... |
924 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
0 | 925 |
|
926 |
^ self atX:aPoint x y:aPoint y |
|
927 |
! |
|
928 |
||
283 | 929 |
at:aPoint put:aColor |
930 |
"set the pixel at aPoint to aColor. |
|
931 |
Pixels start at 0@0 for the upper left pixel, end at |
|
932 |
(width-1)@(height-1) for lower right pixel. |
|
933 |
You should not use this method for image-processing, its |
|
934 |
very slow ... |
|
935 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
936 |
||
937 |
^ self atX:aPoint x y:aPoint y put:aColor |
|
938 |
! |
|
939 |
||
940 |
atPoint:aPoint |
|
941 |
"ST-80 compatibility: return the pixelValue at:aPoint." |
|
942 |
||
943 |
^ self valueAtX:aPoint x y:aPoint y |
|
944 |
! |
|
945 |
||
0 | 946 |
atX:x y:y |
2 | 947 |
"retrieve a pixel at x/y; return a color. |
81 | 948 |
Pixels start at 0@0 for upper left pixel, end at |
949 |
(width-1)@(height-1) for lower right pixel. |
|
2 | 950 |
You should not use this method for image-processing, its |
81 | 951 |
very slow ... |
952 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
2 | 953 |
|
89 | 954 |
|pixel maxPixel r g b| |
955 |
||
956 |
pixel := self valueAtX:x y:y. |
|
957 |
photometric == #blackIs0 ifTrue:[ |
|
746 | 958 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
959 |
^ Color gray:(pixel * (100 / maxPixel)). |
|
89 | 960 |
]. |
961 |
photometric == #whiteIs0 ifTrue:[ |
|
746 | 962 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
963 |
^ Color gray:100 - (pixel * (100 / maxPixel)). |
|
89 | 964 |
]. |
965 |
photometric == #palette ifTrue:[ |
|
746 | 966 |
^ colorMap at:(pixel + 1) |
89 | 967 |
]. |
968 |
photometric == #rgb ifTrue:[ |
|
746 | 969 |
r := (pixel bitShift:16) bitAnd:16rFF. |
970 |
g := (pixel bitShift:8) bitAnd:16rFF. |
|
971 |
b := pixel bitAnd:16rFF. |
|
972 |
^ Color red:r / 255 * 100 |
|
973 |
green:g / 255 * 100 |
|
974 |
blue:b / 255 * 100 |
|
89 | 975 |
]. |
976 |
self error:'invalid photometric' |
|
746 | 977 |
|
978 |
"Modified: 28.5.1996 / 20:54:59 / cg" |
|
2 | 979 |
! |
980 |
||
981 |
atX:x y:y put:aColor |
|
982 |
"set the pixel at x/y to aColor. |
|
81 | 983 |
Pixels start at 0@0 for the upper left pixel, end at |
984 |
(width-1)@(height-1) for the lower right pixel. |
|
89 | 985 |
This method checks if the color can be stored in the image. |
986 |
(i.e. if the receiver is a palette image, the color must be present in there). |
|
987 |
You should not use this method for image-processing, it is very slow ... |
|
81 | 988 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
989 |
||
89 | 990 |
|pixel maxPixel| |
991 |
||
992 |
photometric == #whiteIs0 ifTrue:[ |
|
798 | 993 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
994 |
pixel := maxPixel - (aColor brightness * maxPixel) rounded. |
|
89 | 995 |
] ifFalse:[ |
798 | 996 |
photometric == #blackIs0 ifTrue:[ |
997 |
maxPixel := (1 bitShift:self bitsPerPixel) - 1. |
|
998 |
pixel := (aColor brightness * maxPixel) rounded. |
|
999 |
] ifFalse:[ |
|
1000 |
photometric ~~ #palette ifTrue:[ |
|
1001 |
self error:'format not supported'. |
|
1002 |
^ nil |
|
1003 |
]. |
|
1004 |
pixel := colorMap indexOf:aColor. |
|
1005 |
pixel == 0 ifTrue:[ |
|
1006 |
" |
|
1007 |
the color to be stored is not in the images colormap |
|
1008 |
" |
|
1009 |
self error:'invalid color - not in colorMap' |
|
1010 |
]. |
|
1011 |
pixel := pixel - 1 |
|
1012 |
] |
|
89 | 1013 |
]. |
1014 |
self atX:x y:y putValue:pixel. |
|
798 | 1015 |
|
1016 |
"Modified: 7.6.1996 / 19:17:48 / cg" |
|
81 | 1017 |
! |
1018 |
||
283 | 1019 |
atX:x y:y putValue:aPixelValue |
1020 |
"set the pixel at x/y to aPixelValue. |
|
1021 |
The interpretation of the pixelValue depends on the photometric |
|
1022 |
and the colormap. (see also: Image>>atX:y:put:) |
|
1023 |
Pixels start at 0@0 for the upper left pixel, end at |
|
1024 |
(width-1) @ (height-1) for the lower right pixel. |
|
1025 |
You should not use this method for image-processing, its |
|
1026 |
very slow ... |
|
1027 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
1028 |
||
1029 |
^ self subclassResponsibility |
|
1030 |
! |
|
1031 |
||
1032 |
bits |
|
1033 |
"return the raw image data; depending on the photometric, |
|
1034 |
this has to be interpreted as monochrome, greyscale, |
|
1035 |
palette or rgb data. It is also packed to be dense, so |
|
1036 |
a 4 bitPerSample palette image will store 2 pixels per byte." |
|
1037 |
||
1038 |
^ bytes |
|
1039 |
! |
|
1040 |
||
1041 |
data |
|
1042 |
"for backward compatibility - will vanish" |
|
1043 |
||
1044 |
^ bytes |
|
1045 |
! |
|
1046 |
||
1047 |
data:aByteArray |
|
1048 |
"for backward compatibility - will vanish" |
|
1049 |
||
1050 |
bytes := aByteArray |
|
1051 |
! |
|
1052 |
||
81 | 1053 |
valueAt:aPoint |
1054 |
"retrieve the pixelValue at aPoint; return an integer number. |
|
1055 |
Pixels start at 0@0 for upper left pixel, end at |
|
1056 |
width-1@height-1 for lower right pixel. |
|
1057 |
The returned numbers interpretation depends on the photometric |
|
1058 |
and the colormap. (see also Image>>at: and Image>>atX:y:) |
|
2 | 1059 |
You should not use this method for image-processing, its |
81 | 1060 |
very slow ... |
1061 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
1062 |
||
1063 |
^ self valueAtX:aPoint x y:aPoint y |
|
1064 |
! |
|
1065 |
||
1066 |
valueAtX:x y:y |
|
1067 |
"retrieve the pixelValue at aPoint; return an integer number. |
|
1068 |
Pixels start at 0/0 for upper left pixel, and end at |
|
1069 |
width-1@height-1 for lower right pixel. |
|
1070 |
The returned numbers interpretation depends on the photometric |
|
1071 |
and the colormap. (see also Image>>at: and Image>>atX:y:) |
|
1072 |
You should not use this method for image-processing of |
|
1073 |
big images, its very slow ... |
|
1074 |
(it is meant to access individual pixels - for example, in a bitmap editor)" |
|
2 | 1075 |
|
1076 |
^ self subclassResponsibility |
|
592 | 1077 |
! ! |
1078 |
||
1079 |
!Image methodsFor:'accessing - private'! |
|
1080 |
||
1081 |
bits:aByteArray |
|
1082 |
"set the raw data. |
|
1083 |
||
1084 |
This interface is only to be used when initializing |
|
1085 |
instances or by image readers. Calling for a change of an |
|
1086 |
existing image may confuse later pixel interpretation." |
|
1087 |
||
1088 |
bytes := aByteArray |
|
1089 |
||
1090 |
"Modified: 23.4.1996 / 11:08:28 / cg" |
|
2 | 1091 |
! |
1092 |
||
592 | 1093 |
bitsPerSample:aNumber |
1094 |
"set the number of bits per sample. |
|
1095 |
||
1096 |
This interface is only to be used when initializing |
|
1097 |
instances or by image readers. Calling for a change of an |
|
1098 |
existing image may confuse later pixel interpretation." |
|
1099 |
||
1100 |
bitsPerSample := aNumber |
|
1101 |
||
1102 |
"Modified: 23.4.1996 / 11:08:31 / cg" |
|
1103 |
! |
|
1104 |
||
1105 |
depth:d |
|
1106 |
"set the depth of the image. |
|
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 |
d == 24 ifTrue:[ |
|
1113 |
samplesPerPixel := 3. |
|
1114 |
bitsPerSample := #(8 8 8) |
|
1115 |
] ifFalse:[ |
|
1116 |
samplesPerPixel := 1. |
|
1117 |
bitsPerSample := Array with:d |
|
1118 |
] |
|
1119 |
||
1120 |
"Modified: 23.4.1996 / 11:08:34 / cg" |
|
1121 |
! |
|
1122 |
||
1123 |
extent:anExtent |
|
1124 |
"set the images extent. |
|
1125 |
||
1126 |
This interface is only to be used when initializing |
|
1127 |
instances or by image readers. Calling for a change of an |
|
1128 |
existing image may confuse later pixel interpretation." |
|
1129 |
||
1130 |
width := anExtent x. |
|
1131 |
height := anExtent y |
|
1132 |
||
1133 |
"Modified: 23.4.1996 / 11:08:38 / cg" |
|
1134 |
! |
|
1135 |
||
1136 |
height:aNumber |
|
1137 |
"set the height of the image. |
|
1138 |
||
1139 |
This interface is only to be used when initializing |
|
1140 |
instances or by image readers. Calling for a change of an |
|
1141 |
existing image may confuse later pixel interpretation." |
|
1142 |
||
1143 |
height := aNumber |
|
1144 |
||
1145 |
"Modified: 23.4.1996 / 11:08:40 / cg" |
|
1146 |
! |
|
1147 |
||
1148 |
photometric:aSymbol |
|
1149 |
"set the photometric interpretation of the pixel values. |
|
1150 |
The argument, aSymbol is one of: |
|
1151 |
#blackIs0, #whiteIs0, #palette, #rgb |
|
1152 |
See TIFF documentation, from which the photometric concept is borrowed. |
|
1153 |
||
1154 |
This interface is only to be used when initializing |
|
1155 |
instances or by image readers. Calling for a change of an |
|
1156 |
existing image may confuse later pixel interpretation." |
|
1157 |
||
798 | 1158 |
photometric := aSymbol. |
1159 |
bitsPerSample isNil ifTrue:[ |
|
1160 |
bitsPerSample := Array with:self class imageDepth |
|
1161 |
]. |
|
1162 |
samplesPerPixel isNil ifTrue:[ |
|
1163 |
samplesPerPixel := #(1) |
|
1164 |
]. |
|
1165 |
||
1166 |
"Modified: 7.6.1996 / 16:20:44 / cg" |
|
592 | 1167 |
! |
1168 |
||
1169 |
samplesPerPixel:aNumber |
|
1170 |
"set the array of samples per pixel. |
|
1171 |
||
1172 |
This interface is only to be used when initializing |
|
1173 |
instances or by image readers. Calling for a change of an |
|
1174 |
existing image may confuse later pixel interpretation." |
|
1175 |
||
1176 |
samplesPerPixel := aNumber |
|
1177 |
||
1178 |
"Modified: 23.4.1996 / 11:08:45 / cg" |
|
81 | 1179 |
! |
1180 |
||
283 | 1181 |
width:aNumber |
592 | 1182 |
"set the width of the image. |
1183 |
||
1184 |
This interface is only to be used when initializing |
|
1185 |
instances or by image readers. Calling for a change of an |
|
1186 |
existing image may confuse later pixel interpretation." |
|
283 | 1187 |
|
1188 |
width := aNumber |
|
592 | 1189 |
|
1190 |
"Modified: 23.4.1996 / 11:08:48 / cg" |
|
100 | 1191 |
! |
1192 |
||
283 | 1193 |
width:w height:h |
592 | 1194 |
"set the width and height of the image. |
1195 |
||
1196 |
This interface is only to be used when initializing |
|
1197 |
instances or by image readers. Calling for a change of an |
|
1198 |
existing image may confuse later pixel interpretation." |
|
283 | 1199 |
|
1200 |
width := w. |
|
1201 |
height := h |
|
592 | 1202 |
|
1203 |
"Modified: 23.4.1996 / 11:08:53 / cg" |
|
81 | 1204 |
! |
2 | 1205 |
|
283 | 1206 |
width:w height:h depth:d |
592 | 1207 |
"set the width, height and depth of the image. |
1208 |
||
1209 |
This interface is only to be used when initializing |
|
1210 |
instances or by image readers. Calling for a change of an |
|
1211 |
existing image may confuse later pixel interpretation." |
|
283 | 1212 |
|
1213 |
width := w. |
|
1214 |
height := h. |
|
1215 |
self depth:d. |
|
592 | 1216 |
|
1217 |
"Modified: 23.4.1996 / 11:08:56 / cg" |
|
89 | 1218 |
! |
1219 |
||
283 | 1220 |
width:w height:h depth:d fromArray:bits |
592 | 1221 |
"set the width, height, depth and pixels of the image. |
1222 |
||
1223 |
This interface is only to be used when initializing |
|
1224 |
instances or by image readers. Calling for a change of an |
|
1225 |
existing image may confuse later pixel interpretation." |
|
283 | 1226 |
|
1227 |
width := w. |
|
1228 |
height := h. |
|
1229 |
self depth:d. |
|
1230 |
bytes := bits |
|
592 | 1231 |
|
1232 |
"Modified: 23.4.1996 / 11:08:59 / cg" |
|
1233 |
! |
|
1234 |
||
1235 |
width:w height:h photometric:p samplesPerPixel:spp bitsPerSample:bps colorMap:cm bits:pixels |
|
1236 |
"set all relevant internal state of the image. |
|
1237 |
||
1238 |
This interface is only to be used when initializing |
|
1239 |
instances or by image readers. Calling for a change of an |
|
1240 |
existing image may confuse later pixel interpretation." |
|
1241 |
||
1242 |
width := w. |
|
1243 |
height := h. |
|
1244 |
photometric := p. |
|
1245 |
samplesPerPixel := spp. |
|
1246 |
bitsPerSample := bps. |
|
1247 |
colorMap := cm. |
|
1248 |
bytes := pixels. |
|
1249 |
||
1250 |
"Created: 23.4.1996 / 11:06:40 / cg" |
|
1251 |
"Modified: 23.4.1996 / 11:09:02 / cg" |
|
0 | 1252 |
! ! |
1253 |
||
283 | 1254 |
!Image methodsFor:'binary storage'! |
1255 |
||
1256 |
readBinaryContentsFrom: stream manager: manager |
|
1257 |
"read a binary representation of an image from stream. |
|
1258 |
Redefined to flush any device data." |
|
1259 |
||
1260 |
super readBinaryContentsFrom: stream manager: manager. |
|
1261 |
device := nil. |
|
1262 |
deviceForm := nil. |
|
1263 |
monoDeviceForm := nil. |
|
1264 |
fullColorDeviceForm := nil. |
|
0 | 1265 |
! |
1266 |
||
283 | 1267 |
storeBinaryDefinitionOn: stream manager: manager |
1268 |
"store a binary representation of the receiver on stream. |
|
592 | 1269 |
This is an internal interface for binary storage mechanism. |
283 | 1270 |
Redefined to not store the device form (which is recreated at |
1271 |
load time anyway)" |
|
1272 |
||
1273 |
|tDevice tDeviceForm tMonoDeviceForm tFullColorDeviceForm| |
|
1274 |
||
1275 |
tDevice := device. |
|
1276 |
tDeviceForm := deviceForm. |
|
1277 |
tMonoDeviceForm := monoDeviceForm. |
|
1278 |
tFullColorDeviceForm := fullColorDeviceForm. |
|
1279 |
||
1280 |
device := nil. |
|
1281 |
deviceForm := nil. |
|
1282 |
monoDeviceForm := nil. |
|
1283 |
fullColorDeviceForm := nil. |
|
1284 |
||
1285 |
super storeBinaryDefinitionOn: stream manager: manager. |
|
1286 |
||
1287 |
device := tDevice. |
|
1288 |
deviceForm := tDeviceForm. |
|
1289 |
monoDeviceForm := tMonoDeviceForm. |
|
1290 |
fullColorDeviceForm := tFullColorDeviceForm. |
|
592 | 1291 |
|
1292 |
"Modified: 23.4.1996 / 09:30:50 / cg" |
|
0 | 1293 |
! ! |
1294 |
||
1295 |
!Image methodsFor:'converting'! |
|
1296 |
||
283 | 1297 |
asCachedImage |
592 | 1298 |
"return the receiver associated to the current screens device. |
1299 |
For ST-80 compatibility |
|
1300 |
(ST/X uses Image for both device- and nonDevice-images)" |
|
283 | 1301 |
|
1302 |
^ self on:Screen current |
|
592 | 1303 |
|
1304 |
"Modified: 23.4.1996 / 11:10:32 / cg" |
|
0 | 1305 |
! |
1306 |
||
1307 |
asFormOn:aDevice |
|
72 | 1308 |
"get a device form, with best possible approximation. |
1309 |
remember it in case someone asks again." |
|
1310 |
||
1311 |
|form| |
|
1312 |
||
1313 |
((aDevice == device) and:[deviceForm notNil]) ifTrue:[^ deviceForm]. |
|
0 | 1314 |
|
1315 |
(photometric == #palette) ifTrue:[ |
|
72 | 1316 |
form := self paletteImageAsFormOn:aDevice |
1317 |
] ifFalse:[ |
|
1318 |
(photometric == #rgb) ifTrue:[ |
|
1319 |
form := self rgbImageAsFormOn:aDevice |
|
1320 |
] ifFalse:[ |
|
1321 |
form := self greyImageAsFormOn:aDevice |
|
1322 |
] |
|
0 | 1323 |
]. |
1324 |
||
72 | 1325 |
(device isNil or:[aDevice == device]) ifTrue:[ |
1326 |
"remember this form in the receiver ..." |
|
1327 |
||
1328 |
form notNil ifTrue:[ |
|
1329 |
deviceForm := form. |
|
1330 |
device isNil ifTrue:[ |
|
1331 |
device := aDevice. |
|
1332 |
Lobby register:self |
|
1333 |
] ifFalse:[ |
|
162 | 1334 |
Lobby registerChange:self |
72 | 1335 |
]. |
1336 |
" |
|
1337 |
can save space, by not keeping the images data-bits |
|
1338 |
twice (here and in the device form) |
|
1339 |
" |
|
1340 |
form forgetBits |
|
1341 |
] |
|
0 | 1342 |
]. |
1343 |
||
72 | 1344 |
^ form |
0 | 1345 |
! |
1346 |
||
1347 |
asMonochromeFormOn:aDevice |
|
1348 |
"get a monochrome device form" |
|
1349 |
||
72 | 1350 |
|form| |
1351 |
||
1352 |
((aDevice == device) and:[monoDeviceForm notNil]) ifTrue:[^ monoDeviceForm]. |
|
1353 |
||
0 | 1354 |
(photometric == #palette) ifTrue:[ |
72 | 1355 |
form := self paletteImageAsMonoFormOn:aDevice |
1356 |
] ifFalse:[ |
|
1357 |
(photometric == #rgb) ifTrue:[ |
|
1358 |
form := self rgbImageAsMonoFormOn:aDevice |
|
1359 |
] ifFalse:[ |
|
1360 |
form := self greyImageAsMonoFormOn:aDevice |
|
1361 |
] |
|
0 | 1362 |
]. |
1363 |
||
72 | 1364 |
(device isNil or:[aDevice == device]) ifTrue:[ |
1365 |
"remember this form in the receiver ..." |
|
1366 |
||
1367 |
form notNil ifTrue:[ |
|
1368 |
monoDeviceForm := form. |
|
1369 |
device isNil ifTrue:[ |
|
1370 |
device := aDevice. |
|
1371 |
Lobby register:self |
|
1372 |
] ifFalse:[ |
|
162 | 1373 |
Lobby registerChange:self |
72 | 1374 |
]. |
1375 |
" |
|
1376 |
can save space, by not keeping the images data-bits |
|
1377 |
twice (here and in the device form) |
|
1378 |
" |
|
1379 |
form forgetBits |
|
1380 |
] |
|
0 | 1381 |
]. |
1382 |
||
72 | 1383 |
^ form |
89 | 1384 |
! |
1385 |
||
798 | 1386 |
asOrderedDitheredDepth2GrayImage |
1387 |
"return a dithered depth2 grey image from the receiver image. |
|
1388 |
Uses an 8x8 dithermatrix." |
|
1389 |
||
1390 |
^ Depth2Image |
|
1391 |
width:width |
|
1392 |
height:height |
|
1393 |
fromArray:( |
|
1394 |
self |
|
1395 |
orderedDitheredBitsWithDitherMatrix:(self class orderedDitherMatrixOfSize:8) |
|
1396 |
ditherWidth:8 |
|
1397 |
depth:2) |
|
1398 |
||
1399 |
" |
|
1400 |
|i i2| |
|
1401 |
||
1402 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1403 |
i2 := i asOrderedDitheredDepth2GrayImage |
|
1404 |
||
1405 |
||
1406 |
|i i2| |
|
1407 |
||
1408 |
i := (Image fromFile:'/cdrom/icons/a/a11.ico') magnifiedBy:10. |
|
1409 |
i2 := i asOrderedDitheredDepth2GrayImage |
|
1410 |
||
1411 |
||
1412 |
|i i2| |
|
1413 |
||
1414 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1415 |
i2 := i asOrderedDitheredDepth2GrayImage |
|
1416 |
||
1417 |
||
1418 |
|i i2| |
|
1419 |
||
1420 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1421 |
i2 := i asOrderedDitheredDepth2GrayImage |
|
1422 |
||
1423 |
||
1424 |
|i i2| |
|
1425 |
||
1426 |
i := (Image fromFile:'bitmaps/blue-ball.gif') magnifiedBy:1. |
|
1427 |
i2 := i asOrderedDitheredDepth2GrayImage |
|
1428 |
||
1429 |
||
1430 |
|i i2| |
|
1431 |
||
1432 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1433 |
i2 := i asOrderedDitheredDepth2GrayImage |
|
1434 |
" |
|
1435 |
||
1436 |
"Modified: 7.6.1996 / 17:59:15 / cg" |
|
1437 |
"Created: 7.6.1996 / 18:03:54 / cg" |
|
1438 |
! |
|
1439 |
||
1440 |
asOrderedDitheredDepth4GrayImage |
|
1441 |
"return a dithered depth4 gray image from the receiver image. |
|
1442 |
Uses an 8x8 dithermatrix." |
|
1443 |
||
1444 |
^ Depth4Image |
|
1445 |
width:width |
|
1446 |
height:height |
|
1447 |
fromArray:( |
|
1448 |
self |
|
1449 |
orderedDitheredBitsWithDitherMatrix:(self class orderedDitherMatrixOfSize:8) |
|
1450 |
ditherWidth:8 |
|
1451 |
depth:4) |
|
1452 |
||
1453 |
" |
|
1454 |
|i i2| |
|
1455 |
||
1456 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1457 |
i2 := i asOrderedDitheredDepth4GrayImage |
|
1458 |
||
1459 |
||
1460 |
|i i2| |
|
1461 |
||
1462 |
i := (Image fromFile:'/cdrom/icons/a/a11.ico') magnifiedBy:10. |
|
1463 |
i2 := i asOrderedDitheredDepth4GrayImage |
|
1464 |
||
1465 |
||
1466 |
|i i2| |
|
1467 |
||
1468 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1469 |
i2 := i asOrderedDitheredDepth4GrayImage |
|
1470 |
||
1471 |
||
1472 |
|i i2| |
|
1473 |
||
1474 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1475 |
i2 := i asOrderedDitheredDepth4GrayImage |
|
1476 |
||
1477 |
||
1478 |
|i i2| |
|
1479 |
||
1480 |
i := (Image fromFile:'bitmaps/blue-ball.gif') magnifiedBy:1. |
|
1481 |
i2 := i asOrderedDitheredDepth4GrayImage |
|
1482 |
||
1483 |
||
1484 |
|i i2| |
|
1485 |
||
1486 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1487 |
i2 := i asOrderedDitheredDepth4GrayImage |
|
1488 |
" |
|
1489 |
||
1490 |
"Modified: 7.6.1996 / 16:58:34 / cg" |
|
1491 |
"Created: 7.6.1996 / 18:03:04 / cg" |
|
1492 |
! |
|
1493 |
||
1494 |
asOrderedDitheredDepth8GrayImage |
|
1495 |
"return a dithered depth8 grey image from the receiver image. |
|
1496 |
Uses an 8x8 dithermatrix. |
|
1497 |
This is provided for protocol completeness - 8bit grey images are |
|
1498 |
usually not dithered at all." |
|
1499 |
||
1500 |
^ Depth8Image |
|
1501 |
width:width |
|
1502 |
height:height |
|
1503 |
fromArray:( |
|
1504 |
self |
|
1505 |
orderedDitheredBitsWithDitherMatrix:(self class orderedDitherMatrixOfSize:8) |
|
1506 |
ditherWidth:8 |
|
1507 |
depth:8) |
|
1508 |
||
1509 |
"Modified: 7.6.1996 / 18:08:20 / cg" |
|
1510 |
! |
|
1511 |
||
1512 |
asOrderedDitheredGrayImageDepth:depth |
|
1513 |
"return a dithered depth-x grey image from the receiver image. |
|
1514 |
Uses an 8x8 dithermatrix." |
|
1515 |
||
1516 |
|dither| |
|
1517 |
||
1518 |
dither := self class orderedDitherMatrixOfSize:8. |
|
1519 |
||
1520 |
depth == 1 ifTrue:[ |
|
1521 |
"/ for monochrome, there is highly specialized |
|
1522 |
"/ monochrome dither code available |
|
1523 |
||
1524 |
^ Depth1Image |
|
1525 |
width:width |
|
1526 |
height:height |
|
1527 |
fromArray:( |
|
1528 |
self |
|
1529 |
orderedDitheredMonochromeBitsWithDitherMatrix:dither |
|
1530 |
ditherWidth:8) |
|
1531 |
]. |
|
1532 |
||
1533 |
^ (self class implementorForDepth:depth) |
|
1534 |
width:width |
|
1535 |
height:height |
|
1536 |
fromArray:( |
|
1537 |
self |
|
1538 |
orderedDitheredBitsWithDitherMatrix:dither |
|
1539 |
ditherWidth:8 |
|
1540 |
depth:depth) |
|
1541 |
||
1542 |
" |
|
1543 |
|i i1 i2 i4 i8| |
|
1544 |
||
1545 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1546 |
i1 := i asOrderedDitheredGrayImageDepth:1. |
|
1547 |
i1 inspect. |
|
1548 |
||
1549 |
i2 := i asOrderedDitheredGrayImageDepth:2. |
|
1550 |
i2 inspect. |
|
1551 |
||
1552 |
i4 := i asOrderedDitheredGrayImageDepth:4. |
|
1553 |
i4 inspect. |
|
1554 |
||
1555 |
i8 := i asOrderedDitheredGrayImageDepth:8. |
|
1556 |
i8 inspect. |
|
1557 |
" |
|
1558 |
||
1559 |
" |
|
1560 |
|i i1 i2 i4 i8| |
|
1561 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1562 |
i1 := i asOrderedDitheredGrayImageDepth:1. |
|
1563 |
i1 inspect. |
|
1564 |
||
1565 |
i2 := i asOrderedDitheredGrayImageDepth:2. |
|
1566 |
i2 inspect. |
|
1567 |
||
1568 |
i4 := i asOrderedDitheredGrayImageDepth:4. |
|
1569 |
i4 inspect. |
|
1570 |
||
1571 |
i8 := i asOrderedDitheredGrayImageDepth:8. |
|
1572 |
i8 inspect. |
|
1573 |
" |
|
1574 |
||
1575 |
"Created: 7.6.1996 / 18:03:54 / cg" |
|
1576 |
"Modified: 7.6.1996 / 19:25:03 / cg" |
|
1577 |
! |
|
1578 |
||
1579 |
asOrderedDitheredMonochromeFormOn:aDevice |
|
1580 |
"return a dithered monochrome form from the grey image. |
|
1581 |
Uses a 4x4 dithermatrix." |
|
1582 |
||
1583 |
"/ 4x4 seems a good comprimize between: |
|
1584 |
"/ number of grey levels (8x8 is better) |
|
1585 |
"/ artifacts (4x4 is better) |
|
1586 |
"/ |
|
1587 |
"/ could look at the image and decide upon the number of |
|
1588 |
"/ distinct colors present. Use 8x8 for high-number, |
|
1589 |
"/ 4x4 for small number of colors ... |
|
1590 |
||
1591 |
^ self |
|
1592 |
asOrderedDitheredMonochromeFormOn:aDevice |
|
1593 |
ditherMatrix:(self class orderedDitherMatrixOfSize:4) |
|
1594 |
ditherWidth:4 |
|
1595 |
||
1596 |
"/ ^ self |
|
1597 |
"/ asOrderedDitheredMonochromeFormOn:aDevice |
|
1598 |
"/ ditherMatrix:(self class orderedDitherMatrixOfSize:8) |
|
1599 |
"/ ditherWidth:8 |
|
1600 |
||
1601 |
" |
|
1602 |
|i f| |
|
1603 |
||
1604 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1605 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1606 |
||
1607 |
||
1608 |
|i f| |
|
1609 |
||
1610 |
i := (Image fromFile:'/cdrom/icons/a/a11.ico') magnifiedBy:10. |
|
1611 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1612 |
||
1613 |
||
1614 |
|i f| |
|
1615 |
||
1616 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1617 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1618 |
||
1619 |
||
1620 |
|i f| |
|
1621 |
||
1622 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1623 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1624 |
||
1625 |
||
1626 |
|i f| |
|
1627 |
||
1628 |
i := (Image fromFile:'bitmaps/blue-ball.gif') magnifiedBy:1. |
|
1629 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1630 |
" |
|
1631 |
||
1632 |
"Created: 7.6.1996 / 14:52:32 / cg" |
|
1633 |
"Modified: 7.6.1996 / 16:37:47 / cg" |
|
1634 |
! |
|
1635 |
||
1636 |
asOrderedDitheredMonochromeFormOn:aDevice ditherMatrix:ditherMatrix ditherWidth:dW |
|
1637 |
"return a dithered monochrome form from the image. |
|
1638 |
Uses the passed ditherMatrix and ditherWidth." |
|
1639 |
||
1640 |
|monoBits map| |
|
1641 |
||
1642 |
monoBits := self orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW. |
|
1643 |
map := Array with:(Color black) with:(Color white). |
|
1644 |
^ (Form width:width height:height fromArray:monoBits on:aDevice) |
|
1645 |
colorMap:map |
|
1646 |
||
1647 |
||
1648 |
" |
|
1649 |
|i f| |
|
1650 |
||
1651 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1652 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1653 |
||
1654 |
||
1655 |
|i f| |
|
1656 |
||
1657 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1658 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1659 |
||
1660 |
||
1661 |
|i f| |
|
1662 |
||
1663 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1664 |
f := i asOrderedDitheredMonochromeFormOn:Display. |
|
1665 |
" |
|
1666 |
||
1667 |
"Created: 7.6.1996 / 14:51:42 / cg" |
|
1668 |
"Modified: 7.6.1996 / 17:06:07 / cg" |
|
1669 |
! |
|
1670 |
||
1671 |
asOrderedDitheredMonochromeImage |
|
1672 |
"return a dithered monochrome image from the receiver image. |
|
1673 |
Uses a 4x4 dithermatrix." |
|
1674 |
||
1675 |
"/ 4x4 seems a good comprimize between: |
|
1676 |
"/ number of grey levels (8x8 is better) |
|
1677 |
"/ artifacts (4x4 is better) |
|
1678 |
"/ |
|
1679 |
"/ could look at the image and decide upon the number of |
|
1680 |
"/ distinct colors present. Use 8x8 for high-number, |
|
1681 |
"/ 4x4 for small number of colors ... |
|
1682 |
||
1683 |
^ self |
|
1684 |
asOrderedDitheredMonochromeImageWithDitherMatrix:(self class orderedDitherMatrixOfSize:4) |
|
1685 |
ditherWidth:4 |
|
1686 |
||
1687 |
"/ ^ self |
|
1688 |
"/ asOrderedDitheredMonochromeImageWithDitherMatrix:(self class orderedDitherMatrixOfSize:8) |
|
1689 |
"/ ditherWidth:8 |
|
1690 |
||
1691 |
" |
|
1692 |
|i i2| |
|
1693 |
||
1694 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1695 |
i2 := i asOrderedDitheredMonochromeImage |
|
1696 |
||
1697 |
||
1698 |
|i i2| |
|
1699 |
||
1700 |
i := (Image fromFile:'/cdrom/icons/a/a11.ico') magnifiedBy:10. |
|
1701 |
i2 := i asOrderedDitheredMonochromeImage |
|
1702 |
||
1703 |
||
1704 |
|i i2| |
|
1705 |
||
1706 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
1707 |
i2 := i asOrderedDitheredMonochromeImage |
|
1708 |
||
1709 |
||
1710 |
|i i2| |
|
1711 |
||
1712 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1713 |
i2 := i asOrderedDitheredMonochromeImage |
|
1714 |
||
1715 |
||
1716 |
|i i2| |
|
1717 |
||
1718 |
i := (Image fromFile:'bitmaps/blue-ball.gif') magnifiedBy:1. |
|
1719 |
i2 := i asOrderedDitheredMonochromeImage |
|
1720 |
||
1721 |
||
1722 |
|i i2| |
|
1723 |
||
1724 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1725 |
i2 := i asOrderedDitheredMonochromeImage |
|
1726 |
" |
|
1727 |
||
1728 |
"Created: 7.6.1996 / 15:02:07 / cg" |
|
1729 |
"Modified: 7.6.1996 / 16:58:34 / cg" |
|
1730 |
! |
|
1731 |
||
1732 |
asOrderedDitheredMonochromeImageWithDitherMatrix:ditherMatrix ditherWidth:dW |
|
1733 |
"return a dithered monochrome image from the receiver image. |
|
1734 |
Uses the passed ditherMatrix and ditherWidth." |
|
1735 |
||
1736 |
|monoBits| |
|
1737 |
||
1738 |
monoBits := self orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW. |
|
1739 |
^ (Depth1Image width:width height:height fromArray:monoBits) photometric:#blackIs0 |
|
1740 |
||
1741 |
"order-4 dither: |
|
1742 |
||
1743 |
|i| |
|
1744 |
||
1745 |
i := Image fromFile:'garfield.gif'. |
|
1746 |
i |
|
1747 |
asOrderedDitheredMonochromeImageWithDitherMatrix:(Image orderedDitherMatrixOfSize:4) |
|
1748 |
ditherWidth:4 |
|
1749 |
" |
|
1750 |
||
1751 |
"order-6 dither: |
|
1752 |
||
1753 |
|i| |
|
1754 |
||
1755 |
i := Image fromFile:'garfield.gif'. |
|
1756 |
i |
|
1757 |
asOrderedDitheredMonochromeImageWithDitherMatrix:(Image orderedDitherMatrixOfSize:8) |
|
1758 |
ditherWidth:8 |
|
1759 |
" |
|
1760 |
||
1761 |
||
1762 |
"thresholding at:0.5 (all above 0.5 becomes white): |
|
1763 |
||
1764 |
|i| |
|
1765 |
||
1766 |
i := Image fromFile:'garfield.gif'. |
|
1767 |
i |
|
1768 |
asOrderedDitheredMonochromeImageWithDitherMatrix:(ByteArray new:16 withAll:8) |
|
1769 |
ditherWidth:4 |
|
1770 |
" |
|
1771 |
||
1772 |
"thresholding at: 0.25 (all above 0.25 becomes white): |
|
1773 |
||
1774 |
|i| |
|
1775 |
||
1776 |
i := Image fromFile:'garfield.gif'. |
|
1777 |
i |
|
1778 |
asOrderedDitheredMonochromeImageWithDitherMatrix:(ByteArray new:16 withAll:3) |
|
1779 |
ditherWidth:4 |
|
1780 |
" |
|
1781 |
||
1782 |
"thresholding at: 0.75 (all above 0.75 becomes white): |
|
1783 |
||
1784 |
|i| |
|
1785 |
||
1786 |
i := Image fromFile:'garfield.gif'. |
|
1787 |
i |
|
1788 |
asOrderedDitheredMonochromeImageWithDitherMatrix:(ByteArray new:16 withAll:11) |
|
1789 |
ditherWidth:4 |
|
1790 |
" |
|
1791 |
||
1792 |
"Modified: 7.6.1996 / 17:23:47 / cg" |
|
1793 |
! |
|
1794 |
||
1795 |
asThresholdGrayImageDepth:depth |
|
1796 |
"return a thresholded depth-x grey image from the receiver image." |
|
1797 |
||
1798 |
^ (self class implementorForDepth:depth) |
|
1799 |
width:width |
|
1800 |
height:height |
|
1801 |
fromArray:( |
|
1802 |
self |
|
1803 |
orderedDitheredBitsWithDitherMatrix:#[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8] |
|
1804 |
ditherWidth:4 |
|
1805 |
depth:depth) |
|
1806 |
||
1807 |
" |
|
1808 |
|i i1 i2 i4 i8| |
|
1809 |
||
1810 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1811 |
i1 := i asThresholdGrayImageDepth:1. |
|
1812 |
i1 inspect. |
|
1813 |
||
1814 |
i2 := i asThresholdGrayImageDepth:2. |
|
1815 |
i2 inspect. |
|
1816 |
||
1817 |
i4 := i asThresholdGrayImageDepth:4. |
|
1818 |
i4 inspect. |
|
1819 |
||
1820 |
i8 := i asThresholdGrayImageDepth:8. |
|
1821 |
i8 inspect. |
|
1822 |
" |
|
1823 |
||
1824 |
"Created: 7.6.1996 / 18:13:33 / cg" |
|
1825 |
"Modified: 7.6.1996 / 18:23:51 / cg" |
|
1826 |
! |
|
1827 |
||
1828 |
asThresholdMonochromeFormOn:aDevice |
|
1829 |
"return a threshold monochrome form from the image." |
|
1830 |
||
1831 |
^ self |
|
1832 |
asOrderedDitheredMonochromeFormOn:aDevice |
|
1833 |
ditherMatrix:#[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8] |
|
1834 |
ditherWidth:4 |
|
1835 |
||
1836 |
" |
|
1837 |
|i f| |
|
1838 |
||
1839 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1840 |
f := i asThresholdMonochromeFormOn:Display. |
|
1841 |
||
1842 |
||
1843 |
|i f| |
|
1844 |
||
1845 |
i := (Image fromFile:'/cdrom/icons/a/a11.ico') magnifiedBy:10. |
|
1846 |
f := i asThresholdMonochromeFormOn:Display |
|
1847 |
||
1848 |
||
1849 |
|i f| |
|
1850 |
||
1851 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1852 |
f := i asThresholdMonochromeFormOn:Display. |
|
1853 |
||
1854 |
||
1855 |
|i f| |
|
1856 |
||
1857 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1858 |
f := i asThresholdMonochromeFormOn:Display. |
|
1859 |
" |
|
1860 |
||
1861 |
"Created: 7.6.1996 / 16:15:17 / cg" |
|
1862 |
"Modified: 7.6.1996 / 16:59:25 / cg" |
|
1863 |
! |
|
1864 |
||
1865 |
asThresholdMonochromeImage |
|
1866 |
"return a threshold monochrome image from the image. |
|
1867 |
Threshold means: brightness < 0.5 -> black / otherwise white" |
|
1868 |
||
1869 |
^ self |
|
1870 |
asOrderedDitheredMonochromeImageWithDitherMatrix:#[8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8] |
|
1871 |
ditherWidth:4 |
|
1872 |
||
1873 |
" |
|
1874 |
|i i2| |
|
1875 |
||
1876 |
i := Image fromFile:'bitmaps/claus.gif'. |
|
1877 |
i2 := i asThresholdMonochromeImage |
|
1878 |
||
1879 |
||
1880 |
|i i2| |
|
1881 |
||
1882 |
i := (Image fromFile:'/cdrom/icons/a/a11.ico') magnifiedBy:10. |
|
1883 |
i2 := i asThresholdMonochromeImage |
|
1884 |
||
1885 |
||
1886 |
|i i2| |
|
1887 |
||
1888 |
i := Image fromFile:'bitmaps/granite.tiff'. |
|
1889 |
i2 := i asThresholdMonochromeImage |
|
1890 |
||
1891 |
||
1892 |
|i i2| |
|
1893 |
||
1894 |
i := (Image fromFile:'bitmaps/PasteButton.tiff') magnifiedBy:10. |
|
1895 |
i2 := i asThresholdMonochromeImage |
|
1896 |
" |
|
1897 |
||
1898 |
"Created: 7.6.1996 / 16:15:17 / cg" |
|
1899 |
"Modified: 7.6.1996 / 17:16:10 / cg" |
|
1900 |
! |
|
1901 |
||
283 | 1902 |
fromForm:aForm |
592 | 1903 |
"setup the receiver from a form" |
283 | 1904 |
|
1905 |
|map c0 c1| |
|
1906 |
||
1907 |
width := aForm width. |
|
1908 |
height := aForm height. |
|
1909 |
bytes := aForm bits. |
|
1910 |
bitsPerSample := self bitsPerSample. |
|
1911 |
samplesPerPixel := self samplesPerPixel. |
|
1912 |
map := aForm colorMap. |
|
1913 |
||
1914 |
aForm depth == 1 ifTrue:[ |
|
592 | 1915 |
map isNil ifTrue:[ |
1916 |
photometric := #whiteIs0 |
|
1917 |
] ifFalse:[ |
|
1918 |
c0 := map at:1. |
|
1919 |
c1 := map at:2. |
|
1920 |
((c0 = Color white) |
|
1921 |
and:[c1 = Color black]) ifTrue:[ |
|
1922 |
photometric := #whiteIs0 |
|
1923 |
] ifFalse:[ |
|
1924 |
((c0 = Color black) |
|
1925 |
and:[c1 = Color white]) ifTrue:[ |
|
1926 |
photometric := #blackIs0 |
|
1927 |
] ifFalse:[ |
|
1928 |
photometric := #palette. |
|
1929 |
colorMap := Array with:c0 with:c1. |
|
1930 |
] |
|
1931 |
] |
|
1932 |
] |
|
283 | 1933 |
] ifFalse:[ |
592 | 1934 |
map notNil ifTrue:[ |
1935 |
photometric := #palette. |
|
1936 |
colorMap := map copy. |
|
1937 |
] ifFalse:[ |
|
1938 |
" |
|
1939 |
photometric stays at default |
|
1940 |
(which is rgb for d24, greyscale for others) |
|
1941 |
" |
|
1942 |
] |
|
283 | 1943 |
]. |
592 | 1944 |
|
1945 |
"Modified: 23.4.1996 / 11:11:00 / cg" |
|
283 | 1946 |
! |
1947 |
||
89 | 1948 |
fromImage:anImage |
1949 |
"setup the receiver from another image. |
|
1950 |
Color precision may be lost, if conversion is from a higher depth |
|
191 | 1951 |
image. |
1952 |
WARNING: |
|
1953 |
This implementation is a slow fallback (the loop over the |
|
89 | 1954 |
source pixels is very slow). If this method is used heavily, you |
1955 |
may want to redefine it in concrete subclasses for common source images." |
|
1956 |
||
1957 |
width := anImage width. |
|
1958 |
height := anImage height. |
|
1959 |
bytes := ByteArray uninitializedNew:(self bytesPerRow * height). |
|
1960 |
bitsPerSample := self bitsPerSample. |
|
1961 |
samplesPerPixel := self samplesPerPixel. |
|
191 | 1962 |
self colormapFromImage:anImage. |
89 | 1963 |
anImage colorsFromX:0 y:0 toX:(width-1) y:(height-1) do:[:x :y :clr | |
798 | 1964 |
self atX:x y:y put:clr |
89 | 1965 |
]. |
1966 |
||
1967 |
" |
|
798 | 1968 |
|i i2 i4 i8 i16 i24| |
89 | 1969 |
|
1970 |
i := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
1971 |
i inspect. |
|
1972 |
i2 := Depth2Image fromImage:i. |
|
1973 |
i2 inspect. |
|
1974 |
i4 := Depth4Image fromImage:i. |
|
1975 |
i4 inspect. |
|
1976 |
i8 := Depth8Image fromImage:i. |
|
1977 |
i8 inspect. |
|
1978 |
i24 := Depth24Image fromImage:i. |
|
1979 |
i24 inspect. |
|
1980 |
" |
|
191 | 1981 |
|
1982 |
"Created: 20.9.1995 / 00:59:03 / claus" |
|
798 | 1983 |
"Modified: 7.6.1996 / 16:23:03 / cg" |
191 | 1984 |
! |
1985 |
||
1986 |
fromSubImage:anImage in:aRectangle |
|
1987 |
"setup the receiver from another image, extracting a rectangular |
|
1988 |
area. Color precision may be lost, if conversion is from a higher depth |
|
1989 |
image. |
|
1990 |
WARNING: |
|
1991 |
This implementation is a slow fallback (the loop over the |
|
1992 |
source pixels is very slow). If this method is used heavily, you |
|
1993 |
may want to redefine it in concrete subclasses for the common case of |
|
1994 |
of creating a subImage with the same depth & palette." |
|
1995 |
||
1996 |
|x0 y0| |
|
1997 |
||
1998 |
width := aRectangle width + 1. |
|
1999 |
height := aRectangle height + 1. |
|
2000 |
bytes := ByteArray uninitializedNew:(self bytesPerRow * height). |
|
2001 |
bitsPerSample := self bitsPerSample. |
|
2002 |
samplesPerPixel := self samplesPerPixel. |
|
2003 |
self colormapFromImage:anImage. |
|
2004 |
x0 := aRectangle left. |
|
2005 |
y0 := aRectangle top. |
|
2006 |
((photometric == anImage photometric) |
|
2007 |
and:[self bitsPerPixel == anImage bitsPerPixel |
|
2008 |
and:[colorMap = anImage colorMap]]) ifTrue:[ |
|
2009 |
"/ can do it by value |
|
2010 |
anImage valuesFromX:x0 y:y0 |
|
2011 |
toX:aRectangle right y:aRectangle bottom |
|
2012 |
do:[:x :y :pixelValue | |
|
2013 |
self atX:x-x0 y:y-y0 putValue:pixelValue. |
|
2014 |
] |
|
2015 |
] ifFalse:[ |
|
2016 |
"/ must do it by colors |
|
2017 |
anImage colorsFromX:x0 y:y0 |
|
2018 |
toX:aRectangle right y:aRectangle bottom |
|
2019 |
do:[:x :y :clr | |
|
2020 |
self atX:x-x0 y:y-y0 put:clr. |
|
2021 |
] |
|
2022 |
]. |
|
2023 |
||
2024 |
" |
|
2025 |
|i i2 i4 i8 i24| |
|
2026 |
||
2027 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
2028 |
i inspect. |
|
2029 |
i4 := Depth4Image fromSubImage:i in:(300@160 corner:340@200). |
|
2030 |
i4 inspect. |
|
2031 |
i8 := Depth8Image fromSubImage:i in:(300@160 corner:340@200). |
|
2032 |
i8 inspect. |
|
2033 |
i24 := Depth24Image fromSubImage:i in:(300@160 corner:340@200). |
|
2034 |
i24 inspect. |
|
2035 |
" |
|
2036 |
||
2037 |
"Created: 20.9.1995 / 01:06:02 / claus" |
|
2038 |
"Modified: 20.9.1995 / 10:15:37 / claus" |
|
89 | 2039 |
! |
2040 |
||
283 | 2041 |
monochromeOn:aDevice |
2042 |
"return a monochrome device image of the receiver for aDevice. |
|
2043 |
(monochrome, even if device supports colors)" |
|
2044 |
||
2045 |
((aDevice == device) and:[monoDeviceForm notNil]) ifTrue:[^ self]. |
|
2046 |
(device notNil and:[aDevice ~~ device]) ifTrue:[ |
|
2047 |
"oops, I am already accociated to another device |
|
2048 |
- need a copy ... |
|
2049 |
" |
|
2050 |
^ self copy monochromeOn:aDevice |
|
2051 |
]. |
|
2052 |
monoDeviceForm := self asMonochromeFormOn:aDevice. |
|
2053 |
! |
|
2054 |
||
2055 |
on:aDevice |
|
2056 |
"return an image with the same pixels as the receiver, but |
|
2057 |
associated to aDevice. If the receiver is not yet bound to |
|
2058 |
a device, this will be the receiver. Otherwise, a new image |
|
2059 |
is returned." |
|
2060 |
||
2061 |
((aDevice == device) and:[deviceForm notNil]) ifTrue:[^ self]. |
|
2062 |
(device notNil and:[aDevice ~~ device]) ifTrue:[ |
|
2063 |
"oops, I am already accociated to another device |
|
2064 |
- need a copy ... |
|
2065 |
" |
|
2066 |
^ self copy on:aDevice |
|
2067 |
]. |
|
2068 |
deviceForm := self asFormOn:aDevice. |
|
2069 |
device := aDevice |
|
798 | 2070 |
! |
2071 |
||
2072 |
orderedDitheredBitsWithDitherMatrix:ditherMatrix ditherWidth:dW depth:depth |
|
2073 |
"return the bitmap for a dithered depth-bitmap from the image; |
|
2074 |
with a constant ditherMatrix, this can be used for thresholding. |
|
2075 |
Works for any source depths / photometric, |
|
2076 |
but very very slow since each pixel is processed individually. |
|
2077 |
Redefined by some subclasses for more performance (D8Image)" |
|
2078 |
||
2079 |
|f last dH nDither |
|
2080 |
greyLevels |
|
2081 |
dstIndex "{Class: SmallInteger }" |
|
2082 |
nextDst |
|
2083 |
bytesPerOutRow "{Class: SmallInteger }" |
|
2084 |
pixelsPerByte "{Class: SmallInteger }" |
|
2085 |
outBits |
|
2086 |
w "{Class: SmallInteger }" |
|
2087 |
h "{Class: SmallInteger }" |
|
2088 |
bitCnt "{Class: SmallInteger }" |
|
2089 |
byte "{Class: SmallInteger }" | |
|
2090 |
||
2091 |
Transcript showCR:'slow ordered dither ..'. Transcript endEntry. |
|
2092 |
||
2093 |
nDither := ditherMatrix size. |
|
2094 |
dH := nDither / dW. |
|
2095 |
||
2096 |
w := width. |
|
2097 |
h := height. |
|
2098 |
||
2099 |
greyLevels := 1 bitShift:depth. |
|
2100 |
pixelsPerByte := 8 / depth. |
|
2101 |
||
2102 |
bytesPerOutRow := (w * depth + 7) // 8. |
|
2103 |
outBits := ByteArray uninitializedNew:(bytesPerOutRow * h). |
|
2104 |
(outBits isNil or:[bytes isNil]) ifTrue:[ |
|
2105 |
^ nil |
|
2106 |
]. |
|
2107 |
||
2108 |
dstIndex := 1. |
|
2109 |
0 to:(h-1) do:[:y | |
|
2110 |
nextDst := dstIndex + bytesPerOutRow. |
|
2111 |
byte := 0. |
|
2112 |
bitCnt := 8. |
|
2113 |
||
2114 |
"/ this is the representaion independent (but slow) |
|
2115 |
"/ inner loop - it extracts colors from the receiver |
|
2116 |
||
2117 |
self colorsAtY:y from:0 to:(w-1) do:[:x :clr | |
|
2118 |
|dstClr grey dT pixel| |
|
2119 |
||
2120 |
"/ get the colors grey value [0 .. 1] |
|
2121 |
grey := clr brightness. |
|
2122 |
||
2123 |
"/ remap into [0 .. greyLevels-1] |
|
2124 |
grey := grey * (greyLevels-1). |
|
2125 |
||
2126 |
"/ get threshold pixel [0 .. greyLevels-1] |
|
2127 |
||
2128 |
pixel := grey truncated. |
|
2129 |
||
2130 |
"/ compute the error [0..1] |
|
2131 |
grey := grey - pixel. |
|
2132 |
||
2133 |
"/ map into dither space [0 .. nDither] |
|
2134 |
grey := (grey * (nDither)) rounded. |
|
2135 |
||
2136 |
%{ |
|
2137 |
int __dW = __intVal(dW); |
|
2138 |
int __byte = __intVal(byte); |
|
2139 |
int __dT; |
|
2140 |
int __dstIdx; |
|
2141 |
int __pixel; |
|
2142 |
int __bitCnt = __intVal(bitCnt); |
|
2143 |
||
2144 |
__dT = __ByteArrayInstPtr(ditherMatrix) |
|
2145 |
->ba_element[__intVal(x) % __dW |
|
2146 |
+ (__intVal(y) % __intVal(dH)) * __dW]; |
|
2147 |
||
2148 |
__pixel = __intVal(pixel); |
|
2149 |
||
2150 |
if (__intVal(grey) > __dT) { |
|
2151 |
__pixel++; |
|
2152 |
} |
|
2153 |
__byte = (__byte << __intVal(depth)) | __pixel; |
|
2154 |
||
2155 |
__bitCnt = __bitCnt - __intVal(depth); |
|
2156 |
if (__bitCnt == 0) { |
|
2157 |
__dstIdx = __intVal(dstIndex); |
|
2158 |
__ByteArrayInstPtr(outBits)->ba_element[__dstIdx-1] = __byte; |
|
2159 |
__dstIdx = __dstIdx + 1; |
|
2160 |
dstIndex = __MKSMALLINT(__dstIdx); |
|
2161 |
__byte = 0; |
|
2162 |
__bitCnt = 8; |
|
2163 |
} |
|
2164 |
byte = __MKSMALLINT(__byte); |
|
2165 |
bitCnt = __MKSMALLINT(__bitCnt); |
|
2166 |
%}. |
|
2167 |
0 |
|
2168 |
||
2169 |
]. |
|
2170 |
bitCnt ~~ 8 ifTrue:[ |
|
2171 |
byte := byte bitShift:bitCnt. |
|
2172 |
outBits at:dstIndex put:byte. |
|
2173 |
]. |
|
2174 |
dstIndex := nextDst. |
|
2175 |
]. |
|
2176 |
||
2177 |
^ outBits |
|
2178 |
! |
|
2179 |
||
2180 |
orderedDitheredMonochromeBitsWithDitherMatrix:ditherMatrix ditherWidth:dW |
|
2181 |
"return the bitmap for a dithered monochrome bitmap from the image; |
|
2182 |
with a constant ditherMatrix, this can be used for thresholding. |
|
2183 |
Works for any source depths / photometric, |
|
2184 |
but very very slow since each pixel is processed individually. |
|
2185 |
Redefined by some subclasses for more performance (D8Image)" |
|
2186 |
||
2187 |
|f last dH nDither |
|
2188 |
clr0 clr1 |
|
2189 |
dstIndex "{Class: SmallInteger }" |
|
2190 |
nextDst |
|
2191 |
bytesPerMonoRow "{Class: SmallInteger }" |
|
2192 |
monoBits |
|
2193 |
w "{Class: SmallInteger }" |
|
2194 |
h "{Class: SmallInteger }" |
|
2195 |
bitCnt "{Class: SmallInteger }" |
|
2196 |
byte "{Class: SmallInteger }" | |
|
2197 |
||
2198 |
Transcript showCR:'slow ordered dither ..'. Transcript endEntry. |
|
2199 |
||
2200 |
nDither := ditherMatrix size. |
|
2201 |
dH := nDither / dW. |
|
2202 |
||
2203 |
w := width. |
|
2204 |
h := height. |
|
2205 |
||
2206 |
bytesPerMonoRow := (w + 7) // 8. |
|
2207 |
monoBits := ByteArray uninitializedNew:(bytesPerMonoRow * h). |
|
2208 |
(monoBits isNil or:[bytes isNil]) ifTrue:[ |
|
2209 |
^ nil |
|
2210 |
]. |
|
2211 |
||
2212 |
dstIndex := 1. |
|
2213 |
0 to:(h-1) do:[:y | |
|
2214 |
nextDst := dstIndex + bytesPerMonoRow. |
|
2215 |
byte := 0. |
|
2216 |
bitCnt := 8. |
|
2217 |
self colorsAtY:y from:0 to:(w-1) do:[:x :clr | |
|
2218 |
|dstClr grey dT| |
|
2219 |
||
2220 |
"/ get the colors grey value [0 .. 1] |
|
2221 |
grey := clr brightness. |
|
2222 |
||
2223 |
"/ map into dither space [0 .. nDither] |
|
2224 |
grey := (grey * (nDither)) rounded. |
|
2225 |
||
2226 |
%{ |
|
2227 |
int __dW = __intVal(dW); |
|
2228 |
int __byte = __intVal(byte); |
|
2229 |
int __dT; |
|
2230 |
int __dstIdx; |
|
2231 |
int __bitCnt = __intVal(bitCnt); |
|
2232 |
||
2233 |
__dT = __ByteArrayInstPtr(ditherMatrix) |
|
2234 |
->ba_element[__intVal(x) % __dW |
|
2235 |
+ (__intVal(y) % __intVal(dH)) * __dW]; |
|
2236 |
||
2237 |
__byte = __byte << 1; |
|
2238 |
if (__intVal(grey) > __dT) { |
|
2239 |
__byte = __byte | 1; /* white */ |
|
2240 |
} |
|
2241 |
__bitCnt = __bitCnt - 1; |
|
2242 |
if (__bitCnt == 0) { |
|
2243 |
__dstIdx = __intVal(dstIndex); |
|
2244 |
__ByteArrayInstPtr(monoBits)->ba_element[__dstIdx-1] = __byte; |
|
2245 |
__dstIdx = __dstIdx + 1; |
|
2246 |
dstIndex = __MKSMALLINT(__dstIdx); |
|
2247 |
__byte = 0; |
|
2248 |
__bitCnt = 8; |
|
2249 |
} |
|
2250 |
byte = __MKSMALLINT(__byte); |
|
2251 |
bitCnt = __MKSMALLINT(__bitCnt); |
|
2252 |
%}. |
|
2253 |
0 |
|
2254 |
||
2255 |
"/ "/ compare |
|
2256 |
"/ dT := ditherMatrix at:(x \\ dW) + (y \\ dH * dW) + 1. |
|
2257 |
"/ |
|
2258 |
"/ byte := byte bitShift:1. |
|
2259 |
"/ grey < dT ifTrue:[ |
|
2260 |
"/ byte := byte bitOr:1. |
|
2261 |
"/ ]. |
|
2262 |
"/ bitCnt := bitCnt - 1. |
|
2263 |
"/ bitCnt == 0 ifTrue:[ |
|
2264 |
"/ monoBits at:dstIndex put:byte. |
|
2265 |
"/ dstIndex := dstIndex + 1. |
|
2266 |
"/ byte := 0. |
|
2267 |
"/ bitCnt := 8. |
|
2268 |
"/ ]. |
|
2269 |
||
2270 |
]. |
|
2271 |
bitCnt ~~ 8 ifTrue:[ |
|
2272 |
byte := byte bitShift:bitCnt. |
|
2273 |
monoBits at:dstIndex put:byte. |
|
2274 |
]. |
|
2275 |
dstIndex := nextDst. |
|
2276 |
]. |
|
2277 |
||
2278 |
^ monoBits |
|
283 | 2279 |
! ! |
2280 |
||
2281 |
!Image methodsFor:'converting greyscale images'! |
|
2282 |
||
2283 |
greyImageAsFormOn:aDevice |
|
2284 |
"return a thresholded grey-deviceForm from the grey image." |
|
2285 |
||
2286 |
|pictureDepth nPlanes f| |
|
2287 |
||
2288 |
nPlanes := samplesPerPixel. |
|
2289 |
(nPlanes == 2) ifTrue:[ |
|
386 | 2290 |
'IMAGE: alpha plane ignored' errorPrintNL. |
2291 |
nPlanes := 1 |
|
283 | 2292 |
]. |
2293 |
||
2294 |
pictureDepth := bitsPerSample at:1. |
|
2295 |
||
2296 |
"monochrome is very easy ..." |
|
2297 |
||
2298 |
(pictureDepth == 1) ifTrue:[ |
|
386 | 2299 |
^ Form width:width height:height fromArray:bytes on:aDevice |
283 | 2300 |
]. |
2301 |
||
2302 |
(aDevice visualType == #StaticGray) ifTrue:[ |
|
386 | 2303 |
(aDevice depth == pictureDepth) ifTrue:[ |
2304 |
||
2305 |
"greyscale is easy, if the depths match" |
|
2306 |
||
2307 |
f := Form width:width height:height depth:pictureDepth on:aDevice. |
|
2308 |
f isNil ifTrue:[^ nil]. |
|
2309 |
f initGC. |
|
2310 |
||
2311 |
"if device has white at the opposite corner ..." |
|
2312 |
((aDevice blackpixel == 0) == (photometric == #blackIs0)) ifFalse:[ |
|
2313 |
"have to invert bits" |
|
2314 |
f function:#copyInverted |
|
2315 |
]. |
|
2316 |
aDevice drawBits:bytes depth:pictureDepth width:width height:height |
|
2317 |
x:0 y:0 |
|
2318 |
into:(f id) |
|
2319 |
x:0 y:0 width:width height:height with:(f gcId). |
|
2320 |
^ f |
|
2321 |
]. |
|
2322 |
||
2323 |
"the image has more greylevels than the display - dither" |
|
283 | 2324 |
|
2325 |
" |
|
2326 |
coming soon ... |
|
386 | 2327 |
DitherAlgorithm == #error ifTrue:[ |
2328 |
^ self greyImageAsErrorDitheredGreyFormOn:aDevice |
|
2329 |
]. |
|
283 | 2330 |
" |
386 | 2331 |
DitherAlgorithm == #pattern ifTrue:[ |
2332 |
^ self greyImageAsPatternDitheredGreyFormOn:aDevice |
|
2333 |
]. |
|
2334 |
||
2335 |
"no dither, simply cut off information" |
|
2336 |
(aDevice depth == 1) ifTrue:[ |
|
2337 |
"for monochrome, there is a special method to do this" |
|
2338 |
^ self greyImageAsMonoFormOn:aDevice |
|
2339 |
]. |
|
2340 |
"the general case, will take as many bits from the image |
|
2341 |
as possible for the device" |
|
2342 |
^ self greyImageAsGreyFormOn:aDevice |
|
283 | 2343 |
]. |
2344 |
||
2345 |
||
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2346 |
(aDevice visualType == #PseudoColor |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2347 |
or:[aDevice visualType == #GrayScale |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2348 |
or:[aDevice visualType == #StaticColor]]) ifTrue:[ |
386 | 2349 |
^ self greyImageAsPseudoFormOn:aDevice |
283 | 2350 |
]. |
2351 |
||
2352 |
(aDevice visualType == #TrueColor) ifTrue:[ |
|
386 | 2353 |
^ self greyImageAsTrueColorFormOn:aDevice |
283 | 2354 |
]. |
2355 |
||
2356 |
self error:'cannot convert this format'. |
|
2357 |
^ nil |
|
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2358 |
|
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2359 |
"Modified: 11.12.1995 / 18:55:01 / cg" |
283 | 2360 |
! |
2361 |
||
2362 |
greyImageAsGreyFormOn:aDevice |
|
2363 |
"return an 8-bit Form from the grey image" |
|
2364 |
||
2365 |
|wideBits pictureDepth f map nplanes ncells |
|
2366 |
inverse |
|
2367 |
mapSize "{ Class: SmallInteger }" |
|
2368 |
oldValue "{ Class: SmallInteger }" |
|
2369 |
newValue "{ Class: SmallInteger }" |
|
2370 |
shift "{ Class: SmallInteger }" |
|
2371 |
shift2 "{ Class: SmallInteger }" |
|
2372 |
shift3 "{ Class: SmallInteger }" |
|
2373 |
shift4 "{ Class: SmallInteger }" | |
|
2374 |
||
2375 |
(aDevice depth == 8) ifFalse:[ |
|
2376 |
'IMAGE: non-8 plane displays not supported' errorPrintNL. |
|
2377 |
^ self greyImageAsMonoFormOn:aDevice |
|
2378 |
]. |
|
2379 |
||
2380 |
pictureDepth := bitsPerSample at:1. |
|
2381 |
||
2382 |
wideBits := ByteArray uninitializedNew:(width * height). |
|
2383 |
||
2384 |
map := ByteArray uninitializedNew:(1 bitShift:pictureDepth). |
|
2385 |
||
2386 |
"find the real number of server-planes" |
|
2387 |
nplanes := 8. |
|
2388 |
ncells := 256. |
|
2389 |
[aDevice ncells < ncells] whileTrue:[ |
|
2390 |
nplanes := nplanes - 1. |
|
2391 |
ncells := ncells // 2 |
|
2392 |
]. |
|
2393 |
||
2394 |
"prepare translation table" |
|
2395 |
||
2396 |
shift := nplanes - pictureDepth. |
|
2397 |
shift2 := shift - pictureDepth. |
|
2398 |
shift3 := shift2 - pictureDepth. |
|
2399 |
shift4 := shift3 - pictureDepth. |
|
2400 |
||
2401 |
inverse := aDevice blackpixel ~~ 0. |
|
2402 |
photometric == #blackIs0 ifFalse:[ |
|
2403 |
inverse := inverse not |
|
2404 |
]. |
|
2405 |
mapSize := map size. |
|
2406 |
1 to:mapSize do:[:index | |
|
2407 |
oldValue := index - 1. |
|
2408 |
newValue := oldValue bitShift:shift. |
|
2409 |
newValue := newValue bitOr:(oldValue bitShift:shift2). |
|
2410 |
newValue := newValue bitOr:(oldValue bitShift:shift3). |
|
2411 |
newValue := newValue bitOr:(oldValue bitShift:shift4). |
|
2412 |
inverse ifTrue:[ |
|
2413 |
map at:(map size - index + 1) put:newValue |
|
89 | 2414 |
] ifFalse:[ |
283 | 2415 |
map at:index put:newValue |
2416 |
] |
|
2417 |
]. |
|
2418 |
bytes expandPixels:pictureDepth |
|
2419 |
width:width |
|
2420 |
height:height |
|
2421 |
into:wideBits |
|
2422 |
mapping:map. |
|
2423 |
||
2424 |
f := Form width:width height:height depth:8 on:aDevice. |
|
2425 |
f isNil ifTrue:[^ nil]. |
|
2426 |
f initGC. |
|
2427 |
aDevice drawBits:wideBits depth:8 width:width height:height |
|
2428 |
x:0 y:0 |
|
2429 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
2430 |
^ f |
|
2431 |
! |
|
2432 |
||
2433 |
greyImageAsMonoFormOn:aDevice |
|
2434 |
"return a (thresholded) monochrome Form from the image." |
|
2435 |
||
2436 |
^ self subclassResponsibility |
|
2437 |
! |
|
2438 |
||
2439 |
greyImageAsPatternDitheredGreyFormOn:aDevice |
|
2440 |
"return a dithered greyForm from the grey image. |
|
2441 |
Works for any source/destination depths, but very very slow |
|
2442 |
since each pixel is processed individually. |
|
2443 |
Usually redefined by subclasses for more performance" |
|
2444 |
||
2445 |
|f last |
|
2446 |
x0 "{Class: SmallInteger }" |
|
2447 |
w "{Class: SmallInteger }" |
|
2448 |
h "{Class: SmallInteger }" |
|
2449 |
run "{Class: SmallInteger }" | |
|
2450 |
||
694 | 2451 |
Transcript showCR:'slow dithering ..'. Transcript endEntry. |
283 | 2452 |
|
2453 |
w := width - 1. |
|
2454 |
h := height - 1. |
|
2455 |
||
2456 |
"draw each pixel using dither color (let others do the dithering) |
|
2457 |
although the code is simple, its very slow" |
|
2458 |
||
2459 |
f := Form width:width height:height depth:aDevice depth on:aDevice. |
|
2460 |
f isNil ifTrue:[^ nil]. |
|
2461 |
f initGC. |
|
2462 |
||
2463 |
0 to:h do:[:dstY | |
|
694 | 2464 |
x0 := 0. |
2465 |
run := 0. |
|
2466 |
last := nil. |
|
798 | 2467 |
self colorsAtY:dstY from:0 to:w do:[:x :srcColor | |
694 | 2468 |
|dstColor| |
2469 |
||
2470 |
srcColor ~~ last ifTrue:[ |
|
2471 |
(run ~~ 0) ifTrue:[ |
|
2472 |
f fillRectangleX:x0 y:dstY width:run height:1. |
|
2473 |
]. |
|
2474 |
run := 0. |
|
746 | 2475 |
dstColor := Color gray:(srcColor grayIntensity). |
694 | 2476 |
f paint:dstColor. |
2477 |
last := srcColor. |
|
2478 |
x0 := x |
|
2479 |
]. |
|
2480 |
run := run + 1 |
|
2481 |
]. |
|
2482 |
f fillRectangleX:x0 y:dstY width:run height:1. |
|
283 | 2483 |
]. |
2484 |
^ f |
|
694 | 2485 |
|
798 | 2486 |
"Modified: 7.6.1996 / 19:11:46 / cg" |
283 | 2487 |
! |
2488 |
||
2489 |
greyImageAsPseudoFormOn:aDevice |
|
2490 |
"return an 8-bit pseudo Form from the grey image" |
|
2491 |
||
2492 |
|wideBits pictureDepth f map |
|
2493 |
colorMap usedColors nUsed aColor |
|
2494 |
nColors "{ Class: SmallInteger }" |
|
2495 |
range id| |
|
2496 |
||
2497 |
pictureDepth := bitsPerSample at:1. |
|
2498 |
||
2499 |
(#(2 4 8) includes:pictureDepth) ifFalse:[ |
|
746 | 2500 |
self error:'currently only depth-2, 4 or 8 supported'. |
2501 |
^ nil |
|
283 | 2502 |
]. |
2503 |
||
2504 |
wideBits := ByteArray uninitializedNew:(width * height). |
|
2505 |
||
2506 |
(pictureDepth == 8) ifTrue:[ |
|
746 | 2507 |
"for 8bits, we scan for used colors first; |
2508 |
to avoid allocating too many colors" |
|
2509 |
||
2510 |
usedColors := bytes usedValues. |
|
2511 |
nUsed := usedColors max + 1. |
|
2512 |
||
2513 |
colorMap := Array new:nUsed. |
|
2514 |
photometric == #blackIs0 ifTrue:[ |
|
2515 |
usedColors do:[:grey | |
|
2516 |
colorMap at:(grey + 1) put:(Color gray:(100.0 / 256.0 * grey)) |
|
2517 |
] |
|
2518 |
] ifFalse:[ |
|
2519 |
usedColors do:[:grey | |
|
2520 |
colorMap at:(grey + 1) put:(Color gray:(100 - (100.0 / 256.0 * grey))) |
|
2521 |
] |
|
2522 |
] |
|
89 | 2523 |
] ifFalse:[ |
746 | 2524 |
nColors := (1 bitShift:pictureDepth). |
2525 |
colorMap := Array new:nColors. |
|
2526 |
range := 100 / (nColors - 1) asFloat. |
|
2527 |
photometric == #blackIs0 ifTrue:[ |
|
2528 |
1 to:nColors do:[:i | |
|
2529 |
colorMap at:i put:(Color gray:(i - 1) * range). |
|
2530 |
]. |
|
2531 |
] ifFalse:[ |
|
2532 |
1 to:nColors do:[:i | |
|
2533 |
colorMap at:(nColors - i + 1) put:(Color gray:(i - 1) * range). |
|
2534 |
]. |
|
2535 |
]. |
|
283 | 2536 |
]. |
2537 |
||
2538 |
"XXX should reduce 8->6->4->2 planes, if not all colors could be allocated" |
|
2539 |
||
2540 |
"setup the translation map" |
|
2541 |
map := ByteArray uninitializedNew:256. |
|
2542 |
nColors := colorMap size. |
|
2543 |
1 to:nColors do:[:i | |
|
746 | 2544 |
aColor := colorMap at:i. |
2545 |
aColor notNil ifTrue:[ |
|
2546 |
aColor := aColor on:aDevice. |
|
2547 |
colorMap at:i put:aColor. |
|
2548 |
id := aColor colorId. |
|
2549 |
id notNil ifTrue:[ |
|
2550 |
map at:i put:id |
|
2551 |
] ifFalse:[ |
|
2552 |
map at:i put:0 |
|
2553 |
] |
|
2554 |
] |
|
89 | 2555 |
]. |
283 | 2556 |
|
2557 |
"expand & translate" |
|
2558 |
bytes expandPixels:pictureDepth |
|
746 | 2559 |
width:width |
2560 |
height:height |
|
2561 |
into:wideBits |
|
2562 |
mapping:map. |
|
283 | 2563 |
|
2564 |
f := Form width:width height:height depth:8 on:aDevice. |
|
2565 |
f isNil ifTrue:[^ nil]. |
|
2566 |
f colorMap:colorMap. |
|
2567 |
f initGC. |
|
2568 |
aDevice drawBits:wideBits depth:8 width:width height:height |
|
746 | 2569 |
x:0 y:0 |
2570 |
into:(f id) x:0 y:0 width:width height:height with:(f gcId). |
|
283 | 2571 |
^ f |
746 | 2572 |
|
2573 |
"Modified: 28.5.1996 / 20:55:57 / cg" |
|
0 | 2574 |
! |
2575 |
||
283 | 2576 |
greyImageAsTrueColorFormOn:aDevice |
2577 |
"return a true-color device-form for the grey-image receiver. |
|
2578 |
TODO: the pixel loops ought to be implemented as inline primitive code ..." |
|
2579 |
||
2580 |
|depth |
|
2581 |
myDepth "{ Class: SmallInteger }" |
|
2582 |
nColors "{ Class: SmallInteger }" |
|
2583 |
colorValues |
|
2584 |
scaleDown scaleRed scaleGreen scaleBlue redShift blueShift greenShift |
|
2585 |
form imageBitsdestIndex |
|
2586 |
bestFormat usedDeviceDepth usedDeviceBitsPerPixel imageBits destIndex |
|
2587 |
nPixels| |
|
2588 |
||
2589 |
"/ this is a slow fallback method; this ought to be |
|
2590 |
"/ redefined in DepthxImage for more performance. |
|
2591 |
||
2592 |
depth := aDevice depth. |
|
2593 |
myDepth := self depth. |
|
2594 |
myDepth > 12 ifTrue:[ |
|
2595 |
self error:'unsupported trueColor depth in greyImageAsTrueColorFormOn:'. |
|
2596 |
^ nil |
|
0 | 2597 |
]. |
2598 |
||
283 | 2599 |
"/ compute scale to map from my pixels into devices range |
2600 |
||
2601 |
scaleDown := 1 bitShift:myDepth. |
|
2602 |
scaleRed := (1 bitShift:aDevice bitsRed). |
|
2603 |
scaleGreen := (1 bitShift:aDevice bitsGreen). |
|
2604 |
scaleBlue := (1 bitShift:aDevice bitsBlue). |
|
2605 |
redShift := aDevice shiftRed. |
|
2606 |
greenShift := aDevice shiftGreen. |
|
2607 |
blueShift := aDevice shiftBlue. |
|
2608 |
||
2609 |
nColors := (1 bitShift:myDepth). |
|
2610 |
colorValues := Array new:nColors. |
|
2611 |
1 to:nColors do:[:i | |
|
2612 |
|v gv bv rv nv| |
|
2613 |
||
2614 |
"/ scale down to 0..1 |
|
2615 |
v := (i-1) / scaleDown. |
|
2616 |
rv := (v * scaleRed) rounded. |
|
2617 |
gv := (v * scaleGreen) rounded. |
|
2618 |
bv := (v * scaleBlue) rounded. |
|
2619 |
nv := rv bitShift:redShift. |
|
2620 |
nv := nv bitOr:(gv bitShift:greenShift). |
|
2621 |
nv := nv bitOr:(bv bitShift:blueShift). |
|
2622 |
colorValues at:i put:nv |
|
0 | 2623 |
]. |
283 | 2624 |
photometric == #whiteIs0 ifTrue:[ |
2625 |
"/ reverse the order; 0 is brightest |
|
2626 |
colorValues reverse |
|
2627 |
]. |
|
193 | 2628 |
|
2629 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2630 |
usedDeviceDepth := bestFormat at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2631 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
193 | 2632 |
|
283 | 2633 |
"/ for now, only support some depths |
2634 |
||
2635 |
usedDeviceBitsPerPixel == 16 ifTrue:[ |
|
2636 |
imageBits := ByteArray uninitializedNew:(width * height * 2). |
|
2637 |
||
2638 |
"/ now, walk over the image and replace |
|
2639 |
"/ colorMap indices by color values in the bits array |
|
2640 |
||
2641 |
destIndex := 1. |
|
2642 |
0 to:height-1 do:[:y | |
|
2643 |
0 to:width-1 do:[:x | |
|
2644 |
|greyValue| |
|
2645 |
||
2646 |
greyValue := self valueAtX:x y:y. |
|
2647 |
imageBits wordAt:destIndex put:(colorValues at:greyValue + 1) MSB:true. |
|
2648 |
destIndex := destIndex + 2. |
|
2649 |
] |
|
2650 |
] |
|
2651 |
] ifFalse:[ |
|
2652 |
usedDeviceBitsPerPixel == 32 ifTrue:[ |
|
2653 |
imageBits := ByteArray uninitializedNew:(width * height * 4). |
|
2654 |
||
2655 |
"/ now, walk over the image and replace |
|
2656 |
"/ colorMap indices by color values in the bits array |
|
2657 |
||
2658 |
destIndex := 1. |
|
2659 |
0 to:height-1 do:[:y | |
|
2660 |
0 to:width-1 do:[:x | |
|
2661 |
|greyValue| |
|
2662 |
||
2663 |
greyValue := self valueAtX:x y:y. |
|
2664 |
imageBits doubleWordAt:destIndex put:(colorValues at:greyValue + 1) MSB:true. |
|
2665 |
destIndex := destIndex + 4. |
|
2666 |
] |
|
2667 |
] |
|
2668 |
] |
|
193 | 2669 |
]. |
2670 |
||
283 | 2671 |
imageBits isNil ifTrue:[ |
2672 |
'IMAGE: unimplemented trueColor depth on greyImageAsTrueColorFormOn:' errorPrintNL. |
|
2673 |
^ self paletteImageAsMonoFormOn:aDevice |
|
2674 |
]. |
|
2675 |
||
2676 |
form := |
|
193 | 2677 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
283 | 2678 |
form isNil ifTrue:[^ nil]. |
193 | 2679 |
form initGC. |
2680 |
||
2681 |
form |
|
283 | 2682 |
copyBitsFrom:imageBits bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
193 | 2683 |
width:width height:height |
2684 |
x:0 y:0 toX:0 y:0. |
|
2685 |
||
2686 |
^ form |
|
2687 |
||
283 | 2688 |
"Created: 20.10.1995 / 22:05:10 / cg" |
2689 |
"Modified: 21.10.1995 / 19:30:37 / cg" |
|
0 | 2690 |
! ! |
2691 |
||
2692 |
!Image methodsFor:'converting palette images'! |
|
2693 |
||
283 | 2694 |
paletteImageAs2PlaneFormOn:aDevice |
2695 |
"return a 2-bit grey-deviceForm from the palette image" |
|
2696 |
||
2697 |
^ self subclassResponsibility |
|
2698 |
! |
|
2699 |
||
0 | 2700 |
paletteImageAsFormOn:aDevice |
72 | 2701 |
"return a device-form for the palette-image receiver" |
2702 |
||
380 | 2703 |
|type ddepth| |
193 | 2704 |
|
379 | 2705 |
(ddepth := aDevice depth) == 1 ifTrue:[ |
2706 |
^ self paletteImageAsMonoFormOn:aDevice |
|
2707 |
]. |
|
2708 |
||
193 | 2709 |
((type := aDevice visualType) == #StaticGray) ifTrue:[ |
386 | 2710 |
ddepth == 8 ifTrue:[ |
2711 |
^ self paletteImageAsGreyFormOn:aDevice |
|
2712 |
]. |
|
2713 |
||
2714 |
DitherAlgorithm == #pattern ifTrue:[ |
|
2715 |
^ self paletteImageAsPatternDitheredGreyFormOn:aDevice |
|
2716 |
]. |
|
2717 |
||
2718 |
ddepth == 2 ifTrue:[ |
|
2719 |
^ self paletteImageAs2PlaneFormOn:aDevice |
|
2720 |
]. |
|
2721 |
||
2722 |
^ self paletteImageAsMonoFormOn:aDevice |
|
0 | 2723 |
]. |
193 | 2724 |
(type == #TrueColor) ifTrue:[ |
386 | 2725 |
^ self paletteImageAsTrueColorFormOn:aDevice |
193 | 2726 |
]. |
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2727 |
(type == #PseudoColor) ifTrue:[ |
386 | 2728 |
^ self paletteImageAsPseudoFormOn:aDevice |
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2729 |
]. |
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2730 |
(type == #StaticColor) ifTrue:[ |
386 | 2731 |
^ self paletteImageAsPseudoFormOn:aDevice |
193 | 2732 |
]. |
2733 |
"/ dump fallback: every device should implement b&w images ... |
|
2734 |
^ self paletteImageAsMonoFormOn:aDevice |
|
306
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2735 |
|
5070ca5adcb1
more for StaticColor visuals (OS/2 display)
Claus Gittinger <cg@exept.de>
parents:
305
diff
changeset
|
2736 |
"Modified: 11.12.1995 / 16:34:52 / cg" |
0 | 2737 |
! |
2738 |
||
283 | 2739 |
paletteImageAsGreyFormOn:aDevice |
2740 |
"return an 8-bit grey-deviceForm from the palette image" |
|
2741 |
||
2742 |
^ self subclassResponsibility |
|
2743 |
! |
|
2744 |
||
0 | 2745 |
paletteImageAsMonoFormOn:aDevice |
72 | 2746 |
"return a 1-bit mono-deviceForm from the palette image" |
0 | 2747 |
|
2 | 2748 |
^ self subclassResponsibility |
0 | 2749 |
! |
2750 |
||
283 | 2751 |
paletteImageAsPatternDitheredGreyFormOn:aDevice |
2752 |
"return a dithered grey-deviceForm from the palette image." |
|
0 | 2753 |
|
2 | 2754 |
^ self subclassResponsibility |
0 | 2755 |
! |
2756 |
||
2757 |
paletteImageAsPseudoFormOn:aDevice |
|
72 | 2758 |
"return a pseudo-deviceForm from the palette image." |
0 | 2759 |
|
89 | 2760 |
|tempImage d temp8| |
2761 |
||
2762 |
d := self depth. |
|
2763 |
(#(1 2 4 8) includes:d) ifTrue:[ |
|
2764 |
" |
|
2765 |
fallback code for some depth's: |
|
2766 |
create a temporary Depth8Image and use its conversion method |
|
2767 |
" |
|
2768 |
temp8 := ByteArray uninitializedNew:(width * height). |
|
2769 |
||
2770 |
bytes expandPixels:d |
|
2771 |
width:width |
|
2772 |
height:height |
|
2773 |
into:temp8 |
|
2774 |
mapping:nil. |
|
2775 |
||
2776 |
tempImage := Image width:width height:height depth:8 fromArray:temp8. |
|
2777 |
tempImage colorMap:colorMap. |
|
2778 |
^ tempImage paletteImageAsPseudoFormOn:aDevice |
|
2779 |
]. |
|
2 | 2780 |
^ self subclassResponsibility |
0 | 2781 |
! |
2782 |
||
193 | 2783 |
paletteImageAsTrueColorFormOn:aDevice |
2784 |
"return a true-color device-form for the palette-image receiver." |
|
2785 |
||
209 | 2786 |
|depth |
2787 |
myDepth "{ Class: SmallInteger }" |
|
2788 |
nColors "{ Class: SmallInteger }" |
|
2789 |
colorValues |
|
193 | 2790 |
scaleRed scaleGreen scaleBlue redShift greenShift blueShift |
194 | 2791 |
form imageBits bestFormat usedDeviceDepth usedDeviceBitsPerPixel |
2792 |
destIndex ok| |
|
193 | 2793 |
|
2794 |
"/ this is a slow fallback method; this ought to be |
|
2795 |
"/ redefined in DepthxImage for more performance. |
|
2796 |
||
2797 |
depth := aDevice depth. |
|
2798 |
myDepth := self bitsPerPixel. |
|
2799 |
myDepth > 12 ifTrue:[ |
|
2800 |
'IMAGE: depth > 12 not supported' errorPrintNL. |
|
2801 |
^ nil |
|
2802 |
]. |
|
2803 |
||
2804 |
"/ gather r/g/b values for all colors in the map ... |
|
2805 |
||
2806 |
nColors := colorMap size. |
|
2807 |
||
2808 |
"/ precompute scales to map from 0..100 into devices range |
|
2809 |
"/ (this may be different for the individual components) |
|
2810 |
||
2811 |
scaleRed := ((1 bitShift:aDevice bitsRed) - 1) / 100. |
|
2812 |
scaleGreen := ((1 bitShift:aDevice bitsGreen) - 1) / 100. |
|
2813 |
scaleBlue := ((1 bitShift:aDevice bitsBlue) - 1) / 100. |
|
2814 |
redShift := aDevice shiftRed. |
|
2815 |
greenShift := aDevice shiftGreen. |
|
2816 |
blueShift := aDevice shiftBlue. |
|
2817 |
||
2818 |
colorValues := Array uninitializedNew:nColors. |
|
2819 |
||
2820 |
1 to:nColors do:[:index | |
|
209 | 2821 |
|clr rv gv bv v "{ Class: SmallInteger }" | |
193 | 2822 |
|
2823 |
clr := colorMap at:index. |
|
2824 |
clr notNil ifTrue:[ |
|
2825 |
rv := (clr red * scaleRed) rounded. |
|
2826 |
gv := (clr green * scaleGreen) rounded. |
|
2827 |
bv := (clr blue * scaleBlue) rounded. |
|
2828 |
||
2829 |
v := rv bitShift:redShift. |
|
2830 |
v := v bitOr:(gv bitShift:greenShift). |
|
2831 |
v := v bitOr:(bv bitShift:blueShift). |
|
2832 |
colorValues at:index put:v. |
|
2833 |
"/ clr print. ' ' print. |
|
2834 |
"/ rv print. ' ' print. gv print. ' ' print. bv print. ' ' print. |
|
2835 |
"/ ' -> ' print. v printNL. |
|
2836 |
||
2837 |
] |
|
2838 |
]. |
|
2839 |
||
2840 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2841 |
usedDeviceDepth := bestFormat at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
2842 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
193 | 2843 |
|
2844 |
"/ for now, only support some depths |
|
2845 |
||
2846 |
usedDeviceBitsPerPixel == 16 ifTrue:[ |
|
2847 |
imageBits := ByteArray uninitializedNew:(width * height * 2). |
|
2848 |
||
2849 |
"/ now, walk over the image and replace |
|
2850 |
"/ colorMap indices by color values in the bits array |
|
2851 |
||
2852 |
ok := false. |
|
194 | 2853 |
|
2854 |
%{ /* OPTIONAL */ |
|
2855 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
|
193 | 2856 |
&& __isArray(colorValues) |
2857 |
&& __isByteArray(_INST(bytes)) |
|
2858 |
&& (myDepth == __MKSMALLINT(8)) |
|
2859 |
&& __isByteArray(imageBits)) { |
|
209 | 2860 |
int x, y, w, h, nPix; |
193 | 2861 |
|
2862 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
2863 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
2864 |
OBJ *ap = __ArrayInstPtr(colorValues)->a_element; |
|
2865 |
||
194 | 2866 |
w = __intVal(_INST(width)); |
209 | 2867 |
h = __intVal(_INST(height)); |
2868 |
nPix = w * h; |
|
2869 |
while (nPix > 0) { |
|
2870 |
unsigned idx, v; |
|
2871 |
OBJ clr; |
|
2872 |
||
2873 |
idx = *srcPtr; |
|
2874 |
clr = ap[idx]; |
|
2875 |
v = __intVal(clr); |
|
193 | 2876 |
#ifdef MSBFIRST |
209 | 2877 |
((short *)dstPtr)[0] = v; |
193 | 2878 |
#else |
209 | 2879 |
dstPtr[0] = (v>>8) & 0xFF; |
2880 |
dstPtr[1] = (v) & 0xFF; |
|
193 | 2881 |
#endif |
209 | 2882 |
dstPtr += 2; |
2883 |
srcPtr += 1; |
|
2884 |
nPix--; |
|
193 | 2885 |
} |
2886 |
ok = true; |
|
2887 |
} |
|
2888 |
%}. |
|
2889 |
ok ifFalse:[ |
|
209 | 2890 |
"/ this fallback is only executed if some type is not |
194 | 2891 |
"/ what the primitive expects (for example, if the bytes-instvar |
209 | 2892 |
"/ is not a ByteArray) |
2893 |
"/ or (more important) the source depth is not 8. |
|
2894 |
"/ The code below uses the (depth independent) but slower |
|
2895 |
"/ #valueAtX:y: to fetch pixel values. |
|
193 | 2896 |
destIndex := 1. |
2897 |
0 to:height-1 do:[:y | |
|
2898 |
0 to:width-1 do:[:x | |
|
2899 |
|colorIndex| |
|
2900 |
||
2901 |
colorIndex := self valueAtX:x y:y. |
|
2902 |
imageBits wordAt:destIndex put:(colorValues at:colorIndex + 1) MSB:true. |
|
2903 |
destIndex := destIndex + 2. |
|
2904 |
] |
|
2905 |
] |
|
2906 |
] |
|
2907 |
] ifFalse:[ |
|
2908 |
usedDeviceBitsPerPixel == 32 ifTrue:[ |
|
2909 |
imageBits := ByteArray uninitializedNew:(width * height * 4). |
|
2910 |
||
2911 |
"/ now, walk over the image and replace |
|
2912 |
"/ colorMap indices by color values in the bits array |
|
2913 |
||
2914 |
ok := false. |
|
194 | 2915 |
|
2916 |
%{ /* OPTIONAL */ |
|
2917 |
if (__bothSmallInteger(_INST(height), _INST(width)) |
|
193 | 2918 |
&& __isArray(colorValues) |
2919 |
&& __isByteArray(_INST(bytes)) |
|
2920 |
&& (myDepth == __MKSMALLINT(8)) |
|
2921 |
&& __isByteArray(imageBits)) { |
|
209 | 2922 |
int x, y, w, h, nPix; |
193 | 2923 |
|
2924 |
unsigned char *srcPtr = _ByteArrayInstPtr(_INST(bytes))->ba_element; |
|
2925 |
char *dstPtr = _ByteArrayInstPtr(imageBits)->ba_element; |
|
2926 |
OBJ *ap = __ArrayInstPtr(colorValues)->a_element; |
|
2927 |
||
194 | 2928 |
w = __intVal(_INST(width)); |
209 | 2929 |
h = __intVal(_INST(height)); |
2930 |
nPix = w * h; |
|
2931 |
while (nPix > 0) { |
|
2932 |
unsigned idx, v; |
|
2933 |
OBJ clr; |
|
2934 |
||
2935 |
idx = *srcPtr; |
|
2936 |
clr = ap[idx]; |
|
2937 |
v = __intVal(clr); |
|
193 | 2938 |
#ifdef MSBFIRST |
209 | 2939 |
((long *)dstPtr)[0] = v; |
193 | 2940 |
#else |
209 | 2941 |
dstPtr[0] = (v>>24) & 0xFF; |
2942 |
dstPtr[1] = (v>>16) & 0xFF; |
|
2943 |
dstPtr[2] = (v>>8) & 0xFF; |
|
2944 |
dstPtr[3] = (v) & 0xFF; |
|
193 | 2945 |
#endif |
209 | 2946 |
dstPtr += 4; |
2947 |
srcPtr += 1; |
|
2948 |
nPix--; |
|
193 | 2949 |
} |
2950 |
ok = true; |
|
2951 |
} |
|
2952 |
%}. |
|
2953 |
ok ifFalse:[ |
|
209 | 2954 |
"/ this fallback is only executed if some type is not |
194 | 2955 |
"/ what the primitive expects (for example, if the bytes-instvar |
209 | 2956 |
"/ is not a ByteArray) |
2957 |
"/ or (more important) the source depth is not 8. |
|
2958 |
"/ The code below uses the (depth independent) but slower |
|
2959 |
"/ #valueAtX:y: to fetch pixel values. |
|
194 | 2960 |
|
193 | 2961 |
destIndex := 1. |
2962 |
0 to:height-1 do:[:y | |
|
2963 |
0 to:width-1 do:[:x | |
|
2964 |
|colorIndex| |
|
2965 |
||
2966 |
colorIndex := self valueAtX:x y:y. |
|
2967 |
imageBits doubleWordAt:destIndex put:(colorValues at:colorIndex + 1) MSB:true. |
|
2968 |
destIndex := destIndex + 4. |
|
2969 |
] |
|
2970 |
] |
|
2971 |
] |
|
2972 |
] |
|
2973 |
]. |
|
2974 |
||
2975 |
imageBits isNil ifTrue:[ |
|
2976 |
'IMAGE: unimplemented trueColor depth in paletteImageAsTrueColorFormOn:' errorPrintNL. |
|
2977 |
^ self paletteImageAsMonoFormOn:aDevice |
|
2978 |
]. |
|
2979 |
||
2980 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
|
2981 |
form isNil ifTrue:[^ nil]. |
|
2982 |
form initGC. |
|
2983 |
||
2984 |
form |
|
2985 |
copyBitsFrom:imageBits bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
|
2986 |
width:width height:height |
|
2987 |
x:0 y:0 toX:0 y:0. |
|
2988 |
||
2989 |
^ form |
|
2990 |
||
2991 |
"Created: 20.10.1995 / 22:05:10 / cg" |
|
2992 |
"Modified: 21.10.1995 / 19:30:26 / cg" |
|
0 | 2993 |
! ! |
2994 |
||
283 | 2995 |
!Image methodsFor:'converting rgb images'! |
2996 |
||
2997 |
rgbImageAs2PlaneFormOn:aDevice |
|
2998 |
"return a 2-bit device form for aDevice from the rgb picture, |
|
2999 |
using a threshold algorithm. |
|
3000 |
(i.e. grey value < 0.25 -> black // 0.25..0.5 -> darkgrey // |
|
3001 |
0.5 .. 0.75 -> lightgrey // > 0.75 -> white)." |
|
3002 |
||
3003 |
^ self subclassResponsibility |
|
0 | 3004 |
! |
3005 |
||
283 | 3006 |
rgbImageAs8BitGreyFormOn:aDevice |
3007 |
"return an 8-bit greyForm from the rgb picture" |
|
0 | 3008 |
|
2 | 3009 |
^ self subclassResponsibility |
0 | 3010 |
! |
3011 |
||
283 | 3012 |
rgbImageAsFormOn:aDevice |
3013 |
"convert am rgb image to a device-form on aDevice. |
|
3014 |
Return the device-form." |
|
3015 |
||
3016 |
|visual| |
|
3017 |
||
3018 |
visual := aDevice visualType. |
|
3019 |
(visual == #StaticGray) ifTrue:[ |
|
3020 |
^ self rgbImageAsGreyFormOn:aDevice |
|
0 | 3021 |
]. |
283 | 3022 |
(visual == #TrueColor) ifTrue:[ |
3023 |
^ self rgbImageAsTrueColorFormOn:aDevice |
|
0 | 3024 |
]. |
283 | 3025 |
^ self rgbImageAsPseudoFormOn:aDevice |
0 | 3026 |
! |
3027 |
||
283 | 3028 |
rgbImageAsGreyFormOn:aDevice |
3029 |
"convert an rgb image to a grey device-form on aDevice |
|
3030 |
(for greyscale displays)" |
|
3031 |
||
3032 |
|deviceDepth| |
|
3033 |
||
3034 |
deviceDepth := aDevice depth. |
|
3035 |
||
3036 |
"I have specially tuned methods for monochrome" |
|
3037 |
(deviceDepth == 1) ifTrue:[ |
|
3038 |
DitherAlgorithm == #error ifTrue:[ |
|
3039 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
3040 |
]. |
|
3041 |
DitherAlgorithm == #pattern ifTrue:[ |
|
3042 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
3043 |
]. |
|
3044 |
^ self rgbImageAsMonoFormOn:aDevice |
|
0 | 3045 |
]. |
3046 |
||
283 | 3047 |
"and for 2plane greyscale (i.e. NeXTs)" |
3048 |
(deviceDepth == 2) ifTrue:[ |
|
3049 |
DitherAlgorithm == #error ifTrue:[ |
|
3050 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
72 | 3051 |
]. |
283 | 3052 |
DitherAlgorithm == #pattern ifTrue:[ |
3053 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
3054 |
]. |
|
3055 |
^ self rgbImageAs2PlaneFormOn:aDevice |
|
0 | 3056 |
]. |
3057 |
||
283 | 3058 |
(deviceDepth == 8) ifTrue:[ |
3059 |
^ self rgbImageAs8BitGreyFormOn:aDevice |
|
0 | 3060 |
]. |
3061 |
||
283 | 3062 |
"mhmh need another converter ... |
3063 |
till then we do:" |
|
3064 |
DitherAlgorithm == #error ifTrue:[ |
|
3065 |
^ self rgbImageAsErrorDitheredGreyFormOn:aDevice |
|
3066 |
]. |
|
3067 |
DitherAlgorithm == #pattern ifTrue:[ |
|
3068 |
^ self rgbImageAsPatternDitheredGreyFormOn:aDevice |
|
3069 |
]. |
|
3070 |
^ self rgbImageAsMonoFormOn:aDevice |
|
3071 |
! |
|
3072 |
||
3073 |
rgbImageAsMonoFormOn:aDevice |
|
3074 |
"return a 1-bit monochrome form for aDevice from the rgb picture, |
|
3075 |
using a threshold algorithm. |
|
3076 |
(i.e. grey value < 0.5 -> black, grey value >= 0.5 -> white)." |
|
3077 |
||
3078 |
^ self subclassResponsibility |
|
193 | 3079 |
! |
3080 |
||
283 | 3081 |
rgbImageAsPatternDitheredGreyFormOn:aDevice |
3082 |
"return a dithered greyForm for aDevice from the palette picture. |
|
3083 |
works for any destination depth. |
|
3084 |
A slow algorithm, using draw into the form (which indirectly does |
|
3085 |
the dither) - should be rewritten." |
|
3086 |
||
3087 |
^ self subclassResponsibility |
|
3088 |
! |
|
3089 |
||
3090 |
rgbImageAsPseudoFormOn:aDevice |
|
3091 |
"return a pseudocolor form from the rgb-picture" |
|
3092 |
||
3093 |
^ self subclassResponsibility |
|
3094 |
! |
|
3095 |
||
3096 |
rgbImageAsTrueColorFormOn:aDevice |
|
3097 |
"return a truecolor form from the rgb-picture." |
|
3098 |
||
3099 |
|bestFormat usedDeviceDepth usedDeviceBitsPerPixel depth |
|
3100 |
form| |
|
193 | 3101 |
|
3102 |
bestFormat := self bestSupportedImageFormatFor:aDevice. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3103 |
usedDeviceDepth := bestFormat at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3104 |
usedDeviceBitsPerPixel := bestFormat at:#bitsPerPixel. |
193 | 3105 |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3106 |
"/ 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
|
3107 |
usedDeviceBitsPerPixel == 15 ifTrue:[ |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3108 |
usedDeviceBitsPerPixel := 16 |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3109 |
]. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
3110 |
|
283 | 3111 |
"/ |
3112 |
"/ only the trivial case, where the depths match |
|
3113 |
"/ is handled here; conversions are supposed to |
|
3114 |
"/ be done in concrete subclasses (see D24Image) |
|
3115 |
"/ |
|
3116 |
self bitsPerPixel == usedDeviceBitsPerPixel ifFalse:[ |
|
3117 |
'IMAGE: unimplemented trueColor depth in rgbImageAsTrueColorFormOn:' errorPrintNL. |
|
3118 |
^ self rgbImageAsMonoFormOn:aDevice |
|
193 | 3119 |
]. |
3120 |
||
283 | 3121 |
form := Form width:width height:height depth:usedDeviceDepth on:aDevice. |
3122 |
form isNil ifTrue:[ |
|
3123 |
'IMAGE: display bitmap creation failed' errorPrintNL. |
|
3124 |
^ nil |
|
193 | 3125 |
]. |
3126 |
form initGC. |
|
3127 |
||
3128 |
form |
|
283 | 3129 |
copyBitsFrom:bytes bitsPerPixel:usedDeviceBitsPerPixel depth:usedDeviceDepth |
193 | 3130 |
width:width height:height |
3131 |
x:0 y:0 toX:0 y:0. |
|
3132 |
||
3133 |
^ form |
|
3134 |
||
283 | 3135 |
"Modified: 21.10.1995 / 19:30:11 / cg" |
3136 |
! ! |
|
3137 |
||
3138 |
!Image methodsFor:'copying'! |
|
3139 |
||
3140 |
postCopy |
|
592 | 3141 |
"redefined to also copy the pixels and the colorMap |
3142 |
and clear out any device handles in the copy." |
|
3143 |
||
283 | 3144 |
bytes := bytes copy. |
3145 |
colorMap := colorMap deepCopy. |
|
3146 |
device := deviceForm := monoDeviceForm := fullColorDeviceForm := nil |
|
592 | 3147 |
|
3148 |
"Modified: 23.4.1996 / 11:12:03 / cg" |
|
283 | 3149 |
! ! |
3150 |
||
3151 |
!Image methodsFor:'displaying'! |
|
3152 |
||
758 | 3153 |
displayFilledOn:aGC |
759 | 3154 |
"display the receiver as an opaque image. |
758 | 3155 |
This allows Images to be wrapped by a FillingWrapper" |
3156 |
||
3157 |
aGC displayOpaqueForm:self x:0 y:0. |
|
3158 |
||
3159 |
"Created: 29.5.1996 / 10:34:18 / cg" |
|
759 | 3160 |
"Modified: 29.5.1996 / 10:52:36 / cg" |
758 | 3161 |
! |
3162 |
||
283 | 3163 |
displayOn:aGC at:aPoint |
592 | 3164 |
"draw the receiver in the graphicsContext, aGC. |
283 | 3165 |
Smalltalk-80 compatibility" |
3166 |
||
672 | 3167 |
self displayOn:aGC x:aPoint x y:aPoint y. |
3168 |
||
3169 |
"Modified: 12.5.1996 / 20:16:38 / cg" |
|
3170 |
! |
|
3171 |
||
3172 |
displayOn:aGC x:x y:y |
|
3173 |
"draw the receiver in the graphicsContext, aGC. |
|
3174 |
Smalltalk-80 compatibility" |
|
3175 |
||
3176 |
aGC displayForm:self x:x y:y. |
|
592 | 3177 |
|
3178 |
"Modified: 23.4.1996 / 11:12:31 / cg" |
|
672 | 3179 |
"Created: 12.5.1996 / 20:14:31 / cg" |
758 | 3180 |
! |
3181 |
||
3182 |
displayStrokedOn:aGC |
|
759 | 3183 |
"display the receiver as an non opaque image. |
758 | 3184 |
This allows Images to be wrapped by a StrokingWrapper" |
3185 |
||
3186 |
aGC displayForm:self x:0 y:0. |
|
3187 |
||
759 | 3188 |
"Created: 29.5.1996 / 10:34:18 / cg" |
3189 |
"Modified: 29.5.1996 / 10:52:30 / cg" |
|
283 | 3190 |
! ! |
3191 |
||
3192 |
!Image methodsFor:'enumerating'! |
|
3193 |
||
3194 |
atY:y from:x1 to:x2 do:aBlock |
|
3195 |
"perform aBlock for each pixel from x1 to x2 in row y. |
|
3196 |
The block is passed the color at each pixel. |
|
3197 |
The code here provides a generic and slow implementation, and |
|
3198 |
should be redefined in concrete subclasses, to avoid some processing |
|
3199 |
when going from pixel to pixel (i.e. the byte-index and mask computations |
|
3200 |
and also the color allocation)." |
|
3201 |
||
798 | 3202 |
self obsoleteMethodWarning:'use #colorsAtY:from:to:do:'. |
3203 |
self colorsAtY:y from:x1 to:x2 do:aBlock |
|
3204 |
||
3205 |
"Modified: 7.6.1996 / 19:13:30 / cg" |
|
3206 |
! |
|
3207 |
||
3208 |
colorsAtY:y from:x1 to:x2 do:aBlock |
|
3209 |
"perform aBlock for each pixel from x1 to x2 in row y. |
|
3210 |
The block is passed the color at each pixel. |
|
3211 |
The code here provides a generic and slow implementation, and |
|
3212 |
should be redefined in concrete subclasses, to avoid some processing |
|
3213 |
when going from pixel to pixel (i.e. the byte-index and mask computations |
|
3214 |
and also the color allocation)." |
|
3215 |
||
283 | 3216 |
|xStart "{Class: SmallInteger }" |
3217 |
xEnd "{Class: SmallInteger }"| |
|
3218 |
||
3219 |
xStart := x1. |
|
3220 |
xEnd := x2. |
|
3221 |
xStart to:xEnd do:[:xRun | |
|
798 | 3222 |
aBlock value:xRun value:(self atX:xRun y:y) |
283 | 3223 |
] |
798 | 3224 |
|
3225 |
"Modified: 7.6.1996 / 18:29:25 / cg" |
|
3226 |
"Created: 7.6.1996 / 19:12:51 / cg" |
|
283 | 3227 |
! |
3228 |
||
3229 |
colorsFromX:xStart y:yStart toX:xEnd y:yEnd do:aBlock |
|
3230 |
"perform aBlock for each color in a rectangular area of the image. |
|
3231 |
Notice, that x and y coordinates start at 0@0 for the upper left corner. |
|
3232 |
The block is passed the x and y coordinates and pixelValue at each pixel. |
|
3233 |
The code here provides a generic and slow implementation, and |
|
3234 |
should be redefined in concrete subclasses, to avoid some processing |
|
3235 |
when going from pixel to pixel (i.e. the byte-index and mask computations, |
|
3236 |
and especially, the color allocations)." |
|
3237 |
||
3238 |
|xS "{Class: SmallInteger }" |
|
3239 |
xE "{Class: SmallInteger }"| |
|
3240 |
||
3241 |
xS := xStart. |
|
3242 |
xE := xEnd. |
|
3243 |
yStart to:yEnd do:[:yRun | |
|
798 | 3244 |
self colorsAtY:yRun from:xStart to:xEnd do:[:xRun :color | |
3245 |
aBlock value:xRun value:yRun value:color |
|
3246 |
] |
|
283 | 3247 |
] |
798 | 3248 |
|
3249 |
"Modified: 7.6.1996 / 19:11:39 / cg" |
|
283 | 3250 |
! |
3251 |
||
3252 |
valueAtY:y from:x1 to:x2 do:aBlock |
|
3253 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
|
798 | 3254 |
Obsolete - remains for backward compatibility." |
3255 |
||
3256 |
self obsoleteMethodWarning:'use #valuesAtY:from:to:do:'. |
|
3257 |
self valuesAtY:y from:x1 to:x2 do:aBlock |
|
3258 |
||
3259 |
"Modified: 7.6.1996 / 19:11:06 / cg" |
|
3260 |
! |
|
3261 |
||
3262 |
valuesAtY:y from:x1 to:x2 do:aBlock |
|
3263 |
"perform aBlock for each pixelValue from x1 to x2 in row y. |
|
283 | 3264 |
Notice, that x and y coordinates start at 0@0 for the upper left corner. |
3265 |
The block is passed the x coordinate and the pixelValue at each pixel. |
|
3266 |
(see also Image>>atY:from:to:do:). |
|
3267 |
The code here provides a generic and slow implementation, and |
|
3268 |
should be redefined in concrete subclasses, to avoid some processing |
|
3269 |
when going from pixel to pixel (i.e. the byte-index and mask computations)." |
|
3270 |
||
3271 |
|xStart "{Class: SmallInteger }" |
|
3272 |
xEnd "{Class: SmallInteger }"| |
|
3273 |
||
3274 |
xStart := x1. |
|
3275 |
xEnd := x2. |
|
3276 |
xStart to:xEnd do:[:xRun | |
|
692 | 3277 |
aBlock value:xRun value:(self valueAtX:xRun y:y) |
283 | 3278 |
] |
692 | 3279 |
|
3280 |
"Modified: 17.5.1996 / 12:03:59 / cg" |
|
798 | 3281 |
"Created: 7.6.1996 / 19:09:51 / cg" |
283 | 3282 |
! |
3283 |
||
3284 |
valuesFromX:xStart y:yStart toX:xEnd y:yEnd do:aBlock |
|
3285 |
"perform aBlock for each pixelValue in a rectangular area of the image. |
|
3286 |
Notice, that x and y coordinates start at 0@0 for the upper left corner. |
|
3287 |
The block is passed the x and y coordinates and pixelValue at each pixel. |
|
3288 |
The code here provides a generic and slow implementation, and |
|
3289 |
should be redefined in concrete subclasses, to avoid some processing |
|
3290 |
when going from pixel to pixel (i.e. the byte-index and mask computations)." |
|
3291 |
||
3292 |
|xS "{Class: SmallInteger }" |
|
3293 |
xE "{Class: SmallInteger }"| |
|
3294 |
||
3295 |
xS := xStart. |
|
3296 |
xE := xEnd. |
|
3297 |
yStart to:yEnd do:[:yRun | |
|
798 | 3298 |
self valuesAtY:yRun from:xStart to:xEnd do:[:xRun :pixel | |
3299 |
aBlock value:xRun value:yRun value:pixel |
|
3300 |
] |
|
283 | 3301 |
] |
798 | 3302 |
|
3303 |
"Modified: 7.6.1996 / 19:09:29 / cg" |
|
0 | 3304 |
! ! |
3305 |
||
3306 |
!Image methodsFor:'image manipulations'! |
|
3307 |
||
283 | 3308 |
colorMapProcessing:aBlock |
3309 |
"a helper for all kinds of colormap manipulations. |
|
592 | 3310 |
The argument, aBlock is called for every colormap entry, |
3311 |
and the returned value will replace the original entry in the map. |
|
283 | 3312 |
This will fail for non-palette images. |
592 | 3313 |
See examples in Image>>copyWithColorMapProcessing:" |
283 | 3314 |
|
3315 |
|nColors "{ Class: SmallInteger }"| |
|
3316 |
||
3317 |
colorMap isNil ifTrue:[ |
|
592 | 3318 |
^ self error:'image has no colormap' |
283 | 3319 |
]. |
3320 |
||
3321 |
nColors := colorMap size. |
|
3322 |
1 to:nColors do:[:index | |
|
592 | 3323 |
|clr| |
3324 |
||
3325 |
clr := colorMap at:index. |
|
3326 |
clr notNil ifTrue:[ |
|
3327 |
colorMap at:index put:(aBlock value:clr) |
|
3328 |
] |
|
283 | 3329 |
] |
592 | 3330 |
|
3331 |
"Modified: 23.4.1996 / 11:13:55 / cg" |
|
283 | 3332 |
! |
3333 |
||
89 | 3334 |
copyWithColorMapProcessing:aBlock |
81 | 3335 |
"a helper to create & return new images based on the receiver with |
89 | 3336 |
some colorMap processing. The receiver is copied, and the copied images |
3337 |
colormap is modified by replacing entries with the result of the processing block, |
|
3338 |
which is called with the original color values. The block is supposed to return |
|
3339 |
a color." |
|
81 | 3340 |
|
3341 |
|newImage| |
|
3342 |
||
3343 |
newImage := self copy. |
|
3344 |
newImage colorMap isNil ifTrue:[ |
|
3345 |
self error:'no colormap in image'. |
|
3346 |
^ nil |
|
3347 |
]. |
|
3348 |
||
3349 |
" |
|
3350 |
the code below manipulates the colormap. |
|
3351 |
For non-palette images, special code is required |
|
3352 |
" |
|
89 | 3353 |
newImage colorMapProcessing:aBlock. |
81 | 3354 |
^ newImage |
3355 |
||
3356 |
" |
|
3357 |
leave red component only: |
|
3358 |
||
3359 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 3360 |
copyWithColorMapProcessing:[:clr | Color red:(clr red) green:0 blue:0] |
81 | 3361 |
" |
89 | 3362 |
|
81 | 3363 |
" |
3364 |
make it reddish: |
|
3365 |
||
3366 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 3367 |
copyWithColorMapProcessing:[:clr | Color red:((clr red * 2) min:100) green:clr green blue:clr blue] |
81 | 3368 |
" |
89 | 3369 |
|
81 | 3370 |
" |
3371 |
invert: |
|
3372 |
||
3373 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 3374 |
copyWithColorMapProcessing:[:clr | Color red:(100 - clr red) green:(100 - clr green) blue:(100 - clr green)] |
81 | 3375 |
" |
89 | 3376 |
|
81 | 3377 |
" |
3378 |
lighter: |
|
3379 |
||
3380 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 3381 |
copyWithColorMapProcessing:[:clr | |r g b| |
3382 |
r := clr red. g := clr green. b := clr blue. |
|
3383 |
Color red:(r + (100-r//2)) |
|
3384 |
green:(g + (100-g//2)) |
|
3385 |
blue:(b + (100-b//2))] |
|
81 | 3386 |
" |
89 | 3387 |
|
81 | 3388 |
" |
3389 |
darker: |
|
3390 |
||
3391 |
(Image fromFile:'bitmaps/claus.gif') |
|
89 | 3392 |
copyWithColorMapProcessing:[:clr | Color red:(clr red//2) green:(clr green // 2) blue:(clr blue // 2)] |
81 | 3393 |
" |
3394 |
! |
|
3395 |
||
283 | 3396 |
darkened |
3397 |
"return a new image which is slightly darker than the receiver. |
|
3398 |
The receiver must be a palette image (currently). |
|
592 | 3399 |
CAVEAT: Need an argument, which specifies by how much it should be darker." |
283 | 3400 |
|
3401 |
^ self |
|
592 | 3402 |
copyWithColorMapProcessing:[:clr | |
3403 |
Color red:(clr red // 2) |
|
3404 |
green:(clr green // 2) |
|
3405 |
blue:(clr blue // 2)] |
|
283 | 3406 |
|
3407 |
" |
|
3408 |
(Image fromFile:'bitmaps/claus.gif') inspect |
|
3409 |
(Image fromFile:'bitmaps/claus.gif') darkened inspect |
|
3410 |
" |
|
592 | 3411 |
|
3412 |
"Modified: 23.4.1996 / 11:14:34 / cg" |
|
283 | 3413 |
! |
3414 |
||
3415 |
flipHorizontal |
|
3416 |
"inplace horizontal flip" |
|
3417 |
||
3418 |
|w "{Class: SmallInteger }" |
|
3419 |
h "{Class: SmallInteger }" |
|
3420 |
c2 "{Class: SmallInteger }" |
|
3421 |
value | |
|
3422 |
||
3423 |
w := width - 1. |
|
3424 |
h := height - 1. |
|
3425 |
||
3426 |
0 to:h do:[:row | |
|
3427 |
c2 := w. |
|
3428 |
0 to:(w // 2) do:[:col | |
|
3429 |
value := self valueAtX:col y:row. |
|
3430 |
self atX:col y:row putValue:(self valueAtX:c2 y:row). |
|
3431 |
self atX:c2 y:row putValue:value. |
|
3432 |
c2 := c2 - 1. |
|
3433 |
] |
|
81 | 3434 |
]. |
283 | 3435 |
"flush device info" |
3436 |
self restored |
|
3437 |
! |
|
3438 |
||
3439 |
flipVertical |
|
3440 |
"inplace vertical flip" |
|
3441 |
||
3442 |
|h "{Class: SmallInteger }" |
|
3443 |
bytesPerRow "{Class: SmallInteger }" |
|
3444 |
buffer |
|
3445 |
indexLow "{Class: SmallInteger }" |
|
3446 |
indexHi "{Class: SmallInteger }"| |
|
3447 |
||
3448 |
bytesPerRow := self bytesPerRow. |
|
3449 |
buffer := ByteArray new:bytesPerRow. |
|
3450 |
||
3451 |
h := height - 1. |
|
3452 |
||
3453 |
indexLow := 1. |
|
3454 |
indexHi := bytesPerRow * h + 1. |
|
3455 |
||
3456 |
0 to:(h // 2) do:[:row | |
|
3457 |
buffer replaceFrom:1 to:bytesPerRow with:bytes startingAt:indexLow. |
|
3458 |
bytes replaceFrom:indexLow to:(indexLow + bytesPerRow - 1) with:bytes startingAt:indexHi. |
|
3459 |
bytes replaceFrom:indexHi to:(indexHi + bytesPerRow - 1) with:buffer startingAt:1. |
|
3460 |
indexLow := indexLow + bytesPerRow. |
|
3461 |
indexHi := indexHi - bytesPerRow. |
|
3462 |
]. |
|
3463 |
"flush device info" |
|
3464 |
self restored |
|
3465 |
! |
|
3466 |
||
3467 |
hardMagnifiedBy:scalePoint |
|
3468 |
"return a new image magnified by scalePoint, aPoint. |
|
3469 |
This is the general magnification method, handling non-integral values. |
|
3470 |
It is slower than the integral magnification method." |
|
3471 |
||
3472 |
|mX |
|
3473 |
mY |
|
3474 |
newWidth "{ Class: SmallInteger }" |
|
3475 |
newHeight "{ Class: SmallInteger }" |
|
3476 |
w "{ Class: SmallInteger }" |
|
3477 |
h "{ Class: SmallInteger }" |
|
3478 |
newImage newBits bitsPerPixel newBytesPerRow |
|
3479 |
value srcRow| |
|
3480 |
||
3481 |
mX := scalePoint x. |
|
3482 |
mY := scalePoint y. |
|
3483 |
((mX < 0) or:[mY < 0]) ifTrue:[^ nil]. |
|
3484 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
|
3485 |
||
3486 |
newWidth := (width * mX) truncated. |
|
3487 |
newHeight := (height * mY) truncated. |
|
3488 |
||
3489 |
bitsPerPixel := self depth. |
|
3490 |
newBytesPerRow := ((newWidth * bitsPerPixel) + 7) // 8. |
|
3491 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * newHeight). |
|
3492 |
||
3493 |
newImage := self species new. |
|
3494 |
newImage bits:newBits. |
|
3495 |
newImage width:newWidth. |
|
3496 |
newImage height:newHeight. |
|
3497 |
newImage photometric:photometric. |
|
3498 |
newImage samplesPerPixel:samplesPerPixel. |
|
3499 |
newImage bitsPerSample:bitsPerSample. |
|
3500 |
newImage colorMap:colorMap copy. |
|
3501 |
||
3502 |
"walk over destination image fetching pixels from source image" |
|
3503 |
||
3504 |
w := newWidth - 1. |
|
3505 |
h := newHeight - 1. |
|
3506 |
||
3507 |
0 to:h do:[:row | |
|
3508 |
srcRow := (row // mY). |
|
3509 |
0 to:w do:[:col | |
|
3510 |
value := self valueAtX:(col // mX) y:srcRow. |
|
3511 |
newImage atX:col y:row putValue:value. |
|
89 | 3512 |
] |
283 | 3513 |
]. |
3514 |
||
3515 |
^ newImage |
|
3516 |
||
3517 |
"((Image fromFile:'bitmaps/claus.gif') magnifiedBy:0.5@0.5)" |
|
3518 |
! |
|
3519 |
||
3520 |
hardRotated:degrees |
|
592 | 3521 |
"raise an error - rotation by arbitrary angles is not yet implemented" |
3522 |
||
283 | 3523 |
self error:'not yet implemented' |
592 | 3524 |
|
3525 |
"Modified: 23.4.1996 / 11:15:33 / cg" |
|
81 | 3526 |
! |
3527 |
||
54 | 3528 |
lightened |
89 | 3529 |
"return a new image which is slightly brighter than the receiver. |
3530 |
The receiver must be a palette image (currently). |
|
592 | 3531 |
CAVEAT: Need an argument, which specifies by how much it should be lighter." |
54 | 3532 |
|
81 | 3533 |
^ self |
592 | 3534 |
copyWithColorMapProcessing:[:clr | |r g b| |
3535 |
r := clr red. |
|
3536 |
g := clr green. |
|
3537 |
b := clr blue. |
|
3538 |
Color red:(r + (100-r//2)) |
|
3539 |
green:(g + (100-g//2)) |
|
3540 |
blue:(b + (100-b//2))] |
|
81 | 3541 |
|
3542 |
" |
|
3543 |
(Image fromFile:'bitmaps/claus.gif') inspect |
|
3544 |
(Image fromFile:'bitmaps/claus.gif') lightened inspect |
|
3545 |
(Image fromFile:'bitmaps/claus.gif') darkened inspect |
|
3546 |
(Image fromFile:'bitmaps/claus.gif') darkened darkened inspect |
|
3547 |
" |
|
592 | 3548 |
|
3549 |
"Modified: 23.4.1996 / 11:14:41 / cg" |
|
54 | 3550 |
! |
3551 |
||
134 | 3552 |
magnifiedBy:scale |
81 | 3553 |
"return a new image magnified by scalePoint, aPoint. |
72 | 3554 |
If non-integral magnify is asked for, pass the work on to 'hardMagnifyBy:' |
3555 |
while simple (integral) magnifications are handled here." |
|
0 | 3556 |
|
81 | 3557 |
|scalePoint mX mY |
46 | 3558 |
magX "{ Class: SmallInteger }" "new version of stc can find this out itself..." |
3559 |
magY "{ Class: SmallInteger }" |
|
3560 |
srcOffset "{ Class: SmallInteger }" |
|
3561 |
dstOffset "{ Class: SmallInteger }" |
|
3562 |
w "{ Class: SmallInteger }" |
|
3563 |
h "{ Class: SmallInteger }" |
|
3564 |
first |
|
81 | 3565 |
newWidth newHeight newImage newBits |
3566 |
bitsPerPixel newBytesPerRow oldBytesPerRow| |
|
0 | 3567 |
|
81 | 3568 |
scalePoint := scale asPoint. |
3569 |
mX := scalePoint asPoint x. |
|
3570 |
mY := scalePoint asPoint y. |
|
46 | 3571 |
((mX <= 0) or:[mY <= 0]) ifTrue:[^ nil]. |
0 | 3572 |
((mX = 1) and:[mY = 1]) ifTrue:[^ self]. |
3573 |
||
12 | 3574 |
((mX isMemberOf:SmallInteger) and:[mY isMemberOf:SmallInteger]) ifFalse:[ |
134 | 3575 |
^ self hardMagnifiedBy:scalePoint |
0 | 3576 |
]. |
3577 |
||
2 | 3578 |
bitsPerPixel := self depth. |
0 | 3579 |
oldBytesPerRow := ((width * bitsPerPixel) + 7) // 8. |
3580 |
||
46 | 3581 |
w := width. |
3582 |
h := height. |
|
3583 |
magX := mX. |
|
3584 |
magY := mY. |
|
3585 |
||
3586 |
newWidth := w * mX. |
|
3587 |
newHeight := h * mY. |
|
0 | 3588 |
newBytesPerRow := ((newWidth * bitsPerPixel) + 7) // 8. |
3589 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * newHeight). |
|
3590 |
||
3591 |
newImage := self species new. |
|
2 | 3592 |
newImage bits:newBits. |
0 | 3593 |
newImage width:newWidth. |
3594 |
newImage height:newHeight. |
|
3595 |
newImage photometric:photometric. |
|
3596 |
newImage samplesPerPixel:samplesPerPixel. |
|
3597 |
newImage bitsPerSample:bitsPerSample. |
|
3598 |
newImage colorMap:colorMap copy. |
|
3599 |
||
3600 |
mX = 1 ifTrue:[ |
|
72 | 3601 |
"expand rows only" |
3602 |
srcOffset := 1. |
|
3603 |
dstOffset := 1. |
|
0 | 3604 |
|
72 | 3605 |
1 to:h do:[:row | |
3606 |
1 to:mY do:[:i | |
|
3607 |
newBits replaceFrom:dstOffset |
|
3608 |
to:(dstOffset + oldBytesPerRow - 1) |
|
3609 |
with:bytes |
|
3610 |
startingAt:srcOffset. |
|
3611 |
dstOffset := dstOffset + newBytesPerRow |
|
3612 |
]. |
|
3613 |
srcOffset := srcOffset + oldBytesPerRow. |
|
3614 |
]. |
|
0 | 3615 |
] ifFalse:[ |
72 | 3616 |
"expand cols" |
3617 |
(mX > 1) ifTrue:[ |
|
3618 |
dstOffset := 1. |
|
3619 |
srcOffset := 1. |
|
3620 |
1 to:h do:[:row | |
|
3621 |
self magnifyRowFrom:bytes |
|
3622 |
offset:srcOffset |
|
3623 |
into:newBits |
|
3624 |
offset:dstOffset |
|
3625 |
factor:mX. |
|
0 | 3626 |
|
72 | 3627 |
first := dstOffset. |
3628 |
dstOffset := dstOffset + newBytesPerRow. |
|
3629 |
" and copy for row expansion " |
|
3630 |
2 to:mY do:[:i | |
|
3631 |
newBits replaceFrom:dstOffset |
|
3632 |
to:(dstOffset + newBytesPerRow - 1) |
|
3633 |
with:newBits |
|
3634 |
startingAt:first. |
|
3635 |
dstOffset := dstOffset + newBytesPerRow |
|
3636 |
]. |
|
3637 |
srcOffset := srcOffset + oldBytesPerRow. |
|
3638 |
]. |
|
3639 |
] |
|
0 | 3640 |
]. |
3641 |
^ newImage |
|
3642 |
||
134 | 3643 |
"((Image fromFile:'bitmaps/claus.gif') magnifiedBy:1@2)" |
2 | 3644 |
! |
3645 |
||
154 | 3646 |
magnifiedPreservingRatioTo:anExtent |
3647 |
"return a new image magnified to fit into anExtent, |
|
3648 |
preserving the receivers width/height ratio. |
|
3649 |
(i.e. not distorting the image). |
|
3650 |
See also #magnifiedTo: and #magnifiedBy:" |
|
3651 |
||
3652 |
|rX rY| |
|
3653 |
||
3654 |
rX := anExtent x / self width. |
|
3655 |
rY := anExtent y / self height. |
|
3656 |
^ self magnifiedBy:(rX min:rY) |
|
3657 |
||
3658 |
" |
|
3659 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedPreservingRatioTo:100@100) |
|
3660 |
||
3661 |
in contrast to: |
|
3662 |
||
3663 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedTo:100@100) |
|
3664 |
" |
|
3665 |
! |
|
3666 |
||
134 | 3667 |
magnifiedTo:anExtent |
154 | 3668 |
"return a new image magnified to have the size specified by extent. |
3669 |
This may distort the image if the arguments ratio is not the images ratio. |
|
3670 |
See also #magnifiedPreservingRatioTo: and #magnifiedBy:" |
|
89 | 3671 |
|
134 | 3672 |
^ self magnifiedBy:(anExtent / self extent) |
89 | 3673 |
|
3674 |
" |
|
134 | 3675 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedTo:100@100) |
154 | 3676 |
|
3677 |
in contrast to: |
|
3678 |
||
3679 |
((Image fromFile:'bitmaps/garfield.gif') magnifiedPreservingRatioTo:100@100) |
|
89 | 3680 |
" |
3681 |
! |
|
3682 |
||
12 | 3683 |
rotated:degrees |
3684 |
"return a new image from the old one, by rotating the image |
|
46 | 3685 |
degrees clockwise. |
592 | 3686 |
CAVEAT: Currently, only rotation by a multiple of 90 degrees is implemented." |
12 | 3687 |
|
46 | 3688 |
|w "{Class: SmallInteger }" |
3689 |
h "{Class: SmallInteger }" |
|
12 | 3690 |
c2 "{Class: SmallInteger }" |
692 | 3691 |
r2 "{Class: SmallInteger }" |
3692 |
nW "{Class: SmallInteger }" |
|
3693 |
nH "{Class: SmallInteger }" |
|
24 | 3694 |
newImage newBits newBytesPerRow d| |
12 | 3695 |
|
3696 |
d := degrees. |
|
3697 |
[d < 0] whileTrue:[d := d + 360]. |
|
3698 |
d >= 360 ifTrue:[d := d \\ 360]. |
|
46 | 3699 |
d := d truncated. |
3700 |
d == 0 ifTrue:[^ self]. |
|
3701 |
((d ~~ 90) and:[(d ~~ 270) and:[d ~~ 180]]) ifTrue:[ |
|
592 | 3702 |
^ self hardRotated:d |
12 | 3703 |
]. |
3704 |
||
692 | 3705 |
d == 180 ifTrue:[ |
3706 |
nW := width. |
|
3707 |
nH := height. |
|
3708 |
] ifFalse:[ |
|
3709 |
nW := height. |
|
3710 |
nH := width. |
|
3711 |
]. |
|
3712 |
||
3713 |
newBytesPerRow := ((nW * self depth) + 7) // 8. |
|
3714 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * nH). |
|
12 | 3715 |
|
3716 |
newImage := self species new. |
|
3717 |
newImage bits:newBits. |
|
692 | 3718 |
newImage width:nW. |
3719 |
newImage height:nH. |
|
12 | 3720 |
newImage photometric:photometric. |
3721 |
newImage samplesPerPixel:samplesPerPixel. |
|
3722 |
newImage bitsPerSample:bitsPerSample. |
|
3723 |
newImage colorMap:colorMap copy. |
|
3724 |
||
3725 |
w := width - 1. |
|
3726 |
h := height - 1. |
|
3727 |
||
3728 |
d == 90 ifTrue:[ |
|
592 | 3729 |
0 to:h do:[:row | |
3730 |
c2 := h-row. |
|
798 | 3731 |
self valuesAtY:row from:0 to:w do:[:col :pixel | |
692 | 3732 |
newImage atX:c2 y:col putValue:pixel. |
592 | 3733 |
] |
692 | 3734 |
"/ 0 to:w do:[:col | |
3735 |
"/ newImage atX:c2 y:col putValue:(self valueAtX:col y:row). |
|
3736 |
"/ ] |
|
592 | 3737 |
] |
12 | 3738 |
]. |
46 | 3739 |
d == 180 ifTrue:[ |
592 | 3740 |
0 to:h do:[:row | |
692 | 3741 |
r2 := h - row. |
798 | 3742 |
self valuesAtY:row from:0 to:w do:[:col :pixel | |
692 | 3743 |
newImage atX:(w-col) y:r2 putValue:pixel. |
592 | 3744 |
] |
692 | 3745 |
"/ 0 to:w do:[:col | |
3746 |
"/ newImage atX:(w-col) y:r2 putValue:(self valueAtX:col y:row). |
|
3747 |
"/ ] |
|
592 | 3748 |
] |
46 | 3749 |
]. |
12 | 3750 |
d == 270 ifTrue:[ |
592 | 3751 |
0 to:h do:[:row | |
798 | 3752 |
self valuesAtY:row from:0 to:w do:[:col :pixel | |
692 | 3753 |
newImage atX:row y:(w-col) putValue:pixel. |
592 | 3754 |
] |
692 | 3755 |
"/ 0 to:w do:[:col | |
3756 |
"/ newImage atX:row y:(w-col) putValue:(self valueAtX:col y:row). |
|
3757 |
"/ ] |
|
592 | 3758 |
] |
12 | 3759 |
]. |
3760 |
^ newImage |
|
592 | 3761 |
|
692 | 3762 |
" |
3763 |
|i| |
|
3764 |
||
3765 |
i := Image fromFile:'claus.gif'. |
|
3766 |
i inspect. |
|
3767 |
(i rotated:90) inspect. |
|
3768 |
(i rotated:180) inspect. |
|
3769 |
(i rotated:270) inspect. |
|
3770 |
" |
|
3771 |
||
798 | 3772 |
"Modified: 7.6.1996 / 19:09:22 / cg" |
692 | 3773 |
! |
3774 |
||
3775 |
withPixelFunctionApplied:pixelFunctionBlock |
|
3776 |
"return a new image from the old one, by applying a pixel processor |
|
3777 |
(read `Behond photography, by Gerard J. Holzmann; |
|
3778 |
ISBM 0-13-074410-7) |
|
3779 |
See blurred / oilPointed as examples ...)" |
|
3780 |
||
3781 |
|w "{Class: SmallInteger }" |
|
3782 |
h "{Class: SmallInteger }" |
|
3783 |
newImage newBits newBytesPerRow d| |
|
3784 |
||
3785 |
newBytesPerRow := ((width * self depth) + 7) // 8. |
|
3786 |
newBits := ByteArray uninitializedNew:(newBytesPerRow * height). |
|
3787 |
||
3788 |
newImage := self species new. |
|
3789 |
newImage bits:newBits. |
|
3790 |
newImage width:width. |
|
3791 |
newImage height:height. |
|
3792 |
newImage photometric:photometric. |
|
3793 |
newImage samplesPerPixel:samplesPerPixel. |
|
3794 |
newImage bitsPerSample:bitsPerSample. |
|
3795 |
newImage colorMap:colorMap copy. |
|
3796 |
||
3797 |
w := width - 1. |
|
3798 |
h := height - 1. |
|
3799 |
||
3800 |
0 to:h do:[:y | |
|
3801 |
0 to:w do:[:x | |
|
3802 |
newImage atX:x y:y put:(pixelFunctionBlock |
|
3803 |
value:self |
|
3804 |
value:x |
|
3805 |
value:y) |
|
3806 |
] |
|
3807 |
]. |
|
3808 |
^ newImage |
|
3809 |
||
3810 |
" |
|
3811 |
|i| |
|
3812 |
||
3813 |
i := Image fromFile:'claus.gif'. |
|
3814 |
i inspect. |
|
3815 |
(i withPixelFunctionApplied:[:oldImage :x :y | |
|
3816 |
((x between:100 and:200) |
|
3817 |
and:[y between:100 and:200]) ifTrue:[ |
|
3818 |
oldImage atX:x y:y |
|
3819 |
] ifFalse:[ |
|
3820 |
Color black. |
|
3821 |
] |
|
3822 |
]) inspect. |
|
3823 |
" |
|
3824 |
||
3825 |
"Modified: 17.5.1996 / 12:31:44 / cg" |
|
283 | 3826 |
! ! |
3827 |
||
3828 |
!Image methodsFor:'inspecting'! |
|
3829 |
||
3830 |
inspectorClass |
|
3831 |
"redefined to launch an ImageInspector |
|
3832 |
(instead of the default InspectorView)." |
|
3833 |
||
3834 |
^ ImageInspectorView |
|
3835 |
! ! |
|
3836 |
||
3837 |
!Image methodsFor:'instance release'! |
|
3838 |
||
3839 |
disposed |
|
3840 |
"some Image has been collected - nothing to do" |
|
12 | 3841 |
! |
3842 |
||
283 | 3843 |
release |
3844 |
"release device resources" |
|
3845 |
||
3846 |
device := nil. |
|
3847 |
deviceForm := nil. |
|
3848 |
monoDeviceForm := nil. |
|
3849 |
fullColorDeviceForm := nil. |
|
3850 |
Lobby unregister:self |
|
46 | 3851 |
! |
3852 |
||
283 | 3853 |
restored |
3854 |
"flush device specifics after a snapin or binary restore" |
|
3855 |
||
3856 |
self release |
|
3857 |
! ! |
|
3858 |
||
592 | 3859 |
!Image methodsFor:'obsolete'! |
3860 |
||
3861 |
magnifyBy:scale |
|
3862 |
"obsolete: has been renamed to magnifiedBy: for ST-80 compatibility |
|
3863 |
and name consistency ..." |
|
3864 |
||
3865 |
self obsoleteMethodWarning. |
|
3866 |
^ self magnifiedBy:scale |
|
3867 |
! ! |
|
3868 |
||
283 | 3869 |
!Image methodsFor:'pixel copying'! |
3870 |
||
3871 |
copyFrom:anImage x:srcX y:srcY toX:dstX y:dstY width:w height:h |
|
3872 |
"replace a rectangulat area by pixels from another image. |
|
3873 |
WARNING: |
|
3874 |
This implementation is a slow fallback (the loop over the |
|
3875 |
source pixels is very slow). If this method is used heavily, you |
|
3876 |
may want to redefine it in concrete subclasses for the common case of |
|
3877 |
of copying from an Image with the same depth & palette." |
|
3878 |
||
3879 |
|dX dY| |
|
3880 |
||
3881 |
dX := srcX-dstX. |
|
3882 |
dY := srcY-dstY. |
|
3883 |
((photometric == anImage photometric) |
|
3884 |
and:[self bitsPerPixel == anImage bitsPerPixel |
|
3885 |
and:[colorMap = anImage colorMap]]) ifTrue:[ |
|
3886 |
"/ can loop over values |
|
3887 |
anImage valuesFromX:srcX y:srcY |
|
3888 |
toX:srcX+w-1 y:srcY+h-1 |
|
3889 |
do:[:x :y :pixelValue | |
|
3890 |
self atX:x-dX y:y-dY putValue:pixelValue. |
|
3891 |
] |
|
3892 |
] ifFalse:[ |
|
3893 |
"/ must loop over colors |
|
3894 |
anImage colorsFromX:srcX y:srcY |
|
3895 |
toX:srcX+w-1 y:srcY+h-1 |
|
3896 |
do:[:x :y :clr | |
|
3897 |
self atX:x-dX y:y-dY put:clr. |
|
3898 |
] |
|
3899 |
] |
|
3900 |
||
3901 |
" |
|
3902 |
|i1 i8 i4| |
|
3903 |
||
3904 |
i8 := Image fromFile:'bitmaps/garfield.gif'. |
|
3905 |
i8 inspect. |
|
3906 |
i1 := Image fromFile:'bitmaps/SBrowser.xbm'. |
|
3907 |
i1 inspect. |
|
3908 |
||
3909 |
i4 := Depth4Image fromImage:i8. |
|
3910 |
i4 copyFrom:i1 x:0 y:0 toX:20 y:20 width:30 height:30. |
|
3911 |
i4 inspect. |
|
3912 |
" |
|
3913 |
||
3914 |
"Created: 20.9.1995 / 10:14:01 / claus" |
|
3915 |
"Modified: 20.9.1995 / 10:25:31 / claus" |
|
3916 |
! |
|
3917 |
||
3918 |
subImageIn:aRectangle |
|
3919 |
"create and return a new image consisting of a subArea of myself" |
|
3920 |
||
3921 |
^ self class fromSubImage:self in:aRectangle |
|
3922 |
||
3923 |
" |
|
3924 |
|i| |
|
3925 |
||
3926 |
i := Image fromFile:'bitmaps/garfield.gif'. |
|
3927 |
i inspect. |
|
3928 |
(i subImageIn:(300@160 corner:340@200)) inspect |
|
3929 |
" |
|
3930 |
||
3931 |
"Created: 20.9.1995 / 01:24:20 / claus" |
|
3932 |
! ! |
|
3933 |
||
3934 |
!Image methodsFor:'printing & storing'! |
|
3935 |
||
3936 |
storeOn:aStream |
|
592 | 3937 |
"append a printed representation of the receiver to aStream, |
3938 |
from which a copy of it can be reconstructed." |
|
3939 |
||
283 | 3940 |
aStream nextPutAll:'(' , self class name , ' new)'. |
3941 |
aStream nextPutAll:' width: '. width storeOn:aStream. |
|
3942 |
aStream nextPutAll:'; height: '. height storeOn:aStream. |
|
3943 |
aStream nextPutAll:'; photometric:('. photometric storeOn:aStream. |
|
3944 |
aStream nextPutAll:'); bitsPerSample:('. bitsPerSample storeOn:aStream. |
|
3945 |
aStream nextPutAll:'); samplesPerPixel:('. samplesPerPixel storeOn:aStream. |
|
3946 |
aStream nextPutAll:'); bits:('. bytes storeOn:aStream. aStream nextPutAll:') '. |
|
3947 |
colorMap notNil ifTrue:[ |
|
592 | 3948 |
aStream nextPutAll:'; colorMap:('. |
3949 |
colorMap storeOn:aStream. |
|
3950 |
aStream nextPutAll:')'. |
|
283 | 3951 |
]. |
3952 |
aStream nextPutAll:'; yourself' |
|
592 | 3953 |
|
3954 |
"Modified: 23.4.1996 / 11:18:05 / cg" |
|
0 | 3955 |
! ! |
3956 |
||
3957 |
!Image methodsFor:'private'! |
|
3958 |
||
193 | 3959 |
bestSupportedImageFormatFor:aDevice |
3960 |
"scan through the image formats as supported by aDevice, |
|
3961 |
and return the best to use when the receiver is to be represented |
|
3962 |
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
|
3963 |
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
|
3964 |
|
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3965 |
|bestDeviceDepth bestDeviceBitsPerPixel bestInfo maxInfo myDepth maxDepth maxBitsPerPixel | |
193 | 3966 |
|
3967 |
myDepth := self bitsPerPixel. |
|
3968 |
maxBitsPerPixel := 0. |
|
3969 |
||
3970 |
aDevice supportedImageFormats do:[:entry | |
|
3971 |
|deviceImageDepth deviceImageBitsPerPixel| |
|
3972 |
||
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3973 |
deviceImageDepth := entry at:#depth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3974 |
deviceImageBitsPerPixel := entry at:#bitsPerPixel. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3975 |
|
193 | 3976 |
deviceImageBitsPerPixel > maxBitsPerPixel ifTrue:[ |
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3977 |
maxInfo := entry. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3978 |
"/ maxBitsPerPixel := deviceImageBitsPerPixel. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3979 |
"/ maxDepth := deviceImageDepth. |
193 | 3980 |
]. |
3981 |
deviceImageDepth >= myDepth ifTrue:[ |
|
3982 |
deviceImageDepth == myDepth ifTrue:[ |
|
3983 |
"/ take the better one ... |
|
3984 |
(bestDeviceDepth isNil |
|
3985 |
or:[(bestDeviceBitsPerPixel ~~ bestDeviceDepth) |
|
3986 |
and:[deviceImageDepth == deviceImageBitsPerPixel]]) ifTrue:[ |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3987 |
bestInfo := entry. |
193 | 3988 |
bestDeviceDepth := deviceImageDepth. |
3989 |
bestDeviceBitsPerPixel := deviceImageBitsPerPixel. |
|
3990 |
] |
|
3991 |
] ifFalse:[ |
|
3992 |
"/ take the next-larger depth |
|
3993 |
(bestDeviceDepth isNil |
|
3994 |
or:[deviceImageBitsPerPixel < bestDeviceBitsPerPixel]) ifTrue:[ |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
3995 |
bestInfo := entry. |
193 | 3996 |
bestDeviceDepth := deviceImageDepth. |
3997 |
bestDeviceBitsPerPixel := deviceImageBitsPerPixel. |
|
3998 |
] |
|
3999 |
] |
|
4000 |
]. |
|
4001 |
]. |
|
4002 |
bestDeviceDepth isNil ifTrue:[ |
|
4003 |
maxBitsPerPixel == 0 ifTrue:[ |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4004 |
"/ |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4005 |
"/ oops - nothing appropriate |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4006 |
"/ |
193 | 4007 |
bestDeviceDepth := bestDeviceBitsPerPixel := aDevice depth. |
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4008 |
bestInfo := IdentityDictionary new. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4009 |
bestInfo at:#depth put:bestDeviceDepth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4010 |
bestInfo at:#bitsPerPixel put:bestDeviceDepth. |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4011 |
bestInfo at:#padding put:32. |
193 | 4012 |
] ifFalse:[ |
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4013 |
bestInfo := maxInfo. |
193 | 4014 |
] |
4015 |
]. |
|
235
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4016 |
^ bestInfo |
579f3f804a46
padding info in supportedFormats
Claus Gittinger <cg@exept.de>
parents:
230
diff
changeset
|
4017 |
|
193 | 4018 |
"Modified: 21.10.1995 / 03:52:45 / cg" |
283 | 4019 |
! |
4020 |
||
4021 |
colormapFromImage:anImage |
|
4022 |
"setup the receivers colormap from another image. |
|
4023 |
Color precision may be lost, if conversion is from a higher depth |
|
4024 |
image. This does not convert any pixel values; it is non-public helper |
|
4025 |
for fromImage:/fromSubImake:" |
|
4026 |
||
4027 |
samplesPerPixel == 3 ifTrue:[ |
|
4028 |
photometric := #rgb |
|
4029 |
] ifFalse:[ |
|
4030 |
photometric := anImage photometric. |
|
4031 |
photometric == #palette ifTrue:[ |
|
4032 |
colorMap := anImage colorMap copy. |
|
4033 |
" |
|
4034 |
must compress the colormap, if source image has higher depth |
|
4035 |
than myself. |
|
4036 |
" |
|
4037 |
anImage bitsPerPixel > self bitsPerPixel ifTrue:[ |
|
4038 |
" |
|
4039 |
get used colors are extracted into our colorMap |
|
4040 |
(the at-put below will set the pixelValue according the |
|
4041 |
new colorIndex |
|
4042 |
" |
|
4043 |
colorMap := anImage usedColors asArray. |
|
4044 |
colorMap size > (1 bitShift:self bitsPerPixel) ifTrue:[ |
|
4045 |
'IMAGE: possibly too many colors in image' errorPrintNL |
|
4046 |
] |
|
4047 |
] |
|
4048 |
] |
|
4049 |
]. |
|
4050 |
||
4051 |
"Created: 20.9.1995 / 00:58:42 / claus" |
|
4052 |
! |
|
4053 |
||
798 | 4054 |
greyMapForRange:range |
4055 |
"return a collection to map from pixelValues to greyLevels |
|
4056 |
in the range 0..range" |
|
4057 |
||
4058 |
|d n greyMap| |
|
4059 |
||
4060 |
d := self bitsPerPixel. |
|
4061 |
n := 1 bitShift:d. |
|
4062 |
n >= 4096 ifTrue:[ |
|
4063 |
self error:'size not supported - too large'. |
|
4064 |
^ nil |
|
4065 |
]. |
|
4066 |
||
4067 |
greyMap := ByteArray new:n. |
|
4068 |
photometric == #palette ifTrue:[ |
|
4069 |
1 to:(colorMap size) do:[:i | |
|
4070 |
greyMap at:i put:(range * (colorMap at:i) brightness) rounded |
|
4071 |
]. |
|
4072 |
] ifFalse:[ |
|
4073 |
1 to:n do:[:i | |
|
4074 |
greyMap at:i put:(range / (n-1) * (i-1)) rounded |
|
4075 |
]. |
|
4076 |
photometric == #blackIs0 ifTrue:[ |
|
4077 |
"/ we are done |
|
4078 |
] ifFalse:[ |
|
4079 |
photometric == #whiteIs0 ifTrue:[ |
|
4080 |
greyMap reverse |
|
4081 |
] ifFalse:[ |
|
4082 |
self error:'invalid format'. |
|
4083 |
^ nil |
|
4084 |
] |
|
4085 |
] |
|
4086 |
]. |
|
4087 |
^ greyMap |
|
4088 |
||
4089 |
" |
|
4090 |
Depth8Image new greyMapForRange:64 |
|
4091 |
Depth4Image new greyMapForRange:64 |
|
4092 |
||
4093 |
Depth4Image new greyMapForRange:1 |
|
4094 |
" |
|
4095 |
||
4096 |
"Modified: 7.6.1996 / 16:08:59 / cg" |
|
4097 |
! |
|
4098 |
||
283 | 4099 |
magnifyRowFrom:srcBytes offset:srcStart pixels:oldPixels |
4100 |
into:dstBytes offset:dstStart factor:mX |
|
4101 |
||
4102 |
"magnify a single pixel row - can only magnify by integer factors, |
|
4103 |
can only magnify 1,2,4,8 and 24 bit-per-pixel images. But this is done fast." |
|
4104 |
||
4105 |
^ self subclassResponsibility |
|
2 | 4106 |
! ! |
0 | 4107 |
|
283 | 4108 |
!Image methodsFor:'queries'! |
4109 |
||
4110 |
averageColor |
|
4111 |
"return the average color of the image. |
|
4112 |
This usually only makes sense for textures and patterns |
|
4113 |
(i.e. to compute shadow & light colors for viewBackgrounds). |
|
4114 |
Notice, that for the above purpose, it is usually ok to process |
|
4115 |
a subImage - i.e. use Image>>averageColorIn: on a smaller rectangle" |
|
4116 |
||
4117 |
^ self averageColorIn:(0@0 corner:(width-1)@(height-1)) |
|
4118 |
! |
|
4119 |
||
4120 |
averageColorIn:aRectangle |
|
4121 |
"return the images average color in an area. |
|
4122 |
The implementation below is slow - so you may want to |
|
4123 |
create tuned versions for DepthXImage if you plan to do |
|
4124 |
heavy image processing ... |
|
4125 |
(also, creating tuned versions of the enumeration messages helps a lot)" |
|
4126 |
||
4127 |
|x0 "{ Class:SmallInteger }" |
|
4128 |
y0 "{ Class:SmallInteger }" |
|
4129 |
x1 "{ Class:SmallInteger }" |
|
4130 |
y1 "{ Class:SmallInteger }" |
|
4131 |
sumRed sumGreen sumBlue n| |
|
4132 |
||
4133 |
sumRed := sumGreen := sumBlue := 0. |
|
4134 |
y0 := aRectangle top. |
|
4135 |
y1 := aRectangle bottom. |
|
4136 |
x0 := aRectangle left. |
|
4137 |
x1 := aRectangle right. |
|
4138 |
||
4139 |
self colorsFromX:x0 y:y0 toX:x1 y:y1 do:[:x :y :colorAtXY | |
|
4140 |
sumRed := sumRed + colorAtXY red. |
|
4141 |
sumGreen := sumGreen + colorAtXY green. |
|
4142 |
sumBlue := sumBlue + colorAtXY blue. |
|
4143 |
]. |
|
4144 |
n := (x1 - x0 + 1) * (y1 - y0 + 1). |
|
4145 |
^ Color red:(sumRed / n) green:(sumGreen / n) blue:(sumBlue / n) |
|
4146 |
! |
|
4147 |
||
4148 |
bitsPerPixel |
|
4149 |
"return the number of bits per pixel" |
|
4150 |
||
4151 |
^ (bitsPerSample inject:0 into:[:sum :i | sum + i]) |
|
4152 |
! |
|
4153 |
||
4154 |
bitsPerRow |
|
4155 |
"return the number of bits in one scanline of the image" |
|
4156 |
||
4157 |
^ width * (self bitsPerPixel). |
|
4158 |
! |
|
4159 |
||
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4160 |
bounds |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4161 |
"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
|
4162 |
|
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4163 |
^ 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
|
4164 |
|
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4165 |
"Created: 13.5.1996 / 10:27:06 / cg" |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4166 |
! |
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4167 |
|
283 | 4168 |
brightness |
4169 |
"return the brightness of the image. |
|
4170 |
This usually only makes sense for textures and patterns |
|
4171 |
(i.e. to compute shadow & light colors for viewBackgrounds). |
|
4172 |
Notice, that for the above purpose, only a subimage is inspected here" |
|
4173 |
||
4174 |
^ (self averageColorIn:(0@0 corner:7@7)) brightness |
|
4175 |
! |
|
4176 |
||
4177 |
bytesPerRow |
|
4178 |
"return the number of bytes in one scanline of the image" |
|
4179 |
||
4180 |
|bitsPerRow bytesPerRow| |
|
4181 |
||
4182 |
bitsPerRow := width * (self bitsPerPixel). |
|
4183 |
bytesPerRow := bitsPerRow // 8. |
|
4184 |
((bitsPerRow \\ 8) ~~ 0) ifTrue:[ |
|
4185 |
bytesPerRow := bytesPerRow + 1 |
|
4186 |
]. |
|
4187 |
^ bytesPerRow |
|
4188 |
! |
|
4189 |
||
674 | 4190 |
heightOn:aGC |
4191 |
"return my height, if displayed on aGC; |
|
4192 |
since my height is independent of the device (the number of pixels), |
|
4193 |
return the pixel-height" |
|
4194 |
||
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4195 |
^ height |
674 | 4196 |
|
4197 |
"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
|
4198 |
"Modified: 13.5.1996 / 10:26:36 / cg" |
674 | 4199 |
! |
4200 |
||
283 | 4201 |
isImage |
4202 |
"return true, if the receiver is some kind of image; |
|
4203 |
true is returned here - the method is redefined from Object." |
|
4204 |
||
4205 |
^ true |
|
4206 |
! |
|
4207 |
||
4208 |
isImageOrForm |
|
4209 |
"return true, if the receiver is some kind of image or form; |
|
4210 |
true is returned here - the method is redefined from Object." |
|
4211 |
||
4212 |
^ true |
|
4213 |
! |
|
4214 |
||
4215 |
usedColors |
|
4216 |
"return a collection of colors used in the receiver." |
|
4217 |
||
4218 |
|usedValues max| |
|
4219 |
||
4220 |
usedValues := self usedValues asArray. |
|
4221 |
photometric ~~ #palette ifTrue:[ |
|
746 | 4222 |
max := (1 bitShift:self depth) - 1. |
4223 |
^ usedValues collect:[:val | (Color gray:(100 * val / max ))] |
|
283 | 4224 |
]. |
4225 |
||
4226 |
^ usedValues collect:[:val | (colorMap at:val+1)] |
|
4227 |
||
4228 |
" |
|
4229 |
(Image fromFile:'bitmaps/garfield.gif') usedColors |
|
4230 |
(Image fromFile:'bitmaps/SBrowser.xbm') usedColors |
|
4231 |
(Image fromFile:'ttt.tiff') usedColors |
|
4232 |
" |
|
746 | 4233 |
|
4234 |
"Modified: 28.5.1996 / 20:55:10 / cg" |
|
283 | 4235 |
! |
4236 |
||
4237 |
usedValues |
|
4238 |
"return a collection of color values used in the receiver. |
|
4239 |
Notice, that the interpretation of the pixels depends on the photometric |
|
4240 |
of the image. |
|
4241 |
This is a general and therefore slow implementation; subclasses |
|
4242 |
may want to redefine this method for more performance." |
|
4243 |
||
4244 |
|set| |
|
4245 |
||
4246 |
set := IdentitySet new. |
|
4247 |
self valuesFromX:0 y:0 toX:(self width-1) y:(self height-1) do:[:x :y :pixel | |
|
4248 |
set add:pixel |
|
4249 |
]. |
|
4250 |
^ set |
|
4251 |
||
4252 |
" |
|
4253 |
(Image fromFile:'bitmaps/garfield.gif') usedValues |
|
4254 |
(Image fromFile:'bitmaps/SBrowser.xbm') usedValues |
|
4255 |
(Image fromFile:'ttt.tiff') usedValues |
|
4256 |
" |
|
674 | 4257 |
! |
4258 |
||
4259 |
widthOn:aGC |
|
4260 |
"return my width, if displayed on aGC; |
|
4261 |
since my width is independent of the device (the number of pixels), |
|
4262 |
return the pixel-width" |
|
4263 |
||
680
a905d02dcb82
added #bounds (for VisualComponent protocol compatibility)
Claus Gittinger <cg@exept.de>
parents:
674
diff
changeset
|
4264 |
^ width |
674 | 4265 |
|
4266 |
"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
|
4267 |
"Modified: 13.5.1996 / 10:26:42 / cg" |
283 | 4268 |
! ! |
4269 |
||
4270 |
!Image methodsFor:'saving on file'! |
|
4271 |
||
4272 |
saveOn:aFileName |
|
4273 |
"save the image in a aFileName. The suffix of the filename |
|
4274 |
controls the format. Currently, not all formats may be supported |
|
4275 |
(see ImageReader subclasses implementing save:onFile:)" |
|
4276 |
||
4277 |
" |
|
4278 |
from the extension, get the imageReader class |
|
4279 |
(which should know how to write images as well) |
|
4280 |
" |
|
4281 |
FileFormats associationsDo:[:a | |
|
4282 |
(aFileName endsWith:(a key)) ifTrue:[ |
|
4283 |
^ (a value) save:self onFile:aFileName |
|
4284 |
] |
|
4285 |
]. |
|
4286 |
||
4287 |
" |
|
4288 |
no known extension - could ask user for the format here. |
|
4289 |
currently default to tiff format. |
|
4290 |
" |
|
4291 |
'IMAGE: unknown extension - cannot figure out format - using tiff' errorPrintNL. |
|
4292 |
^ self saveOn:aFileName using:TIFFReader |
|
89 | 4293 |
! |
4294 |
||
283 | 4295 |
saveOn:aFileName using:readerClass |
4296 |
"save the receiver using the representation class" |
|
4297 |
||
4298 |
readerClass save:self onFile:aFileName |
|
4299 |
||
4300 |
" |
|
4301 |
anImage saveOn:'myImage' using:TIFFReader |
|
4302 |
anImage saveOn:'myImage' using:XBMReader |
|
4303 |
" |
|
0 | 4304 |
! ! |
283 | 4305 |
|
4306 |
!Image methodsFor:'screen capture'! |
|
4307 |
||
4308 |
fromScreen:aRectangle |
|
4309 |
"read an image from the display screen" |
|
4310 |
||
4311 |
^ self fromScreen:aRectangle on:Screen current |
|
4312 |
! |
|
4313 |
||
4314 |
fromScreen:aRectangle on:aDevice |
|
4315 |
"read an image from aDevices display screen. |
|
4316 |
Since I have no other displays, only the MonoChrome, StaticGrey |
|
4317 |
and PseudoColor cases have been tested ... |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4318 |
(especially True- and DirectColor may be wrong). |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4319 |
Late note: 24bit rgb now also works." |
283 | 4320 |
|
4321 |
|depth visType |
|
4322 |
x "{ Class: SmallInteger }" |
|
4323 |
y "{ Class: SmallInteger }" |
|
4324 |
w "{ Class: SmallInteger }" |
|
4325 |
h "{ Class: SmallInteger }" |
|
4326 |
dstIndex "{ Class: SmallInteger }" |
|
4327 |
srcIndex "{ Class: SmallInteger }" |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4328 |
srcRow "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4329 |
dstRow "{ Class: SmallInteger }" |
283 | 4330 |
inData tmpData usedPixels mapSize |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4331 |
map bitsPerPixel bytesPerLine byteOrder spaceBitsPerPixel |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4332 |
info bitsPerPixelIn bytesPerLineIn curs cid rootView |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4333 |
maskR "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4334 |
maskG "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4335 |
maskB "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4336 |
shR "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4337 |
shG "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4338 |
shB "{ Class: SmallInteger }" |
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4339 |
shR2 "{ Class: SmallInteger }" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4340 |
shG2 "{ Class: SmallInteger }" |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4341 |
shB2 "{ Class: SmallInteger }" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4342 |
r "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4343 |
g "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4344 |
b "{ Class: SmallInteger }" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4345 |
word| |
283 | 4346 |
|
4347 |
curs := Cursor sourceForm:(Form fromFile:'Camera.xbm') |
|
518 | 4348 |
maskForm:(Form fromFile:'Camera_m.xbm') |
4349 |
hotSpot:16@16. |
|
283 | 4350 |
curs notNil ifTrue:[ |
518 | 4351 |
cid := (curs on:aDevice) id |
283 | 4352 |
]. |
4353 |
||
4354 |
" |
|
4355 |
get some attributes of the display device |
|
4356 |
" |
|
4357 |
visType := aDevice visualType. |
|
4358 |
depth := aDevice depth. |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4359 |
|
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4360 |
"/ kludge for 15bit XFree server |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4361 |
depth == 15 ifTrue:[ |
518 | 4362 |
depth := 16 |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4363 |
]. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4364 |
|
283 | 4365 |
(visType == #StaticGray) ifTrue:[ |
518 | 4366 |
(aDevice blackpixel == 0) ifTrue:[ |
4367 |
photometric := #blackIs0 |
|
4368 |
] ifFalse:[ |
|
4369 |
photometric := #whiteIs0 |
|
4370 |
]. |
|
4371 |
samplesPerPixel := 1. |
|
4372 |
bitsPerPixel := depth. |
|
4373 |
bitsPerSample := Array with:bitsPerPixel. |
|
283 | 4374 |
] ifFalse:[ |
518 | 4375 |
((visType == #PseudoColor) or:[(visType == #StaticColor) or:[visType == #GrayScale]]) ifTrue:[ |
4376 |
photometric := #palette. |
|
4377 |
samplesPerPixel := 1. |
|
4378 |
bitsPerPixel := depth. |
|
4379 |
bitsPerSample := Array with:bitsPerPixel. |
|
4380 |
] ifFalse:[ |
|
4381 |
((visType == #TrueColor) or:[visType == #DirectColor]) ifTrue:[ |
|
4382 |
photometric := #rgb. |
|
4383 |
samplesPerPixel := 3. |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4384 |
"/ bitsPerPixel := depth. |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4385 |
"/ 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
|
4386 |
"/ with:aDevice bitsGreen |
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4387 |
"/ with:aDevice bitsBlue |
518 | 4388 |
bitsPerPixel := 24. |
4389 |
bitsPerSample := #(8 8 8). |
|
4390 |
] ifFalse:[ |
|
4391 |
self error:'screen visual not supported'. |
|
4392 |
^ nil |
|
4393 |
] |
|
4394 |
] |
|
283 | 4395 |
]. |
4396 |
||
4397 |
" |
|
4398 |
dont know yet, how the display pads; assume worst case, |
|
4399 |
offering enough space for 32 bit padding |
|
4400 |
" |
|
4401 |
w := width := aRectangle width. |
|
4402 |
h := height := aRectangle height. |
|
4403 |
x := aRectangle left. |
|
4404 |
y := aRectangle top. |
|
4405 |
||
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4406 |
" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4407 |
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
|
4408 |
(its too late when info is present ...) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4409 |
" |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4410 |
spaceBitsPerPixel := bitsPerPixel. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4411 |
(bitsPerPixel > 8) ifTrue:[ |
518 | 4412 |
spaceBitsPerPixel := 16. |
4413 |
(bitsPerPixel > 16) ifTrue:[ |
|
4414 |
spaceBitsPerPixel := 32. |
|
4415 |
(bitsPerPixel > 32) ifTrue:[ |
|
4416 |
spaceBitsPerPixel := bitsPerPixel. |
|
4417 |
] |
|
4418 |
] |
|
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4419 |
]. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4420 |
|
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4421 |
bytesPerLine := (w * spaceBitsPerPixel + 31) // 32 * 4. |
283 | 4422 |
inData := ByteArray uninitializedNew:(bytesPerLine * height). |
4423 |
||
4424 |
" |
|
4425 |
actually have to grabServer ... but thats not yet available |
|
4426 |
" |
|
728 | 4427 |
rootView := aDevice rootView. |
283 | 4428 |
aDevice setActivePointerGrab:rootView. |
4429 |
aDevice grabPointerIn:rootView id |
|
518 | 4430 |
withCursor:cid pointerMode:#async keyboardMode:#sync confineTo:nil. |
283 | 4431 |
|
4432 |
" |
|
4433 |
get the pixels |
|
4434 |
" |
|
4435 |
info := aDevice getBitsFrom:rootView id x:x y:y width:w height:h into:inData. |
|
4436 |
||
4437 |
" |
|
396
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
4438 |
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
|
4439 |
" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4440 |
byteOrder := info at:#byteOrder. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4441 |
bitsPerPixelIn := info at:#bitsPerPixel. |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4442 |
byteOrder ~~ #msbFirst ifTrue:[ |
518 | 4443 |
bitsPerPixelIn == 16 ifTrue:[ |
4444 |
"/ must swap bytes |
|
4445 |
inData swapBytes |
|
4446 |
] ifFalse:[ |
|
4447 |
bitsPerPixelIn == 32 ifTrue:[ |
|
4448 |
"/ must swap longs |
|
4449 |
inData swapLongs |
|
4450 |
] |
|
4451 |
] |
|
396
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
4452 |
]. |
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
4453 |
|
d088e672be8a
handle screen image grabbing with depth 16/24 and different byteOrder
Claus Gittinger <cg@exept.de>
parents:
394
diff
changeset
|
4454 |
" |
283 | 4455 |
check, if the devices padding is different .. |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4456 |
or if the bitsPerPixels are different |
283 | 4457 |
" |
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4458 |
bytesPerLineIn := (info at:#bytesPerLine). "what I got" |
283 | 4459 |
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
|
4460 |
|
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4461 |
((bytesPerLine ~~ bytesPerLineIn) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4462 |
or:[bitsPerPixelIn ~~ bitsPerPixel]) ifTrue:[ |
518 | 4463 |
tmpData := inData. |
4464 |
inData := ByteArray uninitializedNew:(bytesPerLine * height). |
|
4465 |
||
4466 |
srcRow := 1. |
|
4467 |
dstRow := 1. |
|
4468 |
||
4469 |
bitsPerPixelIn ~~ bitsPerPixel ifTrue:[ |
|
4470 |
"/ for now, only 32 -> 24 is supported |
|
4471 |
||
4472 |
maskR := (1 bitShift:aDevice bitsRed) - 1. |
|
4473 |
maskG := (1 bitShift:aDevice bitsGreen) - 1. |
|
4474 |
maskB := (1 bitShift:aDevice bitsBlue) - 1. |
|
4475 |
shR := aDevice shiftRed negated. |
|
4476 |
shG := aDevice shiftGreen negated. |
|
4477 |
shB := aDevice shiftBlue negated. |
|
4478 |
shR2 := (8 - aDevice bitsRed). |
|
4479 |
shG2 := (8 - aDevice bitsGreen). |
|
4480 |
shB2 := (8 - aDevice bitsBlue). |
|
4481 |
||
4482 |
((bitsPerPixelIn == 32) and:[bitsPerPixel == 24]) ifTrue:[ |
|
4483 |
"/ 'reformatting 32->24...' printNL. |
|
4484 |
1 to:h do:[:hi | |
|
4485 |
srcIndex := srcRow. |
|
4486 |
dstIndex := dstRow. |
|
4487 |
||
4488 |
1 to:w do:[:wi | |
|
4489 |
word := tmpData doubleWordAt:srcIndex MSB:true. |
|
4490 |
r := (word bitShift:shR) bitAnd:maskR. |
|
4491 |
g := (word bitShift:shG) bitAnd:maskG. |
|
4492 |
b := (word bitShift:shB) bitAnd:maskB. |
|
4493 |
||
4494 |
inData at:dstIndex put:r. |
|
4495 |
inData at:dstIndex+1 put:g. |
|
4496 |
inData at:dstIndex+2 put:b. |
|
4497 |
srcIndex := srcIndex + 4. |
|
4498 |
dstIndex := dstIndex + 3. |
|
4499 |
]. |
|
4500 |
dstRow := dstRow + bytesPerLine. |
|
4501 |
srcRow := srcRow + bytesPerLineIn |
|
4502 |
] |
|
4503 |
] ifFalse:[ |
|
4504 |
((bitsPerPixelIn == 16) and:[bitsPerPixel == 24]) ifTrue:[ |
|
4505 |
"/ 'reformatting 16->24...' printNL. |
|
4506 |
1 to:h do:[:hi | |
|
4507 |
srcIndex := srcRow. |
|
4508 |
dstIndex := dstRow. |
|
4509 |
||
4510 |
1 to:w do:[:wi | |
|
4511 |
word := tmpData wordAt:srcIndex MSB:true. |
|
4512 |
r := (word bitShift:shR) bitAnd:maskR. |
|
4513 |
g := (word bitShift:shG) bitAnd:maskG. |
|
4514 |
b := (word bitShift:shB) bitAnd:maskB. |
|
4515 |
||
4516 |
inData at:dstIndex put:(r bitShift:shR2). |
|
4517 |
inData at:dstIndex+1 put:(g bitShift:shG2). |
|
4518 |
inData at:dstIndex+2 put:(b bitShift:shB2). |
|
4519 |
srcIndex := srcIndex + 2. |
|
4520 |
dstIndex := dstIndex + 3. |
|
4521 |
]. |
|
4522 |
dstRow := dstRow + bytesPerLine. |
|
4523 |
srcRow := srcRow + bytesPerLineIn |
|
4524 |
] |
|
4525 |
] ifFalse:[ |
|
4526 |
('IMAGE: unsupported depth combination: ' , bitsPerPixelIn printString , ' -> ' , |
|
4527 |
bitsPerPixel printString) errorPrintNL. |
|
4528 |
^ nil |
|
4529 |
] |
|
4530 |
]. |
|
4531 |
] ifFalse:[ |
|
4532 |
" |
|
4533 |
repad in the buffer |
|
4534 |
" |
|
4535 |
1 to:h do:[:hi | |
|
4536 |
inData replaceFrom:dstRow to:(dstRow + bytesPerLine - 1) |
|
4537 |
with:tmpData startingAt:srcRow. |
|
4538 |
dstRow := dstRow + bytesPerLine. |
|
4539 |
srcRow := srcRow + bytesPerLineIn |
|
4540 |
] |
|
4541 |
] |
|
283 | 4542 |
] ifFalse:[ |
518 | 4543 |
(bytesPerLine * height) ~~ inData size ifTrue:[ |
4544 |
tmpData := inData. |
|
4545 |
inData := ByteArray uninitializedNew:(bytesPerLine * height). |
|
4546 |
inData replaceFrom:1 to:bytesPerLine * height with:tmpData startingAt:1 |
|
4547 |
] |
|
283 | 4548 |
]. |
4549 |
bytes := inData. |
|
4550 |
||
4551 |
"info printNL." |
|
4552 |
||
399
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4553 |
((visType == #StaticGray) |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4554 |
or:[visType == #TrueColor |
0b6e43843204
handle 24 truecolor displays in fromScreen
Claus Gittinger <cg@exept.de>
parents:
396
diff
changeset
|
4555 |
or:[visType == #DirectColor]]) ifTrue:[ |
518 | 4556 |
" |
4557 |
were done, the pixel values are the rgb/grey values |
|
4558 |
" |
|
283 | 4559 |
] ifFalse:[ |
518 | 4560 |
" |
4561 |
what we have now are the color numbers - still need the r/g/b values. |
|
4562 |
find out, which colors are in the picture |
|
4563 |
" |
|
4564 |
usedPixels := inData usedValues. |
|
4565 |
mapSize := usedPixels max + 1. |
|
4566 |
||
4567 |
"get the palette" |
|
4568 |
map := Array new:mapSize. |
|
4569 |
usedPixels do:[:colorIndex | |
|
4570 |
|i| |
|
4571 |
||
4572 |
i := colorIndex + 1. |
|
4573 |
aDevice getRGBFrom:colorIndex into:[:r :g :b | |
|
4574 |
map at:i put:(Color red:r green:g blue:b) |
|
4575 |
] |
|
4576 |
]. |
|
4577 |
colorMap := map. |
|
283 | 4578 |
]. |
4579 |
||
4580 |
aDevice ungrabPointer. |
|
4581 |
||
4582 |
" |
|
4583 |
(Image new) fromScreen:((0 @ 0) corner:(100 @ 100)) on:Display |
|
4584 |
(Image new) fromScreen:((0 @ 0) corner:(500 @ 500)) on:Display |
|
4585 |
" |
|
401
f163a93a0987
grab image as 24bit rgb on 15/16bit trueColor servers
Claus Gittinger <cg@exept.de>
parents:
399
diff
changeset
|
4586 |
|
518 | 4587 |
"Modified: 7.3.1996 / 19:17:33 / cg" |
283 | 4588 |
! ! |
4589 |
||
4590 |
!Image class methodsFor:'documentation'! |
|
4591 |
||
4592 |
version |
|
798 | 4593 |
^ '$Header: /cvs/stx/stx/libview/Image.st,v 1.81 1996-06-07 17:30:06 cg Exp $' |
650 | 4594 |
! ! |
283 | 4595 |
Image initialize! |