-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayLumForCalibration.m
136 lines (120 loc) · 3.71 KB
/
DisplayLumForCalibration.m
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
136
function [ ] = DisplayLumForCalibration( levels, stereoMode, blankOther )
%DISPLAYLUMFORCALIBRATION Displays a bunch of luminances for calibration
% All arguments optional (pass [] to skip one)
% default levels: ([0:10:250, 255, 128:150], 1, false)
% levels is a list of grayscale raw lumenances, or
% a cell array of colors, ex:
% ({[200,0,0], [0,200,0], [0,0,200], [200,200,200]})
%
% Example:
% colors = cell(1,100);
% for i=1:100
% colors(i) = {[180 180 180] + round(rand(1,3))};
% end
% DisplayLumForCalibration(colors);
% Press 'x' to halt, or any other key to advance to next luminance
%
% FIXME Weird display bug happens when blankOther = true!
if nargin<1 || isempty(levels)
levels = [0:10:250, 255, 128:150];
end
if nargin<2 || isempty(stereoMode)
stereoMode = 1; % 1 = OpenGL stereo, see Screen('OpenWindow?')
end
if nargin<3 || isempty(blankOther)
blankOther = false;
end
HW = HardwareParameters();
HW.stereoMode = stereoMode;
HW.stereoTexOffset = [];
HW.usePTBPerPxCorrection = false;
dummyLums = [0:10:250, 255]';
HW.lumCalib = [dummyLums, dummyLums];
[didHWInit HW] = InitializeHardware(HW);
HW = ScreenCustomStereo(HW, 'Flip', HW.winPtr); % initial flip
caughtE = [];
try
if stereoMode
for lum=levels
drawRect(HW, lum, 0, blankOther);
end
% only do the other eye if the two eyes are different
if blankOther
for lum=levels
drawRect(HW, lum, 1, blankOther);
end
end
else
for lum=levels
drawRect(HW, lum);
end
end
catch e
caughtE = e;
end
if didHWInit
HW = CleanupHardware(HW); %#ok<NASGU>
end
if ~isempty(caughtE)
rethrow(caughtE)
end
end
function [] = drawRect(HW, lum, eye, blankOther)
if iscell(lum)
lum = cell2mat(lum);
end
fprintf('Luminance now at ');
if numel(lum) > 1
fprintf('[%3d, %3d, %3d]', lum(1), lum(2), lum(3));
else
fprintf('%3d', lum);
end
if (nargin > 2)
side = 'left eye';
if eye
side = 'right eye';
end
if ~blankOther
side = 'both eyes with stereo on';
end
fprintf(' in %s', side);
else
fprintf(' with stereo off');
end
fprintf('\n');
% TODO Redrawing every frame in attempt to avoid blankOther=true bug
% but it didn't work!
KbWait([],1);
vbl = 0; %Flip at next possible
while ~KbCheck
if (nargin > 2)
% i=0 for left eye, i=1 for right eye
HW = ScreenCustomStereo(HW, 'SelectStereoDrawBuffer', ...
HW.winPtr, eye);
end
Screen('FillRect', HW.winPtr, lum);
if (nargin > 2)
% blank the other eye
otherEye = mod(eye+1,2);
HW = ScreenCustomStereo(HW, 'SelectStereoDrawBuffer', ...
HW.winPtr, otherEye);
if blankOther
otherEyeLum = 0;
else
otherEyeLum = lum;
end
Screen('FillRect', HW.winPtr, otherEyeLum);
end
HW = ScreenCustomStereo(HW, 'DrawingFinished', HW.winPtr);
[HW, vbl] = ScreenCustomStereo(HW, 'Flip', HW.winPtr, ...
vbl+0.25/HW.fps);
end
% Get and interpret the response
[~,keyCode,~] = KbWait([]);
response = KbName(keyCode);
if strcmp(response, 'x')
% 'graceful' bail
throw(MException('DisplayLumForCalibration:Halt', ...
'Halted by user!'));
end
end