summaryrefslogtreecommitdiffstats
path: root/src/trace/potrace
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/potrace')
-rw-r--r--src/trace/potrace/inkscape-potrace.cpp154
-rw-r--r--src/trace/potrace/inkscape-potrace.h26
2 files changed, 76 insertions, 104 deletions
diff --git a/src/trace/potrace/inkscape-potrace.cpp b/src/trace/potrace/inkscape-potrace.cpp
index e18abb338..404d25d3a 100644
--- a/src/trace/potrace/inkscape-potrace.cpp
+++ b/src/trace/potrace/inkscape-potrace.cpp
@@ -363,7 +363,8 @@ PotraceTracingEngine::preview(Glib::RefPtr<Gdk::Pixbuf> thePixbuf)
//*This is the core inkscape-to-potrace binding
-char *PotraceTracingEngine::grayMapToPath(GrayMap *grayMap, long *nodeCount)
+std::string
+PotraceTracingEngine::grayMapToPath(GrayMap *grayMap, long *nodeCount)
{
/* get default parameters */
@@ -395,7 +396,7 @@ char *PotraceTracingEngine::grayMapToPath(GrayMap *grayMap, long *nodeCount)
if (!keepGoing)
{
g_warning("aborted");
- return NULL;
+ return "";
}
/* trace a bitmap*/
@@ -410,7 +411,7 @@ char *PotraceTracingEngine::grayMapToPath(GrayMap *grayMap, long *nodeCount)
g_warning("aborted");
potrace_state_free(potraceState);
potrace_param_free(potraceParams);
- return NULL;
+ return "";
}
Inkscape::SVGOStringStream data;
@@ -426,13 +427,12 @@ char *PotraceTracingEngine::grayMapToPath(GrayMap *grayMap, long *nodeCount)
potrace_param_free(potraceParams);
if (!keepGoing)
- return NULL;
+ return "";
- char *d = strdup((char *)data.str().c_str());
+ std::string d = data.str();
if ( nodeCount)
*nodeCount = thisNodeCount;
-
return d;
}
@@ -442,51 +442,47 @@ char *PotraceTracingEngine::grayMapToPath(GrayMap *grayMap, long *nodeCount)
/**
* This is called for a single scan
*/
-TracingEngineResult *
-PotraceTracingEngine::traceSingle(GdkPixbuf * thePixbuf, int *nrPaths)
+std::vector<TracingEngineResult>
+PotraceTracingEngine::traceSingle(GdkPixbuf * thePixbuf)
{
+ std::vector<TracingEngineResult> results;
+
if (!thePixbuf)
- return NULL;
+ return results;
brightnessFloor = 0.0; //important to set this
GrayMap *grayMap = filter(*this, thePixbuf);
if (!grayMap)
- return NULL;
+ return results;
long nodeCount;
- char *d = grayMapToPath(grayMap, &nodeCount);
+ std::string d = grayMapToPath(grayMap, &nodeCount);
grayMap->destroy(grayMap);
- if (!d)
- {
- *nrPaths = 0;
- return NULL;
- }
char *style = "fill:#000000";
//g_message("### GOT '%s' \n", d);
- TracingEngineResult *result = new TracingEngineResult(style, d, nodeCount);
-
- free(d);
-
- *nrPaths = 1;
+ TracingEngineResult result(style, d, nodeCount);
+ results.push_back(result);
- return result;
+ return results;
}
/**
* Called for multiple-scanning algorithms
*/
-TracingEngineResult *
-PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf, int *nrPaths)
+std::vector<TracingEngineResult>
+PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf)
{
+ std::vector<TracingEngineResult> results;
+
if (!thePixbuf)
- return NULL;
+ return results;
double low = 0.2; //bottom of range
double high = 0.9; //top of range
@@ -496,7 +492,6 @@ PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf, int *nrPaths)
int traceCount = 0;
- TracingEngineResult *results = NULL;
for ( brightnessThreshold = low ;
brightnessThreshold <= high ;
brightnessThreshold += delta)
@@ -505,18 +500,15 @@ PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf, int *nrPaths)
GrayMap *grayMap = filter(*this, thePixbuf);
if (!grayMap)
- return NULL;
+ return results;
long nodeCount;
- char *d = grayMapToPath(grayMap, &nodeCount);
+ std::string d = grayMapToPath(grayMap, &nodeCount);
grayMap->destroy(grayMap);
- if (!d)
- {
- *nrPaths = 0;
- return NULL;
- }
+ if (d.size() == 0)
+ return results;
int grayVal = (int)(256.0 * brightnessThreshold);
char style[31];
@@ -524,22 +516,8 @@ PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf, int *nrPaths)
grayVal, grayVal, grayVal);
//g_message("### GOT '%s' \n", d);
- TracingEngineResult *result = new TracingEngineResult(style, d, nodeCount);
-
- free(d);
-
- if (!results)
- {
- results = result; //first one
- }
- else
- {
- //walk to end of list
- TracingEngineResult *r;
- for (r=results ; r->next ; r=r->next)
- {}
- r->next = result;
- }
+ TracingEngineResult result(style, d, nodeCount);
+ results.push_back(result);
if (!multiScanStack)
brightnessFloor = brightnessThreshold;
@@ -553,9 +531,12 @@ PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf, int *nrPaths)
}
}
- //report the count of paths processed
- *nrPaths = multiScanNrColors;
-
+ //# Remove the bottom-most scan, if requested
+ if (results.size() > 1 && multiScanRemoveBackground)
+ {
+ g_message("remove background");
+ results.erase(results.end() - 1);
+ }
return results;
}
@@ -564,18 +545,18 @@ PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf, int *nrPaths)
/**
* Quantization
*/
-TracingEngineResult *
-PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf, int *nrPaths)
+std::vector<TracingEngineResult>
+PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf)
{
+ std::vector<TracingEngineResult> results;
+
if (!thePixbuf)
- return NULL;
+ return results;
IndexedMap *iMap = filterIndexed(*this, thePixbuf);
if (!iMap)
- return NULL;
-
- TracingEngineResult *results = NULL;
+ return results;
//Create and clear a gray map
GrayMap *gm = GrayMapCreate(iMap->width, iMap->height);
@@ -607,13 +588,10 @@ PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf, int *nrPaths)
//## Now we have a traceable graymap
long nodeCount;
- char *d = grayMapToPath(gm, &nodeCount);
+ std::string d = grayMapToPath(gm, &nodeCount);
- if (!d)
- {
- *nrPaths = 0;
- return NULL;
- }
+ if (d.size() == 0)
+ return results;
//### get style info
char style[13];
@@ -621,29 +599,8 @@ PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf, int *nrPaths)
sprintf(style, "fill:#%02x%02x%02x", rgb.r, rgb.g, rgb.b);
//g_message("### GOT '%s' \n", d);
- TracingEngineResult *result = new TracingEngineResult(style, d, nodeCount);
-
- free(d);
-
- if (!results)
- {
- results = result; //first one
- }
- else
- {
- //prepend
- /*
- result->next = results;
- results = result;
- */
-
- //append
- TracingEngineResult *r;
- for (r=results ; r->next ; r=r->next)
- {}
- r->next = result;
- }
-
+ TracingEngineResult result(style, d, nodeCount);
+ results.push_back(result);
SPDesktop *desktop = SP_ACTIVE_DESKTOP;
if (desktop)
@@ -654,14 +611,18 @@ PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf, int *nrPaths)
}
- }
-
- //report the count of paths processed
- *nrPaths = iMap->nrColors;
+ }// for colorIndex
gm->destroy(gm);
iMap->destroy(iMap);
+ //# Remove the bottom-most scan, if requested
+ if (results.size() > 1 && multiScanRemoveBackground)
+ {
+ g_message("remove background");
+ results.erase(results.end() - 1);
+ }
+
return results;
}
@@ -672,9 +633,8 @@ PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf, int *nrPaths)
* return the path data that is compatible with the d="" attribute
* of an SVG <path> element.
*/
-TracingEngineResult *
-PotraceTracingEngine::trace(Glib::RefPtr<Gdk::Pixbuf> pixbuf,
- int *nrPaths)
+std::vector<TracingEngineResult>
+PotraceTracingEngine::trace(Glib::RefPtr<Gdk::Pixbuf> pixbuf)
{
GdkPixbuf *thePixbuf = pixbuf->gobj();
@@ -685,15 +645,15 @@ PotraceTracingEngine::trace(Glib::RefPtr<Gdk::Pixbuf> pixbuf,
if ( traceType == TRACE_QUANT_COLOR ||
traceType == TRACE_QUANT_MONO )
{
- return traceQuant(thePixbuf, nrPaths);
+ return traceQuant(thePixbuf);
}
else if ( traceType == TRACE_BRIGHTNESS_MULTI )
{
- return traceBrightnessMulti(thePixbuf, nrPaths);
+ return traceBrightnessMulti(thePixbuf);
}
else
{
- return traceSingle(thePixbuf, nrPaths);
+ return traceSingle(thePixbuf);
}
}
diff --git a/src/trace/potrace/inkscape-potrace.h b/src/trace/potrace/inkscape-potrace.h
index 0f257cb95..f3459159f 100644
--- a/src/trace/potrace/inkscape-potrace.h
+++ b/src/trace/potrace/inkscape-potrace.h
@@ -158,6 +158,18 @@ class PotraceTracingEngine : public TracingEngine
return multiScanSmooth;
}
+ /**
+ * Sets whether we want to remove the background (bottom) trace
+ */
+ void setMultiScanRemoveBackground(bool val)
+ {
+ multiScanRemoveBackground= val;
+ }
+ bool getMultiScanRemoveBackground()
+ {
+ return multiScanRemoveBackground;
+ }
+
/**
* This is the working method of this implementing class, and all
@@ -165,8 +177,8 @@ class PotraceTracingEngine : public TracingEngine
* return the path data that is compatible with the d="" attribute
* of an SVG <path> element.
*/
- virtual TracingEngineResult *trace(Glib::RefPtr<Gdk::Pixbuf> pixbuf,
- int *nrPaths);
+ virtual std::vector<TracingEngineResult> trace(
+ Glib::RefPtr<Gdk::Pixbuf> pixbuf);
/**
* Abort the thread that is executing getPathDataFromPixbuf()
@@ -206,16 +218,16 @@ class PotraceTracingEngine : public TracingEngine
int multiScanNrColors;
bool multiScanStack; //do we tile or stack?
bool multiScanSmooth;//do we use gaussian filter?
-
+ bool multiScanRemoveBackground; //do we remove the bottom trace?
/**
* This is the actual wrapper of the call to Potrace. nodeCount
* returns the count of nodes created. May be NULL if ignored.
*/
- char *grayMapToPath(GrayMap *gm, long *nodeCount);
+ std::string grayMapToPath(GrayMap *gm, long *nodeCount);
- TracingEngineResult *traceBrightnessMulti(GdkPixbuf *pixbuf, int *nrPaths);
- TracingEngineResult *traceQuant(GdkPixbuf *pixbuf, int *nrPaths);
- TracingEngineResult *traceSingle(GdkPixbuf *pixbuf, int *nrPaths);
+ std::vector<TracingEngineResult>traceBrightnessMulti(GdkPixbuf *pixbuf);
+ std::vector<TracingEngineResult>traceQuant(GdkPixbuf *pixbuf);
+ std::vector<TracingEngineResult>traceSingle(GdkPixbuf *pixbuf);
};//class PotraceTracingEngine