Haio 0.2.X
Homebrew Asset IO/CDN
Loading...
Searching...
No Matches
haio_pipeline.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "haio_codec.hpp"
4#include "haio_common.hpp"
5#include "haio_formats.hpp"
6#include "haio_object.hpp"
7#include "haio_palette.hpp"
8#include "haio_transform.hpp"
9
10#include <chrono>
11#include <optional>
12#include <span>
13#include <unordered_map>
14
15namespace Haio {
16
17enum class TokenKind {
18 Source,
19 DecodeAuto,
21 Crop,
22 Resize,
23 Radius,
24 Palette,
26};
27
28struct Token {
30 std::string bucket;
31 std::string path;
33
34 /**
35 * Encode only: which colour to store inside the container, when somebody named
36 * one. nothing named is not the same as rgba8888: it means the container picks,
37 * which for most of them is the only colour they write anyway.
38 */
39 std::optional<Color> color;
40
43
44 /** Resize only: a share of the incoming picture, when the size was written as one */
45 int percent = 0;
46 int radius = 0;
47
48 /** Palette only: which colours, how to fit into them, and how many to keep */
49 std::string palette;
51 size_t limit = 0;
53};
54
55/** a conversion described at runtime, which is what a url query builds */
56class Pipeline {
57public:
59 const std::vector<Token>& tokens() const;
60
61private:
62 std::vector<Token> tokens_;
63};
64
65namespace Tokens {
66Token Source(std::string bucket, std::string path);
68Token Decode(Format format);
69Token Crop(Rect rect);
70Token Resize(Size size);
71Token ResizeByPercent(int percent);
72Token Radius(int radius);
73Token Palette(std::string palette, Dither dither, size_t limit, Limit limitHow);
74Token Encode(Format format, std::optional<Color> color = std::nullopt);
75}
76
77Format formatFromExtension(std::string_view path);
78Format formatFromContentType(std::string_view contentType);
79std::string_view extensionFor(Format format);
80std::string_view contentTypeFor(Format format);
81
82/**
83 * a deadline of none lets it run as long as it takes.
84 *
85 * it is checked between stages rather than inside them, so the bound is really "one
86 * more stage after the clock runs out". that is enough for what it defends against: a
87 * query naming a thousand operations is stopped after the first one or two, and a
88 * single stage is already bounded by max_size.
89 */
90Result<Blob> runPipeline(Blob input, const Pipeline& pipeline,
91 std::optional<std::chrono::steady_clock::time_point> deadline = std::nullopt);
92
93std::vector<Token> parseQueryTokens(std::string_view query);
94std::unordered_map<std::string, std::string> parseQueryMap(std::string_view query);
95
96}
const std::vector< Token > & tokens() const
Definition pipeline.cpp:16
Pipeline & operator|=(Token token)
Definition pipeline.cpp:11
Token Palette(std::string palette, Dither dither, size_t limit, Limit limitHow)
Definition pipeline.cpp:57
Token Encode(Format format, std::optional< Color > color=std::nullopt)
Definition pipeline.cpp:73
Token Decode(Format format)
Definition pipeline.cpp:32
Token Source(std::string bucket, std::string path)
Definition pipeline.cpp:21
Token Resize(Size size)
Definition pipeline.cpp:44
Token DecodeAuto()
Definition pipeline.cpp:28
Token ResizeByPercent(int percent)
Definition pipeline.cpp:50
Token Crop(Rect rect)
Definition pipeline.cpp:38
Token Radius(int radius)
Definition pipeline.cpp:67
Definition haio.hpp:11
Result< Blob > Encode(Image< Color::RGBA8888 > image, Format to, std::optional< Color > as=std::nullopt)
Format formatFromContentType(std::string_view contentType)
Definition format.cpp:41
std::string bucket
std::optional< Color > color
std::vector< Token > parseQueryTokens(std::string_view query)
Definition pipeline.cpp:100
Format formatFromExtension(std::string_view path)
Definition format.cpp:33
Result< Blob > runPipeline(Blob input, const Pipeline &pipeline, std::optional< std::chrono::steady_clock::time_point > deadline=std::nullopt)
Definition convert.cpp:15
TokenKind kind
std::expected< T, Error > Result
Result< Image< Color::RGBA8888 > > Decode(const Blob &blob)
std::string_view contentTypeFor(Format format)
Definition format.cpp:62
std::string_view extensionFor(Format format)
Definition format.cpp:54
std::string path
std::string palette
std::unordered_map< std::string, std::string > parseQueryMap(std::string_view query)
Definition pipeline.cpp:81