| 1 | /* |
| 2 | * This file is a port of com.jhlabs.image.PosterizeFilter.java |
| 3 | * Copyright 2006 Jerry Huxtable |
| 4 | * |
| 5 | * posterize.c |
| 6 | * Copyright 2012 Janne Liljeblad |
| 7 | * |
| 8 | * This file is a Frei0r plugin. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "frei0r.h" |
| 29 | #include "frei0r/math.h" |
| 30 | |
| 31 | typedef struct posterize_instance |
| 32 | { |
| 33 | unsigned int width; |
| 34 | unsigned int height; |
| 35 | double levels; |
| 36 | } posterize_instance_t; |
| 37 | |
| 38 | int f0r_init() |
| 39 | { |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | void f0r_deinit() |
| 44 | { /* no initialization required */ } |
| 45 | |
| 46 | void f0r_get_plugin_info(f0r_plugin_info_t* posterize_info) |
| 47 | { |
| 48 | posterize_info->name = "posterize"; |
| 49 | posterize_info->author = "Janne Liljeblad"; |
| 50 | posterize_info->plugin_type = F0R_PLUGIN_TYPE_FILTER; |
| 51 | posterize_info->color_model = F0R_COLOR_MODEL_RGBA8888; |
| 52 | posterize_info->frei0r_version = FREI0R_MAJOR_VERSION; |
| 53 | posterize_info->major_version = 0; |
| 54 | posterize_info->minor_version = 1; |
| 55 | posterize_info->num_params = 1; |
| 56 | posterize_info->explanation = "Posterizes image by reducing the number of colors used in image"; |
| 57 | } |
| 58 | |
| 59 | void f0r_get_param_info(f0r_param_info_t* info, int param_index) |
| 60 | { |
| 61 | switch(param_index) |
| 62 | { |
| 63 | case 0: |
| 64 | info->name = "levels"; |
| 65 | info->type = F0R_PARAM_DOUBLE; |
| 66 | info->explanation = "Number of values per channel"; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | f0r_instance_t f0r_construct(unsigned int width, unsigned int height) |
| 72 | { |
| 73 | posterize_instance_t* inst = (posterize_instance_t*)calloc(1, sizeof(*inst)); |
| 74 | inst->width = width; |
| 75 | inst->height = height; |
| 76 | inst->levels = 5.0 / 48.0;// input range 0 - 1 will be interpreted as levels range 2 - 50 |
| 77 | return (f0r_instance_t)inst; |
| 78 | } |
| 79 | |
| 80 | void f0r_destruct(f0r_instance_t instance) |
| 81 | { |
| 82 | free(instance); |
| 83 | } |
| 84 | |
| 85 | void f0r_set_param_value(f0r_instance_t instance, |
| 86 | f0r_param_t param, int param_index) |
| 87 | { |
| 88 | assert(instance); |
| 89 | posterize_instance_t* inst = (posterize_instance_t*)instance; |
| 90 | |
| 91 | switch(param_index) |
| 92 | { |
| 93 | case 0: |
| 94 | inst->levels = *((double*)param); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void f0r_get_param_value(f0r_instance_t instance, |
| 100 | f0r_param_t param, int param_index) |
| 101 | { |
| 102 | assert(instance); |
| 103 | posterize_instance_t* inst = (posterize_instance_t*)instance; |
| 104 | |
| 105 | switch(param_index) |
| 106 | { |
| 107 | case 0: |
| 108 | *((double*)param) = inst->levels; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void f0r_update(f0r_instance_t instance, double time, |
| 114 | const uint32_t* inframe, uint32_t* outframe) |
| 115 | { |
| 116 | assert(instance); |
| 117 | posterize_instance_t* inst = (posterize_instance_t*)instance; |
| 118 | unsigned int len = inst->width * inst->height; |
| 119 | |
| 120 | // convert input value 0.0-1.0 to int value 2-50 |
| 121 | double levelsInput = inst->levels * 48.0; |
| 122 | levelsInput = CLAMP(levelsInput, 0.0, 48.0) + 2.0; |
| 123 | int numLevels = (int)levelsInput; |
| 124 | |
| 125 | // Prevent division by zero |
| 126 | if (numLevels < 2) numLevels = 2; |
| 127 | |
| 128 | // create levels table |
| 129 | unsigned char levels[256]; |
| 130 | int i; |
| 131 | for (i = 0; i < 256; i++) |
| 132 | { |
| 133 | levels[i] = 255 * (numLevels*i / 256) / (numLevels-1); |
| 134 | } |
| 135 | |
| 136 | unsigned char* dst = (unsigned char*)outframe; |
| 137 | const unsigned char* src = (unsigned char*)inframe; |
| 138 | unsigned char r,g,b = 0; |
| 139 | while (len--) |
| 140 | { |
| 141 | r = *src++; |
| 142 | g = *src++; |
| 143 | b = *src++; |
| 144 | |
| 145 | r = levels[r]; |
| 146 | g = levels[g]; |
| 147 | b = levels[b]; |
| 148 | |
| 149 | *dst++ = r; |
| 150 | *dst++ = g; |
| 151 | *dst++ = b; |
| 152 | *dst++ = *src++;//copy alpha |
| 153 | } |
| 154 | } |
| 155 |