| 1 | /* contrast0r.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 | #include <stdio.h> |
| 23 | |
| 24 | #include "frei0r.h" |
| 25 | #include "frei0r/math.h" |
| 26 | |
| 27 | typedef struct contrast0r_instance |
| 28 | { |
| 29 | unsigned int width; |
| 30 | unsigned int height; |
| 31 | int contrast; /* the contrast [-256, 256] */ |
| 32 | unsigned char lut[256]; /* look-up table */ |
| 33 | } contrast0r_instance_t; |
| 34 | |
| 35 | /* Updates the look-up-table. */ |
| 36 | void update_lut(contrast0r_instance_t *inst) |
| 37 | { |
| 38 | int i; |
| 39 | unsigned char *lut = inst->lut; |
| 40 | int contrast = inst->contrast; |
| 41 | for (i=0; i<128; ++i) |
| 42 | lut[i] = CLAMP0255(i - (((128 - i)*contrast)>>8)); |
| 43 | for (i=128; i<256; ++i) |
| 44 | lut[i] = CLAMP0255(i + (((i - 128)*contrast)>>8)); |
| 45 | } |
| 46 | |
| 47 | int f0r_init() |
| 48 | { |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | void f0r_deinit() |
| 53 | { /* no initialization required */ } |
| 54 | |
| 55 | void f0r_get_plugin_info(f0r_plugin_info_t* contrast0r_info) |
| 56 | { |
| 57 | contrast0r_info->name = "Contrast0r"; |
| 58 | contrast0r_info->author = "Jean-Sebastien Senecal"; |
| 59 | contrast0r_info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 60 | contrast0r_info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 61 | contrast0r_info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 62 | contrast0r_info->major_version = 0; |
| 63 | contrast0r_info->minor_version = 2; |
| 64 | contrast0r_info->num_params = 1; |
| 65 | contrast0r_info->explanation = "Adjusts the contrast of a source image"; |
| 66 | } |
| 67 | |
| 68 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 69 | { |
| 70 | switch(param_index) |
| 71 | { |
| 72 | case 0: |
| 73 | info->name = "Contrast"; |
| 74 | info->type = F0R_PARAM_DOUBLE; |
| 75 | info->explanation = "The contrast value"; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 81 | { |
| 82 | contrast0r_instance_t* inst = (contrast0r_instance_t*)calloc(1, sizeof(*inst)); |
| 83 | inst->width = width; inst->height = height; |
| 84 | /* init look-up-table */ |
| 85 | update_lut(inst); |
| 86 | return (f0r_instance_t)inst; |
| 87 | } |
| 88 | |
| 89 | void f0r_destruct(f0r_instance_t instance) |
| 90 | { |
| 91 | free(instance); |
| 92 | } |
| 93 | |
| 94 | void f0r_set_param_value(f0r_instance_t instance, |
| 95 | f0r_param_t param, int param_index) |
| 96 | { |
| 97 | assert(instance); |
| 98 | contrast0r_instance_t* inst = (contrast0r_instance_t*)instance; |
| 99 | switch(param_index) |
| 100 | { |
| 101 | int val; |
| 102 | case 0: |
| 103 | /* contrast */ |
| 104 | val = (int) ((*((double*)param) - 0.5) * 512.0); /* remap to [-256, 256] */ |
| 105 | if (val != inst->contrast) |
| 106 | { |
| 107 | inst->contrast = val; |
| 108 | update_lut(inst); |
| 109 | } |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void f0r_get_param_value(f0r_instance_t instance, |
| 115 | f0r_param_t param, int param_index) |
| 116 | { |
| 117 | assert(instance); |
| 118 | contrast0r_instance_t* inst = (contrast0r_instance_t*)instance; |
| 119 | |
| 120 | switch(param_index) |
| 121 | { |
| 122 | case 0: |
| 123 | *((double*)param) = (double) ( (inst->contrast + 256.0) / 512.0 ); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void f0r_update(f0r_instance_t instance, double time, |
| 129 | const uint32_t* inframe, uint32_t* outframe) |
| 130 | { |
| 131 | assert(instance); |
| 132 | contrast0r_instance_t* inst = (contrast0r_instance_t*)instance; |
| 133 | unsigned int len = inst->width * inst->height; |
| 134 | |
| 135 | unsigned char* lut = inst->lut; |
| 136 | unsigned char* dst = (unsigned char*)outframe; |
| 137 | const unsigned char* src = (unsigned char*)inframe; |
| 138 | while (len--) |
| 139 | { |
| 140 | *dst++ = lut[*src++]; |
| 141 | *dst++ = lut[*src++]; |
| 142 | *dst++ = lut[*src++]; |
| 143 | *dst++ = *src++; // copy alpha |
| 144 | } |
| 145 | } |
| 146 |