Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
sizes.cpp
Go to the documentation of this file.
1#include <haio_convert.hpp>
2#include <haio_util.hpp>
3
4namespace Haio {
5
7 if (size.width <= 0 || size.height <= 0) {
8 return std::unexpected(Error{ErrorCode::InvalidInput, "image needs positive dimensions"});
9 }
10
11 const auto pixels = static_cast<size_t>(size.width) * static_cast<size_t>(size.height);
12
13 // the addressable colours are just their stride, which strideOf already knows
14 if (const auto stride = strideOf(color); stride != 0) return pixels * stride;
15
16 switch (color) {
17 case Color::ETC1: return Util::GPU::etc1Size(size);
18
19 /**
20 * sixteen bytes for every eight by eight tile, which works out to two bits a
21 * pixel. the sides have to be whole tiles: a chr file has no way to say that
22 * the last row is short, so an odd size would be read past the end.
23 */
24 case Color::CHR_NES: {
25 if (size.width % 8 != 0 || size.height % 8 != 0) {
26 return std::unexpected(Error{ErrorCode::InvalidInput,
27 "nes chr is made of 8x8 tiles, so both sides must be multiples of 8"});
28 }
29 return (static_cast<size_t>(size.width) / 8) * (static_cast<size_t>(size.height) / 8) * 16;
30 }
31
32 /**
33 * three planes: a full size luma and two half size chroma. the halves are
34 * why the dimensions have to be even, and refusing an odd one here is better
35 * than rounding it and handing back a picture a line short.
36 */
37 case Color::YUV420: {
38 if (size.width % 2 != 0 || size.height % 2 != 0) {
39 return std::unexpected(Error{ErrorCode::InvalidInput,
40 "yuv420 needs even width and height"});
41 }
42 const auto chroma = (static_cast<size_t>(size.width) / 2) * (static_cast<size_t>(size.height) / 2);
43 return pixels + chroma * 2;
44 }
45 default: break;
46 }
47 return std::unexpected(Error{ErrorCode::UnsupportedFormat, "unknown colour"});
48}
49
50}
constexpr size_t etc1Size(Size size)
Definition haio.hpp:11
std::expected< T, Error > Result
Result< size_t > sizeOf(Color color, Size size)
Definition sizes.cpp:6
constexpr size_t strideOf(Color color)