%% Write Tiff Image with Color and Alpha Channel Data
% For a dataset with color and alpha channels, set the Tiff tags and then write the data to a file.
% Create an array of data, data, that contains color channels and an alpha channel.

rgb = imread('example.tif');
numrows = size(rgb,1);
numcols = size(rgb,2);
alpha = 255*ones([numrows numcols], 'uint8');
data = cat(3,rgb,alpha);

% Create a Tiff object.

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

% Set the Tiff tags and specify the value of the ExtraSamples tag because the data contains the alpha channel in addition to the color channels.

setTag(t,'Photometric',Tiff.Photometric.RGB);
setTag(t,'Compression',Tiff.Compression.None);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',4);
setTag(t,'SampleFormat',Tiff.SampleFormat.UInt);
setTag(t,'ExtraSamples',Tiff.ExtraSamples.Unspecified);
setTag(t,'ImageLength',numrows);
setTag(t,'ImageWidth',numcols);
setTag(t,'TileLength',32);
setTag(t,'TileWidth',32);
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);

% Write the data to the TIFF file and close the Tiff object.

write(t,data);
close(t);

% Copyright 2012 The MathWorks, Inc.