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