%% Write YCbCr Image Data to TIFF File
% Write a YCbCr image and multiple tag values that characterize the image to a new TIFF file.
% Load and display the Y component of the image.

load('peppers_YCbCr.mat');
imshow(Y);
title('Peppers Image (Y Component)');

% Create a Tiff object to write the image data to a TIFF file.

t = Tiff('myfile_YCbCr.tif','w');  

% Use the tagstruct structure to define multiple tag values that characterize the image.

tagstruct.ImageLength = size(Y,1);
tagstruct.ImageWidth = size(Y,2);
tagstruct.SampleFormat = 1; % uint
tagstruct.Photometric = Tiff.Photometric.YCbCr;
tagstruct.BitsPerSample = 8 ;
tagstruct.SamplesPerPixel = 3;
tagstruct.YCbCrSubSampling = [1,1];
tagstruct.Compression = Tiff.Compression.None;  
tagstruct.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky; 
tagstruct.Software = 'MATLAB'; 

% Set the tag values for the Tiff object.

setTag(t,tagstruct)

% Write image data to the file, and then close the Tiff object.

write(t,Y,Cb,Cr)
close(t)

% Copyright 2012 The MathWorks, Inc.