I found something that fixes the issue, but I'm not 100% sure why it works. In any case, I have included the solution below. I will also be reporting this as a bug/documentation problem, as it is definitely not clear in the documentation of the TIFF class.
To fix this, I modified the changePage() function as follows:
function [] = changePage(obj, imageSize, tileSize)
bitsPerSample = obj.TiffObject.getTag('BitsPerSample');
sFormat = obj.TiffObject.getTag('SampleFormat');
compression = obj.TiffObject.getTag('Compression');
obj.TiffObject.writeDirectory();
obj.ImageSize = [imageSize(1), imageSize(2), 1];
obj.TileLength = tileSize(1);
obj.TileWidth = tileSize(2);
obj.TiffObject.setTag('ImageLength', obj.ImageSize(1));
obj.TiffObject.setTag('ImageWidth', obj.ImageSize(2));
obj.TiffObject.setTag('TileLength', obj.TileLength);
obj.TiffObject.setTag('TileWidth', obj.TileWidth);
obj.TiffObject.setTag('Photometric', Tiff.Photometric.MinIsBlack);
obj.TiffObject.setTag('BitsPerSample', bitsPerSample);
obj.TiffObject.setTag('SampleFormat', sFormat);
obj.TiffObject.setTag('SamplesPerPixel', 1);
obj.TiffObject.setTag('PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);
obj.TiffObject.setTag('Compression',compression);
end
Essentially, I added the rest of the tags that came from the first page. As soon as call to setTag() for Photometric is called, the TiffObject magically contains the entries of previous setTag calls. At first I thought it was because Photometric makes another call to the Tiff class within it, so I rearranged the setTag calls like this:
obj.TiffObject.setTag('Photometric', Tiff.Photometric.MinIsBlack);
obj.TiffObject.setTag('BitsPerSample', bitsPerSample);
obj.TiffObject.setTag('SampleFormat', sFormat);
obj.TiffObject.setTag('SamplesPerPixel', 1);
obj.TiffObject.setTag('PlanarConfiguration', Tiff.PlanarConfiguration.Chunky);
obj.TiffObject.setTag('Compression',compression);
obj.TiffObject.setTag('ImageLength', obj.ImageSize(1));
obj.TiffObject.setTag('ImageWidth', obj.ImageSize(2));
obj.TiffObject.setTag('TileLength', obj.TileLength);
obj.TiffObject.setTag('TileWidth', obj.TileWidth);
Here, the TiffObject did not update until the setTag for ImageLength was called. What this tells me is that in order to update the TiffObject, a certain number of bytes must be written to the metadata. This is not at all clear in the documentation. I thought that rewriteDirectory() would work, but it does not update the TiffObject. It may actually write out to file like it says, but without the corresponding update to the TiffObject the code will crash the next time a tile is written, because it thinks the TileLength/TileWidth have not been set.
I am still open to answers on better ways to do my multi-resolution file writing, as this seems a bit haphazard, so feel free to add.
Best Answer