1 | /* |
---|
2 | * libpipi Pathetic image processing interface library |
---|
3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
---|
4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id$ |
---|
8 | * |
---|
9 | * This library is free software. It comes without any warranty, to |
---|
10 | * the extent permitted by applicable law. You can redistribute it |
---|
11 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
12 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | * histogram.c: histogram functions |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | #include "pipi.h" |
---|
27 | #include "pipi_internals.h" |
---|
28 | |
---|
29 | |
---|
30 | pipi_histogram_t* pipi_new_histogram(void) |
---|
31 | { |
---|
32 | return malloc(sizeof(pipi_histogram_t)); |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | int pipi_get_image_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags) |
---|
37 | { |
---|
38 | pipi_pixels_t *p; |
---|
39 | uint8_t *data; |
---|
40 | float n; |
---|
41 | unsigned int max; |
---|
42 | int i; |
---|
43 | |
---|
44 | if(!h) return -1; |
---|
45 | |
---|
46 | p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_C); |
---|
47 | data = (uint8_t *)p->pixels; |
---|
48 | memset(h->a, 0, 256*(sizeof(unsigned int))); |
---|
49 | memset(h->r, 0, 256*(sizeof(unsigned int))); |
---|
50 | memset(h->g, 0, 256*(sizeof(unsigned int))); |
---|
51 | memset(h->b, 0, 256*(sizeof(unsigned int))); |
---|
52 | memset(h->y, 0, 256*(sizeof(unsigned int))); |
---|
53 | |
---|
54 | |
---|
55 | for(i=0; i< img->w*img->h*4; i+=4) |
---|
56 | { |
---|
57 | if(flags&PIPI_COLOR_A) |
---|
58 | h->a[data[i+3]]++; |
---|
59 | if(flags&PIPI_COLOR_R) |
---|
60 | h->r[data[i+2]]++; |
---|
61 | if(flags&PIPI_COLOR_G) |
---|
62 | h->g[data[i+1]]++; |
---|
63 | if(flags&PIPI_COLOR_B) |
---|
64 | h->b[data[i]]++; |
---|
65 | if(flags&PIPI_COLOR_Y) |
---|
66 | { |
---|
67 | uint32_t p = 0.; |
---|
68 | p += 0.299 * data[i]; |
---|
69 | p += 0.587 * data[i+1]; |
---|
70 | p += 0.114 * data[i+2]; |
---|
71 | |
---|
72 | h->y[p>255?255:p]++; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /* Normalize dataset */ |
---|
77 | if(flags&PIPI_COLOR_R) |
---|
78 | { |
---|
79 | max = 0; |
---|
80 | for(i=0; i<256; i++) |
---|
81 | if(h->r[i] > max) max = h->r[i]; |
---|
82 | n = 255.0f / max; |
---|
83 | for(i=0; i<256; i++) |
---|
84 | h->r[i]*=n; |
---|
85 | } |
---|
86 | if(flags&PIPI_COLOR_G) |
---|
87 | { |
---|
88 | max = 0; |
---|
89 | for(i=0; i<256; i++) |
---|
90 | if(h->g[i] > max) max = h->g[i]; |
---|
91 | n = 255.0f / max; |
---|
92 | for(i=0; i<256; i++) |
---|
93 | h->g[i]*=n; |
---|
94 | } |
---|
95 | if(flags&PIPI_COLOR_B) |
---|
96 | { |
---|
97 | max = 0; |
---|
98 | for(i=0; i<256; i++) |
---|
99 | if(h->b[i] > max) max = h->b[i]; |
---|
100 | n = 255.0f / max; |
---|
101 | for(i=0; i<256; i++) |
---|
102 | h->b[i]*=n; |
---|
103 | } |
---|
104 | if(flags&PIPI_COLOR_A) |
---|
105 | { |
---|
106 | max = 0; |
---|
107 | for(i=0; i<256; i++) |
---|
108 | if(h->a[i] > max) max = h->a[i]; |
---|
109 | n = 255.0f / max; |
---|
110 | for(i=0; i<256; i++) |
---|
111 | h->a[i]*=n; |
---|
112 | } |
---|
113 | if(flags&PIPI_COLOR_Y) |
---|
114 | { |
---|
115 | max = 0; |
---|
116 | for(i=0; i<256; i++) |
---|
117 | if(h->y[i] > max) max = h->y[i]; |
---|
118 | n = 255.0f / max; |
---|
119 | for(i=0; i<256; i++) |
---|
120 | h->y[i]*=n; |
---|
121 | } |
---|
122 | |
---|
123 | pipi_release_pixels(img, p); |
---|
124 | |
---|
125 | return 0; |
---|
126 | } |
---|
127 | |
---|
128 | int pipi_render_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags) |
---|
129 | { |
---|
130 | int x; |
---|
131 | |
---|
132 | if(!img || !h) return -1; |
---|
133 | |
---|
134 | for(x=0; x<256; x++) |
---|
135 | { |
---|
136 | if(flags&PIPI_COLOR_R) |
---|
137 | pipi_draw_line(img, |
---|
138 | x, 255, |
---|
139 | x, 255 - h->r[x], |
---|
140 | 0x00FF0000, |
---|
141 | 0); |
---|
142 | if(flags&PIPI_COLOR_G) |
---|
143 | pipi_draw_line(img, |
---|
144 | x, 255, |
---|
145 | x, 255 - h->g[x], |
---|
146 | 0x0000FF00, |
---|
147 | 0); |
---|
148 | if(flags&PIPI_COLOR_B) |
---|
149 | pipi_draw_line(img, |
---|
150 | x, 255, |
---|
151 | x, 255 - h->b[x], |
---|
152 | 0x000000FF, |
---|
153 | 0); |
---|
154 | if(flags&PIPI_COLOR_A) |
---|
155 | pipi_draw_line(img, |
---|
156 | x, 255, |
---|
157 | x, 255 - h->a[x], |
---|
158 | 0x00000FFF, |
---|
159 | 0); |
---|
160 | if(flags&PIPI_COLOR_Y) |
---|
161 | pipi_draw_line(img, |
---|
162 | x, 255, |
---|
163 | x, 255 - h->y[x], |
---|
164 | 0x00FFFFFF, |
---|
165 | 0); |
---|
166 | } |
---|
167 | |
---|
168 | return 0; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | int pipi_free_histogram(pipi_histogram_t* h) |
---|
173 | { |
---|
174 | if(h) free(h); |
---|
175 | else return -1; |
---|
176 | |
---|
177 | return 0; |
---|
178 | } |
---|