| 1 | /* |
| 2 | * ikaleid0sc0pe.h |
| 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 | * Interface and factory function to the kaleidoscope library |
| 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 | #ifndef SRC_FILTER_KALEID0SC0PE_IKALEID0SC0PE_H_ |
| 26 | #define SRC_FILTER_KALEID0SC0PE_IKALEID0SC0PE_H_ 1 |
| 27 | |
| 28 | #include <cstdint> |
| 29 | #include <memory> |
| 30 | |
| 31 | namespace libkaleid0sc0pe { |
| 32 | class IKaleid0sc0pe; |
| 33 | } |
| 34 | |
| 35 | namespace std |
| 36 | { |
| 37 | |
| 38 | // default delete for IKaleid0sc0pe |
| 39 | // this isn't going to work across an actual shared object boundary but |
| 40 | // not particular worried about that at the moment |
| 41 | template<> |
| 42 | class default_delete<libkaleid0sc0pe::IKaleid0sc0pe> |
| 43 | { |
| 44 | public: |
| 45 | void operator()(libkaleid0sc0pe::IKaleid0sc0pe* p); |
| 46 | }; |
| 47 | |
| 48 | } // namespace std |
| 49 | |
| 50 | namespace libkaleid0sc0pe { |
| 51 | |
| 52 | /** |
| 53 | * Class which implements the kaleid0sc0pe effect |
| 54 | */ |
| 55 | class IKaleid0sc0pe { |
| 56 | public: |
| 57 | /** |
| 58 | * Sets the origin of the kaleid0sc0pe effect. These are given in the range 0 -> 1. |
| 59 | * Values other than 0.5,0.5 may end up reflecting outside the source image. |
| 60 | * These areas will be filled depending on the settings in #set_reflect_edges and |
| 61 | * #set_background_colour. |
| 62 | * Defaults to 0.5, 0.5. |
| 63 | * @param x x coordinate of the origin |
| 64 | * @param y y coordinate of the origin |
| 65 | * @return |
| 66 | * - 0: Success |
| 67 | * - -1: Error |
| 68 | * - -2: Parameter out of range |
| 69 | */ |
| 70 | virtual std::int32_t set_origin(float x, float y) = 0; |
| 71 | |
| 72 | /** |
| 73 | * Returns the origin x coordinate. |
| 74 | */ |
| 75 | virtual float get_origin_x() const = 0; |
| 76 | |
| 77 | /** |
| 78 | * Returns the origin y coordinate. |
| 79 | */ |
| 80 | virtual float get_origin_y() const = 0; |
| 81 | |
| 82 | /** |
| 83 | * Sets the segmentation resulting in \p segmentation * 2 segments in the output frame. |
| 84 | * Segmentation values that are 1, 2 or a multiple of 4, are oriented to an image corner |
| 85 | * and centred will always reflect back to the source segment. |
| 86 | * Other settings may end up reflecting outside the source image. These areas will be filled |
| 87 | * depending on the settings in #set_reflect_edges and #set_background_colour. |
| 88 | * Defaults to 16. |
| 89 | * @param segmentation the segmentation value |
| 90 | * @return |
| 91 | * - 0: Success |
| 92 | * - -1: Error |
| 93 | * - -2: Parameter out of range |
| 94 | */ |
| 95 | virtual std::int32_t set_segmentation(std::uint32_t segmentation) = 0; |
| 96 | |
| 97 | /** |
| 98 | * Returns the segmentation value |
| 99 | */ |
| 100 | virtual std::uint32_t get_segmentation() const = 0; |
| 101 | |
| 102 | /** |
| 103 | * When #set_reflect_edges is not true and a reflected pixel ends up outside the image |
| 104 | * we can clamp the pixels that fall outside the image but within \p threshold pixels |
| 105 | * of the edge to the edge. |
| 106 | * Defaults to 0 |
| 107 | * @param threshold the threshold in pixels. |
| 108 | * @return |
| 109 | * - 0: Success |
| 110 | * - -1: Error |
| 111 | */ |
| 112 | virtual std::int32_t set_edge_threshold(std::uint32_t threshold) = 0; |
| 113 | |
| 114 | /** |
| 115 | * Returns the edge threshold |
| 116 | */ |
| 117 | virtual std::uint32_t get_edge_threshold() const = 0; |
| 118 | |
| 119 | /// Defines a corner |
| 120 | enum class Corner { |
| 121 | TL = 0, //< Top Left |
| 122 | TR, //< Top Right |
| 123 | BR, //< Bottom Right |
| 124 | BL //< Bottom Left |
| 125 | }; |
| 126 | |
| 127 | /// Defines an angular direction |
| 128 | enum class Direction { |
| 129 | CLOCKWISE = 0, //< Clockwise |
| 130 | ANTICLOCKWISE, //< Anti Clockwise |
| 131 | NONE //< No direction |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * Sets the direction that the source segment rotates in. If |
| 136 | * Direction::NONE then the source segment is centred on the corner. |
| 137 | * Otherwise it extends in the given direction. |
| 138 | * Defaults to Direction::NONE |
| 139 | * @param direction the direction |
| 140 | * @return |
| 141 | * - 0: Success |
| 142 | * - -1: Error |
| 143 | */ |
| 144 | virtual std::int32_t set_segment_direction(Direction direction) = 0; |
| 145 | |
| 146 | /** |
| 147 | * Returns the segment direction |
| 148 | */ |
| 149 | virtual Direction get_segment_direction() const = 0; |
| 150 | |
| 151 | /** |
| 152 | * Unless directly specified with #set_source_segment the source segment is always aligned to |
| 153 | * the furthest corner of the image from the origin. |
| 154 | * The source segment has it's edge (or centre) on a line from the origin to the furthest corner, |
| 155 | * and extends in the direction given by #set_segment_direction. |
| 156 | * If multiple corners are equidistant from the origin then this indicates which |
| 157 | * corner is preferred. The algorithm searches from this corner, in the direction |
| 158 | * specified in #set_preferred_corner_search_direction to find the furthest corner. |
| 159 | * Defaults to Corner::BR |
| 160 | * @param corner the preferred corner |
| 161 | * @return |
| 162 | * - 0: Success |
| 163 | * - -1: Error |
| 164 | */ |
| 165 | virtual std::int32_t set_preferred_corner(Corner corner) = 0; |
| 166 | |
| 167 | /** |
| 168 | * Returns the preferred corner |
| 169 | */ |
| 170 | virtual Corner get_preferred_corner() const = 0; |
| 171 | |
| 172 | /** |
| 173 | * The direction to search for the furthest corner in. |
| 174 | * Defaults to Direction::CLOCKWISE |
| 175 | * @param direction the search direction |
| 176 | * @return |
| 177 | * - 0: Success |
| 178 | * - -1: Error |
| 179 | * - -2: Invalid parameter (\p direction cannot by Direction::NONE) |
| 180 | */ |
| 181 | virtual std::int32_t set_preferred_corner_search_direction(Direction direction) = 0; |
| 182 | |
| 183 | /** |
| 184 | * Returns the corner search direction |
| 185 | */ |
| 186 | virtual Direction get_preferred_corner_search_direction() const = 0; |
| 187 | |
| 188 | /** |
| 189 | * Reflected points can end up outside the source image depending on segmentation, |
| 190 | * source segment and origin settings. When this occurs three options are provided, |
| 191 | * lookup the pixel in a reflected tessellation of the original image, set the pixel |
| 192 | * to the background colour provided in #set_background_colour or write nothing to the |
| 193 | * output frame for that pixel. |
| 194 | * Defaults to \c true which is to lookup in the reflected tessellation. |
| 195 | * @param reflect if \c true then lookup in a reflection, if \c false use background color |
| 196 | * or do nothing if no background color was set. |
| 197 | * @return |
| 198 | * - 0: Success |
| 199 | * - -1: Error |
| 200 | */ |
| 201 | virtual std::int32_t set_reflect_edges(bool reflect) = 0; |
| 202 | |
| 203 | /** |
| 204 | * Returns the reflect edges setting |
| 205 | */ |
| 206 | virtual bool get_reflect_edges() const = 0; |
| 207 | |
| 208 | /** |
| 209 | * If not reflecting edges then this sets the colour to use when the kaleid0sc0pe effect |
| 210 | * for a point ends up outside the source image. The data pointed to should be at least as |
| 211 | * wide as a pixel and must be valid for the lifetime of the class instance. |
| 212 | * The caller retains ownership of the passed memory. |
| 213 | * Defaults to \c nullptr |
| 214 | * @param colour the background colour, if \c nullptr then the output buffer is not modified |
| 215 | * if reflection does not land in the source segment. |
| 216 | * @return |
| 217 | * - 0: Success |
| 218 | * - -1: Error |
| 219 | */ |
| 220 | virtual std::int32_t set_background_colour(void* colour) = 0; |
| 221 | |
| 222 | /** |
| 223 | * Returns the background colour |
| 224 | */ |
| 225 | virtual void* get_background_colour() const = 0; |
| 226 | |
| 227 | /** |
| 228 | * Allows to explicitly specify the location of the source segment. 0 radians is in the positive |
| 229 | * horizontal direction and +ve rotates anti-clockwise. |
| 230 | * @param angle If positive or 0 then the angle of the centre of the source segment in radians. If negative |
| 231 | * (the default) then the source segment is auto calculated based on origin, preferred corner, |
| 232 | * direction and corner search direction. |
| 233 | * @return |
| 234 | * - 0: Success |
| 235 | * - -1: Error |
| 236 | */ |
| 237 | virtual std::int32_t set_source_segment(float angle) = 0; |
| 238 | |
| 239 | /** |
| 240 | * Returns the source segment |
| 241 | */ |
| 242 | virtual float get_source_segment() const = 0; |
| 243 | |
| 244 | /** |
| 245 | * Applies the kaleid0sc0pe effect to \p in_frame and returns it in \p out_frame. |
| 246 | * Each parameter must point to enough memory to contain the image specified in the |
| 247 | * constructor and must be aligned to an integer multiple of 16 bytes in memory. |
| 248 | * @param in_frame the input frame to process |
| 249 | * @param out_frame receives the output image |
| 250 | * @return |
| 251 | * - 0: Success |
| 252 | * - -1: Error |
| 253 | * - -2: Invalid parameter (nullptr) |
| 254 | */ |
| 255 | virtual std::int32_t process(const void* in_frame, void* out_frame) = 0; |
| 256 | |
| 257 | /** |
| 258 | * Sets the number of threads to use when processing. |
| 259 | * Default to 0. |
| 260 | * @param threading the number of threads to use. \c 0, calculate automatically, |
| 261 | * otherwise the explicit thread count. |
| 262 | * @return |
| 263 | * - 0: Success |
| 264 | * - -1: Error |
| 265 | */ |
| 266 | virtual std::int32_t set_threading(std::uint32_t threading) = 0; |
| 267 | |
| 268 | /** |
| 269 | * Returns the number of threads to use. |
| 270 | */ |
| 271 | virtual std::uint32_t get_threading() const = 0; |
| 272 | |
| 273 | /** |
| 274 | * Visualises the currently configured segmentation. The pure green segment is the |
| 275 | * source segment. |
| 276 | * @param out_frame receives the output image |
| 277 | * @return |
| 278 | * - 0: Success |
| 279 | * - -1: Error |
| 280 | * - -2: Invalid parameter (nullptr) |
| 281 | */ |
| 282 | virtual std::int32_t visualise(void* out_frame) = 0; |
| 283 | |
| 284 | /** |
| 285 | * Virtual destructor |
| 286 | */ |
| 287 | virtual ~IKaleid0sc0pe() {}; |
| 288 | |
| 289 | /** |
| 290 | * Static factory function. Frame width and height must be a multiple of 4. |
| 291 | * @param width the frame width |
| 292 | * @param height the frame height |
| 293 | * @param component_size the byte size of each frame pixel component |
| 294 | * @param num_components the number of components per pixel |
| 295 | * @param stride the image stride, if \c 0 then calculated as \p width * \p component_size * \p num_components |
| 296 | */ |
| 297 | static std::unique_ptr<IKaleid0sc0pe> factory(std::uint32_t width, std::uint32_t height, std::uint32_t component_size, std::uint32_t num_components, std::uint32_t stride = 0) { |
| 298 | return std::unique_ptr<IKaleid0sc0pe>(create(width, height, component_size, num_components, stride)); |
| 299 | } |
| 300 | |
| 301 | private: |
| 302 | static IKaleid0sc0pe* create(std::uint32_t width, std::uint32_t height, std::uint32_t component_size, std::uint32_t num_components, std::uint32_t stride = 0); |
| 303 | |
| 304 | }; |
| 305 | |
| 306 | } // namespace libkaleid0sc0pe |
| 307 | |
| 308 | #endif // SRC_FILTER_KALEID0SC0PE_IKALEID0SC0PE_H_ |