Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_icc.c File Reference

Finding the ICC colour profile embedded in an image file. More...

#include "nilorea/n_icc.h"
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_tiff.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
Include dependency graph for n_icc.c:

Go to the source code of this file.

Macros

#define N_ICC_FIRST_READ   (256u * 1024u)
 Bytes n_icc_from_file() reads first; a profile, when there is one, is almost always in them.
#define N_ICC_MIN_BYTES   132u
 Smallest ICC profile: its 128 byte header and a tag count.
#define N_ICC_MORE   2
 What a parser answers when it ran off the end of the bytes it was given before it could say: the file reader then reads more.
#define N_ICC_TIFF_TAG   34675
 TIFF tag of an embedded ICC profile.

Functions

static size_t _be16 (const unsigned char *p)
 helper: a big endian 16 bit value
static size_t _be32 (const unsigned char *p)
 helper: a big endian 32 bit value
static int _from_buffer (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 helper: n_icc_from_buffer(), with N_ICC_MORE when the bytes ran out before the answer
static int _from_jpeg (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 helper: the profile of a JPEG, from its APP2 chunks put back in order
static int _from_png (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 helper: the profile of a PNG, from its iCCP chunk, inflated
static int _from_tiff (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 helper: the profile of a TIFF held whole in memory
static int _from_webp (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 helper: the profile of a WebP, from its ICCP chunk
static int _give (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 helper: hand a found profile back, a copy of len bytes at data, checked
static size_t _le32 (const unsigned char *p)
 helper: a little endian 32 bit value
static int _tiff_file (FILE *file, unsigned char **icc, size_t *icc_len)
 helper: read the TIFF profile of a file where its directory says, so a large TIFF is not read whole to find a profile at its end
int n_icc_from_buffer (const unsigned char *data, size_t len, unsigned char **icc, size_t *icc_len)
 Find the profile embedded in an image file held in memory.
int n_icc_from_file (const char *path, unsigned char **icc, size_t *icc_len)
 Find the profile embedded in an image file.
int n_icc_is_profile (const unsigned char *data, size_t len)
 Does a buffer look like an ICC profile: at least a header, a size field no larger than the buffer, and "acsp" at byte 36.

Variables

static const char n_icc_jpeg_id [12] = {'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', 0}
 JPEG APP2 identifier of a profile chunk, terminator included.

Detailed Description

Finding the ICC colour profile embedded in an image file.

Author
Castagnier Mickael
Version
1.0
Date
28/09/2026

Definition in file n_icc.c.

Macro Definition Documentation

◆ N_ICC_FIRST_READ

#define N_ICC_FIRST_READ   (256u * 1024u)

Bytes n_icc_from_file() reads first; a profile, when there is one, is almost always in them.

Definition at line 47 of file n_icc.c.

Referenced by n_icc_from_file().

◆ N_ICC_MIN_BYTES

#define N_ICC_MIN_BYTES   132u

Smallest ICC profile: its 128 byte header and a tag count.

Definition at line 39 of file n_icc.c.

Referenced by n_icc_is_profile().

◆ N_ICC_MORE

#define N_ICC_MORE   2

What a parser answers when it ran off the end of the bytes it was given before it could say: the file reader then reads more.

Definition at line 43 of file n_icc.c.

Referenced by _from_jpeg(), _from_png(), _from_webp(), n_icc_from_buffer(), and n_icc_from_file().

◆ N_ICC_TIFF_TAG

#define N_ICC_TIFF_TAG   34675

TIFF tag of an embedded ICC profile.

Definition at line 50 of file n_icc.c.

Referenced by _from_tiff(), and _tiff_file().

Function Documentation

◆ _be16()

size_t _be16 ( const unsigned char * p)
static

helper: a big endian 16 bit value

Definition at line 56 of file n_icc.c.

Referenced by _from_jpeg(), and _tiff_file().

Here is the caller graph for this function:

◆ _be32()

size_t _be32 ( const unsigned char * p)
static

helper: a big endian 32 bit value

Definition at line 61 of file n_icc.c.

Referenced by _from_png(), _tiff_file(), and n_icc_is_profile().

Here is the caller graph for this function:

◆ _from_buffer()

int _from_buffer ( const unsigned char * data,
size_t len,
unsigned char ** icc,
size_t * icc_len )
static

helper: n_icc_from_buffer(), with N_ICC_MORE when the bytes ran out before the answer

Definition at line 293 of file n_icc.c.

References _from_jpeg(), _from_png(), _from_tiff(), and _from_webp().

Referenced by n_icc_from_buffer(), and n_icc_from_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _from_jpeg()

int _from_jpeg ( const unsigned char * data,
size_t len,
unsigned char ** icc,
size_t * icc_len )
static

helper: the profile of a JPEG, from its APP2 chunks put back in order

Definition at line 100 of file n_icc.c.

References _be16(), LOG_ERR, n_icc_is_profile(), n_icc_jpeg_id, N_ICC_MAX_BYTES, N_ICC_MORE, and n_log.

Referenced by _from_buffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _from_png()

int _from_png ( const unsigned char * data,
size_t len,
unsigned char ** icc,
size_t * icc_len )
static

helper: the profile of a PNG, from its iCCP chunk, inflated

Definition at line 186 of file n_icc.c.

References _be32(), LOG_ERR, n_icc_is_profile(), N_ICC_MAX_BYTES, N_ICC_MORE, n_log, and type().

Referenced by _from_buffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _from_tiff()

int _from_tiff ( const unsigned char * data,
size_t len,
unsigned char ** icc,
size_t * icc_len )
static

helper: the profile of a TIFF held whole in memory

Definition at line 276 of file n_icc.c.

References _give(), N_TIFF_ENTRY::count, N_ICC_TIFF_TAG, n_tiff_find(), n_tiff_free(), n_tiff_parse(), N_TIFF_ENTRY::offset, and N_TIFF_ENTRY::type.

Referenced by _from_buffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _from_webp()

int _from_webp ( const unsigned char * data,
size_t len,
unsigned char ** icc,
size_t * icc_len )
static

helper: the profile of a WebP, from its ICCP chunk

Definition at line 255 of file n_icc.c.

References _give(), _le32(), and N_ICC_MORE.

Referenced by _from_buffer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _give()

int _give ( const unsigned char * data,
size_t len,
unsigned char ** icc,
size_t * icc_len )
static

helper: hand a found profile back, a copy of len bytes at data, checked

Returns
1, or -1 when it is not a profile or there is no memory

Definition at line 83 of file n_icc.c.

References LOG_ERR, n_icc_is_profile(), N_ICC_MAX_BYTES, and n_log.

Referenced by _from_tiff(), and _from_webp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _le32()

size_t _le32 ( const unsigned char * p)
static

helper: a little endian 32 bit value

Definition at line 66 of file n_icc.c.

Referenced by _from_webp(), and _tiff_file().

Here is the caller graph for this function:

◆ _tiff_file()

int _tiff_file ( FILE * file,
unsigned char ** icc,
size_t * icc_len )
static

helper: read the TIFF profile of a file where its directory says, so a large TIFF is not read whole to find a profile at its end

Definition at line 322 of file n_icc.c.

References _be16(), _be32(), _le32(), LOG_ERR, n_icc_is_profile(), N_ICC_MAX_BYTES, N_ICC_TIFF_TAG, n_log, and type().

Referenced by n_icc_from_file().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ n_icc_jpeg_id

const char n_icc_jpeg_id[12] = {'I', 'C', 'C', '_', 'P', 'R', 'O', 'F', 'I', 'L', 'E', 0}
static

JPEG APP2 identifier of a profile chunk, terminator included.

Definition at line 53 of file n_icc.c.

Referenced by _from_jpeg().