Draw bitmaps pixel by pixel

Michiel Spape Michiel.Spape at nottingham.ac.uk
Thu Jun 17 10:35:46 UTC 2010


Hi Tobias,
I'm sorry, but if you want to accomplish advanced things in programming, you'll often have to abandon your previous - perfectly okay - code. As said, I'm quite sure you might achieve writing a BMP file to disk, of a random mask, within E-Prime, then later using that, but I'm unsure of the specifics regarding BMPs. I do find it unlikely you'll end up writing fewer code than my suggestion. Anyway, having a little bit more time, I found a few bugs in my code and here's a step-by-step implementation guide. 
1. Start with A NEW EXPERIMENT. After finishing all steps, you should be able to integrate the code within your existing experiment, but do that afterwards - for now, start from a clean slate.
2. Enter a text display at the beginning of the (only) procedure, make it infinitely long in duration and add keyboard response.
3. Enter one inline object after this text display.
4. Go to the user script (alt+5, user tab) and write the following:

Dim cnvsmask as Canvas
Dim cnvs as Canvas
Sub buildMask (masksize as integer, nCircles as integer, circlesize as integer)
	dim functiondur as long	
	functiondur = clock.read
	dim cCircle as integer
	cnvsmask.FillColor = CColor("black")
	dim onetothree as integer
	dim ppoint as point
	for cCircle = 1 to nCircles
		onetothree = random (1,3) 'Apologies to David for writing a really ugly if-statement!
		if onetothree = 1 then cnvsmask.FillColor = CColor("black")	
		if onetothree = 2 then cnvsmask.FillColor = CColor("white")	
		if onetothree = 3 then cnvsmask.FillColor = CColor("gray")	
		cnvsmask.Circle random (display.xres / 2 - masksize / 2, display.xres / 2 + masksize / 2), random (display.yres / 2 - masksize / 2, display.yres / 2 + masksize / 2), circlesize / 2
	next cCircle
 	debug.print "Mask took " & (clock.read - functiondur) & " ms to build"
End Sub

Sub drawmask (masksize as integer)
	dim midrect as rect
	midRect.Left = display.xres / 2 - masksize/2
	midRect.Right = display.xres / 2 + masksize/2
	midRect.Top = display.yres / 2 - masksize/2
	midRect.Bottom = display.yres / 2 + masksize /2
	cnvs.copy cnvsmask, midrect, midrect
End Sub

5. Go to the one inline you inserted after the textdisplay and write the following:

Set cnvs = Display.Canvas 'sets canvas to active canvas (i.e. what you write to it will be shown on the screen)
Set cnvsmask = Display.CreateCanvas()  'sets the canvas mask as a secondary canvas (i.e. what you write to it will only be shown to the screen after copying it to cnvs)
buildMask 100, 50, 10  'builds a mask of size 100 with 50 circles of size 10. Try 1000 circles as well and see what it does to your timing!
Display.waitforverticalblank 'to avoid syncing problems 
Drawmask 100 'draws the mask of size 100 
sleep 1000 'keeps the mask 1000 ms on the screen

6. Also, read my comments above. I've just tested all this and works fine.

... finally, once you get all the above, getting the above to run within your programme will merely make your line:
if c.getattrib("TargPos") = (i-1) then CueDisplay_SlideImage.Filename = "mask.bmp"
into
if c.getattrib("TargPos") = (i-1) then 
	Display.waitforverticalblank
	drawmask 100
end if

PROVIDED the mask is already built, of course.
Cheers,
Mich

Michiel Spapé
Research Fellow
Perception & Action group
University of Nottingham
School of Psychology

-----Original Message-----
From: e-prime at googlegroups.com [mailto:e-prime at googlegroups.com] On Behalf Of Tobias
Sent: 17 June 2010 10:50
To: E-Prime
Subject: Re: Draw bitmaps pixel by pixel

Thanks a lot for your detailed help. I am wondering how to implement
that code in my program. For now, I have a line calling for a certain
mask (which is always the same):

if c.getattrib("TargPos") = (i-1) then CueDisplay_SlideImage.Filename
= "mask.bmp"

So instead of this mask.bmp I would have need of the randomly
generated bitmap. I am not quite sure how to implement your code at
this place.

Thanks a lot in advance,
Tobias

On Jun 16, 6:36 pm, Michiel Spape <Michiel.Sp... at nottingham.ac.uk>
wrote:
> Hi,
> Drawing things pixel by pixel is quite easily implemented, given you know enough about programming (I seem to remember you do) - drawing bmp's pixel for pixel is more difficult, I expect (since A: you'd have to randomise pixels B: fill a file with it, then C: draw the file, whereas just doing the whole randomising in canvas saves step B and C). I think I even have something:
>
> Dim canvasmask as Mask
> Dim cnvs as Canvas
>
> Sub buildMask (masksize as integer, nCircles as integer, circlesize as integer)
>         dim functiondur as long
>         functiondur = clock.read
>         dim cCircle as integer
>         dim onetothree as integer
>         dim ppoint as point
>         for cCircle = 1 to nCircles
>                 onetothree = random (1,3)
>                 if onetothree = 1 then cnvsmask.FillColor = CColor("black")  
>                 if onetothree = 2 then cnvsmask.FillColor = CColor("white")  
>                 if onetothree = 3 then cnvsmask.FillColor = CColor("gray")    
>                 cnvsmask.Circle random (display.xres / 2 - masksize / 2, display.xres / 2 + masksize / 2), random (display.yres / 2 - masksize / 2, display.yres / 2 + masksize / 2), circlesize / 2
>         next cCircle
>         debug.print "Mask took " & (clock.read - functiondur) & " ms to build"
> End Sub
>
> Sub drawmask (masksize as integer)
>         dim midrect as rect
>         midRect.Left = display.xres / 2 - masksize/2
>         midRect.Right = display.xres / 2 + masksize/2
>         midRect.Top = display.yres / 2 - masksize/2
>         midRect.Bottom = display.yres / 2 + masksize /2
>         cnvs.copy cnvsmask, midrect, midrect
> End Sub
>
> ... I think that should work. Anyway, dump the whole thing into your userscript, then use the following in an inline to A: build the mask (might take some time, hence the debugging script), B: show the mask (it's useful to do this separately for timing issues)
>
> 'probably do this at the beginning of the trial
> buildMask 100, 50, 10 'draws 50 circles of size 10 in a mask of size 100 (all pixels)
>
> 'do this when you want to show stuff
> Display.waitforverticalblank 'for sync issues, not sure about exact syntax.
> Drawmask 100
>
> ...of course, my code here was mainly written (and subsequently abolished) for shape-based (circle) masking. More effective code can be written if you just want to do pixel-by-pixel colouring. I imagine you get the picture.
>
> Cheers,
> Mich
>
> Michiel Spapé
> Research Fellow
> Perception & Action group
> University of Nottingham
> School of Psychology
>
> -----Original Message-----
> From: e-prime at googlegroups.com [mailto:e-prime at googlegroups.com] On Behalf Of Tobias
> Sent: 16 June 2010 12:15
> To: E-Prime
> Subject: Draw bitmaps pixel by pixel
>
> Hej together,
>
> I want to program a random mask for a visual attention experiment. The
> idea is, that the mask should be randomly created each trial because
> there are some hints that subjects "learn" to ignore a mask if it is
> repeatedly used. So what I think of is a white noise or something. Is
> there a way in E-Prime to create bitmaps pixel by pixel with an
> inline?
>
> Cheers,
> Tobias
>
> --
> You received this message because you are subscribed to the Google Groups "E-Prime" group.
> To post to this group, send email to e-prime at googlegroups.com.
> To unsubscribe from this group, send email to e-prime+unsubscribe at googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/e-prime?hl=en.
>
> This message has been checked for viruses but the contents of an attachment
> may still contain software viruses which could damage your computer system:
> you are advised to perform your own checks. Email communications with the
> University of Nottingham may be monitored as permitted by UK legislation.

-- 
You received this message because you are subscribed to the Google Groups "E-Prime" group.
To post to this group, send email to e-prime at googlegroups.com.
To unsubscribe from this group, send email to e-prime+unsubscribe at googlegroups.com.
For more options, visit this group at http://groups.google.com/group/e-prime?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "E-Prime" group.
To post to this group, send email to e-prime at googlegroups.com.
To unsubscribe from this group, send email to e-prime+unsubscribe at googlegroups.com.
For more options, visit this group at http://groups.google.com/group/e-prime?hl=en.



More information about the Eprime mailing list