Debian Codegraph @

src/filter/posterize/posterize.c

raw symbols SCIP
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"cxx . . $ `<file>/include/frei0r.h`/
29 #include "frei0r/math.h"cxx . . $ `<file>/include/frei0r/math.h`/
30
31 typedef struct posterize_instancecxx . . $ posterize_instance#
32 {
33 unsigned int widthcxx . . $ posterize_instance#width.;
34 unsigned int heightcxx . . $ posterize_instance#height.;
35 double levelscxx . . $ posterize_instance#levels.;
36 } posterize_instance_tcxx . . $ posterize_instance_t#;
37
38 int f0r_initcxx . . $ f0r_init(b126dc7c1de90089).()
39 {
40 return 1;
41 }
42
43 void f0r_deinitcxx . . $ f0r_deinit(49f6e7a06ebc5aa8).()
44 { /* no initialization required */ }
45
46 void f0r_get_plugin_infocxx . . $ f0r_get_plugin_info(35d753456c0ea59c).(f0r_plugin_info_tcxx . . $ f0r_plugin_info_t#* posterize_info)
47 {
48 posterize_info->namecxx . . $ f0r_plugin_info#name. = "posterize";
49 posterize_info->authorcxx . . $ f0r_plugin_info#author. = "Janne Liljeblad";
50 posterize_info->plugin_typecxx . . $ f0r_plugin_info#plugin_type. = F0R_PLUGIN_TYPE_FILTERcxx . . $ `include/frei0r.h:247:9`!;
51 posterize_info->color_modelcxx . . $ f0r_plugin_info#color_model. = F0R_COLOR_MODEL_RGBA8888cxx . . $ `include/frei0r.h:308:9`!;
52 posterize_info->frei0r_versioncxx . . $ f0r_plugin_info#frei0r_version. = FREI0R_MAJOR_VERSIONcxx . . $ `include/frei0r.h:217:9`!;
53 posterize_info->major_versioncxx . . $ f0r_plugin_info#major_version. = 0;
54 posterize_info->minor_versioncxx . . $ f0r_plugin_info#minor_version. = 1;
55 posterize_info->num_paramscxx . . $ f0r_plugin_info#num_params. = 1;
56 posterize_info->explanationcxx . . $ f0r_plugin_info#explanation. = "Posterizes image by reducing the number of colors used in image";
57 }
58
59 void f0r_get_param_infocxx . . $ f0r_get_param_info(a9f9aa7954cc4a37).(f0r_param_info_tcxx . . $ f0r_param_info_t#* info, int param_index)
60 {
61 switch(param_index)
62 {
63 case 0:
64 info->namecxx . . $ f0r_param_info#name. = "levels";
65 info->typecxx . . $ f0r_param_info#type. = F0R_PARAM_DOUBLEcxx . . $ `include/frei0r.h:383:9`!;
66 info->explanationcxx . . $ f0r_param_info#explanation. = "Number of values per channel";
67 break;
68 }
69 }
70
71 f0r_instance_tcxx . . $ f0r_instance_t# f0r_constructcxx . . $ f0r_construct(da3f471cd007a696).(unsigned int width, unsigned int height)
72 {
73 posterize_instance_tcxx . . $ posterize_instance_t#* inst = (posterize_instance_tcxx . . $ posterize_instance_t#*)calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*inst));
74 inst->widthcxx . . $ posterize_instance#width. = width;
75 inst->heightcxx . . $ posterize_instance#height. = height;
76 inst->levelscxx . . $ posterize_instance#levels. = 5.0 / 48.0;// input range 0 - 1 will be interpreted as levels range 2 - 50
77 return (f0r_instance_tcxx . . $ f0r_instance_t#)inst;
78 }
79
80 void f0r_destructcxx . . $ f0r_destruct(5d22bdacc48458e8).(f0r_instance_tcxx . . $ f0r_instance_t# instance)
81 {
82 freecxx . . $ free(5d22bdacc48458e8).(instance);
83 }
84
85 void f0r_set_param_valuecxx . . $ f0r_set_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance,
86 f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
87 {
88 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
89 posterize_instance_tcxx . . $ posterize_instance_t#* inst = (posterize_instance_tcxx . . $ posterize_instance_t#*)instance;
90
91 switch(param_index)
92 {
93 case 0:
94 inst->levelscxx . . $ posterize_instance#levels. = *((double*)param);
95 break;
96 }
97 }
98
99 void f0r_get_param_valuecxx . . $ f0r_get_param_value(5be095a8278d2bf5).(f0r_instance_tcxx . . $ f0r_instance_t# instance,
100 f0r_param_tcxx . . $ f0r_param_t# param, int param_index)
101 {
102 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
103 posterize_instance_tcxx . . $ posterize_instance_t#* inst = (posterize_instance_tcxx . . $ posterize_instance_t#*)instance;
104
105 switch(param_index)
106 {
107 case 0:
108 *((double*)param) = inst->levelscxx . . $ posterize_instance#levels.;
109 break;
110 }
111 }
112
113 void f0r_updatecxx . . $ f0r_update(96cd7b27f2fca8a7).(f0r_instance_tcxx . . $ f0r_instance_t# instance, double time,
114 const uint32_tcxx . . $ uint32_t#* inframe, uint32_tcxx . . $ uint32_t#* outframe)
115 {
116 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!(instance);
117 posterize_instance_tcxx . . $ posterize_instance_t#* inst = (posterize_instance_tcxx . . $ posterize_instance_t#*)instance;
118 unsigned int len = inst->widthcxx . . $ posterize_instance#width. * inst->heightcxx . . $ posterize_instance#height.;
119
120 // convert input value 0.0-1.0 to int value 2-50
121 double levelsInput = inst->levelscxx . . $ posterize_instance#levels. * 48.0;
122 levelsInput = CLAMPcxx . . $ `include/frei0r/math.h:48:9`!(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