| 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_1cxx . . $ `src/filter/tint0r/tint0r.c:30:9`! 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_AVX2cxx . . $ `src/filter/tint0r/tint0r.c:38:9`! 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_NEONcxx . . $ `src/filter/tint0r/tint0r.c:45:9`! 0 |
| 46 | #endif |
| 47 | |
| 48 | #include <frei0r.h>cxx . . $ `<file>/include/frei0r.h`/ |
| 49 | #include <frei0r/math.h>cxx . . $ `<file>/include/frei0r/math.h`/ |
| 50 | |
| 51 | typedef struct tint0r_instancecxx . . $ tint0r_instance# |
| 52 | { |
| 53 | unsigned int widthcxx . . $ tint0r_instance#width.; |
| 54 | unsigned int heightcxx . . $ tint0r_instance#height.; |
| 55 | f0r_param_color_tcxx . . $ f0r_param_color_t# blackColorcxx . . $ tint0r_instance#blackColor.; |
| 56 | f0r_param_color_tcxx . . $ f0r_param_color_t# whiteColorcxx . . $ tint0r_instance#whiteColor.; |
| 57 | double amountcxx . . $ tint0r_instance#amount.; /* the amount value [0, 1] */ |
| 58 | } tint0r_instance_tcxx . . $ tint0r_instance_t#; |
| 59 | |
| 60 | int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).() |
| 61 | { |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).() |
| 66 | { /* no initialization required */ } |
| 67 | |
| 68 | void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* info) |
| 69 | { |
| 70 | info->namecxx . . $ f0r_plugin_info#name. = "Tint0r"; |
| 71 | info->authorcxx . . $ f0r_plugin_info#author. = "Maksim Golovkin & Cynthia"; |
| 72 | info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!; |
| 73 | info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_BGRA8888cxx . . $ `include/frei0r.h:299:9`!; |
| 74 | info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!; |
| 75 | info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0; |
| 76 | info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1; |
| 77 | info->num_paramscxx . . $ f0r_plugin_info#num_params. = 3; |
| 78 | info->explanationcxx . . $ f0r_plugin_info#explanation. = "Tint a source image with specified colors"; |
| 79 | } |
| 80 | |
| 81 | void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index) |
| 82 | { |
| 83 | switch(param_index) |
| 84 | { |
| 85 | case 0: |
| 86 | info->namecxx . . $ f0r_param_info#name. = "Map black to"; |
| 87 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_COLORcxx . . $ `include/frei0r.h:389:9`!; |
| 88 | info->explanationcxx . . $ f0r_param_info#explanation. = "The color to map source color with null luminance"; |
| 89 | break; |
| 90 | case 1: |
| 91 | info->namecxx . . $ f0r_param_info#name. = "Map white to"; |
| 92 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_COLORcxx . . $ `include/frei0r.h:389:9`!; |
| 93 | info->explanationcxx . . $ f0r_param_info#explanation. = "The color to map source color with full luminance"; |
| 94 | break; |
| 95 | case 2: |
| 96 | info->namecxx . . $ f0r_param_info#name. = "Tint amount"; |
| 97 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 98 | info->explanationcxx . . $ f0r_param_info#explanation. = "Amount of color"; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height) |
| 104 | { |
| 105 | tint0r_instance_tcxx . . $ tint0r_instance_t#* inst = (tint0r_instance_tcxx . . $ tint0r_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst)); |
| 106 | inst->widthcxx . . $ tint0r_instance#width. = width; inst->heightcxx . . $ tint0r_instance#height. = height; |
| 107 | inst->amountcxx . . $ tint0r_instance#amount. = 0.25; |
| 108 | inst->whiteColorcxx . . $ tint0r_instance#whiteColor..rcxx . . $ f0r_param_color#r. = 0.5; |
| 109 | inst->whiteColorcxx . . $ tint0r_instance#whiteColor..gcxx . . $ f0r_param_color#g. = 1.0; |
| 110 | inst->whiteColorcxx . . $ tint0r_instance#whiteColor..bcxx . . $ f0r_param_color#b. = 0.5; |
| 111 | inst->blackColorcxx . . $ tint0r_instance#blackColor..rcxx . . $ f0r_param_color#r. = 0.0; |
| 112 | inst->blackColorcxx . . $ tint0r_instance#blackColor..gcxx . . $ f0r_param_color#g. = 0.0; |
| 113 | inst->blackColorcxx . . $ tint0r_instance#blackColor..bcxx . . $ f0r_param_color#b. = 0.0; |
| 114 | return (f0r_instance_tcxx . . $ f0r_instance_t#)inst; |
| 115 | } |
| 116 | |
| 117 | void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 118 | { |
| 119 | freecxx . . $ free(5d22bdacc48458e8).(instance); |
| 120 | } |
| 121 | |
| 122 | void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 123 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 124 | { |
| 125 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 126 | tint0r_instance_tcxx . . $ tint0r_instance_t#* inst = (tint0r_instance_tcxx . . $ tint0r_instance_t#*)instance; |
| 127 | |
| 128 | switch(param_index) |
| 129 | { |
| 130 | case 0: |
| 131 | /* black color */ |
| 132 | inst->blackColorcxx . . $ tint0r_instance#blackColor. = *((f0r_param_color_tcxx . . $ f0r_param_color_t# *)param); |
| 133 | break; |
| 134 | case 1: |
| 135 | /* white color */ |
| 136 | inst->whiteColorcxx . . $ tint0r_instance#whiteColor. = *((f0r_param_color_tcxx . . $ f0r_param_color_t# *)param); |
| 137 | break; |
| 138 | case 2: |
| 139 | /* amount */ |
| 140 | inst->amountcxx . . $ tint0r_instance#amount. = *((double *)param); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 146 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 147 | { |
| 148 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 149 | tint0r_instance_tcxx . . $ tint0r_instance_t#* inst = (tint0r_instance_tcxx . . $ tint0r_instance_t#*)instance; |
| 150 | |
| 151 | switch(param_index) |
| 152 | { |
| 153 | case 0: |
| 154 | *((f0r_param_color_tcxx . . $ f0r_param_color_t#*)param) = inst->blackColorcxx . . $ tint0r_instance#blackColor.; |
| 155 | break; |
| 156 | case 1: |
| 157 | *((f0r_param_color_tcxx . . $ f0r_param_color_t#*)param) = inst->whiteColorcxx . . $ tint0r_instance#whiteColor.; |
| 158 | break; |
| 159 | case 2: |
| 160 | *((double *)param) = inst->amountcxx . . $ tint0r_instance#amount.; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static inline unsigned char map_colorcxx . . $ map_color(19c7d675f1edb246).(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*CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(val, 0, 1)); |
| 169 | } |
| 170 | |
| 171 | #if USE_SSE4_1cxx . . $ `src/filter/tint0r/tint0r.c:30:9`! |
| 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_AVX2cxx . . $ `src/filter/tint0r/tint0r.c:38:9`! |
| 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_NEONcxx . . $ `src/filter/tint0r/tint0r.c:45:9`! |
| 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_scalarcxx . . $ tint_scalar(f0d8988d8308bd44).(const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe, size_t len, |
| 244 | double amount, f0r_param_color_tcxx . . $ f0r_param_color_t# blackColor, f0r_param_color_tcxx . . $ 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_colorcxx . . $ map_color(19c7d675f1edb246).(amount, comp_amount, b, luma, blackColor.bcxx . . $ f0r_param_color#b., whiteColor.bcxx . . $ f0r_param_color#b.); |
| 263 | dst[1] = map_colorcxx . . $ map_color(19c7d675f1edb246).(amount, comp_amount, g, luma, blackColor.gcxx . . $ f0r_param_color#g., whiteColor.gcxx . . $ f0r_param_color#g.); |
| 264 | dst[2] = map_colorcxx . . $ map_color(19c7d675f1edb246).(amount, comp_amount, r, luma, blackColor.rcxx . . $ f0r_param_color#r., whiteColor.rcxx . . $ f0r_param_color#r.); |
| 265 | dst[3] = src[3]; // Copy alpha |
| 266 | |
| 267 | src += 4; |
| 268 | dst += 4; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, |
| 273 | const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe) |
| 274 | { |
| 275 | assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance); |
| 276 | tint0r_instance_tcxx . . $ tint0r_instance_t#* inst = (tint0r_instance_tcxx . . $ tint0r_instance_t#*)instance; |
| 277 | size_t len = inst->widthcxx . . $ tint0r_instance#width. * inst->heightcxx . . $ tint0r_instance#height.; |
| 278 | |
| 279 | #if USE_SSE4_1cxx . . $ `src/filter/tint0r/tint0r.c:30:9`! |
| 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_scalarcxx . . $ tint_scalar(f0d8988d8308bd44).(inframe, outframe, len, inst->amountcxx . . $ tint0r_instance#amount., inst->blackColorcxx . . $ tint0r_instance#blackColor., inst->whiteColorcxx . . $ tint0r_instance#whiteColor.); |
| 297 | #endif |
| 298 | } |
| 299 |