| 1 | /* |
| 2 | * api.cpp |
| 3 | * Copyright (C) 2020-2023 Brendan Hack ([email protected]) |
| 4 | * This file is part of a Frei0r plugin that applies a kaleidoscope |
| 5 | * effect. |
| 6 | * Version 1.1 july 2023 |
| 7 | * |
| 8 | * Implements the Frei0r interface |
| 9 | * |
| 10 | * This source code is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This source code is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Please refer |
| 18 | * to the GNU Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Public License along |
| 21 | * with this source code; if not, write to: Free Software Foundation, |
| 22 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | * |
| 24 | */ |
| 25 | #include "frei0r.hpp" |
| 26 | #include "ikaleid0sc0pe.h" |
| 27 | |
| 28 | class kaleid0sc0pe : public frei0r::filter |
| 29 | { |
| 30 | public: |
| 31 | kaleid0sc0pe(unsigned int width, unsigned int height): |
| 32 | m_kaleid0sc0pe(libkaleid0sc0pe::IKaleid0sc0pe::factory(width,height,1,4)), |
| 33 | m_origin_x(0.5), |
| 34 | m_origin_y(0.5), |
| 35 | m_segmentation(16/128.0), |
| 36 | m_direction(1.0), |
| 37 | m_corner(0), |
| 38 | m_corner_direction(true), |
| 39 | m_reflect_edges(true), |
| 40 | m_edge_threshold(0), |
| 41 | m_bg_alpha(1), |
| 42 | m_specify_source(false), |
| 43 | m_source_segment(0), |
| 44 | m_multithreaded(true), |
| 45 | m_threads(0) |
| 46 | { |
| 47 | m_bg_colour.r = 1.0; |
| 48 | m_bg_colour.g = 0.0; |
| 49 | m_bg_colour.b = 1.0; |
| 50 | |
| 51 | register_param(m_origin_x, |
| 52 | "origin_x", |
| 53 | "origin of the kaleid0sc0pe in x. default 0.5"); |
| 54 | register_param(m_origin_y, |
| 55 | "origin_y", |
| 56 | "origin of the kaleid0sc0pe in y. default 0.5"); |
| 57 | register_param(m_segmentation, |
| 58 | "segmentation", |
| 59 | "kaleid0sc0pe segmentation / 128, segmentations of 1, 2 or multiples of 4 work best. default 16/128"); |
| 60 | register_param(m_specify_source, |
| 61 | "specify_source", |
| 62 | "if true then source angle is read from source_segment, otherwise auto-calculated"); |
| 63 | register_param(m_source_segment, |
| 64 | "source_segment", |
| 65 | "centre of source segment if specify_source is true. 0 is in +x and rotates counter clockwise"); |
| 66 | register_param(m_direction, |
| 67 | "segmentation_direction", |
| 68 | "segmentation direction, < 1/3 is none, < 2/3 is counter clockwise, otherwise clockwise"); |
| 69 | register_param(m_reflect_edges, |
| 70 | "reflect_edges", |
| 71 | "if true then reflections that end up outside the source reflect back into it, otherwise the specified background colour is used."); |
| 72 | register_param(m_edge_threshold, |
| 73 | "edge_threshold", |
| 74 | "edge threshold / 4, reflections outside the image but within this distance clamp to the edge. default 0"); |
| 75 | register_param(m_corner, |
| 76 | "preferred_corner", |
| 77 | "preferred corner, 0 is top right, 0.25 top left, 0.5 bottom left, 0.75 bottom right"); |
| 78 | register_param(m_corner_direction, |
| 79 | "corner_search", |
| 80 | "if true search clockwise for furthest corner, otherwise counter clockwise"); |
| 81 | register_param(m_bg_colour, |
| 82 | "bg_color", |
| 83 | "colour to use if reflection lies outside of source image and not reflecting back in. default 1,0,1"); |
| 84 | register_param(m_bg_alpha, |
| 85 | "bg_alpha", |
| 86 | "alpha to use if reflection lies outside of source image and not reflecting back in. default 1"); |
| 87 | register_param(m_multithreaded, |
| 88 | "multithreaded", |
| 89 | "set to true to enable multithreaded calculation. default true"); |
| 90 | register_param(m_threads, |
| 91 | "n_threads", |
| 92 | "the number of threads to use, if 0 then autocalculate otherwise value * 32. default 0"); |
| 93 | |
| 94 | |
| 95 | m_kaleid0sc0pe->set_background_colour(m_background); |
| 96 | } |
| 97 | |
| 98 | virtual void update(double time, |
| 99 | uint32_t* out, |
| 100 | const uint32_t* in) { |
| 101 | |
| 102 | update_params(); |
| 103 | m_kaleid0sc0pe->process(in, out); |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | void update_params() |
| 108 | { |
| 109 | m_kaleid0sc0pe->set_origin(static_cast<float>(m_origin_x), static_cast<float>(m_origin_y)); |
| 110 | m_kaleid0sc0pe->set_segmentation(static_cast<std::uint32_t>(m_segmentation * 128)); |
| 111 | if (m_direction < 1/3.0) { |
| 112 | m_kaleid0sc0pe->set_segment_direction(libkaleid0sc0pe::IKaleid0sc0pe::Direction::NONE); |
| 113 | } else if (m_direction < 2/3.0) { |
| 114 | m_kaleid0sc0pe->set_segment_direction(libkaleid0sc0pe::IKaleid0sc0pe::Direction::ANTICLOCKWISE); |
| 115 | } else { |
| 116 | m_kaleid0sc0pe->set_segment_direction(libkaleid0sc0pe::IKaleid0sc0pe::Direction::CLOCKWISE); |
| 117 | } |
| 118 | if (m_corner < 0.25) { |
| 119 | m_kaleid0sc0pe->set_preferred_corner(libkaleid0sc0pe::IKaleid0sc0pe::Corner::TR); |
| 120 | } else if (m_corner < 0.5) { |
| 121 | m_kaleid0sc0pe->set_preferred_corner(libkaleid0sc0pe::IKaleid0sc0pe::Corner::TL); |
| 122 | } else if (m_corner < 0.75) { |
| 123 | m_kaleid0sc0pe->set_preferred_corner(libkaleid0sc0pe::IKaleid0sc0pe::Corner::BL); |
| 124 | } else { |
| 125 | m_kaleid0sc0pe->set_preferred_corner(libkaleid0sc0pe::IKaleid0sc0pe::Corner::BR); |
| 126 | } |
| 127 | if (m_corner_direction) { |
| 128 | m_kaleid0sc0pe->set_preferred_corner_search_direction(libkaleid0sc0pe::IKaleid0sc0pe::Direction::CLOCKWISE); |
| 129 | } else { |
| 130 | m_kaleid0sc0pe->set_preferred_corner_search_direction(libkaleid0sc0pe::IKaleid0sc0pe::Direction::ANTICLOCKWISE); |
| 131 | } |
| 132 | m_kaleid0sc0pe->set_reflect_edges(m_reflect_edges); |
| 133 | m_kaleid0sc0pe->set_edge_threshold(static_cast<std::uint32_t>(m_edge_threshold * 4)); |
| 134 | |
| 135 | if (m_specify_source) { |
| 136 | m_kaleid0sc0pe->set_source_segment(static_cast<float>(m_source_segment) * 3.141592654f * 2); |
| 137 | } else { |
| 138 | m_kaleid0sc0pe->set_source_segment(-1); |
| 139 | } |
| 140 | if (m_multithreaded) { |
| 141 | m_kaleid0sc0pe->set_threading(static_cast<std::uint32_t>(m_threads * 32)); |
| 142 | } else { |
| 143 | m_kaleid0sc0pe->set_threading(1); |
| 144 | } |
| 145 | m_background[0] = static_cast<std::uint8_t>(m_bg_colour.r * 255); |
| 146 | m_background[1] = static_cast<std::uint8_t>(m_bg_colour.g * 255); |
| 147 | m_background[2] = static_cast<std::uint8_t>(m_bg_colour.b * 255); |
| 148 | m_background[3] = static_cast<std::uint8_t>(m_bg_alpha * 255); |
| 149 | } |
| 150 | |
| 151 | double m_origin_x; |
| 152 | double m_origin_y; |
| 153 | |
| 154 | double m_segmentation; |
| 155 | double m_direction; |
| 156 | |
| 157 | double m_corner; |
| 158 | bool m_corner_direction; |
| 159 | |
| 160 | bool m_reflect_edges; |
| 161 | |
| 162 | double m_edge_threshold; |
| 163 | |
| 164 | f0r_param_color m_bg_colour; |
| 165 | double m_bg_alpha; |
| 166 | |
| 167 | bool m_specify_source; |
| 168 | double m_source_segment; |
| 169 | |
| 170 | bool m_multithreaded; |
| 171 | double m_threads; |
| 172 | |
| 173 | std::uint8_t m_background[4]; |
| 174 | |
| 175 | std::unique_ptr<libkaleid0sc0pe::IKaleid0sc0pe> m_kaleid0sc0pe; |
| 176 | }; |
| 177 | |
| 178 | frei0r::construct<kaleid0sc0pe> plugin("Kaleid0sc0pe", "Applies a kaleid0sc0pe effect", "Brendan Hack", 1, 1, F0R_COLOR_MODEL_RGBA8888); |