# Antialiasing and subpixel plotting

**URL:** <https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717>\
**Category:** Uncategorized\
**Created:** [June 7, 2006, 6:57am UTC](https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717 "2006-06-07T06:57:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [June 7, 2006, 6:57am UTC](https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717/1 "2006-06-07T06:57:24Z")

</div>

Greetings,  
  
I am switching over from the OS 9.2 Psychtoolbox to the OsX version   
for the Macintosh. How can I antialias diagonal lines and get   
subpixel resolution on my stereo images (I'm using a split screen and   
mirrors at the moment)? I used to do it by drawing big pictures in   
the OffScreenWindow and shrinking them down to the OnScreenWindow   
using the CopyWindow command. The DrawTexture function looks like   
the thing to use but I don't know what a "texturePointer" is.  
  
Many thanks in advance.  
  
Dr. Philip Grove  
School of Psychology  
The University of Queensland  
St. Lucia, QLD 4072  
Australia  
[p.grove@...](mailto:p.grove@...)

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [June 12, 2006, 1:49am UTC](https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717/2 "2006-06-12T01:49:43Z")

</div>

Hi Jenny,  
  
version 1 of your code with Screen('MakeTexture') should work if you  
provide the dstRect parameter explicitely to either  
Screen('DrawTexture') or Screen('CopyWindow'). If you don't provide  
it, PTB will center your texture in the onscreen window, so if your  
image is bigger than the screen, you'll just see the center-portion of it.  
  
If you set dstRect = rect, as returned by Screen('OpenWindow'), PTB  
will rescale (downsample by use of bilinear filtering) the image to  
fit the screen and you'll get what you want.  
  
Method 2: Drawing into offscreen window has currently one limitation.  
It does not allow for offscreen windows greater than the screen, so  
this may only work for small stimuli.  
  
But here is some new way of getting subpixel resolution and  
Anti-Aliasing without any special coding:  
  
I've updated the PTB beta a few minutes ago: Now you can pass a new  
optional parameter 'multiSample' to Screen('OpenWindow') -- see its  
updated online help. If you set multiSample to a value greater than  
zero, then PTB will ask your gfx hardware to perform automatic  
anti-aliasing on all drawn content, where the value of 'multiSample'  
specifies the number of samples to use for computing each pixels final  
color value. Values supported by todays hardware are, depending on the  
age of your hardware, screen resolution and amount of video memory on  
the card: 2, 4, 8 and (on very new cards at low resolution) 16. If you  
ask for a value greater than the one supported by your hardware, PTB  
will simply use the highest supported value. The Anti-Aliasing mode is  
reported to the Matlab window. This method of brute force  
Anti-Aliasing obviously comes at a cost: The much higher amount of  
required video memory and memory bandwidth will significantly reduce  
the drawing speed, so only use it if you need it.  
  
A good beginners intro on how this works can be found at:  
  
\<[http://en.wikipedia.org/wiki/Supersampling](http://en.wikipedia.org/wiki/Supersampling)\>  
  
best,  
-mario

---

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [June 12, 2006, 11:12am UTC](https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717/3 "2006-06-12T11:12:29Z")

</div>

Hi Mario,  
  
Thanks so much for that quick and incredibly helpful reply.  
  
I've downloaded the new PTB beta and had a go with the new multisample.   
It works beautifully and does just what I want. (At least, with   
multisample=4 it does -- with multisample=8, I got a red flashing   
screen, a yellow exclamation mark and lots of warnings about   
synchronisation failure. But that's fine; I know not to use that in   
future). Thanks so much for adding this.  
  
I did also try your other solution, setting dstRect to rect, but this   
didn't work for me. The code I used was  
  

> \> % Set up screen  
> \> [window,rect]=Screen('OpenWindow',0);  
> \> nx = rect(3); ny = rect(4);  
> \> white=WhiteIndex(window); black=BlackIndex(window);  
> \> x = [1:nx]; y = [1:ny]; [x2,y2] = meshgrid(x,y);  
> \>  
> \> % Generate a simple image that's twice as big as my screen along   
> \> both axes. It's black with part of a white circle.  
> \> expandfactor = 2;  
> \> im=black\*ones(ny\*expandfactor,nx\*expandfactor);  
> \> im(x2.^2+y2.^2\<500000)=white;  
> \>  
> \> % Now squish this image into my screen.  
> \> textureIndex=Screen('MakeTexture', window, im);  
> \> Screen('DrawTexture', window, textureIndex,rect\*expandfactor,rect,1);  
> \> % I also tried Screen('DrawTexture', window, textureIndex,[],rect,1);  
> \> Screen('Flip', window);

Is this what you meant? This did give me an antialiased image, but it   
was doubled vertically, ie the picture appeared twice down my screen,   
and there was some other weird stuff too round the edges. Of course I   
don't need this to work now you've provided the multisample function,   
but I thought you might like to have the feedback.  
  
Best wishes and very many thanks for your help,  
  
Jenny  
  
(using revision 363, beta version 1.0.6 on Windows XP).  
  
--   
Jenny Read Office: +44 191 222 7559  
Royal Society University Research Fellow Mobile: +44 794 401 5796  
Henry Wellcome Building for Neuroecology, Fax: +44 191 222 5622  
University of Newcastle, Framlington Place mailto:[J.C.A.Read@...](mailto:J.C.A.Read@...)  
Newcastle upon Tyne, NE2 4HH, UK. [http://www.staff.ncl.ac.uk/j.c.a.read](http://www.staff.ncl.ac.uk/j.c.a.read)  
  
  
  
Mario Kleiner wrote:  
  

> \> Hi Jenny,  
> \>  
> \> version 1 of your code with Screen('MakeTexture') should work if you  
> \> provide the dstRect parameter explicitely to either  
> \> Screen('DrawTexture') or Screen('CopyWindow'). If you don't provide  
> \> it, PTB will center your texture in the onscreen window, so if your  
> \> image is bigger than the screen, you'll just see the center-portion of it.  
> \>  
> \> If you set dstRect = rect, as returned by Screen('OpenWindow'), PTB  
> \> will rescale (downsample by use of bilinear filtering) the image to  
> \> fit the screen and you'll get what you want.  
> \>  
> \> Method 2: Drawing into offscreen window has currently one limitation.  
> \> It does not allow for offscreen windows greater than the screen, so  
> \> this may only work for small stimuli.  
> \>  
> \> But here is some new way of getting subpixel resolution and  
> \> Anti-Aliasing without any special coding:  
> \>  
> \> I've updated the PTB beta a few minutes ago: Now you can pass a new  
> \> optional parameter 'multiSample' to Screen('OpenWindow') -- see its  
> \> updated online help. If you set multiSample to a value greater than  
> \> zero, then PTB will ask your gfx hardware to perform automatic  
> \> anti-aliasing on all drawn content, where the value of 'multiSample'  
> \> specifies the number of samples to use for computing each pixels final  
> \> color value. Values supported by todays hardware are, depending on the  
> \> age of your hardware, screen resolution and amount of video memory on  
> \> the card: 2, 4, 8 and (on very new cards at low resolution) 16. If you  
> \> ask for a value greater than the one supported by your hardware, PTB  
> \> will simply use the highest supported value. The Anti-Aliasing mode is  
> \> reported to the Matlab window. This method of brute force  
> \> Anti-Aliasing obviously comes at a cost: The much higher amount of  
> \> required video memory and memory bandwidth will significantly reduce  
> \> the drawing speed, so only use it if you need it.  
> \>  
> \> A good beginners intro on how this works can be found at:  
> \>  
> \> \<[http://en.wikipedia.org/wiki/Supersampling](http://en.wikipedia.org/wiki/Supersampling)   
> \> \<[http://en.wikipedia.org/wiki/Supersampling](http://en.wikipedia.org/wiki/Supersampling)\>\>  
> \>  
> \> best,  
> \> -mario  
> \>  
> \>

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [June 19, 2006, 6:34am UTC](https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717/4 "2006-06-19T06:34:55Z")

</div>

Hi Jenny,  
  
beta updated again: Better error-checking, special case for  
multisampling implemented, always selects high-quality filter,  
stereomode 4 (the one for nView desktop spanning) improved to be  
actually useful on a setup like yours.  
  
Could you retest with this beta?  
  
best,  
-mario

---

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [July 23, 2006, 6:22am UTC](https://psychtoolbox.discourse.group/t/antialiasing-and-subpixel-plotting/1717/5 "2006-07-23T06:22:08Z")

</div>

It is pretty easy. Make a file called startup.m somewhere on the path  
(on linux or mac the best place is ~/matlab, I'm afraid I'm not sure  
where the best place is on windows). Startup is run everytime matlab  
starts. Add this line to your startup.m:  
  
addpath(genpath('~/matlab/Psychtoolbox'));  
  
Again, this is path stuff for linux or mac, but shouldn't be hard to  
translate it appropriately for windows. This command will put  
everything in your Psychtoolbox folder on your path. Give your other  
Psychtoolbox a different name, like Psychtoolboxbeta, and also put  
this in your startup.m:  
  
addpath(genpath('~/matlab/Psychtoolboxbeta'));  
  
You can comment out the one that you don't want to use. Anytime you  
want to switch, or after installing/updating the psychtoolbox, run  
restoredefaultpath. Or you could just have this as the first command  
in the startup.m This will get rid of anything extra on your path.  
Then run startup. You could even do something fancy, like put an  
optional argument to startup that lets you choose which Psychtoolbox  
you want (make sure you have just one of the toolboxes as the default  
though, so when matlab starts it just has one psychtoolbox on the  
path).  
  
Doing it this way means that you need to put everything you want on  
your path in the startup, rather than using the gui, but this isn't  
hard, since the addpath(genpath()) syntax adds subdirectories.  
  
hope this helps,  
cheers,  
maria  
  

> On 7/22/06, Charles E. (Ted) Wright \<[cew@...](mailto:cew@...)\> wrote:  
> \> Hi -  
> \>  
> \> Having been monitoring this thread about the Windows support of the beta  
> \> branch of the PTB, I am interested in trying it out. My question is how  
> \> difficult it will be to have both on the same machine. To do this will I  
> \> need to reset all of the entries for the PTB in the Matlab path each time I  
> \> want to switch back and forth. I imagine that I could put together a  
> \> function to do this automatically -- is that the idea?  
> \>  
> \> Thanks,  
> \>  
> \> Ted Wright  
> \>  
> \> On Fri, 21 Jul 2006, Mario Kleiner wrote:  
> \>  
> \> \> You need to DownloadPsychtoolbox('beta',...). The 'stable' branch  
> \> \> doesn't support M$-Windows or any other feature added in the last 6  
> \> \> months yet. If you did that, then a 'which Screen' should show you  
> \> \> that Screen.dll is installed inside the downloaded PTB, otherwise you  
> \> \> need to reset your Matlab path properly.  
> \> \>  
> \> \> -mario  
> \>  
> \>  
> \>  
> \> Post your message to: [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com)  
> \> Please indicate OS9, OSX, or WIN version, and include your full name.  
> \> Denis Pelli, David Brainard, and Allen Ingling.  
> \> [http://psychtoolbox.org](http://psychtoolbox.org)  
> \>  
> \> Yahoo! Groups Links  
> \>  
> \>  
> \>  
> \>  
> \>  
> \>  
> \>  
>    
>    
> --   
> Maria Mckinley  
> Scientific Programmer  
> Shadlen Lab  
> Physiology and Biophysics  
> University of Washington  
> (206) 616-7107  
> [parody@...](mailto:parody@...)
