Mercurial > octave-libtiff
comparison scripts/io/Tiff.m @ 31171:8bf3fa6b6977
Tiff: added readRGBAStrip and readRGBATile methods
* __tiff__.cc: added internal functions for reading strips and tiles
using LibTIFF's RGBA interface.
* Tiff.m: added readRGBAStrip and readRGBATile methods to the Tiff class
and added unit tests for the ne methods.
author | magedrifaat <magedrifaat@gmail.com> |
---|---|
date | Sun, 14 Aug 2022 02:40:03 +0200 |
parents | 72a159bc5a4c |
children | 3f5f1404af8a |
comparison
equal
deleted
inserted
replaced
31170:72a159bc5a4c | 31171:8bf3fa6b6977 |
---|---|
258 error ("Image file was closed"); | 258 error ("Image file was closed"); |
259 endif | 259 endif |
260 [RGB, alpha] = __tiff_read_rgba_image__ (t.tiff_handle); | 260 [RGB, alpha] = __tiff_read_rgba_image__ (t.tiff_handle); |
261 endfunction | 261 endfunction |
262 | 262 |
263 function [RGB, alpha] = readRGBAStrip (t, row) | |
264 if (t.closed) | |
265 error ("Image file was closed"); | |
266 endif | |
267 [RGB, alpha] = __tiff_read_rgba_strip__ (t.tiff_handle, row); | |
268 endfunction | |
269 | |
270 function [RGB, alpha] = readRGBATile (t, row, col) | |
271 if (t.closed) | |
272 error ("Image file was closed"); | |
273 endif | |
274 [RGB, alpha] = __tiff_read_rgba_tile__ (t.tiff_handle, row, col); | |
275 endfunction | |
276 | |
263 function write (t, imageData) | 277 function write (t, imageData) |
264 if (t.closed) | 278 if (t.closed) |
265 error ("Image file was closed"); | 279 error ("Image file was closed"); |
266 endif | 280 endif |
267 __tiff_write__ (t.tiff_handle, imageData); | 281 __tiff_write__ (t.tiff_handle, imageData); |
1032 %! ## This is need for the tag to take effect, should be | 1046 %! ## This is need for the tag to take effect, should be |
1033 %! ## replaced by rewriteDirectory | 1047 %! ## replaced by rewriteDirectory |
1034 %! writeEncodedStrip (img, 1, uint8 (reshape (1:20, [2, 10]))); | 1048 %! writeEncodedStrip (img, 1, uint8 (reshape (1:20, [2, 10]))); |
1035 %! assert (computeStrip (img, 1, 2), 6); | 1049 %! assert (computeStrip (img, 1, 2), 6); |
1036 %! assert (computeStrip (img, 100, 1), 5); | 1050 %! assert (computeStrip (img, 100, 1), 5); |
1037 %! img = Tiff ("test.tif", "w"); | 1051 %! img = Tiff (filename, "w"); |
1038 %! setTag (img, "TileWidth", 16); | 1052 %! setTag (img, "TileWidth", 16); |
1039 %! setTag (img, "TileLength", 16); | 1053 %! setTag (img, "TileLength", 16); |
1040 %! fail ("computeStrip (img, 1, 1)", "The image is tiled not stripped"); | 1054 %! fail ("computeStrip (img, 1, 1)", "The image is tiled not stripped"); |
1041 %! endfunction | 1055 %! endfunction |
1042 %! file_wrapper (@test_fn); | 1056 %! file_wrapper (@test_fn); |
1060 %! ## This is need for the tag to take effect, should be | 1074 %! ## This is need for the tag to take effect, should be |
1061 %! ## replaced by rewriteDirectory | 1075 %! ## replaced by rewriteDirectory |
1062 %! writeEncodedTile (img, 1, uint8 (reshape (1:256, [16, 16]))); | 1076 %! writeEncodedTile (img, 1, uint8 (reshape (1:256, [16, 16]))); |
1063 %! assert (computeTile (img, [1, 1], 2), 5); | 1077 %! assert (computeTile (img, [1, 1], 2), 5); |
1064 %! assert (computeTile (img, [100, 100], 1), 4); | 1078 %! assert (computeTile (img, [100, 100], 1), 4); |
1065 %! img = Tiff ("test.tif", "w"); | 1079 %! img = Tiff (filename, "w"); |
1066 %! fail ("computeTile (img, 1, 1)", "The image is stripped not tiled"); | 1080 %! fail ("computeTile (img, 1, 1)", "The image is stripped not tiled"); |
1067 %! endfunction | 1081 %! endfunction |
1068 %! file_wrapper (@test_fn); | 1082 %! file_wrapper (@test_fn); |
1069 | 1083 |
1070 ## test write method one pixel bilevel image | 1084 ## test write method one pixel bilevel image |
1492 %! [rgb, alpha] = readRGBAImage (img); | 1506 %! [rgb, alpha] = readRGBAImage (img); |
1493 %! assert (rgb, data(:,:,1:3)); | 1507 %! assert (rgb, data(:,:,1:3)); |
1494 %! assert (alpha, data(:,:,4)); | 1508 %! assert (alpha, data(:,:,4)); |
1495 %! endfunction | 1509 %! endfunction |
1496 %! file_wrapper (@test_fn); | 1510 %! file_wrapper (@test_fn); |
1511 | |
1512 ## test readRGBAStrip | |
1513 %!testif HAVE_TIFF | |
1514 %! function test_fn (filename) | |
1515 %! img = Tiff (filename, "w"); | |
1516 %! setTag(img, struct ( | |
1517 %! "ImageLength", 10, "ImageWidth", 10, | |
1518 %! "BitsPerSample", 8, "SamplesPerPixel", 3, | |
1519 %! "PhotometricInterpretation", 2, | |
1520 %! "PlanarConfiguration", 1, | |
1521 %! "RowsPerStrip", 3 | |
1522 %! )); | |
1523 %! data = uint8 (randi ([0,255], [10, 10, 3])); | |
1524 %! write (img, data); | |
1525 %! [rgb, alpha] = readRGBAStrip (img, 1); | |
1526 %! assert (rgb, data(1:3,:,:)); | |
1527 %! assert (alpha, uint8 (repmat ([255], [3, 10]))); | |
1528 %! endfunction | |
1529 %! file_wrapper (@test_fn); | |
1530 | |
1531 ## test readRGBAStrip boundary strip | |
1532 %!testif HAVE_TIFF | |
1533 %! function test_fn (filename) | |
1534 %! img = Tiff (filename, "w"); | |
1535 %! setTag(img, struct ( | |
1536 %! "ImageLength", 10, "ImageWidth", 10, | |
1537 %! "BitsPerSample", 8, "SamplesPerPixel", 3, | |
1538 %! "PhotometricInterpretation", 2, | |
1539 %! "PlanarConfiguration", 1, | |
1540 %! "RowsPerStrip", 3 | |
1541 %! )); | |
1542 %! data = uint8 (randi ([0,255], [10, 10, 3])); | |
1543 %! write (img, data); | |
1544 %! [rgb, alpha] = readRGBAStrip (img, 10); | |
1545 %! assert (rgb, data(10,:,:)); | |
1546 %! assert (alpha, uint8 (repmat ([255], [1, 10]))); | |
1547 %! endfunction | |
1548 %! file_wrapper (@test_fn); | |
1549 | |
1550 ## test readRGBAStrip with alpha | |
1551 %!testif HAVE_TIFF | |
1552 %! function test_fn (filename) | |
1553 %! img = Tiff (filename, "w"); | |
1554 %! setTag(img, struct ( | |
1555 %! "ImageLength", 10, "ImageWidth", 10, | |
1556 %! "BitsPerSample", 8, "SamplesPerPixel", 4, | |
1557 %! "PhotometricInterpretation", 2, | |
1558 %! "PlanarConfiguration", 1, | |
1559 %! "RowsPerStrip", 3, | |
1560 %! "ExtraSamples", 1 | |
1561 %! )); | |
1562 %! data = uint8 (randi ([0,255], [10, 10, 4])); | |
1563 %! write (img, data); | |
1564 %! [rgb, alpha] = readRGBAStrip (img, 1); | |
1565 %! assert (rgb, data(1:3,:, 1:3)); | |
1566 %! assert (alpha, data (1:3, :, 4)); | |
1567 %! endfunction | |
1568 %! file_wrapper (@test_fn); | |
1569 | |
1570 ## test readRGBATile | |
1571 %!testif HAVE_TIFF | |
1572 %! function test_fn (filename) | |
1573 %! img = Tiff (filename, "w"); | |
1574 %! setTag(img, struct ( | |
1575 %! "ImageLength", 40, "ImageWidth", 40, | |
1576 %! "BitsPerSample", 8, "SamplesPerPixel", 3, | |
1577 %! "PhotometricInterpretation", 2, | |
1578 %! "PlanarConfiguration", 1, | |
1579 %! "TileLength", 16, "TileWidth", 32 | |
1580 %! )); | |
1581 %! data = uint8 (randi ([0,255], [40, 40, 3])); | |
1582 %! write (img, data); | |
1583 %! [rgb, alpha] = readRGBATile (img, 1, 1); | |
1584 %! assert (rgb, data(1:16,1:32,:)); | |
1585 %! assert (alpha, uint8 (repmat ([255], [16, 32]))); | |
1586 %! endfunction | |
1587 %! file_wrapper (@test_fn); | |
1588 | |
1589 ## test readRGBATile boundary tile | |
1590 %!testif HAVE_TIFF | |
1591 %! function test_fn (filename) | |
1592 %! img = Tiff (filename, "w"); | |
1593 %! setTag(img, struct ( | |
1594 %! "ImageLength", 40, "ImageWidth", 40, | |
1595 %! "BitsPerSample", 8, "SamplesPerPixel", 3, | |
1596 %! "PhotometricInterpretation", 2, | |
1597 %! "PlanarConfiguration", 1, | |
1598 %! "TileLength", 16, "TileWidth", 32 | |
1599 %! )); | |
1600 %! data = uint8 (randi ([0,255], [40, 40, 3])); | |
1601 %! write (img, data); | |
1602 %! [rgb, alpha] = readRGBATile (img, 40, 40); | |
1603 %! assert (rgb, data(33:end,33:end,:)); | |
1604 %! assert (alpha, uint8 (repmat ([255], [8, 8]))); | |
1605 %! endfunction | |
1606 %! file_wrapper (@test_fn); | |
1607 | |
1608 ## test readRGBATile ith alpha | |
1609 %!testif HAVE_TIFF | |
1610 %! function test_fn (filename) | |
1611 %! img = Tiff (filename, "w"); | |
1612 %! setTag(img, struct ( | |
1613 %! "ImageLength", 40, "ImageWidth", 40, | |
1614 %! "BitsPerSample", 8, "SamplesPerPixel", 4, | |
1615 %! "PhotometricInterpretation", 2, | |
1616 %! "PlanarConfiguration", 1, | |
1617 %! "TileLength", 16, "TileWidth", 32, | |
1618 %! "ExtraSamples", 1 | |
1619 %! )); | |
1620 %! data = uint8 (randi ([0,255], [40, 40, 4])); | |
1621 %! write (img, data); | |
1622 %! [rgb, alpha] = readRGBATile (img, 1, 1); | |
1623 %! assert (rgb, data(1:16,1:32,1:3)); | |
1624 %! assert (alpha, data(1:16,1:32,4)); | |
1625 %! endfunction | |
1626 %! file_wrapper (@test_fn); |