

tooSmall = 0;
tooGray = 0;
tooOK = 0;


%% First read full directory

%% This is the folder where you downloaded the .jpeg files to:
jpegDir = '/proj/video_data3/videosim/ejacstr/JVET/CEs/Gothenburg2019/downloadStill/imagesQ99/HD/'

%% This is the destination directory where you want our .yuv files. 
destDir = '/proj/video_data3/videosim/ejacstr/JVET/CEs/Gothenburg2019/downloadStill/yuvHD/'

    if(exist(destDir))
        didcreate = 0;
    else
        didcreate = 1;
        mkdir(destDir);
    end

    numberOfImagesSoFar = 0;
    dirNumber = 1;

    listing = dir(jpegDir);

    %% create an array of all valid files
    for myIndex = 1:size(listing,1)
        % only handle files that are not directories and only have exactly 17 characthers such as 'image_0014828.jpg'
        if (listing(myIndex).isdir == 0)*(size(listing(myIndex).name,2)==17)
            % only handle files that start with 'i' and ends with '.jpg'.
            if (listing(myIndex).name(1)=='i')*(sum(listing(myIndex).name(end-3:end)=='.jpg')==4)
                numberOfImagesSoFar = numberOfImagesSoFar + 1;
                presentArray(numberOfImagesSoFar) = myIndex;
            end;
        end;
    end;

for loadNumber = 0:290


   sourceFileName = sprintf('%s%s', jpegDir, listing(presentArray(loadNumber+1)).name);
   disp(sprintf('loadnumber = %d, sourceFileName = %s', loadNumber, sourceFileName))
   numberString = listing(presentArray(loadNumber+1)).name(7:13);

   if loadNumber > numberOfImagesSoFar
      break;
   end;
		   
   % avoid CMYK files
   if numberString == '0000058' 
     continue;
   end;
   if numberString == '0000273'
     continue;
   end;
   if numberString == '0000525'
     continue;
   end;
   if numberString == '0000686'
     continue;
   end;
   if numberString == '0000842'
     continue;
   end;

   imgRGB = double(imread(sourceFileName));

   if(size(imgRGB,1) < 1080 || size(imgRGB,2) < 1920)
     if(size(imgRGB,2) < 1080 || size(imgRGB,1) < 1920)
        disp(sprintf('********* Too small. Size is %d %d %d.', size(imgRGB, 1), size(imgRGB, 2), size(imgRGB,3)))
        tooSmall = tooSmall + 1;
        continue;
     else
        newImg = [];
        newImg(:,:,1) = imgRGB(:,:,1)';
        newImg(:,:,2) = imgRGB(:,:,2)';
        newImg(:,:,3) = imgRGB(:,:,3)';
	imgRGB = newImg;
        disp(sprintf('********* Transposed image. New size is %d %d %d.', size(imgRGB, 1), size(imgRGB, 2), size(imgRGB,3)))
     end;
   end;

   % convert to grayscale if not already grayscale
   if(size(imgRGB,3)==1)
      disp(sprintf('------------ Only gray.'))
      tooGray = tooGray + 1;
      continue;
   else
      imgYCbCr = toYCbCrfrom709(imgRGB);
   end;

   disp(sprintf('Should work. Size is %d %d %d.', size(imgYCbCr, 1), size(imgYCbCr, 2), size(imgYCbCr,3)))
   tooOK = tooOK + 1;

   % get middle 1920x1080 part of image

   starty = floor((size(imgYCbCr,1)-1080)/2)+1;
   startx = floor((size(imgYCbCr,2)-1920)/2)+1;

   imgYCbCr = imgYCbCr(starty:starty+1080-1, startx:startx+1920-1, :);

   % subsample CbCr
   
   imgCbCr222 = imgYCbCr;
   imgCbCr222sum = (imgCbCr222(1:end-1,1:end-1,:) + imgCbCr222(2:end, 1:end-1,:) + imgCbCr222(1:end-1, 2:end,:) + imgCbCr222(2:end, 2:end,:))/4;
   imgCbCr222sub = imgCbCr222sum(1:2:end, 1:2:end,:);
   imgCbCr222 = round(imgCbCr222sub+128);
   imgYCbCr(:,:,1) = round(imgYCbCr(:,:,1));

   % save file as yuv-file
   yuvFileName = sprintf('%s/yuvfile_%s.yuv', destDir, numberString);
   saveYUVsingle8bit(yuvFileName, imgYCbCr, imgCbCr222);

end;

disp(sprintf('small, gray, OK = %d %d %d out of %d (OK percentage = %f)', tooSmall, tooGray, tooOK, (tooSmall+tooGray+tooOK), 100*tooOK/(tooSmall+tooGray+tooOK)))

