clear all; close all; clc;

p_obr = 20; % počet vybratých obr.

% Načítanie a prípadná úprava dát 

% logaritmus10 = 0; % surové dáta
logaritmus10 = 1; % dekadický logaritmus

% priemer = 0; % 1. obr. z 8
priemer = 1; % priemer z 8 obr.

data = cell(p_obr,8);     % databaza obrazov 
for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\0.25\gel_1.0%_0.25%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
        obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%         data{i,1} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,1} = Image;
            case 1
                Image = log10(Image);
                data{i,1} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\0.125\gel_1.0%_0.125%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,2} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,2} = Image;
            case 1
                Image = log10(Image);
                data{i,2} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\0.0625\gel_1.0%_0.0625%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,3} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,3} = Image;
            case 1
                Image = log10(Image);
                data{i,3} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\0.03125\gel_1.0%_0.03125%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,4} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,4} = Image;
            case 1
                Image = log10(Image);
                data{i,4} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\0.015625\gel_1.0%_0.015625%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,5} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,5} = Image;
            case 1
                Image = log10(Image);
                data{i,5} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\0.0078125\gel_1.0%_0.0078125%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,6} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,6} = Image;
            case 1
                Image = log10(Image);
                data{i,6} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\1.953125.10-3\gel_1.0%_1.953125.10-3%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,7} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,7} = Image;
            case 1
                Image = log10(Image);
                data{i,7} = Image;
        end
end

for i = 1:p_obr
    adress = ['F:\gallo\dp\6.5.2015\3.90625.10-3\gel_1.0%_3.90625.10-3%_intralipid_0' num2str(i) '.FRG']; 
    
    fid = fopen(adress);
    header.FileIdent               = fread(fid, 16, 'schar');    
    header.NumberOfImages          = fread(fid,  1, 'int32');
    fclose(fid);
    ImageRaw = frgread_surove(adress,1);
    obr = cell(8,1);
        for k = 1:8
            obr{k,1} = ImageRaw(: ,k:8:4096); 
        end
        o1 = obr{1,1};
        o2 = obr{2,1};
        o3 = obr{3,1};
        o4 = obr{4,1};
        o5 = obr{5,1};
        o6 = obr{6,1};
        o7 = obr{7,1};
        o8 = obr{8,1};
%     data{i,8} = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        switch(priemer)
            case 0
                Image = o1;
            case 1
                Image = (o1+o2+o3+o4+o5+o6+o7+o8)/8;
        end
        switch(logaritmus10)
            case 0
                data{i,8} = Image;
            case 1
                Image = log10(Image);
                data{i,8} = Image;
        end
end

%% uloženie databázy vybraných obrazov

switch(priemer)
    case 0
        switch(logaritmus10)
            case 0
                save('databaza_obr_oct_intralipid_gel_surove_bez_priemeru.mat','data')
            case 1
                save('databaza_obr_oct_intralipid_gel_log10_bez_priemeru.mat','data')
        end
    case 1
        switch(logaritmus10)
            case 0
                save('databaza_obr_oct_intralipid_gel_surove_priemer.mat','data')
            case 1
                save('databaza_obr_oct_intralipid_gel_log10_priemer.mat','data')
        end
end
