about summary refs log tree commit diff
path: root/lib/libppmfuzzy.c
blob: e149b42ab979e517ffdabcd88a03df4145ce4e6b (plain) (blame)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*=============================================================================
  This file contains fuzzy color matching code.

  It is all based on algorithms and methods by Kenan Kalajdzic
  <kenan@unix.ba> in 2006.
=============================================================================*/

#include "pm_c_util.h"
#include "nstring.h"
#include "ppm.h"

typedef double fzLog;
    /* fuzzy logic truth value */

/*----------------------------------------------------------------------------
   Member functions
-----------------------------------------------------------------------------*/

static fzLog
memberS(fzLog const x1,
        fzLog const x2,
        fzLog const x) {

    fzLog retval;

    if (x <= x1)
        retval = 0;
    else if (x > x1 && x <= x2)
        retval = (x - x1) / (x2 - x1);
    else
        retval = 1;

    return retval;
}



static fzLog
memberZ(fzLog const x1,
        fzLog const x2,
        fzLog const x) {

    fzLog retval;

    if (x <= x1)
        retval = 1;
    else if (x > x1 && x <= x2)
        retval = (x2 - x) / (x2 - x1);
    else
        retval = 0;

    return retval;
}



static fzLog
memberTrapez(fzLog const x1,
             fzLog const x2,
             fzLog const x3,
             fzLog const x4,
             fzLog const x) {

    fzLog retval;
    
    if (x <= x1 || x > x4)
        retval = 0;
    else if (x > x1 && x <= x2)
        retval = (x - x1) / (x2 - x1);
    else if (x > x2 && x <= x3)
        retval = 1;
    else
        retval = (x4 - x) / (x4 - x3);

    return retval;
}



/*----------------------------------------------------------------------------
   Hue membership functions
-----------------------------------------------------------------------------*/

static fzLog
hueIsAround000(double const hue) {

    return memberZ(10, 20, hue);
}



static fzLog
hueIsAround015(double const hue) {

    return memberZ(20, 40, hue);
}



static fzLog
hueIsAround030(double const hue) {

    return memberTrapez(10, 20, 40, 60, hue);
}



static fzLog
hueIsAround060(double const hue) {

    return memberTrapez(40, 50, 60, 70, hue);
}



static fzLog
hueIsAround120(double const hue) {

    return memberTrapez(60, 70, 150, 180, hue);
}



static fzLog
hueIsAround180(double const hue) {

    return memberTrapez(150, 180, 240, 260, hue);
}



static fzLog
hueIsAround270(double const hue) {

    return memberTrapez(240, 260, 290, 310, hue);
}



static fzLog
hueIsAround320(double const hue) {

    return memberTrapez(290, 310, 320, 350, hue);
}



static fzLog
hueIsAround360(double const hue) {

    return memberS(320, 350, hue);
}



/*----------------------------------------------------------------------------
   Saturation membership functions
-----------------------------------------------------------------------------*/

static fzLog
satIsVeryLow(double const sat) {

    return memberZ(0.03, 0.08, sat);
}



static fzLog
satIsLow(double const sat) {
    return memberTrapez(0.03, 0.08, 0.17, 0.2, sat);
}



static fzLog
satIsMedium(double const sat) {

    return memberTrapez(0.17, 0.2, 0.6, 0.8, sat);
}



static fzLog
satIsHigh(double const sat) {

    return memberS(0.6, 0.8, sat);
}



/*----------------------------------------------------------------------------
   Value membership functions
-----------------------------------------------------------------------------*/

static fzLog
valIsVeryLow(double const val) {

    return memberZ(0.05, 0.2, val);
}



static fzLog
valIsLow(double const val) {

    return memberTrapez(0.05, 0.2, 0.25, 0.3, val);
}



static fzLog
valIsMedium(double const val) {

    return memberTrapez(0.25, 0.3, 0.6, 0.7, val);
}



static fzLog
valIsHigh(double const val) {

    return memberTrapez(0.6, 0.7, 0.95, 0.97, val);
}



static fzLog
valIsVeryHigh(double const val) {

    return memberS(0.95, 0.97, val);
}



/*----------------------------------------------------------------------------
   Fuzzy logic functions
-----------------------------------------------------------------------------*/

static fzLog
fzAnd(fzLog const opLeft,
      fzLog const opRight) {

    return MIN(opLeft, opRight);
}



static fzLog
fzOr(fzLog const opLeft,
     fzLog const opRight) {

    return MAX(opLeft, opRight);
}



static fzLog
fzNot(fzLog const op) {
    return 1.0 - op;
}



/*----------------------------------------------------------------------------
   Fuzzy color matching
-----------------------------------------------------------------------------*/

static void
matchBk(pixel     const color,
        pixval    const maxval,
        fzLog (* const bkMatchP)[BKCOLOR_COUNT]) {

    struct hsv const hsv = ppm_hsv_from_color(color, maxval);

    fzLog const satVeryLow = satIsVeryLow(hsv.s);
    fzLog const satLow     = satIsLow(hsv.s);
    fzLog const satMedium  = satIsMedium(hsv.s);
    fzLog const satHigh    = satIsHigh(hsv.s);

    fzLog const valVeryLow  = valIsVeryLow(hsv.v);
    fzLog const valLow      = valIsLow(hsv.v);
    fzLog const valMedium   = valIsMedium(hsv.v);
    fzLog const valHigh     = valIsHigh(hsv.v);
    fzLog const valVeryHigh = valIsVeryHigh(hsv.v);

    fzLog const hueAround000 = hueIsAround000(hsv.h);
    fzLog const hueAround015 = hueIsAround015(hsv.h);
    fzLog const hueAround030 = hueIsAround030(hsv.h);
    fzLog const hueAround060 = hueIsAround060(hsv.h);
    fzLog const hueAround120 = hueIsAround120(hsv.h);
    fzLog const hueAround180 = hueIsAround180(hsv.h);
    fzLog const hueAround270 = hueIsAround270(hsv.h);
    fzLog const hueAround320 = hueIsAround320(hsv.h);
    fzLog const hueAround360 = hueIsAround360(hsv.h);

    (*bkMatchP)[BKCOLOR_BLACK]  =
        fzAnd(fzOr(satVeryLow, satLow), fzOr(valVeryLow, valLow));

    (*bkMatchP)[BKCOLOR_GRAY]   =
        fzAnd(satVeryLow, fzAnd(fzNot(valVeryLow), fzNot(valVeryHigh)));

    (*bkMatchP)[BKCOLOR_WHITE]  =
        fzAnd(satVeryLow, valVeryHigh);
    
    (*bkMatchP)[BKCOLOR_RED]    =
        fzAnd(fzAnd(fzOr(hueAround000, hueAround360), fzNot(satVeryLow)),
              fzOr(valMedium, valHigh)
             );

    (*bkMatchP)[BKCOLOR_ORANGE] =
        fzAnd(fzAnd(hueAround030, fzOr(satMedium, satHigh)),
              fzOr(fzOr(valMedium, valHigh), valVeryHigh)
             );

    (*bkMatchP)[BKCOLOR_YELLOW] =
        fzAnd(fzAnd(hueAround060, fzOr(satMedium, satHigh)),
              fzOr(valHigh, valVeryHigh)
             );

    (*bkMatchP)[BKCOLOR_GREEN]  =
        fzAnd(fzAnd(hueAround120, fzOr(satMedium, satHigh)),
              fzAnd(fzNot(valVeryLow), fzNot(valLow))
             );

    (*bkMatchP)[BKCOLOR_BLUE]   =
        fzAnd(fzAnd(hueAround180, fzNot(satVeryLow)),
              fzNot(valVeryLow)
             );

    (*bkMatchP)[BKCOLOR_VIOLET] =
        fzAnd(fzAnd(hueAround270, fzOr(satMedium, satHigh)),
              fzOr(valMedium, valHigh)
             );

    (*bkMatchP)[BKCOLOR_PURPLE] =
        fzAnd(fzAnd(hueAround320, fzOr(satMedium, satHigh)),
              fzOr(valMedium, valHigh)
             );

    (*bkMatchP)[BKCOLOR_BROWN]  =
	fzOr(
             fzAnd(fzOr(hueAround015, hueAround360),
                   fzAnd(fzNot(satVeryLow), fzOr(valLow, valMedium))),
             fzAnd(hueAround015, satLow)
	    );
}



bk_color
ppm_bk_color_from_color(pixel  const color,
                        pixval const maxval) {

    fzLog bkmatch[BKCOLOR_COUNT];
    bk_color i;
    bk_color bestSoFar;
    fzLog bestMatch;

    matchBk(color, maxval, &bkmatch);

    for (i = 0, bestSoFar = 0, bestMatch = 0.0; i < BKCOLOR_COUNT; ++i) {
        if (bkmatch[i] > bestMatch) {
            bestSoFar = i;
            bestMatch = bkmatch[i];
        }
    }
    return bestSoFar;
}



static pixel const bkColorMap[BKCOLOR_COUNT] = {
    {174, 174, 174}, /* BKCOLOR_GRAY   */
    {128,  42,  42}, /* BKCOLOR_BROWN  */
    {255, 128,   0}, /* BKCOLOR_ORANGE */
    {255,   0,   0}, /* BKCOLOR_RED    */
    {255, 255,   0}, /* BKCOLOR_YELLOW */
    {  0, 255,   0}, /* BKCOLOR_GREEN  */
    {  0,   0, 255}, /* BKCOLOR_BLUE   */
    {143,  94, 153}, /* BKCOLOR_VIOLET */
    {160,  32, 240}, /* BKCOLOR_PURPLE */
    {255, 255, 255}, /* BKCOLOR_WHITE  */
    {  0,   0,   0}  /* BKCOLOR_BLACK  */
};



pixel
ppm_color_from_bk_color(bk_color const bkColor,
                        pixval   const maxval) {

    pixel const color255 = bkColorMap[bkColor];

    pixel retval;

    if (maxval != 255) {
        PPM_DEPTH(retval, color255, 255, maxval);
    } else
        retval = color255;

    return retval;
}



static const char * const bkColorNameMap[BKCOLOR_COUNT] = {
    "gray",
    "brown",
    "orange",
    "red",
    "yellow",
    "green",
    "blue",
    "violet",
    "purple",
    "white",
    "black"
};



bk_color
ppm_bk_color_from_name(const char * const name) {

    bk_color i;

    for (i = 0; i < BKCOLOR_COUNT; ++i) {
        if (STREQ(name, bkColorNameMap[i]))
            return i;
    }
    pm_error("Invalid Berlin-Kay color name: '%s'", name);
    return 0;  /* quiet compiler warning */
}



const char *
ppm_name_from_bk_color(bk_color const bkColor) {

    if (bkColor >= BKCOLOR_COUNT)
        pm_error("Invalid color passed to name_from_bk_color(): %u",
                 bkColor);

    return bkColorNameMap[bkColor];
}