Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
haio_formats.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <string_view>
5
6namespace Haio {
7
8/**
9 * what a file is: the container that holds pixels. RAW means no container at all,
10 * just the bytes of a colour format, which is why it needs its size supplied.
11 *
12 * the \@mime and \@ext tags are read by scripts/gen_codecs.cpp, which builds the lookups both ways
13 * from them. the first one is what haio writes, the rest are only accepted. this is what
14 * a c++26 enum annotation will carry once the compiler parses them, and until then a
15 * tag keeps them on the enumerator instead of in a table somewhere else.
16 */
17enum class Format {
18 /** no mime type of its own: bare pixels are application/octet-stream */
20 /**
21 * @mime image/png
22 * @ext png
23 */
25 /**
26 * @mime image/x-portable-pixmap image/x-portable-anymap
27 * @ext ppm pgm pbm pnm
28 */
30 /**
31 * @mime image/jpeg
32 * @ext jpg jpeg
33 */
35 /**
36 * @mime image/ktx
37 * @ext ktx
38 */
40 /**
41 * @mime image/ktx2
42 * @ext ktx2
43 */
45 /**
46 * @mime image/vnd-ms.dds image/vnd.ms-dds
47 * @ext dds
48 */
50 /**
51 * @mime image/x-pvr
52 * @ext pvr
53 */
55 /**
56 * @mime image/x-zcis
57 * @ext zcis
58 */
60
61 /**
62 * not files but pictures drawn with escape codes, for looking at one without
63 * leaving the terminal. they only ever encode: nothing arrives as ansi.
64 *
65 * @mime text/x-ansi
66 * @ext ansi
67 */
69 /**
70 * @mime text/x-ansi-halfblock
71 * @ext utf8
72 */
74
75 /**
76 * a cartridge, which is a container like any other: it says where its pattern
77 * data begins and how much of it there is.
78 *
79 * @mime application/x-nes-rom
80 * @ext nes
81 */
83
84 /**
85 * last on purpose. every format above starts with bytes that mean nothing else,
86 * and a tga starts with its own image size: recognising one is a matter of the
87 * header agreeing with itself, so it is asked only after everything that can
88 * prove itself outright has said no.
89 *
90 * @mime image/x-tga image/x-targa image/tga
91 * @ext tga targa tpic
92 */
94
95 /**
96 * @mime image/gif
97 * @ext gif
98 */
100};
101
102/**
103 * how the pixels are laid out. these never appear as a file on their own: ETC1 and
104 * RGB565 are what a gpu container carries, the rest are what codecs hand around.
105 */
106enum class Color {
110
111 /**
112 * five bits a channel packed into two bytes, little endian, with the top bit
113 * spare: 0RRRRRGGGGGBBBBB. it is what a sixteen bit tga holds, and the spare bit
114 * is why the same two bytes are a different colour once somebody declares it to
115 * be alpha.
116 */
118
119 /**
120 * the same two bytes with the alpha at the other end: RRRRRGGGGGBBBBBA. the name
121 * reads from the top bit down, which is the order the bits sit in the sixteen bit
122 * word once it is read little endian.
123 *
124 * one bit of alpha is a yes or a no, so converting to it rounds: half opaque and
125 * up survives, the rest goes clear.
126 */
128
129 /**
130 * blue first, which is how every windows era container stores a pixel. a tga, a
131 * dds and a bmp all write their bytes this way round, so the swap belongs in one
132 * colour rather than inside each of their decoders.
133 */
136
140
141 /**
142 * an index per pixel plus the colours it indexes, which is why this is the one
143 * colour whose Image carries something besides pixels.
144 */
146
147 /**
148 * how a console keeps its indices, which is not one per byte.
149 *
150 * nes chr is two bits a pixel, in eight by eight tiles, with the two bits of a
151 * pixel living in separate planes eight bytes apart. that is a layout and not a
152 * container, the same way ETC1 is, so it is a colour: a .chr file is Format::RAW
153 * holding this, and converting to PALETTE is what unpacks it.
154 */
156};
157
158/**
159 * the other names these layouts go by.
160 *
161 * ffmpeg's -pix_fmt is where most people learned to name a pixel layout on a command
162 * line, so what they already type for it is accepted beside the enumerator's own
163 * name. the list sits next to the enum rather than inside the lookup that reads it,
164 * because the command line grammar prints it too, and a list written in two places is
165 * a list that drifts.
166 */
168 std::string_view spelling;
170};
171
172inline constexpr ColorAlias colorAliases[] = {
173 // short spellings the enumerator name cannot carry until enum annotations land
174 {"rgb", Color::RGB888},
175 {"rgba", Color::RGBA8888},
176 {"rgb24", Color::RGB888},
177 {"bgr24", Color::BGR888},
178 {"bgra", Color::BGRA8888},
179 // the way it tends to get written down when it is listed beside rgba8888
180 {"bgr8888", Color::BGRA8888},
181 {"rgb565le", Color::RGB565},
182 {"rgb555le", Color::RGB555},
183 {"gray", Color::GRAY8},
184 {"grey", Color::GRAY8},
185 {"pal8", Color::PALETTE},
186 // the p is for planar, which is the only way haio keeps it
187 {"yuv420p", Color::YUV420},
188};
189
190}
Definition haio.hpp:11
constexpr ColorAlias colorAliases[]
std::string_view spelling