annotate libinterp/corefcn/__tiff__.cc @ 31166:b8b6cc05c8ea

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