Possible fix for Form>>bmpColorsFrom:count:depth:

Kevin Fisher kfisher at rim.net
Tue Feb 2 18:19:12 UTC 1999


Ok I went ahead and tried to fix the problem I was having yesterday
with the BMPFile reader in class Form.  The problem that I encountered
was that if a colour  map was read that contained less than the total number
of colours possible (specified by 2^depth), then the remainder of the
colour map was set to 'nil' as opposed to some valid colour value.
This caused problems with other classes that index the colourmap up
to the total size of the map as opposed to the total number of colours
that are actually in the map (confused yet? :).

If you want to see the bug in action just try creating a ThreePhaseButtonMorph
using an 8-bit bitmap using less than 256 colours!  With my fix there is
no problem, the bitmap gets displayed okay.

Anyway, here's my fix...I just set any uninitialized entries in the colourmap
to 'black'.  If there's a more correct way to do this, please let me know!


'From Squeak 2.3 of January 14, 1999 on 2 February 1999 at 1:37:04 pm'!

!Form class methodsFor: 'BMP file reading' stamp: 'kgf 2/1/1999 16:04'!
bmpColorsFrom: aBinaryStream count: colorCount depth: depth
	"Read colorCount BMP color map entries from the given binary stream. Answer an array of Colors."

	| maxLevel colors b g r |
	colorCount = 0 ifTrue: [  "this BMP file does not have a color map"
		"default monochrome color map"
		depth = 1 ifTrue: [^ Array with: Color white with: Color black].
		"default gray-scale color map"
		maxLevel _ (2 raisedTo: depth) - 1.
		^ (0 to: maxLevel) collect: [:level | Color gray: (level asFloat / maxLevel)]].
	
	
	colors _ Array new: (2 raisedTo: depth).
	1 to: colorCount do: [:i |
		b _ aBinaryStream next.
		g _ aBinaryStream next.
		r _ aBinaryStream next.
		aBinaryStream skip: 1.
		colors at: i put: (Color r: r g: g b: b range: 255)].
	colorCount to: (colors size) do: [:i |
		colors at: i put: (Color r: 0 g: 0 b: 0 range: 255)].
	^ colors
! !





More information about the Squeak-dev mailing list