~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychTests/Color3DLUTTest.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function Color3DLUTTest
 
2
% A simple test/demo of use of 3D CLUT color correction.
 
3
%
 
4
% Shows some simple test color transformations, performs some CLUT
 
5
% conversion accuracy test at the end, measuring how smaller CLUT sizes /
 
6
% resolutions affect the precision of color conversion.
 
7
%
 
8
% Press any key to progress through different tests/demos.
 
9
%
 
10
 
 
11
% History:
 
12
% 21.09.2012  mk  Written.
 
13
%
 
14
global diffImage;
 
15
 
 
16
% Open window with 3D CLUT color correction enabled:
 
17
PsychImaging('PrepareConfiguration');
 
18
PsychImaging('AddTask', 'AllViews', 'DisplayColorCorrection', 'LookupTable3D');
 
19
w=PsychImaging('OpenWindow', 0, 0, [0 0 400 400], [], [], [], [], [], kPsychGUIWindow);
 
20
 
 
21
% Build initial identity 3D-CLUT with 16 slots in each color dimension:
 
22
clut = ones(3,16,16,16);
 
23
 
 
24
% Put red-intensity gradient along red-axis of clut cube:
 
25
for i=0:15; clut(1,i+1,:,:) = ones(16,16) * (i/15); end
 
26
% Put green-intensity gradient along gree-axis of clut cube:
 
27
for i=0:15; clut(2,:,i+1,:) = ones(16,16) * (i/15); end
 
28
% Put blue-intensity gradient along blue-axis of clut cube:
 
29
for i=0:15; clut(3,:,:,i+1) = ones(16,16) * (i/15); end
 
30
 
 
31
% Store original identity lut as baseline:
 
32
origclut = clut;
 
33
 
 
34
DrawStim(w, clut, 'Identity mapping: gray gradient, increasing left to right.');
 
35
 
 
36
% Reduce level of verbosity of PsychColorCorrection() to warnings:
 
37
oldverbosity = PsychColorCorrection('Verbosity', 2);
 
38
 
 
39
% Invert red intensity gradient:
 
40
for i=0:15; clut(1,i+1,:,:) = ones(16,16) * (1 - (i/15)); end
 
41
DrawStim(w, clut, 'Identity mapping: Red channel gradient, decreasing left to right.');
 
42
 
 
43
% Invert green intensity gradient:
 
44
clut = origclut;
 
45
for i=0:15; clut(2,:,i+1,:) = ones(16,16) * (1 - (i/15)); end
 
46
DrawStim(w, clut, 'Identity mapping: Green channel gradient, decreasing left to right.');
 
47
 
 
48
% Invert blue intensity gradient:
 
49
clut = origclut;
 
50
for i=0:15; clut(3,:,:,i+1) = ones(16,16) * (1 - (i/15)); end
 
51
DrawStim(w, clut, 'Identity mapping: Blue channel gradient, decreasing left to right.');
 
52
 
 
53
% Only yellow gradient: Clear blue output subvolume:
 
54
clut = origclut;
 
55
clut(3, :, :, :) = 0;
 
56
DrawStim(w, clut, 'Identity mapping: Yellow gradient, blue channel all-zeros.');
 
57
 
 
58
% Some basic correctness test:
 
59
Screen('FillRect', w, 0);
 
60
 
 
61
% Iterate over various CLUT subsampling levels from full resolution
 
62
% identity mapping to a unit value cube and measure how precision of color
 
63
% mapping degrades with decreasing resolution of the 3D CLUT:
 
64
for k = 8:-1:1
 
65
    mi = 2^k - 1;
 
66
    clut = ones(3,mi+1,mi+1,mi+1);
 
67
    
 
68
    % Put red-intensity gradient along red-axis of clut cube:
 
69
    for i=0:mi; clut(1,i+1,:,:) = ones(mi+1,mi+1) * (i/mi); end
 
70
    % Put green-intensity gradient along gree-axis of clut cube:
 
71
    for i=0:mi; clut(2,:,i+1,:) = ones(mi+1,mi+1) * (i/mi); end
 
72
    % Put blue-intensity gradient along blue-axis of clut cube:
 
73
    for i=0:mi; clut(3,:,:,i+1) = ones(mi+1,mi+1) * (i/mi); end
 
74
    
 
75
    PsychColorCorrection('SetLookupTable3D', w, clut);
 
76
    
 
77
    % Each RGB input triplet should map to identical output triplet (minus
 
78
    % interpolation errors, of course:
 
79
    refImage = uint8(rand(400, 400, 3) * 255);
 
80
    tex = Screen('MakeTexture', w, refImage);
 
81
    Screen('DrawTexture', w, tex);
 
82
    Screen('Close', tex);
 
83
    
 
84
    % Run pipeline, readback result:
 
85
    Screen('DrawingFinished', w);
 
86
    testImage = Screen('GetImage', w, [], 'backBuffer');
 
87
    
 
88
    % Show it to user:
 
89
    Screen('Flip', w);
 
90
    WaitSecs(0.5);
 
91
    
 
92
    % Compute differences:
 
93
    diffImage = testImage - refImage;
 
94
    maxdiff = max(max(max(diffImage)));
 
95
    mindiff = min(min(min(diffImage)));
 
96
    meandiff = mean(mean(mean(diffImage)));
 
97
 
 
98
    fprintf('k=%i : CLUT Resolution %i : max = %f min = %f mean = %f error.\n', k, mi+1, maxdiff, mindiff, meandiff);
 
99
end
 
100
 
 
101
% We're done:
 
102
PsychColorCorrection('Verbosity', oldverbosity);
 
103
Screen('CloseAll');
 
104
 
 
105
return;
 
106
 
 
107
end
 
108
 
 
109
function DrawStim(w, clut, label)
 
110
    % Draw some test stim:
 
111
    Screen('FillOval', w, [255 0 0]);
 
112
 
 
113
    % A little intensity gradient from rgb (0,0,0) to (255,255,255):
 
114
    Screen('DrawLines', w, [[0 ; 200] , [400 ; 200]], 10, [[0 ; 0 ; 0] , [255 ; 255 ; 255]]);
 
115
 
 
116
    % Show label for this test:
 
117
    Screen('TextSize', w, 14);
 
118
    DrawFormattedText(w, label, 'center', 50, 255);
 
119
    
 
120
    % Upload it:
 
121
    PsychColorCorrection('SetLookupTable3D', w, clut);
 
122
    
 
123
    % And show the color corrected stim:
 
124
    Screen('Flip', w);
 
125
    
 
126
    % Wait until keypress:
 
127
    KbStrokeWait;
 
128
    
 
129
    return;
 
130
end