Form manipulation from a Plugin

Andreas Raab andreas.raab at gmx.de
Mon Apr 7 14:08:27 UTC 2003


Diego,

There are actually various problems in the code you're quoting. A safe way
to deal with this is along the following lines:

	| formOop bitsOop bitsPtr |
	self var: #bitsPtr type: 'unsigned int*'.

	"... grab other stuff before..."
	bitsOop := interpreterProxy fetchPointer: 0 ofObject: formOop.

	"first test if bitsOop is really what we're expecting"
	((interpreterProxy isInteger: bitsOop) or:[
		(interpreterProxy isWords: bitsOop) not])
			ifTrue:[^interpreterProxy primitiveFail].

[note: those tests above *MUST* be done as it is quite possible that forms
contain either integers (surface references) or byte arrays (hibernated).
Not testing for this will hose your system].

	"now test if the length of the bitmap is right"
	(interpreterProxy slotSizeOf: bitsOop) = expectedSize
		ifFalse:[^interpreterProxy primitiveFail]

[again, this test is needed if for some reason the format of the form has
been screwed up. the expectedSize here is whatever the number of words for a
form of that size/depth is expected to be; e.g., for a 32bpp form just
width*height]

	"now grab the pointer to the bits"
	bitsPtr := interpreterProxy firstIndexableField: bitsOop.

[At this point you have a C 'unsigned int *' pointer to those bits. If you
wanted to work on a byte pointer instead you could just change the
declaration to be 'unsigned char *' but watch the indexing!]

	0 to: expectedSize-1 do:[:i| "for all words..."
		pixel := bitsPtr at: i.
		... etc ...

You should be pretty safe doing it the above way as it will ensure all of
the invariants are checked.

Cheers,
  - Andreas

> -----Original Message-----
> From: squeak-dev-bounces at lists.squeakfoundation.org 
> [mailto:squeak-dev-bounces at lists.squeakfoundation.org] On 
> Behalf Of diegogomezdeck at consultar.com
> Sent: Monday, April 07, 2003 3:58 PM
> To: squeak-dev at lists.squeakfoundation.org
> Subject: Form manipulation from a Plugin
> 
> 
> Hi list...
> 
> I'm processing a 32 bits Form in a slang-plugin.
> 
> The method receive the Form as a parameter:
> 
>    formOop := interpreterProxy stackObjectValue: 1.
> 
> Then I ask for widht, height and a pointer to bits:
> 
>    bits := interpreterProxy fetchPointer: 0 ofObject: formOop.
>    width := self fetchIntOrFloat: 1 ofObject: formOop.
>    height := self fetchIntOrFloat: 2 ofObject: formOop.
> 
> Then I iterate over the bits:
> 
>    bits
>       to: bits + (height - 1 * (width * 4))
>       by: 4
>       do: [:pos |
>          pixel := interpreterProxy longAt: pos.
>          alpha := (pixel bitAnd: 16rFF000000) >> 24.
>          <some other stuff>
>       ].
> 
> The problem I have is the following: The alpha part of pixel 
> is always 0.
> (I don't need to say that the form has not transparent colors)
> 
> Any idea of my problem?
> 
> 
> TIA,
> 
> Diego Gomez Deck
> 
> 
> 
> 
> 



More information about the Squeak-dev mailing list