lib/png/src/decode/model.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2
3 pub const Header = struct {
4 width: u32,
5 height: u32,
6 bit_depth: u8,
7 color_type: u8,
8 };
9
10 pub const Image = struct {
11 width: u32,
12 height: u32,
13 rgba8: []u8,
14 };
15
16 pub const DecodeError = error{
17 InvalidSignature,
18 InvalidChunk,
19 UnsupportedFormat,
20 MissingPalette,
21 InvalidFilter,
22 TruncatedImage,
23 };
24
25 pub const Exhaustion = error{
26 InputByteCapacityExceeded,
27 ImagePixelCapacityExceeded,
28 SourceRowByteCapacityExceeded,
29 IdatByteCapacityExceeded,
30 };
31
32 pub const Error = DecodeError || Exhaustion || error{
33 CapacityOverflow,
34 DecodeStorageInUse,
35 DecodeInputMismatch,
36 };
37
38 pub fn validDepth(color_type: u8, bit_depth: u8) bool {
39 return switch (color_type) {
40 0 => bit_depth == 1 or bit_depth == 2 or bit_depth == 4 or
41 bit_depth == 8 or bit_depth == 16,
42 3 => bit_depth == 1 or bit_depth == 2 or bit_depth == 4 or bit_depth == 8,
43 2, 4, 6 => bit_depth == 8 or bit_depth == 16,
44 else => false,
45 };
46 }
47
48 pub fn samplesPerPixel(color_type: u8) usize {
49 return switch (color_type) {
50 0, 3 => 1,
51 4 => 2,
52 2 => 3,
53 6 => 4,
54 else => 0,
55 };
56 }
57
58 pub fn added(left: usize, right: usize) error{CapacityOverflow}!usize {
59 return std.math.add(usize, left, right) catch error.CapacityOverflow;
60 }
61
62 pub fn multiplied(left: usize, right: usize) error{CapacityOverflow}!usize {
63 return std.math.mul(usize, left, right) catch error.CapacityOverflow;
64 }
65
66 pub fn toUsize(value: anytype) error{CapacityOverflow}!usize {
67 return std.math.cast(usize, value) orelse error.CapacityOverflow;
68 }