Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_tiff.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
27
28#include "nilorea/n_tiff.h"
29#include "nilorea/n_common.h"
30#include "nilorea/n_log.h"
31
32#include <string.h>
33
36#define N_TIFF_MAX_ENTRIES 4096
37
41static size_t _n_tiff_type_size(uint16_t type) {
42 switch (type) {
43 case 1: /* BYTE */
44 case 2: /* ASCII */
45 case 6: /* SBYTE */
46 case 7: /* UNDEFINED */
47 return 1;
48 case 3: /* SHORT */
49 case 8: /* SSHORT */
50 return 2;
51 case 4: /* LONG */
52 case 9: /* SLONG */
53 case 11: /* FLOAT */
54 return 4;
55 case 5: /* RATIONAL */
56 case 10: /* SRATIONAL */
57 case 12: /* DOUBLE */
58 return 8;
59 default:
60 return 0;
61 }
62}
63
68static uint16_t _n_tiff_u16(const N_TIFF* tiff, const unsigned char* p) {
69 if (tiff->big_endian)
70 return (uint16_t)((p[0] << 8) | p[1]);
71 return (uint16_t)((p[1] << 8) | p[0]);
72}
73
78static uint32_t _n_tiff_u32(const N_TIFF* tiff, const unsigned char* p) {
79 if (tiff->big_endian)
80 return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3];
81 return ((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) | ((uint32_t)p[1] << 8) | (uint32_t)p[0];
82}
83
88static uint64_t _n_tiff_u64(const N_TIFF* tiff, const unsigned char* p) {
89 uint64_t hi = _n_tiff_u32(tiff, tiff->big_endian ? p : p + 4);
90 uint64_t lo = _n_tiff_u32(tiff, tiff->big_endian ? p + 4 : p);
91 return (hi << 32) | lo;
92}
93
94N_TIFF* n_tiff_parse_ifd(const unsigned char* data, size_t len, size_t ifd) {
95 N_TIFF* tiff = NULL;
96 size_t count = 0;
97 size_t i = 0;
98
99 __n_assert(data, return NULL);
100 if (len < 8)
101 return NULL;
102 Malloc(tiff, N_TIFF, 1);
103 __n_assert(tiff, return NULL);
104 tiff->data = data;
105 tiff->len = len;
106 if (data[0] == 'I' && data[1] == 'I')
107 tiff->big_endian = 0;
108 else if (data[0] == 'M' && data[1] == 'M')
109 tiff->big_endian = 1;
110 else {
111 Free(tiff);
112 return NULL;
113 }
114 tiff->magic = _n_tiff_u16(tiff, data + 2);
115 if (ifd < 8 || ifd > len - 2) {
116 Free(tiff);
117 return NULL;
118 }
119 count = _n_tiff_u16(tiff, data + ifd);
120 if (count == 0 || count > N_TIFF_MAX_ENTRIES || ifd + 2 + count * 12 > len) {
121 Free(tiff);
122 return NULL;
123 }
124 Malloc(tiff->entries, N_TIFF_ENTRY, count);
125 if (!tiff->entries) {
126 Free(tiff);
127 return NULL;
128 }
129 for (i = 0; i < count; i++) {
130 const unsigned char* e = data + ifd + 2 + i * 12;
131 N_TIFF_ENTRY entry;
132 size_t size = 0;
133 size_t bytes = 0;
134
135 entry.tag = _n_tiff_u16(tiff, e);
136 entry.type = _n_tiff_u16(tiff, e + 2);
137 entry.count = _n_tiff_u32(tiff, e + 4);
138 size = _n_tiff_type_size(entry.type);
139 if (size == 0 || entry.count == 0)
140 continue;
141 /* A count so large that its size overflows cannot fit either. */
142 if ((size_t)entry.count > len / size)
143 continue;
144 bytes = size * entry.count;
145 /* Four bytes or less sit in the entry itself; more are at an offset. */
146 if (bytes <= 4)
147 entry.offset = (size_t)(e + 8 - data);
148 else {
149 entry.offset = _n_tiff_u32(tiff, e + 8);
150 if (entry.offset > len || bytes > len - entry.offset)
151 continue;
152 }
153 tiff->entries[tiff->count++] = entry;
154 }
155 return tiff;
156}
157
158N_TIFF* n_tiff_parse(const unsigned char* data, size_t len) {
159 __n_assert(data, return NULL);
160 if (len < 8)
161 return NULL;
162 /* IFD0's offset is the header's last four bytes, in the header's order. */
163 if (data[0] == 'I' && data[1] == 'I')
164 return n_tiff_parse_ifd(data, len, (size_t)data[4] | (size_t)data[5] << 8 | (size_t)data[6] << 16 | (size_t)data[7] << 24);
165 if (data[0] == 'M' && data[1] == 'M')
166 return n_tiff_parse_ifd(data, len, (size_t)data[7] | (size_t)data[6] << 8 | (size_t)data[5] << 16 | (size_t)data[4] << 24);
167 return NULL;
168}
169
170void n_tiff_free(N_TIFF** tiff) {
171 if (!tiff || !*tiff)
172 return;
173 FreeNoLog((*tiff)->entries);
174 Free((*tiff));
175}
176
177const N_TIFF_ENTRY* n_tiff_find(const N_TIFF* tiff, uint16_t tag) {
178 size_t i = 0;
179
180 __n_assert(tiff, return NULL);
181 for (i = 0; i < tiff->count; i++) {
182 if (tiff->entries[i].tag == tag)
183 return &tiff->entries[i];
184 }
185 return NULL;
186}
187
188size_t n_tiff_count(const N_TIFF* tiff, uint16_t tag) {
189 const N_TIFF_ENTRY* entry = n_tiff_find(tiff, tag);
190
191 return entry ? entry->count : 0;
192}
193
194size_t n_tiff_get_doubles(const N_TIFF* tiff, uint16_t tag, double* out, size_t max) {
195 const N_TIFF_ENTRY* entry = NULL;
196 size_t i = 0;
197 size_t n = 0;
198
199 __n_assert(tiff, return 0);
200 __n_assert(out, return 0);
201 entry = n_tiff_find(tiff, tag);
202 if (!entry || entry->type == 2 || entry->type == 7)
203 return 0;
204 n = entry->count < max ? entry->count : max;
205 for (i = 0; i < n; i++) {
206 const unsigned char* p = tiff->data + entry->offset + i * _n_tiff_type_size(entry->type);
207
208 switch (entry->type) {
209 case 1:
210 out[i] = (double)p[0];
211 break;
212 case 6:
213 out[i] = (double)(int8_t)p[0];
214 break;
215 case 3:
216 out[i] = (double)_n_tiff_u16(tiff, p);
217 break;
218 case 8:
219 out[i] = (double)(int16_t)_n_tiff_u16(tiff, p);
220 break;
221 case 4:
222 out[i] = (double)_n_tiff_u32(tiff, p);
223 break;
224 case 9:
225 out[i] = (double)(int32_t)_n_tiff_u32(tiff, p);
226 break;
227 case 5: {
228 uint32_t num = _n_tiff_u32(tiff, p);
229 uint32_t den = _n_tiff_u32(tiff, p + 4);
230
231 out[i] = den ? (double)num / (double)den : 0.0;
232 break;
233 }
234 case 10: {
235 int32_t num = (int32_t)_n_tiff_u32(tiff, p);
236 int32_t den = (int32_t)_n_tiff_u32(tiff, p + 4);
237
238 out[i] = den ? (double)num / (double)den : 0.0;
239 break;
240 }
241 case 11: {
242 uint32_t bits = _n_tiff_u32(tiff, p);
243 float value = 0.0f;
244
245 memcpy(&value, &bits, sizeof(value));
246 out[i] = (double)value;
247 break;
248 }
249 case 12: {
250 uint64_t bits = _n_tiff_u64(tiff, p);
251 double value = 0.0;
252
253 memcpy(&value, &bits, sizeof(value));
254 out[i] = value;
255 break;
256 }
257 default:
258 return i;
259 }
260 }
261 return n;
262}
263
264int n_tiff_get_ascii(const N_TIFF* tiff, uint16_t tag, char* out, size_t out_len) {
265 const N_TIFF_ENTRY* entry = NULL;
266 size_t n = 0;
267 size_t i = 0;
268
269 __n_assert(out, return -1);
270 if (out_len == 0)
271 return -1;
272 out[0] = '\0';
273 __n_assert(tiff, return -1);
274 entry = n_tiff_find(tiff, tag);
275 if (!entry || entry->type != 2)
276 return -1;
277 n = entry->count < out_len - 1 ? entry->count : out_len - 1;
278 for (i = 0; i < n && tiff->data[entry->offset + i] != '\0'; i++)
279 out[i] = (char)tiff->data[entry->offset + i];
280 out[i] = '\0';
281 return 0;
282}
static void type(N_GUI_CTX *gui, const char *s)
Type a whole string into whatever has focus.
#define FreeNoLog(__ptr)
Free Handler without log.
Definition n_common.h:272
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
size_t offset
where the values start in the buffer, already checked to hold them
Definition n_tiff.h:76
uint16_t tag
tag number
Definition n_tiff.h:70
N_TIFF_ENTRY * entries
the entries whose values lie inside the buffer, in file order
Definition n_tiff.h:90
uint16_t type
TIFF field type, 1 BYTE to 12 DOUBLE.
Definition n_tiff.h:72
const unsigned char * data
the buffer, borrowed
Definition n_tiff.h:82
uint32_t count
number of values
Definition n_tiff.h:74
int big_endian
1 for "MM" (Motorola, big endian), 0 for "II"
Definition n_tiff.h:86
size_t len
its length
Definition n_tiff.h:84
size_t count
how many
Definition n_tiff.h:92
uint16_t magic
the 16 bit value after the byte order mark
Definition n_tiff.h:88
N_TIFF * n_tiff_parse(const unsigned char *data, size_t len)
Parse the first directory of a TIFF structured buffer.
Definition n_tiff.c:158
size_t n_tiff_count(const N_TIFF *tiff, uint16_t tag)
How many values a tag has.
Definition n_tiff.c:188
size_t n_tiff_get_doubles(const N_TIFF *tiff, uint16_t tag, double *out, size_t max)
Read a numeric tag as doubles.
Definition n_tiff.c:194
const N_TIFF_ENTRY * n_tiff_find(const N_TIFF *tiff, uint16_t tag)
Find an entry by tag.
Definition n_tiff.c:177
int n_tiff_get_ascii(const N_TIFF *tiff, uint16_t tag, char *out, size_t out_len)
Read an ASCII tag.
Definition n_tiff.c:264
N_TIFF * n_tiff_parse_ifd(const unsigned char *data, size_t len, size_t ifd)
Parse a directory at a given offset of a TIFF structured buffer: a sub-directory such as EXIF's,...
Definition n_tiff.c:94
void n_tiff_free(N_TIFF **tiff)
Release a parse, and set the pointer to NULL.
Definition n_tiff.c:170
The first directory of a TIFF structured buffer.
Definition n_tiff.h:80
One directory entry.
Definition n_tiff.h:68
Common headers and low-level functions & define.
Generic log system.
static size_t _n_tiff_type_size(uint16_t type)
Bytes one value of a field type takes.
Definition n_tiff.c:41
static uint64_t _n_tiff_u64(const N_TIFF *tiff, const unsigned char *p)
Read a 64 bit value in the buffer's byte order.
Definition n_tiff.c:88
#define N_TIFF_MAX_ENTRIES
Most entries a directory is read for: a real TIFF directory holds tens, a DCP a few dozen; a count pa...
Definition n_tiff.c:36
static uint16_t _n_tiff_u16(const N_TIFF *tiff, const unsigned char *p)
Read a 16 bit value in the buffer's byte order.
Definition n_tiff.c:68
static uint32_t _n_tiff_u32(const N_TIFF *tiff, const unsigned char *p)
Read a 32 bit value in the buffer's byte order.
Definition n_tiff.c:78
Reading the tags of a TIFF structured buffer.