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
|
const std = @import("std");
pub const Version = packed struct {
major: u8,
minor: u8,
pub fn format(self: @This(), writer: *std.Io.Writer) !void {
try writer.print("v{}.{}", .{self.major, self.minor});
}
};
pub const Unit = enum(u8) {
none = 0,
pixels_per_inch = 1,
pixels_per_cm = 2,
};
const Self = @This();
version: Version,
unit: Unit,
density: struct { x: u16, y: u16 },
thumbnail: struct { x: u8, y: u8 },
const identifier: []const u8 = "JFIF\x00";
pub fn read(reader: *std.Io.Reader) !Self {
const ident = try reader.peekArray(identifier.len);
if (!std.mem.eql(u8, ident, identifier)) {
return error.WrongIdentifier;
}
reader.toss(identifier.len);
var self: Self = undefined;
self.version.major = try reader.takeByte();
self.version.minor = try reader.takeByte();
self.unit = @enumFromInt(try reader.takeByte());
self.density.x = try reader.takeInt(u16, .big);
self.density.y = try reader.takeInt(u16, .big);
self.thumbnail.x = try reader.takeInt(u8, .big);
self.thumbnail.y = try reader.takeInt(u8, .big);
// WARNING: we do not decode the thumbnail yet.
const size = 3 * self.thumbnail.x * self.thumbnail.y;
reader.toss(size);
return self;
}
|