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