-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClmbsImg_BMP.c
237 lines (182 loc) · 5.54 KB
/
ClmbsImg_BMP.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "ColumbusImage.h"
#include "ClmbsImg_Util.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct
{
uint8_t magic[2]; //Magic Bytes 'B' and 'M'
uint32_t size; //Size of whole file
uint32_t unused; //Should be 0
uint32_t offset; //Offset to bitmap data
} BMP_HEADER;
typedef struct
{
uint32_t infosize; //Size of info struct (40 bytes)
int32_t width; //Width of image
int32_t height; //Height of image
uint16_t planes; //Should be 1
uint16_t bits; //Bits per pixel (1, 4, 8, 16, 24, 32)
uint32_t compression; //0 = none, 1 = 8-bit RLE, 2 = 4-bit RLE
uint32_t size_data; //The image size
uint32_t hres; //Horizontal resolution (pixel per meter)
uint32_t vres; //Vertical resolution (pixel per meter)
uint32_t colors; //Number of palette colors
uint32_t important_colors; //Number of important colors;
} BMP_INFO;
typedef struct
{
uint8_t b; //Blue channel
uint8_t g; //Green channel
uint8_t r; //Red channel
uint8_t a; //Alpha channel
} BMP_PALETTE;
static bool ReadHeader(BMP_HEADER* header, FILE* fp)
{
if (header == NULL || fp == NULL) return false;
if (!ReadUint8(&header->magic[0], fp)) return false;
if (!ReadUint8(&header->magic[1], fp)) return false;
if (!ReadUint32(&header->size, fp)) return false;
if (!ReadUint32(&header->unused, fp)) return false;
if (!ReadUint32(&header->offset, fp)) return false;
return true;
}
static bool WriteHeader(BMP_HEADER header, FILE* fp)
{
if (fp == NULL) return false;
if (!WriteUint8(&header.magic[0], fp)) return false;
if (!WriteUint8(&header.magic[1], fp)) return false;
if (!WriteUint32(&header.size, fp)) return false;
if (!WriteUint32(&header.unused, fp)) return false;
if (!WriteUint32(&header.offset, fp)) return false;
return true;
}
static bool ReadInfo(BMP_INFO* info, FILE* fp)
{
if (info == NULL || fp == NULL) return false;
if (!ReadUint32(&info->infosize, fp)) return false;
if (!ReadInt32(&info->width, fp)) return false;
if (!ReadInt32(&info->height, fp)) return false;
if (!ReadUint16(&info->planes, fp)) return false;
if (!ReadUint16(&info->bits, fp)) return false;
if (!ReadUint32(&info->compression, fp)) return false;
if (!ReadUint32(&info->size_data, fp)) return false;
if (!ReadUint32(&info->hres, fp)) return false;
if (!ReadUint32(&info->vres, fp)) return false;
if (!ReadUint32(&info->colors, fp)) return false;
if (!ReadUint32(&info->important_colors, fp)) return false;
uint8_t* empty = (uint8_t*)malloc(68);
ReadBytes(empty, 68, fp);
free(empty);
return true;
}
static bool WriteInfo(BMP_INFO info, FILE* fp)
{
if (fp == NULL) return false;
if (!WriteUint32(&info.infosize, fp)) return false;
if (!WriteInt32(&info.width, fp)) return false;
if (!WriteInt32(&info.height, fp)) return false;
if (!WriteUint16(&info.planes, fp)) return false;
if (!WriteUint16(&info.bits, fp)) return false;
if (!WriteUint32(&info.compression, fp)) return false;
if (!WriteUint32(&info.size_data, fp)) return false;
if (!WriteUint32(&info.hres, fp)) return false;
if (!WriteUint32(&info.vres, fp)) return false;
if (!WriteUint32(&info.colors, fp)) return false;
if (!WriteUint32(&info.important_colors, fp)) return false;
return true;
}
static bool ReadPalette(BMP_PALETTE* palette, FILE* fp)
{
if (palette == NULL || fp == NULL) return false;
if (!ReadUint8(&palette->b, fp)) return false;
if (!ReadUint8(&palette->g, fp)) return false;
if (!ReadUint8(&palette->r, fp)) return false;
if (!ReadUint8(&palette->a, fp)) return false;
return true;
}
static bool Decode(uint8_t* data, size_t size, size_t bits)
{
switch (bits)
{
case 24:
return bgr2rgb(data, size);
break;
case 32:
return bgra2rgba(data, size);
break;
};
return true;
}
bool ClmbsImg_IsBMP(const char* file)
{
FILE* fp = fopen(file, "rb");
if (fp == NULL)
return false;
unsigned char magic[2];
fread(magic, sizeof(magic), 1, fp);
fclose(fp);
if (magic[0] == 'B' && magic[1] == 'M')
return true;
else
return false;
}
ClmbsImg_Data ClmbsImg_LoadBMP(const char* file)
{
ClmbsImg_Data ret;
FILE* fp = fopen(file, "rb");
if (fp == NULL) return ret;
BMP_HEADER header;
BMP_INFO info;
if (!ReadHeader(&header, fp)) return ret;
if (!ReadInfo(&info, fp)) return ret;
uint8_t* data = malloc(header.size - 68);
fread(data, header.size - 68, 1, fp);
Decode(data, header.size - 68, info.bits);
ret.w = info.width;
ret.h = info.height;
ret.bpp = info.bits / 8;
ret.data = data;
fclose(fp);
return ret;
}
bool ClmbsImg_SaveBMP(const char* file, ClmbsImg_Data data)
{
FILE* fp = fopen(file, "wb");
if (fp == NULL) return false;
BMP_HEADER header;
BMP_INFO info;
header.magic[0] = 'B';
header.magic[1] = 'M';
header.size = data.w * data.h * data.bpp + 54;
header.unused = 0;
header.offset = 54;
info.infosize = 40;
info.width = data.w;
info.height = data.h;
info.planes = 1;
info.bits = data.bpp * 8;
info.compression = 0;
info.size_data = data.w * data.h * data.bpp;
info.hres = 0;
info.vres = 0;
info.colors = 0;
info.important_colors = 0;
if (!WriteHeader(header, fp)) return false;
if (!WriteInfo(info, fp)) return false;
uint8_t* buffer = (uint8_t*)malloc(data.w * data.h * data.bpp);
memcpy(buffer, data.data, data.w * data.h * data.bpp);
Decode(buffer, data.w * data.h * data.bpp, data.bpp * 8);
fwrite(buffer, data.w * data.h * data.bpp, 1, fp);
fclose(fp);
free(buffer);
return true;
}
#ifdef __cplusplus
}
#endif