diff --git a/src/trace/trace.cpp b/src/trace/trace.cpp
index ea822c5364797d2e150a3d52d547453d866ce93c..d40fb89d7e67567b2e112c53ec1824e85c78d67f 100644 (file)
--- a/src/trace/trace.cpp
+++ b/src/trace/trace.cpp
#include "siox.h"
#include "imagemap-gdk.h"
-namespace Inkscape {
-namespace Trace {
+
+namespace Inkscape
+{
+
+namespace Trace
+{
/**
- *
+ * Get the selected image. Also check for any SPItems over it, in
+ * case the user wants SIOX pre-processing.
*/
SPImage *
Tracer::getSelectedSPImage()
{
+
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
if (!desktop)
{
return NULL;
}
- Inkscape::Selection *sel = SP_DT_SELECTION(desktop);
+ Inkscape::MessageStack *msgStack = sp_desktop_message_stack(desktop);
+
+ Inkscape::Selection *sel = sp_desktop_selection(desktop);
if (!sel)
{
char *msg = _("Select an <b>image</b> to trace");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
//g_warning(msg);
return NULL;
}
if (img) //we want only one
{
char *msg = _("Select only one <b>image</b> to trace");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
return NULL;
}
img = SP_IMAGE(item);
if (!img || sioxShapes.size() < 1)
{
char *msg = _("Select one image and one or more shapes above it");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
return NULL;
}
return img;
if (!item)
{
char *msg = _("Select an <b>image</b> to trace"); //same as above
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
//g_warning(msg);
return NULL;
}
if (!SP_IS_IMAGE(item))
{
char *msg = _("Select an <b>image</b> to trace");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
//g_warning(msg);
return NULL;
}
-/**
- *
- */
-GdkPixbuf *
-Tracer::getSelectedImage()
-{
-
- SPImage *img = getSelectedSPImage();
- if (!img)
- return NULL;
-
- GdkPixbuf *pixbuf = img->pixbuf;
-
- return pixbuf;
-
-}
-
+typedef org::siox::SioxImage SioxImage;
+typedef org::siox::Siox Siox;
GdkPixbuf *
Tracer::sioxProcessImage(SPImage *img, GdkPixbuf *origPixbuf)
{
+ if (!sioxEnabled)
+ return origPixbuf;
//Convert from gdk, so a format we know. By design, the pixel
//format in PackedPixelMap is identical to what is needed by SIOX
- PackedPixelMap *ppMap = gdkPixbufToPackedPixelMap(origPixbuf);
- //We need to create two things:
- // 1. An array of long pixel values of ARGB
- // 2. A matching array of per-pixel float 'confidence' values
- unsigned long *imgBuf = ppMap->pixels;
- float *confidenceMatrix = new float[ppMap->width * ppMap->height];
+ SioxImage simage(origPixbuf);
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
if (!desktop)
return NULL;
}
- Inkscape::Selection *sel = SP_DT_SELECTION(desktop);
+ Inkscape::MessageStack *msgStack = sp_desktop_message_stack(desktop);
+
+ Inkscape::Selection *sel = sp_desktop_selection(desktop);
if (!sel)
{
char *msg = _("Select an <b>image</b> to trace");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
//g_warning(msg);
return NULL;
}
double width = (double)(aImg->bbox.x1 - aImg->bbox.x0);
double height = (double)(aImg->bbox.y1 - aImg->bbox.y0);
- double iwidth = (double)ppMap->width;
- double iheight = (double)ppMap->height;
+ double iwidth = (double)simage.getWidth();
+ double iheight = (double)simage.getHeight();
double iwscale = width / iwidth;
double ihscale = height / iheight;
- unsigned long cmIndex = 0;
-
-
std::vector<NRArenaItem *> arenaItems;
std::vector<SPShape *>::iterator iter;
for (iter = sioxShapes.begin() ; iter!=sioxShapes.end() ; iter++)
}
//g_message("%d arena items\n", arenaItems.size());
- PackedPixelMap *dumpMap = PackedPixelMapCreate(ppMap->width, ppMap->height);
+ PackedPixelMap *dumpMap = PackedPixelMapCreate(
+ simage.getWidth(), simage.getHeight());
- for (int row=0 ; row<ppMap->height ; row++)
+ for (int row=0 ; row<simage.getHeight() ; row++)
{
double ypos = ((double)aImg->bbox.y0) + ihscale * (double) row;
- for (int col=0 ; col<ppMap->width ; col++)
+ for (int col=0 ; col<simage.getWidth() ; col++)
{
//Get absolute X,Y position
double xpos = ((double)aImg->bbox.x0) + iwscale * (double)col;
{
//g_message("hit!\n");
dumpMap->setPixelLong(dumpMap, col, row, 0L);
- confidenceMatrix[cmIndex] =
- org::siox::SioxSegmentator::CERTAIN_FOREGROUND_CONFIDENCE;
+ simage.setConfidence(col, row,
+ Siox::UNKNOWN_REGION_CONFIDENCE);
}
else
{
dumpMap->setPixelLong(dumpMap, col, row,
- ppMap->getPixel(ppMap, col, row));
- confidenceMatrix[cmIndex] =
- org::siox::SioxSegmentator::CERTAIN_BACKGROUND_CONFIDENCE;
+ simage.getPixel(col, row));
+ simage.setConfidence(col, row,
+ Siox::CERTAIN_BACKGROUND_CONFIDENCE);
}
- cmIndex++;
}
}
+ //dumpMap->writePPM(dumpMap, "siox1.ppm");
+ dumpMap->destroy(dumpMap);
+
//## ok we have our pixel buf
- org::siox::SioxSegmentator ss(ppMap->width, ppMap->height, NULL, 0);
- ss.segmentate(imgBuf, confidenceMatrix, 0, 0.0);
+ org::siox::Siox sengine;
+ org::siox::SioxImage result =
+ sengine.extractForeground(simage, 0xffffff);
+ if (!result.isValid())
+ {
+ g_warning("Invalid SIOX result");
+ return NULL;
+ }
- dumpMap->writePPM(dumpMap, "siox.ppm");
- dumpMap->destroy(dumpMap);
+ //result.writePPM("siox2.ppm");
/* Free Arena and ArenaItem */
/*
nr_object_unref((NRObject *) arena);
*/
- GdkPixbuf *newPixbuf = packedPixelMapToGdkPixbuf(ppMap);
- ppMap->destroy(ppMap);
- delete confidenceMatrix;
+ GdkPixbuf *newPixbuf = result.getGdkPixbuf();
return newPixbuf;
}
+/**
+ *
+ */
+GdkPixbuf *
+Tracer::getSelectedImage()
+{
+
+ SPImage *img = getSelectedSPImage();
+ if (!img)
+ return NULL;
+
+ GdkPixbuf *pixbuf = img->pixbuf;
+ if (!pixbuf)
+ return NULL;
+
+ if (sioxEnabled)
+ {
+ GdkPixbuf *sioxPixbuf = sioxProcessImage(img, pixbuf);
+ if (!sioxPixbuf)
+ {
+ g_object_ref(pixbuf);
+ return pixbuf;
+ }
+ else
+ {
+ return sioxPixbuf;
+ }
+ }
+ else
+ {
+ g_object_ref(pixbuf);
+ return pixbuf;
+ }
+
+}
+
//#########################################################################
return;
}
- Inkscape::Selection *selection = SP_DT_SELECTION (desktop);
+ Inkscape::MessageStack *msgStack = sp_desktop_message_stack(desktop);
+
+ Inkscape::Selection *selection = sp_desktop_selection (desktop);
if (!SP_ACTIVE_DOCUMENT)
{
char *msg = _("Trace: No active document");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
//g_warning(msg);
engine = NULL;
return;
}
GdkPixbuf *pixbuf = img->pixbuf;
+ g_object_ref(pixbuf);
+
+ pixbuf = sioxProcessImage(img, pixbuf);
if (!pixbuf)
{
char *msg = _("Trace: Image has no bitmap data");
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::ERROR_MESSAGE, msg);
+ msgStack->flash(Inkscape::ERROR_MESSAGE, msg);
//g_warning(msg);
engine = NULL;
return;
}
- //## SIOX pre-processing to get a smart subimage of the pixbuf.
- //## This is done before any other filters
- if (sioxEnabled)
- {
- /*
- Ok, we have requested siox, and getSelectedSPImage() has found a single
- bitmap and one or more SPItems above it. Now what we need to do is create
- a siox-segmented subimage pixbuf. We not need alter 'img' at all, since this
- pixbuf will be the same dimensions and at the same location.
- Remember to free this new pixbuf later.
- */
- pixbuf = sioxProcessImage(img, pixbuf);
- }
-
int nrPaths;
TracingEngineResult *results = engine->trace(pixbuf, &nrPaths);
//printf("nrPaths:%d\n", nrPaths);
Inkscape::GC::release(pathRepr);
}
- //did we allocate a pixbuf copy?
- if (sioxEnabled)
- {
- g_free(pixbuf);
- }
+ //release our pixbuf
+ g_object_unref(pixbuf);
delete results;
engine = NULL;
char *msg = g_strdup_printf(_("Trace: Done. %ld nodes created"), totalNodeCount);
- SP_DT_MSGSTACK(desktop)->flash(Inkscape::NORMAL_MESSAGE, msg);
+ msgStack->flash(Inkscape::NORMAL_MESSAGE, msg);
g_free(msg);
}