Mercurial > octave-libtiff
view scripts/io/Tiff.m @ 31139:3431a15b2c75
Tiff.m: refactored tests to use functions to reduce code repetition.
author | magedrifaat <magedrifaat@gmail.com> |
---|---|
date | Thu, 28 Jul 2022 20:50:41 +0200 |
parents | 68762676dab1 |
children | 5f70efad6e2c |
line wrap: on
line source
classdef Tiff properties (Constant = true) TagID = struct( "SubFileType", 254, "ImageWidth", 256, "ImageLength", 257, "BitsPerSample", 258, "Compression", 259, "Photometric", 262, "Thresholding", 263, "FillOrder", 266, "DocumentName", 269, "ImageDescription", 270, "Make", 271, "Model", 272, "StripOffsets", 273, "Orientation", 274, "SamplesPerPixel", 277, "RowsPerStrip", 278, "StripByteCounts", 279, "MinSampleValue", 280, "MaxSampleValue", 281, "XResolution", 282, "YResolution", 283, "PlanarConfiguration", 284, "PageName", 285, "XPosition", 286, "YPosition", 287, "GrayResponseUnit", 290, 'GrayResponseCurve', 291, "Group3Options", 292, "Group4Options", 293, "ResolutionUnit", 296, "PageNumber", 297, "TransferFunction", 301, "Software", 305, "DateTime", 306, "Artist", 315, "HostComputer", 316, "WhitePoint", 318, "PrimaryChromaticities", 319, "ColorMap", 320, "HalfToneHints", 321, "TileWidth", 322, "TileLength", 323, "TileOffsets", 324, "TileByteCounts", 325, "SubIFD", 330, "InkSet", 332, "InkNames", 333, "NumberOfInks", 334, "DotRange", 336, "TargetPrinter", 337, "ExtraSamples", 338, "SampleFormat", 339, "SMinSampleValue", 340, "SMaxSampleValue", 341, "YCbCrCoefficients", 529, "YCbCrSubSampling", 530, "YCbCrPositioning", 531, "ReferenceBlackWhite", 532, "XMP", 700, "ImageDepth", 32997, "Copyright", 33432, "RichTIFFIPTC", 33723, "Photoshop", 34377, "ICCProfile", 34675, "SToNits", 37439, "JPEGQuality", 65537, "JPEGColorMode", 65538, "ZipQuality", 65557, "SGILogDataFmt", 65560 ) endproperties properties (Access = private) tiff_handle endproperties methods function t = Tiff (filename, mode="r") if (nargin == 0 || nargin > 2) % print_usage(); error("Usage: Tiff(filename[, mode])"); endif t.tiff_handle = __open_tiff__ (filename, mode); endfunction function close (t) __close_tiff__ (t.tiff_handle); endfunction function tag = getTag (t, tag_name) tag = __tiff_get_tag__ (t.tiff_handle, tag_name); endfunction function setTag (t, tag_name, tag_value) __tiff_set_tag__ (t.tiff_handle, tag_name, tag_value); endfunction function argout = read (t) argout = __tiff_read__ (t.tiff_handle); endfunction function writeEncodedStrip (t, stripNumber, imageData) __tiff_write_encoded_strip__ (t.tiff_handle, stripNumber, imageData); endfunction % TODO(maged): add documentation and make print_usage work endmethods endclassdef %!function set_configs (img, height, width, channels=-1, bit_depth=-1, rows_per_strip=-1) %! setTag (img, "ImageLength", height); %! setTag (img, "ImageWidth", width); %! if channels != -1 %! setTag (img, "SamplesPerPixel", channels); %! endif %! if bit_depth != -1 %! setTag (img, "BitsPerSample", bit_depth); %! endif %! if rows_per_strip != -1 %! setTag(img, "RowsPerStrip", rows_per_strip); %! endif %!endfunction %!function file_wrapper (fn) %! filename = [tempname() ".tif"]; %! unwind_protect %! fn (filename); %! unwind_protect_cleanup %! unlink (filename); %! end_unwind_protect %!endfunction %!function verify_data (filename, data, ex_size) %! img = Tiff (filename, "r"); %! data2 = img.read (); %! assert (size (data2), ex_size); %! assert (data2, resize (data, size (data2))); %! img.close (); %!endfunction ## test one-pixel grayscale image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 1, 1, 1, 8); %! data = uint8 (randi (intmax ("uint8"), 1, 1)); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [1, 1]); %! endfunction %! file_wrapper (@test_fn); ## test failure to write to image without dimensions %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! data = uint8 (randi (intmax ("uint8"), 1, 1)); %! fail ("writeEncodedStrip (img, 1, data)", "Failed to read image width"); %! img.close (); %! endfunction %! file_wrapper (@test_fn); ## test one row grayscale image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 1, 10, 1, 8); %! data = uint8 (reshape (1:10, [1, 10])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [1, 10]); %! endfunction %! file_wrapper (@test_fn); ## test wrong size of row %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 1, 10, 1, 8); %! data = uint8 (reshape (1:9, [1, 9])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [1, 10]); %! endfunction %! file_wrapper (@test_fn); ## test one strip grayscale image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 10, 10, 1, 8); %! data = uint8 (reshape (1:100, [10, 10])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [10, 10]); %! endfunction %! file_wrapper (@test_fn); ## test wrong height of strip %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 10, 10, 1, 8); %! data = uint8 (reshape (1:110, [11, 10])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [10, 10]); %! endfunction %! file_wrapper (@test_fn); ## test one strip RGB image chunky planes (RGBRGBRGB) %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 10, 10, 3, 8); %! setTag(img, "PlanarConfiguration", 1); %! setTag(img, "PhotometricInterpretation", 2); %! data = uint8 (reshape (1:300, [10, 10, 3])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [10, 10, 3]); %! endfunction %! file_wrapper (@test_fn); ## test wrong number of channels %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 10, 10, 3, 8); %! setTag(img, "PlanarConfiguration", 1); %! setTag(img, "PhotometricInterpretation", 2); %! data = uint8 (reshape (1:400, [10, 10, 4])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [10, 10, 3]); %! endfunction %! file_wrapper (@test_fn); ## test one strip RGB image separate planes (RRRGGGBBB) %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 10, 10, 3, 8); %! setTag(img, "PlanarConfiguration", 2); %! setTag(img, "PhotometricInterpretation", 2); %! data = uint8 (reshape (1:300, [10, 10, 3])); %! for i = 1:3 %! writeEncodedStrip (img, i, data(:,:,i)); %! endfor %! img.close (); %! verify_data (filename, data, [10, 10, 3]); %! endfunction %! file_wrapper (@test_fn); ## test 16-bit grayscale image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 20, 20, 1, 16); %! data = uint16 (reshape (1:400, [20, 20])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [20, 20]); %! endfunction %! file_wrapper (@test_fn); ## test 32-bit grayscale image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 20, 20, 1, 32); %! data = uint32 (reshape (1:400, [20, 20])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [20, 20]); %! endfunction %! file_wrapper (@test_fn); ## test 16-bit RGB image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 20, 20, 3, 16); %! setTag(img, "PlanarConfiguration", 1); %! setTag(img, "PhotometricInterpretation", 2); %! data = uint16 (reshape (1:1200, [20, 20, 3])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [20, 20, 3]); %! endfunction %! file_wrapper (@test_fn); ## test 32-bit RGB image %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 20, 20, 3, 32); %! setTag(img, "PlanarConfiguration", 1); %! setTag(img, "PhotometricInterpretation", 2); %! data = uint32 (reshape (1:1200, [20, 20, 3])); %! writeEncodedStrip (img, 1, data); %! img.close (); %! verify_data (filename, data, [20, 20, 3]); %! endfunction %! file_wrapper (@test_fn); ## test failure data-type and bit-depth mismatch %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 20, 20, 3, 16); %! setTag(img, "PlanarConfiguration", 1); %! setTag(img, "PhotometricInterpretation", 2); %! data = uint8 (reshape (1:1200, [20, 20, 3])); %! fail ("writeEncodedStrip (img, 1, data)", "Only uint16 data is allowed for uint images with bit depth of 16"); %! img.close (); %! endfunction %! file_wrapper (@test_fn); ## test failure writing to img read-only file %!testif HAVE_TIFF %! function test_fn (filename) %! img = Tiff (filename, "w"); %! set_configs (img, 1, 1, 1, 8); %! data = uint8 (1); %! writeEncodedStrip (img, 1, data); %! img.close(); %! img = Tiff (filename, "r"); %! fail ("writeEncodedStrip(img, 1, [1])", "Can't write data to a file opened in read-only mode"); %! img.close(); %! endfunction %! file_wrapper (@test_fn);