| 1 | /* tint0r.c |
| 2 | * Copyright (C) 2009 Maksim Golovkin ([email protected]), |
| 3 | * 2025 Cynthia ([email protected]) |
| 4 | * This file is a Frei0r plugin. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (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, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <assert.h> |
| 24 | |
| 25 | /* Check for SSE4.1 support */ |
| 26 | #if defined(__SSE4_1__) |
| 27 | #include <smmintrin.h> |
| 28 | #define USE_SSE4_1 1 |
| 29 | #else |
| 30 | #define USE_SSE4_1 0 |
| 31 | #endif |
| 32 | |
| 33 | /* Check for other SIMD instruction sets */ |
| 34 | #if defined(__AVX__) && defined(__AVX2__) |
| 35 | #include <immintrin.h> |
| 36 | #define USE_AVX2 1 |
| 37 | #else |
| 38 | #define USE_AVX2 0 |
| 39 | #endif |
| 40 | |
| 41 | #if defined(__ARM_NEON__) || defined(__ARM_NEON) |
| 42 | #include <arm_neon.h> |
| 43 | #define USE_NEON 1 |
| 44 | #else |
| 45 | #define USE_NEON 0 |
| 46 | #endif |
| 47 | |
| 48 | #include <frei0r.h> |
| 49 | #include <frei0r/math.h> |
| 50 | |
| 51 | typedef struct tint0r_instance |
| 52 | { |
| 53 | unsigned int width; |
| 54 | unsigned int height; |
| 55 | f0r_param_color_t blackColor; |
| 56 | f0r_param_color_t whiteColor; |
| 57 | double amount; /* the amount value [0, 1] */ |
| 58 | } tint0r_instance_t; |
| 59 | |
| 60 | int f0r_init() |
| 61 | { |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | void f0r_deinit() |
| 66 | { /* no initialization required */ } |
| 67 | |
| 68 | void f0r_get_plugin_info(f0r_plugin_info_t* info) |
| 69 | { |
| 70 | info->name = "Tint0r"; |
| 71 | info->author = "Maksim Golovkin & Cynthia"; |
| 72 | info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 73 | info->color_model = F0R_COLOR_MODEL_BGRA8888; |
| 74 | info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 75 | info->major_version = 0; |
| 76 | info->minor_version = 1; |
| 77 | info->num_params = 3; |
| 78 | info->explanation = "Tint a source image with specified colors"; |
| 79 | } |
| 80 | |
| 81 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 82 | { |
| 83 | switch(param_index) |
| 84 | { |
| 85 | case 0: |
| 86 | info->name = "Map black to"; |
| 87 | info->type = F0R_PARAM_COLOR; |
| 88 | info->explanation = "The color to map source color with null luminance"; |
| 89 | break; |
| 90 | case 1: |
| 91 | info->name = "Map white to"; |
| 92 | info->type = F0R_PARAM_COLOR; |
| 93 | info->explanation = "The color to map source color with full luminance"; |
| 94 | break; |
| 95 | case 2: |
| 96 | info->name = "Tint amount"; |
| 97 | info->type = F0R_PARAM_DOUBLE; |
| 98 | info->explanation = "Amount of color"; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 104 | { |
| 105 | tint0r_instance_t* inst = (tint0r_instance_t*)calloc(1, sizeof(*inst)); |
| 106 | inst->width = width; inst->height = height; |
| 107 | inst->amount = 0.25; |
| 108 | inst->whiteColor.r = 0.5; |
| 109 | inst->whiteColor.g = 1.0; |
| 110 | inst->whiteColor.b = 0.5; |
| 111 | inst->blackColor.r = 0.0; |
| 112 | inst->blackColor.g = 0.0; |
| 113 | inst->blackColor.b = 0.0; |
| 114 | return (f0r_instance_t)inst; |
| 115 | } |
| 116 | |
| 117 | void f0r_destruct(f0r_instance_t instance) |
| 118 | { |
| 119 | free(instance); |
| 120 | } |
| 121 | |
| 122 | void f0r_set_param_value(f0r_instance_t instance, |
| 123 | f0r_param_t param, int param_index) |
| 124 | { |
| 125 | assert(instance); |
| 126 | tint0r_instance_t* inst = (tint0r_instance_t*)instance; |
| 127 | |
| 128 | switch(param_index) |
| 129 | { |
| 130 | case 0: |
| 131 | /* black color */ |
| 132 | inst->blackColor = *((f0r_param_color_t *)param); |
| 133 | break; |
| 134 | case 1: |
| 135 | /* white color */ |
| 136 | inst->whiteColor = *((f0r_param_color_t *)param); |
| 137 | break; |
| 138 | case 2: |
| 139 | /* amount */ |
| 140 | inst->amount = *((double *)param); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void f0r_get_param_value(f0r_instance_t instance, |
| 146 | f0r_param_t param, int param_index) |
| 147 | { |
| 148 | assert(instance); |
| 149 | tint0r_instance_t* inst = (tint0r_instance_t*)instance; |
| 150 | |
| 151 | switch(param_index) |
| 152 | { |
| 153 | case 0: |
| 154 | *((f0r_param_color_t*)param) = inst->blackColor; |
| 155 | break; |
| 156 | case 1: |
| 157 | *((f0r_param_color_t*)param) = inst->whiteColor; |
| 158 | break; |
| 159 | case 2: |
| 160 | *((double *)param) = inst->amount; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static inline unsigned char map_color(double amount, double comp_amount, float color, float luma, float minColor, float maxColor) |
| 166 | { |
| 167 | double val = (comp_amount * color) + amount * (luma * (maxColor - minColor) + minColor); |
| 168 | return (unsigned char)(255*CLAMP(val, 0, 1)); |
| 169 | } |
| 170 | |
| 171 | #if USE_SSE4_1 |
| 172 | static void tint_sse41(const uint32_t* inframe, uint32_t* outframe, size_t len, |
| 173 | double amount, f0r_param_color_t blackColor, f0r_param_color_t whiteColor) |
| 174 | { |
| 175 | const __m128 weights = _mm_set_ps(0.0, 0.114, 0.587, 0.299), |
| 176 | sse_amount = _mm_set1_ps(amount), |
| 177 | /* Pass the alpha channel */ |
| 178 | comp_amount = _mm_set_ps(1.0, |
| 179 | 1.0 - amount, |
| 180 | 1.0 - amount, |
| 181 | 1.0 - amount); |
| 182 | |
| 183 | /* Zero the alpha component to exclude it from calculations. */ |
| 184 | const __m128 cmin = _mm_set_ps(0.0, blackColor.b, blackColor.g, blackColor.r), |
| 185 | cdelta = _mm_sub_ps(_mm_set_ps(0.0, whiteColor.b, whiteColor.g, whiteColor.r), cmin), |
| 186 | tmp0 = _mm_mul_ps(cdelta, sse_amount), |
| 187 | tmp1 = _mm_mul_ps(_mm_mul_ps(sse_amount, _mm_set1_ps(255.0)), cmin); |
| 188 | |
| 189 | __m128i p; |
| 190 | __m128 p0, p1, p2, p3, luma; |
| 191 | |
| 192 | // Process pixels in groups of 4 |
| 193 | for (size_t i = 0; i < len; i++) |
| 194 | { |
| 195 | /* Load four pixels at once. */ |
| 196 | p = _mm_loadu_si128((__m128i*)(inframe + i * 4)); |
| 197 | |
| 198 | /* Extract four pixels into separate XMM registers and convert them to float. */ |
| 199 | p0 = _mm_cvtepi32_ps(_mm_cvtepu8_epi32(p)); |
| 200 | p1 = _mm_cvtepi32_ps(_mm_cvtepu8_epi32(_mm_srli_si128(p, 4))); |
| 201 | p2 = _mm_cvtepi32_ps(_mm_cvtepu8_epi32(_mm_srli_si128(p, 8))); |
| 202 | p3 = _mm_cvtepi32_ps(_mm_cvtepu8_epi32(_mm_srli_si128(p, 12))); |
| 203 | |
| 204 | #define tint(v) \ |
| 205 | luma = _mm_dp_ps((v), weights, 0x7F); \ |
| 206 | v = _mm_add_ps(_mm_mul_ps(comp_amount, (v)), \ |
| 207 | _mm_add_ps(_mm_mul_ps(luma, tmp0), tmp1)); |
| 208 | |
| 209 | tint(p0); tint(p1); tint(p2); tint(p3); |
| 210 | |
| 211 | /* Convert from float to int */ |
| 212 | __m128i pi0, pi1, pi2, pi3; |
| 213 | pi0 = _mm_cvtps_epi32(p0); |
| 214 | pi1 = _mm_cvtps_epi32(p1); |
| 215 | pi2 = _mm_cvtps_epi32(p2); |
| 216 | pi3 = _mm_cvtps_epi32(p3); |
| 217 | |
| 218 | /* Gather the processed pixels */ |
| 219 | p = _mm_packus_epi16(_mm_packus_epi32(pi0, pi1), |
| 220 | _mm_packus_epi32(pi2, pi3)); |
| 221 | |
| 222 | _mm_storeu_si128((__m128i*)(outframe + i * 4), p); |
| 223 | } |
| 224 | } |
| 225 | #elif USE_AVX2 |
| 226 | static void tint_avx2(const uint32_t* inframe, uint32_t* outframe, size_t len, |
| 227 | double amount, f0r_param_color_t blackColor, f0r_param_color_t whiteColor) |
| 228 | { |
| 229 | // AVX2 implementation would go here |
| 230 | // For now, fall back to scalar implementation |
| 231 | // This is a placeholder for a future AVX2 implementation |
| 232 | } |
| 233 | #elif USE_NEON |
| 234 | static void tint_neon(const uint32_t* inframe, uint32_t* outframe, size_t len, |
| 235 | double amount, f0r_param_color_t blackColor, f0r_param_color_t whiteColor) |
| 236 | { |
| 237 | // NEON implementation would go here |
| 238 | // For now, fall back to scalar implementation |
| 239 | // This is a placeholder for a future NEON implementation |
| 240 | } |
| 241 | #endif |
| 242 | |
| 243 | static void tint_scalar(const uint32_t* inframe, uint32_t* outframe, size_t len, |
| 244 | double amount, f0r_param_color_t blackColor, f0r_param_color_t whiteColor) |
| 245 | { |
| 246 | double comp_amount = 1.0 - amount; |
| 247 | |
| 248 | const unsigned char* src = (const unsigned char*)inframe; |
| 249 | unsigned char* dst = (unsigned char*)outframe; |
| 250 | |
| 251 | float b, g, r; |
| 252 | float luma; |
| 253 | |
| 254 | while (len--) |
| 255 | { |
| 256 | b = src[0] / 255.0f; |
| 257 | g = src[1] / 255.0f; |
| 258 | r = src[2] / 255.0f; |
| 259 | |
| 260 | luma = (b * 0.114f + g * 0.587f + r * 0.299f); |
| 261 | |
| 262 | dst[0] = map_color(amount, comp_amount, b, luma, blackColor.b, whiteColor.b); |
| 263 | dst[1] = map_color(amount, comp_amount, g, luma, blackColor.g, whiteColor.g); |
| 264 | dst[2] = map_color(amount, comp_amount, r, luma, blackColor.r, whiteColor.r); |
| 265 | dst[3] = src[3]; // Copy alpha |
| 266 | |
| 267 | src += 4; |
| 268 | dst += 4; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void f0r_update(f0r_instance_t instance, double time, |
| 273 | const uint32_t* inframe, uint32_t* outframe) |
| 274 | { |
| 275 | assert(instance); |
| 276 | tint0r_instance_t* inst = (tint0r_instance_t*)instance; |
| 277 | size_t len = inst->width * inst->height; |
| 278 | |
| 279 | #if USE_SSE4_1 |
| 280 | // Process in chunks of 4 pixels for SSE |
| 281 | size_t sse_len = len / 4; |
| 282 | size_t remainder = len % 4; |
| 283 | |
| 284 | if (sse_len > 0) { |
| 285 | tint_sse41(inframe, outframe, sse_len, inst->amount, inst->blackColor, inst->whiteColor); |
| 286 | } |
| 287 | |
| 288 | // Handle remaining pixels with scalar implementation |
| 289 | if (remainder > 0) { |
| 290 | const uint32_t* remaining_in = inframe + (sse_len * 4); |
| 291 | uint32_t* remaining_out = outframe + (sse_len * 4); |
| 292 | tint_scalar(remaining_in, remaining_out, remainder, inst->amount, inst->blackColor, inst->whiteColor); |
| 293 | } |
| 294 | #else |
| 295 | // Use scalar implementation for all pixels |
| 296 | tint_scalar(inframe, outframe, len, inst->amount, inst->blackColor, inst->whiteColor); |
| 297 | #endif |
| 298 | } |
| 299 |