This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp.h
277 lines (205 loc) · 6.62 KB
/
bmp.h
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Author: Christian Vallentin <mail@vallentinsource.com>
// Website: http://vallentinsource.com
// Repository: https://github.com/MrVallentin/LoadBMP
//
// Date Created: January 03, 2014
// Last Modified: August 13, 2016
//
// Version: 1.1.0
// Copyright (c) 2014-2016 Christian Vallentin <mail@vallentinsource.com>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
// Include loadbmp.h as following
// to create the implementation file.
//
// #define LOADBMP_IMPLEMENTATION
// #include "loadbmp.h"
#ifndef LOADBMP_H
#define LOADBMP_H
#ifdef __cplusplus
extern "C" {
#endif
// Errors
#define LOADBMP_NO_ERROR 0
#define LOADBMP_OUT_OF_MEMORY 1
#define LOADBMP_FILE_NOT_FOUND 2
#define LOADBMP_FILE_OPERATION 3
#define LOADBMP_INVALID_FILE_FORMAT 4
#define LOADBMP_INVALID_SIGNATURE 5
#define LOADBMP_INVALID_BITS_PER_PIXEL 6
// Components
#define LOADBMP_RGB 3
#define LOADBMP_RGBA 4
#ifdef LOADBMP_IMPLEMENTATION
# define LOADBMP_API
#else
# define LOADBMP_API extern
#endif
// LoadBMP uses raw buffers and support RGB and RGBA formats.
// The order is RGBRGBRGB... or RGBARGBARGBA..., from top left
// to bottom right, without any padding.
LOADBMP_API unsigned int loadbmp_decode_file(
const char *filename, unsigned char **imageData, unsigned int *width, unsigned int *height, unsigned int components);
LOADBMP_API unsigned int loadbmp_encode_file(
const char *filename, const unsigned char *imageData, unsigned int width, unsigned int height, unsigned int components);
#ifdef LOADBMP_IMPLEMENTATION
// Disable Microsoft Visual C++ compiler security warnings for fopen, strcpy, etc being unsafe
#if defined(_MSC_VER) && (_MSC_VER >= 1310)
# pragma warning(disable: 4996)
#endif
#include <stdlib.h> /* malloc(), free() */
#include <string.h> /* memset(), memcpy() */
#include <stdio.h> /* fopen(), fwrite(), fread(), fclose() */
LOADBMP_API unsigned int loadbmp_decode_file(
const char *filename, unsigned char **imageData, unsigned int *width, unsigned int *height, unsigned int components)
{
FILE *f = fopen(filename, "rb");
if (!f)
return LOADBMP_FILE_NOT_FOUND;
unsigned char bmp_file_header[14];
unsigned char bmp_info_header[40];
unsigned char bmp_pad[3];
unsigned int w, h;
unsigned char *data = NULL;
unsigned int x, y, i, padding;
memset(bmp_file_header, 0, sizeof(bmp_file_header));
memset(bmp_info_header, 0, sizeof(bmp_info_header));
if (fread(bmp_file_header, sizeof(bmp_file_header), 1, f) == 0)
{
fclose(f);
return LOADBMP_INVALID_FILE_FORMAT;
}
if (fread(bmp_info_header, sizeof(bmp_info_header), 1, f) == 0)
{
fclose(f);
return LOADBMP_INVALID_FILE_FORMAT;
}
if ((bmp_file_header[0] != 'B') || (bmp_file_header[1] != 'M'))
{
fclose(f);
return LOADBMP_INVALID_SIGNATURE;
}
if ((bmp_info_header[14] != 24) && (bmp_info_header[14] != 32))
{
fclose(f);
return LOADBMP_INVALID_BITS_PER_PIXEL;
}
w = (bmp_info_header[4] + (bmp_info_header[5] << 8) + (bmp_info_header[6] << 16) + (bmp_info_header[7] << 24));
h = (bmp_info_header[8] + (bmp_info_header[9] << 8) + (bmp_info_header[10] << 16) + (bmp_info_header[11] << 24));
if ((w > 0) && (h > 0))
{
data = (unsigned char*)malloc(w * h * components);
if (!data)
{
fclose(f);
return LOADBMP_OUT_OF_MEMORY;
}
for (y = (h - 1); y != -1; y--)
{
for (x = 0; x < w; x++)
{
i = (x + y * w) * components;
if (fread(data + i, 3, 1, f) == 0)
{
free(data);
fclose(f);
return LOADBMP_INVALID_FILE_FORMAT;
}
data[i] ^= data[i + 2] ^= data[i] ^= data[i + 2]; // BGR -> RGB
if (components == LOADBMP_RGBA)
data[i + 3] = 255;
}
padding = ((4 - (w * 3) % 4) % 4);
if (fread(bmp_pad, 1, padding, f) != padding)
{
free(data);
fclose(f);
return LOADBMP_INVALID_FILE_FORMAT;
}
}
}
(*width) = w;
(*height) = h;
(*imageData) = data;
fclose(f);
return LOADBMP_NO_ERROR;
}
LOADBMP_API unsigned int loadbmp_encode_file(
const char *filename, const unsigned char *imageData, unsigned int width, unsigned int height, unsigned int components)
{
FILE *f = fopen(filename, "wb");
if (!f)
return LOADBMP_FILE_OPERATION;
unsigned char bmp_file_header[14] = { 'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0 };
unsigned char bmp_info_header[40] = { 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0 };
const unsigned char bmp_pad[3] = { 0, 0, 0 };
const unsigned int size = 54 + width * height * 3; // 3 as the BMP format uses 3 channels (red, green, blue and NO alpha)
unsigned int x, y, i, padding;
unsigned char pixel[3];
bmp_file_header[2] = (unsigned char)(size);
bmp_file_header[3] = (unsigned char)(size >> 8);
bmp_file_header[4] = (unsigned char)(size >> 16);
bmp_file_header[5] = (unsigned char)(size >> 24);
bmp_info_header[4] = (unsigned char)(width);
bmp_info_header[5] = (unsigned char)(width >> 8);
bmp_info_header[6] = (unsigned char)(width >> 16);
bmp_info_header[7] = (unsigned char)(width >> 24);
bmp_info_header[8] = (unsigned char)(height);
bmp_info_header[9] = (unsigned char)(height >> 8);
bmp_info_header[10] = (unsigned char)(height >> 16);
bmp_info_header[11] = (unsigned char)(height >> 24);
if (fwrite(bmp_file_header, 14, 1, f) == 0)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}
if (fwrite(bmp_info_header, 40, 1, f) == 0)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}
for (y = (height - 1); y != -1; y--)
{
for (x = 0; x < width; x++)
{
i = (x + y * width) * components;
memcpy(pixel, imageData + i, sizeof(pixel));
pixel[0] ^= pixel[2] ^= pixel[0] ^= pixel[2]; // RGB -> BGR
if (fwrite(pixel, sizeof(pixel), 1, f) == 0)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}
}
padding = ((4 - (width * 3) % 4) % 4);
if (fwrite(bmp_pad, 1, padding, f) != padding)
{
fclose(f);
return LOADBMP_FILE_OPERATION;
}
}
fclose(f);
return LOADBMP_NO_ERROR;
}
#endif
#ifdef __cplusplus
}
#endif
#endif