summaryrefslogtreecommitdiff
path: root/include/tinyff/image/png.h
blob: f26c7a19af11ca03f3803c254e4547a2005aca22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * @file png.h
 * @brief PNG image format handling for tinyff.
 * @author @TazyFoundSoup
 * 
 * This header defines structure and functions for working with PNG images,
 * including validation and opening PNG files.
 * 
 * @see src/format/image/png.c
 */

#ifndef PNG_H
#define PNG_H

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include "tinyff/result.h"


/**
 * @brief PNG file signature (first 8 bytes)
 * 
 * Used to validate PNG files.
 * Will not be used for users directly.
 * 
 * @see ff_png_isvalid
 */
static const unsigned char PNG_SIGNATURE[8] = {
    0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A
};

/**
 * @brief PNG image context structure
 * 
 * Holds information about a PNG image, including its raw file pointer,
 * dimensions, bit depth, color type, pixel data, and validity status.
 */
typedef struct {
    FILE *raw;
    uint32_t width;
    uint32_t height;
    uint8_t bit_depth;
    uint8_t color_type;
    uint8_t *pixels;
    size_t count_pixels;
    bool valid;
} ff_png_ctx;


/**
 * @brief PNG chunk types
 * 
 * Enumerates the different types of PNG chunks.
 * 
 * @details
 * - FF_PNG_CHUNK_TYPE_IHDR: Image Header Chunk
 * - FF_PNG_CHUNK_TYPE_PLTE: Palette Chunk
 * - FF_PNG_CHUNK_TYPE_IDAT: Image Data Chunk
 * - FF_PNG_CHUNK_TYPE_IEND: Image End Chunk
 * 
 * @see ff_png_chunk
 */
typedef enum {
    FF_PNG_CHUNK_TYPE_IHDR,
    FF_PNG_CHUNK_TYPE_PLTE,
    FF_PNG_CHUNK_TYPE_IDAT,
    FF_PNG_CHUNK_TYPE_IEND,

    // There are more,
    // but these ones will be
    // enough for now
} ff_png_chunk_type;

/**
 * @brief PNG chunk structure
 * 
 * Represents a PNG chunk, including its length, type, data, and CRC.
 * 
 * @see ff_png_chunk_type
 */
typedef struct {
    uint32_t length;
    ff_png_chunk_type type;
    uint32_t *data; // length bytes (maximum is uint32_t)
    uint32_t crc;
} ff_png_chunk;

/**
 * @brief Validate PNG from FILE pointer
 * 
 * Checks necessary validations for PNG files.
 * 
 * @param file An already opened FILE pointer located at start
 * @return A result indicating the success/failure of the validation
 * 
 * @warning FILE pointer must be at the start of the file, or unexpected results may occur.
 */
ff_result ff_png_isvalid(FILE *file);

/**
 * @brief Open a PNG file and create its context
 * 
 * Opens a PNG file, validates it, and prepares its context for further processing.
 * 
 * @param filepath The path to the PNG file
 * @param out_ctx Pointer to store the created PNG context
 * @return A result indicating the success/failure of the operation
 * 
 * @todo Implement parsing
 * @warning The caller is responsible for freeing the allocated ff_png_ctx and its resources.
 * 
 * 
 * @see ff_png_ctx
 */
ff_result ff_open_png(const char *filepath, ff_png_ctx **out_ctx);



#endif