2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// The following contains classes and prototypes for most code of centerline
|
|
|
|
// vectorization.
|
2016-03-19 06:57:51 +13:00
|
|
|
#include "tcenterlinevectP.h"
|
|
|
|
|
|
|
|
// TnzLib includes
|
|
|
|
#include "toonz/tcenterlinevectorizer.h"
|
|
|
|
#include "toonz/Naa2TlvConverter.h"
|
|
|
|
|
|
|
|
// TnzCore includes
|
|
|
|
#include "tpalette.h"
|
|
|
|
#include "tcolorstyles.h"
|
|
|
|
#include "trastercm.h"
|
|
|
|
#include "ttoonzimage.h"
|
|
|
|
#include "trasterimage.h"
|
|
|
|
#include "tvectorimage.h"
|
|
|
|
#include "tgeometry.h"
|
|
|
|
#include "tstroke.h"
|
|
|
|
#include "tropcm.h"
|
|
|
|
|
|
|
|
// STD includes
|
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#include <queue>
|
|
|
|
#include <map>
|
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
//*********************************
|
|
|
|
//* Further miscellaneous *
|
|
|
|
//*********************************
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Reduces strokes of image by currConfig->m_reduceThicknessRatio/100
|
|
|
|
inline void reduceThickness(TVectorImageP image, double thicknessRatio) {
|
|
|
|
thicknessRatio *= 0.01;
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
int j;
|
|
|
|
for (i = 0; i < image->getStrokeCount(); ++i) {
|
|
|
|
for (j = 0; j < image->getStroke(i)->getControlPointCount(); ++j) {
|
|
|
|
TThickPoint newCP = image->getStroke(i)->getControlPoint(j);
|
|
|
|
newCP.thick *= thicknessRatio;
|
|
|
|
image->getStroke(i)->setControlPoint(j, newCP);
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Delete Skeleton graphs and list
|
|
|
|
inline void deleteSkeletonList(SkeletonList *skeleton) {
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < skeleton->size(); ++i) delete (*skeleton)[i];
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
delete skeleton;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================================================
|
|
|
|
|
2016-07-14 00:05:06 +12:00
|
|
|
static TVectorImageP copyStrokes(std::vector<TStroke *> &strokes) {
|
2016-06-15 18:43:10 +12:00
|
|
|
unsigned int i;
|
|
|
|
TVectorImageP result = new TVectorImage;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
for (i = 0; i < strokes.size(); ++i)
|
|
|
|
if (strokes[i]->getStyle() >= 0) result->addStroke(strokes[i]);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return result;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
inline TThickPoint randomized(const TThickPoint &P) {
|
|
|
|
TThickPoint Q = P;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
Q.x += ((((double)rand()) / RAND_MAX) - 0.5) * 0.1;
|
|
|
|
Q.y += ((((double)rand()) / RAND_MAX) - 0.5) * 0.1;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return Q;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Give stroke extremities a random shake. May help for region computing.
|
2016-07-14 00:05:06 +12:00
|
|
|
static void randomizeExtremities(TVectorImageP vi) {
|
2016-06-15 18:43:10 +12:00
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < vi->getStrokeCount(); ++i) {
|
|
|
|
TThickPoint P = vi->getStroke(i)->getControlPoint(0);
|
|
|
|
vi->getStroke(i)->setControlPoint(0, randomized(P));
|
|
|
|
int n = vi->getStroke(i)->getControlPointCount();
|
|
|
|
P = vi->getStroke(i)->getControlPoint(n - 1);
|
|
|
|
vi->getStroke(i)->setControlPoint(n - 1, randomized(P));
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Looped frame
|
|
|
|
inline void addFrameStrokes(TVectorImageP vi, TRasterP ras, TPalette *palette) {
|
|
|
|
const double epsThick = 1.0;
|
|
|
|
TStroke *frameStroke;
|
|
|
|
std::vector<TThickPoint> CPs;
|
|
|
|
|
|
|
|
CPs.push_back(TThickPoint(0, 0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx() / 2.0, 0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), 0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), 0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), 0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), ras->getLy() / 2.0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx(), ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(ras->getLx() / 2.0, ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(0, ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(0, ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(0, ras->getLy(), epsThick));
|
|
|
|
CPs.push_back(TThickPoint(0, ras->getLy() / 2.0, epsThick));
|
|
|
|
CPs.push_back(TThickPoint(0, 0, epsThick));
|
|
|
|
|
|
|
|
frameStroke = new TStroke(CPs);
|
|
|
|
frameStroke->setStyle(0);
|
|
|
|
frameStroke->setSelfLoop(true);
|
|
|
|
vi->addStroke(frameStroke);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================================================
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Returns if input stroke is edge boundary of an ink-filled region
|
|
|
|
bool VectorizerCore::isInkRegionEdge(TStroke *stroke) {
|
|
|
|
return stroke->getFlag(SkeletonArc::SS_OUTLINE);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Similar as above
|
|
|
|
bool VectorizerCore::isInkRegionEdgeReversed(TStroke *stroke) {
|
|
|
|
return stroke->getFlag(SkeletonArc::SS_OUTLINE_REVERSED);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void VectorizerCore::clearInkRegionFlags(TVectorImageP vi) {
|
|
|
|
int flag = SkeletonArc::SS_OUTLINE | SkeletonArc::SS_OUTLINE_REVERSED;
|
|
|
|
for (int i = 0; i < (int)vi->getStrokeCount(); i++)
|
|
|
|
vi->getStroke(i)->setFlag(flag, false);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
//************************
|
|
|
|
//* Vectorizer Main *
|
|
|
|
//************************
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
TVectorImageP VectorizerCore::centerlineVectorize(
|
2016-06-15 18:43:10 +12:00
|
|
|
TImageP &image, const CenterlineConfiguration &configuration,
|
|
|
|
TPalette *palette) {
|
|
|
|
TRasterImageP ri = image;
|
|
|
|
TToonzImageP ti = image;
|
|
|
|
|
|
|
|
TRasterP ras;
|
|
|
|
if (ri)
|
|
|
|
ras = ri->getRaster();
|
|
|
|
else {
|
|
|
|
ras = ti->getRaster()->clone();
|
|
|
|
TRop::expandPaint(ras);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (configuration.m_naaSource) {
|
|
|
|
if (TRaster32P ras32 = ras) {
|
|
|
|
Naa2TlvConverter converter;
|
|
|
|
|
|
|
|
converter.process(ras32);
|
|
|
|
converter.setPalette(palette);
|
|
|
|
|
2016-11-09 17:52:45 +13:00
|
|
|
QList<int> dummy;
|
2020-04-12 17:19:20 +12:00
|
|
|
ti = converter.makeTlv(true, dummy);
|
|
|
|
if (ti) // Transparent synthetic inks
|
2016-06-15 18:43:10 +12:00
|
|
|
{
|
|
|
|
image = ti;
|
|
|
|
ras = ti->getRaster();
|
|
|
|
|
|
|
|
TRop::expandPaint(ras);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorizerCoreGlobals globals;
|
|
|
|
globals.currConfig = &configuration;
|
|
|
|
|
|
|
|
Contours polygons;
|
|
|
|
polygonize(ras, polygons, globals);
|
|
|
|
|
|
|
|
// Most time-consuming part of vectorization, 'this' is passed to inform of
|
|
|
|
// partial progresses
|
|
|
|
SkeletonList *skeletons = skeletonize(polygons, this, globals);
|
|
|
|
|
|
|
|
if (isCanceled()) {
|
|
|
|
// Clean and return 0 at cancel command
|
|
|
|
deleteSkeletonList(skeletons);
|
|
|
|
|
|
|
|
return TVectorImageP();
|
|
|
|
}
|
|
|
|
|
|
|
|
organizeGraphs(skeletons, globals);
|
|
|
|
|
|
|
|
// junctionRecovery(polygons); //Da' problemi per maxThickness<inf...
|
|
|
|
// sarebbe da rendere compatibile
|
|
|
|
|
|
|
|
std::vector<TStroke *> sortibleResult;
|
|
|
|
TVectorImageP result;
|
|
|
|
|
|
|
|
calculateSequenceColors(ras, globals); // Extract stroke colors here
|
|
|
|
conversionToStrokes(sortibleResult, globals);
|
|
|
|
applyStrokeColors(sortibleResult, ras, palette,
|
|
|
|
globals); // Strokes get sorted here
|
|
|
|
result = copyStrokes(sortibleResult);
|
|
|
|
|
|
|
|
// Further misc adjustments
|
|
|
|
if (globals.currConfig->m_thicknessRatio < 100)
|
|
|
|
reduceThickness(result, configuration.m_thicknessRatio);
|
|
|
|
if (globals.currConfig->m_maxThickness == 0.0)
|
|
|
|
for (unsigned int i = 0; i < result->getStrokeCount(); ++i)
|
|
|
|
result->getStroke(i)->setSelfLoop(true);
|
|
|
|
if (globals.currConfig->m_makeFrame) addFrameStrokes(result, ras, palette);
|
|
|
|
// randomizeExtremities(result); //Cuccio random - non serve...
|
|
|
|
|
|
|
|
deleteSkeletonList(skeletons);
|
|
|
|
|
|
|
|
return result;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|