summaryrefslogtreecommitdiff
path: root/bmp.h
blob: a4d794ba93eea69ae82f9b98ed400c511bc76699 (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
/*
 * Copyright (C) 2014 Olivier Gayot <duskcoder@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef BMP_H
#define BMP_H

#include <endian.h>

#include <stdint.h>

struct packed_bmp_header {
    uint16_t magic;
    uint32_t bmp_size;
    uint16_t reserved;
    uint16_t reserved2;
    uint32_t data_offset;
} __attribute__((packed));

struct bitmap {
    uint32_t size;
    uint32_t data_offset;
    uint32_t width;
    uint32_t height;
    uint16_t bpp;
};

/* Windows BITMAPINFOHEADER */
struct bitmap_info_header {
    uint32_t header_size;
    uint32_t bmp_width;
    uint32_t bmp_height;
    uint16_t planes;
    uint16_t bpp;
    uint32_t compression;
    uint32_t img_size;
    uint32_t horizontal_res;
    uint32_t vertical_res;
    uint32_t palette;
    uint32_t important_colors;
} __attribute((packed));

enum {
#if __BYTE_ORDER == __BIG_ENDIAN
    BMP_MAGIC_BM = 0x424D,
    BMP_MAGIC_BA = 0x4241,
    BMP_MAGIC_CI = 0x4349,
    BMP_MAGIC_CP = 0x4350,
    BMP_MAGIC_IC = 0x4943,
    BMP_MAGIC_PT = 0x5054,
#elif __BYTE_ORDER == __LITTLE_ENDIAN
    BMP_MAGIC_BM = 0x4D42,
    BMP_MAGIC_BA = 0x4142,
    BMP_MAGIC_CI = 0x4943,
    BMP_MAGIC_CP = 0x5043,
    BMP_MAGIC_IC = 0x4349,
    BMP_MAGIC_PT = 0x5450,
#endif
};

enum {
    /* bmp header */
    BMP_ESUCCESS,
    BMP_ENOTBMP,
    BMP_ENOTSUPP,

    BMP_EINVALSIZ,

    BMP_EINVALBPP,

    BMP_ERRNO_COUNT,
};

const char *bmp_error_string[] = {
    [BMP_ESUCCESS] = "Success",
    [BMP_ENOTBMP] = "File is not a BMP",
    [BMP_ENOTSUPP] = "File format not supported",
    [BMP_EINVALSIZ] = "Inconsistent file size",
    [BMP_EINVALBPP] = "Invalid BPP value",
};

int bmp_errno = BMP_ESUCCESS;

static inline const char *bmp_strerror(int err)
{
    return bmp_error_string[err];
}

#endif /* BMP_H */