%% Create YCbCr/JPEG image from RGB data
% Write RGB image data to a TIFF file as an YCbCr/JPEG image.
% Get RGB data.

rgb = imread('example.tif');

% Create a Tiff object, t, and set the tags. Specify that the input data is RGB using the JPEGColorMode tag.

t = Tiff('myfile.tif','w');
setTag(t,'Photometric',Tiff.Photometric.YCbCr);
setTag(t,'Compression',Tiff.Compression.JPEG);
setTag(t,'YCbCrSubSampling',[2 2]);
setTag(t,'BitsPerSample',8);
setTag(t,'SamplesPerPixel',3);
setTag(t,'SampleFormat',Tiff.SampleFormat.UInt);
setTag(t,'ImageLength',size(rgb,1));
setTag(t,'ImageWidth',size(rgb,2));
setTag(t,'TileLength',32);
setTag(t,'TileWidth',32);
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);
setTag(t,'JPEGColorMode',Tiff.JPEGColorMode.RGB);
setTag(t,'JPEGQuality',75);

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

write(t,rgb);
close(t);

% Copyright 2012 The MathWorks, Inc.