%% Loading data
pre = compose_hud_all2('D:/EDDebugging/hud_images', 'no_hud.dds', 896, 926);
post = load_dds('D:/EDDebugging/hud_images/after_pp.dds');

%% Solving the problem in RGB
pre_sat = min(pre, repmat(1, size(pre)));
pre_r = pre_sat(:,:,1);
pre_g = pre_sat(:,:,2);
pre_b = pre_sat(:,:,3);
post_r = post(:,:,1);
post_g = post(:,:,2);
post_b = post(:,:,3);

% rr * pre.r + rg * pre.g + rb * pre.b + rc = post.r
A = [pre_r(:) pre_g(:) pre_b(:) ones(prod(size(pre_r)),1)];
r = A\post_r(:);
g = A\post_g(:);
b = A\post_b(:);
mat = [r g b];

test_img = zeros(size(pre));
for y = 1:size(pre,1)
    for x = 1:size(pre,2)
        rgb = squeeze(pre(y,x,:))';
        c = [rgb 1];
        test_img(y,x,:) = c * mat;
    end
end
imshow(test_img)