-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsv_c.c
More file actions
431 lines (391 loc) · 10.2 KB
/
hsv_c.c
File metadata and controls
431 lines (391 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* C implementation */
/* gcc -02 -fomit-frame-pointer -o vector vector.c */
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <float.h>
#include <assert.h>
#include <time.h>
// From a given set of RGB values, determines min and max values.
double fmax_rgb_value(double red, double green, double blue);
double fmin_rgb_value(double red, double green, double blue);
// Convert RGB color model into HSV and reciprocally
// METHOD 1
double * rgb_to_hsv(double r, double g, double b);
double * hsv_to_rgb(double h, double s, double v);
// METHOD 2
struct rgb struct_hsv_to_rgb(double h, double s, double v);
struct hsv struct_rgb_to_hsv(double r, double g, double b);
#define ONE_255 1.0/255.0
#define ONE_360 1.0/360.0
#define cmax(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
struct hsv{
double h; // hue
double s; // saturation
double v; // value
};
struct rgb{
double r;
double g;
double b;
};
// All inputs have to be double precision (python float) in range [0.0 ... 255.0]
// Output: return the maximum value from given RGB values (double precision).
inline double fmax_rgb_value(double red, double green, double blue)
{
if (red>green){
if (red>blue) {
return red;
}
else {
return blue;
}
}
else if (green>blue){
return green;
}
else {
return blue;
}
}
// All inputs have to be double precision (python float) in range [0.0 ... 255.0]
// Output: return the minimum value from given RGB values (double precision).
inline double fmin_rgb_value(double red, double green, double blue)
{
if (red<green){
if (red<blue){
return red;
}
else{
return blue;
}
}
else if (green<blue){
return green;
}
else{
return blue;
}
}
// Convert RGB color model into HSV model (Hue, Saturation, Value)
// all colors inputs have to be double precision (RGB normalized values),
// (python float) in range [0.0 ... 1.0]
// outputs is a C array containing 3 values, HSV (double precision)
// to convert in % do the following:
// h = h * 360.0
// s = s * 100.0
// v = v * 100.0
inline double * rgb_to_hsv(double r, double g, double b)
{
// check if all inputs are normalized
assert ((0.0<=r) <= 1.0);
assert ((0.0<=g) <= 1.0);
assert ((0.0<=b) <= 1.0);
double mx, mn;
double h, df, s, v, df_;
double *hsv = malloc (sizeof (double) * 3);
// Check if the memory has been successfully
// allocated by malloc or not
if (hsv == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
mx = fmax_rgb_value(r, g, b);
mn = fmin_rgb_value(r, g, b);
df = mx-mn;
df_ = 1.0/df;
if (mx == mn)
{
h = 0.0;}
// The conversion to (int) approximate the final result
else if (mx == r){
h = fmod(60.0 * ((g-b) * df_) + 360.0, 360);
}
else if (mx == g){
h = fmod(60.0 * ((b-r) * df_) + 120.0, 360);
}
else if (mx == b){
h = fmod(60.0 * ((r-g) * df_) + 240.0, 360);
}
if (mx == 0){
s = 0.0;
}
else{
s = df/mx;
}
v = mx;
hsv[0] = h * ONE_360;
hsv[1] = s;
hsv[2] = v;
return hsv;
}
// Convert HSV color model into RGB (red, green, blue)
// all inputs have to be double precision, (python float) in range [0.0 ... 1.0]
// outputs is a C array containing RGB values (double precision) normalized.
// to convert for a pixel colors
// r = r * 255.0
// g = g * 255.0
// b = b * 255.0
inline double * hsv_to_rgb(double h, double s, double v)
{
// check if all inputs are normalized
assert ((0.0<= h) <= 1.0);
assert ((0.0<= s) <= 1.0);
assert ((0.0<= v) <= 1.0);
int i;
double f, p, q, t;
double *rgb = malloc (sizeof (double) * 3);
// Check if the memory has been successfully
// allocated by malloc or not
if (rgb == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
if (s == 0.0){
rgb[0] = v;
rgb[1] = v;
rgb[2] = v;
return rgb;
}
i = (int)(h*6.0);
f = (h*6.0) - i;
p = v*(1.0 - s);
q = v*(1.0 - s*f);
t = v*(1.0 - s*(1.0-f));
i = i%6;
if (i == 0){
rgb[0] = v;
rgb[1] = t;
rgb[2] = p;
return rgb;
}
else if (i == 1){
rgb[0] = q;
rgb[1] = v;
rgb[2] = p;
return rgb;
}
else if (i == 2){
rgb[0] = p;
rgb[1] = v;
rgb[2] = t;
return rgb;
}
else if (i == 3){
rgb[0] = p;
rgb[1] = q;
rgb[2] = v;
return rgb;
}
else if (i == 4){
rgb[0] = t;
rgb[1] = p;
rgb[2] = v;
return rgb;
}
else if (i == 5){
rgb[0] = v;
rgb[1] = p;
rgb[2] = q;
return rgb;
}
return rgb;
}
/*
METHOD 2
Return a structure instead of pointers
// outputs is a C structure containing 3 values, HSV (double precision)
// to convert in % do the following:
// h = h * 360.0
// s = s * 100.0
// v = v * 100.0
*/
inline struct hsv struct_rgb_to_hsv(double r, double g, double b)
{
// check if all inputs are normalized
assert ((0.0<=r) <= 1.0);
assert ((0.0<=g) <= 1.0);
assert ((0.0<=b) <= 1.0);
double mx, mn;
double h, df, s, v, df_;
struct hsv hsv_;
mx = fmax_rgb_value(r, g, b);
mn = fmin_rgb_value(r, g, b);
df = mx-mn;
df_ = 1.0/df;
if (mx == mn)
{
h = 0.0;}
// The conversion to (int) approximate the final result
else if (mx == r){
h = fmod(60.0 * ((g-b) * df_) + 360.0, 360);
}
else if (mx == g){
h = fmod(60.0 * ((b-r) * df_) + 120.0, 360);
}
else if (mx == b){
h = fmod(60.0 * ((r-g) * df_) + 240.0, 360);
}
if (mx == 0){
s = 0.0;
}
else{
s = df/mx;
}
v = mx;
hsv_.h = h * ONE_360;
hsv_.s = s;
hsv_.v = v;
return hsv_;
}
// Convert HSV color model into RGB (red, green, blue)
// all inputs have to be double precision, (python float) in range [0.0 ... 1.0]
// outputs is a C structure containing RGB values (double precision) normalized.
// to convert for a pixel colors
// r = r * 255.0
// g = g * 255.0
// b = b * 255.0
inline struct rgb struct_hsv_to_rgb(double h, double s, double v)
{
// check if all inputs are normalized
assert ((0.0<= h) <= 1.0);
assert ((0.0<= s) <= 1.0);
assert ((0.0<= v) <= 1.0);
int i;
double f, p, q, t;
struct rgb rgb_={.r=0.0, .g=0.0, .b=0.0};
if (s == 0.0){
rgb_.r = v;
rgb_.g = v;
rgb_.b = v;
return rgb_;
}
i = (int)(h*6.0);
f = (h*6.0) - i;
p = v*(1.0 - s);
q = v*(1.0 - s*f);
t = v*(1.0 - s*(1.0-f));
i = i%6;
if (i == 0){
rgb_.r = v;
rgb_.g = t;
rgb_.b = p;
return rgb_;
}
else if (i == 1){
rgb_.r = q;
rgb_.g = v;
rgb_.b = p;
return rgb_;
}
else if (i == 2){
rgb_.r = p;
rgb_.g = v;
rgb_.b = t;
return rgb_;
}
else if (i == 3){
rgb_.r = p;
rgb_.g = q;
rgb_.b = v;
return rgb_;
}
else if (i == 4){
rgb_.r = t;
rgb_.g = p;
rgb_.b = v;
return rgb_;
}
else if (i == 5){
rgb_.r = v;
rgb_.g = p;
rgb_.b = q;
return rgb_;
}
return rgb_;
}
//
//
//int main ()
//{
//double *ar;
//double *ar1;
//int i, j, k;
//double r, g, b;
//double h, s, v;
//
//int n = 1000000;
//double *ptr;
//clock_t begin = clock();
//struct hsv hsv_;
//struct rgb rgb_;
//
///* here, do your time-consuming job */
//for (i=0; i<=n; ++i){
// ptr = rgb_to_hsv(25.0/255.0, 60.0/255.0, 128.0/255.0);
// printf("\nHSV1 : %f %f %f ", ptr[0], ptr[1], ptr[2]);
// hsv_ = struct_rgb_to_hsv(25.0/255.0, 60.0/255.0, 128.0/255.0);
// printf("\nHSV2 : %f %f %f ", hsv_.h, hsv_.s, hsv_.v);
// rgb_ = struct_hsv_to_rgb(hsv_.h, hsv_.s, hsv_.v);
// printf("\nHSV3 : %f %f %f ", rgb_.r, rgb_.g, rgb_.b);
//
//}
//
//clock_t end = clock();
//double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
//printf("\ntotal time %f :", time_spent);
//
//printf("\nTesting algorithm(s).");
//n = 0;
//for (i=0; i<256; i++){
// for (j=0; j<256; j++){
// for (k=0; k<256; k++){
// ar = rgb_to_hsv((double)i/255, (double)j/255, (double)k/255);
// h=ar[0];
// s=ar[1];
// v=ar[2];
// free(ar);
// ar1 = hsv_to_rgb(h, s, v);
// r = round(ar1[0] * 255.0);
// g = round(ar1[1] * 255.0);
// b = round(ar1[2] * 255.0);
// free(ar1);
// // printf("\n\nRGB VALUES:R:%i G:%i B:%i ", i, j, k);
// // printf("\nRGB VALUES:R:%f G:%f B:%f ", r, g, b);
// // printf("\n %f, %f, %f ", h, s, v);
//
// if (abs(i - r) > 0.1) {
// printf("\n\nRGB VALUES:R:%i G:%i B:%i ", i, j, k);
// printf("\nRGB VALUES:R:%f G:%f B:%f ", r, g, b);
// printf("\n %f, %f, %f ", h, s, v);
// n+=1;
// return -1;
// }
// if (abs(j - g) > 0.1){
// printf("\n\nRGB VALUES:R:%i G:%i B:%i ", i, j, k);
// printf("\nRGB VALUES:R:%f G:%f B:%f ", r, g, b);
// printf("\n %f, %f, %f ", h, s, v);
// n+=1;
// return -1;
// }
//
// if (abs(k - b) > 0.1){
// printf("\n\nRGB VALUES:R:%i G:%i B:%i ", i, j, k);
// printf("\nRGB VALUES:R:%f G:%f B:%f ", r, g, b);
// printf("\n %f, %f, %f ", h, s, v);
// n+=1;
// return -1;
//
// }
// }
// }
//}
//printf("\nError(s) found. %i ", n);
//
//return 0;
//}