-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbmp_reader.cpp
More file actions
79 lines (54 loc) · 1.69 KB
/
bmp_reader.cpp
File metadata and controls
79 lines (54 loc) · 1.69 KB
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
#include "bmp_reader.hpp"
#include "cio_physfs.hpp"
#include "dib_reader.hpp"
#include "int_pack.hpp"
a5::Bitmap load_bmp(const char* filename, std::uint32_t mask_color)
{
cio::physfs_stream file(filename);
if (!file.is_open())
EOMAP_ERROR("Could not open file %s:\n%s", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
char bm[14];
if (file.read(bm, 14) != 14 || bm[0] != 'B' || bm[1] != 'M')
EOMAP_ERROR("%s is not a valid BMP file", filename);
std::uint32_t bmsize = int_pack_32_le(bm + 2);
if (bmsize < 54)
EOMAP_ERROR("%s is not a valid BMP file", filename);
std::uint32_t dibsize = bmsize - 14;
auto buf = std::make_unique<char[]>(dibsize);
std::size_t total_read = 0;
while (total_read < bmsize - 14)
{
std::size_t bytes_read = file.read(buf.get() + total_read, dibsize - total_read);
if (bytes_read == 0)
{
if (file.error())
EOMAP_ERROR("IO error reading file %s:\n%s", filename, file.errstr());
break;
}
total_read += bytes_read;
}
dib_reader reader(buf.get(), dibsize);
auto check_result = reader.check_format();
if (check_result != nullptr)
EOMAP_ERROR("Can't load %s: %s", filename, check_result);
int w = reader.width();
int h = reader.height();
a5::Bitmap bmp(w, h);
auto dpyfmt = static_cast<ALLEGRO_PIXEL_FORMAT>(
al_get_bitmap_format(bmp)
);
reader.mask_color = mask_color;
auto fmt = static_cast<a5::Pixel_Format::Format>(
reader.start(dpyfmt)
);
auto lock = bmp.Lock(fmt, a5::Bitmap::WriteOnly);
char* start = reinterpret_cast<char*>(lock.Data());
auto pitch = lock.Pitch();
int rows = bmp.Height();
for (int i = 0; i < rows; ++i)
{
char* row = start + pitch * i;
reader.read_line(row, i);
}
return bmp;
}