File: SetResolution.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (135 lines) | stat: -rw-r--r-- 4,556 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function oldRes=SetResolution(screenNumber,width,height,hz,pixelSize)
% oldRes=SetResolution(screenNumber,width,height,[hz],[pixelSize])
% oldRes=SetResolution(screenNumber,res)
% 
% Set the resolution of the screen.  This is intended to be used in
% programs that run psychophysical experiments, so SetResolution is
% strict. It issues a fatal error unless it can provide exactly the
% resolution you specified. (For lenience, look at NearestResolution.)
%
% "screenNumber" is the screen number.
%
% "width" and "height" are the desired dimensions in pixels.  
%
% "hz" is the desired refresh rate (default is current frame rate).  
%
% "pixelSize" 8, 16, 24, or 32 bits and defaults to current pixelSize.
% On modern operating systems, you should leave "pixelSize" alone, as
% often there will be only one good choice anyway, which is already set
% as the default, so changing it will usually change it for the worse!
%
% Returns the current resolution as "oldRes".  
% 
%   oldRes=SetResolution(0,1024,768,75);
%   w=Screen(0,'OpenWindow');
%   Screen(w,'PutImage',image);
%   Screen(w,'Close');
%   SetResolution(0, oldRes);
% 
% To display a list of available resolutions, try ResolutionTest. Also see
% NearestResolution, ResolutionTest, and Screen('Resolution')
% and Screen('Resolutions').
% 
% NOTE: Apple has all the new LCD screens return a frame rate of 0, so
% we treat that value as a special case. A request for "hz" of NaN will
% match only with a frame rate of 0.
%
% NOTE: On Linux/X11 this only works as you'd expect if there is only one
% video output connected to a given Psychtoolbox screen / X-Screen. On a
% setup with multiple outputs per screen, this will only change the
% size of the framebuffer, but not the resolution of the actual displays!
% Use Screen('ConfigureDisplay', 'Scanout', ...); to change settings on
% such a multi-display per screen setup on Linux.
% 
% Originally written by Sabina Wolfson.

% HISTORY:
% 1/27/00 SSW Wrote it.
% 1/28/00 dgp Cosmetic editing. Made screenNumber first argument. Renamed from set_resolution to SetResolution.
% 4/9/02  dgp Check the width and height arguments.
% 4/13/02 dgp Cosmetic.
% 4/29/02 dgp Screen Resolutions is only available on Mac.
% 6/6/02  dgp Accept res instead of parameter list.
% 6/7/02  dgp Hz value of NaN matches NaN.
% 9/23/07 mk  Adapted for Psychtoolbox-3.
% 4/15/14 mk  Adapted for multi-output per x-screen setups on Linux.
% 2/14/21 mk  Help text refined.

if nargin<2 || nargin>5
    error(sprintf('%s\n%s','USAGE: oldRes=SetResolution(screenNumber,width,height,[hz],[pixelSize])',...
                           '       oldRes=SetResolution(screenNumber,res)'));
end

if ~ismember(screenNumber, Screen('Screens'))
    error(sprintf('Invalid screenNumber %d.',screenNumber));
end

oldRes = Screen('Resolution', screenNumber);

if nargin<5 || isempty(pixelSize)
    pixelSize=oldRes.pixelSize;
end

if nargin<4 || isempty(hz)
    hz=oldRes.hz;
end

if nargin==2 && isa(width,'struct')
    res=width;
    if isfield(res,'width')
        width=res.width;
    end
    
    if isfield(res,'height')
        height=res.height;
    end
    
    if isfield(res,'hz')
        hz=res.hz;
    end
    
    if isfield(res,'pixelSize')
        pixelSize=res.pixelSize;
    end
end

if ~exist('height','var')  || isempty(height) || ~isfinite(height)
    error('height (in pixels) must be specified.');
end

if ~exist('width','var') || isempty(width) || ~isfinite(width)
    error('width (in pixels) must be specified.');
end

if isnan(hz)
    hz = 0;
end

% One display output per screen? Then we can validate the settings.
if Screen('ConfigureDisplay', 'NumberOutputs', screenNumber) < 2
    res = Screen('Resolutions', screenNumber, 0); % get a list of available resolutions

    resIndex=find([res.width]==width & [res.height]==height & [res.hz]==hz);
    if isempty(resIndex)
        error(sprintf('Can''t find a resolution of %g x %g x %g Hz. Resolution remains unchanged.',width,height,hz));
    end
    
    nres = res(resIndex(1));
else
    % Multiple outputs per screen. Can't validate, but take settings at face value and hope for the best:
    nres.width = width;
    nres.height = height;
    nres.hz = hz;
    nres.pixelSize = pixelSize;
    warning('SetResolution: Only changing virtual resolution of multi-display screen %i!\n');
end

if isempty(pixelSize) || ~isfinite(pixelSize)
    nres.pixelSize = [];
else
    nres.pixelSize = pixelSize;
end

oldRes = Screen('Resolution', screenNumber, nres.width, nres.height, nres.hz, nres.pixelSize);

return;