| 1 | /* This file is part of frei0r (https://frei0r.dyne.org) |
| 2 | * |
| 3 | * Copyright (C) 2024-2025 Dyne.org foundation |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as |
| 7 | * published by the Free Software Foundation, either version 3 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include "test-pattern.h" |
| 21 | #include <math.h> |
| 22 | #include <stdio.h> |
| 23 | |
| 24 | #ifndef M_PI |
| 25 | #define M_PI 3.14159265358979323846 |
| 26 | #endif |
| 27 | |
| 28 | static void set_pixel(uint32_t* frame, int width, int height, int x, int y, |
| 29 | uint8_t r, uint8_t g, uint8_t b, uint8_t a, int color_model) { |
| 30 | if (x >= 0 && x < width && y >= 0 && y < height) { |
| 31 | if (color_model == 0) { |
| 32 | frame[y * width + x] = (a << 24) | (r << 16) | (g << 8) | b; |
| 33 | } else { |
| 34 | frame[y * width + x] = (a << 24) | (b << 16) | (g << 8) | r; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static void draw_circle(uint32_t* frame, int width, int height, |
| 40 | int cx, int cy, int radius, |
| 41 | uint8_t r, uint8_t g, uint8_t b, uint8_t a, int color_model) { |
| 42 | int x_start = cx - radius > 0 ? cx - radius : 0; |
| 43 | int x_end = cx + radius < width ? cx + radius : width - 1; |
| 44 | int y_start = cy - radius > 0 ? cy - radius : 0; |
| 45 | int y_end = cy + radius < height ? cy + radius : height - 1; |
| 46 | |
| 47 | for (int y = y_start; y <= y_end; y++) { |
| 48 | for (int x = x_start; x <= x_end; x++) { |
| 49 | int dx = x - cx; |
| 50 | int dy = y - cy; |
| 51 | if (dx * dx + dy * dy <= radius * radius) { |
| 52 | set_pixel(frame, width, height, x, y, r, g, b, a, color_model); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void draw_filled_rect(uint32_t* frame, int width, int height, |
| 59 | int x1, int y1, int x2, int y2, |
| 60 | uint8_t r, uint8_t g, uint8_t b, uint8_t a, int color_model) { |
| 61 | if (x1 > x2) { int tmp = x1; x1 = x2; x2 = tmp; } |
| 62 | if (y1 > y2) { int tmp = y1; y1 = y2; y2 = tmp; } |
| 63 | |
| 64 | if (x1 < 0) x1 = 0; |
| 65 | if (y1 < 0) y1 = 0; |
| 66 | if (x2 >= width) x2 = width - 1; |
| 67 | if (y2 >= height) y2 = height - 1; |
| 68 | |
| 69 | for (int y = y1; y <= y2; y++) { |
| 70 | for (int x = x1; x <= x2; x++) { |
| 71 | set_pixel(frame, width, height, x, y, r, g, b, a, color_model); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void draw_rotated_square(uint32_t* frame, int width, int height, |
| 77 | int cx, int cy, int size, double angle, |
| 78 | uint8_t r, uint8_t g, uint8_t b, uint8_t a, int color_model) { |
| 79 | double cos_a = cos(angle); |
| 80 | double sin_a = sin(angle); |
| 81 | int half = size / 2; |
| 82 | |
| 83 | for (int dy = -half; dy <= half; dy++) { |
| 84 | for (int dx = -half; dx <= half; dx++) { |
| 85 | int rx = (int)(dx * cos_a - dy * sin_a); |
| 86 | int ry = (int)(dx * sin_a + dy * cos_a); |
| 87 | set_pixel(frame, width, height, cx + rx, cy + ry, r, g, b, a, color_model); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static void draw_digit(uint32_t* frame, int width, int height, |
| 93 | int x, int y, int digit, int scale, |
| 94 | uint8_t r, uint8_t g, uint8_t b, uint8_t a, int color_model) { |
| 95 | // Simple 5x7 bitmap font for digits 0-9 |
| 96 | static const uint8_t font[10][7] = { |
| 97 | {0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F}, // 0 |
| 98 | {0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E}, // 1 |
| 99 | {0x1F, 0x01, 0x01, 0x1F, 0x10, 0x10, 0x1F}, // 2 |
| 100 | {0x1F, 0x01, 0x01, 0x1F, 0x01, 0x01, 0x1F}, // 3 |
| 101 | {0x11, 0x11, 0x11, 0x1F, 0x01, 0x01, 0x01}, // 4 |
| 102 | {0x1F, 0x10, 0x10, 0x1F, 0x01, 0x01, 0x1F}, // 5 |
| 103 | {0x1F, 0x10, 0x10, 0x1F, 0x11, 0x11, 0x1F}, // 6 |
| 104 | {0x1F, 0x01, 0x01, 0x02, 0x04, 0x08, 0x10}, // 7 |
| 105 | {0x1F, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x1F}, // 8 |
| 106 | {0x1F, 0x11, 0x11, 0x1F, 0x01, 0x01, 0x1F} // 9 |
| 107 | }; |
| 108 | |
| 109 | if (digit < 0 || digit > 9) return; |
| 110 | |
| 111 | for (int row = 0; row < 7; row++) { |
| 112 | uint8_t line = font[digit][row]; |
| 113 | for (int col = 0; col < 5; col++) { |
| 114 | if (line & (1 << (4 - col))) { |
| 115 | for (int sy = 0; sy < scale; sy++) { |
| 116 | for (int sx = 0; sx < scale; sx++) { |
| 117 | set_pixel(frame, width, height, |
| 118 | x + col * scale + sx, |
| 119 | y + row * scale + sy, |
| 120 | r, g, b, a, color_model); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void draw_number(uint32_t* frame, int width, int height, |
| 129 | int x, int y, int number, int scale, |
| 130 | uint8_t r, uint8_t g, uint8_t b, uint8_t a, int color_model) { |
| 131 | if (number == 0) { |
| 132 | draw_digit(frame, width, height, x, y, 0, scale, r, g, b, a, color_model); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // Count digits |
| 137 | int temp = number; |
| 138 | int digits = 0; |
| 139 | while (temp > 0) { |
| 140 | digits++; |
| 141 | temp /= 10; |
| 142 | } |
| 143 | |
| 144 | // Draw each digit |
| 145 | temp = number; |
| 146 | for (int i = digits - 1; i >= 0; i--) { |
| 147 | int digit = temp % 10; |
| 148 | draw_digit(frame, width, height, x + i * 6 * scale, y, digit, scale, r, g, b, a, color_model); |
| 149 | temp /= 10; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void draw_zone_plate_patch(uint32_t* frame, int width, int height, |
| 154 | int cx, int cy, int size, int frame_num, int color_model) { |
| 155 | double phase = frame_num * 0.05; |
| 156 | int half = size / 2; |
| 157 | |
| 158 | for (int dy = -half; dy < half; dy++) { |
| 159 | for (int dx = -half; dx < half; dx++) { |
| 160 | double dist = sqrt(dx * dx + dy * dy); |
| 161 | double freq = dist * 0.3; |
| 162 | double value = (sin(freq + phase) + 1.0) * 0.5; |
| 163 | |
| 164 | uint8_t intensity = (uint8_t)(value * 255); |
| 165 | set_pixel(frame, width, height, cx + dx, cy + dy, |
| 166 | intensity, intensity, intensity, 255, color_model); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void generate_animated_test_pattern(uint32_t* frame, int width, int height, int frame_num, int color_model) { |
| 172 | // 1. Animated radial gradient background |
| 173 | int center_x = width / 2; |
| 174 | int center_y = height / 2; |
| 175 | double max_dist = sqrt(center_x * center_x + center_y * center_y); |
| 176 | |
| 177 | // Color cycling through the animation |
| 178 | double color_phase = frame_num * 0.02; |
| 179 | |
| 180 | for (int y = 0; y < height; y++) { |
| 181 | for (int x = 0; x < width; x++) { |
| 182 | int dx = x - center_x; |
| 183 | int dy = y - center_y; |
| 184 | double dist = sqrt(dx * dx + dy * dy); |
| 185 | double norm_dist = dist / max_dist; |
| 186 | |
| 187 | // Create a pulsing radial gradient with color shift |
| 188 | double angle = atan2(dy, dx); |
| 189 | double r_val = (sin(norm_dist * M_PI + color_phase) + 1.0) * 0.5; |
| 190 | double g_val = (sin(norm_dist * M_PI + color_phase + M_PI * 2.0 / 3.0) + 1.0) * 0.5; |
| 191 | double b_val = (sin(norm_dist * M_PI + color_phase + M_PI * 4.0 / 3.0) + 1.0) * 0.5; |
| 192 | |
| 193 | // Add some angular variation |
| 194 | double angular_mod = (sin(angle * 3.0 + color_phase * 0.5) + 1.0) * 0.2; |
| 195 | |
| 196 | uint8_t r = (uint8_t)((r_val + angular_mod) * 127 + 32); |
| 197 | uint8_t g = (uint8_t)((g_val + angular_mod) * 127 + 32); |
| 198 | uint8_t b = (uint8_t)((b_val + angular_mod) * 127 + 32); |
| 199 | |
| 200 | if (color_model == 0) { |
| 201 | frame[y * width + x] = (255 << 24) | (r << 16) | (g << 8) | b; |
| 202 | } else { |
| 203 | frame[y * width + x] = (255 << 24) | (b << 16) | (g << 8) | r; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // 2. Bouncing circle |
| 209 | int circle_radius = 40; |
| 210 | int circle_x = (int)(center_x + sin(frame_num * 0.05) * (center_x - circle_radius - 20)); |
| 211 | int circle_y = (int)(center_y + cos(frame_num * 0.037) * (center_y - circle_radius - 20)); |
| 212 | |
| 213 | draw_circle(frame, width, height, circle_x, circle_y, circle_radius, |
| 214 | 255, 200, 0, 255, color_model); |
| 215 | |
| 216 | // 3. Rotating square |
| 217 | int square_size = 60; |
| 218 | int square_x = width / 4; |
| 219 | int square_y = height / 4; |
| 220 | double rotation = frame_num * 0.03; |
| 221 | |
| 222 | draw_rotated_square(frame, width, height, square_x, square_y, square_size, rotation, |
| 223 | 0, 255, 200, 255, color_model); |
| 224 | |
| 225 | // 4. Horizontal moving bar |
| 226 | int bar_height = 20; |
| 227 | int bar_y = (int)(height * 0.75 + sin(frame_num * 0.04) * (height * 0.15)); |
| 228 | |
| 229 | draw_filled_rect(frame, width, height, 0, bar_y - bar_height/2, |
| 230 | width - 1, bar_y + bar_height/2, |
| 231 | 255, 100, 200, 255, color_model); |
| 232 | |
| 233 | // 5. Zone plate patch in corner |
| 234 | int zone_size = 80; |
| 235 | draw_zone_plate_patch(frame, width, height, |
| 236 | width - zone_size/2 - 10, |
| 237 | zone_size/2 + 10, |
| 238 | zone_size, frame_num, color_model); |
| 239 | |
| 240 | // 6. Frame counter |
| 241 | draw_number(frame, width, height, 10, 10, frame_num, 2, |
| 242 | 255, 255, 255, 255, color_model); |
| 243 | } |