1 #pragma once 2 3 #ifndef NAA2TLVCONVERTER_H 4 #define NAA2TLVCONVERTER_H 5 6 #include "tcommon.h" 7 #include "tpixel.h" 8 #include "ttoonzimage.h" 9 #include "tvectorimage.h" 10 11 #include <QPoint> 12 #include <QString> 13 #include <QMap> 14 #include <QList> 15 #include <QVector> 16 17 #undef DVAPI 18 #undef DVVAR 19 #ifdef TOONZLIB_EXPORTS 20 #define DVAPI DV_EXPORT_API 21 #define DVVAR DV_EXPORT_VAR 22 #else 23 #define DVAPI DV_IMPORT_API 24 #define DVVAR DV_IMPORT_VAR 25 #endif 26 27 template <class T> 28 class WorkRaster { 29 public: 30 typedef T Pixel; WorkRaster(int lx,int ly)31 WorkRaster(int lx, int ly) 32 : m_lx(lx), m_ly(ly), m_buffer(new Pixel[lx * ly]) {} 33 getLx()34 inline int getLx() const { return m_lx; } getLy()35 inline int getLy() const { return m_ly; } 36 inline Pixel *pixels(int y = 0) const { return m_buffer.get() + m_lx * y; } 37 38 private: 39 std::unique_ptr<Pixel[]> m_buffer; 40 int m_lx, m_ly; 41 }; 42 43 struct DVAPI RegionInfo { 44 int colorIndex; 45 int pixelCount; 46 QMap<int, int> links; // region -> link strength (i.e. number of connections) 47 QList<int> boundaries; 48 QMap<int, int> thicknessHistogram; 49 double thickness; 50 int perimeter; 51 int inkBoundary; 52 double disc; 53 double param1; 54 enum Type { 55 Unknown = 0x0, 56 Background = 0x1, 57 Ink = 0x2, 58 SyntheticInk = (0x100 | 0x2), // generated) ink between paint regions 59 MainInk = (0x200 | 0x2), // ink adjacent to background region 60 ThinInk = (0x1000 | 0x2), // ink adjacent to background region 61 Paint = 0x4, 62 LargePaint = (0x400 | 0x4), 63 SmallPaint = (0x800 | 0x4), 64 Unused = 0x8000 // see separateRegions() 65 }; 66 Type type; 67 QPoint pos; // un pixel interno (serve per rintracciare la regione sullo 68 // schermo) 69 int x0, y0, x1, y1; 70 RegionInfoRegionInfo71 RegionInfo() 72 : colorIndex(-1) 73 , pixelCount(-1) 74 , type(Unknown) 75 , thickness(0) 76 , perimeter(0) 77 , inkBoundary(0) 78 , disc(0) 79 , param1(0) 80 , x0(0) 81 , y0(0) 82 , x1(-1) 83 , y1(-1) {} 84 touchRegionRegionInfo85 void touchRegion(int regionId) { 86 QMap<int, int>::Iterator it = links.find(regionId); 87 if (it == links.end()) 88 links.insert(regionId, 1); 89 else 90 it.value() += 1; 91 } 92 isBackgroundRegionInfo93 inline bool isBackground() const { return type == RegionInfo::Background; } isInkRegionInfo94 inline bool isInk() const { return (type & RegionInfo::Ink) != 0; } 95 96 QString getTypeString() const; 97 }; 98 99 class DVAPI Naa2TlvConverter { 100 public: 101 const int MaxColorCount; 102 WorkRaster<unsigned short> *m_regionRas; 103 WorkRaster<unsigned char> *m_borderRas; 104 WorkRaster<unsigned char> *m_dotRas; 105 WorkRaster<unsigned char> *m_syntheticInkRas; 106 QVector<TPixel32> m_colors; 107 QVector<RegionInfo> m_regions; 108 double m_inkThickness; 109 TPalette *m_palette; 110 bool m_valid; 111 112 Naa2TlvConverter(); 113 ~Naa2TlvConverter(); 114 115 void setSourceImage(const TRaster32P &srcRas); 116 void setPalette(TPalette *palette); getPalette()117 TPalette *getPalette() const { return m_palette; } 118 119 void separateRegions(); 120 void computeLinks(); 121 void findRegionBorders(); 122 void erodeRegions(); 123 void computeMainInkThickness(); 124 void assignColorTypes(); 125 void addBorderInks(); 126 void findBackgroundRegions(); 127 void findMainInks(); 128 void findLargePaints(); 129 void findThinInks(); 130 void findPaints(); 131 void findPaints2(); 132 void findThinPaints(); 133 void findSuspectInks(); 134 135 void measureThickness(); 136 137 int measureThickness(int x, int y); 138 139 void process(const TRaster32P &srcRas); 140 getRegionIndex(int x,int y)141 int getRegionIndex(int x, int y) const { 142 if (!!m_regionRas && 0 <= x && x < m_regionRas->getLx() && 0 <= y && 143 y < m_regionRas->getLy()) 144 return m_regionRas->pixels(y)[x]; 145 else 146 return -1; 147 } 148 149 TToonzImageP makeTlv(bool transparentSyntheticInks, QList<int> &usedStyleIds, 150 double dpi = 0.0); 151 152 TVectorImageP vectorize(const TToonzImageP &ti); 153 TVectorImageP vectorize(const TRaster32P &ras); 154 TVectorImageP vectorize(); 155 156 void removeUnusedStyles(const QList<int> &styleIds); 157 }; 158 159 #endif 160