Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
sizes.cpp
Go to the documentation of this file.
1
#include <
haio_convert.hpp
>
2
#include <
haio_util.hpp
>
3
4
namespace
Haio
{
5
6
Result<size_t>
sizeOf
(
Color
color,
Size
size) {
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
}
haio_convert.hpp
haio_util.hpp
Haio::Util::GPU::etc1Size
constexpr size_t etc1Size(Size size)
Definition
haio_util.hpp:122
Haio
Definition
haio.hpp:11
Haio::Color
Color
Definition
haio_formats.hpp:106
Haio::Color::CHR_NES
@ CHR_NES
Definition
haio_formats.hpp:155
Haio::Color::YUV420
@ YUV420
Definition
haio_formats.hpp:139
Haio::Color::ETC1
@ ETC1
Definition
haio_formats.hpp:138
Haio::Result
std::expected< T, Error > Result
Definition
haio_codec.hpp:32
Haio::sizeOf
Result< size_t > sizeOf(Color color, Size size)
Definition
sizes.cpp:6
Haio::strideOf
constexpr size_t strideOf(Color color)
Definition
haio_codec.hpp:48
Haio::Size::height
int height
Definition
haio_object.hpp:14
Haio::ErrorCode::UnsupportedFormat
@ UnsupportedFormat
Definition
haio_codec.hpp:19
Haio::ErrorCode::InvalidInput
@ InvalidInput
Definition
haio_codec.hpp:18
Haio::Size::width
int width
Definition
haio_object.hpp:13
Haio::Error
Definition
haio_codec.hpp:26
Haio::Size
Definition
haio_object.hpp:12
library
backend
codecs
color
sizes.cpp
Generated by
1.18.0