Mercurial > octave-libtiff
comparison scripts/image/private/__tiff_imfinfo__.m @ 31201:e5e8cb049b4b
__tiff_imfinfo__: converted to private octave function
* libinterp/corefcn/__tiff__.cc: removed internal function __tiff__imfinfo__.
* scipts/image/private/__tiff_imfinfo__.m: implemented private octave function
__tiff_imfinfo__.
* scripts/image/module.mk: added entry for the new function.
author | magedrifaat <magedrifaat@gmail.com> |
---|---|
date | Fri, 02 Sep 2022 20:52:47 +0200 |
parents | |
children | be6ccdcd5775 |
comparison
equal
deleted
inserted
replaced
31200:4e8152ccc61a | 31201:e5e8cb049b4b |
---|---|
1 function info = __tiff_imfinfo__ (filename) | |
2 tif = Tiff (filename); | |
3 dir_count = tif.numberOfDirectories (); | |
4 | |
5 info = struct (); | |
6 | |
7 if __have_feature__ ("MAGICK") | |
8 ## Obtain data from magick if available (as it handles EXIF tags) | |
9 info = __imfinfo__ (filename); | |
10 else | |
11 ## Otherwise just obtain common tags and ignore EXIF tags | |
12 [file_stat, err, msg] = stat (filename); | |
13 if err > 0 | |
14 error ("imfinfo: Failed to access file: %s", msg); | |
15 endif | |
16 | |
17 for dir_idx = 1:dir_count | |
18 info(dir_idx).Filename = filename; | |
19 info(dir_idx).FileModDate = strftime ("%e-%b-%Y %H:%M:%S", | |
20 localtime (file_stat.ctime)); | |
21 info(dir_idx).FileSize = file_stat.size; | |
22 info(dir_idx).Format = "tif"; | |
23 info(dir_idx).FormatVersion = ""; | |
24 endfor | |
25 endif | |
26 | |
27 for dir_idx = 1:dir_count | |
28 tif.setDirectory (dir_idx); | |
29 | |
30 ## Read the file signature (first 4 bytes) | |
31 a = fopen (filename); | |
32 info(dir_idx).FormatSignature = reshape (fread (a, 4, "uint8"), [1, 4]); | |
33 fclose (a); | |
34 | |
35 info(dir_idx).Width = tif.getTag (Tiff.TagID.ImageWidth); | |
36 info(dir_idx).Height = tif.getTag (Tiff.TagID.ImageLength); | |
37 | |
38 info(dir_idx).SamplesPerPixel = tif.getTag (Tiff.TagID.SamplesPerPixel); | |
39 info(dir_idx).BitsPerSample = tif.getTag (Tiff.TagID.BitsPerSample); | |
40 info(dir_idx).BitDepth = info(dir_idx).SamplesPerPixel ... | |
41 * info(dir_idx).BitsPerSample; | |
42 | |
43 planar = tif.getTag (Tiff.TagID.PlanarConfiguration); | |
44 if planar == Tiff.PlanarConfiguration.Chunky | |
45 info(dir_idx).PlanarConfiguration = "Chunky"; | |
46 elseif planar == Tiff.PlanarConfiguration.Separate | |
47 info(dir_idx).PlanarConfiguration = "Separate"; | |
48 else | |
49 info(dir_idx).PlanarConfiguration = "Unrecognized"; | |
50 endif | |
51 | |
52 info(dir_idx).Colormap = []; | |
53 | |
54 photometrics = [Tiff.Photometric.MinIsBlack, Tiff.Photometric.MinIsWhite,... | |
55 Tiff.Photometric.RGB, Tiff.Photometric.Palette,... | |
56 Tiff.Photometric.Separated]; | |
57 color_types = {"grayscale", "grayscale", "truecolor", "indexed", "cmyk"}; | |
58 photometric_strs = {"BlackIsZero", "WhiteIsZero", "RGB",... | |
59 "RGB Palette", "CMYK"}; | |
60 | |
61 photometric = get_tag_defaulted (tif, Tiff.TagID.Photometric, | |
62 Tiff.Photometric.MinIsBlack); | |
63 idx = (photometric == photometrics); | |
64 if ! any (idx) | |
65 info(dir_idx).ColorType = "undefined"; | |
66 info(dir_idx).PhotometricInterpretation = "undefined"; | |
67 else | |
68 info(dir_idx).ColorType = color_types{idx}; | |
69 info(dir_idx).PhotometricInterpretation = photometric_strs{idx}; | |
70 if (photometric == Tiff.Photometric.Palette) | |
71 info(dir_idx).Colormap ... | |
72 = get_tag_defaulted (tif, Tiff.TagID.ColorMap, []); | |
73 endif | |
74 endif | |
75 | |
76 ## FIXME: implement isBigEndian | |
77 # if (tif.isBigEndian ()) | |
78 # info(dir_idx).ByteOrder = "big-endian"; | |
79 # else | |
80 # info(dir_idx).ByteOrder = "little-endian"; | |
81 # endif | |
82 | |
83 info(dir_idx).NewSubFileType ... | |
84 = get_tag_defaulted (tif, Tiff.TagID.SubFileType, | |
85 Tiff.SubFileType.Default); | |
86 | |
87 | |
88 info(dir_idx).Compression = "unrecognized"; | |
89 compressions = fieldnames (Tiff.Compression); | |
90 compression = tif.getTag (Tiff.TagID.Compression); | |
91 for comp_idx = 1:numel (compressions) | |
92 if (compression == Tiff.Compression.(compressions{comp_idx})) | |
93 info(dir_idx).Compression = compressions{comp_idx}; | |
94 break; | |
95 endif | |
96 endfor | |
97 | |
98 info(dir_idx).TileLength ... | |
99 = get_tag_defaulted (tif, Tiff.TagID.TileLength, []); | |
100 info(dir_idx).TileWidth ... | |
101 = get_tag_defaulted (tif, Tiff.TagID.TileWidth, []); | |
102 | |
103 info(dir_idx).TileOffsets ... | |
104 = get_tag_defaulted (tif, Tiff.TagID.TileOffsets, []); | |
105 info(dir_idx).TileByteCounts ... | |
106 = get_tag_defaulted (tif, Tiff.TagID.TileByteCounts, []); | |
107 | |
108 info(dir_idx).RowsPerStrip ... | |
109 = get_tag_defaulted (tif, Tiff.TagID.RowsPerStrip, []); | |
110 info(dir_idx).StripOffsets ... | |
111 = get_tag_defaulted (tif, Tiff.TagID.StripOffsets, []); | |
112 info(dir_idx).StripByteCounts ... | |
113 = get_tag_defaulted (tif, Tiff.TagID.StripByteCounts, []); | |
114 | |
115 info(dir_idx).XResolution ... | |
116 = get_tag_defaulted (tif, Tiff.TagID.XResolution, []); | |
117 info(dir_idx).YResolution ... | |
118 = get_tag_defaulted (tif, Tiff.TagID.YResolution, []); | |
119 | |
120 info(dir_idx).ResolutionUnit = "Inch"; | |
121 try | |
122 resunit = tif.getTag (tiff.TagID.ResolutionUnit); | |
123 if (resunit == Tiff.ResolutionUnit.None) | |
124 info(dir_idx).ResolutionUnit = "None"; | |
125 elseif (resuint == Tiff.ResolutionUnit.Centimeter) | |
126 info(dir_idx).ResolutionUnit = "Centimeter"; | |
127 endif | |
128 end_try_catch | |
129 | |
130 info(dir_idx).Orientation ... | |
131 = get_tag_defaulted (tif, Tiff.TagID.Orientation, | |
132 Tiff.Orientation.TopLeft); | |
133 | |
134 info(dir_idx).FillOrder ... | |
135 = get_tag_defaulted (tif, Tiff.TagID.FillOrder, 1); | |
136 | |
137 gray_unit = get_tag_defaulted (tif, Tiff.TagID.GrayResponseUnit, 2); | |
138 info(dir_idx).GrayResponseUnit = 1 / (10 ^ gray_unit); | |
139 | |
140 info(dir_idx).MinSampleValue ... | |
141 = get_tag_defaulted (tif, Tiff.TagID.MinSampleValue, 0); | |
142 info(dir_idx).MaxSampleValue ... | |
143 = get_tag_defaulted (tif, Tiff.TagID.MaxSampleValue, | |
144 2^info(dir_idx).BitsPerSample - 1); | |
145 info(dir_idx).MinSampleValue ... | |
146 = repmat (info(dir_idx).MinSampleValue, | |
147 [1, info(dir_idx).SamplesPerPixel]); | |
148 info(dir_idx).MaxSampleValue ... | |
149 = repmat (info(dir_idx).MaxSampleValue, | |
150 [1, info(dir_idx).SamplesPerPixel]); | |
151 | |
152 info(dir_idx).Thresholding ... | |
153 = get_tag_defaulted (tif, Tiff.TagID.Thresholding, | |
154 Tiff.Thresholding.BiLevel); | |
155 | |
156 ## FIXME: implement getDirectoryOffset method | |
157 # info(dir_idx).Offset = tif.getDirectoryOffset (); | |
158 | |
159 info(dir_idx).ImageDescription ... | |
160 = get_tag_defaulted (tif, Tiff.TagID.ImageDescription, ""); | |
161 | |
162 info(dir_idx).Photoshop ... | |
163 = get_tag_defaulted (tif, Tiff.TagID.Photoshop, []); | |
164 | |
165 info(dir_idx).XMP ... | |
166 = get_tag_defaulted (tif, Tiff.TagID.XMP, ""); | |
167 endfor | |
168 endfunction | |
169 | |
170 function tag_val = get_tag_defaulted (tif, tag, default_val) | |
171 tag_val = default_val; | |
172 try | |
173 tag_val = tif.getTag (tag); | |
174 end_try_catch | |
175 endfunction |