18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-14 06:53:27 +01:00
Files
maszyna/scripting/scriptscreenkernels.h

67 lines
2.4 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <cstddef>
#include <cstdint>
#include <span>
#include <vector>
// image filters the script screens call through the host (led_post, resize_lanczos), run natively rather than
// in the interpreter. they give the very pixels of the Python screens: led_util.render_post() and Pillow's
// resize(..., Image.LANCZOS). the images are RGBA, 4 bytes a pixel, row by row; alpha is ignored and written as 255
namespace script_screens
{
// the size in bytes of an RGBA image
constexpr std::size_t image_size(int const Width, int const Height)
{
return static_cast<std::size_t>(Width) * static_cast<std::size_t>(Height) * 4;
}
// led_util's render_post: every diode becomes 2x2 pixels with a darker grid between them, glowing into its neighbours
// (Bloom), in a 1 pixel black frame. Target takes (2 Width + 2) x (2 Height + 2) pixels. returns false for sizes that
// don't fit the spans
bool led_post(std::span<std::uint8_t const> Source, int Width, int Height, std::span<std::uint8_t> Target, bool Bloom);
// Pillow's resize with the Lanczos filter, horizontally then vertically, each pass rounded to 8 bits. returns false
// for sizes that don't fit the spans
bool resize_lanczos(std::span<std::uint8_t const> Source, int Width, int Height, std::span<std::uint8_t> Target, int TargetWidth, int TargetHeight);
// one step of the post-processing a script asks for on its image (image:led_post, image:resize). the steps of the image
// render returns run on the gpu when the renderer can (script_screens::post_processor), otherwise on the cpu
struct post_step
{
enum class kind
{
led, // led_post
resize // resize_lanczos
};
kind type{kind::led};
bool bloom{true}; // led: with the glow
int width{0}; // resize: the size it gives
int height{0};
};
struct post_image
{
std::vector<std::uint8_t> pixels; // RGBA, from the top row down
int width{0};
int height{0};
};
// the size of an image Width wide, or Height high, after the step
int width_after(post_step const &Step, int Width);
int height_after(post_step const &Step, int Height);
// runs the steps on the cpu, one after another. false for sizes the kernels refuse; the image is then as it was
bool apply_post(post_image &Image, std::span<post_step const> Steps);
} // namespace script_screens