| 1 | /* |
| 2 | * kaleid0sc0pe.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 | * The kaleidoscope class definition. |
| 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_KALEID0SC0PE_H_ |
| 26 | #define SRC_FILTER_KALEID0SC0PE_KALEID0SC0PE_H_ 1 |
| 27 | |
| 28 | #include "ikaleid0sc0pe.h" |
| 29 | |
| 30 | #include <vector> |
| 31 | #include <cmath> |
| 32 | #include <functional> |
| 33 | |
| 34 | #ifdef NO_SSE2 |
| 35 | #ifdef __SSE2__ |
| 36 | #undef __SSE2__ |
| 37 | #endif |
| 38 | #elif !defined(__SSE2__) |
| 39 | // set sse2 flag on windows |
| 40 | #if _M_IX86_FP == 2 || _M_X64 == 100 |
| 41 | #ifndef _M_ARM |
| 42 | #define __SSE2__ |
| 43 | #endif |
| 44 | #endif |
| 45 | #endif |
| 46 | |
| 47 | #ifdef __SSE2__ |
| 48 | #include <emmintrin.h> |
| 49 | #endif |
| 50 | |
| 51 | namespace libkaleid0sc0pe { |
| 52 | |
| 53 | /** |
| 54 | * Class which implements the kaleid0sc0pe effect |
| 55 | */ |
| 56 | class Kaleid0sc0pe: public IKaleid0sc0pe { |
| 57 | public: |
| 58 | /** |
| 59 | * Constructor |
| 60 | * @param width the frame width |
| 61 | * @param height the frame height |
| 62 | * @param component_size the byte size of each frame pixel component |
| 63 | * @param num_components the number of components per pixel |
| 64 | * @param stride the image stride, if \c 0 then calculated as \p width * \p component_size * \p num_components |
| 65 | */ |
| 66 | Kaleid0sc0pe(std::uint32_t width, std::uint32_t height, std::uint32_t component_size, std::uint32_t num_components, std::uint32_t stride = 0); |
| 67 | |
| 68 | virtual std::int32_t set_origin(float x, float y); |
| 69 | |
| 70 | virtual float get_origin_x() const; |
| 71 | |
| 72 | virtual float get_origin_y() const; |
| 73 | |
| 74 | virtual std::int32_t set_segmentation(std::uint32_t segmentation); |
| 75 | |
| 76 | virtual std::uint32_t get_segmentation() const; |
| 77 | |
| 78 | virtual std::int32_t set_edge_threshold(std::uint32_t threshold); |
| 79 | |
| 80 | virtual std::uint32_t get_edge_threshold() const; |
| 81 | |
| 82 | virtual std::int32_t set_segment_direction(Direction direction); |
| 83 | |
| 84 | virtual Direction get_segment_direction() const; |
| 85 | |
| 86 | virtual std::int32_t set_preferred_corner(Corner corner); |
| 87 | |
| 88 | virtual Corner get_preferred_corner() const; |
| 89 | |
| 90 | virtual std::int32_t set_preferred_corner_search_direction(Direction direction); |
| 91 | |
| 92 | virtual Direction get_preferred_corner_search_direction() const; |
| 93 | |
| 94 | virtual std::int32_t set_reflect_edges(bool reflect); |
| 95 | |
| 96 | virtual bool get_reflect_edges() const; |
| 97 | |
| 98 | virtual std::int32_t set_background_colour(void* colour); |
| 99 | |
| 100 | virtual void *get_background_colour() const; |
| 101 | |
| 102 | virtual std::int32_t set_source_segment(float angle); |
| 103 | |
| 104 | virtual float get_source_segment() const; |
| 105 | |
| 106 | virtual std::int32_t process(const void* in_frame, void* out_frame); |
| 107 | |
| 108 | virtual std::int32_t set_threading(std::uint32_t threading); |
| 109 | |
| 110 | virtual std::uint32_t get_threading() const; |
| 111 | |
| 112 | virtual std::int32_t visualise(void* out_frame); |
| 113 | |
| 114 | private: |
| 115 | void init(); |
| 116 | |
| 117 | #ifdef __SSE2__ |
| 118 | /// Defines reflection information for a given point in the frame |
| 119 | struct Reflect_info { |
| 120 | __m128 screen_x; ///< x coordinate in screen space (range -0.5->0.5, left negative) |
| 121 | __m128 screen_y; ///< y coordinate in screen space (range -0.5->0.5, top negative) |
| 122 | __m128 angle; ///< angle from this point to the start of the source segment |
| 123 | __m128 segment_number; ///< segment number the point resides in |
| 124 | __m128i segment_number_i; |
| 125 | __m128 reference_angle; ///< positive angle to start of source segment |
| 126 | }; |
| 127 | |
| 128 | Reflect_info calculate_reflect_info(__m128i *x, __m128i *y); |
| 129 | |
| 130 | /// Converts coordinates to screen space |
| 131 | /// @param x x coordinate |
| 132 | /// @param y y coordinate |
| 133 | /// @param sx source x coordinate |
| 134 | /// @param sy source y coordinate |
| 135 | void to_screen(__m128 *x, __m128 *y, __m128i *sx, __m128i *sy); |
| 136 | |
| 137 | /// Converts coordinates from screen space in place |
| 138 | /// @param x x coordinate |
| 139 | /// @param y y coordinate |
| 140 | void from_screen(__m128 *x, __m128 *y); |
| 141 | |
| 142 | /// Rotate the four coordinates from <tt>x,y</tt> to <tt>x+4,y</tt> and store results in |
| 143 | /// <tt>source_x,source_y</tt> |
| 144 | /// @param x x coordinate to start rotate from |
| 145 | /// @param y y coordinate to rotate |
| 146 | /// @param source_x receives the x coordinate results |
| 147 | /// @param source_y receives the y coordinate results |
| 148 | inline void rotate(int x, int y, __m128 *source_x, __m128 *source_y); |
| 149 | #else |
| 150 | /// Defines reflection information for a given point in the frame |
| 151 | struct Reflect_info { |
| 152 | float screen_x; ///< x coordinate in screen space (range -0.5->0.5, left negative) |
| 153 | float screen_y; ///< y coordinate in screen space (range -0.5->0.5, top negative) |
| 154 | float angle; ///< angle from this point to the start of the source segment |
| 155 | std::uint32_t segment_number; ///< segment number the point resides in |
| 156 | float reference_angle; ///< positive angle to start of source segment |
| 157 | }; |
| 158 | |
| 159 | /// Calculates the reflection information for a given point. |
| 160 | /// NB: init() must have already been called. |
| 161 | /// @param x the x coordinate |
| 162 | /// @param x the y coordinate |
| 163 | /// @return the reflection information for the point |
| 164 | Reflect_info calculate_reflect_info(std::uint32_t x, std::uint32_t y); |
| 165 | |
| 166 | /// Converts coordinates to screen space |
| 167 | /// @param x receives x coordinate |
| 168 | /// @param y receives y coordinate |
| 169 | /// @param sx source x coordinate |
| 170 | /// @param sy source y coordinate |
| 171 | void to_screen(float *x, float *y, std::uint32_t sx, std::uint32_t sy); |
| 172 | |
| 173 | /// Converts coordinates from screen space in place |
| 174 | /// @param x x coordinate |
| 175 | /// @param y y coordinate |
| 176 | void from_screen(float *x, float *y); |
| 177 | #endif |
| 178 | /// A block of data to process |
| 179 | struct Block { |
| 180 | const std::uint8_t* in_frame; |
| 181 | std::uint8_t* out_frame; |
| 182 | std::uint32_t x_start; |
| 183 | std::uint32_t y_start; |
| 184 | std::uint32_t x_end; |
| 185 | std::uint32_t y_end; |
| 186 | |
| 187 | /// \param in_frame the input frame |
| 188 | /// \param out_frame the output frame |
| 189 | /// \param x_start start x coordinate of block to process |
| 190 | /// \param y_start start y coordinate of block to process |
| 191 | /// \param x_end end x coordinate of block to process (inclusive) |
| 192 | /// \param y_end end y coordinate of block to process (inclusive) |
| 193 | Block(const std::uint8_t* _in_frame, std::uint8_t* _out_frame, std::uint32_t _x_start, std::uint32_t _y_start, std::uint32_t _x_end, std::uint32_t _y_end): |
| 194 | in_frame(_in_frame), |
| 195 | out_frame(_out_frame), |
| 196 | x_start(_x_start), |
| 197 | y_start(_y_start), |
| 198 | x_end(_x_end), |
| 199 | y_end(_y_end) |
| 200 | {} |
| 201 | }; |
| 202 | |
| 203 | /// Process a block |
| 204 | void process_block(Block *block); |
| 205 | |
| 206 | /// Copy pixel <tt>x,y</tt> from \p in to \p out using the background colour |
| 207 | /// if the pixel is out of range |
| 208 | /// @param x x coordinate to copy |
| 209 | /// @param y y coordinate to copy |
| 210 | /// @param in the first pixel in the source image |
| 211 | /// @param out destination |
| 212 | void process_bg(float x, float y, const std::uint8_t* in, std::uint8_t* out); |
| 213 | |
| 214 | |
| 215 | #ifdef __SSE2__ |
| 216 | // Process a block using background colour copy |
| 217 | void process_block_bg(Block* block); |
| 218 | #endif |
| 219 | |
| 220 | std::uint8_t *lookup(std::uint8_t *p, std::uint32_t x, std::uint32_t y); |
| 221 | |
| 222 | const std::uint8_t* lookup(const std::uint8_t* p, std::uint32_t x, std::uint32_t y); |
| 223 | |
| 224 | std::uint32_t m_width; |
| 225 | std::uint32_t m_height; |
| 226 | std::uint32_t m_component_size; |
| 227 | std::uint32_t m_num_components; |
| 228 | std::uint32_t m_stride; |
| 229 | std::uint32_t m_pixel_size; |
| 230 | |
| 231 | float m_aspect; |
| 232 | |
| 233 | float m_origin_x; |
| 234 | float m_origin_y; |
| 235 | float m_origin_native_x; |
| 236 | float m_origin_native_y; |
| 237 | |
| 238 | std::uint32_t m_segmentation; |
| 239 | Direction m_segment_direction; |
| 240 | |
| 241 | Corner m_preferred_corner; |
| 242 | Direction m_preferred_search_dir; |
| 243 | |
| 244 | bool m_edge_reflect; |
| 245 | |
| 246 | void* m_background_colour; |
| 247 | std::uint32_t m_edge_threshold; |
| 248 | |
| 249 | float m_source_segment_angle; |
| 250 | |
| 251 | std::uint32_t m_n_segments; |
| 252 | float m_start_angle; |
| 253 | float m_segment_width; |
| 254 | |
| 255 | std::uint32_t m_n_threads; |
| 256 | |
| 257 | #ifdef __SSE2__ |
| 258 | __m128 m_sse_aspect; |
| 259 | __m128 m_sse_origin_native_x; |
| 260 | __m128 m_sse_origin_native_y; |
| 261 | __m128 m_sse_start_angle; |
| 262 | __m128 m_sse_segment_width; |
| 263 | __m128 m_sse_half_segment_width; |
| 264 | __m128 m_sse_ps_0; |
| 265 | __m128 m_sse_ps_1; |
| 266 | __m128 m_sse_ps_2; |
| 267 | __m128i m_sse_epi32_1; |
| 268 | __m128i m_sse_epi32_2; |
| 269 | __m128i m_sse_shift_1; |
| 270 | __m128 m_sse_width; |
| 271 | __m128 m_sse_height; |
| 272 | __m128 m_sse_width_m1; |
| 273 | __m128 m_sse_height_m1; |
| 274 | #endif |
| 275 | }; |
| 276 | |
| 277 | } // namespace libkaleid0sc0pe |
| 278 | |
| 279 | #endif // SRC_FILTER_KALEID0SC0PE_KALEID0SC0PE_H_ |