Code

add document to action events
[inkscape.git] / src / trace / siox.cpp
index 9c5db4731eef30b81b1a2e73d42b045b29f2583c..c69af04a165c85266a3897e98cdd8576018dac8e 100644 (file)
@@ -20,6 +20,7 @@
 #include <math.h>
 #include <stdarg.h>
 #include <map>
+#include <algorithm>
 
 
 namespace org
@@ -34,8 +35,6 @@ namespace siox
 //#  C L A B
 //########################################################################
 
-static std::map<unsigned long, CieLab> clabLookupTable;
-
 /**
  * Convert integer A, R, G, B values into an pixel value.
  */
@@ -111,7 +110,7 @@ void CieLab::init()
     if (!_clab_inited_)
         {
         cbrt_table[0] = pow(float(1)/float(ROOT_TAB_SIZE*2), 0.3333);
-       qn_table[0]   = pow(float(1)/float(ROOT_TAB_SIZE*2), 0.2);
+        qn_table[0]   = pow(float(1)/float(ROOT_TAB_SIZE*2), 0.2);
         for(int i = 1; i < ROOT_TAB_SIZE +1; i++)
             {
             cbrt_table[i] = pow(float(i)/float(ROOT_TAB_SIZE), 0.3333);
@@ -130,19 +129,6 @@ CieLab::CieLab(unsigned long rgb)
 {
     init();
 
-    //First try looking up in the cache
-    std::map<unsigned long, CieLab>::iterator iter;
-    iter = clabLookupTable.find(rgb);
-    if (iter != clabLookupTable.end())
-        {
-        CieLab res = iter->second;
-        C = res.C;
-        L = res.L;
-        A = res.A;
-        B = res.B;
-        }
-
-
     int ir  = (rgb>>16) & 0xff;
     int ig  = (rgb>> 8) & 0xff;
     int ib  = (rgb    ) & 0xff;
@@ -170,18 +156,14 @@ CieLab::CieLab(unsigned long rgb)
     else
         fb = fb / 12.92;
 
-    fr = fr * 100.0;
-    fg = fg * 100.0;
-    fb = fb * 100.0;
-
     // Use white = D65
-    float x = fr * 0.4124 + fg * 0.3576 + fb * 0.1805;
-    float y = fr * 0.2126 + fg * 0.7152 + fb * 0.0722;
-    float z = fr * 0.0193 + fg * 0.1192 + fb * 0.9505;
+    const float x = fr * 0.4124 + fg * 0.3576 + fb * 0.1805;
+    const float y = fr * 0.2126 + fg * 0.7152 + fb * 0.0722;
+    const float z = fr * 0.0193 + fg * 0.1192 + fb * 0.9505;
 
-    float vx = x /  95.047;
-    float vy = y / 100.000;
-    float vz = z / 108.883;
+    float vx = x / 0.95047;
+    float vy = y;
+    float vz = z / 1.08883;
 
     //printf("vx:%f vy:%f vz:%f\n", vx, vy, vz);
     if (vx > 0.008856)
@@ -206,16 +188,12 @@ CieLab::CieLab(unsigned long rgb)
     L = 116.0 * vy - 16.0;
     A = 500.0 * (vx - vy);
     B = 200.0 * (vy - vz);
-
-    // Cache for next time
-    clabLookupTable[rgb] = *this;
-
 }
 
 
 
 /**
- * Return this CieLab's value a a packed-pixel ARGB value
+ * Return this CieLab's value converted to a packed-pixel ARGB value
  */
 unsigned long CieLab::toRGB()
 {
@@ -242,13 +220,8 @@ unsigned long CieLab::toRGB()
     else
         vz = (vz - 16.0 / 116.0) / 7.787;
 
-    float x =  95.047 * vx; //use white = D65
-    float y = 100.000 * vy;
-    float z = 108.883 * vz;
-
-    vx = x / 100.0;
-    vy = y / 100.0;
-    vz = z / 100.0;
+    vx *= 0.95047; //use white = D65
+    vz *= 1.08883;
 
     float vr =(float)(vx *  3.2406 + vy * -1.5372 + vz * -0.4986);
     float vg =(float)(vx * -0.9689 + vy *  1.8758 + vz *  0.0415);
@@ -331,25 +304,25 @@ public:
     Tupel(float minBgDistArg, long indexMinBgArg,
           float minFgDistArg, long indexMinFgArg)
         {
-       minBgDist  = minBgDistArg;
-       indexMinBg = indexMinBgArg;
-       minFgDist  = minFgDistArg;
-       indexMinFg = indexMinFgArg;
+        minBgDist  = minBgDistArg;
+        indexMinBg = indexMinBgArg;
+        minFgDist  = minFgDistArg;
+        indexMinFg = indexMinFgArg;
         }
     Tupel(const Tupel &other)
         {
-       minBgDist  = other.minBgDist;
-       indexMinBg = other.indexMinBg;
-       minFgDist  = other.minFgDist;
-       indexMinFg = other.indexMinFg;
+        minBgDist  = other.minBgDist;
+        indexMinBg = other.indexMinBg;
+        minFgDist  = other.minFgDist;
+        indexMinFg = other.indexMinFg;
         }
     Tupel &operator=(const Tupel &other)
         {
-       minBgDist  = other.minBgDist;
-       indexMinBg = other.indexMinBg;
-       minFgDist  = other.minFgDist;
-       indexMinFg = other.indexMinFg;
-       return *this;
+        minBgDist  = other.minBgDist;
+        indexMinBg = other.indexMinBg;
+        minFgDist  = other.minFgDist;
+        indexMinFg = other.indexMinFg;
+        return *this;
         }
     virtual ~Tupel()
         {}
@@ -404,6 +377,24 @@ SioxImage::~SioxImage()
     if (cmdata)  delete[] cmdata;
 }
 
+/**
+ * Error logging
+ */
+void SioxImage::error(char *fmt, ...)
+{
+    char msgbuf[256];
+    va_list args;
+    va_start(args, fmt);
+    vsnprintf(msgbuf, 255, fmt, args);
+    va_end(args) ;
+#ifdef HAVE_GLIB
+    g_warning("SioxImage error: %s\n", msgbuf);
+#else
+    fprintf(stderr, "SioxImage error: %s\n", msgbuf);
+#endif
+}
+
+
 /**
  * Returns true if the previous operation on this image
  * was successful, else false.
@@ -432,8 +423,12 @@ void SioxImage::setPixel(unsigned int x,
                          unsigned int y,
                          unsigned int pixval)
 {
-    if (x > width || y > height)
+    if (x >= width || y >= height)
+        {
+        error("setPixel: out of bounds (%d,%d)/(%d,%d)",
+                   x, y, width, height);
         return;
+        }
     unsigned long offset = width * y + x;
     pixdata[offset] = pixval; 
 }
@@ -448,8 +443,12 @@ void SioxImage::setPixel(unsigned int x, unsigned int y,
                          unsigned int g,
                          unsigned int b)
 {
-    if (x > width || y > height)
+    if (x >= width || y >= height)
+        {
+        error("setPixel: out of bounds (%d,%d)/(%d,%d)",
+                   x, y, width, height);
         return;
+        }
     unsigned long offset = width * y + x;
     unsigned int pixval = ((a << 24) & 0xff000000) |
                           ((r << 16) & 0x00ff0000) |
@@ -466,8 +465,12 @@ void SioxImage::setPixel(unsigned int x, unsigned int y,
  */
 unsigned int SioxImage::getPixel(unsigned int x, unsigned int y)
 {
-    if (x > width || y > height)
+    if (x >= width || y >= height)
+        {
+        error("getPixel: out of bounds (%d,%d)/(%d,%d)",
+                   x, y, width, height);
         return 0L;
+        }
     unsigned long offset = width * y + x;
     return pixdata[offset]; 
 }
@@ -488,8 +491,12 @@ void SioxImage::setConfidence(unsigned int x,
                               unsigned int y,
                               float confval)
 {
-    if (x > width || y > height)
+    if (x >= width || y >= height)
+        {
+        error("setConfidence: out of bounds (%d,%d)/(%d,%d)",
+                   x, y, width, height);
         return;
+        }
     unsigned long offset = width * y + x;
     cmdata[offset] = confval; 
 }
@@ -500,8 +507,12 @@ void SioxImage::setConfidence(unsigned int x,
  */
 float SioxImage::getConfidence(unsigned int x, unsigned int y)
 {
-    if (x > width || y > height)
+    if (x >= width || y >= height)
+        {
+        g_warning("getConfidence: out of bounds (%d,%d)/(%d,%d)",
+                   x, y, width, height);
         return 0.0;
+        }
     unsigned long offset = width * y + x;
     return cmdata[offset]; 
 }
@@ -873,7 +884,7 @@ SioxImage Siox::extractForeground(const SioxImage &originalImage,
         return workImage;
         }
 
-    trace("knownBg:%d knownFg:%d", knownBg.size(), knownFg.size());
+    trace("knownBg:%zu knownFg:%zu", knownBg.size(), knownFg.size());
 
 
     std::vector<CieLab> bgSignature ;
@@ -1624,7 +1635,6 @@ void Siox::erode(float *cm, int xres, int yres)
 
 
 
-
 /**
  * Normalizes the matrix to values to [0..1].
  */
@@ -1632,8 +1642,10 @@ void Siox::normalizeMatrix(float *cm, int cmSize)
 {
     float max= -1000000.0f;
     for (int i=0; i<cmSize; i++)
-        if (max<cm[i] > max)
-            max=cm[i];
+        if (cm[i] > max) max=cm[i];
+        
+    //good to use STL, but max() is not iterative
+    //float max = *std::max(cm, cm + cmSize);
 
     if (max<=0.0 || max==1.0)
         return;