| 1 | /* curves.c |
| 2 | * Copyright (C) 2009 Maksim Golovkin ([email protected]) |
| 3 | * Copyright (C) 2010 Till Theato ([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 | #ifndef _GNU_SOURCE |
| 22 | #define _GNU_SOURCEcxx . . $ `src/filter/curves/curves.c:22:9`! |
| 23 | #endif |
| 24 | #include <stdlib.h> |
| 25 | #include <assert.h> |
| 26 | #include <math.h> |
| 27 | #include <string.h> |
| 28 | #include <stdio.h> |
| 29 | |
| 30 | #include "frei0r.h"cxx . . $ `<file>/include/frei0r.h`/ |
| 31 | #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/ |
| 32 | |
| 33 | #define MAX3cxx . . $ `src/filter/curves/curves.c:33:9`!(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) ) |
| 34 | #define MIN3cxx . . $ `src/filter/curves/curves.c:34:9`!(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) ) |
| 35 | |
| 36 | #define POS_TOP_LEFTcxx . . $ `src/filter/curves/curves.c:36:9`! 0 |
| 37 | #define POS_TOP_RIGHTcxx . . $ `src/filter/curves/curves.c:37:9`! 1 |
| 38 | #define POS_BOTTOM_LEFTcxx . . $ `src/filter/curves/curves.c:38:9`! 2 |
| 39 | #define POS_BOTTOM_RIGHTcxx . . $ `src/filter/curves/curves.c:39:9`! 3 |
| 40 | |
| 41 | #define POINTcxx . . $ `src/filter/curves/curves.c:41:9`! "Point " |
| 42 | #define INPUT_VALUEcxx . . $ `src/filter/curves/curves.c:42:9`! " input value" |
| 43 | #define OUTPUT_VALUEcxx . . $ `src/filter/curves/curves.c:43:9`! " output value" |
| 44 | |
| 45 | enum CHANNELScxx . . $ CHANNELS# { CHANNEL_REDcxx . . $ CHANNEL_RED. = 0, CHANNEL_GREENcxx . . $ CHANNEL_GREEN., CHANNEL_BLUEcxx . . $ CHANNEL_BLUE., CHANNEL_ALPHAcxx . . $ CHANNEL_ALPHA., CHANNEL_LUMAcxx . . $ CHANNEL_LUMA., CHANNEL_RGBcxx . . $ CHANNEL_RGB., CHANNEL_HUEcxx . . $ CHANNEL_HUE., CHANNEL_SATURATIONcxx . . $ CHANNEL_SATURATION. }; |
| 46 | |
| 47 | typedef struct positioncxx . . $ position# |
| 48 | { |
| 49 | double xcxx . . $ position#x.; |
| 50 | double ycxx . . $ position#y.; |
| 51 | } positioncxx . . $ position#; |
| 52 | |
| 53 | typedef positioncxx . . $ position# bspline_pointcxx . . $ bspline_point#[3]; // [0] = handle1, [1] = point, [2] = handle2 |
| 54 | |
| 55 | |
| 56 | typedef struct curves_instancecxx . . $ curves_instance# |
| 57 | { |
| 58 | unsigned int widthcxx . . $ curves_instance#width.; |
| 59 | unsigned int heightcxx . . $ curves_instance#height.; |
| 60 | enum CHANNELScxx . . $ CHANNELS# channelcxx . . $ curves_instance#channel.; |
| 61 | double pointNumbercxx . . $ curves_instance#pointNumber.; |
| 62 | double pointscxx . . $ curves_instance#points.[10]; |
| 63 | double drawCurvescxx . . $ curves_instance#drawCurves.; |
| 64 | double curvesPositioncxx . . $ curves_instance#curvesPosition.; |
| 65 | double formulacxx . . $ curves_instance#formula.; |
| 66 | |
| 67 | char *bsplinecxx . . $ curves_instance#bspline.; |
| 68 | double *bsplineMapcxx . . $ curves_instance#bsplineMap.; |
| 69 | double *csplineMapcxx . . $ curves_instance#csplineMap.; |
| 70 | float *curveMapcxx . . $ curves_instance#curveMap.; |
| 71 | } curves_instance_tcxx . . $ curves_instance_t#; |
| 72 | |
| 73 | |
| 74 | // color conversion functions taken from: |
| 75 | // http://www.cs.rit.edu/~ncs/color/t_convert.html |
| 76 | // slightly modified |
| 77 | |
| 78 | // r,g,b values are from 0 to 255 |
| 79 | // h = [0,360], s = [0,1], v = [0,1] |
| 80 | // if s == 0, then h = -1 (undefined) |
| 81 | void RGBtoHSVcxx . . $ RGBtoHSV(4eeb89725be9c4c3).(double r, double g, double b, double *h, double *s, double *v) |
| 82 | { |
| 83 | double min = MIN3cxx . . $ `src/filter/curves/curves.c:34:9`!(r, g, b); |
| 84 | double max = MAX3cxx . . $ `src/filter/curves/curves.c:33:9`!(r, g, b); |
| 85 | *v = max / 255.; |
| 86 | |
| 87 | double delta = max - min; |
| 88 | |
| 89 | if (delta != 0) { |
| 90 | *s = delta / max; // s |
| 91 | } else { |
| 92 | // r = g = b // s = 0 |
| 93 | *s = 0; |
| 94 | *h = -1; |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (r == max) |
| 99 | *h = (g - b) / delta; // between yellow & magenta |
| 100 | else if (g == max) |
| 101 | *h = 2 + (b - r) / delta; // between cyan & yellow |
| 102 | else |
| 103 | *h = 4 + (r - g) / delta; // between magenta & cyan |
| 104 | |
| 105 | *h *= 60; // degrees |
| 106 | if (*h < 0) |
| 107 | *h += 360; |
| 108 | } |
| 109 | |
| 110 | // r,g,b values are from 0 to 1 |
| 111 | // h = [0,360], s = [0,1], v = [0,1] |
| 112 | void HSVtoRGBcxx . . $ HSVtoRGB(f38ff659fc243f5b).(double *r, double *g, double *b, double h, double s, double v) |
| 113 | { |
| 114 | if (s == 0) { |
| 115 | // achromatic (grey) |
| 116 | *r = *g = *b = v; |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | h /= 60; // sector 0 to 5 |
| 121 | int i = (int)h; |
| 122 | double f = h - i; // factorial part of h |
| 123 | double p = v * (1 - s); |
| 124 | |
| 125 | if (i & 1) { |
| 126 | double q = v * (1 - s * f); |
| 127 | switch (i) { |
| 128 | case 1: |
| 129 | *r = q; |
| 130 | *g = v; |
| 131 | *b = p; |
| 132 | break; |
| 133 | case 3: |
| 134 | *r = p; |
| 135 | *g = q; |
| 136 | *b = v; |
| 137 | break; |
| 138 | case 5: |
| 139 | *r = v; |
| 140 | *g = p; |
| 141 | *b = q; |
| 142 | break; |
| 143 | } |
| 144 | } else { |
| 145 | double t = v * (1 - s * (1 - f)); |
| 146 | switch (i) { |
| 147 | case 0: |
| 148 | *r = v; |
| 149 | *g = t; |
| 150 | *b = p; |
| 151 | break; |
| 152 | case 2: |
| 153 | *r = p; |
| 154 | *g = v; |
| 155 | *b = t; |
| 156 | break; |
| 157 | case 4: |
| 158 | *r = t; |
| 159 | *g = p; |
| 160 | *b = v; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void updateBsplineMapcxx . . $ updateBsplineMap(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance); |
| 167 | void updateCsplineMapcxx . . $ updateCsplineMap(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance); |
| 168 | |
| 169 | char **param_namescxx . . $ param_names. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 170 | int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).() |
| 171 | { |
| 172 | param_namescxx . . $ param_names. = (char**)calloccxx . . $ calloc(63a7bf2941bfcd7a).(10, sizeof(char *)); |
| 173 | for(int i = 0; i < 10; i++) { |
| 174 | char *val = i % 2 == 0?INPUT_VALUEcxx . . $ `src/filter/curves/curves.c:42:9`!:OUTPUT_VALUEcxx . . $ `src/filter/curves/curves.c:43:9`!; |
| 175 | param_namescxx . . $ param_names.[i] = (char*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(strlencxx . . $ strlen(751346f8406fd082).(POINTcxx . . $ `src/filter/curves/curves.c:41:9`!) + 2 + strlencxx . . $ strlen(751346f8406fd082).(val), sizeof(char)); |
| 176 | sprintfcxx . . $ sprintf(26c8642d26409d6d).(param_namescxx . . $ param_names.[i], "%s%d%s", POINTcxx . . $ `src/filter/curves/curves.c:41:9`!, i / 2 + 1, val); |
| 177 | } |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).() |
| 182 | { |
| 183 | for(int i = 0; i < 10; i++) |
| 184 | freecxx . . $ free(5d22bdacc48458e8).(param_namescxx . . $ param_names.[i]); |
| 185 | freecxx . . $ free(5d22bdacc48458e8).(param_namescxx . . $ param_names.); |
| 186 | } |
| 187 | |
| 188 | void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* curves_info) |
| 189 | { |
| 190 | curves_info->namecxx . . $ f0r_plugin_info#name. = "Curves"; |
| 191 | curves_info->authorcxx . . $ f0r_plugin_info#author. = "Maksim Golovkin, Till Theato"; |
| 192 | curves_info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!; |
| 193 | curves_info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!; |
| 194 | curves_info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!; |
| 195 | curves_info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0; |
| 196 | curves_info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 4; |
| 197 | curves_info->num_paramscxx . . $ f0r_plugin_info#num_params. = 16; |
| 198 | curves_info->explanationcxx . . $ f0r_plugin_info#explanation. = "Adjust luminance or color channel intensity with curve level mapping"; |
| 199 | } |
| 200 | |
| 201 | char *get_param_namecxx . . $ get_param_name(ff224f080444ee45).(int param_index) { |
| 202 | return param_namescxx . . $ param_names.[param_index]; |
| 203 | } |
| 204 | |
| 205 | void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index) |
| 206 | { |
| 207 | switch(param_index) |
| 208 | { |
| 209 | case 0: |
| 210 | info->namecxx . . $ f0r_param_info#name. = "Channel"; |
| 211 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 212 | info->explanationcxx . . $ f0r_param_info#explanation. = "Channel to adjust (0 = red, 0.1 = green, 0.2 = blue, 0.3 = alpha, 0.4 = luma, 0.5 = rgb, 0.6 = hue, 0.7 = saturation)"; |
| 213 | break; |
| 214 | case 1: |
| 215 | info->namecxx . . $ f0r_param_info#name. = "Show curves"; |
| 216 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_BOOLcxx . . $ `include/frei0r.h:377:9`!; |
| 217 | info->explanationcxx . . $ f0r_param_info#explanation. = "Draw curve graph on output image"; |
| 218 | break; |
| 219 | case 2: |
| 220 | info->namecxx . . $ f0r_param_info#name. = "Graph position"; |
| 221 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 222 | info->explanationcxx . . $ f0r_param_info#explanation. = "Output image corner where curve graph will be drawn (0.1 = TOP,LEFT; 0.2 = TOP,RIGHT; 0.3 = BOTTOM,LEFT; 0.4 = BOTTOM, RIGHT)"; |
| 223 | break; |
| 224 | case 3: |
| 225 | info->namecxx . . $ f0r_param_info#name. = "Curve point number"; |
| 226 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 227 | info->explanationcxx . . $ f0r_param_info#explanation. = "Number of points to use to build curve (/10 to fit [0,1] parameter range). Minimum 2 (0.2), Maximum 5 (0.5). Not relevant for Bézier spline."; |
| 228 | break; |
| 229 | case 4: |
| 230 | info->namecxx . . $ f0r_param_info#name. = "Luma formula"; |
| 231 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_BOOLcxx . . $ `include/frei0r.h:377:9`!; |
| 232 | info->explanationcxx . . $ f0r_param_info#explanation. = "Use Rec. 601 (false) or Rec. 709 (true)"; |
| 233 | break; |
| 234 | case 5: |
| 235 | info->namecxx . . $ f0r_param_info#name. = "Bézier spline"; |
| 236 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_STRINGcxx . . $ `include/frei0r.h:400:9`!; |
| 237 | info->explanationcxx . . $ f0r_param_info#explanation. = "Use cubic Bézier spline. Has to be a sorted list of points in the format 'handle1x;handle1y#pointx;pointy#handle2x;handle2y'(pointx = in, pointy = out). Points are separated by a '|'.The values can have 'double' precision. x, y for points should be in the range 0-1. x,y for handles might also be out of this range."; |
| 238 | default: |
| 239 | if (param_index > 5) { |
| 240 | info->namecxx . . $ f0r_param_info#name. = get_param_namecxx . . $ get_param_name(ff224f080444ee45).(param_index - 6); |
| 241 | info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!; |
| 242 | info->explanationcxx . . $ f0r_param_info#explanation. = get_param_namecxx . . $ get_param_name(ff224f080444ee45).(param_index - 6); |
| 243 | } |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height) |
| 249 | { |
| 250 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst)); |
| 251 | inst->widthcxx . . $ curves_instance#width. = width; inst->heightcxx . . $ curves_instance#height. = height; |
| 252 | inst->channelcxx . . $ curves_instance#channel. = CHANNEL_RGBcxx . . $ CHANNEL_RGB.; |
| 253 | inst->drawCurvescxx . . $ curves_instance#drawCurves. = 1; |
| 254 | inst->curvesPositioncxx . . $ curves_instance#curvesPosition. = 3; |
| 255 | inst->pointNumbercxx . . $ curves_instance#pointNumber. = 2; |
| 256 | inst->formulacxx . . $ curves_instance#formula. = 1; |
| 257 | inst->bsplinecxx . . $ curves_instance#bspline. = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(char)); |
| 258 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 259 | inst->csplineMapcxx . . $ curves_instance#csplineMap. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 260 | inst->curveMapcxx . . $ curves_instance#curveMap. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 261 | inst->pointscxx . . $ curves_instance#points.[0] = 0; |
| 262 | inst->pointscxx . . $ curves_instance#points.[1] = 0; |
| 263 | inst->pointscxx . . $ curves_instance#points.[2] = 1; |
| 264 | inst->pointscxx . . $ curves_instance#points.[3] = 1; |
| 265 | inst->pointscxx . . $ curves_instance#points.[4] = 0; |
| 266 | inst->pointscxx . . $ curves_instance#points.[5] = 0; |
| 267 | inst->pointscxx . . $ curves_instance#points.[6] = 0; |
| 268 | inst->pointscxx . . $ curves_instance#points.[7] = 0; |
| 269 | inst->pointscxx . . $ curves_instance#points.[8] = 0; |
| 270 | inst->pointscxx . . $ curves_instance#points.[9] = 0; |
| 271 | return (f0r_instance_tcxx . . $ f0r_instance_t#)inst; |
| 272 | } |
| 273 | |
| 274 | void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 275 | { |
| 276 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)instance; |
| 277 | freecxx . . $ free(5d22bdacc48458e8).(inst->bsplinecxx . . $ curves_instance#bspline.); |
| 278 | freecxx . . $ free(5d22bdacc48458e8).(inst->bsplineMapcxx . . $ curves_instance#bsplineMap.); |
| 279 | freecxx . . $ free(5d22bdacc48458e8).(inst->csplineMapcxx . . $ curves_instance#csplineMap.); |
| 280 | freecxx . . $ free(5d22bdacc48458e8).(inst->curveMapcxx . . $ curves_instance#curveMap.); |
| 281 | freecxx . . $ free(5d22bdacc48458e8).(inst); |
| 282 | } |
| 283 | |
| 284 | void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 285 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 286 | { |
| 287 | assertcxx . . $ `<external>/4398592474888995393/assert.h:75:11`!(instance); |
| 288 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)instance; |
| 289 | |
| 290 | double tmp; |
| 291 | f0r_param_stringcxx . . $ f0r_param_string# bspline; |
| 292 | |
| 293 | switch(param_index) |
| 294 | { |
| 295 | case 0: |
| 296 | tmp = *((f0r_param_doublecxx . . $ f0r_param_double# *)param); |
| 297 | if (tmp >= 1) { |
| 298 | // legacy support |
| 299 | if (tmp == 3) { |
| 300 | if (inst->channelcxx . . $ curves_instance#channel. != CHANNEL_LUMAcxx . . $ CHANNEL_LUMA.) { |
| 301 | inst->channelcxx . . $ curves_instance#channel. = CHANNEL_LUMAcxx . . $ CHANNEL_LUMA.; |
| 302 | if (strlencxx . . $ strlen(751346f8406fd082).(inst->bsplinecxx . . $ curves_instance#bspline.)) |
| 303 | updateBsplineMapcxx . . $ updateBsplineMap(5d22bdacc48458e8).(instance); |
| 304 | else |
| 305 | updateCsplineMapcxx . . $ updateCsplineMap(5d22bdacc48458e8).(instance); |
| 306 | } |
| 307 | } else { |
| 308 | if ((int)inst->channelcxx . . $ curves_instance#channel. != (int)tmp) { |
| 309 | inst->channelcxx . . $ curves_instance#channel. = (enum CHANNELScxx . . $ CHANNELS#)((int)tmp); |
| 310 | if (strlencxx . . $ strlen(751346f8406fd082).(inst->bsplinecxx . . $ curves_instance#bspline.)) |
| 311 | updateBsplineMapcxx . . $ updateBsplineMap(5d22bdacc48458e8).(instance); |
| 312 | else |
| 313 | updateCsplineMapcxx . . $ updateCsplineMap(5d22bdacc48458e8).(instance); |
| 314 | } |
| 315 | } |
| 316 | } else { |
| 317 | if ((int)inst->channelcxx . . $ curves_instance#channel. != (int)(tmp * 10)) { |
| 318 | inst->channelcxx . . $ curves_instance#channel. = (enum CHANNELScxx . . $ CHANNELS#)(tmp * 10); |
| 319 | if (strlencxx . . $ strlen(751346f8406fd082).(inst->bsplinecxx . . $ curves_instance#bspline.)) |
| 320 | updateBsplineMapcxx . . $ updateBsplineMap(5d22bdacc48458e8).(instance); |
| 321 | else |
| 322 | updateCsplineMapcxx . . $ updateCsplineMap(5d22bdacc48458e8).(instance); |
| 323 | } |
| 324 | } |
| 325 | break; |
| 326 | case 1: |
| 327 | inst->drawCurvescxx . . $ curves_instance#drawCurves. = *((f0r_param_doublecxx . . $ f0r_param_double# *)param); |
| 328 | break; |
| 329 | case 2: |
| 330 | inst->curvesPositioncxx . . $ curves_instance#curvesPosition. = floor(*((f0r_param_doublecxx . . $ f0r_param_double# *)param) * 10); |
| 331 | break; |
| 332 | case 3: |
| 333 | inst->pointNumbercxx . . $ curves_instance#pointNumber. = floor(*((f0r_param_doublecxx . . $ f0r_param_double# *)param) * 10); |
| 334 | inst->pointNumbercxx . . $ curves_instance#pointNumber. = MINcxx . . $ `include/frei0r/math.h:67:9`!(inst->pointNumber, 5); |
| 335 | break; |
| 336 | case 4: |
| 337 | inst->formulacxx . . $ curves_instance#formula. = *((f0r_param_doublecxx . . $ f0r_param_double# *)param); |
| 338 | break; |
| 339 | case 5: |
| 340 | bspline = *((f0r_param_stringcxx . . $ f0r_param_string# *)param); |
| 341 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(inst->bsplinecxx . . $ curves_instance#bspline., bspline) != 0) { |
| 342 | freecxx . . $ free(5d22bdacc48458e8).(inst->bsplinecxx . . $ curves_instance#bspline.); |
| 343 | inst->bsplinecxx . . $ curves_instance#bspline. = strdupcxx . . $ strdup(a8b8e32347182212).(bspline); |
| 344 | updateBsplineMapcxx . . $ updateBsplineMap(5d22bdacc48458e8).(instance); |
| 345 | } |
| 346 | break; |
| 347 | default: |
| 348 | if (param_index > 5){ |
| 349 | inst->pointscxx . . $ curves_instance#points.[param_index - 6] = *((f0r_param_doublecxx . . $ f0r_param_double# *)param); //Assigning value to curve point |
| 350 | updateCsplineMapcxx . . $ updateCsplineMap(5d22bdacc48458e8).(instance); |
| 351 | } |
| 352 | |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance, |
| 358 | f0r_param_tcxx . . $ f0r_param_t# param, int param_index) |
| 359 | { |
| 360 | assertcxx . . $ `<external>/4398592474888995393/assert.h:75:11`!(instance); |
| 361 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)instance; |
| 362 | |
| 363 | switch(param_index) |
| 364 | { |
| 365 | case 0: |
| 366 | *((f0r_param_doublecxx . . $ f0r_param_double# *)param) = inst->channelcxx . . $ curves_instance#channel. / 10.; |
| 367 | break; |
| 368 | case 1: |
| 369 | *((f0r_param_doublecxx . . $ f0r_param_double# *)param) = inst->drawCurvescxx . . $ curves_instance#drawCurves.; |
| 370 | break; |
| 371 | case 2: |
| 372 | *((f0r_param_doublecxx . . $ f0r_param_double# *)param) = inst->curvesPositioncxx . . $ curves_instance#curvesPosition. / 10.; |
| 373 | break; |
| 374 | case 3: |
| 375 | *((f0r_param_doublecxx . . $ f0r_param_double# *)param) = inst->pointNumbercxx . . $ curves_instance#pointNumber. / 10.; |
| 376 | break; |
| 377 | case 4: |
| 378 | *((f0r_param_doublecxx . . $ f0r_param_double# *)param) = inst->formulacxx . . $ curves_instance#formula.; |
| 379 | break; |
| 380 | case 5: |
| 381 | *((f0r_param_stringcxx . . $ f0r_param_string# *)param) = inst->bsplinecxx . . $ curves_instance#bspline.; |
| 382 | break; |
| 383 | default: |
| 384 | if (param_index > 5) |
| 385 | *((f0r_param_doublecxx . . $ f0r_param_double# *)param) = inst->pointscxx . . $ curves_instance#points.[param_index - 6]; //Fetch curve point value |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | double* gaussSLESolvecxx . . $ gaussSLESolve(aad16bd664a909ce).(size_t size, double* A) { |
| 391 | int extSize = size + 1; |
| 392 | //direct way: transform matrix A to triangular form |
| 393 | for(int row = 0; row < size; row++) { |
| 394 | int col = row; |
| 395 | int lastRowToSwap = size - 1; |
| 396 | while (A[row * extSize + col] == 0 && lastRowToSwap > row) { //checking if current and lower rows can be swapped |
| 397 | for(int i = 0; i < extSize; i++) { |
| 398 | double tmp = A[row * extSize + i]; |
| 399 | A[row * extSize + i] = A[lastRowToSwap * extSize + i]; |
| 400 | A[lastRowToSwap * extSize + i] = tmp; |
| 401 | } |
| 402 | lastRowToSwap--; |
| 403 | } |
| 404 | double coeff = A[row * extSize + col]; |
| 405 | for(int j = 0; j < extSize; j++) |
| 406 | A[row * extSize + j] /= coeff; |
| 407 | if (lastRowToSwap > row) { |
| 408 | for(int i = row + 1; i < size; i++) { |
| 409 | double rowCoeff = -A[i * extSize + col]; |
| 410 | for(int j = col; j < extSize; j++) |
| 411 | A[i * extSize + j] += A[row * extSize + j] * rowCoeff; |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | //backward way: find solution from last to first |
| 416 | double *solution = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(size, sizeof(double)); |
| 417 | for(int i = size - 1; i >= 0; i--) { |
| 418 | solution[i] = A[i * extSize + size];// |
| 419 | for(int j = size - 1; j > i; j--) { |
| 420 | solution[i] -= solution[j] * A[i * extSize + j]; |
| 421 | } |
| 422 | } |
| 423 | return solution; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | |
| 428 | double* calcSplineCoeffscxx . . $ calcSplineCoeffs(960b153087286a21).(double* points, size_t pointsSize) { |
| 429 | double* coeffs = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 430 | int size = pointsSize; |
| 431 | int mxSize = size > 3?4:size; |
| 432 | int extMxSize = mxSize + 1; |
| 433 | if (size == 2) { //coefficients of linear function Ax + B = y |
| 434 | double *m = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(mxSize * extMxSize, sizeof(double)); |
| 435 | for(int i = 0; i < size; i++) { |
| 436 | int offset = i * 2; |
| 437 | m[i * extMxSize] = points[offset]; |
| 438 | m[i * extMxSize + 1] = 1; |
| 439 | m[i * extMxSize + 2] = points[offset + 1]; |
| 440 | } |
| 441 | coeffs = gaussSLESolvecxx . . $ gaussSLESolve(aad16bd664a909ce).(size, m); |
| 442 | freecxx . . $ free(5d22bdacc48458e8).(m); |
| 443 | } else if (size == 3) { //coefficients of quadrant function Ax^2 + Bx + C = y |
| 444 | double *m = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(mxSize * extMxSize, sizeof(double)); |
| 445 | for(int i = 0; i < size; i++) { |
| 446 | int offset = i * 2; |
| 447 | m[i * extMxSize] = points[offset]*points[offset]; |
| 448 | m[i * extMxSize + 1] = points[offset]; |
| 449 | m[i * extMxSize + 2] = 1; |
| 450 | m[i * extMxSize + 3] = points[offset + 1]; |
| 451 | } |
| 452 | coeffs = gaussSLESolvecxx . . $ gaussSLESolve(aad16bd664a909ce).(size, m); |
| 453 | freecxx . . $ free(5d22bdacc48458e8).(m); |
| 454 | } else if (size > 3) { //coefficients of cubic spline Ax^3 + Bx^2 + Cx + D = y |
| 455 | coeffs = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(5 * size,sizeof(double)); |
| 456 | for(int i = 0; i < size; i++) { |
| 457 | int offset = i * 5; |
| 458 | int srcOffset = i * 2; |
| 459 | coeffs[offset] = points[srcOffset]; |
| 460 | coeffs[offset + 1] = points[srcOffset + 1]; |
| 461 | } |
| 462 | coeffs[3] = coeffs[(size - 1) * 5 + 3] = 0; |
| 463 | double *alpha = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(size - 1,sizeof(double)); |
| 464 | double *beta = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(size - 1,sizeof(double)); |
| 465 | alpha[0] = beta[0] = 0; |
| 466 | for(int i = 1; i < size - 1; i++) { |
| 467 | int srcI = i * 2; |
| 468 | int srcI_1 = (i - 1) * 2; |
| 469 | int srcI1 = (i + 1) * 2; |
| 470 | double h_i = points[srcI] - points[srcI_1], |
| 471 | h_i1 = points[srcI1] - points[srcI]; |
| 472 | double A = h_i; |
| 473 | double C = 2. * (h_i + h_i1); |
| 474 | double B = h_i1; |
| 475 | double F = 6. * ((points[srcI1 + 1] - points[srcI + 1]) / h_i1 - (points[srcI + 1] - points[srcI_1 + 1]) / h_i); |
| 476 | double z = (A * alpha[i - 1] + C); |
| 477 | alpha[i] = -B / z; |
| 478 | beta[i] = (F - A * beta[i - 1]) / z; |
| 479 | } |
| 480 | for (int i = size - 2; i > 0; --i) |
| 481 | coeffs[i * 5 + 3] = alpha[i] * coeffs[(i + 1) * 5 + 3] + beta[i]; |
| 482 | freecxx . . $ free(5d22bdacc48458e8).(beta); |
| 483 | freecxx . . $ free(5d22bdacc48458e8).(alpha); |
| 484 | |
| 485 | for (int i = size - 1; i > 0; --i){ |
| 486 | int srcI = i * 2; |
| 487 | int srcI_1 = (i - 1) * 2; |
| 488 | double h_i = points[srcI] - points[srcI_1]; |
| 489 | int offset = i * 5; |
| 490 | coeffs[offset + 4] = (coeffs[offset + 3] - coeffs[offset - 2]) / h_i; |
| 491 | coeffs[offset + 2] = h_i * (2. * coeffs[offset + 3] + coeffs[offset - 2]) / 6. + (points[srcI + 1] - points[srcI_1 + 1]) / h_i; |
| 492 | } |
| 493 | } |
| 494 | return coeffs; |
| 495 | } |
| 496 | |
| 497 | double splinecxx . . $ spline(73fbe2043d1f50a0).(double x, double* points, size_t pointSize, double* coeffs) { |
| 498 | int size = pointSize; |
| 499 | if (size == 2) { |
| 500 | return coeffs[0] * x + coeffs[1]; |
| 501 | } else if (size == 3) { |
| 502 | return (coeffs[0] * x + coeffs[1]) * x + coeffs[2]; |
| 503 | } else if (size > 3) { |
| 504 | int offset = 5; |
| 505 | if (x <= points[0]) //find valid interval of cubic spline |
| 506 | offset = 1; |
| 507 | else if (x >= points[(size - 1) * 2]) |
| 508 | offset = size - 1; |
| 509 | else { |
| 510 | int i = 0, j = size - 1; |
| 511 | while (i + 1 < j) { |
| 512 | int k = i + (j - i) / 2; |
| 513 | if (x <= points[k * 2]) |
| 514 | j = k; |
| 515 | else |
| 516 | i = k; |
| 517 | } |
| 518 | offset = j; |
| 519 | } |
| 520 | offset *= 5; |
| 521 | double dx = x - coeffs[offset]; |
| 522 | return ((coeffs[offset + 4] * dx / 6. + coeffs[offset + 3] / 2.) * dx + coeffs[offset + 2]) * dx + coeffs[offset + 1]; |
| 523 | } |
| 524 | /* This should never be reached, statement passifies the compiler*/ |
| 525 | return -1.0; |
| 526 | } |
| 527 | |
| 528 | void swapcxx . . $ swap(22e9c2f393fa84e8).(double *points, int i, int j) { |
| 529 | int offsetX = i * 2, offsetY = j * 2; |
| 530 | double tempX = points[offsetX], tempY = points[offsetX + 1]; |
| 531 | points[offsetX] = points[offsetY]; |
| 532 | points[offsetX + 1] = points[offsetY + 1]; |
| 533 | points[offsetY] = tempX; |
| 534 | points[offsetY + 1] = tempY; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | /** |
| 539 | * Calculates a x,y pair for \param t on the cubic Bézier curve defined by \param points. |
| 540 | * \param t "time" in the range 0-1 |
| 541 | * \param points points[0] = point1, point[1] = handle1, point[2] = handle2, point[3] = point2 |
| 542 | */ |
| 543 | positioncxx . . $ position# pointOnBeziercxx . . $ pointOnBezier(cc781fc7b2922560).(double t, positioncxx . . $ position# points[4]) |
| 544 | { |
| 545 | positioncxx . . $ position# pos; |
| 546 | |
| 547 | /* |
| 548 | * Calculating a point on the bezier curve using the coefficients from Bernstein basis polynomial of degree 3. |
| 549 | * Using the De Casteljau algorithm would be slightly faster when calculating a lot of values |
| 550 | * but the difference is far from noticeable here since we update the spline only when the parameter changes |
| 551 | */ |
| 552 | double c1 = (1-t) * (1-t) * (1-t); |
| 553 | double c2 = 3 * t * (1-t) * (1-t); |
| 554 | double c3 = 3 * t * t * (1-t); |
| 555 | double c4 = t * t * t; |
| 556 | pos.xcxx . . $ position#x. = points[0].xcxx . . $ position#x.*c1 + points[1].xcxx . . $ position#x.*c2 + points[2].xcxx . . $ position#x.*c3 + points[3].xcxx . . $ position#x.*c4; |
| 557 | pos.ycxx . . $ position#y. = points[0].ycxx . . $ position#y.*c1 + points[1].ycxx . . $ position#y.*c2 + points[2].ycxx . . $ position#y.*c3 + points[3].ycxx . . $ position#y.*c4; |
| 558 | return pos; |
| 559 | } |
| 560 | |
| 561 | #if defined(_WIN32) || defined(_WIN64) |
| 562 | # define strtok_r strtok_s |
| 563 | #endif |
| 564 | |
| 565 | /** |
| 566 | * Splits given string into sub-strings at given delimiter. |
| 567 | * \param string input string |
| 568 | * \param delimiter delimiter |
| 569 | * \param tokens pointer to array of strings, will be filled with sub-strings |
| 570 | * \return Number of sub-strings |
| 571 | */ |
| 572 | int tokenisecxx . . $ tokenise(d996ab1fcfa78789).(char *string, const char *delimiter, char ***tokens) |
| 573 | { |
| 574 | int count = 0; |
| 575 | char *input = strdupcxx . . $ strdup(a8b8e32347182212).(string); |
| 576 | char *result = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 577 | char *saveptr; |
| 578 | result = strtok_rcxx . . $ strtok_r(a42394c85d6397dd).(input, delimiter, &saveptr); |
| 579 | while (result != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 580 | *tokens = realloccxx . . $ realloc(417ab160f6baadc6).(*tokens, (count + 1) * sizeof(char *)); |
| 581 | (*tokens)[count++] = strdupcxx . . $ strdup(a8b8e32347182212).(result); |
| 582 | result = strtok_rcxx . . $ strtok_r(a42394c85d6397dd).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, delimiter, &saveptr); |
| 583 | } |
| 584 | freecxx . . $ free(5d22bdacc48458e8).(input); |
| 585 | return count; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Updates the color map according to the bézier spline described in the "Bézier spline" parameter. |
| 590 | */ |
| 591 | void updateBsplineMapcxx . . $ updateBsplineMap(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 592 | { |
| 593 | assertcxx . . $ `<external>/4398592474888995393/assert.h:75:11`!(instance); |
| 594 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)instance; |
| 595 | |
| 596 | int range = inst->channelcxx . . $ curves_instance#channel. == CHANNEL_HUEcxx . . $ CHANNEL_HUE. ? 361 : 256; |
| 597 | freecxx . . $ free(5d22bdacc48458e8).(inst->bsplineMapcxx . . $ curves_instance#bsplineMap.); |
| 598 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(range * sizeof(double)); |
| 599 | // fill with default values, in case the spline does not cover the whole range |
| 600 | if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_HUEcxx . . $ CHANNEL_HUE.) { |
| 601 | for(int i = 0; i < 361; ++i) |
| 602 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[i] = i; |
| 603 | } else if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_LUMAcxx . . $ CHANNEL_LUMA. || inst->channelcxx . . $ curves_instance#channel. == CHANNEL_SATURATIONcxx . . $ CHANNEL_SATURATION.) { |
| 604 | for(int i = 0; i < 256; ++i) |
| 605 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[i] = inst->channelcxx . . $ curves_instance#channel. == CHANNEL_LUMAcxx . . $ CHANNEL_LUMA. ? 1 : i / 255.; |
| 606 | } else { |
| 607 | for(int i = 0; i < 256; ++i) |
| 608 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[i] = i; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * string -> list of points |
| 613 | */ |
| 614 | char **pointStr = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(char *)); |
| 615 | int count = tokenisecxx . . $ tokenise(d996ab1fcfa78789).(inst->bsplinecxx . . $ curves_instance#bspline., "|", &pointStr); |
| 616 | |
| 617 | bspline_pointcxx . . $ bspline_point# *points = (bspline_pointcxx . . $ bspline_point# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(count * sizeof(bspline_pointcxx . . $ bspline_point#)); |
| 618 | |
| 619 | for (int i = 0; i < count; ++i) { |
| 620 | char **positionsStr = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(char *)); |
| 621 | int positionsNum = tokenisecxx . . $ tokenise(d996ab1fcfa78789).(pointStr[i], "#", &positionsStr); |
| 622 | if (positionsNum == 3) { // h1, p, h2 |
| 623 | for (int j = 0; j < positionsNum; ++j) { |
| 624 | char **coords = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(char *)); |
| 625 | int coordsNum = tokenisecxx . . $ tokenise(d996ab1fcfa78789).(positionsStr[j], ";", &coords); |
| 626 | if (coordsNum == 2) { // x, y |
| 627 | points[i][j].xcxx . . $ position#x. = atofcxx . . $ atof(9a18bdb0e56c1bc2).(coords[0]); |
| 628 | points[i][j].ycxx . . $ position#y. = atofcxx . . $ atof(9a18bdb0e56c1bc2).(coords[1]); |
| 629 | } |
| 630 | for (int k = 0; k < coordsNum; ++k) |
| 631 | freecxx . . $ free(5d22bdacc48458e8).(coords[k]); |
| 632 | freecxx . . $ free(5d22bdacc48458e8).(coords); |
| 633 | } |
| 634 | } |
| 635 | for(int j = 0; j < positionsNum; ++j) |
| 636 | freecxx . . $ free(5d22bdacc48458e8).(positionsStr[j]); |
| 637 | freecxx . . $ free(5d22bdacc48458e8).(positionsStr); |
| 638 | } |
| 639 | |
| 640 | for (int i = 0; i < count; ++i) |
| 641 | freecxx . . $ free(5d22bdacc48458e8).(pointStr[i]); |
| 642 | freecxx . . $ free(5d22bdacc48458e8).(pointStr); |
| 643 | |
| 644 | /* |
| 645 | * Actual work: calculate curves between points and fill map |
| 646 | */ |
| 647 | positioncxx . . $ position# p[4]; |
| 648 | double t, step, diff, y; |
| 649 | int pn, c, k; |
| 650 | for (int i = 0; i < count - 1; ++i) { |
| 651 | p[0] = points[i][1]; |
| 652 | p[1] = points[i][2]; |
| 653 | p[2] = points[i+1][0]; |
| 654 | p[3] = points[i+1][1]; |
| 655 | |
| 656 | // make sure points are in correct order |
| 657 | if (p[0].xcxx . . $ position#x. > p[3].xcxx . . $ position#x.) |
| 658 | continue; |
| 659 | |
| 660 | // try to avoid loops and other cases of one x having multiple y |
| 661 | p[1].xcxx . . $ position#x. = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(p[1].x, p[0].x, p[3].x); |
| 662 | p[2].xcxx . . $ position#x. = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(p[2].x, p[0].x, p[3].x); |
| 663 | |
| 664 | t = 0; |
| 665 | pn = 0; |
| 666 | // number of points calculated for this curve |
| 667 | // x range * 10 should give enough points |
| 668 | c = (int)((p[3].xcxx . . $ position#x. - p[0].xcxx . . $ position#x.) * range * 10); |
| 669 | if (c == 0) { |
| 670 | // points have same x value -> will result in a jump in the curve |
| 671 | // calculate anyways, in case we only have these two points (with more points this x value will be calculated three times) |
| 672 | c = 1; |
| 673 | } |
| 674 | step = 1 / (double)c; |
| 675 | positioncxx . . $ position# *curve = (positioncxx . . $ position# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64).((c + 1) * sizeof(positioncxx . . $ position#)); |
| 676 | while (t <= 1) { |
| 677 | curve[pn++] = pointOnBeziercxx . . $ pointOnBezier(cc781fc7b2922560).(t, p); |
| 678 | t += step; |
| 679 | } |
| 680 | |
| 681 | // Fill the map in range this curve provides |
| 682 | k = 0; |
| 683 | // connection points will be written twice (but therefore no special case for the last point is required) |
| 684 | for (int j = (int)(p[0].xcxx . . $ position#x. * (range-1)); j <= (int)(p[3].xcxx . . $ position#x. * (range-1)); ++j) { |
| 685 | if (k > 0) |
| 686 | --k; |
| 687 | diff = fabs(j / ((double)(range-1)) - curve[k].xcxx . . $ position#x.); |
| 688 | y = curve[k].ycxx . . $ position#y.; |
| 689 | // Find point closest to the one needed (integers 0 - range) |
| 690 | while (++k < pn) { |
| 691 | if (fabs(j / ((double)(range-1)) - curve[k].xcxx . . $ position#x.) > diff) |
| 692 | break; |
| 693 | diff = fabs(j / ((double)(range-1)) - curve[k].xcxx . . $ position#x.); |
| 694 | y = curve[k++].ycxx . . $ position#y.; |
| 695 | } |
| 696 | |
| 697 | if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_HUEcxx . . $ CHANNEL_HUE.) |
| 698 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[j] = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(y * 360, 0, 360); |
| 699 | else if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_LUMAcxx . . $ CHANNEL_LUMA.) |
| 700 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[j] = y / (j == 0 ? 1 : j / 255.); |
| 701 | else if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_SATURATIONcxx . . $ CHANNEL_SATURATION.) |
| 702 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[j] = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(y, 0, 1); |
| 703 | else |
| 704 | inst->bsplineMapcxx . . $ curves_instance#bsplineMap.[j] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(ROUNDcxx . . $ `include/frei0r/math.h:53:9`!(y * 255)); |
| 705 | } |
| 706 | |
| 707 | freecxx . . $ free(5d22bdacc48458e8).(curve); |
| 708 | } |
| 709 | |
| 710 | freecxx . . $ free(5d22bdacc48458e8).(points); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Updates the color map according to the cubic spline described in the "Curve Point" parameter. |
| 715 | */ |
| 716 | void updateCsplineMapcxx . . $ updateCsplineMap(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance) |
| 717 | { |
| 718 | assertcxx . . $ `<external>/4398592474888995393/assert.h:75:11`!(instance); |
| 719 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)instance; |
| 720 | |
| 721 | int range = inst->channelcxx . . $ curves_instance#channel. == CHANNEL_HUEcxx . . $ CHANNEL_HUE. ? 361 : 256; |
| 722 | freecxx . . $ free(5d22bdacc48458e8).(inst->csplineMapcxx . . $ curves_instance#csplineMap.); |
| 723 | inst->csplineMapcxx . . $ curves_instance#csplineMap. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(range * sizeof(double)); |
| 724 | // fill with default values, in case the spline does not cover the whole range |
| 725 | if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_HUEcxx . . $ CHANNEL_HUE.) { |
| 726 | for(int i = 0; i < 361; ++i) |
| 727 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[i] = i; |
| 728 | } else if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_LUMAcxx . . $ CHANNEL_LUMA. || inst->channelcxx . . $ curves_instance#channel. == CHANNEL_SATURATIONcxx . . $ CHANNEL_SATURATION.) { |
| 729 | for(int i = 0; i < 256; ++i) |
| 730 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[i] = inst->channelcxx . . $ curves_instance#channel. == CHANNEL_LUMAcxx . . $ CHANNEL_LUMA. ? 1 : i / 255.; |
| 731 | } else { |
| 732 | for(int i = 0; i < 256; ++i) |
| 733 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[i] = i; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Retrieve points |
| 738 | */ |
| 739 | double *points = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(inst->pointNumbercxx . . $ curves_instance#pointNumber. * 2, sizeof(double)); |
| 740 | int i = inst->pointNumbercxx . . $ curves_instance#pointNumber. * 2; |
| 741 | //copy point values |
| 742 | while(--i >= 0) |
| 743 | points[i] = inst->pointscxx . . $ curves_instance#points.[i]; |
| 744 | //sort point values by X component |
| 745 | for(i = 1; i < inst->pointNumbercxx . . $ curves_instance#pointNumber.; i++) |
| 746 | for(int j = i; j > 0 && points[j * 2] < points[(j - 1) * 2]; j--) |
| 747 | swapcxx . . $ swap(22e9c2f393fa84e8).(points, j, j - 1); |
| 748 | //calculating spline coeffincients |
| 749 | double *coeffs = calcSplineCoeffscxx . . $ calcSplineCoeffs(960b153087286a21).(points, (size_t)inst->pointNumbercxx . . $ curves_instance#pointNumber.); |
| 750 | |
| 751 | /* |
| 752 | * Actual work: calculate curves between points and fill map |
| 753 | */ |
| 754 | for(int j = 0; j < range; j++) { |
| 755 | double x = j / (double)(range - 1); |
| 756 | double y = splinecxx . . $ spline(73fbe2043d1f50a0).(x, points, (size_t)inst->pointNumbercxx . . $ curves_instance#pointNumber., coeffs); |
| 757 | if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_HUEcxx . . $ CHANNEL_HUE.) |
| 758 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[j] = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(y * 360, 0, 360); |
| 759 | else if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_LUMAcxx . . $ CHANNEL_LUMA.) |
| 760 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[j] = y / (j == 0 ? 1 : j / 255.); |
| 761 | else if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_SATURATIONcxx . . $ CHANNEL_SATURATION.) |
| 762 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[j] = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(y, 0, 1); |
| 763 | else |
| 764 | inst->csplineMapcxx . . $ curves_instance#csplineMap.[j] = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).(ROUNDcxx . . $ `include/frei0r/math.h:53:9`!(y * 255)); |
| 765 | } |
| 766 | if (inst->drawCurvescxx . . $ curves_instance#drawCurves.) { |
| 767 | int scale = inst->heightcxx . . $ curves_instance#height. / 2; |
| 768 | freecxx . . $ free(5d22bdacc48458e8).(inst->curveMapcxx . . $ curves_instance#curveMap.); |
| 769 | inst->curveMapcxx . . $ curves_instance#curveMap. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(scale * sizeof(float)); |
| 770 | for(i = 0; i < scale; i++) |
| 771 | inst->curveMapcxx . . $ curves_instance#curveMap.[i] = splinecxx . . $ spline(73fbe2043d1f50a0).((double)i / scale, points, (size_t)inst->pointNumbercxx . . $ curves_instance#pointNumber., coeffs) * scale; |
| 772 | } |
| 773 | |
| 774 | freecxx . . $ free(5d22bdacc48458e8).(coeffs); |
| 775 | freecxx . . $ free(5d22bdacc48458e8).(points); |
| 776 | } |
| 777 | |
| 778 | void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time, |
| 779 | const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe) |
| 780 | { |
| 781 | assertcxx . . $ `<external>/4398592474888995393/assert.h:75:11`!(instance); |
| 782 | curves_instance_tcxx . . $ curves_instance_t#* inst = (curves_instance_tcxx . . $ curves_instance_t#*)instance; |
| 783 | unsigned int len = inst->widthcxx . . $ curves_instance#width. * inst->heightcxx . . $ curves_instance#height.; |
| 784 | |
| 785 | // test initialization c/b spline |
| 786 | double *splinemap = strlencxx . . $ strlen(751346f8406fd082).(inst->bsplinecxx . . $ curves_instance#bspline.)>0 ? inst->bsplineMapcxx . . $ curves_instance#bsplineMap. : inst->csplineMapcxx . . $ curves_instance#csplineMap.; |
| 787 | if(!splinemap) { |
| 788 | memcpycxx . . $ memcpy(cc817cb175f282d).(outframe,inframe,inst->widthcxx . . $ curves_instance#width. * inst->heightcxx . . $ curves_instance#height. * 4); |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | unsigned char* dst = (unsigned char*)outframe; |
| 793 | const unsigned char* src = (unsigned char*)inframe; |
| 794 | |
| 795 | int i = 0; |
| 796 | double *map = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 797 | int scale = inst->heightcxx . . $ curves_instance#height. / 2; |
| 798 | double *points = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 799 | if (strlencxx . . $ strlen(751346f8406fd082).(inst->bsplinecxx . . $ curves_instance#bspline.) == 0) { |
| 800 | points = (double*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(inst->pointNumbercxx . . $ curves_instance#pointNumber. * 2, sizeof(double)); |
| 801 | i = inst->pointNumbercxx . . $ curves_instance#pointNumber. * 2; |
| 802 | //copy point values |
| 803 | while(--i >= 0) |
| 804 | points[i] = inst->pointscxx . . $ curves_instance#points.[i]; |
| 805 | //sort point values by X component |
| 806 | for(i = 1; i < inst->pointNumbercxx . . $ curves_instance#pointNumber.; i++) |
| 807 | for(int j = i; j > 0 && points[j * 2] < points[(j - 1) * 2]; j--) |
| 808 | swapcxx . . $ swap(22e9c2f393fa84e8).(points, j, j - 1); |
| 809 | |
| 810 | map = inst->csplineMapcxx . . $ curves_instance#csplineMap.; |
| 811 | } else { |
| 812 | map = inst->bsplineMapcxx . . $ curves_instance#bsplineMap.; |
| 813 | } |
| 814 | |
| 815 | int r, g, b, luma; |
| 816 | double factorR, factorG, factorB, lumaValue; |
| 817 | double rf, gf, bf, hue, sat, val; |
| 818 | |
| 819 | switch ((int)inst->channelcxx . . $ curves_instance#channel.) { |
| 820 | case CHANNEL_RGBcxx . . $ CHANNEL_RGB.: |
| 821 | while (len--) { |
| 822 | *dst++ = map[*src++]; // r |
| 823 | *dst++ = map[*src++]; // g |
| 824 | *dst++ = map[*src++]; // b |
| 825 | *dst++ = *src++; // a |
| 826 | } |
| 827 | break; |
| 828 | case CHANNEL_REDcxx . . $ CHANNEL_RED.: |
| 829 | memcpycxx . . $ memcpy(cc817cb175f282d).(outframe, inframe, len*sizeof(uint32_tcxx . . $ uint32_t#)); |
| 830 | while (len--) { |
| 831 | *dst = map[*dst]; |
| 832 | dst += 4; |
| 833 | } |
| 834 | break; |
| 835 | case CHANNEL_GREENcxx . . $ CHANNEL_GREEN.: |
| 836 | memcpycxx . . $ memcpy(cc817cb175f282d).(outframe, inframe, len*sizeof(uint32_tcxx . . $ uint32_t#)); |
| 837 | dst += 1; |
| 838 | while (len--) { |
| 839 | *dst = map[*dst]; |
| 840 | dst += 4; |
| 841 | } |
| 842 | break; |
| 843 | case CHANNEL_BLUEcxx . . $ CHANNEL_BLUE.: |
| 844 | memcpycxx . . $ memcpy(cc817cb175f282d).(outframe, inframe, len*sizeof(uint32_tcxx . . $ uint32_t#)); |
| 845 | dst += 2; |
| 846 | while (len--) { |
| 847 | *dst = map[*dst]; |
| 848 | dst += 4; |
| 849 | } |
| 850 | break; |
| 851 | case CHANNEL_ALPHAcxx . . $ CHANNEL_ALPHA.: |
| 852 | memcpycxx . . $ memcpy(cc817cb175f282d).(outframe, inframe, len*sizeof(uint32_tcxx . . $ uint32_t#)); |
| 853 | dst += 3; |
| 854 | while (len--) { |
| 855 | *dst = map[*dst]; |
| 856 | dst += 4; |
| 857 | } |
| 858 | break; |
| 859 | case CHANNEL_LUMAcxx . . $ CHANNEL_LUMA.: |
| 860 | if (inst->formulacxx . . $ curves_instance#formula.) { // Rec.709 |
| 861 | factorR = .2126; |
| 862 | factorG = .7152; |
| 863 | factorB = .0722; |
| 864 | } else { // Rec. 601 |
| 865 | factorR = .299; |
| 866 | factorG = .587; |
| 867 | factorB = .114; |
| 868 | } |
| 869 | while (len--) { |
| 870 | r = *src++; |
| 871 | g = *src++; |
| 872 | b = *src++; |
| 873 | luma = ROUNDcxx . . $ `include/frei0r/math.h:53:9`!(factorR * r + factorG * g + factorB * b); |
| 874 | lumaValue = map[luma]; |
| 875 | if (luma == 0) { |
| 876 | *dst++ = lumaValue; |
| 877 | *dst++ = lumaValue; |
| 878 | *dst++ = lumaValue; |
| 879 | } else { |
| 880 | *dst++ = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int)(r * lumaValue)); |
| 881 | *dst++ = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int)(g * lumaValue)); |
| 882 | *dst++ = CLAMP0255cxx . . $ CLAMP0255(bc0fba5608a0bfe7).((int)(b * lumaValue)); |
| 883 | } |
| 884 | *dst++ = *src++; |
| 885 | } |
| 886 | break; |
| 887 | case CHANNEL_HUEcxx . . $ CHANNEL_HUE.: |
| 888 | while (len--) { |
| 889 | rf = *src++; |
| 890 | gf = *src++; |
| 891 | bf = *src++; |
| 892 | RGBtoHSVcxx . . $ RGBtoHSV(4eeb89725be9c4c3).(rf, gf, bf, &hue, &sat, &val); |
| 893 | if (hue != -1) { |
| 894 | HSVtoRGBcxx . . $ HSVtoRGB(f38ff659fc243f5b).(&rf, &gf, &bf, map[(int)hue], sat, val); |
| 895 | *dst++ = rf * 255; |
| 896 | *dst++ = gf * 255; |
| 897 | *dst++ = bf * 255; |
| 898 | } else { |
| 899 | *dst++ = rf; |
| 900 | *dst++ = gf; |
| 901 | *dst++ = bf; |
| 902 | } |
| 903 | *dst++ = *src++; |
| 904 | } |
| 905 | break; |
| 906 | case CHANNEL_SATURATIONcxx . . $ CHANNEL_SATURATION.: |
| 907 | while (len--) { |
| 908 | rf = *src++; |
| 909 | gf = *src++; |
| 910 | bf = *src++; |
| 911 | RGBtoHSVcxx . . $ RGBtoHSV(4eeb89725be9c4c3).(rf, gf, bf, &hue, &sat, &val); |
| 912 | HSVtoRGBcxx . . $ HSVtoRGB(f38ff659fc243f5b).(&rf, &gf, &bf, hue, map[(int)(sat * 255)], val); |
| 913 | *dst++ = rf * 255; |
| 914 | *dst++ = gf * 255; |
| 915 | *dst++ = bf * 255; |
| 916 | *dst++ = *src++; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | |
| 921 | if (inst->drawCurvescxx . . $ curves_instance#drawCurves. && !strlencxx . . $ strlen(751346f8406fd082).(inst->bsplinecxx . . $ curves_instance#bspline.)) { |
| 922 | unsigned char color[] = {0, 0, 0}; |
| 923 | if (inst->channelcxx . . $ curves_instance#channel. == CHANNEL_REDcxx . . $ CHANNEL_RED. || inst->channelcxx . . $ curves_instance#channel. == CHANNEL_GREENcxx . . $ CHANNEL_GREEN. || inst->channelcxx . . $ curves_instance#channel. == CHANNEL_BLUEcxx . . $ CHANNEL_BLUE.) |
| 924 | color[(int)inst->channelcxx . . $ curves_instance#channel.] = 255; |
| 925 | //calculating graph offset by given position values |
| 926 | int graphXOffset = inst->curvesPositioncxx . . $ curves_instance#curvesPosition. == POS_TOP_LEFTcxx . . $ `src/filter/curves/curves.c:36:9`! || inst->curvesPositioncxx . . $ curves_instance#curvesPosition. == POS_BOTTOM_LEFTcxx . . $ `src/filter/curves/curves.c:38:9`!?0:inst->widthcxx . . $ curves_instance#width. - scale; |
| 927 | int graphYOffset = inst->curvesPositioncxx . . $ curves_instance#curvesPosition. == POS_TOP_LEFTcxx . . $ `src/filter/curves/curves.c:36:9`! || inst->curvesPositioncxx . . $ curves_instance#curvesPosition. == POS_TOP_RIGHTcxx . . $ `src/filter/curves/curves.c:37:9`!?0:inst->heightcxx . . $ curves_instance#height. - scale; |
| 928 | int maxYvalue = scale - 1; |
| 929 | int stride = inst->widthcxx . . $ curves_instance#width.; |
| 930 | dst = (unsigned char*)outframe; |
| 931 | float lineWidth = scale / 254.; |
| 932 | int cellSize = floor(lineWidth * 32); |
| 933 | //filling up background and drawing grid |
| 934 | for(i = 0; i < scale; i++) { |
| 935 | if (i % cellSize > lineWidth) //point doesn't aly on the grid |
| 936 | for(int j = 0; j < scale; j++) { |
| 937 | if (j % cellSize > lineWidth) { //point doesn't aly on the grid |
| 938 | int offset = ((maxYvalue - i + graphYOffset) * stride + j + graphXOffset) * 4; |
| 939 | dst[offset] = (dst[offset] >> 1) + 0x7F; |
| 940 | offset++; |
| 941 | dst[offset] = (dst[offset] >> 1) + 0x7F; |
| 942 | offset++; |
| 943 | dst[offset] = (dst[offset] >> 1) + 0x7F; |
| 944 | offset++; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | float doubleLineWidth = 4 * lineWidth; |
| 949 | //drawing points on the graph |
| 950 | for(i = 0; i < inst->pointNumbercxx . . $ curves_instance#pointNumber.; i++) { |
| 951 | int pointOffset = i * 2; |
| 952 | int xPoint = points[pointOffset++] * maxYvalue; |
| 953 | int yPoint = points[pointOffset] * maxYvalue; |
| 954 | for(int x = (int)floor(xPoint - doubleLineWidth); x <= xPoint + doubleLineWidth; x++) { |
| 955 | if (x >= 0 && x < scale) { |
| 956 | for(int y = (int)floor(yPoint - doubleLineWidth); y <= yPoint + doubleLineWidth; y++) { |
| 957 | if (y >= 0 && y < scale) { |
| 958 | int offset = ((maxYvalue - y + graphYOffset) * stride + x + graphXOffset) * 4; |
| 959 | dst[offset++] = color[0]; |
| 960 | dst[offset++] = color[1]; |
| 961 | dst[offset++] = color[2]; |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | freecxx . . $ free(5d22bdacc48458e8).(points); |
| 968 | //drawing curve on the graph |
| 969 | float halfLineWidth = lineWidth * .5; |
| 970 | float prevY = 0; |
| 971 | for(int j = 0; j < scale; j++) { |
| 972 | float y = inst->curveMapcxx . . $ curves_instance#curveMap.[j]; |
| 973 | if (j == 0 || y == prevY) { |
| 974 | for(i = (int)floor(y - halfLineWidth); i <= ceil(y + halfLineWidth); i++) { |
| 975 | int clampedI = i < 0?0:i >= scale?scale - 1:i; |
| 976 | int offset = ((maxYvalue - clampedI + graphYOffset) * stride + j + graphXOffset) * 4; |
| 977 | dst[offset++] = color[0]; |
| 978 | dst[offset++] = color[1]; |
| 979 | dst[offset++] = color[2]; |
| 980 | } |
| 981 | } else { |
| 982 | int factor = prevY > y?-1:1; |
| 983 | float gap = halfLineWidth * factor; |
| 984 | //medium value between previous value and current value |
| 985 | float mid = (y - prevY) * .5 + prevY; |
| 986 | //drawing line from previous value to mid point |
| 987 | for(i = ROUNDcxx . . $ `include/frei0r/math.h:53:9`!(prevY - gap); factor * i < factor * (mid + gap); i += factor) { |
| 988 | int clampedI = i < 0?0:i >= scale?scale - 1:i; |
| 989 | int offset = ((maxYvalue - clampedI + graphYOffset) * stride + j - 1 + graphXOffset) * 4; |
| 990 | dst[offset++] = color[0]; |
| 991 | dst[offset++] = color[1]; |
| 992 | dst[offset++] = color[2]; |
| 993 | } |
| 994 | //drawing line from mid point to current value |
| 995 | for(i = ROUNDcxx . . $ `include/frei0r/math.h:53:9`!(mid - gap); factor * i < factor * ceil(y + gap); i += factor) { |
| 996 | int clampedI = i < 0?0:i >= scale?scale - 1:i; |
| 997 | int offset = ((maxYvalue - clampedI + graphYOffset) * stride + j + graphXOffset) * 4; |
| 998 | dst[offset++] = color[0]; |
| 999 | dst[offset++] = color[1]; |
| 1000 | dst[offset++] = color[2]; |
| 1001 | } |
| 1002 | } |
| 1003 | prevY = y; |
| 1004 | } |
| 1005 | } |
| 1006 | } |