-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbmp.zig
242 lines (214 loc) · 7.82 KB
/
bmp.zig
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
// Read the BMP/Windows bitmap file format and convert the bitmap data to a
// form that can be put in a display buffer.
//
// For reference:
// https://en.wikipedia.org/wiki/BMP_file_format
const std = @import("std");
const utils = @import("utils.zig");
const BaseError = error {
BmpInvalidFile,
BmpUnsupportedEncoding,
BmpUnreadHeader,
BmpAlreadyRead,
} || utils.Error;
const magic = "BM";
const BmpHeader = packed struct {
magic0: u8 = magic[0],
magic1: u8 = magic[1],
file_size: u32,
reserved: u32 = 0,
data_offset: u32,
fn check(self: *const BmpHeader) BaseError!void {
const sz = @sizeOf(BmpHeader);
if (self.magic0 != magic[0] or self.magic1 != magic[1] or
self.file_size <= sz or self.data_offset <= sz or
self.data_offset >= self.file_size) {
return BaseError.BmpInvalidFile;
}
}
};
const Encoding = enum(u32) {
Rgb = 0,
RunLenEnc8 = 1,
RunLenEnc16 = 2,
Rgba = 3,
_,
};
// The "Device-independent bitmap" header apparently is a separate struct in
// the Windows C API.
const DibHeader = packed struct {
dib_header_size: u32 = @sizeOf(DibHeader),
width: u32,
height: u32,
planes: u16,
bits_per_pixel: u16,
encoding: Encoding, // "compression"
image_size: u32,
x_pixels_per_meter: u32,
y_pixels_per_meter: u32,
color_table_count: u32,
important_color_count: u32,
fn check(self: *const DibHeader) BaseError!void {
if (self.dib_header_size < @sizeOf(DibHeader) or self.width == 0 or self.height == 0 or
self.planes == 0 or self.bits_per_pixel == 0) {
return BaseError.BmpInvalidFile;
}
if (self.encoding != .Rgba) {
// TODO: That's what Gimp and ImageMagick seem to produce
return BaseError.BmpUnsupportedEncoding;
}
if (@intCast(usize, self.width) * self.height * self.bits_per_pixel / 8 != self.image_size) {
// TODO: image_size might be 0?
return BaseError.BmpInvalidFile;
}
}
};
pub fn Bmp(comptime File: type) type {
return struct {
const Self = @This();
const Reader = File.Reader;
const SeekableStream = File.SeekableStream;
pub const Error = BaseError || Reader.Error || SeekableStream.SeekError;
reader: Reader,
seekable_stream: SeekableStream,
headers_read: bool = false,
bmp_header: BmpHeader = undefined,
dib_header: DibHeader = undefined,
pub fn init(file: *File) Self {
return .{
.reader = file.reader(),
.seekable_stream = file.seekableStream(),
};
}
fn read_header_i(self: *Self, comptime Type: type, value: *Type) Error!*Type {
const count = try self.reader.read(std.mem.asBytes(value));
if (count < @sizeOf(Type)) {
return Error.BmpInvalidFile;
}
return value;
}
pub fn read_header(self: *Self) Error!void {
try self.seekable_stream.seekTo(0);
try (try self.read_header_i(BmpHeader, &self.bmp_header)).check();
try (try self.read_header_i(DibHeader, &self.dib_header)).check();
self.headers_read = true;
}
pub fn image_size_pixels(self: *Self) Error!utils.U32Point {
if (!self.headers_read) try self.read_header();
return utils.U32Point{.x = self.dib_header.width, .y = self.dib_header.height};
}
pub fn image_size_bytes(self: *Self) Error!utils.U32Point {
return (try self.image_size_pixels()).multiply(self.dib_header.bits_per_pixel).divide(8);
}
pub fn image_size_bytes_total(self: *Self) Error!usize {
if (!self.headers_read) try self.read_header();
return self.dib_header.image_size;
}
pub fn read_bitmap(self: *Self, pos: *usize, buffer: []u8) Error!?usize {
if (buffer.len == 0) {
return Error.NotEnoughDestination;
}
// NOTE: BMP data is "bottom-up":
// https://devblogs.microsoft.com/oldnewthing/20210525-00/?p=105250
// Each row is in the expected order from left lsb to right msb,
// but the most bottom row is first in the bitmap data of the file.
// We need to supply the expected order to the buffer.
// TODO: Each row is aligned to 4 bytes. The padding for this would
// need to be omitted from the output. Right now though we only
// support 32 bpp RGBA, which won't have the padding.
const total_expected = try self.image_size_bytes_total();
const width: usize = self.dib_header.width * self.dib_header.bits_per_pixel / 8;
const data_end: usize = self.bmp_header.data_offset + total_expected;
var got: usize = 0;
while (got < buffer.len and pos.* < total_expected) {
const row = pos.* / width;
const col = @mod(pos.*, width);
const seek_to = data_end - width * (row + 1) + col;
const count = @minimum(width - col, buffer.len - got);
try self.seekable_stream.seekTo(seek_to);
if ((try self.reader.read(buffer[got..got + count])) != count) {
return Error.BmpInvalidFile;
}
got += count;
pos.* += count;
}
return if (got == 0) null else got;
}
};
}
test "read test.bmp" {
var file = try std.fs.cwd().openFile("misc/test.bmp", .{.read = true});
defer file.close();
var bmp = Bmp(@TypeOf(file)).init(&file);
try bmp.read_header();
// std.debug.print("BMP STRUCT: {}\n", .{bmp});
var bitmap = [_]u8{0} ** 1024;
var buffer: [129]u8 = undefined;
var pos: usize = 0;
while (try bmp.read_bitmap(&pos, buffer[0..])) |got| {
for (buffer[0..got]) |byte, i| {
bitmap[pos - got + i] = byte;
}
}
// std.debug.print("BITMAP: {}\n", .{utils.fmt_dump_hex(bitmap[0..])});
var expected_ascii =
" " ++
" @@@@@@ " ++
" @@@@@@@@ " ++
" @@@@@@@@@@ " ++
" @@@@@@@@@@@@ " ++
" @@@@-@@@@-@@@@ " ++
" @@@---@@---@@@ " ++
" @@@-@@@@-@@@@@ " ++
" @@@@@@@@@@@@@@ " ++
" @@@@@@@@@@@@@@ " ++
" @@@@@@@@@@@@@@ " ++
" @@@------@@@ " ++
" @@@@@@@@@@ " ++
" @@@@@@@@ " ++
" @@@@@@ " ++
" ";
var expected: [1024]u8 = undefined;
for (expected_ascii) |ascii_pixel, i| {
var e = expected[i * 4..(i + 1) * 4];
switch (ascii_pixel) {
' ' => { // Transparent
e[0] = 0;
e[1] = 0;
e[2] = 0;
e[3] = 0;
},
'@' => { // Yellow
e[0] = 0;
e[1] = 0xff;
e[2] = 0xff;
e[3] = 0xff;
},
'-' => { // Black
e[0] = 0;
e[1] = 0;
e[2] = 0;
e[3] = 0xff;
},
else => @panic("Unexpected \"expected\" ASCII"),
}
}
if (false) {
for (std.mem.bytesAsSlice(u32, bitmap[0..])) |p, i| {
const c: u8 = switch (p) {
0 => ' ',
0xff000000 => '-',
0xffffff00 => '@',
else => {
std.debug.print("GOT pixel value {x}\n", .{p});
@panic("??");
},
};
std.debug.print("{c}", .{c});
if (@mod(i, 16) == 0) {
std.debug.print("\n", .{});
}
}
}
try utils.expect_equal_bytes(expected[0..], bitmap[0..]);
}