Haio
0.2.X
Homebrew Asset IO/CDN
Toggle main menu visibility
Loading...
Searching...
No Matches
encode.cpp
Go to the documentation of this file.
1
#include <
haio_codec.hpp
>
2
#include <haio_codecs.hpp>
3
#include <
haio_convert.hpp
>
4
5
#include <string>
6
7
namespace
Haio::Codecs
{
8
9
/**
10
* @addtogroup encode
11
* @{
12
*/
13
/**
14
* takes rgba and drops the alpha on the way out, so the caller does not have to
15
* convert first. the header and the pixels share one buffer: Move writes straight
16
* into the tail instead of building a second image to copy from.
17
*/
18
template
<>
19
Result<Blob>
Encode<Format::PPM, Color::RGBA8888>
(
Image<Color::RGBA8888>
img) {
20
const
Size
size{img.
width
, img.
height
};
21
HAIO_TRY
(incoming,
sizeOf
(
Color::RGBA8888
, size));
22
if
(img.
data
.size() != incoming)
HAIO_FAIL
(
InvalidInput
,
"invalid rgba8888 image for ppm encode"
);
23
24
HAIO_TRY
(pixels,
sizeOf
(
Color::RGB888
, size));
25
const
auto
header =
"P6\n"
+ std::to_string(img.
width
) +
" "
+ std::to_string(img.
height
) +
"\n255\n"
;
26
27
std::vector<uint8_t> out(header.size() + pixels);
28
std::copy(header.begin(), header.end(), out.begin());
29
30
if
(
auto
moved =
Move<Color::RGBA8888, Color::RGB888>
(img.
data
, std::span{out}.subspan(header.size()), size); !moved) {
31
return
std::unexpected(moved.error());
32
}
33
return
Blob
{
Format::PPM
,
Color::RGB888
,
"image/x-portable-pixmap"
, {}, std::move(out)};
34
}
35
/** @} */
36
37
}
Haio::Codecs::Encode< Format::PPM, Color::RGBA8888 >
Result< Blob > Encode< Format::PPM, Color::RGBA8888 >(Image< Color::RGBA8888 > img)
Definition
encode.cpp:19
haio_codec.hpp
HAIO_TRY
#define HAIO_TRY(name, expr)
Definition
haio_codec.hpp:246
HAIO_FAIL
#define HAIO_FAIL(why, message)
Definition
haio_codec.hpp:258
haio_convert.hpp
Haio::Codecs
Definition
haio_codec.hpp:136
Haio::Codecs::Move
Result< void > Move(Bytes src, std::span< uint8_t > dst, Size size)=delete
Haio::Color::RGBA8888
@ RGBA8888
Definition
haio_formats.hpp:107
Haio::Color::RGB888
@ RGB888
Definition
haio_formats.hpp:108
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::Format::PPM
@ PPM
Definition
haio_formats.hpp:29
Haio::ErrorCode::InvalidInput
@ InvalidInput
Definition
haio_codec.hpp:18
Haio::Blob
Definition
haio_codec.hpp:122
Haio::Size
Definition
haio_object.hpp:12
Haio::Image
Definition
haio_codec.hpp:36
Haio::Image::height
int height
Definition
haio_codec.hpp:39
Haio::Image::width
int width
Definition
haio_codec.hpp:38
Haio::Image::data
std::vector< uint8_t > data
Definition
haio_codec.hpp:40
library
backend
codecs
container
ppm
encode.cpp
Generated by
1.18.0