From adbc8efa025ad921775e3c171c3ce3bd7763d1ca Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Tue, 26 Jul 2011 21:55:07 -0700 Subject: Cleaning up trace methods to aid fixing color inversion. (bzr r10507) --- src/message-stack.cpp | 6 + src/message-stack.h | 29 +++-- src/trace/potrace/inkscape-potrace.cpp | 203 ++++++++++++++++----------------- src/trace/potrace/inkscape-potrace.h | 10 ++ 4 files changed, 136 insertions(+), 112 deletions(-) (limited to 'src') diff --git a/src/message-stack.cpp b/src/message-stack.cpp index d2101009e..c1669e3db 100644 --- a/src/message-stack.cpp +++ b/src/message-stack.cpp @@ -62,6 +62,12 @@ void MessageStack::cancel(MessageId id) { } } +MessageId MessageStack::flash(MessageType type, Glib::ustring const &message) +{ + MessageId id = flash( type, message.c_str() ); + return id; +} + MessageId MessageStack::flash(MessageType type, gchar const *message) { switch (type) { case INFORMATION_MESSAGE: // stay rather long so as to seem permanent, but eventually disappear diff --git a/src/message-stack.h b/src/message-stack.h index ae8860965..3b8307761 100644 --- a/src/message-stack.h +++ b/src/message-stack.h @@ -5,8 +5,10 @@ /* * Authors: * MenTaLguY + * Jon A. Cruz * * Copyright (C) 2004 MenTaLguY + * Copyright (C) 2011 Jon A. Cruz * * Released under GNU GPL, read the file 'COPYING' for more information */ @@ -18,6 +20,7 @@ #include #include #include +#include #include "gc-managed.h" #include "gc-finalized.h" #include "gc-anchored.h" @@ -108,15 +111,27 @@ public: */ void cancel(MessageId id); - /** @brief temporarily pushes a message onto the stack - * - * @param type the message type - * @param message the message text - * - * @return the id of the pushed message - */ + /** + * Temporarily pushes a message onto the stack. + * + * @param type the message type + * @param message the message text + * + * @return the id of the pushed message + */ MessageId flash(MessageType type, gchar const *message); + /** + * Temporarily pushes a message onto the stack. + * + * @param type the message type + * @param message the message text + * + * @return the id of the pushed message + */ + MessageId flash(MessageType type, Glib::ustring const &message); + + /** @brief temporarily pushes a message onto the stack using * printf-like formatting * diff --git a/src/trace/potrace/inkscape-potrace.cpp b/src/trace/potrace/inkscape-potrace.cpp index 2f4dc7a6f..6583fb735 100644 --- a/src/trace/potrace/inkscape-potrace.cpp +++ b/src/trace/potrace/inkscape-potrace.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "trace/filterset.h" #include "trace/quantize.h" @@ -31,7 +32,7 @@ #include "curve.h" #include "bitmap.h" - +using Glib::ustring; static void updateGui() { @@ -57,6 +58,12 @@ static void potraceStatusCallback(double /*progress*/, void *userData) /* callba } +namespace { +ustring twohex( int value ) +{ + return ustring::format(std::hex, std::setfill(L'0'), std::setw(2), value); +} +} // namespace //required by potrace @@ -471,67 +478,56 @@ PotraceTracingEngine::traceGrayMap(GrayMap *grayMap) /** * Called for multiple-scanning algorithms */ -std::vector -PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf) +std::vector PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf) { - std::vector results; - if (!thePixbuf) - return results; - - double low = 0.2; //bottom of range - double high = 0.9; //top of range - double delta = (high - low ) / ((double)multiScanNrColors); - - brightnessFloor = 0.0; //Set bottom to black - - int traceCount = 0; + if ( thePixbuf ) { + double low = 0.2; //bottom of range + double high = 0.9; //top of range + double delta = (high - low ) / ((double)multiScanNrColors); - for ( brightnessThreshold = low ; - brightnessThreshold <= high ; - brightnessThreshold += delta) + brightnessFloor = 0.0; //Set bottom to black - { - - GrayMap *grayMap = filter(*this, thePixbuf); - if (!grayMap) - return results; - - long nodeCount; - std::string d = grayMapToPath(grayMap, &nodeCount); + int traceCount = 0; - grayMap->destroy(grayMap); + for ( brightnessThreshold = low ; + brightnessThreshold <= high ; + brightnessThreshold += delta) { + GrayMap *grayMap = filter(*this, thePixbuf); + if ( grayMap ) { + long nodeCount; + std::string d = grayMapToPath(grayMap, &nodeCount); - if (d.size() == 0) - return results; + grayMap->destroy(grayMap); - int grayVal = (int)(256.0 * brightnessThreshold); - char style[31]; - sprintf(style, "fill-opacity:1.0;fill:#%02x%02x%02x", - grayVal, grayVal, grayVal); + if ( !d.empty() ) { + //### get style info + int grayVal = (int)(256.0 * brightnessThreshold); + ustring style = ustring::compose("fill-opacity:1.0;fill:%1%2%3", twohex(grayVal), twohex(grayVal), twohex(grayVal) ); - //g_message("### GOT '%s' \n", d); - TracingEngineResult result(style, d, nodeCount); - results.push_back(result); + //g_message("### GOT '%s' \n", style.c_str()); + TracingEngineResult result(style, d, nodeCount); + results.push_back(result); - if (!multiScanStack) - brightnessFloor = brightnessThreshold; + if (!multiScanStack) { + brightnessFloor = brightnessThreshold; + } - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop) - { - gchar *msg = g_strdup_printf(_("Trace: %d. %ld nodes"), traceCount++, nodeCount); - sp_desktop_message_stack(desktop)->flash(Inkscape::NORMAL_MESSAGE, msg); - g_free(msg); + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if (desktop) { + ustring msg = ustring::compose(_("Trace: %1. %2 nodes"), traceCount++, nodeCount); + sp_desktop_message_stack(desktop)->flash(Inkscape::NORMAL_MESSAGE, msg); + } + } } } - //# Remove the bottom-most scan, if requested - if (results.size() > 1 && multiScanRemoveBackground) - { - results.erase(results.end() - 1); + //# Remove the bottom-most scan, if requested + if (results.size() > 1 && multiScanRemoveBackground) { + results.erase(results.end() - 1); } + } return results; } @@ -540,77 +536,64 @@ PotraceTracingEngine::traceBrightnessMulti(GdkPixbuf * thePixbuf) /** * Quantization */ -std::vector -PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf) +std::vector PotraceTracingEngine::traceQuant(GdkPixbuf * thePixbuf) { - std::vector results; - if (!thePixbuf) - return results; - - IndexedMap *iMap = filterIndexed(*this, thePixbuf); - if (!iMap) - return results; - - //Create and clear a gray map - GrayMap *gm = GrayMapCreate(iMap->width, iMap->height); - for (int row=0 ; rowheight ; row++) - for (int col=0 ; colwidth ; col++) - gm->setPixel(gm, col, row, GRAYMAP_WHITE); - - - for (int colorIndex=0 ; colorIndexnrColors ; colorIndex++) - { - - /*Make a gray map for each color index */ - for (int row=0 ; rowheight ; row++) - { - for (int col=0 ; colwidth ; col++) - { - int indx = (int) iMap->getPixel(iMap, col, row); - if (indx == colorIndex) - gm->setPixel(gm, col, row, GRAYMAP_BLACK); //black - else if (!multiScanStack) - gm->setPixel(gm, col, row, GRAYMAP_WHITE); //white + if (thePixbuf) { + IndexedMap *iMap = filterIndexed(*this, thePixbuf); + if ( iMap ) { + //Create and clear a gray map + GrayMap *gm = GrayMapCreate(iMap->width, iMap->height); + for (int row=0 ; rowheight ; row++) { + for (int col=0 ; colwidth ; col++) { + gm->setPixel(gm, col, row, GRAYMAP_WHITE); } } - //## Now we have a traceable graymap - long nodeCount; - std::string d = grayMapToPath(gm, &nodeCount); - - if (d.size() == 0) - return results; + for (int colorIndex=0 ; colorIndexnrColors ; colorIndex++) { + // Make a gray map for each color index + for (int row=0 ; rowheight ; row++) { + for (int col=0 ; colwidth ; col++) { + int indx = (int) iMap->getPixel(iMap, col, row); + if (indx == colorIndex) { + gm->setPixel(gm, col, row, GRAYMAP_BLACK); //black + } else if (!multiScanStack) { + gm->setPixel(gm, col, row, GRAYMAP_WHITE); //white + } + } + } - //### get style info - char style[13]; - RGB rgb = iMap->clut[colorIndex]; - sprintf(style, "fill:#%02x%02x%02x", rgb.r, rgb.g, rgb.b); + //## Now we have a traceable graymap + long nodeCount; + std::string d = grayMapToPath(gm, &nodeCount); - //g_message("### GOT '%s' \n", d); - TracingEngineResult result(style, d, nodeCount); - results.push_back(result); + if ( !d.empty() ) { + //### get style info + RGB rgb = iMap->clut[colorIndex]; + ustring style = ustring::compose("fill:#%1%2%3", twohex(rgb.r), twohex(rgb.g), twohex(rgb.b) ); - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if (desktop) - { - gchar *msg = g_strdup_printf(_("Trace: %d. %ld nodes"), colorIndex, nodeCount); - sp_desktop_message_stack(desktop)->flash(Inkscape::NORMAL_MESSAGE, msg); - g_free(msg); - } + //g_message("### GOT '%s' \n", style.c_str()); + TracingEngineResult result(style, d, nodeCount); + results.push_back(result); + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if (desktop) { + ustring msg = ustring::compose(_("Trace: %1. %2 nodes"), colorIndex, nodeCount); + sp_desktop_message_stack(desktop)->flash(Inkscape::NORMAL_MESSAGE, msg); + } + } + }// for colorIndex - }// for colorIndex - - gm->destroy(gm); - iMap->destroy(iMap); + gm->destroy(gm); + iMap->destroy(iMap); + } - //# Remove the bottom-most scan, if requested - if (results.size() > 1 && multiScanRemoveBackground) - { - results.erase(results.end() - 1); + //# Remove the bottom-most scan, if requested + if (results.size() > 1 && multiScanRemoveBackground) { + results.erase(results.end() - 1); } + } return results; } @@ -667,3 +650,13 @@ PotraceTracingEngine::abort() } // namespace Trace } // namespace Inkscape +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : diff --git a/src/trace/potrace/inkscape-potrace.h b/src/trace/potrace/inkscape-potrace.h index b32ab6461..5ed0c0e5a 100644 --- a/src/trace/potrace/inkscape-potrace.h +++ b/src/trace/potrace/inkscape-potrace.h @@ -286,3 +286,13 @@ class PotraceTracingEngine : public TracingEngine #endif //__INKSCAPE_POTRACE_H__ +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : -- cgit v1.2.3