
function  Image = frgread(Path, FrameNumber)

fid = fopen(Path);
% fid = fopen('D:\Dizertace\Intralipid\Intralipid3\Int0_625pr.FRG');

% File header read
header.FileIdent               = fread(fid, 16, 'schar');
header.NumberOfImages          = fread(fid,  1, 'int32');
header.ImageWidth              = fread(fid,  1, 'int32');
header.ImageDepth              = fread(fid,  1, 'int32');
header.NumFramesInVolume       = fread(fid,  1, 'int32');
header.NumberOfVolumes         = fread(fid,  1, 'int32');
header.FFTlength               = fread(fid,  1, 'int32');
header.FrameSize               = fread(fid,  1, 'int32');
header.RecordLength            = fread(fid,  1, 'int32');
header.PSOCTfringeMode         = fread(fid,  1, 'int16');
header.AverageNumber           = fread(fid,  1, 'int16');
header.ImagingMode             = fread(fid,  1, 'int16');
header.DopplerOverlay          = fread(fid,  1, 'int16');
header.RedShiftDopplerOverlay  = fread(fid,  1, 'int32');
header.BlueShiftDopplerOverlay = fread(fid,  1, 'int32');
header.Reserved                = fread(fid,448, 'int8');
header.FrameData               = fread(fid,  1, 'int32');
% 
% disp(['              FileIdent: ' char(header.FileIdent')]);
% disp(['         NumberOfImages: ' num2str(header.NumberOfImages)]);
% disp(['             ImageWidth: ' num2str(header.ImageWidth)]);
% disp(['             ImageDepth: ' num2str(header.ImageDepth)]);
% disp(['      NumFramesInVolume: ' num2str(header.NumFramesInVolume)]);
% disp(['        NumberOfVolumes: ' num2str(header.NumberOfVolumes)]);
% disp(['              FFTlength: ' num2str(header.FFTlength)]);
% disp(['              FrameSize: ' num2str(header.FrameSize)]);
% disp(['           RecordLength: ' num2str(header.RecordLength)]);
% disp(['        PSOCTfringeMode: ' num2str(header.PSOCTfringeMode)]);
% disp(['          AverageNumber: ' num2str(header.AverageNumber)]);
% disp(['            ImagingMode: ' num2str(header.ImagingMode)]);
% disp(['         DopplerOverlay: ' num2str(header.DopplerOverlay)]);
% disp([' RedShiftDopplerOverlay: ' num2str(header.RedShiftDopplerOverlay)]);
% disp(['BlueShiftDopplerOverlay: ' num2str(header.BlueShiftDopplerOverlay)]);

Frames = {zeros([header.NumFramesInVolume 1])};

BytesOfEachFrame = 40 + header.FFTlength*header.ImageWidth*2;

%% data read
    
fseek(fid, BytesOfEachFrame*(FrameNumber-1), 'cof');
frameData.TimeElapsed = fread(fid, 1, 'int32');
frameData.SystemTime  = fread(fid, 1, 'int32');
frameData.Reseved     = fread(fid, 32, 'int8');

RawData = fread(fid, [header.FFTlength,header.ImageWidth], 'int16');

% úprava surových dat oknem (Gauss)
% [index_length, width] = size(RawData);
% window_band = 0.3*index_length;
% depth_index = (1:index_length) - round(index_length/2);
% window = exp(-(depth_index./window_band).^2)'*ones(1,width);
% RawData = RawData.*window;    

WholeImage = ifft(RawData, header.FFTlength); % zpětná Fourierova transformace

Image = abs(WholeImage(1:header.ImageDepth,:));

Contrast   = 1.5;                         % převod do decibelové míry
Brightness = -180;
% 
% % Contrast   = 3;                         % převod do decibelové míry
% % Brightness = -195;
dB_Range   = 70;
Aline_max  = max(max(Image));
a = Contrast*255;
b = 128 + Brightness + 127*Contrast;
dBRange = 10/(log(10)*dB_Range);
Image   = log(Image./Aline_max)*dBRange*a + b;
Image((Image > 255)) = 255;
Image((Image < 0))   = 0;

Image = Image - min(min(min(Image)));  
Image = Image/max(max(max(Image)));       % normalizace

% figure(2000)
% imshow(Image,[])
% pause

% disp('hotovo')
fclose(fid);
%%







