| 1 | /* |
| 2 | * Copyright (C) 2024 Johann JEG ([email protected]) |
| 3 | * |
| 4 | * This file is a Frei0r plugin. |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation, either version 3 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #define _USE_MATH_DEFINEScxx . . $ `src/filter/mirr0r/mirr0r.cpp:20:9`! |
| 21 | #include "frei0r.hpp"cxx . . $ `<file>/include/frei0r.hpp`/ |
| 22 | #include <cairo.h> |
| 23 | #include <cmath> |
| 24 | |
| 25 | class Mirr0rcxx . . $ Mirr0r# : public frei0rcxx . . $ frei0r/::filtercxx . . $ frei0r/filter# { |
| 26 | |
| 27 | private: |
| 28 | |
| 29 | unsigned int widthcxx . . $ Mirr0r#width.; |
| 30 | unsigned int heightcxx . . $ Mirr0r#height.; |
| 31 | double x_offsetcxx . . $ Mirr0r#x_offset.; |
| 32 | double y_offsetcxx . . $ Mirr0r#y_offset.; |
| 33 | double zoomcxx . . $ Mirr0r#zoom.; |
| 34 | double rotationcxx . . $ Mirr0r#rotation.; |
| 35 | |
| 36 | public: |
| 37 | |
| 38 | Mirr0rcxx . . $ Mirr0r#Mirr0r(b4d55b1286ff73e8).(unsigned int width, unsigned int height){ |
| 39 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(this->x_offsetcxx . . $ Mirr0r#x_offset., "x_offset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Horizontal offset for image positioning."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 40 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(this->y_offsetcxx . . $ Mirr0r#y_offset., "y_offset"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Vertical offset for image positioning."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 41 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(this->zoomcxx . . $ Mirr0r#zoom., "zoom"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Zoom level for image scaling."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 42 | register_paramcxx . . $ frei0r/fx#register_param(acfd1d4411be0164).(this->rotationcxx . . $ Mirr0r#rotation., "rotation"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., "Rotation angle in degrees."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 43 | |
| 44 | this->widthcxx . . $ Mirr0r#width. = width; |
| 45 | this->heightcxx . . $ Mirr0r#height. = height; |
| 46 | this->x_offsetcxx . . $ Mirr0r#x_offset. = 0.0; |
| 47 | this->y_offsetcxx . . $ Mirr0r#y_offset. = 0.0; |
| 48 | this->zoomcxx . . $ Mirr0r#zoom. = 0.5; |
| 49 | this->rotationcxx . . $ Mirr0r#rotation. = 0.0; |
| 50 | } |
| 51 | |
| 52 | ~cxx . . $ Mirr0r#`~Mirr0r`(49f6e7a06ebc5aa8).Mirr0rcxx . . $ Mirr0r#(){ |
| 53 | |
| 54 | } |
| 55 | |
| 56 | void clearScreencxx . . $ Mirr0r#clearScreen(db2649241b158601).(cairo_t* cr, int width, int height) { |
| 57 | cairo_save (cr); |
| 58 | cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0); |
| 59 | cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); |
| 60 | cairo_paint (cr); |
| 61 | cairo_restore (cr); |
| 62 | } |
| 63 | |
| 64 | virtual void updatecxx . . $ Mirr0r#update(da7b4a17ceb4e697).(double time, uint32_t* out, const uint32_t* in) { |
| 65 | |
| 66 | int w = this->widthcxx . . $ Mirr0r#width.; |
| 67 | int h = this->heightcxx . . $ Mirr0r#height.; |
| 68 | |
| 69 | // Calculate the stride for the image surface based on width and format |
| 70 | int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w); |
| 71 | |
| 72 | // Create a Cairo surface for the destination image |
| 73 | cairo_surface_t* dest_image = cairo_image_surface_create_for_data((unsigned char*)out, |
| 74 | CAIRO_FORMAT_ARGB32, |
| 75 | w, |
| 76 | h, |
| 77 | stride); |
| 78 | |
| 79 | // Create a Cairo drawing context for the destination surface |
| 80 | cairo_t *cr = cairo_create(dest_image); |
| 81 | |
| 82 | // Clear the screen |
| 83 | clearScreencxx . . $ Mirr0r#clearScreen(db2649241b158601).(cr, w, h); |
| 84 | |
| 85 | // Create a Cairo surface for the source image |
| 86 | cairo_surface_t *image = cairo_image_surface_create_for_data((unsigned char*)in, |
| 87 | CAIRO_FORMAT_ARGB32, |
| 88 | w, |
| 89 | h, |
| 90 | stride); |
| 91 | // Create a pattern from the source image surface |
| 92 | cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); |
| 93 | // Set the pattern extend mode to reflect (mirror) when the pattern is out of bounds |
| 94 | cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REFLECT); |
| 95 | |
| 96 | // Initialize the transformation matrix |
| 97 | cairo_matrix_t matrix; |
| 98 | cairo_matrix_init_identity(&matrix); |
| 99 | |
| 100 | // Calculate the center coordinates of the destination image |
| 101 | float center_x = (float)w / 2.0f; |
| 102 | float center_y = (float)h / 2.0f; |
| 103 | |
| 104 | float scale_factor = 1.5f - this->zoomcxx . . $ Mirr0r#zoom.; |
| 105 | |
| 106 | // Apply transformations |
| 107 | cairo_matrix_translate(&matrix, center_x - (this->x_offsetcxx . . $ Mirr0r#x_offset.) * w, center_y - (this->y_offsetcxx . . $ Mirr0r#y_offset.) * h); |
| 108 | cairo_matrix_scale(&matrix, scale_factor, scale_factor); |
| 109 | cairo_matrix_rotate(&matrix, this->rotationcxx . . $ Mirr0r#rotation. * M_PIcxx . . $ `<external>/11263917310783833665/math.h:1289:10`!); |
| 110 | cairo_matrix_translate(&matrix, -center_x, -center_y); |
| 111 | |
| 112 | // Set the transformation matrix for the pattern |
| 113 | cairo_pattern_set_matrix(pattern, &matrix); |
| 114 | // Set the source pattern to be used for drawing |
| 115 | cairo_set_source(cr, pattern); |
| 116 | |
| 117 | // Draw a rectangle covering the entire image area |
| 118 | cairo_rectangle(cr, 0, 0, w, h); |
| 119 | // Fill the rectangle with the source pattern |
| 120 | cairo_fill(cr); |
| 121 | |
| 122 | // Clean up resources |
| 123 | cairo_pattern_destroy (pattern); |
| 124 | cairo_surface_destroy (image); |
| 125 | cairo_surface_destroy (dest_image); |
| 126 | cairo_destroy (cr); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | frei0rcxx . . $ frei0r/::constructcxx . . $ frei0r/construct#<Mirr0rcxx . . $ Mirr0r#> plugincxx . . $ plugin.( |
| 131 | "Mirr0r"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 132 | "Repeats and flips the input image when it goes out of bounds, allowing for adjustable offset, zoom and rotation. A versatile tool for creative video effects."cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 133 | "Johann JEG"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d)., |
| 134 | 1, 0, |
| 135 | F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!); |
| 136 |