7 n__tiff_8h
gullradriel edited this page 2026-10-25 10:46:33 +01:00

include/nilorea/n_tiff.h

Reading the tags of a TIFF structured buffer. More...

Structures

Name
struct N_TIFF_ENTRY
One directory entry.
struct N_TIFF
The first directory of a TIFF structured buffer.

Detailed Description

Reading the tags of a TIFF structured buffer.

Author: Castagnier Mickael

Version: 1.0

Date: 28/09/2026

TIFF is a container more than an image format: DNG, Adobe's DCP camera profiles, EXIF blocks and several camera RAW formats are all a header and directories of tagged values. This reads the first directory (IFD0) of such a buffer and hands its values back by tag number, converted: every numeric type as doubles, ASCII as a string. A directory another one points to - the EXIF directory behind tag 0x8769, the GPS one behind 0x8825 - is read the same way with n_tiff_parse_ifd(), from the offset the pointer holds.

Both byte orders are read. The 16 bit magic after the byte order mark is reported rather than checked, since the formats built on TIFF change it (42 for TIFF, 0x4352 "RC" for a DCP, 0x55 for Panasonic RW2, 0x4f52 for an Olympus ORF): the caller decides what it accepts.

Everything is checked against the buffer's length: a directory, a count or an offset that points outside it makes the entry, or the whole parse, fail rather than read out of bounds. The buffer is borrowed and must outlive the N_TIFF.

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#ifndef N_TIFF_HEADER_GUARD
#define N_TIFF_HEADER_GUARD

#ifdef __cplusplus
extern "C" {
#endif


#include <stddef.h>
#include <stdint.h>

#define N_TIFF_MAGIC 42
#define N_TIFF_MAGIC_DCP 0x4352

typedef struct N_TIFF_ENTRY {
    uint16_t tag;
    uint16_t type;
    uint32_t count;
    size_t offset;
} N_TIFF_ENTRY;

typedef struct N_TIFF {
    const unsigned char* data;
    size_t len;
    int big_endian;
    uint16_t magic;
    N_TIFF_ENTRY* entries;
    size_t count;
} N_TIFF;

N_TIFF* n_tiff_parse(const unsigned char* data, size_t len);

N_TIFF* n_tiff_parse_ifd(const unsigned char* data, size_t len, size_t ifd);

void n_tiff_free(N_TIFF** tiff);

const N_TIFF_ENTRY* n_tiff_find(const N_TIFF* tiff, uint16_t tag);

size_t n_tiff_count(const N_TIFF* tiff, uint16_t tag);

size_t n_tiff_get_doubles(const N_TIFF* tiff, uint16_t tag, double* out, size_t max);

int n_tiff_get_ascii(const N_TIFF* tiff, uint16_t tag, char* out, size_t out_len);


#ifdef __cplusplus
}
#endif

#endif