Debian Codegraph @

src/filter/mirr0r/mirr0r.cpp

raw symbols SCIP
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_DEFINES
21 #include "frei0r.hpp"
22 #include <cairo.h>
23 #include <cmath>
24
25 class Mirr0r : public frei0r::filter {
26
27 private:
28
29 unsigned int width;
30 unsigned int height;
31 double x_offset;
32 double y_offset;
33 double zoom;
34 double rotation;
35
36 public:
37
38 Mirr0r(unsigned int width, unsigned int height){
39 register_param(this->x_offset, "x_offset", "Horizontal offset for image positioning.");
40 register_param(this->y_offset, "y_offset", "Vertical offset for image positioning.");
41 register_param(this->zoom, "zoom", "Zoom level for image scaling.");
42 register_param(this->rotation, "rotation", "Rotation angle in degrees.");
43
44 this->width = width;
45 this->height = height;
46 this->x_offset = 0.0;
47 this->y_offset = 0.0;
48 this->zoom = 0.5;
49 this->rotation = 0.0;
50 }
51
52 ~Mirr0r(){
53
54 }
55
56 void clearScreen(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 update(double time, uint32_t* out, const uint32_t* in) {
65
66 int w = this->width;
67 int h = this->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 clearScreen(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->zoom;
105
106 // Apply transformations
107 cairo_matrix_translate(&matrix, center_x - (this->x_offset) * w, center_y - (this->y_offset) * h);
108 cairo_matrix_scale(&matrix, scale_factor, scale_factor);
109 cairo_matrix_rotate(&matrix, this->rotation * M_PI);
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 frei0r::construct<Mirr0r> plugin(
131 "Mirr0r",
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.",
133 "Johann JEG",
134 1, 0,
135 F0R_COLOR_MODEL_RGBA8888);
136