summaryrefslogtreecommitdiff
path: root/include/tinyff/image/png.h
blob: 571c75d7f65ad14483d0772f22c919c48be4744e (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
#ifndef PNG_H
#define PNG_H

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

#include <tinyff/tinyff.h>
#include <tinyff/common.h>
#include <tinyff/stream.h>
#include <tinyff/image/generic.h>
#include <tinyff/dbg.h>



static const unsigned char PNG_SIGNATURE[8] = {
    0x89, 'P', 'N', 'G', 0x0D, 0x0A, 0x1A, 0x0A
};

typedef enum {
    FF_PNG_MODE_NONE = 0,
    FF_PNG_MODE_DIRECT_COLOR, // pixels
    FF_PNG_MODE_PALETTE       // palette
} ff_png_mode;

typedef struct {
    FF_BASE
    
    // Raw stream handle
    ff_stream *raw;

    // Image dimensions
    uint32_t width;
    uint32_t height;

    // Color information
    uint8_t bit_depth;
    uint8_t color_type;

    // Interlace method
    uint8_t interlace_method;

    // Image data
    
    ff_png_mode image_mode;

    union {
        uint8_t* pixels;
        uint8_t* imap; // Map of all the indices defines in PLTE
    } data;

    // Palette (if needed; can be left NULL if it's direct color)
    uint8_t* palette;
    uint32_t palette_size;

} ff_png_ctx;

// Small BpP helper function

static inline uint16_t ff_png_bpp(ff_png_ctx *ctx);


typedef ff_result (*ff_png_chunk_handler_ptr)(ff_ctx* ctx, uint8_t *buf, size_t len, ff_png_ctx* png_ctx);

typedef struct {
    const char *type;
    ff_png_chunk_handler_ptr handler;
} ff_png_chunk_handler;

// Handler declarations
// Massive W.I.P

// Required by definition
ff_result ff_png_header_handler(ff_ctx* ctx, uint8_t *buf, size_t len, ff_png_ctx* png_ctx); // IHDR
ff_result ff_png_palette_handler(ff_ctx* ctx, uint8_t *buf, size_t len, ff_png_ctx* png_ctx); // PLTE
ff_result ff_png_data_handler(ff_ctx* ctx, uint8_t *buf, size_t len, ff_png_ctx* png_ctx); // IDAT
ff_result ff_png_end_handler(ff_ctx* ctx, uint8_t *buf, size_t len, ff_png_ctx* png_ctx); // IEND

// Ancillary chunks
ff_result ff_png_trans_handler(ff_ctx* ctx, uint8_t *buf, size_t len, ff_png_ctx* png_ctx); // tRNS


extern const ff_png_chunk_handler ff_png_chunk_handlers[];





ff_result ff_png_normalize(ff_ctx* ctx, ff_png_ctx *png_ctx, ff_image_ctx **out_data);

#endif