function [ output_args ] = gif_screen_generation( input_args )
%GIF_SCREEN_GENERATION Summary of this function goes here
%   Detailed explanation goes here
%[FileName,PathName] = uigetfile('*.gif', 'Choisir gif en entrée');
%[img,map] = imread(strcat(PathName,FileName), 'frames', 'all');
clc;

buffer = zeros(3072,1);
good_file_counter = 0;
pixel_value = 0;
nb_loops = 7;

RandStream.setDefaultStream (RandStream('mt19937ar','seed',sum(100*clock)));
dir_name = uigetdir('C:\', 'Select folder with gifs');
listing = dir(strcat(dir_name,'\*.gif'));
[nb_files x] = size(listing);

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);

c = onCleanup(@()cleanup_fn(s));

while 1
    file_index = ceil(rand * nb_files);
    file_ok = true;
    try
        fprintf('Opening %s...\n', listing(file_index).name);
        [img,map] = imread(strcat(dir_name,'\',listing(file_index).name), 'frames', 'all');
    catch
        fprintf('Problem opening %s\n', listing(file_index).name);
        delete(strcat(dir_name,'\',listing(file_index).name));
        file_ok = false;
    end
    
    if file_ok
        [height width depth length] = size(img);
        is_inv_test = findstr(listing(file_index).name, 'inv');
        [is_inv b] = size(is_inv_test);

        pictures = zeros(64, 64, 3, length);
        nb_loops = 100 / length;
        max_val = 0;

        for i=1:length    
            pictures(:,:,:,i) = imresize(im2uint8(ind2rgb(img(:,:,:,i),map)), [64 64]);
            for j=1:3
                pictures(:,:,j,i)= rot90(pictures(:,:,j,i));
            end
        end

        for a=1:length
            for x=1:64
                for y=1:64
                    for z=1:3
                        if pictures(x,y,z,a) > max_val
                            max_val = pictures(x,y,z,a);
                        end
                    end
                end
            end
        end

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

        for nb=1:nb_loops
            for l=1:length
                counter = 1;
                for z=1:3
                    for x=1:64
                        for y=1:(64/4)
                            A = 0;
                            for j=1:4                                
                                pixel_value = pictures(x, (y-1)*4 + j, z, l);
                                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
                                if is_inv
                                    val = bitand(3, ~val);
                                end
                                A = bitor(A,bitshift(val,(4-j)*2));
                            end
                            buffer(counter) = A;
                            counter = counter + 1;
                        end
                    end
                end
                pause(0.027);
                while(strcmp(s.TransferStatus, 'idle') == 0)
                            counter = counter + 1;
                end       
                fwrite(s,buffer,'uint8', 'async');  
            end
        end
        
        %{
        answer = questdlg('Keep file?', 'Delete question', 'No', 'Yes', 'Invert', 'No');
        if(strcmp(answer, 'No'))
            delete(strcat(dir_name,'\',listing(file_index).name));
        elseif(strcmp(answer, 'Invert'))
            good_file_counter = good_file_counter + 1;
            newName = sprintf('image%05d_inv.gif', good_file_counter); 
            movefile(strcat(dir_name,'\',listing(file_index).name),strcat(dir_name,'\',newName)); 
        else
            good_file_counter = good_file_counter + 1;
            newName = sprintf('image%05d.gif', good_file_counter); 
            movefile(strcat(dir_name,'\',listing(file_index).name),strcat(dir_name,'\',newName)); 
        end
        %}
    end
end

while(strcmp(s.TransferStatus, 'idle') == 0)
    counter = counter + 1;
end
fclose(s);
delete(s);
end

function [] = cleanup_fn(stream)
    while(strcmp(stream.TransferStatus, 'idle') == 0)
    end
    fclose(stream);
    delete(stream);
end