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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
|
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef SEEN_SIOX_H
#define SEEN_SIOX_H
/*
* Copyright 2005, 2006 by Gerald Friedland, Kristian Jantz and Lars Knipping
*
* Conversion to C++ for Inkscape by Bob Jamison
*
* Released under GNU GPL v2+, read the file 'COPYING' for more information.
*/
/*
* Note by Bob Jamison:
* After translating the siox.org Java API to C++ and receiving an
* education into this wonderful code, I began again,
* and started this version using lessons learned. This version is
* an attempt to provide an dependency-free SIOX engine that anyone
* can use in their project with minimal effort.
*
* Many thanks to the fine people at siox.org.
*/
#include <string>
#include <vector>
#define HAVE_GLIB
#ifdef HAVE_GLIB
#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif
namespace org
{
namespace siox
{
//########################################################################
//# C L A B
//########################################################################
/**
*
*/
class CieLab
{
public:
/**
*
*/
CieLab()
{
init();
C = 0;
L = A = B = 0.0f;
}
/**
*
*/
CieLab(unsigned long rgb);
/**
*
*/
CieLab(float lArg, float aArg, float bArg)
{
init();
C = 0;
L = lArg;
A = aArg;
B = bArg;
}
/**
*
*/
CieLab(const CieLab &other)
{
init();
C = other.C;
L = other.L;
A = other.A;
B = other.B;
}
/**
*
*/
CieLab &operator=(const CieLab &other)
{
init();
C = other.C;
L = other.L;
A = other.A;
B = other.B;
return *this;
}
/**
*
*/
virtual ~CieLab()
= default;
/**
* Retrieve a CieLab value via index.
*/
virtual float operator()(unsigned int index)
{
if (index==0) return L;
else if (index==1) return A;
else if (index==2) return B;
else return 0;
}
/**
*
*/
virtual void add(const CieLab &other)
{
C += other.C;
L += other.L;
A += other.A;
B += other.B;
}
/**
*
*/
virtual void mul(float scale)
{
L *= scale;
A *= scale;
B *= scale;
}
/**
*
*/
virtual unsigned long toRGB();
/**
* Approximate cube roots
*/
double cbrt(double x);
/**
*
*/
double qnrt(double x);
/**
* Raise to the 2.4 power
*/
double pow24(double x);
/**
* Squared Euclidean distance between this and another color
*/
float diffSq(const CieLab &other);
/**
* Computes squared euclidean distance in CieLab space for two colors
* given as RGB values.
*/
static float diffSq(unsigned int rgb1, unsigned int rgb2);
/**
* Computes squared euclidean distance in CieLab space for two colors
* given as RGB values.
*/
static float diff(unsigned int rgb0, unsigned int rgb1);
unsigned int C;
float L;
float A;
float B;
private:
/**
*
*/
void init();
};
//########################################################################
//# S I O X I M A G E
//########################################################################
/**
* This is a generic image type that provides a consistent interface
* to Siox, so that developers will not need to worry about data arrays.
*/
class SioxImage
{
public:
/**
* Create an image with the given width and height
*/
SioxImage(unsigned int width, unsigned int height);
/**
* Copy constructor
*/
SioxImage(const SioxImage &other);
/**
* Assignment
*/
SioxImage &operator=(const SioxImage &other);
/**
* Clean up after use.
*/
virtual ~SioxImage();
/**
* Returns true if the previous operation on this image
* was successful, else false.
*/
virtual bool isValid();
/**
* Sets whether an operation was successful, and whether
* this image should be considered a valid one.
* was successful, else false.
*/
virtual void setValid(bool val);
/**
* Set a pixel at the x,y coordinates to the given value.
* If the coordinates are out of range, do nothing.
*/
virtual void setPixel(unsigned int x,
unsigned int y,
unsigned int pixval);
/**
* Set a pixel at the x,y coordinates to the given r, g, b values.
* If the coordinates are out of range, do nothing.
*/
virtual void setPixel(unsigned int x, unsigned int y,
unsigned int a,
unsigned int r,
unsigned int g,
unsigned int b);
/**
* Get a pixel at the x,y coordinates given. If
* the coordinates are out of range, return 0
*/
virtual unsigned int getPixel(unsigned int x, unsigned int y);
/**
* Return the image data buffer
*/
virtual unsigned int *getImageData();
/**
* Set a confidence value at the x,y coordinates to the given value.
* If the coordinates are out of range, do nothing.
*/
virtual void setConfidence(unsigned int x,
unsigned int y,
float conf);
/**
* Get a confidence value at the x,y coordinates given. If
* the coordinates are out of range, return 0
*/
virtual float getConfidence(unsigned int x, unsigned int y);
/**
* Return the confidence data buffer
*/
virtual float *getConfidenceData();
/**
* Return the width of this image
*/
virtual int getWidth();
/**
* Return the height of this image
*/
virtual int getHeight();
/**
* Saves this image as a simple color PPM
*/
bool writePPM(const std::string &fileName);
#ifdef HAVE_GLIB
/**
* Special constructor to create an image from a GdkPixbuf.
*/
SioxImage(GdkPixbuf *buf);
/**
* Creates a GdkPixbuf from this image. The user must
* remember to destroy the image when no longer needed.
* with g_free(pixbuf)
*/
GdkPixbuf *getGdkPixbuf();
#endif
private:
SioxImage()
= default;
/**
* Assign values to that of another
*/
void assign(const SioxImage &other);
/**
* Initialize values. Used by constructors
*/
void init(unsigned int width, unsigned int height);
bool valid;
unsigned int width;
unsigned int height;
unsigned long imageSize;
/**
* Pixel data
*/
unsigned int *pixdata;
/**
* Confidence matrix data
*/
float *cmdata;
private:
/**
* Error logging
*/
void error(const char *fmt, ...) G_GNUC_PRINTF(2,3);
};
//########################################################################
//# S I O X O B S E R V E R
//########################################################################
class Siox;
/**
* This is a class for observing the progress of a Siox engine. Overload
* the methods in your subclass to get the desired behaviour.
*/
class SioxObserver
{
public:
/**
* Constructor. Context can point to anything, and is usually
* used to point to a C++ object or C state object, to delegate
* callback processing to something else. Use NULL to ignore.
*/
SioxObserver(void *contextArg) : context(nullptr)
{ context = contextArg; }
/**
* Destructor
*/
virtual ~SioxObserver()
= default;
/**
* Informs the observer how much has been completed.
* Return false if the processing should be aborted.
*/
virtual bool progress(float /*percentCompleted*/)
{
return true;
}
/**
* Send an error string to the Observer. Processing will
* be halted.
*/
virtual void error(const std::string &/*msg*/)
{
}
protected:
void *context;
};
//########################################################################
//# S I O X
//########################################################################
/**
*
*/
class Siox
{
public:
/**
* Confidence corresponding to a certain foreground region (equals one).
*/
static const float CERTAIN_FOREGROUND_CONFIDENCE; //=1.0f;
/**
* Confidence for a region likely being foreground.
*/
static const float FOREGROUND_CONFIDENCE; //=0.8f;
/**
* Confidence for foreground or background type being equally likely.
*/
static const float UNKNOWN_REGION_CONFIDENCE; //=0.5f;
/**
* Confidence for a region likely being background.
*/
static const float BACKGROUND_CONFIDENCE; //=0.1f;
/**
* Confidence corresponding to a certain background reagion (equals zero).
*/
static const float CERTAIN_BACKGROUND_CONFIDENCE; //=0.0f;
/**
* Construct a Siox engine
*/
Siox();
/**
* Construct a Siox engine. Use null to ignore
*/
Siox(SioxObserver *observer);
/**
*
*/
virtual ~Siox();
/**
* Extract the foreground of the original image, according
* to the values in the confidence matrix. If the operation fails,
* sioxImage.isValid() will be false.
* backgroundFillColor is any ARGB color, such as 0xffffff (white)
* or 0x000000 (black)
*/
virtual SioxImage extractForeground(const SioxImage &originalImage,
unsigned int backgroundFillColor);
private:
SioxObserver *sioxObserver;
/**
* Progress reporting
*/
bool progressReport(float percentCompleted);
/**
* Flag this as false during processing to abort
*/
bool keepGoing;
/**
* Image width
*/
unsigned int width;
/**
* Image height
*/
unsigned int height;
/**
* Image size in pixels
*/
unsigned long pixelCount;
/**
* Image data
*/
unsigned int *image;
/**
* Image confidence matrix
*/
float *cm;
/**
* Markup for image editing
*/
int *labelField;
/**
* Our signature limits
*/
float limits[3];
/**
* Maximum distance of two lab values.
*/
float clusterSize;
/**
* Initialize the Siox engine to its 'pristine' state.
* Performed at the beginning of extractForeground().
*/
void init();
/**
* Clean up any debris from processing.
*/
void cleanup();
/**
* Error logging
*/
void error(const char *fmt, ...) G_GNUC_PRINTF(2,3);
/**
* Trace logging
*/
void trace(const char *fmt, ...) G_GNUC_PRINTF(2,3);
/**
* Stage 1 of the color signature work. 'dims' will be either
* 2 for grays, or 3 for colors
*/
void colorSignatureStage1(CieLab *points,
unsigned int leftBase,
unsigned int rightBase,
unsigned int recursionDepth,
unsigned int *clusters,
const unsigned int dims);
/**
* Stage 2 of the color signature work
*/
void colorSignatureStage2(CieLab *points,
unsigned int leftBase,
unsigned int rightBase,
unsigned int recursionDepth,
unsigned int *clusters,
const float threshold,
const unsigned int dims);
/**
* Main color signature method
*/
bool colorSignature(const std::vector<CieLab> &inputVec,
std::vector<CieLab> &result,
const unsigned int dims);
/**
*
*/
void keepOnlyLargeComponents(float threshold,
double sizeFactorToKeep);
/**
*
*/
int depthFirstSearch(int startPos, float threshold, int curLabel);
/**
*
*/
void fillColorRegions();
/**
* Applies the morphological dilate operator.
*
* Can be used to close small holes in the given confidence matrix.
*/
void dilate(float *cm, int xres, int yres);
/**
* Applies the morphological erode operator.
*/
void erode(float *cm, int xres, int yres);
/**
* Normalizes the matrix to values to [0..1].
*/
void normalizeMatrix(float *cm, int cmSize);
/**
* Multiplies matrix with the given scalar.
*/
void premultiplyMatrix(float alpha, float *cm, int cmSize);
/**
* Blurs confidence matrix with a given symmetrically weighted kernel.
*/
void smooth(float *cm, int xres, int yres,
float f1, float f2, float f3);
/**
* Squared Euclidean distance of p and q.
*/
float sqrEuclideanDist(float *p, int pSize, float *q);
};
} // namespace siox
} // namespace org
#endif // SEEN_SIOX_H
//########################################################################
//# E N D O F F I L E
//########################################################################
|