annotate libinterp/corefcn/__tiff__.cc @ 31162:28817158ca86

Tiff: changed write functions to use fcntl wrappers.
author magedrifaat <magedrifaat@gmail.com>
date Wed, 10 Aug 2022 20:46:56 +0200
parents b731c8f6db95
children d701c6a4cda1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1 #if defined (HAVE_CONFIG_H)
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
2 # include "config.h"
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
3 #endif
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
4
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
5 #include <string>
31094
ab5b33e447b0 Modified getTag to account for different tag data types and multivalued tags, the current implementation is still buggy for most tags and data types
magedrifaat <magedrifaat@gmail.com>
parents: 31093
diff changeset
6 #include <iostream>
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
7
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
8 #include "defun.h"
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
9 #include "ov.h"
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
10 #include "ovl.h"
31093
e2bed4daae82 Fix typo in module-files
magedrifaat <magedrifaat@gmail.com>
parents: 31092
diff changeset
11 #include "error.h"
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
12
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
13 #include "errwarn.h"
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
14
31162
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
15 #include "fcntl-wrappers.h"
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
16
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
17 #if defined (HAVE_TIFF)
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
18 # include <tiffio.h>
31161
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
19 TIFFErrorHandler tiff_default_handler = NULL;
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
20 #endif
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
21
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
22 namespace octave
31099
6fc4bf5e14e1 Cleaned up the interface
magedrifaat <magedrifaat@gmail.com>
parents: 31098
diff changeset
23 {
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
24 #if defined (HAVE_TIFF)
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
25
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
26 struct tiff_image_data
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
27 {
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
28 public:
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
29 uint32_t width;
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
30 uint32_t height;
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
31 uint16_t samples_per_pixel;
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
32 uint16_t bits_per_sample;
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
33 uint16_t planar_configuration;
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
34 uint16_t is_tiled;
31122
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
35
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
36 tiff_image_data (TIFF *tif)
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
37 {
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
38 if (! TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &width))
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
39 error ("Failed to read image width");
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
40
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
41 if (! TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &height))
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
42 error ("Failed to read image height");
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
43
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
44 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL,
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
45 &samples_per_pixel))
31122
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
46 error ("Failed to read the SamplesPerPixel tag");
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
47
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
48 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
49 &bits_per_sample))
31122
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
50 error ("Failed to read the BitsPerSample tag");
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
51
31147
7af78a63d3c3 Tiff.m: added test for writing and reading tiled images.
magedrifaat <magedrifaat@gmail.com>
parents: 31146
diff changeset
52 // TODO(maged): this doesn't really work for writing as LibTIFF will
7af78a63d3c3 Tiff.m: added test for writing and reading tiled images.
magedrifaat <magedrifaat@gmail.com>
parents: 31146
diff changeset
53 // refuse to write unless the tag is set
31131
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
54 if (! TIFFGetField (tif, TIFFTAG_PLANARCONFIG,
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
55 &planar_configuration))
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
56 // LibTIFF has a bug where it incorrectly returns 0 as a default
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
57 // value for PlanarConfiguration although the default value is 1
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
58 planar_configuration = 1;
31122
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
59
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
60 is_tiled = TIFFIsTiled(tif);
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
61 }
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
62 };
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
63
31162
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
64 void
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
65 check_readonly (TIFF *tif)
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
66 {
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
67 // This can't use O_RDONLY directly because its header file "fcntl.h"
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
68 // isn't available on Windows. The wrapper, however, seems to return
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
69 // the right thing even on Windows.
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
70 if (TIFFGetMode (tif) == octave_o_rdonly_wrapper ())
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
71 error ("Can't write data to a file opened in read-only mode");
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
72 }
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
73
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
74 // Error if status is not 1 (success status for TIFFGetField)
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
75 void
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
76 validate_tiff_get_field (bool status, void *p_to_free=NULL)
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
77 {
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
78 if (status != 1)
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
79 {
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
80 if (p_to_free != NULL)
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
81 _TIFFfree (p_to_free);
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
82 error ("Failed to read tag");
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
83 }
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
84 }
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
85
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
86 uint32_t get_rows_in_strip (uint32_t strip_no, uint32_t strip_count,
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
87 uint32_t rows_per_strip,
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
88 tiff_image_data *image_data)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
89 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
90 // Calculate the expected number of elements in the strip data array
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
91 // All strips have equal number of rows except strips at the bottom
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
92 // of the image can have less number of rows
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
93 uint32_t rows_in_strip = rows_per_strip;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
94 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
95 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
96 // All strips have equal number of rows excpet strips at the bottom
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
97 // of the image can have less number of rows
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
98 if (strip_no == strip_count - 1)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
99 rows_in_strip = image_data->height - rows_in_strip * strip_no;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
100 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
101 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
102 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
103 // For separate planes, we should check the last strip of each channel
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
104 uint32_t strips_per_channel
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
105 = strip_count / image_data->samples_per_pixel;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
106 for (uint32_t boundary_strip = strips_per_channel - 1;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
107 boundary_strip <= strip_count;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
108 boundary_strip += strips_per_channel)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
109 if (strip_no == boundary_strip)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
110 rows_in_strip = image_data->height
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
111 - rows_in_strip * (strip_no % strips_per_channel);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
112 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
113 else
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
114 error ("Planar Configuration not supported");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
115
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
116 return rows_in_strip;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
117 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
118
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
119 template <typename T>
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
120 octave_value
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
121 read_strip (TIFF *tif, uint32_t strip_no, tiff_image_data * image_data)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
122 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
123 // ASSUMES stripped image and strip_no is a valid zero-based strip
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
124 // index for the tif image
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
125
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
126 uint32_t rows_in_strip;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
127 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_ROWSPERSTRIP, &rows_in_strip))
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
128 error ("Failed to obtain a value for RowsPerStrip");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
129
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
130 if (rows_in_strip > image_data->height)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
131 rows_in_strip = image_data->height;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
132
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
133 uint32_t strip_count = TIFFNumberOfStrips (tif);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
134 rows_in_strip = get_rows_in_strip (strip_no, strip_count,
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
135 rows_in_strip, image_data);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
136 dim_vector strip_dims;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
137 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
138 strip_dims = dim_vector (image_data->samples_per_pixel,
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
139 image_data->width, rows_in_strip);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
140 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
141 strip_dims = dim_vector (image_data->width, rows_in_strip, 1);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
142 else
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
143 error ("Unsupported bit depth");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
144
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
145 T strip_data (strip_dims);
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
146 uint8_t *strip_fvec
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
147 = reinterpret_cast<uint8_t *> (strip_data.fortran_vec ());
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
148
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
149 if (image_data->bits_per_sample == 8
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
150 || image_data->bits_per_sample == 16
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
151 || image_data->bits_per_sample == 32
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
152 || image_data->bits_per_sample == 64)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
153 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
154 if (TIFFReadEncodedStrip (tif, strip_no, strip_fvec, -1) == -1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
155 error ("Failed to read strip data");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
156 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
157 else if (image_data->bits_per_sample == 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
158 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
159 if (image_data->samples_per_pixel != 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
160 error ("Bi-Level images must have one channel only");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
161
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
162 // Create a buffer to hold the packed strip data
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
163 // Unique pointers are faster than vectors for constant size buffers
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
164 std::unique_ptr<uint8_t []> strip_ptr
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
165 = std::make_unique<uint8_t []> (TIFFStripSize (tif));
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
166 uint8_t *strip_buf = strip_ptr.get ();
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
167 if (TIFFReadEncodedStrip (tif, strip_no, strip_buf, -1) == -1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
168 error ("Failed to read strip data");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
169
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
170 // According to the format specification, the row should be byte
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
171 // aligned so the number of bytes is rounded up to the nearest byte
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
172 uint32_t padded_width = (image_data->width + 7) / 8;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
173 // Packing the pixel data into bits
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
174 for (uint32_t row = 0; row < rows_in_strip; row++)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
175 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
176 for (uint32_t col = 0; col < image_data->width; col++)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
177 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
178 uint8_t shift = 7 - col % 8;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
179 strip_fvec[col] = (strip_buf[col / 8] >> shift) & 0x1;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
180 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
181 strip_fvec += image_data->width;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
182 strip_buf += padded_width;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
183 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
184 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
185 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
186 error ("Unsupported bit depth");
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
187
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
188 Array<octave_idx_type> perm (dim_vector (3, 1));
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
189 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
190 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
191 perm(0) = 2;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
192 perm(1) = 1;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
193 perm(2) = 0;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
194 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
195 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
196 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
197 perm(0) = 1;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
198 perm(1) = 0;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
199 perm(2) = 2;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
200 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
201
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
202 strip_data = strip_data.permute (perm);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
203 return octave_value (strip_data);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
204 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
205
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
206 template <typename T>
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
207 octave_value
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
208 read_tile (TIFF *tif, uint32_t tile_no, tiff_image_data *image_data)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
209 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
210 // ASSUMES tiled image and tile_no is a valid zero-based tile
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
211 // index for the tif image
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
212
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
213 // TODO(maged): refactor into a function?
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
214 uint32_t tile_width, tile_height;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
215 if (! TIFFGetField (tif, TIFFTAG_TILELENGTH, &tile_height))
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
216 error ("Filed to read tile length");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
217
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
218 if (! TIFFGetField (tif, TIFFTAG_TILEWIDTH, &tile_width))
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
219 error ("Filed to read tile length");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
220
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
221 if (tile_height == 0 || tile_height % 16 != 0
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
222 || tile_width == 0 || tile_width % 16 != 0)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
223 error ("Tile dimesion tags are invalid");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
224
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
225 dim_vector tile_dims;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
226 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
227 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
228 tile_dims = dim_vector (image_data->samples_per_pixel, tile_width,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
229 tile_height);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
230 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
231 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
232 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
233 tile_dims = dim_vector (tile_width, tile_height, 1);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
234 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
235 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
236 error ("Unsupported planar configuration");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
237
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
238 T tile_data (tile_dims);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
239 uint8_t *tile_fvec
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
240 = reinterpret_cast<uint8_t *> (tile_data.fortran_vec ());
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
241
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
242 if (image_data->bits_per_sample == 8
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
243 || image_data->bits_per_sample == 16
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
244 || image_data->bits_per_sample == 32
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
245 || image_data->bits_per_sample == 64)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
246 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
247 if (TIFFReadEncodedTile (tif, tile_no, tile_fvec, -1) == -1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
248 error ("Failed to read tile data");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
249 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
250 else if (image_data->bits_per_sample == 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
251 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
252 if (image_data->samples_per_pixel != 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
253 error ("Bi-Level images must have one channel only");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
254
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
255 // Create a buffer to hold the packed tile data
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
256 // Unique pointers are faster than vectors for constant size buffers
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
257 std::unique_ptr<uint8_t []> tile_ptr
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
258 = std::make_unique<uint8_t []> (TIFFTileSize (tif));
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
259 uint8_t *tile_buf = tile_ptr.get ();
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
260 if (TIFFReadEncodedTile (tif, tile_no, tile_buf, -1) == -1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
261 error ("Failed to read tile data");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
262
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
263 // unpack tile bits into output matrix cells
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
264 for (uint32_t row = 0; row < tile_height; row++)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
265 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
266 for (uint32_t col = 0; col < tile_width; col++)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
267 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
268 uint8_t shift = 7 - col % 8;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
269 tile_fvec[col] = (tile_buf [col / 8] >> shift) & 0x1;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
270 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
271 tile_fvec += tile_width;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
272 tile_buf += tile_width / 8;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
273 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
274 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
275 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
276 error ("Unsupported bit depth");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
277
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
278 Array<octave_idx_type> perm (dim_vector (3, 1));
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
279 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
280 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
281 perm(0) = 2;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
282 perm(1) = 1;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
283 perm(2) = 0;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
284 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
285 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
286 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
287 perm(0) = 1;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
288 perm(1) = 0;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
289 perm(2) = 2;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
290 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
291
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
292 tile_data = tile_data.permute (perm);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
293
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
294 // Get the actual tile dimensions
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
295 uint32_t tiles_across = (image_data->width + tile_width - 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
296 / tile_width;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
297 uint32_t tiles_down = (image_data->height + tile_height - 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
298 / tile_height;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
299 uint32_t corrected_width = tile_width;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
300 uint32_t corrected_height = tile_height;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
301 if (tile_no % tiles_across == tiles_across - 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
302 corrected_width = image_data->width - tile_width * (tiles_across - 1);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
303 if ((tile_no / tiles_across) % tiles_down == tiles_down - 1)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
304 corrected_height = image_data->height - tile_height * (tiles_down - 1);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
305
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
306 dim_vector corrected_dims;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
307 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
308 corrected_dims = dim_vector (corrected_height, corrected_width,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
309 image_data->samples_per_pixel);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
310 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
311 corrected_dims = dim_vector (corrected_height, corrected_width);
31159
e960e3a3b3f6 Tiff.m: added structs for tag value enums to the Tiff class.
magedrifaat <magedrifaat@gmail.com>
parents: 31158
diff changeset
312
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
313 tile_data.resize (corrected_dims);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
314
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
315 return octave_value (tile_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
316 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
317
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
318 template <typename T>
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
319 octave_value
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
320 read_strip_or_tile (TIFF *tif, uint32_t strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
321 tiff_image_data *image_data)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
322 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
323 if (image_data->is_tiled)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
324 return read_tile<T> (tif, strip_tile_no, image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
325 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
326 return read_strip<T> (tif, strip_tile_no, image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
327 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
328
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
329 octave_value
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
330 handle_read_strip_or_tile (TIFF *tif, uint32_t strip_tile_no)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
331 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
332 // Obtain all necessary tags
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
333 tiff_image_data image_data (tif);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
334
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
335 uint16_t sample_format;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
336 if (! TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format))
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
337 error ("Failed to obtain a value for sample format");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
338
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
339 if (sample_format == 3)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
340 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
341 if (image_data.bits_per_sample != 32
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
342 && image_data.bits_per_sample != 64)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
343 error ("Floating point images are only supported for bit depths of 32 and 64");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
344 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
345 else if (sample_format != 1 && sample_format != 4)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
346 error ("Unsupported sample format");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
347
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
348 switch (image_data.bits_per_sample)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
349 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
350 case 1:
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
351 return read_strip_or_tile<boolNDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
352 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
353 break;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
354 case 8:
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
355 return read_strip_or_tile<uint8NDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
356 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
357 break;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
358 case 16:
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
359 return read_strip_or_tile<uint16NDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
360 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
361 break;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
362 case 32:
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
363 if (sample_format == 3)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
364 return read_strip_or_tile<FloatNDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
365 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
366 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
367 return read_strip_or_tile<uint32NDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
368 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
369 break;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
370 case 64:
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
371 if (sample_format == 3)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
372 return read_strip_or_tile<NDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
373 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
374 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
375 return read_strip_or_tile<uint64NDArray> (tif, strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
376 &image_data);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
377 break;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
378 default:
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
379 error ("Unsupported bit depth");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
380 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
381 }
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
382
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
383 template <typename T>
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
384 octave_value
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
385 read_stripped_image (TIFF *tif, tiff_image_data *image_data)
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
386 {
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
387 typedef typename T::element_type P;
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
388
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
389 // For 1-bit and 4-bit images, each row must be byte aligned and padding
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
390 // is added to the end of the row to reach the byte mark.
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
391 // To facilitate reading the data, the matrix is defined with the padded
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
392 // size and the padding is removed at the end.
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
393 uint32_t padded_width = image_data->width;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
394 uint8_t remove_padding = 0;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
395 if ((image_data->bits_per_sample == 1 || image_data->bits_per_sample == 4)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
396 && padded_width % 8 != 0)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
397 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
398 padded_width += (8 - padded_width % 8) % 8;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
399 remove_padding = 1;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
400 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
401
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
402 // The matrix dimensions are defined in the order that corresponds to
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
403 // the order of strip data read from LibTIFF.
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
404 // At the end, the matrix is permutated to the order expected by Octave
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
405 T img;
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
406 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
407 img = T (dim_vector (image_data->samples_per_pixel, padded_width,
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
408 image_data->height));
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
409 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
410 img = T (dim_vector (padded_width, image_data->height,
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
411 image_data->samples_per_pixel));
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
412 else
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
413 error ("Unsupported Planar Configuration");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
414
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
415 P *img_fvec = img.fortran_vec ();
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
416
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
417 // Obtain the necessary data for handling the strips
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
418 uint32_t strip_count = TIFFNumberOfStrips (tif);
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
419
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
420 // Can't rely on StripByteCounts because in compressed images
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
421 // the byte count reflect the actual number of bytes stored
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
422 // in the file not the size of the uncompressed strip
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
423 int64_t strip_size;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
424 uint64_t written_size = 0;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
425 uint64_t image_size = padded_width * image_data->height
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
426 * image_data->samples_per_pixel
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
427 * sizeof (P);
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
428 for (uint32_t strip = 0; strip < strip_count; strip++)
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
429 {
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
430 // Read the strip data into the matrix directly
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
431 strip_size = TIFFReadEncodedStrip (tif, strip, img_fvec, -1);
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
432
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
433 // Check if the strip read failed.
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
434 if (strip_size == -1)
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
435 error ("Failed to read strip data");
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
436
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
437 // Check if the size being read exceeds the bounds of the matrix
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
438 // In case of a corrupt image with more data than needed
31129
dfab9c6982bf Tiff getTag: added support for single quotes string fo the tag name
magedrifaat <magedrifaat@gmail.com>
parents: 31128
diff changeset
439 // This is probably redundant as LibTIFF checks sizes internally
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
440 if (written_size + strip_size > image_size)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
441 error ("Strip data is larger than the image size");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
442
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
443 if (image_data->bits_per_sample == 1)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
444 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
445 if (image_data->samples_per_pixel != 1)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
446 error ("Bi-Level images must have one channel only");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
447
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
448 // The strip size is multiplied by 8 to reflect tha actual
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
449 // number of bytes written to the matrix since each byte
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
450 // in the original strip contains 8 pixels of data
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
451 strip_size *= 8;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
452
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
453 // Checking bounds again with the new size
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
454 if (written_size + strip_size > image_size)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
455 error ("Strip data is larger than the image size");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
456
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
457 // Iterate over the memory region backwards to expand the bits
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
458 // to their respective bytes without overwriting the read data
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
459 for (int64_t pixel = strip_size - 1; pixel >= 0; pixel--)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
460 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
461 // TODO(maged): is it necessary to check FillOrder?
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
462 uint8_t bit_number = 7 - pixel % 8;
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
463 uint8_t * img_u8 = reinterpret_cast<uint8_t *> (img_fvec);
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
464 img_fvec[pixel] = (img_u8[pixel / 8] >> bit_number) & 0x01;
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
465 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
466 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
467 else if (image_data->bits_per_sample == 4)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
468 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
469 if (image_data->samples_per_pixel != 1)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
470 error ("4-bit images are only supported for grayscale");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
471
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
472 // Strip size is multplied by as each byte contains 2 pixels
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
473 // and each pixel is later expanded into its own byte
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
474 strip_size *= 2;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
475
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
476 // Checking bounds again with the ne size
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
477 if (written_size + strip_size > image_size)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
478 error ("Strip data is larger than the image size");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
479
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
480 // Iterate over the memory region backwards to expand the nibbles
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
481 // to their respective bytes without overwriting the read data
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
482 for (int64_t pixel = strip_size - 1; pixel >= 0; pixel--)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
483 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
484 uint8_t shift = pixel % 2 == 0? 4: 0;
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
485 uint8_t * img_u8 = reinterpret_cast<uint8_t *> (img_fvec);
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
486 img_fvec[pixel] = (img_u8[pixel / 2] >> shift) & 0x0F;
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
487 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
488 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
489 else if (image_data->bits_per_sample != 8 &&
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
490 image_data->bits_per_sample != 16 &&
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
491 image_data->bits_per_sample != 32 &&
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
492 image_data->bits_per_sample != 64)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
493 error ("Unsupported bit depth");
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
494
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
495 // Advance the pointer by the amount of bytes read
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
496 img_fvec
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
497 = reinterpret_cast<P *> (reinterpret_cast <uint8_t *> (img_fvec)
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
498 + strip_size);
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
499 written_size += strip_size;
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
500 }
31119
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
501
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
502 // The matrices are permutated back to the shape expected by Octave
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
503 // which is height x width x channels
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
504 Array<octave_idx_type> perm (dim_vector (3, 1));
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
505 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
506 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
507 perm(0) = 2;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
508 perm(1) = 1;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
509 perm(2) = 0;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
510 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
511 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
512 {
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
513 perm(0) = 1;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
514 perm(1) = 0;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
515 perm(2) = 2;
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
516 }
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
517
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
518 img = img.permute (perm);
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
519
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
520 if (remove_padding)
dbca50246dfc Tiff read: changed logic to matrix operations instead of pointer math
magedrifaat <magedrifaat@gmail.com>
parents: 31118
diff changeset
521 img.resize (dim_vector (image_data->height, image_data->width));
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
522
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
523 return octave_value (img);
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
524 }
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
525
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
526 template <typename T>
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
527 octave_value
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
528 read_tiled_image (TIFF *tif, tiff_image_data *image_data)
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
529 {
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
530 typedef typename T::element_type P;
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
531
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
532 // Obtain the necessary data for handling the tiles
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
533 uint32_t tile_width, tile_height;
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
534 validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_TILEWIDTH,
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
535 &tile_width));
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
536 validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_TILELENGTH,
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
537 &tile_height));
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
538 uint32_t tile_count = TIFFNumberOfTiles (tif);
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
539 uint32_t tiles_across = (image_data->width + tile_width - 1)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
540 / tile_width;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
541 uint32_t tiles_down = (image_data->height + tile_height - 1)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
542 / tile_height;
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
543
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
544 T img;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
545 // The matrix dimensions are defined in the order that corresponds to
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
546 // the order of the tile data read from LibTIFF.
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
547 // At the end, the matrix is permutated, reshaped and resized to be in the
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
548 // shape expected by Octave
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
549 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
550 img = T (dim_vector (image_data->samples_per_pixel, tile_width,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
551 tile_height, tiles_across, tiles_down));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
552 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
553 img = T (dim_vector (tile_width, tile_height, tiles_across, tiles_down,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
554 image_data->samples_per_pixel));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
555 else
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
556 error ("Unsupported Planar Configuration");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
557
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
558 P *img_fvec = img.fortran_vec ();
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
559
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
560 // image_size is calculated from the tile data and not from the
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
561 // image parameters to account for the additional padding in the
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
562 // boundary tiles
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
563 uint64_t image_size = tile_width * tile_height * tile_count * sizeof(P);
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
564 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
565 image_size *= image_data->samples_per_pixel;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
566
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
567 // Can't rely on TileByteCounts because compressed images will report
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
568 // the number of bytes present in the file as opposed to the actual
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
569 // number of bytes of uncompressed data that is needed here
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
570 int64_t tile_size;
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
571 uint64_t written_size = 0;
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
572 for (uint32_t tile = 0; tile < tile_count; tile++)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
573 {
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
574 tile_size = TIFFReadEncodedTile(tif, tile, img_fvec, -1);
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
575
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
576 if (tile_size == -1)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
577 error ("Failed to read tile data");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
578
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
579 // Checking if the read bytes would exceed the size of the matrix
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
580 if (tile_size + written_size > image_size)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
581 error ("Tile data is larger than image size");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
582
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
583 if (image_data->bits_per_sample == 1)
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
584 {
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
585 if (image_data->samples_per_pixel != 1)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
586 error ("Bi-Level images must have one channel only");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
587
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
588 // The tile size is multiplied by 8 to reflect tha actual
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
589 // number of bytes written to the matrix since each byte
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
590 // in the original tile contains 8 pixels of data
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
591 tile_size *= 8;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
592
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
593 // Checking bounds again with the new size
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
594 if (written_size + tile_size > image_size)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
595 error ("Tile data is larger than the image size");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
596
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
597 // Iterate over the memory region backwards to expand the bits
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
598 // to their respective bytes without overwriting the read data
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
599 for (int64_t pixel = tile_size - 1; pixel >= 0; pixel--)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
600 {
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
601 // TODO(maged): is it necessary to check FillOrder?
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
602 uint8_t bit_number = 7 - pixel % 8;
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
603 uint8_t * img_u8 = reinterpret_cast<uint8_t *> (img_fvec);
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
604 img_fvec[pixel]= (img_u8[pixel / 8] >> bit_number) & 0x01;
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
605 }
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
606 }
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
607 else if (image_data->bits_per_sample == 4)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
608 {
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
609 if (image_data->samples_per_pixel != 1)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
610 error ("4-bit images are only supported for grayscale");
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
611
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
612 // tile size is multplied by as each byte contains 2 pixels
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
613 // and each pixel is later expanded into its own byte
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
614 tile_size *= 2;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
615
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
616 // Checking bounds again with the ne size
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
617 if (written_size + tile_size > image_size)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
618 error ("Tile data is larger than the image size");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
619
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
620 // Iterate over the memory region backwards to expand the nibbles
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
621 // to their respective bytes without overwriting the read data
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
622 for (int64_t pixel = tile_size - 1; pixel >= 0; pixel--)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
623 {
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
624 uint8_t shift = pixel % 2 == 0? 4: 0;
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
625 uint8_t * img_u8 = reinterpret_cast<uint8_t *> (img_fvec);
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
626 img_fvec[pixel] = (img_u8[pixel / 2] >> shift) & 0x0F;
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
627 }
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
628 }
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
629 else if (image_data->bits_per_sample != 8 &&
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
630 image_data->bits_per_sample != 16 &&
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
631 image_data->bits_per_sample != 32 &&
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
632 image_data->bits_per_sample != 64)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
633 error ("Unsupported bit depth");
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
634
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
635 img_fvec
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
636 = reinterpret_cast<P *> (reinterpret_cast<uint8_t *> (img_fvec)
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
637 + tile_size);
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
638 written_size += tile_size;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
639 }
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
640
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
641 // The data is now in the matrix but in a different order than expected
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
642 // by Octave and with additional padding in boundary tiles.
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
643 // To get it to the right order, the dimensions are permutated to
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
644 // align tiles to their correct grid, then reshaped to remove the
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
645 // extra dimensions (tiles_across, tiles_down), then resized to
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
646 // remove any extra padding, and finally permutated to the correct
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
647 // order that is: height x width x channels
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
648 Array<octave_idx_type> perm1 (dim_vector (5, 1));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
649 Array<octave_idx_type> perm2 (dim_vector (3, 1));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
650 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
651 {
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
652 perm1(0) = 0;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
653 perm1(1) = 1;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
654 perm1(2) = 3;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
655 perm1(3) = 2;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
656 perm1(4) = 4;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
657 img = img.permute (perm1);
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
658
31120
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
659 img = img.reshape (dim_vector (image_data->samples_per_pixel,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
660 tile_width * tiles_across,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
661 tile_height * tiles_down));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
662
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
663 if (tile_width * tiles_across != image_data->width
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
664 || tile_height * tiles_down != image_data->height)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
665 img.resize (dim_vector (image_data->samples_per_pixel,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
666 image_data->width, image_data->height));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
667
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
668 perm2(0) = 2;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
669 perm2(1) = 1;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
670 perm2(2) = 0;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
671 img = img.permute (perm2);
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
672 }
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
673 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
674 {
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
675 perm1(0) = 0;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
676 perm1(1) = 2;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
677 perm1(2) = 1;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
678 perm1(3) = 3;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
679 perm1(4) = 4;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
680 img = img.permute (perm1);
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
681
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
682 img = img.reshape (dim_vector (tile_width * tiles_across,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
683 tile_height * tiles_down,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
684 image_data->samples_per_pixel));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
685
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
686 if (tile_width * tiles_across != image_data->width
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
687 || tile_height * tiles_down != image_data->height)
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
688 img.resize (dim_vector (image_data->width, image_data->height,
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
689 image_data->samples_per_pixel));
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
690
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
691 perm2(0) = 1;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
692 perm2(1) = 0;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
693 perm2(2) = 2;
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
694 img = img.permute (perm2);
46bb98cec195 Tiff read: implemented all cases for tiled images using matrix operations
magedrifaat <magedrifaat@gmail.com>
parents: 31119
diff changeset
695 }
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
696
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
697 return octave_value (img);
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
698 }
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
699
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
700 template <typename T>
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
701 octave_value
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
702 read_image (TIFF *tif, tiff_image_data *image_data)
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
703 {
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
704 if (image_data->is_tiled)
31118
f8be3654caef Tiff read: Support for tiled images with normal planar configuration
magedrifaat <magedrifaat@gmail.com>
parents: 31117
diff changeset
705 return read_tiled_image<T> (tif, image_data);
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
706 else
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
707 return read_stripped_image<T> (tif, image_data);
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
708 }
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
709
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
710 // Convert tag value to double
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
711 octave_value
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
712 interpret_scalar_tag_data (void *data, TIFFDataType tag_datatype)
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
713 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
714 double retval;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
715
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
716 switch (tag_datatype)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
717 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
718 case TIFF_BYTE:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
719 case TIFF_UNDEFINED:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
720 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
721 retval = static_cast<double> (*(reinterpret_cast<uint8_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
722 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
723 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
724 case TIFF_SHORT:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
725 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
726 retval = static_cast<double> (*(reinterpret_cast<uint16_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
727 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
728 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
729 case TIFF_LONG:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
730 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
731 retval = static_cast<double> (*(reinterpret_cast<uint32_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
732 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
733 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
734 case TIFF_LONG8:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
735 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
736 retval = static_cast<double> (*(reinterpret_cast<uint64_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
737 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
738 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
739 case TIFF_RATIONAL:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
740 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
741 error ("TIFF_RATIONAL should have at least 2 elements but got only 1");
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
742 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
743 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
744 case TIFF_SBYTE:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
745 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
746 retval = static_cast<double> (*(reinterpret_cast<int8_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
747 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
748 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
749 case TIFF_SSHORT:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
750 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
751 retval = static_cast<double> (*(reinterpret_cast<int16_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
752 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
753 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
754 case TIFF_SLONG:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
755 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
756 retval = static_cast<double> (*(reinterpret_cast<int32_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
757 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
758 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
759 case TIFF_SLONG8:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
760 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
761 retval = static_cast<double> (*(reinterpret_cast<int64_t *> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
762 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
763 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
764 case TIFF_FLOAT:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
765 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
766 retval = *(reinterpret_cast<float *> (data));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
767 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
768 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
769 case TIFF_DOUBLE:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
770 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
771 retval = *(reinterpret_cast<double *> (data));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
772 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
773 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
774 case TIFF_SRATIONAL:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
775 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
776 error ("TIFF_SRATIONAL should have at least 2 elements but got only 1");
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
777 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
778 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
779 case TIFF_IFD:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
780 case TIFF_IFD8:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
781 error ("Unimplemented IFFD data type");
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
782 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
783 default:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
784 error ("Unsupported tag data type");
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
785 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
786
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
787 return octave_value (retval);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
788 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
789
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
790 // Convert memory buffer into a suitable octave value
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
791 // depending on tag_datatype
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
792 octave_value
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
793 interpret_tag_data (void *data, uint32_t count, TIFFDataType tag_datatype)
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
794 {
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
795 octave_value retval;
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
796 // Apparently matlab converts scalar numerical values into double
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
797 // but doesn't do the same for arrays
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
798 if (count == 1 && tag_datatype != TIFF_ASCII)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
799 {
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
800 retval = interpret_scalar_tag_data (data, tag_datatype);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
801 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
802 else
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
803 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
804 dim_vector arr_dims (1, count);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
805
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
806 switch (tag_datatype)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
807 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
808 case TIFF_BYTE:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
809 case TIFF_UNDEFINED:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
810 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
811 uint8NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
812 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
813 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
814 arr(i) = (reinterpret_cast<uint8_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
815 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
816 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
817 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
818 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
819 case TIFF_ASCII:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
820 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
821 retval = octave_value (*(reinterpret_cast<char **> (data)));
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
822 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
823 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
824 case TIFF_SHORT:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
825 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
826 uint16NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
827 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
828 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
829 arr(i) = (reinterpret_cast<uint16_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
830 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
831 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
832 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
833 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
834 case TIFF_LONG:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
835 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
836 uint32NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
837 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
838 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
839 arr(i) = (reinterpret_cast<uint32_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
840 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
841 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
842 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
843 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
844 case TIFF_LONG8:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
845 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
846 uint64NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
847 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
848 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
849 arr(i) = (reinterpret_cast<uint64_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
850 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
851 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
852 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
853 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
854 case TIFF_RATIONAL:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
855 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
856 NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
857 for (uint32_t i = 0; i < count; i+=2)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
858 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
859 arr(i / 2) = static_cast<float> ((reinterpret_cast<uint32_t *> (data))[i])
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
860 / static_cast<float> ((reinterpret_cast<uint32_t *> (data))[i+1]);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
861 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
862 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
863 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
864 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
865 case TIFF_SBYTE:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
866 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
867 int8NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
868 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
869 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
870 arr(i) = (reinterpret_cast<int8_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
871 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
872 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
873 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
874 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
875 case TIFF_SSHORT:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
876 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
877 int16NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
878 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
879 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
880 arr(i) = (reinterpret_cast<int16_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
881 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
882 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
883 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
884 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
885 case TIFF_SLONG:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
886 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
887 int32NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
888 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
889 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
890 arr(i) = (reinterpret_cast<int32_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
891 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
892 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
893 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
894 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
895 case TIFF_SLONG8:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
896 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
897 int64NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
898 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
899 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
900 arr(i) = (reinterpret_cast<int64_t *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
901 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
902 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
903 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
904 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
905 case TIFF_FLOAT:
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
906 {
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
907 NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
908 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
909 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
910 arr(i) = (reinterpret_cast<float *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
911 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
912 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
913 break;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
914 }
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
915 case TIFF_DOUBLE:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
916 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
917 NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
918 for (uint32_t i = 0; i < count; i++)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
919 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
920 arr(i) = (reinterpret_cast<double *> (data))[i];
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
921 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
922 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
923 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
924 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
925 case TIFF_SRATIONAL:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
926 {
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
927 NDArray arr (arr_dims);
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
928 for (uint32_t i = 0; i < count; i+=2)
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
929 {
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
930 arr(i / 2) = static_cast<float> ((reinterpret_cast<int32_t *> (data))[i])
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
931 / static_cast<float> ((reinterpret_cast<int32_t *> (data))[i+1]);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
932 }
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
933 retval = octave_value (arr);
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
934 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
935 }
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
936 case TIFF_IFD:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
937 case TIFF_IFD8:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
938 // TODO(maged): implement IFD datatype?
31129
dfab9c6982bf Tiff getTag: added support for single quotes string fo the tag name
magedrifaat <magedrifaat@gmail.com>
parents: 31128
diff changeset
939 error ("Unimplemented IFD data type");
31105
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
940 break;
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
941 default:
7c5b8a294f60 getTag returns double for scalar values
magedrifaat <magedrifaat@gmail.com>
parents: 31104
diff changeset
942 error ("Unsupported tag data type");
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
943 }
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
944 }
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
945
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
946 return retval;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
947 }
31094
ab5b33e447b0 Modified getTag to account for different tag data types and multivalued tags, the current implementation is still buggy for most tags and data types
magedrifaat <magedrifaat@gmail.com>
parents: 31093
diff changeset
948
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
949 octave_value
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
950 get_scalar_field_data (TIFF *tif, const TIFFField *fip)
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
951 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
952 uint32_t tag_id = TIFFFieldTag (fip);
31097
75ab26f147a5 getTag: Implemented single array tags, only multi-array and special tags are unimplemented
magedrifaat <magedrifaat@gmail.com>
parents: 31096
diff changeset
953
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
954 // TIFFFieldReadCount returns VARIABLE for some scalar tags
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
955 // (e.g. Compression) But TIFFFieldPassCount seems consistent
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
956 // Since scalar tags are the last to be handled, any tag that
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
957 // require a count to be passed is an unsupported tag.
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
958 if (TIFFFieldPassCount (fip))
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
959 error ("Unsupported tag");
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
960
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
961 int type_size = TIFFDataWidth (TIFFFieldDataType (fip));
31129
dfab9c6982bf Tiff getTag: added support for single quotes string fo the tag name
magedrifaat <magedrifaat@gmail.com>
parents: 31128
diff changeset
962
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
963 void *data = _TIFFmalloc (type_size);
31131
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
964 if (tag_id == TIFFTAG_PLANARCONFIG)
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
965 {
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
966 // Workaround for a bug in LibTIFF where it incorrectly returns
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
967 // zero as the default value for PlanaConfiguration
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
968 if (! TIFFGetField(tif, tag_id, data))
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
969 *reinterpret_cast<uint16_t *> (data) = 1;
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
970 }
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
971 else
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
972 validate_tiff_get_field (TIFFGetFieldDefaulted (tif, tag_id, data), data);
7349994f30f8 Tiff: fixed the first test to use a single-pixel image
magedrifaat <magedrifaat@gmail.com>
parents: 31130
diff changeset
973
31130
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
974 octave_value tag_data_ov = interpret_tag_data (data, 1,
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
975 TIFFFieldDataType (fip));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
976 _TIFFfree (data);
31094
ab5b33e447b0 Modified getTag to account for different tag data types and multivalued tags, the current implementation is still buggy for most tags and data types
magedrifaat <magedrifaat@gmail.com>
parents: 31093
diff changeset
977
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
978 return tag_data_ov;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
979 }
31094
ab5b33e447b0 Modified getTag to account for different tag data types and multivalued tags, the current implementation is still buggy for most tags and data types
magedrifaat <magedrifaat@gmail.com>
parents: 31093
diff changeset
980
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
981 octave_value
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
982 get_array_field_data (TIFF *tif, const TIFFField *fip, uint32_t array_size)
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
983 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
984 void *data;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
985 validate_tiff_get_field (TIFFGetField (tif, TIFFFieldTag (fip), &data));
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
986
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
987 return interpret_tag_data (data, array_size, TIFFFieldDataType (fip));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
988 }
31097
75ab26f147a5 getTag: Implemented single array tags, only multi-array and special tags are unimplemented
magedrifaat <magedrifaat@gmail.com>
parents: 31096
diff changeset
989
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
990 octave_value
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
991 get_field_data (TIFF *tif, const TIFFField *fip)
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
992 {
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
993 octave_value tag_data_ov;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
994 uint32_t tag_id = TIFFFieldTag (fip);
31097
75ab26f147a5 getTag: Implemented single array tags, only multi-array and special tags are unimplemented
magedrifaat <magedrifaat@gmail.com>
parents: 31096
diff changeset
995
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
996 // TODO(maged): find/create images to test the special tags
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
997 switch (tag_id)
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
998 {
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
999 case TIFFTAG_STRIPBYTECOUNTS:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1000 case TIFFTAG_STRIPOFFSETS:
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1001 tag_data_ov = get_array_field_data (tif, fip,
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1002 TIFFNumberOfStrips (tif));
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1003 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1004 case TIFFTAG_TILEBYTECOUNTS:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1005 case TIFFTAG_TILEOFFSETS:
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1006 tag_data_ov = get_array_field_data (tif, fip, TIFFNumberOfTiles (tif));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1007 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1008 case TIFFTAG_YCBCRCOEFFICIENTS:
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1009 tag_data_ov = get_array_field_data (tif, fip, 3);
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1010 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1011 case TIFFTAG_REFERENCEBLACKWHITE:
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1012 tag_data_ov = get_array_field_data (tif, fip, 6);
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1013 break;
31106
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1014 case TIFFTAG_GRAYRESPONSECURVE:
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1015 {
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1016 uint16_t bits_per_sample;
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1017 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1018 &bits_per_sample))
31106
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1019 error ("Failed to obtain the bit depth");
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1020
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1021 tag_data_ov = get_array_field_data (tif, fip, 1<<bits_per_sample);
31106
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1022 break;
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1023 }
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1024 case TIFFTAG_COLORMAP:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1025 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1026 uint16_t bits_per_sample;
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1027 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1028 &bits_per_sample))
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1029 error ("Failed to obtain the bit depth");
31143
a68f2dadafee Tiff.m: added all remaining tests for implemented features.
magedrifaat <magedrifaat@gmail.com>
parents: 31142
diff changeset
1030
a68f2dadafee Tiff.m: added all remaining tests for implemented features.
magedrifaat <magedrifaat@gmail.com>
parents: 31142
diff changeset
1031 // According to the format specification, this field should
a68f2dadafee Tiff.m: added all remaining tests for implemented features.
magedrifaat <magedrifaat@gmail.com>
parents: 31142
diff changeset
1032 // be 8 or 16 only.
a68f2dadafee Tiff.m: added all remaining tests for implemented features.
magedrifaat <magedrifaat@gmail.com>
parents: 31142
diff changeset
1033 if (bits_per_sample > 16)
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1034 error ("Too high bit depth for a palette image");
31098
3cbd0d82167c getTag: Implemented all special tags, only unsupported geotiff tags not implemented
magedrifaat <magedrifaat@gmail.com>
parents: 31097
diff changeset
1035
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1036 uint32_t count = 1 << bits_per_sample;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1037 uint16_t *red, *green, *blue;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1038 validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP,
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1039 &red, &green, &blue));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1040
31130
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1041 // Retrieving the data of the three channels and concatenating
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1042 // them together
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1043 OCTAVE_LOCAL_BUFFER (NDArray, array_list, 3);
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1044 dim_vector col_dims(count, 1);
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1045 array_list[0] = NDArray (interpret_tag_data (red,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1046 count,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1047 TIFFFieldDataType(fip))
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1048 .uint16_array_value ()
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1049 .reshape (col_dims));
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1050 array_list[1] = NDArray (interpret_tag_data (green,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1051 count,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1052 TIFFFieldDataType(fip))
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1053 .uint16_array_value ()
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1054 .reshape (col_dims));
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1055 array_list[2] = NDArray (interpret_tag_data (blue,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1056 count,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1057 TIFFFieldDataType(fip))
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1058 .uint16_array_value ()
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1059 .reshape (col_dims));
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1060
31130
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1061 NDArray mat_out = NDArray::cat(1, 3, array_list);
31121
341796f9efb6 Tiff getTag: converted colormap tag data to the correct type and range
magedrifaat <magedrifaat@gmail.com>
parents: 31120
diff changeset
1062 // Normalize the range to be between 0 and 1
341796f9efb6 Tiff getTag: converted colormap tag data to the correct type and range
magedrifaat <magedrifaat@gmail.com>
parents: 31120
diff changeset
1063 mat_out /= UINT16_MAX;
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1064
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1065 tag_data_ov = octave_value (mat_out);
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1066 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1067 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1068 case TIFFTAG_TRANSFERFUNCTION:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1069 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1070 uint16_t samples_per_pixel;
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1071 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL,
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1072 &samples_per_pixel))
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1073 error ("Failed to obtain the number of samples per pixel");
31098
3cbd0d82167c getTag: Implemented all special tags, only unsupported geotiff tags not implemented
magedrifaat <magedrifaat@gmail.com>
parents: 31097
diff changeset
1074
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1075 uint16_t bits_per_sample;
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1076 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE,
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1077 &bits_per_sample))
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1078 error ("Failed to obtain the number of samples per pixel");
31098
3cbd0d82167c getTag: Implemented all special tags, only unsupported geotiff tags not implemented
magedrifaat <magedrifaat@gmail.com>
parents: 31097
diff changeset
1079
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1080 uint32_t count = 1 << bits_per_sample;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1081 uint16_t *ch1, *ch2, *ch3;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1082 if (samples_per_pixel == 1)
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1083 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1084 validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP, &ch1));
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1085 tag_data_ov = interpret_tag_data (ch1, count,
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1086 TIFFFieldDataType (fip));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1087 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1088 else
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1089 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1090 validate_tiff_get_field (TIFFGetField (tif, TIFFTAG_COLORMAP,
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1091 &ch1, &ch2, &ch3));
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1092
31130
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1093 OCTAVE_LOCAL_BUFFER (uint16NDArray, array_list, 3);
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1094 dim_vector col_dims(count, 1);
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1095 array_list[0] = interpret_tag_data (ch1,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1096 count,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1097 TIFFFieldDataType (fip))
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1098 .uint16_array_value ()
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1099 .reshape (col_dims);
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1100 array_list[1] = interpret_tag_data (ch2,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1101 count,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1102 TIFFFieldDataType (fip))
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1103 .uint16_array_value ()
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1104 .reshape (col_dims);
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1105 array_list[2] = interpret_tag_data (ch3,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1106 count,
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1107 TIFFFieldDataType (fip))
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1108 .uint16_array_value ()
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1109 .reshape (col_dims);
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1110
31130
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1111 tag_data_ov = octave_value (uint16NDArray::cat (1, 3, array_list));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1112 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1113 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1114 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1115 case TIFFTAG_PAGENUMBER:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1116 case TIFFTAG_HALFTONEHINTS:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1117 case TIFFTAG_DOTRANGE:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1118 case TIFFTAG_YCBCRSUBSAMPLING:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1119 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1120 uint16_t tag_part1, tag_part2;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1121 validate_tiff_get_field (TIFFGetField (tif, tag_id,
31114
9dead1249449 Tiff getTag: fixed matrix shape for ColorMap and TransferFunction
magedrifaat <magedrifaat@gmail.com>
parents: 31113
diff changeset
1122 &tag_part1, &tag_part2));
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1123
31130
8475bdb70457 Tiff getTag: changed ColorMap to use Array::cat instead of memcpy
magedrifaat <magedrifaat@gmail.com>
parents: 31129
diff changeset
1124 NDArray mat_out (dim_vector (1, 2));
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1125 mat_out(0)
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1126 = interpret_tag_data (&tag_part1, 1,
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1127 TIFFFieldDataType (fip)).double_value ();
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1128 mat_out(1)
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1129 = interpret_tag_data (&tag_part2, 1,
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1130 TIFFFieldDataType (fip)).double_value ();
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1131
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1132 tag_data_ov = octave_value (mat_out);
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1133 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1134 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1135 case TIFFTAG_SUBIFD:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1136 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1137 uint16_t count;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1138 uint64_t *offsets;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1139 validate_tiff_get_field (TIFFGetField (tif, tag_id, &count, &offsets));
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1140 tag_data_ov = interpret_tag_data (offsets, count,
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1141 TIFFFieldDataType (fip));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1142 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1143 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1144 case TIFFTAG_EXTRASAMPLES:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1145 {
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1146 uint16_t count;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1147 uint16_t *types;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1148 validate_tiff_get_field (TIFFGetField (tif, tag_id, &count, &types));
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1149 tag_data_ov = interpret_tag_data (types, count,
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1150 TIFFFieldDataType (fip));
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1151 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1152 }
31103
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1153 // TODO(maged): These tags are more complex to implement
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1154 // will be implemented and tested later.
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1155 case TIFFTAG_XMLPACKET:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1156 case TIFFTAG_RICHTIFFIPTC:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1157 case TIFFTAG_PHOTOSHOP:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1158 case TIFFTAG_ICCPROFILE:
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1159 {
31103
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1160 error ("Complex Tags not implemented");
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1161 break;
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1162 }
31106
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1163 // These tags are not mentioned in the LibTIFF documentation
31103
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1164 // but are handled correctly by the library
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1165 case TIFFTAG_ZIPQUALITY:
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1166 case TIFFTAG_SGILOGDATAFMT:
31106
f5a88c0a61ab Support for GrayResponseCurve and GrayResponseUnit tags
magedrifaat <magedrifaat@gmail.com>
parents: 31105
diff changeset
1167 case TIFFTAG_GRAYRESPONSEUNIT:
31103
76b21bed2920 Undocumented and unimplemented tags are explicilty mentioned and handled
magedrifaat <magedrifaat@gmail.com>
parents: 31102
diff changeset
1168 {
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1169 tag_data_ov = get_scalar_field_data (tif, fip);
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1170 break;
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1171 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1172 default:
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1173 tag_data_ov = get_scalar_field_data (tif, fip);
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1174 }
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1175
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1176 return tag_data_ov;
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1177 }
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1178
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1179 void
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1180 set_field_data (TIFF *tif, const TIFFField *fip, octave_value tag_ov)
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1181 {
31139
3431a15b2c75 Tiff.m: refactored tests to use functions to reduce code repetition.
magedrifaat <magedrifaat@gmail.com>
parents: 31138
diff changeset
1182 // TODO(maged): prevent calling here for read-only images
3431a15b2c75 Tiff.m: refactored tests to use functions to reduce code repetition.
magedrifaat <magedrifaat@gmail.com>
parents: 31138
diff changeset
1183
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1184 // TODO(maged): complete the implementation of this function
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1185 uint32_t tag_id = TIFFFieldTag (fip);
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1186 uint32_t tag_data = tag_ov.double_value ();
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1187
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1188 if (! TIFFSetField(tif, tag_id, tag_data))
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1189 error ("Failed to set tag value");
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1190 }
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1191
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1192 template <typename T>
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1193 void
31133
e9925d528428 Tiff writeEncodedStrip: used octave_value functions instead of type_name
magedrifaat <magedrifaat@gmail.com>
parents: 31131
diff changeset
1194 write_strip (TIFF *tif, uint32_t strip_no, T strip_data,
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1195 tiff_image_data *image_data)
31133
e9925d528428 Tiff writeEncodedStrip: used octave_value functions instead of type_name
magedrifaat <magedrifaat@gmail.com>
parents: 31131
diff changeset
1196 {
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1197 uint32_t rows_in_strip;
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1198 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_ROWSPERSTRIP, &rows_in_strip))
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1199 error ("Failed to obtain the RowsPerStrip tag");
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1200
31127
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1201 // The tag has a default value of UINT32_MAX which means the entire
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1202 // image, but we need to cap it to the height for later calculations
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1203 if (rows_in_strip > image_data->height)
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1204 rows_in_strip = image_data->height;
0d9633ee715e Tiff: fixed a bug where the default value of some tags was ignored
magedrifaat <magedrifaat@gmail.com>
parents: 31126
diff changeset
1205
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1206 // LibTIFF uses zero-based indexing as opposed to Octave's 1-based
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1207 strip_no--;
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1208
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1209 uint32_t strip_count = TIFFNumberOfStrips (tif);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1210 rows_in_strip = get_rows_in_strip (strip_no, strip_count,
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1211 rows_in_strip, image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1212
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1213 dim_vector strip_dimensions;
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1214 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1215 strip_dimensions = dim_vector (rows_in_strip, image_data->width,
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1216 image_data->samples_per_pixel);
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1217 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1218 strip_dimensions = dim_vector (rows_in_strip, image_data->width, 1);
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1219 else
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1220 error ("Planar configuration not supported");
31145
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1221
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1222 if (strip_data.dim1 () > rows_in_strip)
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1223 warning ("The strip is composed of %u rows but the input has %ld rows.",
31145
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1224 rows_in_strip,
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1225 strip_data.dim1 ());
31137
233130c0b1f6 Tiff writeEncodedStrip: changed strip dimension check behavior to mimic matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31136
diff changeset
1226
31145
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1227 if (strip_data.dim2 () > image_data->width)
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1228 warning ("The image width is %u but the input has %ld columns.",
31145
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1229 image_data->width,
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1230 strip_data.dim2 ());
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1231
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1232 if (strip_data.ndims () > 2)
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1233 {
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1234 if (image_data->planar_configuration == PLANARCONFIG_CONTIG
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1235 && strip_data.dim3 () > image_data->samples_per_pixel)
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1236 warning ("The strip is composed of %u channels but the input has %ld channels.",
31145
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1237 image_data->samples_per_pixel,
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1238 strip_data.dim3 ());
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1239 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1240 && strip_data.dim3 () > 1)
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1241 warning ("The strip is composed of %u channel but the input has %ld channels.",
31145
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1242 1, strip_data.dim3 ());
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1243 }
2e11f9cb30b8 Tiff writeEncodedStrip: added warning for input data larger than the size of the strip.
magedrifaat <magedrifaat@gmail.com>
parents: 31144
diff changeset
1244
31137
233130c0b1f6 Tiff writeEncodedStrip: changed strip dimension check behavior to mimic matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31136
diff changeset
1245 strip_data.resize (strip_dimensions);
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1246
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1247 // Permute the dimesions of the strip to match the expected memory
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1248 // arrangement of LibTIFF (channel x width x height)
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1249 Array<octave_idx_type> perm (dim_vector (3, 1));
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1250 perm(0) = 2;
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1251 perm(1) = 1;
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1252 perm(2) = 0;
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1253 strip_data = strip_data.permute (perm);
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1254
31144
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1255 void *data_vec = strip_data.fortran_vec ();
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1256 if (image_data->bits_per_sample == 8
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1257 || image_data->bits_per_sample == 16
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1258 || image_data->bits_per_sample == 32
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1259 || image_data->bits_per_sample == 64)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1260 {
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1261 // Can't rely on LibTIFF's TIFFStripSize because boundary strips
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1262 // can be smaller in size
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1263 tsize_t strip_size = strip_data.numel ()
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1264 * image_data->bits_per_sample / 8;
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1265 if (TIFFWriteEncodedStrip (tif, strip_no, data_vec, strip_size) == -1)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1266 error ("Failed to write strip data to image");
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1267
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1268 }
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1269 else if (image_data->bits_per_sample == 1)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1270 {
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1271 if (image_data->samples_per_pixel != 1)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1272 error ("Bi-Level images must have one channel only");
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1273
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1274 // Create a buffer to hold the packed strip data
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1275 // Unique pointers are faster than vectors for constant size buffers
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1276 std::unique_ptr<uint8_t []> strip_ptr
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1277 = std::make_unique<uint8_t []> (TIFFStripSize (tif));
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1278 uint8_t *strip_buf = strip_ptr.get ();
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1279 uint8_t *data_u8 = reinterpret_cast<uint8_t *> (data_vec);
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1280 // According to the format specification, the row should be byte
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1281 // aligned so the number of bytes is rounded up to the nearest byte
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1282 uint32_t padded_width = (image_data->width + 7) / 8;
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1283 // Packing the pixel data into bits
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1284 for (uint32_t row = 0; row < rows_in_strip; row++)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1285 {
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1286 for (uint32_t col = 0; col < image_data->width; col++)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1287 {
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1288 uint8_t shift = 7 - col % 8;
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1289 strip_buf[row * padded_width + col / 8] |= data_u8[col] << shift;
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1290 }
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1291 data_u8 += image_data->width;
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1292 }
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1293 tsize_t strip_size = padded_width * rows_in_strip;
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1294 if (TIFFWriteEncodedStrip (tif, strip_no, strip_buf, strip_size) == -1)
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1295 error ("Failed to write strip data to image");
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1296 }
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1297 else
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1298 {
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1299 error ("Unsupported bit depth");
8ba9f2326ee9 Tiff writeEncodedStrip: added support for BiLevel images
magedrifaat <magedrifaat@gmail.com>
parents: 31143
diff changeset
1300 }
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1301 }
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1302
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1303 template <typename T>
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1304 void
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1305 write_tile (TIFF *tif, uint32_t tile_no, T tile_data,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1306 tiff_image_data *image_data)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1307 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1308 uint32_t tile_width, tile_height;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1309 if (! TIFFGetField (tif, TIFFTAG_TILEWIDTH, &tile_width))
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1310 error ("Failed to get the tile width");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1311 if (! TIFFGetField (tif, TIFFTAG_TILELENGTH, &tile_height))
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1312 error ("Failed to get the tile length");
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
1313
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
1314 if (tile_height == 0 || tile_height % 16 != 0
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
1315 || tile_width == 0 || tile_width % 16 != 0)
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
1316 error ("Tile dimesion tags are invalid");
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1317
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1318 if (tile_no < 1 || tile_no > TIFFNumberOfTiles (tif))
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1319 error ("Tile number out of bounds");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1320
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1321 if (tile_data.dim1 () > tile_height)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1322 warning ("The tile is composed of %u rows but input has %ld rows",
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1323 tile_height, tile_data.dim1 ());
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1324 if (tile_data.dim2 () > tile_width)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1325 warning ("The tile is composed of %u columns but input has %ld columns",
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1326 tile_width, tile_data.dim2 ());
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1327 if (tile_data.ndims () > 2)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1328 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1329 if (image_data->planar_configuration == PLANARCONFIG_CONTIG
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1330 && tile_data.dim3 () > image_data->samples_per_pixel)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1331 warning ("The tile is composed of %u channels but input has %ld channels",
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1332 image_data->samples_per_pixel, tile_data.dim3 ());
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1333 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1334 && tile_data.dim3 () > 1)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1335 warning ("The tile is composed of %u channels but input has %ld channels",
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1336 1, tile_data.dim3 ());
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1337 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1338
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1339 dim_vector tile_dimensions;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1340 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1341 tile_dimensions = dim_vector (tile_height, tile_width,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1342 image_data->samples_per_pixel);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1343 else if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1344 tile_dimensions = dim_vector (tile_height, tile_width, 1);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1345 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1346 error ("Planar configuration not supported");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1347
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1348 tile_data.resize (tile_dimensions);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1349 Array<octave_idx_type> perm (dim_vector (3, 1));
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1350 perm(0) = 2;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1351 perm(1) = 1;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1352 perm(2) = 0;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1353 tile_data = tile_data.permute (perm);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1354
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1355 // Octave indexing is 1-based while LibTIFF is zero-based
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1356 tile_no--;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1357 void *data_vec = tile_data.fortran_vec ();
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1358 if (image_data->bits_per_sample == 8
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1359 || image_data->bits_per_sample == 16
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1360 || image_data->bits_per_sample == 32
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1361 || image_data->bits_per_sample == 64)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1362 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1363 if (TIFFWriteEncodedTile (tif, tile_no, data_vec,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1364 TIFFTileSize (tif)) == -1)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1365 error ("Failed to write tile data to image");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1366
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1367 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1368 else if (image_data->bits_per_sample == 1)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1369 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1370 if (image_data->samples_per_pixel != 1)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1371 error ("Bi-Level images must have one channel only");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1372
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1373 // Create a buffer to hold the packed tile data
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1374 // Unique pointers are faster than vectors for constant size buffers
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1375 std::unique_ptr<uint8_t []> tile_ptr
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1376 = std::make_unique<uint8_t []> (TIFFTileSize (tif));
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1377 uint8_t *tile_buf = tile_ptr.get ();
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1378 uint8_t *data_u8 = reinterpret_cast<uint8_t *> (data_vec);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1379 // Packing the pixel data into bits
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1380 for (uint32_t row = 0; row < tile_height; row++)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1381 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1382 for (uint32_t col = 0; col < tile_width; col++)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1383 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1384 uint8_t shift = 7 - col % 8;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1385 tile_buf[row * tile_width/8 + col/8] |= data_u8[col] << shift;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1386 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1387 data_u8 += tile_width;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1388 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1389 if (TIFFWriteEncodedTile (tif, tile_no, tile_buf,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1390 TIFFTileSize (tif)) == -1)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1391 error ("Failed to write tile data to image");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1392 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1393 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1394 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1395 error ("Unsupported bit depth");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1396 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1397 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1398
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1399 template <typename T>
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1400 void
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1401 write_strip_or_tile (TIFF *tif, uint32_t strip_tile_no, T strip_data,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1402 tiff_image_data *image_data)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1403 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1404 if (image_data->is_tiled)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1405 write_tile<T> (tif, strip_tile_no, strip_data, image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1406 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1407 write_strip<T> (tif, strip_tile_no, strip_data, image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1408 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1409
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1410 void
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1411 handle_write_strip_or_tile (TIFF *tif, uint32_t strip_tile_no,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1412 octave_value data_ov,
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1413 tiff_image_data *image_data)
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1414 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1415
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1416 // SampleFormat tag is not a required field and has a default value of 1
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1417 // So we need to use TIFFGetFieldDefaulted in case it is not present in
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1418 // the file
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1419 uint16_t sample_format;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1420 if (! TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format))
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1421 error ("Failed to obtain a value for sample format");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1422
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1423 // TODO(maged): add support for signed integer images
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1424 if (sample_format == 3)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1425 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1426 if (image_data->bits_per_sample != 32
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1427 && image_data->bits_per_sample != 64)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1428 error ("Floating point images are only supported for bit depths of 32 and 64");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1429 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1430
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1431 // The standard specifies that a SampleFormat of 4 should be treated
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1432 // the same as 1 (unsigned integer)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1433 else if (sample_format != 1 && sample_format != 4)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1434 error ("Unsupported sample format");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1435
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1436 switch (image_data->bits_per_sample)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1437 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1438 case 1:
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1439 // We need to check for both scalar and matrix types to handle single
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1440 // element strip
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1441 if (data_ov.is_bool_scalar () || data_ov.is_bool_matrix ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1442 write_strip_or_tile<boolNDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1443 data_ov.bool_array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1444 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1445 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1446 error ("Expected logical matrix for BiLevel image");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1447 break;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1448 case 8:
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1449 if (data_ov.is_uint8_type ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1450 write_strip_or_tile<uint8NDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1451 data_ov.uint8_array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1452 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1453 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1454 error ("Only uint8 data is allowed for uint images with bit depth of 8");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1455 break;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1456 case 16:
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1457 if (data_ov.is_uint16_type ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1458 write_strip_or_tile<uint16NDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1459 data_ov.uint16_array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1460 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1461 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1462 error ("Only uint16 data is allowed for uint images with bit depth of 16");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1463 break;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1464 case 32:
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1465 if (sample_format == 3)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1466 if (data_ov.is_single_type () || data_ov.is_double_type ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1467 write_strip_or_tile<FloatNDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1468 data_ov.float_array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1469 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1470 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1471 error ("Only single and double data are allowed for floating-point images");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1472 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1473 if (data_ov.is_uint32_type ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1474 write_strip_or_tile<uint32NDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1475 data_ov.uint32_array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1476 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1477 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1478 error ("Only uint32 data is allowed for uint images with bit depth of 32");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1479 break;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1480 case 64:
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1481 if (sample_format == 3)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1482 if (data_ov.is_single_type () || data_ov.is_double_type ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1483 write_strip_or_tile<NDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1484 data_ov.array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1485 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1486 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1487 error ("Only single and double data are allowed for floating-point images");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1488 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1489 if (data_ov.is_uint64_type ())
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1490 write_strip_or_tile<uint64NDArray> (tif, strip_tile_no,
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1491 data_ov.uint64_array_value (),
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1492 image_data);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1493 else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1494 error ("Only uint64 data is allowed for uint images with bit depth of 64");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1495 break;
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1496 default:
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1497 error ("Unsupported bit depth");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1498 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1499 }
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
1500
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1501 template <typename T>
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1502 void
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1503 write_stripped_image (TIFF *tif, T pixel_data, tiff_image_data *image_data)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1504 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1505 // TODO(maged): remove this? ASSUMES pixel data dimensions are already validated
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1506
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1507 typedef typename T::element_type P;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1508
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1509 // Permute pixel data to be aligned in memory to the way LibTIFF
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1510 // expects the data to be (i.e. channel x width x height for chunky
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1511 // and width x height x channel for separate planes)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1512 Array<octave_idx_type> perm (dim_vector (3, 1));
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1513 if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1514 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1515 perm(0) = 1;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1516 perm(1) = 0;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1517 perm(2) = 2;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1518 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1519 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1520 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1521 perm(0) = 2;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1522 perm(1) = 1;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1523 perm(2) = 0;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1524 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1525 pixel_data = pixel_data.permute (perm);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1526
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1527 uint32_t row_per_strip;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1528 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_ROWSPERSTRIP, &row_per_strip))
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1529 error ("Failed to obtain the RowPerStrip tag");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1530
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1531 // The default value is INT_MAX so we need to cap it to the image height
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1532 if (row_per_strip > image_data->height)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1533 row_per_strip = image_data->height;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1534
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1535 uint8_t *pixel_fvec = reinterpret_cast<uint8_t *> (pixel_data.fortran_vec ());
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1536 uint32_t strip_count = TIFFNumberOfStrips (tif);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1537 tsize_t strip_size;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1538 uint32_t rows_in_strip;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1539 for (uint32_t strip = 0; strip < strip_count; strip++)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1540 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1541 rows_in_strip = get_rows_in_strip (strip, strip_count,
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1542 row_per_strip, image_data);
31154
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1543 if (image_data->bits_per_sample == 8
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1544 || image_data->bits_per_sample == 16
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1545 || image_data->bits_per_sample == 32
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1546 || image_data->bits_per_sample == 64)
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1547 {
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1548 strip_size = rows_in_strip * image_data->width * sizeof (P);
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1549 if (image_data->planar_configuration == PLANARCONFIG_CONTIG)
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1550 strip_size *= image_data->samples_per_pixel;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1551 if (! TIFFWriteEncodedStrip (tif, strip, pixel_fvec, strip_size))
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1552 error ("Failed to rite strip data");
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1553 pixel_fvec += strip_size;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1554 }
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1555 else if (image_data->bits_per_sample == 1)
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1556 {
31156
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1557 if (image_data->samples_per_pixel != 1)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1558 error ("Bi-Level images must have one channel only");
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1559
31154
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1560 // Create a buffer to hold the packed strip data
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1561 // Unique pointers are faster than vectors for constant size buffers
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1562 std::unique_ptr<uint8_t []> strip_ptr
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1563 = std::make_unique<uint8_t []> (TIFFStripSize (tif));
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1564 uint8_t *strip_buf = strip_ptr.get ();
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1565 // According to the format specification, the row should be byte
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1566 // aligned so the number of bytes is rounded up to the nearest byte
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1567 uint32_t padded_width = (image_data->width + 7) / 8;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1568 // Packing the pixel data into bits
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1569 for (uint32_t row = 0; row < rows_in_strip; row++)
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1570 {
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1571 for (uint32_t col = 0; col < image_data->width; col++)
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1572 {
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1573 uint8_t shift = 7 - col % 8;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1574 strip_buf[row * padded_width + col / 8]
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1575 |= pixel_fvec[col] << shift;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1576 }
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1577 pixel_fvec += image_data->width;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1578 }
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1579 strip_size = padded_width * rows_in_strip;
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1580 if (TIFFWriteEncodedStrip (tif, strip, strip_buf, strip_size) == -1)
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1581 error ("Failed to write strip data to image");
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1582 }
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1583 else
828b7cc9aa36 Tiff write: added support for BiLevel stripped images and added unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31153
diff changeset
1584 error ("Unsupported bit depth");
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1585 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1586 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1587
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1588 template <typename T>
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1589 void
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1590 write_tiled_image (TIFF *tif, T pixel_data, tiff_image_data *image_data)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1591 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1592 // TODO(maged): remove this? ASSUMES pixel data dimensions are already validated
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1593
31155
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1594 uint32_t tile_width, tile_height;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1595 if (! TIFFGetField (tif, TIFFTAG_TILEWIDTH, &tile_width))
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1596 error ("Failed to get the tile width");
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1597 if (! TIFFGetField (tif, TIFFTAG_TILELENGTH, &tile_height))
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1598 error ("Failed to get the tile length");
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1599
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1600 if (tile_height == 0 || tile_height % 16 != 0
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1601 || tile_width == 0 || tile_width % 16 != 0)
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1602 error ("Tile dimesion tags are invalid");
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1603
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1604 uint32_t tiles_across = (image_data->width + tile_width - 1)
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1605 / tile_width;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1606 uint32_t tiles_down = (image_data->height + tile_height - 1)
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1607 / tile_height;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1608
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1609 // Resize the image data to add tile padding
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1610 dim_vector padded_dims (tiles_down * tile_height,
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1611 tiles_across * tile_width,
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1612 image_data->samples_per_pixel);
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1613 pixel_data.resize (padded_dims);
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1614
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1615 // Reshape the image to separate tiles into their own dimension
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1616 // so we can permute them to the right order
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1617 dim_vector tiled_dims (tile_height, tiles_down, tile_width, tiles_across,
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1618 image_data->samples_per_pixel);
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1619 pixel_data = pixel_data.reshape (tiled_dims);
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1620
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1621 // Permute the dimesnions to get the memory alignment to match LibTIFF
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1622 Array<octave_idx_type> perm (dim_vector (5, 1));
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1623 if (image_data->planar_configuration == PLANARCONFIG_SEPARATE)
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1624 {
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1625 // For separate planes, the data coming from libtiff will have all
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1626 // tiles of the first sample then all tiles of the second sample
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1627 // and so on. Tiles of each sample will be ordered from left to right
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1628 // and from top to bottom. And data inside each tile is organised as
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1629 // rows and each row contains columns.
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1630 // So the order for LibTIFF is:
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1631 // samples x tiles_down x tiles_across x tile_height x tile_width
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1632 // But memory orientation of Octave arrays is reversed so we set it to
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1633 // tile_width x tile_height x tiles_across x tiles_down x samples
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1634 perm(0) = 2;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1635 perm(1) = 0;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1636 perm(2) = 3;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1637 perm(3) = 1;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1638 perm(4) = 4;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1639 }
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1640 else
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1641 {
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1642 // For chunky planes, the data coming from libtiff will be ordered
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1643 // from left to right and from top to bottom. And data inside each
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1644 // tile is organised as rows and each row contains columns and each
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1645 // column contains samples.
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1646 // So the order for LibTIFF is:
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1647 // tiles_down x tiles_across x tile_height x tile_width x samples
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1648 // But memory orientation of Octave arrays is reversed so we set it to
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1649 // samples x tile_width x tile_height x tiles_across x tiles_down
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1650 perm(0) = 4;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1651 perm(1) = 2;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1652 perm(2) = 0;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1653 perm(3) = 3;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1654 perm(4) = 1;
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1655 }
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1656 pixel_data = pixel_data.permute (perm);
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1657
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1658 uint8_t *pixel_fvec
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1659 = reinterpret_cast<uint8_t *> (pixel_data.fortran_vec ());
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1660 uint32_t tile_count = TIFFNumberOfTiles (tif);
31156
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1661 tsize_t tile_size = TIFFTileSize (tif);
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1662
31155
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1663 for (uint32_t tile = 0; tile <tile_count; tile++)
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1664 {
31156
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1665 if (image_data->bits_per_sample == 8
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1666 || image_data->bits_per_sample == 16
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1667 || image_data->bits_per_sample == 32
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1668 || image_data->bits_per_sample == 64)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1669 {
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1670 if (! TIFFWriteEncodedTile (tif, tile, pixel_fvec, tile_size))
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1671 error ("Failed to write tile data");
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1672 pixel_fvec += tile_size;
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1673 }
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1674 else if (image_data->bits_per_sample == 1)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1675 {
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1676 if (image_data->samples_per_pixel != 1)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1677 error ("Bi-Level images must have one channel only");
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1678
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1679 // Create a buffer to hold the packed tile data
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1680 // Unique pointers are faster than vectors for
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1681 // constant size buffers
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1682 std::unique_ptr<uint8_t []> tile_ptr
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1683 = std::make_unique<uint8_t []> (tile_size);
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1684 uint8_t *tile_buf = tile_ptr.get ();
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1685 // Packing the pixel data into bits
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1686 for (uint32_t row = 0; row < tile_height; row++)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1687 {
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1688 for (uint32_t col = 0; col < tile_width; col++)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1689 {
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1690 uint8_t shift = 7 - col % 8;
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1691 tile_buf[row * tile_width / 8 + col / 8]
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1692 |= pixel_fvec[col] << shift;
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1693 }
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1694 pixel_fvec += tile_width;
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1695 }
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1696 if (TIFFWriteEncodedTile (tif, tile, tile_buf,
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1697 TIFFTileSize (tif)) == -1)
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1698 error ("Failed to write tile data to image");
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1699 }
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1700 else
1e633a093faa Tiff write: added support for logical tile images and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31155
diff changeset
1701 error ("Unsupported bit depth");
31155
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1702 }
a30b144bc10b Tiff write: added support for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31154
diff changeset
1703
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1704 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1705
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1706 template <typename T>
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1707 void
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1708 write_image (TIFF *tif, T pixel_data, tiff_image_data *image_data)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1709 {
31159
e960e3a3b3f6 Tiff.m: added structs for tag value enums to the Tiff class.
magedrifaat <magedrifaat@gmail.com>
parents: 31158
diff changeset
1710 // TODO(maged): matlab sets the remaining to zero for less width and height
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1711 // and issues a warning for larger widths but not for larger height? (we should error)
31159
e960e3a3b3f6 Tiff.m: added structs for tag value enums to the Tiff class.
magedrifaat <magedrifaat@gmail.com>
parents: 31158
diff changeset
1712 // and errors for less or more number of channels
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1713 if (image_data->height != pixel_data.dim1 ()
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1714 || image_data->width != pixel_data.dim2 ()
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1715 || pixel_data.ndims () > 3
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1716 || (image_data->samples_per_pixel > 1 && pixel_data.ndims () < 3)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1717 || (pixel_data.ndims () > 2
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1718 && image_data->samples_per_pixel != pixel_data.dim3 ()))
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1719 error ("Dimensions of the input don't match image dimenions");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1720
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1721 if (image_data->is_tiled)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1722 write_tiled_image<T> (tif, pixel_data, image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1723 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1724 write_stripped_image<T> (tif, pixel_data, image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1725
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1726 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1727
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
1728
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1729 #endif
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1730
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1731 DEFUN (__open_tiff__, args, ,
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1732 "Open a Tiff file and return its handle")
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1733 {
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1734 #if defined (HAVE_TIFF)
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1735 int nargin = args.length ();
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1736
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1737 if (nargin == 0 || nargin > 2)
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
1738 {
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1739 // TODO(maged): return invalid object instead??
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1740 error ("No filename supplied\n");
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
1741 }
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1742
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1743 std::string filename = args(0).string_value ();
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1744 std::string mode = "r";
31094
ab5b33e447b0 Modified getTag to account for different tag data types and multivalued tags, the current implementation is still buggy for most tags and data types
magedrifaat <magedrifaat@gmail.com>
parents: 31093
diff changeset
1745
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1746 if (nargin == 2)
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1747 mode = args(1).string_value ();
31123
0bcb35909ef4 Tiff open: removed support for rh mode and marked r+ as not yet supported
magedrifaat <magedrifaat@gmail.com>
parents: 31122
diff changeset
1748
31142
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1749 const std::vector<std::string> supported_modes {"r", "w", "w8", "a"};
31123
0bcb35909ef4 Tiff open: removed support for rh mode and marked r+ as not yet supported
magedrifaat <magedrifaat@gmail.com>
parents: 31122
diff changeset
1750
31142
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1751 if (std::find (supported_modes.cbegin (), supported_modes.cend (), mode)
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1752 == supported_modes.cend ())
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1753 {
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1754 if (mode == "r+")
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1755 error ("Openning files in r+ mode is not yet supported");
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1756 else
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1757 error ("Invalid mode for openning Tiff file: %s", mode.c_str ());
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1758 }
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1759
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1760 TIFF *tif = TIFFOpen (filename.c_str (), mode.c_str ());
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1761
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1762 if (! tif)
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1763 error ("Failed to open Tiff file\n");
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1764
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1765 // TODO(maged): use inheritance of octave_base_value instead
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1766 octave_value tiff_ov = octave_value ((uint64_t)tif);
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1767 return octave_value_list (tiff_ov);
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1768 #else
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1769 octave_unused_parameter (args);
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1770 err_disabled_feature ("Tiff", "Tiff");
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1771 #endif
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1772 }
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1773
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1774
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1775 DEFUN (__close_tiff__, args, ,
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1776 "Close a tiff file")
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1777 {
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1778 #if defined (HAVE_TIFF)
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
1779 int nargin = args.length ();
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1780
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1781 if (nargin == 0)
31101
f24d7bcad2d3 Partially fixed formatting of C++ code
magedrifaat <magedrifaat@gmail.com>
parents: 31099
diff changeset
1782 error ("No handle provided\n");
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1783
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1784 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1785 TIFFClose (tif);
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1786
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1787 return octave_value_list ();
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1788 #else
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1789 err_disabled_feature ("close", "Tiff");
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1790 #endif
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1791 }
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1792
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
1793
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1794 DEFUN (__tiff_get_tag__, args, ,
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1795 "Get the value of a tag from a tiff image")
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1796 {
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1797 #if defined (HAVE_TIFF)
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1798 int nargin = args.length ();
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1799
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1800 if (nargin == 0)
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1801 error ("No handle provided\n");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1802
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1803 if (nargin < 2)
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1804 error ("No tag name provided\n");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1805
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1806 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31094
ab5b33e447b0 Modified getTag to account for different tag data types and multivalued tags, the current implementation is still buggy for most tags and data types
magedrifaat <magedrifaat@gmail.com>
parents: 31093
diff changeset
1807
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1808 uint32_t tag_id;
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1809 const TIFFField *fip;
31129
dfab9c6982bf Tiff getTag: added support for single quotes string fo the tag name
magedrifaat <magedrifaat@gmail.com>
parents: 31128
diff changeset
1810
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1811 if (args(1).is_string ())
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1812 {
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1813 std::string tagName = args(1).string_value ();
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1814 fip = TIFFFieldWithName (tif, tagName.c_str ());
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1815 if (! fip)
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1816 error ("Tiff tag not found");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1817
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1818 tag_id = TIFFFieldTag (fip);
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1819 }
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1820 else
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1821 {
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1822 tag_id = args(1).int_value ();
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1823 fip = TIFFFieldWithTag (tif, tag_id);
31129
dfab9c6982bf Tiff getTag: added support for single quotes string fo the tag name
magedrifaat <magedrifaat@gmail.com>
parents: 31128
diff changeset
1824
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1825 if (! fip)
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1826 error ("Tiff tag not found");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1827 }
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1828
31117
530dbd1d6b07 Tiff getTag: fixed bug for multivalued tags where only first item is returned
magedrifaat <magedrifaat@gmail.com>
parents: 31115
diff changeset
1829 return octave_value_list (get_field_data (tif, fip));
31104
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1830 #else
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1831 err_disabled_feature ("getTag", "Tiff");
b5d59c115e52 Use HAVE_TIFF flag to optionally disable Tiff
magedrifaat <magedrifaat@gmail.com>
parents: 31103
diff changeset
1832 #endif
31102
d6ecf0e8838b Add the new code to the octave namespace
magedrifaat <magedrifaat@gmail.com>
parents: 31101
diff changeset
1833 }
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1834
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1835
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1836 DEFUN (__tiff_set_tag__, args, ,
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1837 "Set the value of a tag in a tiff image")
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1838 {
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1839 #if defined (HAVE_TIFF)
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1840 int nargin = args.length ();
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1841
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1842 if (nargin < 2)
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1843 error ("Too few arguments provided\n");
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1844
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1845 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31162
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
1846
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
1847 check_readonly (tif);
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1848
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1849 if (args(1).isstruct ())
31140
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1850 {
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
1851 octave_scalar_map tags = args(1).xscalar_map_value ("__tiff_set_tag__: struct argument must be a scalar structure");
31142
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1852 string_vector keys = tags.fieldnames ();
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1853 // Using iterators instead of this loop method seems to process
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1854 // the elements of the struct in a different order than they were
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1855 // assigned which makes the behavior here incompatible with matlab
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1856 // in case of errors.
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1857 for (octave_idx_type i = 0; i < keys.numel (); i++)
31140
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1858 {
31142
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1859 std::string key = keys[i];
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1860 const TIFFField *fip = TIFFFieldWithName (tif, key.c_str ());
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1861 if (! fip)
31140
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1862 error ("Tag %s not found", key.c_str ());
31142
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1863 set_field_data (tif, fip, tags.contents (key));
31140
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1864 }
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1865 }
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1866 else
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1867 {
31140
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1868 if (nargin < 3)
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1869 error ("Too few arguments provided");
5f70efad6e2c Tiff setTag: added support for setting multiple tags at once using structs
magedrifaat <magedrifaat@gmail.com>
parents: 31139
diff changeset
1870
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1871 const TIFFField *fip;
31142
97e7ee3b27b7 Tiff setTag: modify the behavior with structs to be compatible with matlab
magedrifaat <magedrifaat@gmail.com>
parents: 31140
diff changeset
1872 // TODO(maged): matlab actually checks for its own strings not LibTIFF's
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1873 // Make it work in both ways
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1874 if (args(1).is_string ())
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1875 {
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1876 std::string tagName = args(1).string_value ();
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1877 fip = TIFFFieldWithName (tif, tagName.c_str ());
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1878 if (! fip)
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1879 error ("Tiff tag not found");
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1880 }
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1881 else
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1882 {
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1883 uint32_t tag_id = args(1).int_value ();
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1884 fip = TIFFFieldWithTag (tif, tag_id);
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1885 if (! fip)
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1886 error ("Tiff tag not found");
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1887 }
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1888
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1889 set_field_data (tif, fip, args(2));
31124
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1890 }
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1891
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1892 return octave_value_list ();
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1893 #else
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1894 err_disabled_feature ("setTag", "Tiff");
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1895 #endif
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1896 }
e8d1cc309bc9 Tiff: added initial implementation of setTag function for scalar tags
magedrifaat <magedrifaat@gmail.com>
parents: 31123
diff changeset
1897
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1898 DEFUN (__tiff_read__, args, nargout,
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1899 "Read the image in the current IFD")
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1900 {
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1901 #if defined (HAVE_TIFF)
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1902 int nargin = args.length ();
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1903
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1904 if (nargin == 0)
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1905 error ("No handle provided\n");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1906
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
1907 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1908
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1909 // TODO(maged): nargout and ycbcr
31128
524cb3106432 __tiff__.cc: handled compiler warnings for unused params and old-style casts.
magedrifaat <magedrifaat@gmail.com>
parents: 31127
diff changeset
1910 octave_unused_parameter (nargout);
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1911
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1912 // Obtain all necessary tags
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
1913 // The main purpose of this struct is to hold all the necessary tags that
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
1914 // will be common among all the different cases of handling the the image
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
1915 // data to avoid repeating the same calls to TIFFGetField in the different
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
1916 // functions as each call is a possible point of failure
31122
1662939a7a49 Tiff read: moved image_date initialization into a constructor
magedrifaat <magedrifaat@gmail.com>
parents: 31121
diff changeset
1917 tiff_image_data image_data (tif);
31125
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1918
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1919 uint16_t sample_format;
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1920 if (! TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format))
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1921 error ("Failed to obtain a value for sample format");
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1922
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1923 if (sample_format == 3)
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1924 {
31135
b7ffe64e0287 Tiff.m: added test for 16- and 32-bit grayscale and RGB images.
magedrifaat <magedrifaat@gmail.com>
parents: 31133
diff changeset
1925 if (image_data.bits_per_sample != 32 && image_data.bits_per_sample != 64)
31125
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1926 error ("Floating point images are only supported for bit depths of 32 and 64");
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1927 }
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1928 else if (sample_format != 1 && sample_format != 4)
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1929 error ("Unsupported sample format");
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1930
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1931 octave_value_list retval;
31112
e3d8443585fe Tiff read refactored reading stripped images into a separate function
magedrifaat <magedrifaat@gmail.com>
parents: 31111
diff changeset
1932 switch (image_data.bits_per_sample)
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1933 {
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1934 case 1:
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1935 retval(0) = read_image<boolNDArray> (tif, &image_data);
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1936 break;
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1937 case 4:
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1938 case 8:
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1939 retval(0) = read_image<uint8NDArray> (tif, &image_data);
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1940 break;
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1941 case 16:
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1942 retval(0) = read_image<uint16NDArray> (tif, &image_data);
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1943 break;
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1944 case 32:
31125
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1945 if (sample_format == 3)
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1946 retval(0) = read_image<FloatNDArray> (tif, &image_data);
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1947 else
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1948 retval(0) = read_image<uint32NDArray> (tif, &image_data);
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1949 break;
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1950 case 64:
31125
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1951 if (sample_format == 3)
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1952 retval(0) = read_image<NDArray> (tif, &image_data);
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1953 else
3b775b939de4 Tiff read: Added support for floating-point images
magedrifaat <magedrifaat@gmail.com>
parents: 31124
diff changeset
1954 retval(0) = read_image<uint64NDArray> (tif, &image_data);
31113
a74059523d74 Tiff read: use template to remove repitition in filling matrices
magedrifaat <magedrifaat@gmail.com>
parents: 31112
diff changeset
1955 break;
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1956 default:
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1957 error ("Unsupported bit depth");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1958 }
31110
2daeeff33980 Tiff read fixed segfault bug for compressed images
magedrifaat <magedrifaat@gmail.com>
parents: 31109
diff changeset
1959
31109
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1960 return retval;
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1961 #else
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1962 err_disabled_feature ("read", "Tiff");
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1963 #endif
06814e8b5a29 add function to read an entire image
magedrifaat <magedrifaat@gmail.com>
parents: 31106
diff changeset
1964 }
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
1965
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
1966 DEFUN (__tiff_read_encoded_strip__, args, nargout,
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1967 "Read a strip from the image in the current IFD")
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1968 {
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1969 #if defined (HAVE_TIFF)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1970 int nargin = args.length ();
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1971
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1972 if (nargin != 2)
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1973 error ("rong number of arguments");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1974
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1975 TIFF *tif = (TIFF *)(args(0).uint64_value ());
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1976
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1977 // TODO(maged): nargout and ycbcr
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1978 octave_unused_parameter (nargout);
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1979
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1980 if (TIFFIsTiled (tif))
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1981 error ("The image is tiled not stripped");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1982
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1983 uint32_t strip_no;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1984 if (args(1).is_scalar_type ())
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1985 strip_no = args(1).uint32_scalar_value ();
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1986 else
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1987 error ("Expected scalar for strip number");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1988
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1989 if (strip_no < 1 || strip_no > TIFFNumberOfStrips (tif))
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1990 error ("Strip number out of bounds");
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1991
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1992 // Convert from Octave's 1-based indexing to zero-based indexing
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1993 strip_no--;
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
1994
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1995 return octave_value_list (handle_read_strip_or_tile (tif, strip_no));
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1996 #else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1997 err_disabled_feature ("readEncodedStrip", "Tiff");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1998 #endif
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
1999 }
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2000
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2001 DEFUN (__tiff_read_encoded_tile__, args, nargout,
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2002 "Read a tile from the image in the current IFD")
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2003 {
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2004 #if defined (HAVE_TIFF)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2005 int nargin = args.length ();
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2006
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2007 if (nargin != 2)
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2008 error ("rong number of arguments");
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2009
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2010 TIFF *tif = (TIFF *)(args(0).uint64_value ());
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2011
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2012 // TODO(maged): nargout and ycbcr
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2013 octave_unused_parameter (nargout);
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2014
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2015 if (! TIFFIsTiled (tif))
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2016 error ("The image is stripped not tiled");
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2017
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2018 uint32_t tile_no;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2019 if (args(1).is_scalar_type ())
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2020 tile_no = args(1).uint32_scalar_value ();
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2021 else
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2022 error ("Expected scalar for tile number");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2023
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2024 if (tile_no < 1 || tile_no > TIFFNumberOfTiles (tif))
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2025 error ("Tile number out of bounds");
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2026
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2027 // Convert from Octave's 1-based indexing to zero-based indexing
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2028 tile_no--;
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2029
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2030 return octave_value_list (handle_read_strip_or_tile (tif, tile_no));
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2031 #else
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2032 err_disabled_feature ("readEncodedTile", "Tiff");
31157
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2033 #endif
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2034 }
dc3d2744916d Tiff: added readEncodedStrip method and corresponding unit tests
magedrifaat <magedrifaat@gmail.com>
parents: 31156
diff changeset
2035
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2036 DEFUN (__tiff_write__, args, ,
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2037 "Write the image data to the current IFD")
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2038 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2039 #if defined (HAVE_TIFF)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2040 int nargin = args.length ();
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2041
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2042 if (nargin < 2)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2043 error ("Wrong number of arguments\n");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2044
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2045 TIFF *tif = (TIFF *)(args(0).uint64_value ());
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2046
31162
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
2047 check_readonly (tif);
31153
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2048
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2049 // Obtain all necessary tags
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2050 tiff_image_data image_data (tif);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2051
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2052 uint16_t sample_format;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2053 if (! TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format))
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2054 error ("Failed to obtain a value for sample format");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2055
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2056 if (sample_format == 3)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2057 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2058 if (image_data.bits_per_sample != 32
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2059 && image_data.bits_per_sample != 64)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2060 error ("Floating point images are only supported for bit depths of 32 and 64");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2061 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2062 else if (sample_format != 1 && sample_format != 4)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2063 error ("Unsupported sample format");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2064
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2065 switch (image_data.bits_per_sample)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2066 {
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2067 case 1:
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2068 // We need to check for both scalar and matrix types to handle single
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2069 // pixel image
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2070 if (args (1).is_bool_scalar () || args (1).is_bool_matrix ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2071 write_image<boolNDArray> (tif, args (1).bool_array_value (),
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2072 &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2073 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2074 error ("Expected logical matrix for BiLevel image");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2075 break;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2076 case 8:
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2077 if (args (1).is_uint8_type ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2078 write_image<uint8NDArray> (tif, args (1).uint8_array_value (),
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2079 &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2080 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2081 error ("Only uint8 data is allowed for uint images with bit depth of 8");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2082 break;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2083 case 16:
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2084 if (args (1).is_uint16_type ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2085 write_image<uint16NDArray> (tif, args (1).uint16_array_value (),
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2086 &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2087 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2088 error ("Only uint16 data is allowed for uint images with bit depth of 16");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2089 break;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2090 case 32:
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2091 if (sample_format == 3)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2092 if (args (1).is_single_type () || args (1).is_double_type ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2093 write_image<FloatNDArray> (tif, args (1).float_array_value (),
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2094 &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2095 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2096 error ("Only single and double data are allowed for floating-point images");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2097 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2098 if (args (1).is_uint32_type ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2099 write_image<uint32NDArray> (tif, args (1).uint32_array_value (),
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2100 &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2101 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2102 error ("Only uint32 data is allowed for uint images with bit depth of 32");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2103 break;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2104 case 64:
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2105 if (sample_format == 3)
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2106 if (args (1).is_single_type () || args (1).is_double_type ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2107 write_image<NDArray> (tif, args (1).array_value (), &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2108 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2109 error ("Only single and double data are allowed for floating-point images");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2110 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2111 if (args (1).is_uint64_type ())
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2112 write_image<uint64NDArray> (tif, args (1).uint64_array_value (),
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2113 &image_data);
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2114 else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2115 error ("Only uint64 data is allowed for uint images with bit depth of 64");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2116 break;
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2117 default:
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2118 error ("Unsupported bit depth");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2119 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2120
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2121 return octave_value_list ();
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2122 #else
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2123 err_disabled_feature ("write", "Tiff");
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2124 #endif
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2125 }
c66d6c7f025e Tiff: implemented write method for stripped images
magedrifaat <magedrifaat@gmail.com>
parents: 31152
diff changeset
2126
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2127 DEFUN (__tiff_write_encoded_strip__, args, ,
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2128 "Write an encoded strip to the image")
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2129 {
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2130 #if defined (HAVE_TIFF)
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2131 int nargin = args.length ();
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2132
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2133 // TODO(maged): add support for YCbCr data
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2134 if (nargin < 3)
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2135 error ("Too few arguments provided\n");
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2136
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2137 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2138
31162
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
2139 check_readonly (tif);
31138
68762676dab1 Tiff writeEncodedStrip: prevent writing to a read-only file
magedrifaat <magedrifaat@gmail.com>
parents: 31137
diff changeset
2140
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2141 // Obtain all necessary tags
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2142 tiff_image_data image_data (tif);
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2143
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2144 if (image_data.is_tiled)
31143
a68f2dadafee Tiff.m: added all remaining tests for implemented features.
magedrifaat <magedrifaat@gmail.com>
parents: 31142
diff changeset
2145 error ("Can't write strips to a tiled image");
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2146
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2147 uint32_t strip_no = args(1).uint32_scalar_value ();
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2148 if (strip_no < 1 || strip_no > TIFFNumberOfStrips (tif))
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2149 error ("Strip number out of range");
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2150
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2151 handle_write_strip_or_tile (tif, strip_no, args(2), &image_data);
31126
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2152
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2153 return octave_value_list ();
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2154 #else
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2155 err_disabled_feature ("writeEncodedStrip", "Tiff");
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2156 #endif
7851c5b9c950 Tiff: implemented writeEncodedStrip function for writing a strip to an image
magedrifaat <magedrifaat@gmail.com>
parents: 31125
diff changeset
2157 }
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2158
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2159 DEFUN (__tiff_write_encoded_tile__, args, ,
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2160 "Write an encoded tile to the image")
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2161 {
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2162 #if defined (HAVE_TIFF)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2163 int nargin = args.length ();
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2164
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2165 // TODO(maged): add support for YCbCr data
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2166 if (nargin < 3)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2167 error ("Too few arguments provided\n");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2168
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2169 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2170
31162
28817158ca86 Tiff: changed write functions to use fcntl wrappers.
magedrifaat <magedrifaat@gmail.com>
parents: 31161
diff changeset
2171 check_readonly (tif);
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2172
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2173 // Obtain all necessary tags
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2174 tiff_image_data image_data (tif);
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2175
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2176 if (! image_data.is_tiled)
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2177 error ("Can't write tiles to a stripped image");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2178
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2179 uint32_t tile_no = args(1).uint32_scalar_value ();
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2180 if (tile_no < 1 || tile_no > TIFFNumberOfTiles (tif))
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2181 error ("Tile number out of range");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2182
31158
f2ae7763739a Tiff: added writeEncodedTile method to read tiles from image
magedrifaat <magedrifaat@gmail.com>
parents: 31157
diff changeset
2183 handle_write_strip_or_tile (tif, tile_no, args(2), &image_data);
31146
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2184
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2185 return octave_value_list ();
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2186 #else
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2187 err_disabled_feature ("writeEncodedTile", "Tiff");
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2188 #endif
50402b8dfb4a Tiff: added writeEncodedTile function for writing tiled images
magedrifaat <magedrifaat@gmail.com>
parents: 31145
diff changeset
2189 }
31148
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2190
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2191 DEFUN (__tiff_is_tiled__, args, ,
31148
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2192 "Get whether the image is tiled")
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2193 {
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2194 #if defined (HAVE_TIFF)
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2195 int nargin = args.length ();
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2196
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2197 if (nargin == 0)
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2198 error ("No handle provided\n");
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2199
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2200 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31148
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2201 bool is_tiled = static_cast<bool> (TIFFIsTiled (tif));
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2202 return octave_value_list (octave_value (is_tiled));
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2203 #else
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2204 err_disabled_feature ("isTiled", "Tiff");
31148
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2205 #endif
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2206 }
4bc9a1938f9a Tiff: added isTiled method to check if the image is tiled or stripped
magedrifaat <magedrifaat@gmail.com>
parents: 31147
diff changeset
2207
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2208 DEFUN (__tiff_number_of_strips__, args, ,
31149
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2209 "Get the number of strips in the image")
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2210 {
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2211 #if defined (HAVE_TIFF)
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2212 int nargin = args.length ();
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2213
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2214 if (nargin == 0)
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2215 error ("No handle provided\n");
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2216
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2217 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2218
31149
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2219 if (TIFFIsTiled (tif))
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2220 error ("The image is tiled not stripped");
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2221
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2222 double strip_count = static_cast<double> (TIFFNumberOfStrips (tif));
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2223 return octave_value_list (octave_value (strip_count));
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2224 #else
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2225 err_disabled_feature ("numberOfStrips", "Tiff");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2226 #endif
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2227 }
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2228
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2229 DEFUN (__tiff_number_of_tiles__, args, ,
31151
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2230 "Get the number of tiles in the image")
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2231 {
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2232 #if defined (HAVE_TIFF)
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2233 int nargin = args.length ();
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2234
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2235 if (nargin == 0)
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2236 error ("No handle provided\n");
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2237
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2238 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2239
31151
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2240 if (! TIFFIsTiled (tif))
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2241 error ("The image is stripped not tiled");
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2242
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2243 double tile_count = static_cast<double> (TIFFNumberOfTiles (tif));
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2244 return octave_value_list (octave_value (tile_count));
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2245 #else
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2246 err_disabled_feature ("numberOfTiles", "Tiff");
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2247 #endif
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2248 }
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2249
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2250 DEFUN (__tiff_compute_strip__, args, ,
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2251 "Get the strip index containing the given row")
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2252 {
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2253 #if defined (HAVE_TIFF)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2254 int nargin = args.length ();
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2255
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2256 if (nargin < 2 || nargin > 3)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2257 error ("Wrong number of arguments\n");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2258
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2259 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2260
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2261 if (TIFFIsTiled (tif))
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2262 error ("The image is tiled not stripped");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2263
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2264 tiff_image_data image_data (tif);
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2265
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2266 uint32_t row = args(1).uint32_scalar_value ();
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2267 if (row > image_data.height)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2268 row = image_data.height;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2269
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2270 // Convert from 1-based to zero-based indexing but avoid underflow
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2271 if (row > 0)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2272 row--;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2273
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2274 uint16_t plane;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2275 if (nargin > 2)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2276 {
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2277 if (image_data.planar_configuration == PLANARCONFIG_CONTIG)
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2278 error ("Can't use plane argument for images with chunky PlanarConfiguration");
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2279 plane = args(2).uint16_scalar_value ();
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2280 if (plane > image_data.samples_per_pixel)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2281 plane = image_data.samples_per_pixel;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2282 if (plane > 0)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2283 plane--;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2284 }
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2285 else
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2286 {
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2287 if (image_data.planar_configuration == PLANARCONFIG_SEPARATE)
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2288 error ("The plane argument is required for images with separate PlanarConfiguration");
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2289 plane = 0;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2290 }
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2291
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2292 double strip_number = TIFFComputeStrip (tif, row, plane) + 1;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2293 if (strip_number > TIFFNumberOfStrips (tif))
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2294 strip_number = TIFFNumberOfStrips (tif);
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2295 return octave_value_list (octave_value (strip_number));
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2296 #else
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2297 err_disabled_feature ("computeStrip", "Tiff");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2298 #endif
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2299 }
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2300
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2301 DEFUN (__tiff_compute_tile__, args, ,
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2302 "Get the tile index containing the given row and column")
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2303 {
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2304 #if defined (HAVE_TIFF)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2305 int nargin = args.length ();
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2306
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2307 if (nargin < 2 || nargin > 3)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2308 error ("Wrong number of arguments\n");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2309
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2310 TIFF *tif = (TIFF *)(args(0).uint64_value ());
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2311
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2312 if (! TIFFIsTiled (tif))
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2313 error ("The image is stripped not tiled");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2314
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2315 uint32NDArray coords = args(1).uint32_array_value ();
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2316 if (coords.dim2() < 2)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2317 error ("Coordinates must be in the shape [row, col]");
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2318 uint32_t row = coords(0, 0);
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2319 uint32_t col = coords(0, 1);
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2320
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2321 tiff_image_data image_data (tif);
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2322
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2323 if (col > image_data.width)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2324 col = image_data.width;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2325 if (row > image_data.height)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2326 row = image_data.height;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2327
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2328 // Convert from 1-based to zero-based indexing but avoid underflow
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2329 if (row > 0)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2330 row--;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2331 if (col > 0)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2332 col--;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2333
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2334 uint16_t plane;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2335 if (nargin > 2)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2336 {
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2337 if (image_data.planar_configuration == PLANARCONFIG_CONTIG)
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2338 error ("Can't use plane argument for images with chunky PlanarConfiguration");
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2339 plane = args(2).uint16_scalar_value ();
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2340 if (plane > image_data.samples_per_pixel)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2341 plane = image_data.samples_per_pixel;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2342 if (plane > 0)
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2343 plane--;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2344 }
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2345 else
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2346 {
31152
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2347 if (image_data.planar_configuration == PLANARCONFIG_SEPARATE)
2244617f4da5 Tiff computeStrip: fixed inconsistency in checking plane argument.
magedrifaat <magedrifaat@gmail.com>
parents: 31151
diff changeset
2348 error ("The plane argument is required for images with separate PlanarConfiguration");
31150
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2349 plane = 0;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2350 }
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2351
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2352 double tile_number = TIFFComputeTile (tif, col, row, 0, plane) + 1;
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2353 if (tile_number > TIFFNumberOfTiles (tif))
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2354 tile_number = TIFFNumberOfTiles (tif);
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2355 return octave_value_list (octave_value (tile_number));
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2356 #else
6bede2d6f273 Tiff: added computeStrip and computeTile methods
magedrifaat <magedrifaat@gmail.com>
parents: 31149
diff changeset
2357 err_disabled_feature ("computeTile", "Tiff");
31149
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2358 #endif
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2359 }
d4dbc69f301e Tiff: implemented numberOfStrips method
magedrifaat <magedrifaat@gmail.com>
parents: 31148
diff changeset
2360
31160
5f0c3da75926 Tiff: moved internal functions to corefcn.
magedrifaat <magedrifaat@gmail.com>
parents: 31159
diff changeset
2361 DEFUN (__tiff_version__, , ,
31151
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2362 "Get the version stamp of LibTIFF")
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2363 {
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2364 #if defined (HAVE_TIFF)
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2365 std::string version = std::string (TIFFGetVersion ());
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2366 return octave_value_list (octave_value (version));
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2367 #else
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2368 err_disabled_feature ("getVersion", "Tiff");
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2369 #endif
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2370 }
6fb54834aa93 Tiff: added numberOfTiles and getVersion methods
magedrifaat <magedrifaat@gmail.com>
parents: 31150
diff changeset
2371
31161
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2372 DEFUN (__tiff_set_errors_enabled__, args, ,
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2373 "Enables or Disables error output from LibTIFF")
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2374 {
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2375 #if defined (HAVE_TIFF)
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2376 if (args (0).bool_value ())
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2377 TIFFSetErrorHandler (tiff_default_handler);
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2378 else if (tiff_default_handler == NULL)
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2379 tiff_default_handler = TIFFSetErrorHandler (NULL);
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2380 else
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2381 TIFFSetErrorHandler (NULL);
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2382
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2383 return octave_value_list ();
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2384 #else
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2385 err_disabled_feature ("setErrorEnabled", "Tiff");
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2386 #endif
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2387 }
b731c8f6db95 Tiff: added setLibTIFFErrorsEnabled method to enable/disable LibTIFF errors
magedrifaat <magedrifaat@gmail.com>
parents: 31160
diff changeset
2388
31092
a736190ce738 Added the Tiff classdef files to octave
magedrifaat <magedrifaat@gmail.com>
parents:
diff changeset
2389 }