| 1 | /* |
| 2 | * This file used to contain sigmoidal transfer function from file plug-ins/common/softglow.c in gimp. |
| 3 | * However, it is now modified to match ImageMagick; i.e. whereas only the steepness of the sigmoidal |
| 4 | * curves was tunable earlier, now the midpoint of the curve is adjustable as well -- both to a degree. |
| 5 | * |
| 6 | * sigmoidaltransfer.c |
| 7 | * Copyright 2012 Janne Liljeblad, 2025 Cynthia |
| 8 | * |
| 9 | * This file is a Frei0r plugin. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <assert.h> |
| 28 | #include <stdio.h> |
| 29 | #include <math.h> |
| 30 | #include "frei0r.h" |
| 31 | #include "frei0r/math.h" |
| 32 | |
| 33 | typedef struct sigmoidal_instance |
| 34 | { |
| 35 | unsigned int width; |
| 36 | unsigned int height; |
| 37 | double base; |
| 38 | double sharpness; |
| 39 | |
| 40 | /* Precomputed values of the (scaled and shifted) sigmoid function |
| 41 | is stored in this lookup table. */ |
| 42 | uint8_t lut[256]; |
| 43 | } sigmoidal_instance_t; |
| 44 | |
| 45 | void gen_sigmoid_lut (uint8_t *const lut, const float base, const float sharpness) |
| 46 | { |
| 47 | float k = expf(sharpness * 5.0) / 255.0; |
| 48 | float b = (base - 0.5) * 63.0; |
| 49 | |
| 50 | for (int i = 0; i < 256; ++i) |
| 51 | lut[i] = CLAMP (255.0 / (1.0 + expf(-k * (i - b - 127.0))), 0, 255.0); |
| 52 | } |
| 53 | |
| 54 | void sigmoidal_transfer(f0r_instance_t instance, double time, |
| 55 | const uint32_t* inframe, uint32_t* outframe) |
| 56 | { |
| 57 | assert(instance); |
| 58 | sigmoidal_instance_t* inst = (sigmoidal_instance_t*)instance; |
| 59 | unsigned int len = inst->width * inst->height; |
| 60 | |
| 61 | const unsigned char* src = (unsigned char*)inframe; |
| 62 | unsigned char* dst = (unsigned char*)outframe; |
| 63 | |
| 64 | unsigned char luma, r, g, b; |
| 65 | double val; |
| 66 | while (len--) |
| 67 | { |
| 68 | r = *src++; |
| 69 | g = *src++; |
| 70 | b = *src++; |
| 71 | |
| 72 | //desaturate |
| 73 | luma = (unsigned char)(0.299 * r + 0.587 * g + 0.114 * b); |
| 74 | //compute sigmoidal transfer |
| 75 | luma = inst->lut[luma]; |
| 76 | |
| 77 | *dst++ = luma; |
| 78 | *dst++ = luma; |
| 79 | *dst++ = luma; |
| 80 | *dst++ = *src++; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | int f0r_init() |
| 86 | { |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | void f0r_deinit() |
| 91 | { /* no initialization required */ } |
| 92 | |
| 93 | void f0r_get_plugin_info(f0r_plugin_info_t* sigmoidalInfo) |
| 94 | { |
| 95 | sigmoidalInfo->name = "sigmoidaltransfer"; |
| 96 | sigmoidalInfo->author = "Janne Liljeblad & Cynthia"; |
| 97 | sigmoidalInfo->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 98 | sigmoidalInfo->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 99 | sigmoidalInfo->frei0r_version = FREI0R_MAJOR_VERSION; |
| 100 | sigmoidalInfo->major_version = 1; |
| 101 | sigmoidalInfo->minor_version = 0; |
| 102 | sigmoidalInfo->num_params = 2; |
| 103 | sigmoidalInfo->explanation = "Desaturates image and creates a particular look that could be called Stamp, Newspaper or Photocopy"; |
| 104 | } |
| 105 | |
| 106 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 107 | { |
| 108 | switch ( param_index ) { |
| 109 | case 0: |
| 110 | info->name = "base"; |
| 111 | info->type = F0R_PARAM_DOUBLE; |
| 112 | info->explanation = "Brightness of image. Midpoint of sigmoidal curve"; |
| 113 | break; |
| 114 | case 1: |
| 115 | info->name = "sharpness"; |
| 116 | info->type = F0R_PARAM_DOUBLE; |
| 117 | info->explanation = "Sharpness of transfer"; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 123 | { |
| 124 | sigmoidal_instance_t* inst = (sigmoidal_instance_t*)calloc(1, sizeof(*inst)); |
| 125 | inst->width = width; |
| 126 | inst->height = height; |
| 127 | inst->base = 0.5; |
| 128 | inst->sharpness = 3.0 / 5.0; |
| 129 | |
| 130 | gen_sigmoid_lut (inst->lut, inst->base, inst->sharpness); |
| 131 | return (f0r_instance_t)inst; |
| 132 | } |
| 133 | |
| 134 | void f0r_destruct(f0r_instance_t instance) |
| 135 | { |
| 136 | free(instance); |
| 137 | } |
| 138 | |
| 139 | void f0r_set_param_value(f0r_instance_t instance, |
| 140 | f0r_param_t param, int param_index) |
| 141 | { |
| 142 | sigmoidal_instance_t* inst = (sigmoidal_instance_t*)instance; |
| 143 | switch (param_index) |
| 144 | { |
| 145 | case 0: |
| 146 | inst->base = *((double*)param); |
| 147 | break; |
| 148 | case 1: |
| 149 | inst->sharpness = *((double*)param); |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | gen_sigmoid_lut (inst->lut, inst->base, inst->sharpness); |
| 154 | } |
| 155 | |
| 156 | void f0r_get_param_value(f0r_instance_t instance, |
| 157 | f0r_param_t param, int param_index) |
| 158 | { |
| 159 | sigmoidal_instance_t* inst = (sigmoidal_instance_t*)instance; |
| 160 | switch (param_index) |
| 161 | { |
| 162 | case 0: |
| 163 | *((double*)param) = inst->base; |
| 164 | break; |
| 165 | case 1: |
| 166 | *((double*)param) = inst->sharpness; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void f0r_update(f0r_instance_t instance, double time, |
| 172 | const uint32_t* inframe, uint32_t* outframe) |
| 173 | { |
| 174 | sigmoidal_transfer(instance, time, inframe, outframe); |
| 175 | } |
| 176 |