Debian Codegraph @

src/filter/shake0scillate/shake0scillate.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 #include "frei0r.hpp"
21 #include <cairo.h>
22 #define _USE_MATH_DEFINES
23 #include <cmath>
24
25 class Shake0scillate : public frei0r::filter {
26
27 private:
28
29 double movement_amount_x;
30 double movement_speed_x;
31 double movement_amount_y;
32 double movement_speed_y;
33 double rotation_amount;
34 double rotation_speed;
35 double scale;
36 double phase;
37 bool mirrored;
38
39 const float MOVEMENT_AMOUNT_MULTIPLIER = 0.3f;
40 const float ROTATION_AMOUNT_MULTIPLIER = 0.2f;
41 const float SPEED_MULTIPLIER = 10.0f;
42
43 public:
44
45 Shake0scillate(unsigned int width, unsigned int height) {
46 register_param(this->movement_amount_x, "movement_amount_x", "Amount of the X movement.");
47 register_param(this->movement_speed_x, "movement_speed_x", "X Movement Speed.");
48 register_param(this->movement_amount_y, "movement_amount_y", "Amount of the Y movement.");
49 register_param(this->movement_speed_y, "movement_speed_y", "Y Movement Speed.");
50 register_param(this->rotation_amount, "rotation_amount", "Rotation amount.");
51 register_param(this->rotation_speed, "rotation_speed", "Rotation Speed.");
52 register_param(this->scale, "scale", "Scale level.");
53 register_param(this->phase, "phase", "The phase of the sin and cos functions of this effect.");
54 register_param(this->mirrored, "mirrored", "On/Off Image Mirror Extend");
55
56 this->movement_amount_x = 0.5; // Movement amount off is 0.5
57 this->movement_speed_x = 0.2;
58 this->movement_amount_y = 0.5; // Movement amount off is 0.5
59 this->movement_speed_y = 0.1;
60 this->rotation_amount = 0.5; // Rotation amount off is 0.5
61 this->rotation_speed = 0.2;
62 this->scale = 0.5; // Original image size is 0.5
63 this->phase = 0.0;
64 this->mirrored = true;
65 }
66
67 ~Shake0scillate() {
68
69 }
70
71 void clearScreen(cairo_t* cr) {
72 cairo_save (cr);
73 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
74 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
75 cairo_paint (cr);
76 cairo_restore (cr);
77 }
78
79 void oscillate(cairo_matrix_t* matrix, double time) {
80 float x = 0.0f;
81 float y = 0.0f;
82
83 // Operations for the translation amount do not depend on the screen size
84 float movement_range_x = MOVEMENT_AMOUNT_MULTIPLIER * this->height;
85 float movement_range_y = MOVEMENT_AMOUNT_MULTIPLIER * this->width;
86
87 // Translate X
88 if(this->movement_amount_x != 0.5){
89 float movement_x = (this->movement_amount_x - 0.5f) * 2.0f;
90 float movement_speed_value_x = this->movement_speed_x * SPEED_MULTIPLIER * time;
91 x = movement_x * movement_range_x * std::sin(movement_speed_value_x + this->phase);
92 }
93
94 // Translate Y
95 if(this->movement_amount_y != 0.5){
96 float movement_y = (this->movement_amount_y - 0.5f) * 2.0f;
97 float movement_speed_value_y = this->movement_speed_y * SPEED_MULTIPLIER * time;
98 y = movement_y * movement_range_y * std::cos(movement_speed_value_y + this->phase);
99 }
100
101 // Translate by adding the half screen coordinates to scale and rotate from center
102 float center_x = this->width / 2.0f;
103 float center_y = this->height / 2.0f;
104 cairo_matrix_translate(matrix, center_x + x, center_y + y);
105
106 // Scale
107 if(this->scale != 0.5){
108 float scale_factor = 1.5f - this->scale;
109 cairo_matrix_scale(matrix, scale_factor, scale_factor);
110 }
111
112 // Rotation
113 if(this->rotation_amount != 0.5){
114 float rotation_speed_value = (this->rotation_speed * SPEED_MULTIPLIER * time) + this->phase;
115 float rotation = ((this->rotation_amount - 0.5f) * ROTATION_AMOUNT_MULTIPLIER) * std::sin(rotation_speed_value) * M_PI;
116 cairo_matrix_rotate(matrix, rotation);
117 }
118
119 // Restore translate coordinates
120 cairo_matrix_translate(matrix, -center_x, -center_y);
121 }
122
123 virtual void update(double time, uint32_t* out, const uint32_t* in) {
124 int w = this->width;
125 int h = this->height;
126
127 // Calculate the stride for the image surface based on width and format
128 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, w);
129
130 // Create a Cairo surface for the destination image
131 cairo_surface_t* dest_image = cairo_image_surface_create_for_data((unsigned char*)out,
132 CAIRO_FORMAT_ARGB32,
133 w,
134 h,
135 stride);
136
137 // Create a Cairo drawing context for the destination surface
138 cairo_t *cr = cairo_create(dest_image);
139
140 // Clear the screen
141 clearScreen(cr);
142
143 // Create a Cairo surface for the source image
144 cairo_surface_t *image = cairo_image_surface_create_for_data((unsigned char*)in,
145 CAIRO_FORMAT_ARGB32,
146 w,
147 h,
148 stride);
149
150 // Create a pattern from the source image surface
151 cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image);
152
153 if(this->mirrored){
154 // Set the pattern extend mode to reflect (mirror) when the pattern is out of bounds
155 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REFLECT);
156 } else {
157 // Set the pattern extend mode to none (mirroring does not apply)
158 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
159 }
160
161 // Initialize the transformation matrix
162 cairo_matrix_t matrix;
163 cairo_matrix_init_identity(&matrix);
164
165 // Call the function that applies the movement
166 oscillate(&matrix, time);
167
168 // Set the transformation matrix for the pattern
169 cairo_pattern_set_matrix(pattern, &matrix);
170 // Set the source pattern to be used for drawing
171 cairo_set_source(cr, pattern);
172
173 // Draw a rectangle covering the entire image area
174 cairo_rectangle(cr, 0, 0, w, h);
175 // Fill the rectangle with the source pattern
176 cairo_fill(cr);
177
178 // Clean up resources
179 cairo_pattern_destroy (pattern);
180 cairo_surface_destroy (image);
181 cairo_surface_destroy (dest_image);
182 cairo_destroy (cr);
183 }
184 };
185
186 frei0r::construct<Shake0scillate> plugin(
187 "Shake0scillate",
188 "Animate the input image with adjustable parameters such as amount, speed, rotation, scale and option to mirror the image if it goes outside the screen bounds.",
189 "Johann JEG",
190 1, 0,
191 F0R_COLOR_MODEL_RGBA8888);