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