| 1 | /* |
| 2 | * kaleid0sc0pe.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 | * The kaleidoscope class implementation. |
| 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 "kaleid0sc0pe.h" |
| 26 | #include "frei0r/math.h" |
| 27 | #include <memory> |
| 28 | #include <cstring> |
| 29 | #ifndef NO_FUTURE |
| 30 | #include <future> |
| 31 | #endif |
| 32 | |
| 33 | #ifdef __SSE2__ |
| 34 | #define USE_SSE2 |
| 35 | #include "sse_mathfun_extension.h" |
| 36 | #undef USE_SSE2 |
| 37 | #ifdef HAS_INTEL_INTRINSICS |
| 38 | #include <immintrin.h> |
| 39 | #endif |
| 40 | #ifdef HAS_SIN_INTRINSIC |
| 41 | #define _mm_call_sin_ps _mm_sin_ps |
| 42 | #else |
| 43 | #define _mm_call_sin_ps sin_ps |
| 44 | #endif |
| 45 | #ifdef HAS_COS_INTRINSIC |
| 46 | #define _mm_call_cos_ps _mm_cos_ps |
| 47 | #else |
| 48 | #define _mm_call_cos_ps cos_ps |
| 49 | #endif |
| 50 | #ifdef HAS_ATAN2_INTRINSIC |
| 51 | #define _mm_call_atan2_ps _mm_atan2_ps |
| 52 | #else |
| 53 | #define _mm_call_atan2_ps atan2_ps |
| 54 | #endif |
| 55 | #endif |
| 56 | |
| 57 | #ifndef M_PI |
| 58 | #define M_PI 3.14159265358979323846 |
| 59 | #endif |
| 60 | #ifndef M_2PI |
| 61 | #define M_2PI 6.28318530717958647693 |
| 62 | #endif |
| 63 | #ifndef MF_PI |
| 64 | #define MF_PI 3.14159265358979323846f |
| 65 | #endif |
| 66 | #ifndef MF_2PI |
| 67 | #define MF_2PI 6.28318530717958647693f |
| 68 | #endif |
| 69 | |
| 70 | namespace std |
| 71 | { |
| 72 | |
| 73 | void default_delete<libkaleid0sc0pe::IKaleid0sc0pe>::operator()(libkaleid0sc0pe::IKaleid0sc0pe *p) |
| 74 | { |
| 75 | delete p; |
| 76 | } |
| 77 | |
| 78 | } // namespace std |
| 79 | |
| 80 | namespace libkaleid0sc0pe { |
| 81 | |
| 82 | IKaleid0sc0pe *IKaleid0sc0pe::create(std::uint32_t width, std::uint32_t height, std::uint32_t component_size, std::uint32_t num_components, std::uint32_t stride) |
| 83 | { |
| 84 | return new Kaleid0sc0pe(width, height, component_size, num_components, stride); |
| 85 | } |
| 86 | |
| 87 | Kaleid0sc0pe::Kaleid0sc0pe(std::uint32_t width, std::uint32_t height, std::uint32_t component_size, std::uint32_t num_components, std::uint32_t stride): |
| 88 | m_width(width), |
| 89 | m_height(height), |
| 90 | m_component_size(component_size), |
| 91 | m_num_components(num_components), |
| 92 | m_stride(stride ? stride : width * component_size * num_components), |
| 93 | m_pixel_size(component_size * num_components), |
| 94 | m_aspect(width/static_cast<float>(height)), |
| 95 | m_origin_x(0.5f), |
| 96 | m_origin_y(0.5f), |
| 97 | m_origin_native_x(m_origin_x * width), |
| 98 | m_origin_native_y(m_origin_y * height), |
| 99 | m_segmentation(16), |
| 100 | m_segment_direction(Direction::NONE), |
| 101 | m_preferred_corner(Corner::BR), |
| 102 | m_preferred_search_dir(Direction::CLOCKWISE), |
| 103 | m_edge_reflect(true), |
| 104 | m_background_colour(nullptr), |
| 105 | m_edge_threshold(0), |
| 106 | m_source_segment_angle(-1), |
| 107 | m_n_segments(0), |
| 108 | m_start_angle(0), |
| 109 | m_segment_width(0), |
| 110 | m_n_threads(0) |
| 111 | { |
| 112 | #ifdef __SSE2__ |
| 113 | m_sse_width = _mm_set1_ps(static_cast<float>(m_width)); |
| 114 | m_sse_height = _mm_set1_ps(static_cast<float>(m_height)); |
| 115 | m_sse_aspect = _mm_set1_ps(m_width / static_cast<float>(m_height)); |
| 116 | m_sse_ps_0 = _mm_set1_ps(0.0f); |
| 117 | m_sse_ps_1 = _mm_set1_ps(1.0f); |
| 118 | m_sse_ps_1 = _mm_set1_ps(1.0f); |
| 119 | m_sse_ps_2 = _mm_set1_ps(2.0f); |
| 120 | m_sse_epi32_1 = _mm_set1_epi32(1); |
| 121 | m_sse_epi32_2 = _mm_set1_epi32(2); |
| 122 | m_sse_shift_1 = _mm_cvtsi32_si128(1); |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | std::int32_t Kaleid0sc0pe::set_origin(float x, float y) |
| 127 | { |
| 128 | if (x < 0 || y < 0 || x > 1 || y > 1) { |
| 129 | return -2; |
| 130 | } |
| 131 | m_origin_x = x; |
| 132 | m_origin_y = y; |
| 133 | |
| 134 | m_origin_native_x = m_origin_x * m_width; |
| 135 | m_origin_native_y = m_origin_y * m_height; |
| 136 | |
| 137 | m_n_segments = 0; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | float Kaleid0sc0pe::get_origin_x() const |
| 143 | { |
| 144 | return m_origin_x; |
| 145 | } |
| 146 | |
| 147 | float Kaleid0sc0pe::get_origin_y() const |
| 148 | { |
| 149 | return m_origin_y; |
| 150 | } |
| 151 | |
| 152 | std::int32_t Kaleid0sc0pe::set_segmentation(std::uint32_t segmentation) |
| 153 | { |
| 154 | if (segmentation == 0) { |
| 155 | return -2; |
| 156 | } |
| 157 | m_segmentation = segmentation; |
| 158 | m_n_segments = 0; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | std::uint32_t Kaleid0sc0pe::get_segmentation() const |
| 164 | { |
| 165 | return m_segmentation; |
| 166 | } |
| 167 | |
| 168 | std::int32_t Kaleid0sc0pe::set_edge_threshold(std::uint32_t threshold) |
| 169 | { |
| 170 | m_edge_threshold = threshold; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | std::uint32_t Kaleid0sc0pe::get_edge_threshold() const |
| 175 | { |
| 176 | return m_edge_threshold; |
| 177 | } |
| 178 | |
| 179 | std::int32_t Kaleid0sc0pe::set_preferred_corner(Corner corner) |
| 180 | { |
| 181 | m_preferred_corner = corner; |
| 182 | m_n_segments = 0; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | Kaleid0sc0pe::Corner Kaleid0sc0pe::get_preferred_corner() const |
| 187 | { |
| 188 | return m_preferred_corner; |
| 189 | } |
| 190 | |
| 191 | std::int32_t Kaleid0sc0pe::set_preferred_corner_search_direction(Direction direction) |
| 192 | { |
| 193 | if (direction == Direction::NONE) { |
| 194 | return -2; |
| 195 | } |
| 196 | m_preferred_search_dir = direction; |
| 197 | m_n_segments = 0; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | Kaleid0sc0pe::Direction Kaleid0sc0pe::get_preferred_corner_search_direction() const |
| 202 | { |
| 203 | return m_preferred_search_dir; |
| 204 | } |
| 205 | |
| 206 | std::int32_t Kaleid0sc0pe::set_reflect_edges(bool reflect) |
| 207 | { |
| 208 | m_edge_reflect = reflect; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | bool Kaleid0sc0pe::get_reflect_edges() const |
| 213 | { |
| 214 | return m_edge_reflect; |
| 215 | } |
| 216 | |
| 217 | std::int32_t Kaleid0sc0pe::set_background_colour(void* colour) |
| 218 | { |
| 219 | m_background_colour = colour; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | void* Kaleid0sc0pe::get_background_colour() const |
| 224 | { |
| 225 | return m_background_colour; |
| 226 | } |
| 227 | |
| 228 | std::int32_t Kaleid0sc0pe::set_source_segment(float angle) |
| 229 | { |
| 230 | m_source_segment_angle = angle; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | float Kaleid0sc0pe::get_source_segment() const |
| 235 | { |
| 236 | return m_source_segment_angle; |
| 237 | } |
| 238 | |
| 239 | static double distance_sq(double x1, double y1, double x2, double y2) |
| 240 | { |
| 241 | return std::pow(x1 - x2, 2) + std::pow(y1 - y2, 2); |
| 242 | } |
| 243 | |
| 244 | std::int32_t inc_idx(std::int32_t start_idx, std::int32_t inc, std::int32_t max) |
| 245 | { |
| 246 | start_idx += inc; |
| 247 | return (start_idx < 0) ? max - 1 : start_idx % max; |
| 248 | } |
| 249 | |
| 250 | void Kaleid0sc0pe::init() |
| 251 | { |
| 252 | m_n_segments = m_segmentation * 2; |
| 253 | m_segment_width = MF_PI * 2 / m_n_segments; |
| 254 | |
| 255 | if (m_source_segment_angle < 0) { |
| 256 | // find origin rotation |
| 257 | std::uint32_t corners[4][2] = { |
| 258 | { 0, 0 }, |
| 259 | { 1, 0 }, |
| 260 | { 1, 1 }, |
| 261 | { 0, 1 } |
| 262 | }; |
| 263 | std::int32_t start_idx(0); |
| 264 | switch (m_preferred_corner) { |
| 265 | case Corner::TL: start_idx = 0; break; |
| 266 | case Corner::TR: start_idx = 1; break; |
| 267 | case Corner::BR: start_idx = 2; break; |
| 268 | case Corner::BL: start_idx = 3; break; |
| 269 | } |
| 270 | std::int32_t dir = m_preferred_search_dir == Direction::CLOCKWISE ? 1 : -1; |
| 271 | std::uint32_t idx = start_idx; |
| 272 | float origin_x = m_origin_x; |
| 273 | float origin_y = m_origin_y; |
| 274 | double dist = distance_sq(origin_x, origin_y, corners[idx][0], corners[idx][1]); |
| 275 | std::int32_t corner = idx; |
| 276 | idx = inc_idx(idx, dir, 4); |
| 277 | while (idx != start_idx) { |
| 278 | double d = distance_sq(origin_x, origin_y, corners[idx][0], corners[idx][1]); |
| 279 | if (d > dist) { |
| 280 | dist = d; |
| 281 | corner = idx; |
| 282 | } |
| 283 | idx = inc_idx(idx, dir, 4); |
| 284 | } |
| 285 | |
| 286 | float start_line_x = corners[corner][0] - origin_x; |
| 287 | float start_line_y = corners[corner][1] - origin_y; |
| 288 | m_start_angle = std::atan2(start_line_y, start_line_x) - (m_segment_direction == Direction::NONE ? |
| 289 | 0 : |
| 290 | (m_segment_width / (m_segment_direction == Direction::CLOCKWISE ? -2 : 2))); |
| 291 | } else { |
| 292 | m_start_angle = -m_source_segment_angle; |
| 293 | } |
| 294 | #ifdef __SSE2__ |
| 295 | m_sse_origin_native_x = _mm_set1_ps(m_origin_x * m_width); |
| 296 | m_sse_origin_native_y = _mm_set1_ps(m_origin_y * m_height); |
| 297 | m_sse_start_angle = _mm_set1_ps(m_start_angle); |
| 298 | m_sse_segment_width = _mm_set1_ps(m_segment_width); |
| 299 | m_sse_half_segment_width = _mm_set1_ps(m_segment_width/2); |
| 300 | #endif |
| 301 | } |
| 302 | |
| 303 | #ifdef __SSE2__ |
| 304 | Kaleid0sc0pe::Reflect_info Kaleid0sc0pe::calculate_reflect_info(__m128i* x, __m128i* y) |
| 305 | { |
| 306 | Reflect_info info; |
| 307 | |
| 308 | to_screen(&info.screen_x, &info.screen_y, x, y); |
| 309 | |
| 310 | info.angle = _mm_sub_ps(_mm_call_atan2_ps(info.screen_y, info.screen_x), m_sse_start_angle); |
| 311 | info.reference_angle = _mm_add_ps(_mm_and_ps(info.angle, *(v4sf*)_ps_inv_sign_mask), m_sse_half_segment_width); |
| 312 | // we do a max with 0 since atan2_ps will return nan for atan2(0,0) which ends up with a negative reference angle. |
| 313 | info.segment_number_i = _mm_cvttps_epi32(_mm_max_ps(_mm_div_ps(info.reference_angle, m_sse_segment_width), m_sse_ps_0)); |
| 314 | info.segment_number = _mm_cvtepi32_ps(info.segment_number_i); |
| 315 | |
| 316 | return info; |
| 317 | } |
| 318 | |
| 319 | void Kaleid0sc0pe::to_screen(__m128* x, __m128* y, __m128i* sx, __m128i* sy) |
| 320 | { |
| 321 | *x = _mm_cvtepi32_ps(*sx); |
| 322 | *x = _mm_sub_ps(*x, m_sse_origin_native_x); |
| 323 | *y = _mm_cvtepi32_ps(*sy); |
| 324 | *y = _mm_sub_ps(*y, m_sse_origin_native_y); |
| 325 | *y = _mm_mul_ps(*y, m_sse_aspect); |
| 326 | } |
| 327 | |
| 328 | void Kaleid0sc0pe::from_screen(__m128* x, __m128* y) |
| 329 | { |
| 330 | *x = _mm_add_ps(*x, m_sse_origin_native_x); |
| 331 | *y = _mm_div_ps(*y, m_sse_aspect); |
| 332 | *y = _mm_add_ps(*y, m_sse_origin_native_y); |
| 333 | } |
| 334 | |
| 335 | void Kaleid0sc0pe::rotate(int x, int y, __m128 *source_x, __m128 *source_y) |
| 336 | { |
| 337 | ALIGN16_BEG int ALIGN16_END mx[4] = { x, x + 1, x + 2, x + 3 }; |
| 338 | ALIGN16_BEG int ALIGN16_END my[4] = { y, y, y, y }; |
| 339 | |
| 340 | Reflect_info info = calculate_reflect_info((__m128i*)mx, (__m128i*)my); |
| 341 | |
| 342 | __m128 reflection_angle = _mm_mul_ps(info.segment_number, m_sse_segment_width); |
| 343 | |
| 344 | __m128i segi_p1 = _mm_add_epi32(info.segment_number_i, m_sse_epi32_1); |
| 345 | __m128 refl_factor = _mm_cvtepi32_ps(_mm_sub_epi32(_mm_srl_epi32(segi_p1, m_sse_shift_1), _mm_srl_epi32(info.segment_number_i, m_sse_shift_1))); |
| 346 | |
| 347 | reflection_angle = _mm_sub_ps(reflection_angle, _mm_mul_ps(refl_factor, _mm_sub_ps(m_sse_segment_width, _mm_mul_ps(m_sse_ps_2, _mm_sub_ps(info.reference_angle, reflection_angle))))); |
| 348 | |
| 349 | reflection_angle = _mm_mul_ps(reflection_angle, _mm_sub_ps(m_sse_ps_0, _mm_or_ps(_mm_and_ps(info.angle, *(v4sf*)_ps_sign_mask), m_sse_ps_1))); |
| 350 | |
| 351 | reflection_angle = _mm_mul_ps(reflection_angle, _mm_and_ps(_mm_cmpge_ps(info.segment_number, m_sse_ps_1), m_sse_ps_1)); |
| 352 | |
| 353 | __m128 cos_angle = _mm_call_cos_ps(reflection_angle); |
| 354 | __m128 sin_angle = _mm_call_sin_ps(reflection_angle); |
| 355 | *source_x = _mm_sub_ps(_mm_mul_ps(info.screen_x, cos_angle), _mm_mul_ps(info.screen_y, sin_angle)); |
| 356 | *source_y = _mm_add_ps(_mm_mul_ps(info.screen_y, cos_angle), _mm_mul_ps(info.screen_x, sin_angle)); |
| 357 | |
| 358 | from_screen(source_x, source_y); |
| 359 | } |
| 360 | |
| 361 | #else |
| 362 | Kaleid0sc0pe::Reflect_info Kaleid0sc0pe::calculate_reflect_info(std::uint32_t x, std::uint32_t y) |
| 363 | { |
| 364 | Reflect_info info; |
| 365 | |
| 366 | to_screen(&(info.screen_x), &(info.screen_y), x, y); |
| 367 | |
| 368 | info.angle = std::atan2(info.screen_y, info.screen_x) - m_start_angle; |
| 369 | info.reference_angle = std::fabs(info.angle) + m_segment_width / 2; |
| 370 | info.segment_number = std::uint32_t(info.reference_angle / m_segment_width); |
| 371 | |
| 372 | return info; |
| 373 | } |
| 374 | void Kaleid0sc0pe::to_screen(float *x, float *y, std::uint32_t sx, std::uint32_t sy) |
| 375 | { |
| 376 | *x = sx - m_origin_native_x; |
| 377 | *y = (sy - m_origin_native_y) * m_aspect; |
| 378 | } |
| 379 | |
| 380 | void Kaleid0sc0pe::from_screen(float *x, float *y) |
| 381 | { |
| 382 | *x = *x + m_origin_native_x; |
| 383 | *y = *y / m_aspect + m_origin_native_y; |
| 384 | } |
| 385 | #endif |
| 386 | |
| 387 | const std::uint8_t *Kaleid0sc0pe::lookup(const std::uint8_t* p, std::uint32_t x, std::uint32_t y) |
| 388 | { |
| 389 | return p + m_stride * static_cast<std::size_t>(y) + m_pixel_size * static_cast<std::size_t>(x); |
| 390 | } |
| 391 | |
| 392 | std::uint8_t* Kaleid0sc0pe::lookup(std::uint8_t* p, std::uint32_t x, std::uint32_t y) |
| 393 | { |
| 394 | return p + m_stride * static_cast<std::size_t>(y) + m_pixel_size * static_cast<std::size_t>(x); |
| 395 | } |
| 396 | |
| 397 | void Kaleid0sc0pe::process_bg(float x, float y, const std::uint8_t* in, std::uint8_t* out) |
| 398 | { |
| 399 | if (x < 0 && -x <= m_edge_threshold) { |
| 400 | x = 0; |
| 401 | } |
| 402 | else if (x >= m_width && x < m_width + m_edge_threshold) { |
| 403 | x = m_width - 1.0f; |
| 404 | } |
| 405 | if (y < 0 && -y <= m_edge_threshold) { |
| 406 | y = 0; |
| 407 | } |
| 408 | else if (y >= m_height && y < m_height + m_edge_threshold) { |
| 409 | y = m_height - 1.0f; |
| 410 | } |
| 411 | if (static_cast<std::uint32_t>(x) >= 0 && static_cast<std::uint32_t>(x) < m_width && |
| 412 | static_cast<std::uint32_t>(y) >= 0 && static_cast<std::uint32_t>(y) < m_height) { |
| 413 | std::memcpy(out, lookup(in, static_cast<std::uint32_t>(x), static_cast<std::uint32_t>(y)), m_pixel_size); |
| 414 | } |
| 415 | else if (m_background_colour) { |
| 416 | std::memcpy(out, reinterpret_cast<const std::uint8_t*>(m_background_colour), m_pixel_size); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | #ifdef __SSE2__ |
| 421 | void Kaleid0sc0pe::process_block(Block* block) |
| 422 | { |
| 423 | for (std::int32_t y = block->y_start; y <= static_cast<std::int32_t>(block->y_end); ++y) { |
| 424 | for (std::int32_t x = block->x_start; x <= static_cast<std::int32_t>(block->x_end); x += 4) { |
| 425 | std::uint8_t* out = lookup(block->out_frame, x, y); |
| 426 | __m128 source_x; |
| 427 | __m128 source_y; |
| 428 | |
| 429 | // rotate points to source_x,source_y |
| 430 | rotate(x, y, &source_x, &source_y); |
| 431 | |
| 432 | // reflect back into image if necessary |
| 433 | |
| 434 | source_x = _mm_and_ps(source_x, *(v4sf*)_ps_inv_sign_mask); |
| 435 | __m128 ge_width = _mm_cmpge_ps(source_x, m_sse_width); |
| 436 | source_x = _mm_or_ps(_mm_and_ps(_mm_sub_ps(m_sse_width, _mm_sub_ps(source_x, m_sse_width)), ge_width), _mm_andnot_ps(ge_width, source_x)); |
| 437 | |
| 438 | // same for y |
| 439 | source_y = _mm_and_ps(source_y, *(v4sf*)_ps_inv_sign_mask); |
| 440 | __m128 ge_height = _mm_cmpge_ps(source_y, m_sse_height); |
| 441 | source_y = _mm_or_ps(_mm_and_ps(_mm_sub_ps(m_sse_height, _mm_sub_ps(source_y, m_sse_height)), ge_height), _mm_andnot_ps(ge_height,source_y)); |
| 442 | |
| 443 | __m128i source_xi = _mm_cvttps_epi32(_mm_min_ps(_mm_max_ps(source_x, _mm_setzero_ps()), _mm_sub_ps(m_sse_width, m_sse_ps_1))); |
| 444 | __m128i source_yi = _mm_cvttps_epi32(_mm_min_ps(_mm_max_ps(source_y, _mm_setzero_ps()), _mm_sub_ps(m_sse_height, m_sse_ps_1))); |
| 445 | |
| 446 | std::int32_t* sx = reinterpret_cast<std::int32_t*>(&source_xi); |
| 447 | std::int32_t* sy = reinterpret_cast<std::int32_t*>(&source_yi); |
| 448 | |
| 449 | for (int i = 0; i < 4; ++i) { |
| 450 | sx[i] = CLAMP(sx[i], 0, static_cast<std::int32_t>(m_width) - 1); |
| 451 | sy[i] = CLAMP(sy[i], 0, static_cast<std::int32_t>(m_height) - 1); |
| 452 | } |
| 453 | |
| 454 | std::memcpy(out, lookup(block->in_frame, sx[0], sy[0]), m_pixel_size); |
| 455 | out += m_pixel_size; |
| 456 | std::memcpy(out, lookup(block->in_frame, sx[1], sy[1]), m_pixel_size); |
| 457 | out += m_pixel_size; |
| 458 | std::memcpy(out, lookup(block->in_frame, sx[2], sy[2]), m_pixel_size); |
| 459 | out += m_pixel_size; |
| 460 | std::memcpy(out, lookup(block->in_frame, sx[3], sy[3]), m_pixel_size); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | void Kaleid0sc0pe::process_block_bg(Block* block) |
| 466 | { |
| 467 | for (std::int32_t y = block->y_start; y <= static_cast<std::int32_t>(block->y_end); ++y) { |
| 468 | for (std::int32_t x = block->x_start; x <= static_cast<std::int32_t>(block->x_end); x += 4) { |
| 469 | std::uint8_t* out = lookup(block->out_frame, x, y); |
| 470 | __m128 source_x; |
| 471 | __m128 source_y; |
| 472 | |
| 473 | // rotate points to source_x,source_y |
| 474 | rotate(x, y, &source_x, &source_y); |
| 475 | |
| 476 | float* sx = reinterpret_cast<float*>(&source_x); |
| 477 | float* sy = reinterpret_cast<float*>(&source_y); |
| 478 | process_bg(sx[0], sy[0], block->in_frame, out); |
| 479 | out += m_pixel_size; |
| 480 | process_bg(sx[1], sy[1], block->in_frame, out); |
| 481 | out += m_pixel_size; |
| 482 | process_bg(sx[2], sy[2], block->in_frame, out); |
| 483 | out += m_pixel_size; |
| 484 | process_bg(sx[3], sy[3], block->in_frame, out); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
| 490 | #else |
| 491 | void Kaleid0sc0pe::process_block(Block *block) |
| 492 | { |
| 493 | for (std::uint32_t y = block->y_start; y <= block->y_end; ++y) { |
| 494 | for (std::uint32_t x = block->x_start; x <= block->x_end; ++x) { |
| 495 | std::uint8_t* out = lookup(block->out_frame, x, y); |
| 496 | |
| 497 | Reflect_info info = calculate_reflect_info(x, y); |
| 498 | |
| 499 | if (info.segment_number) { |
| 500 | float reflection_angle = (info.segment_number * m_segment_width); |
| 501 | reflection_angle -= info.segment_number % 2 ? (m_segment_width - 2 * (info.reference_angle - reflection_angle)) : 0; |
| 502 | |
| 503 | reflection_angle *= std::signbit(info.angle) ? 1 : -1; |
| 504 | float cos_angle = std::cos(reflection_angle); |
| 505 | float sin_angle = std::sin(reflection_angle); |
| 506 | float source_x = info.screen_x * cos_angle - info.screen_y * sin_angle; |
| 507 | float source_y = info.screen_y * cos_angle + info.screen_x * sin_angle; |
| 508 | |
| 509 | from_screen(&source_x, &source_y); |
| 510 | |
| 511 | if (m_edge_reflect) { |
| 512 | if (source_x < 0) { |
| 513 | source_x = -source_x; |
| 514 | } else if (source_x > m_width - 10e-4f) { |
| 515 | source_x = m_width - (source_x - m_width + 10e-4f); |
| 516 | } if (source_y < 0) { |
| 517 | source_y = -source_y; |
| 518 | } else if (source_y > m_height - 10e-4f) { |
| 519 | source_y = m_height - (source_y - m_height + 10e-4f); |
| 520 | } |
| 521 | source_x = CLAMP(source_x, 0.0f, static_cast<float>(m_width - 1)); |
| 522 | source_y = CLAMP(source_y, 0.0f, static_cast<float>(m_height - 1)); |
| 523 | std::memcpy(out, lookup(block->in_frame, static_cast<std::uint32_t>(source_x), static_cast<std::uint32_t>(source_y)), m_pixel_size); |
| 524 | } else { |
| 525 | process_bg(source_x, source_y, block->in_frame, out); |
| 526 | } |
| 527 | } else { |
| 528 | std::memcpy(out, lookup(block->in_frame, x, y), m_pixel_size); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | #endif |
| 534 | |
| 535 | std::uint8_t colours[63][3] = { |
| 536 | { 0x00, 0xFF, 0x00 }, |
| 537 | { 0x00, 0x00, 0xFF }, |
| 538 | { 0xFF, 0x00, 0x00 }, |
| 539 | { 0x01, 0xFF, 0xFE }, |
| 540 | { 0xFF, 0xA6, 0xFE }, |
| 541 | { 0xFF, 0xDB, 0x66 }, |
| 542 | { 0x00, 0x64, 0x01 }, |
| 543 | { 0x01, 0x00, 0x67 }, |
| 544 | { 0x95, 0x00, 0x3A }, |
| 545 | { 0x00, 0x7D, 0xB5 }, |
| 546 | { 0xFF, 0x00, 0xF6 }, |
| 547 | { 0xFF, 0xEE, 0xE8 }, |
| 548 | { 0x77, 0x4D, 0x00 }, |
| 549 | { 0x90, 0xFB, 0x92 }, |
| 550 | { 0x00, 0x76, 0xFF }, |
| 551 | { 0xD5, 0xFF, 0x00 }, |
| 552 | { 0xFF, 0x93, 0x7E }, |
| 553 | { 0x6A, 0x82, 0x6C }, |
| 554 | { 0xFF, 0x02, 0x9D }, |
| 555 | { 0xFE, 0x89, 0x00 }, |
| 556 | { 0x7A, 0x47, 0x82 }, |
| 557 | { 0x7E, 0x2D, 0xD2 }, |
| 558 | { 0x85, 0xA9, 0x00 }, |
| 559 | { 0xFF, 0x00, 0x56 }, |
| 560 | { 0xA4, 0x24, 0x00 }, |
| 561 | { 0x00, 0xAE, 0x7E }, |
| 562 | { 0x68, 0x3D, 0x3B }, |
| 563 | { 0xBD, 0xC6, 0xFF }, |
| 564 | { 0x26, 0x34, 0x00 }, |
| 565 | { 0xBD, 0xD3, 0x93 }, |
| 566 | { 0x00, 0xB9, 0x17 }, |
| 567 | { 0x9E, 0x00, 0x8E }, |
| 568 | { 0x00, 0x15, 0x44 }, |
| 569 | { 0xC2, 0x8C, 0x9F }, |
| 570 | { 0xFF, 0x74, 0xA3 }, |
| 571 | { 0x01, 0xD0, 0xFF }, |
| 572 | { 0x00, 0x47, 0x54 }, |
| 573 | { 0xE5, 0x6F, 0xFE }, |
| 574 | { 0x78, 0x82, 0x31 }, |
| 575 | { 0x0E, 0x4C, 0xA1 }, |
| 576 | { 0x91, 0xD0, 0xCB }, |
| 577 | { 0xBE, 0x99, 0x70 }, |
| 578 | { 0x96, 0x8A, 0xE8 }, |
| 579 | { 0xBB, 0x88, 0x00 }, |
| 580 | { 0x43, 0x00, 0x2C }, |
| 581 | { 0xDE, 0xFF, 0x74 }, |
| 582 | { 0x00, 0xFF, 0xC6 }, |
| 583 | { 0xFF, 0xE5, 0x02 }, |
| 584 | { 0x62, 0x0E, 0x00 }, |
| 585 | { 0x00, 0x8F, 0x9C }, |
| 586 | { 0x98, 0xFF, 0x52 }, |
| 587 | { 0x75, 0x44, 0xB1 }, |
| 588 | { 0xB5, 0x00, 0xFF }, |
| 589 | { 0x00, 0xFF, 0x78 }, |
| 590 | { 0xFF, 0x6E, 0x41 }, |
| 591 | { 0x00, 0x5F, 0x39 }, |
| 592 | { 0x6B, 0x68, 0x82 }, |
| 593 | { 0x5F, 0xAD, 0x4E }, |
| 594 | { 0xA7, 0x57, 0x40 }, |
| 595 | { 0xA5, 0xFF, 0xD2 }, |
| 596 | { 0xFF, 0xB1, 0x67 }, |
| 597 | { 0x00, 0x9B, 0xFF }, |
| 598 | { 0xE8, 0x5E, 0xBE } |
| 599 | }; |
| 600 | |
| 601 | std::int32_t Kaleid0sc0pe::set_segment_direction(Direction direction) |
| 602 | { |
| 603 | m_segment_direction = direction; |
| 604 | m_n_segments = 0; |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | libkaleid0sc0pe::Kaleid0sc0pe::Direction Kaleid0sc0pe::get_segment_direction() const |
| 609 | { |
| 610 | return m_segment_direction; |
| 611 | } |
| 612 | |
| 613 | std::int32_t Kaleid0sc0pe::process(const void* in_frame, void* out_frame) |
| 614 | { |
| 615 | if (in_frame == nullptr || out_frame == nullptr) { |
| 616 | return -2; |
| 617 | } |
| 618 | #ifdef __SSE2__ |
| 619 | if (m_width % 4 != 0) { |
| 620 | return -2; |
| 621 | } |
| 622 | #endif |
| 623 | if (m_n_segments == 0) { |
| 624 | init(); |
| 625 | } |
| 626 | #ifdef NO_FUTURE |
| 627 | Block block(reinterpret_cast<const std::uint8_t*>(in_frame), |
| 628 | reinterpret_cast<std::uint8_t*>(out_frame), |
| 629 | 0, 0, |
| 630 | m_width - 1, m_height - 1); |
| 631 | #ifdef __SSE2__ |
| 632 | if (m_edge_reflect) { |
| 633 | process_block(&block); |
| 634 | } else { |
| 635 | process_block_bg(&block); |
| 636 | } |
| 637 | #else |
| 638 | process_block(&block); |
| 639 | #endif |
| 640 | #else |
| 641 | if (m_n_threads == 1) { |
| 642 | Block block(reinterpret_cast<const std::uint8_t*>(in_frame), |
| 643 | reinterpret_cast<std::uint8_t*>(out_frame), |
| 644 | 0, 0, |
| 645 | m_width - 1, m_height - 1); |
| 646 | #ifdef __SSE2__ |
| 647 | if (m_edge_reflect) { |
| 648 | process_block(&block); |
| 649 | } else { |
| 650 | process_block_bg(&block); |
| 651 | } |
| 652 | #else |
| 653 | process_block(&block); |
| 654 | #endif |
| 655 | } else { |
| 656 | std::uint32_t n_threads = m_n_threads == 0 ? std::thread::hardware_concurrency() : m_n_threads; |
| 657 | |
| 658 | std::vector<std::future<void>> futures; |
| 659 | std::vector<std::unique_ptr<Block>> blocks; |
| 660 | |
| 661 | std::uint32_t block_height = m_height / n_threads; |
| 662 | std::uint32_t y_start = 0; |
| 663 | std::uint32_t y_end = m_height - block_height * (n_threads - 1) - 1; |
| 664 | |
| 665 | for (std::uint32_t i = 0; i < n_threads; ++i) { |
| 666 | blocks.emplace_back(new Block( |
| 667 | reinterpret_cast<const std::uint8_t*>(in_frame), |
| 668 | reinterpret_cast<std::uint8_t*>(out_frame), |
| 669 | 0, y_start, |
| 670 | m_width - 1, y_end)); |
| 671 | |
| 672 | #ifdef __SSE2__ |
| 673 | futures.push_back(std::async(std::launch::async, m_edge_reflect ? &Kaleid0sc0pe::process_block : &Kaleid0sc0pe::process_block_bg, this, blocks[i].get())); |
| 674 | #else |
| 675 | futures.push_back(std::async(std::launch::async, &Kaleid0sc0pe::process_block, this, blocks[i].get())); |
| 676 | #endif |
| 677 | y_start = y_end + 1; |
| 678 | y_end += block_height; |
| 679 | } |
| 680 | for (auto& f : futures) { |
| 681 | f.wait(); |
| 682 | } |
| 683 | } |
| 684 | #endif |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | std::int32_t Kaleid0sc0pe::set_threading(std::uint32_t threading) |
| 690 | { |
| 691 | m_n_threads = threading; |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | std::uint32_t Kaleid0sc0pe::get_threading() const |
| 696 | { |
| 697 | return m_n_threads; |
| 698 | } |
| 699 | |
| 700 | std::int32_t Kaleid0sc0pe::visualise(void* out_frame) |
| 701 | { |
| 702 | if (out_frame == nullptr) { |
| 703 | return -2; |
| 704 | } |
| 705 | #ifdef __SSE2__ |
| 706 | if (m_width % 4 != 0) { |
| 707 | return -2; |
| 708 | } |
| 709 | #endif |
| 710 | if (m_n_segments == 0) { |
| 711 | init(); |
| 712 | } |
| 713 | |
| 714 | for (std::uint32_t y = 0; y < m_height; ++y) { |
| 715 | #ifdef __SSE2__ |
| 716 | for (std::uint32_t x = 0; x < m_width; x+=4) { |
| 717 | #else |
| 718 | for (std::uint32_t x = 0; x < m_width; ++x) { |
| 719 | #endif |
| 720 | std::uint8_t* out = lookup(reinterpret_cast<std::uint8_t*>(out_frame), x, y); |
| 721 | #ifdef __SSE2__ |
| 722 | ALIGN16_BEG int ALIGN16_END mx[4] = { static_cast<int>(x), static_cast<int>(x) + 1, static_cast<int>(x) + 2, static_cast<int>(x) + 3}; |
| 723 | ALIGN16_BEG int ALIGN16_END my[4] = { static_cast<int>(y), static_cast<int>(y), static_cast<int>(y), static_cast<int>(y) }; |
| 724 | |
| 725 | Reflect_info info = calculate_reflect_info((__m128i*)mx, (__m128i*)my); |
| 726 | //float* segment_number = reinterpret_cast<float*>(&info.segment_number); |
| 727 | std::int32_t* segment_number = reinterpret_cast<std::int32_t*>(&info.segment_number_i); |
| 728 | std::uint32_t col_idx = (*segment_number) % 63; |
| 729 | out[0] = colours[col_idx][0]; |
| 730 | out[1] = colours[col_idx][1]; |
| 731 | out[2] = colours[col_idx][2]; |
| 732 | if (m_num_components > 3) { |
| 733 | out[3] = 0xff; |
| 734 | out++; |
| 735 | } |
| 736 | segment_number++; |
| 737 | out += 3; |
| 738 | |
| 739 | col_idx = (*segment_number) % 63; |
| 740 | out[0] = colours[col_idx][0]; |
| 741 | out[1] = colours[col_idx][1]; |
| 742 | out[2] = colours[col_idx][2]; |
| 743 | if (m_num_components > 3) { |
| 744 | out[3] = 0xff; |
| 745 | out++; |
| 746 | } |
| 747 | segment_number++; |
| 748 | out += 3; |
| 749 | |
| 750 | col_idx = (*segment_number) % 63; |
| 751 | out[0] = colours[col_idx][0]; |
| 752 | out[1] = colours[col_idx][1]; |
| 753 | out[2] = colours[col_idx][2]; |
| 754 | if (m_num_components > 3) { |
| 755 | out[3] = 0xff; |
| 756 | out++; |
| 757 | } |
| 758 | segment_number++; |
| 759 | out += 3; |
| 760 | |
| 761 | col_idx = (*segment_number) % 63; |
| 762 | out[0] = colours[col_idx][0]; |
| 763 | out[1] = colours[col_idx][1]; |
| 764 | out[2] = colours[col_idx][2]; |
| 765 | if (m_num_components > 3) { |
| 766 | out[3] = 0xff; |
| 767 | out++; |
| 768 | } |
| 769 | |
| 770 | #else |
| 771 | Reflect_info info = calculate_reflect_info(x, y); |
| 772 | std::uint32_t col_idx = info.segment_number % 63; |
| 773 | out[0] = colours[col_idx][0]; |
| 774 | out[1] = colours[col_idx][1]; |
| 775 | out[2] = colours[col_idx][2]; |
| 776 | if (m_num_components > 3) { |
| 777 | out[3] = 0xff; |
| 778 | } |
| 779 | #endif |
| 780 | |
| 781 | } |
| 782 | } |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | } // namespace libkaleid0sc0pe |