%Chapter 8,  Fig 8.2
%Eric Dubois, updated 2018-12-14
%Create a color sinusoidal image
% [C](x,y) = [A1] + [A2]cos(2*pi*(ux+vy))
% Provide endpoints [A1]+[A2] and [A1]-[A2] in RGB cube
% u and v are provided
% sinusoidal image is defined in sRGB and so gamma correction is applied as
% last step
clear all; close all;
%Fix the image size
NH = 600; NV = 600; % horizontal and vertical picture dimensions
dx=1/NV;  % sample spacing in ph
x=0:dx:(NH-1)*dx; y=0:dx:(NV-1)*dx;   %x and y values  spaced by dx
[X,Y]=meshgrid(x,y);              % generate a 2D grid of xy values
u=-20; v=5;       % specify the horizontal and vertical spatial frequencies  
CS = cos(2*pi*(u*X+v*Y));
A12p = [0 .7 0];
A12m = [1 0 1];
A1 = .5*(A12p + A12m);
A2 = .5*(A12p - A12m);
% create color sinusoid
for k = 1:3
    C(:,:,k) = A1(k) + A2(k)*CS;
end
% apply sRGB gamma correction
Cp = 1.055*(C.^0.41666)-0.055;
Cp(C<.0031308) = C(C<.0031308)*12.92;
figure;
image(1:NV,1:NH,Cp);
set(gcf,'Color',[1 1 1]);
axis square;
axis off;
set(gca,'ydir','reverse'); 