The Trunk: Graphics-ul.133.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Graphics-ul.133.mcz

commits-2
Levente Uzonyi uploaded a new version of Graphics to project The Trunk:
http://source.squeak.org/trunk/Graphics-ul.133.mcz

==================== Summary ====================

Name: Graphics-ul.133
Author: ul
Time: 28 April 2010, 11:26:50.367 am
UUID: d81b8f98-7b9a-c744-a652-229157d9445d
Ancestors: Graphics-laza.132

- integrated the png speedup changes and transparency fixes (by Juan)
- also kept Andreas' and Yoshiki's version in a comment at PNGReadWriter >> #copyPixelsGray:

=============== Diff against Graphics-laza.132 ===============

Item was changed:
  ----- Method: PNGReadWriter>>processTransparencyChunk (in category 'chunks') -----
  processTransparencyChunk
 
  | red green blue |
 
  "Transcript show: '  TRANSPARENCY ',chunk printString."
  colorType = 0 ifTrue: [
  transparentPixelValue := chunk unsignedShortAt: 1 bigEndian: true.
+ palette at: transparentPixelValue put: Color transparent.
  ^self
  ].
  colorType = 2 ifTrue: [
  red := chunk at: 2.
  green := chunk at: 2.
  blue := chunk at: 2.
  transparentPixelValue := 16rFF00 + red << 8 + green << 8 + blue.
  ^self
  ].
  colorType = 3 ifTrue: [
  chunk withIndexDo: [ :alpha :index |
  palette at: index put: ((palette at: index) alpha: alpha/255)
  ].
  ^self
  ].
  !

Item was changed:
  ----- Method: PNGReadWriter>>copyPixelsGray: (in category 'pixel copies') -----
  copyPixelsGray: y
  "Handle non-interlaced grayscale color mode (colorType = 0)"
+
+ | base bits |
+ bitsPerChannel = 16 ifTrue: [
+ "Warning: This is extremely slow. Besides we are downsampling to 8 bits!!"
+ | blitter |
+ blitter := BitBlt current bitPokerToForm: form.
+ 0 to: width - 1 do: [ :x |
+ blitter pixelAt: x @ y put: 255 - (thisScanline at: x * 2 + 1) ].
+ ^self ].
+
+ "Just copy the bits"
+
+ "This Smalltalk version might be easier to understand than the others below."
+ base := y * form width * bitsPerChannel // 32 + 1.
+ bits := form bits.
+ 0 to: thisScanline size - 1 // 4 do: [ :i |
+ | ii word |
+ ii := i * 4.
+ "This somewhat weird mixture of (#* and #+) with (#bitShift: and #bitOr:)
+ is to make use of faster arithmetic bytecodes, but not of slow largeintegers."
+ word :=
+ (((thisScanline at: ii + 1) * 256 +
+ (thisScanline at: ii + 2) * 256 +
+ (thisScanline at: ii + 3)) bitShift: 8) bitOr:
+ (thisScanline at: ii + 4).
+ bits at: base + i put: word ].
+
+ "This interesting technique (By Andreas Raab) is faster for very large images, but might be slower for small ones"
+ "^self copyPixelsGrayWeirdBitBltHack: y ".
+ "It uses the following method:
+ PNGReadWriter >> copyPixelsGrayWeirdBitBltHack: y
+ ""Handle non-interlaced black and white color mode (colorType = 0)
+ By Andreas Raab""
+
+ | source dest cmap |
+ source := Form extent: 1 @ (thisScanline size // 4) depth: 32 bits: thisScanline.
+ dest := Form extent: 1 @ (form bits size) depth: 32 bits: form bits.
+ cmap := Smalltalk isLittleEndian
+ ifTrue:[ColorMap
+ shifts: #(-24 -8 8 24)
+ masks: #(16rFF000000 16r00FF0000 16r0000FF00 16r000000FF)].
+ (BitBlt toForm: dest)
+ sourceForm: source;
+ destX: 0 destY: (y * form width*bitsPerChannel//32) width: 1 height: (form width+31*bitsPerChannel//32);
+ colorMap: cmap;
+ combinationRule: 3;
+ copyBits."
+
+ "This interesting technique  (By Yoshiki Ohshima) is faster for very large images, but might be slower for small ones"
+ "form bits copyFromByteArray2: thisScanline to: y * (form width* bitsPerChannel // 32)".
+ "It uses the following method:
+ BitMap >> copyFromByteArray2: byteArray to: i
+ ""This method should work with either byte orderings""
+
+ | myHack byteHack |
+ myHack := Form new hackBits: self.
+ byteHack := Form new hackBits: byteArray.
+ Smalltalk  isLittleEndian ifTrue: [byteHack swapEndianness].
+ byteHack displayOn: myHack at:  0@i"!
- | blitter pixPerByte mask shifts pixelNumber rawByte pixel transparentIndex |
- blitter := BitBlt current bitPokerToForm: form.
- transparentIndex := form colors size.
- bitsPerChannel = 16
- ifTrue: [0
- to: width - 1
- do: [:x | blitter pixelAt: x @ y put: 255
- - (thisScanline at: x << 1 + 1)].
- ^ self]
- ifFalse: [bitsPerChannel = 8
- ifTrue: [1
- to: width
- do: [:x | blitter
- pixelAt: x - 1 @ y
- put: (thisScanline at: x)].
- ^ self].
- bitsPerChannel = 1
- ifTrue: [pixPerByte := 8.
- mask := 1.
- shifts := #(7 6 5 4 3 2 1 0 )].
- bitsPerChannel = 2
- ifTrue: [pixPerByte := 4.
- mask := 3.
- shifts := #(6 4 2 0 )].
- bitsPerChannel = 4
- ifTrue: [pixPerByte := 2.
- mask := 15.
- shifts := #(4 0 )].
- pixelNumber := 0.
- 0 to: width - 1 do: [:x |
- rawByte := thisScanline at: pixelNumber // pixPerByte + 1.
- pixel := rawByte
- >> (shifts at: pixelNumber \\ pixPerByte + 1) bitAnd: mask.
- pixel = transparentPixelValue ifTrue: [pixel := transparentIndex].
- blitter pixelAt: x @ y put: pixel.
- pixelNumber := pixelNumber + 1
- ]
- ]!

Item was changed:
  ----- Method: PNGReadWriter>>grayColorsFor: (in category 'miscellaneous') -----
  grayColorsFor: d
  "return a color table for a gray image"
 
  palette := Array new: 1<<d.
  d = 1 ifTrue: [
  palette at: 1 put: Color black.
  palette at: 2 put: Color white.
+ ^  palette
- ^ palette,{Color transparent}
  ].
  d = 2 ifTrue: [
  palette at: 1 put: Color black.
  palette at: 2 put: (Color gray: 85.0 / 255.0).
  palette at: 3 put: (Color gray: 170.0 / 255.0).
  palette at: 4 put: Color white.
+ ^ palette
- ^ palette,{Color transparent}.
  ].
  d = 4 ifTrue: [
  0 to: 15 do: [ :g |
  palette at: g+1 put: (Color gray: (g/15) asFloat) ].
+ ^ palette
- ^ palette,{Color transparent}
  ].
  d = 8 ifTrue: [
  0 to: 255 do: [ :g |
  palette at: g+1 put: (Color gray: (g/255) asFloat) ].
+ ^ palette
- ^ palette "??transparent??"
  ].
  !