function [  ] = rgb_screen_generation( input_args )
clc;

[FileName,PathName] = uigetfile('*.*', 'Choisir image en entrée');
I = imread(strcat(PathName,FileName));
[height width depth] = size(I);
counter = 1;

if(height ~= 64 && width ~= 64)
    fprintf('Mauvaises dimensions\n');
    return
end

buffer = zeros(3072,1);
pixel_value = 0;
max_val = 0;

for i=1:depth
    I(:,:,i)= rot90(I(:,:,i));
end
    
for x=1:height
    for y=1:width
        for z=1:depth
            if I(x,y,z) > max_val
                max_val = I(x,y,z);
            end
        end
    end
end

fprintf('Maximum détecté de %d\n', max_val);

for z=1:depth
    for x=1:height
        for y=1:(width/4)
            A = 0;
            for j=1:4
                pixel_value = I(x, (y-1)*4 + j, z);
                if pixel_value >= floor(max_val*0.75)
                    val = 3;
                elseif pixel_value >= floor(max_val*0.5)
                    val = 2;
                elseif pixel_value >= floor(max_val*0.25)
                    val = 1;
                else
                    val = 0;
                end
                A = bitor(A,bitshift(val,(4-j)*2));
            end
            buffer(counter) = A;
            counter = counter + 1;
        end
    end
end


if(exist('input_args', 'var'))
    s = serial('COM9', 'Parity', 'none', 'ByteOrder', 'BigEndian', 'InputBufferSize', 2000000, 'OutputBufferSize', 1000000 , 'BaudRate', 125000, 'DataBits', 8, 'RequestToSend', 'off', 'StopBits', 1, 'ReadAsyncMode', 'continuous', 'FlowControl', 'none');
    fopen(s);
    fwrite(s,'r','uint8');
    fclose(s);
    pause(0.2);
end

s = serial('COM9', 'Parity', 'none', 'ByteOrder', 'BigEndian', 'InputBufferSize', 2000000, 'OutputBufferSize', 1000000 , 'BaudRate', 1041666, 'DataBits', 8, 'RequestToSend', 'off', 'StopBits', 1, 'ReadAsyncMode', 'continuous', 'FlowControl', 'none');
fopen(s);
fwrite(s,buffer,'uint8'); 
fclose(s);
delete(s);

fprintf('sent\n');

end