level = 0;
basename = 'medical';
dirname = sprintf('%s_%d', basename, level);
filename = sprintf('%s.jpg', dirname);
fprintf('Chopping %s\n', filename);

%[rs, gs, bs] = jpgread(filename);
load medical_rp;
fprintf(' rp loaded\n');
load medical_gp;
fprintf(' gp loaded\n');
load medical_bp;
fprintf(' bp loaded\n');

height = size(medical_rp, 1);
width = size(medical_rp, 2);
step_size = 100;

for i = 1:step_size:height % rows
  for j = 1:step_size:width % columns
    chunk_rs = medical_rp(i:(min(i+step_size-1, height)), j:(min(j+step_size-1, width)) );
    chunk_gs = medical_gp(i:(min(i+step_size-1, height)), j:(min(j+step_size-1, width)) );
    chunk_bs = medical_bp(i:(min(i+step_size-1, height)), j:(min(j+step_size-1, width)) );
    
    filename = sprintf('%s/%s_%d_%d.jpg', dirname, dirname, ...
                       round(i/step_size)+1, round(j/step_size)+1);
    fprintf('%s\n', filename);
    jpgwrite(filename, chunk_rs, chunk_gs, chunk_bs);
  end

end

