| 1 | /* saturat0r.c |
| 2 | * Copyright (C) 2004 Jean-Sebastien Senecal ([email protected]) |
| 3 | * This file is a Frei0r plugin. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (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 General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <assert.h> |
| 22 | |
| 23 | #include "frei0r.h" |
| 24 | #include "frei0r/math.h" |
| 25 | |
| 26 | #define MAX_SATURATION 8.0 |
| 27 | |
| 28 | typedef struct saturat0r_instance |
| 29 | { |
| 30 | unsigned int width; |
| 31 | unsigned int height; |
| 32 | double saturation; /* the saturation value [0, 1] */ |
| 33 | } saturat0r_instance_t; |
| 34 | |
| 35 | int f0r_init() |
| 36 | { |
| 37 | return 1; |
| 38 | } |
| 39 | |
| 40 | void f0r_deinit() |
| 41 | { /* no initialization required */ } |
| 42 | |
| 43 | void f0r_get_plugin_info(f0r_plugin_info_t* saturat0r_info) |
| 44 | { |
| 45 | saturat0r_info->name = "Saturat0r"; |
| 46 | saturat0r_info->author = "Jean-Sebastien Senecal"; |
| 47 | saturat0r_info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 48 | saturat0r_info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 49 | saturat0r_info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 50 | saturat0r_info->major_version = 0; |
| 51 | saturat0r_info->minor_version = 3; |
| 52 | saturat0r_info->num_params = 1; |
| 53 | saturat0r_info->explanation = "Adjusts the saturation of a source image"; |
| 54 | } |
| 55 | |
| 56 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 57 | { |
| 58 | switch(param_index) |
| 59 | { |
| 60 | case 0: |
| 61 | info->name = "Saturation"; |
| 62 | info->type = F0R_PARAM_DOUBLE; |
| 63 | info->explanation = "The saturation value"; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 69 | { |
| 70 | saturat0r_instance_t* inst = (saturat0r_instance_t*)calloc(1, sizeof(*inst)); |
| 71 | inst->width = width; inst->height = height; |
| 72 | inst->saturation=1.0/MAX_SATURATION; |
| 73 | return (f0r_instance_t)inst; |
| 74 | } |
| 75 | |
| 76 | void f0r_destruct(f0r_instance_t instance) |
| 77 | { |
| 78 | free(instance); |
| 79 | } |
| 80 | |
| 81 | void f0r_set_param_value(f0r_instance_t instance, |
| 82 | f0r_param_t param, int param_index) |
| 83 | { |
| 84 | assert(instance); |
| 85 | saturat0r_instance_t* inst = (saturat0r_instance_t*)instance; |
| 86 | |
| 87 | switch(param_index) |
| 88 | { |
| 89 | case 0: |
| 90 | /* saturations */ |
| 91 | inst->saturation = *((double*)param); |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void f0r_get_param_value(f0r_instance_t instance, |
| 97 | f0r_param_t param, int param_index) |
| 98 | { |
| 99 | assert(instance); |
| 100 | saturat0r_instance_t* inst = (saturat0r_instance_t*)instance; |
| 101 | |
| 102 | switch(param_index) |
| 103 | { |
| 104 | case 0: |
| 105 | *((double*)param) = inst->saturation; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void f0r_update(f0r_instance_t instance, double time, |
| 111 | const uint32_t* inframe, uint32_t* outframe) |
| 112 | { |
| 113 | assert(instance); |
| 114 | saturat0r_instance_t* inst = (saturat0r_instance_t*)instance; |
| 115 | unsigned int len = inst->width * inst->height; |
| 116 | double saturation = inst->saturation * MAX_SATURATION; |
| 117 | |
| 118 | unsigned char* dst = (unsigned char*)outframe; |
| 119 | const unsigned char* src = (unsigned char*)inframe; |
| 120 | |
| 121 | double one_minus_saturation = 1.0-saturation; |
| 122 | int bwgt = (int)(7471.0 * one_minus_saturation); |
| 123 | int gwgt = (int)(38470.0 * one_minus_saturation); |
| 124 | int rwgt = (int)(19595.0 * one_minus_saturation); |
| 125 | |
| 126 | int b, g, r, bw; |
| 127 | |
| 128 | if (0 <= saturation && saturation <=1) // optimisation: no clamping needed |
| 129 | { |
| 130 | while (len--) |
| 131 | { |
| 132 | b = *src++; |
| 133 | g = *src++; |
| 134 | r = *src++; |
| 135 | |
| 136 | bw = (b*bwgt + g*gwgt + r*rwgt) >> 16; |
| 137 | |
| 138 | *dst++ = (unsigned char) (bw + b*saturation); |
| 139 | *dst++ = (unsigned char) (bw + g*saturation); |
| 140 | *dst++ = (unsigned char) (bw + r*saturation); |
| 141 | |
| 142 | *dst++ = *src++; // copy alpha |
| 143 | } |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | while (len--) |
| 148 | { |
| 149 | b = *src++; |
| 150 | g = *src++; |
| 151 | r = *src++; |
| 152 | |
| 153 | bw = (b*bwgt + g*gwgt + r*rwgt) >> 16; |
| 154 | |
| 155 | *dst++ = CLAMP0255((int) (bw + b*saturation)); |
| 156 | *dst++ = CLAMP0255((int) (bw + g*saturation)); |
| 157 | *dst++ = CLAMP0255((int) (bw + r*saturation)); |
| 158 | |
| 159 | *dst++ = *src++; // copy alpha |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 |