%% Write Image Strip to TIFF File
% Read two strips from a TIFF file and write them to a new TIFF file in different positions.
% Open a TIFF file with image data in stripped layout, get the image data and the number of strips in the image.

tr = Tiff('peppers_RGB_stripped.tif','r');
imageR = read(tr);
nStrips = numberOfStrips(tr)

% Read the 2nd and 5th strips of the image.

stripTwo = readEncodedStrip(tr,2);
stripFive = readEncodedStrip(tr,5);

% Create a Tiff object for a new file and copy the image and tag information from the first file.

tw = Tiff('write_strip.tif','w');
tagstruct.ImageLength = getTag(tr,'ImageLength');
tagstruct.ImageWidth =  getTag(tr,'ImageWidth');
tagstruct.Photometric = getTag(tr,'Photometric');
tagstruct.RowsPerStrip = getTag(tr,'RowsPerStrip');
tagstruct.BitsPerSample = getTag(tr,'BitsPerSample');
tagstruct.SamplesPerPixel = getTag(tr,'SamplesPerPixel');
tagstruct.PlanarConfiguration = getTag(tr,'PlanarConfiguration');  
setTag(tw,tagstruct); 
write(tw,imageR)

% Write stripFive in the position for strip number 2 and stripTwo in the position for strip number 5.

writeEncodedStrip(tw,2,stripFive);
writeEncodedStrip(tw,5,stripTwo);

% Read and display the new image next to the original image.

imageW = read(tw);
subplot(121);
imshow(imageR); 
title('Original Image')
subplot(122);
imshow(imageW); 
title('Strips Shuffled Image')

% Close the Tiff objects.

close(tr);
close(tw);

% Copyright 2012 The MathWorks, Inc.