-
Notifications
You must be signed in to change notification settings - Fork 511
/
stagevisitor.cpp
1621 lines (1312 loc) · 54.6 KB
/
stagevisitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// TnzCore includes
#include "tpixelutils.h"
#include "tstroke.h"
#include "tofflinegl.h"
#include "tstencilcontrol.h"
#include "tvectorgl.h"
#include "tvectorrenderdata.h"
#include "tcolorfunctions.h"
#include "tpalette.h"
#include "tropcm.h"
#include "trasterimage.h"
#include "tvectorimage.h"
#include "tmeshimage.h"
#include "tcolorstyles.h"
#include "timage_io.h"
#include "tregion.h"
#include "toonz/toonzscene.h"
// TnzBase includes
#include "tenv.h"
// TnzExt includes
#include "ext/meshutils.h"
#include "ext/plasticskeleton.h"
#include "ext/plasticskeletondeformation.h"
#include "ext/plasticdeformerstorage.h"
// TnzLib includes
#include "toonz/stageplayer.h"
#include "toonz/stage.h"
#include "toonz/stage2.h"
#include "toonz/tcolumnfx.h"
#include "toonz/txsheet.h"
#include "toonz/txshsimplelevel.h"
#include "toonz/txshchildlevel.h"
#include "toonz/txshcolumn.h"
#include "toonz/txshcell.h"
#include "toonz/onionskinmask.h"
#include "toonz/dpiscale.h"
#include "toonz/imagemanager.h"
#include "toonz/tstageobjecttree.h"
#include "toonz/glrasterpainter.h"
#include "toonz/preferences.h"
#include "toonz/fill.h"
#include "toonz/levelproperties.h"
#include "toonz/autoclose.h"
#include "toonz/txshleveltypes.h"
#include "imagebuilders.h"
#include "toonz/tframehandle.h"
#include "toonz/preferences.h"
// Qt includes
#include <QImage>
#include <QPainter>
#include <QPolygon>
#include <QThreadStorage>
#include <QMatrix>
#include <QThread>
#include <QGuiApplication>
#include "toonz/stagevisitor.h"
//**********************************************************************************************
// Stage namespace
//**********************************************************************************************
/*! \namespace Stage
\brief The Stage namespace provides objects, classes and methods useful to
view or display images.
*/
using namespace Stage;
/*! \var Stage::inch
For historical reasons camera stand is defined in a coordinate
system in which
an inch is equal to 'Stage::inch' unit.
Pay attention: modify this value condition apparent line
thickness of
images .pli.
*/
// const double Stage::inch = 53.33333;
//**********************************************************************************************
// Local namespace
//**********************************************************************************************
namespace {
QImage rasterToQImage(const TRaster32P &ras) {
QImage image(ras->getRawData(), ras->getLx(), ras->getLy(),
QImage::Format_ARGB32_Premultiplied);
return image;
}
//----------------------------------------------------------------
QImage rasterToQImage(const TRasterGR8P &ras) {
QImage image(ras->getLx(), ras->getLy(), QImage::Format_ARGB32_Premultiplied);
int lx = ras->getLx(), ly = ras->getLy();
for (int y = 0; y < ly; y++) {
TPixelGR8 *pix = ras->pixels(y);
TPixelGR8 *endPix = pix + lx;
QRgb *outPix = (QRgb *)image.scanLine(y);
for (; pix < endPix; ++pix) {
int value = pix->value;
*outPix++ = qRgba(value, value, value, 255);
}
}
return image;
}
//----------------------------------------------------------------
QImage rasterToQImage(const TRasterP &ras) {
if (TRaster32P src32 = ras)
return rasterToQImage(src32);
else if (TRasterGR8P srcGr8 = ras)
return rasterToQImage(srcGr8);
// assert(!"Cannot use drawImage with this image!");
return QImage();
}
//----------------------------------------------------------------
//! Draw orthogonal projection of \b bbox onto x-axis and y-axis.
void draw3DShadow(const TRectD &bbox, double z, double phi) {
// bruttino assai, ammetto
double a = bigBoxSize[0];
double b = bigBoxSize[1];
glColor3d(0.9, 0.9, 0.86);
glBegin(GL_LINE_STRIP);
glVertex3d(bbox.x0, bbox.y0, z);
glVertex3d(bbox.x0, bbox.y1, z);
glVertex3d(bbox.x1, bbox.y1, z);
glVertex3d(bbox.x1, bbox.y0, z);
glVertex3d(bbox.x0, bbox.y0, z);
glEnd();
double y = -b;
double x = phi >= 0 ? a : -a;
double xm = 0.5 * (bbox.x0 + bbox.x1);
double ym = 0.5 * (bbox.y0 + bbox.y1);
if (bbox.y0 > y) {
glBegin(GL_LINE_STRIP);
glVertex3d(xm, y, z);
glVertex3d(xm, bbox.y0, z);
glEnd();
} else if (bbox.y1 < y) {
glBegin(GL_LINE_STRIP);
glVertex3d(xm, y, z);
glVertex3d(xm, bbox.y1, z);
glEnd();
}
if (bbox.x0 > x) {
glBegin(GL_LINE_STRIP);
glVertex3d(x, ym, z);
glVertex3d(bbox.x0, ym, z);
glEnd();
} else if (bbox.x1 < x) {
glBegin(GL_LINE_STRIP);
glVertex3d(x, ym, z);
glVertex3d(bbox.x1, ym, z);
glEnd();
}
glColor3d(0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
glVertex3d(bbox.x0, -b, z);
glVertex3d(bbox.x1, -b, z);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3d(x, bbox.y0, z);
glVertex3d(x, bbox.y1, z);
glEnd();
}
//=====================================================================
// Plastic function declarations
/*!
Returns from the specified player the stage object to be plastic
deformed - or 0 if current Toonz rules prevent it from being deformed.
*/
TStageObject *plasticDeformedObj(const Stage::Player &player,
const PlasticVisualSettings &pvs);
//! Draws the specified mesh image
void onMeshImage(TMeshImage *mi, const Stage::Player &player,
const ImagePainter::VisualSettings &vs,
const TAffine &viewAff);
//! Applies Plastic deformation of the specified player's stage object.
void onPlasticDeformedImage(TStageObject *playerObj,
const Stage::Player &player,
const ImagePainter::VisualSettings &vs,
const TAffine &viewAff);
} // namespace
//**********************************************************************************************
// Picker implementation
//**********************************************************************************************
Picker::Picker(const TAffine &viewAff, const TPointD &point,
const ImagePainter::VisualSettings &vs, int devPixRatio)
: Visitor(vs)
, m_viewAff(viewAff)
, m_point(point)
, m_columnIndexes()
, m_minDist2(25.0)
, m_devPixRatio(devPixRatio) {}
//-----------------------------------------------------------------------------
void Picker::setMinimumDistance(double d) {
m_minDist2 = (double)(m_devPixRatio * m_devPixRatio) * d * d;
}
//-----------------------------------------------------------------------------
void Picker::onImage(const Stage::Player &player) {
// if m_currentColumnIndex is other than the default value (-1),
// then pick only the current column.
if (m_currentColumnIndex != -1 &&
m_currentColumnIndex != player.m_ancestorColumnIndex)
return;
bool picked = false;
TAffine aff = m_viewAff * player.m_placement;
TPointD point = aff.inv() * m_point;
const TImageP &img = player.image();
if (TVectorImageP vi = img) {
double w = 0;
UINT strokeIndex = 0;
double dist2 = 0;
TRegion *r = vi->getRegion(point);
int styleId = 0;
if (r) styleId = r->getStyle();
if (styleId != 0)
picked = true;
else if (vi->getNearestStroke(point, w, strokeIndex, dist2)) {
dist2 *= aff.det();
TStroke *stroke = vi->getStroke(strokeIndex);
TThickPoint thickPoint = stroke->getThickPoint(w);
double len2 = thickPoint.thick * thickPoint.thick * aff.det();
double checkDist = std::max(m_minDist2, len2);
if (dist2 < checkDist) picked = true;
}
} else if (TRasterImageP ri = img) {
TRaster32P ras = ri->getRaster();
if (!ras) return;
ras->lock();
TPointD pp = player.m_dpiAff.inv() * point + ras->getCenterD();
TPoint p(tround(pp.x), tround(pp.y));
if (!ras->getBounds().contains(p)) return;
TPixel32 *pix = ras->pixels(p.y);
if (pix[p.x].m != 0) picked = true;
TAffine aff2 = (aff * player.m_dpiAff).inv();
TPointD pa(p.x, p.y);
TPointD dx = aff2 * (m_point + TPointD(3, 0)) - aff2 * m_point;
TPointD dy = aff2 * (m_point + TPointD(0, 3)) - aff2 * m_point;
double rx = dx.x * dx.x + dx.y * dx.y;
double ry = dy.x * dy.x + dy.y * dy.y;
int radius = tround(sqrt(rx > ry ? rx : ry));
TRect rect = TRect(p.x - radius, p.y - radius, p.x + radius, p.y + radius) *
ras->getBounds();
for (int y = rect.y0; !picked && y <= rect.y1; y++) {
pix = ras->pixels(y);
for (int x = rect.x0; !picked && x <= rect.x1; x++)
if (pix[x].m != 0) picked = true;
}
ras->unlock();
} else if (TToonzImageP ti = img) {
TRasterCM32P ras = ti->getRaster();
if (!ras) return;
ras->lock();
TPointD pp = player.m_dpiAff.inv() * point + ras->getCenterD();
TPoint p(tround(pp.x), tround(pp.y));
if (!ras->getBounds().contains(p)) return;
TPixelCM32 *pix = ras->pixels(p.y) + p.x;
if (!pix->isPurePaint() || pix->getPaint() != 0) picked = true;
ras->unlock();
}
if (picked) {
int columnIndex = player.m_ancestorColumnIndex;
if (m_columnIndexes.empty() || m_columnIndexes.back() != columnIndex)
m_columnIndexes.push_back(columnIndex);
int row = player.m_frame;
if (m_rows.empty() || m_rows.back() != row) m_rows.push_back(row);
}
}
//-----------------------------------------------------------------------------
void Picker::beginMask() {}
//-----------------------------------------------------------------------------
void Picker::endMask() {}
//-----------------------------------------------------------------------------
void Picker::enableMask() {}
//-----------------------------------------------------------------------------
void Picker::disableMask() {}
//-----------------------------------------------------------------------------
int Picker::getColumnIndex() const {
if (m_columnIndexes.empty())
return -1;
else
return m_columnIndexes.back();
}
//-----------------------------------------------------------------------------
void Picker::getColumnIndexes(std::vector<int> &indexes) const {
indexes = m_columnIndexes;
}
//-----------------------------------------------------------------------------
int Picker::getRow() const {
if (m_rows.empty())
return -1;
else
return m_rows.back();
}
//**********************************************************************************************
// RasterPainter implementation
//**********************************************************************************************
RasterPainter::RasterPainter(const TDimension &dim, const TAffine &viewAff,
const TRect &rect,
const ImagePainter::VisualSettings &vs,
bool checkFlags)
: Visitor(vs)
, m_dim(dim)
, m_viewAff(viewAff)
, m_clipRect(rect)
, m_maskLevel(0)
, m_singleColumnEnabled(false)
, m_checkFlags(checkFlags)
, m_doRasterDarkenBlendedView(false) {}
//-----------------------------------------------------------------------------
//! Utilizzato solo per TAB Pro
void RasterPainter::beginMask() {
flushRasterImages(); // per evitare che venga fatto dopo il beginMask
++m_maskLevel;
TStencilControl::instance()->beginMask();
}
//! Utilizzato solo per TAB Pro
void RasterPainter::endMask() {
flushRasterImages(); // se ci sono delle immagini raster nella maschera
// devono uscire ora
--m_maskLevel;
TStencilControl::instance()->endMask();
}
//! Utilizzato solo per TAB Pro
void RasterPainter::enableMask() {
TStencilControl::instance()->enableMask(TStencilControl::SHOW_INSIDE);
}
//! Utilizzato solo per TAB Pro
void RasterPainter::disableMask() {
flushRasterImages(); // se ci sono delle immagini raster mascherate devono
// uscire ora
TStencilControl::instance()->disableMask();
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
TEnv::DoubleVar AutocloseDistance("InknpaintAutocloseDistance", 10.0);
TEnv::DoubleVar AutocloseAngle("InknpaintAutocloseAngle", 60.0);
TEnv::IntVar AutocloseInk("InknpaintAutocloseInk", 1);
TEnv::IntVar AutocloseOpacity("InknpaintAutocloseOpacity", 255);
//-----------------------------------------------------------------------------
int RasterPainter::getNodesCount() { return m_nodes.size(); }
//-----------------------------------------------------------------------------
void RasterPainter::clearNodes() { m_nodes.clear(); }
//-----------------------------------------------------------------------------
TRasterP RasterPainter::getRaster(int index, QMatrix &matrix) {
if ((int)m_nodes.size() <= index) return TRasterP();
if (m_nodes[index].m_onionMode != Node::eOnionSkinNone) return TRasterP();
if (m_nodes.empty()) return TRasterP();
double delta = sqrt(fabs(m_nodes[0].m_aff.det()));
TRectD bbox = m_nodes[0].m_bbox.enlarge(delta);
int i;
for (i = 1; i < (int)m_nodes.size(); i++) {
delta = sqrt(fabs(m_nodes[i].m_aff.det()));
bbox += m_nodes[i].m_bbox.enlarge(delta);
}
TRect rect(tfloor(bbox.x0), tfloor(bbox.y0), tceil(bbox.x1), tceil(bbox.y1));
rect = rect * TRect(0, 0, m_dim.lx - 1, m_dim.ly - 1);
TAffine aff = TTranslation(-rect.x0, -rect.y0) * m_nodes[index].m_aff;
matrix = QMatrix(aff.a11, aff.a21, aff.a12, aff.a22, aff.a13, aff.a23);
return m_nodes[index].m_raster;
}
//-----------------------------------------------------------------------------
/*! Make frame visualization.
\n If onon-skin is active, create a new raster with dimension containing
all
frame with onion-skin; recall \b TRop::quickPut with argument
each frame
with onion-skin and new raster. If onion-skin is not active
recall
\b TRop::quickPut with argument current frame and new raster.
*/
namespace {
QThreadStorage<std::vector<char> *> threadBuffers;
}
void RasterPainter::flushRasterImages() {
if (m_nodes.empty()) return;
// Build nodes bbox union
double delta = sqrt(fabs(m_nodes[0].m_aff.det()));
TRectD bbox = m_nodes[0].m_bbox.enlarge(delta);
int i, nodesCount = m_nodes.size();
for (i = 1; i < nodesCount; ++i) {
delta = sqrt(fabs(m_nodes[i].m_aff.det()));
bbox += m_nodes[i].m_bbox.enlarge(delta);
}
TRect rect(tfloor(bbox.x0), tfloor(bbox.y0), tceil(bbox.x1), tceil(bbox.y1));
rect = rect * TRect(0, 0, m_dim.lx - 1, m_dim.ly - 1);
int lx = rect.getLx(), ly = rect.getLy();
TDimension dim(lx, ly);
// this is needed since a stop motion live view
// doesn't register as a node correctly
// there is probably a better way to do this.
if (rect.getLx() == 0 && lx == 0) {
rect = m_clipRect;
dim = m_dim;
}
// Build a raster buffer of sufficient size to hold said union.
// The buffer is per-thread cached in order to improve the rendering speed.
if (!threadBuffers.hasLocalData())
threadBuffers.setLocalData(new std::vector<char>());
int size = dim.lx * dim.ly * sizeof(TPixel32);
std::vector<char> *vbuff = (std::vector<char> *)threadBuffers.localData();
if (size > (int)vbuff->size()) vbuff->resize(size);
TRaster32P ras(dim.lx, dim.ly, dim.lx, (TPixel32 *)&(*vbuff)[0]);
TRaster32P ras2;
if (m_vs.m_colorMask != 0) {
ras2 = TRaster32P(ras->getSize());
ras->clear();
} else
ras2 = ras;
// Clear the buffer - it will hold all the stacked nodes content to be overed
// on top of the OpenGL buffer through a glDrawPixel()
ras->lock();
ras->clear(); // ras is typically reused - and we need it transparent first
TRect r = rect - rect.getP00();
TRaster32P viewedRaster = ras->extract(r);
int current = -1;
// Retrieve preferences-related data
int tc = m_checkFlags ? ToonzCheck::instance()->getChecks() : 0;
int index = ToonzCheck::instance()->getColorIndex();
TPixel32 frontOnionColor, backOnionColor;
bool onionInksOnly;
Preferences::instance()->getOnionData(frontOnionColor, backOnionColor,
onionInksOnly);
// Stack every node on top of the raster buffer
for (i = 0; i < nodesCount; ++i) {
if (m_nodes[i].m_isCurrentColumn) current = i;
TAffine aff = TTranslation(-rect.x0, -rect.y0) * m_nodes[i].m_aff;
TDimension imageDim = m_nodes[i].m_raster->getSize();
TPointD offset(0.5, 0.5);
aff *= TTranslation(offset); // very quick and very dirty fix: in
// camerastand the images seems shifted of an
// half pixel...it's a quickput approximation?
TPixel32 colorscale = TPixel32(0, 0, 0, m_nodes[i].m_alpha);
int inksOnly;
if (m_nodes[i].m_onionMode != Node::eOnionSkinNone) {
inksOnly = onionInksOnly;
if (m_nodes[i].m_onionMode == Node::eOnionSkinFront)
colorscale = TPixel32(frontOnionColor.r, frontOnionColor.g,
frontOnionColor.b, m_nodes[i].m_alpha);
else if (m_nodes[i].m_onionMode == Node::eOnionSkinBack)
colorscale = TPixel32(backOnionColor.r, backOnionColor.g,
backOnionColor.b, m_nodes[i].m_alpha);
} else {
if (m_nodes[i].m_filterColor != TPixel32::Black) {
colorscale = m_nodes[i].m_filterColor;
colorscale.m = m_nodes[i].m_alpha;
}
inksOnly = tc & ToonzCheck::eInksOnly;
}
if (TRaster32P src32 = m_nodes[i].m_raster)
TRop::quickPut(viewedRaster, src32, aff, colorscale,
m_nodes[i].m_doPremultiply, m_nodes[i].m_whiteTransp,
m_nodes[i].m_isFirstColumn, m_doRasterDarkenBlendedView);
else if (TRasterGR8P srcGr8 = m_nodes[i].m_raster)
TRop::quickPut(viewedRaster, srcGr8, aff, colorscale);
else if (TRasterCM32P srcCm = m_nodes[i].m_raster) {
assert(m_nodes[i].m_palette);
int oldframe = m_nodes[i].m_palette->getFrame();
m_nodes[i].m_palette->setFrame(m_nodes[i].m_frame);
TPaletteP plt;
int styleIndex = -1;
if ((tc & ToonzCheck::eGap || tc & ToonzCheck::eAutoclose) &&
m_nodes[i].m_isCurrentColumn) {
srcCm = srcCm->clone();
plt = m_nodes[i].m_palette->clone();
styleIndex = plt->addStyle(TPixel::Magenta);
if (tc & ToonzCheck::eAutoclose)
TAutocloser(srcCm, AutocloseDistance, AutocloseAngle, styleIndex,
AutocloseOpacity)
.exec();
if (tc & ToonzCheck::eGap)
AreaFiller(srcCm).rectFill(m_nodes[i].m_savebox, 1, true, true,
false);
} else
plt = m_nodes[i].m_palette;
if (tc == 0 || tc == ToonzCheck::eBlackBg ||
!m_nodes[i].m_isCurrentColumn)
TRop::quickPut(viewedRaster, srcCm, plt, aff, colorscale, inksOnly);
else {
TRop::CmappedQuickputSettings settings;
settings.m_globalColorScale = colorscale;
settings.m_inksOnly = inksOnly;
settings.m_transparencyCheck =
tc & (ToonzCheck::eTransparency | ToonzCheck::eGap);
settings.m_blackBgCheck = tc & ToonzCheck::eBlackBg;
/*-- InkCheck, Ink#1Check, PaintCheckはカレントカラムにのみ有効 --*/
settings.m_inkIndex =
m_nodes[i].m_isCurrentColumn
? (tc & ToonzCheck::eInk ? index
: (tc & ToonzCheck::eInk1 ? 1 : -1))
: -1;
settings.m_paintIndex = m_nodes[i].m_isCurrentColumn
? (tc & ToonzCheck::ePaint ? index : -1)
: -1;
Preferences::instance()->getTranspCheckData(
settings.m_transpCheckBg, settings.m_transpCheckInk,
settings.m_transpCheckPaint);
settings.m_isOnionSkin = m_nodes[i].m_onionMode != Node::eOnionSkinNone;
settings.m_gapCheckIndex = styleIndex;
TRop::quickPut(viewedRaster, srcCm, plt, aff, settings);
}
srcCm = TRasterCM32P();
plt = TPaletteP();
m_nodes[i].m_palette->setFrame(oldframe);
} else
assert(!"Cannot use quickput with this raster combination!");
}
if (m_vs.m_colorMask != 0) {
TRop::setChannel(ras, ras, m_vs.m_colorMask, false);
TRop::quickPut(ras2, ras, TAffine());
}
// Now, output the raster buffer on top of the OpenGL buffer
glPushAttrib(GL_COLOR_BUFFER_BIT); // Preserve blending and stuff
glEnable(GL_BLEND);
glBlendFunc(GL_ONE,
GL_ONE_MINUS_SRC_ALPHA); // The raster buffer is intended in
// premultiplied form - thus the GL_ONE on src
glDisable(GL_DEPTH_TEST);
glDisable(GL_DITHER);
glDisable(GL_LOGIC_OP);
/* disable, since these features are never enabled, and cause OpenGL to assert
* on systems that don't support them: see #591 */
#if 0
#ifdef GL_EXT_convolution
if( GLEW_EXT_convolution ) {
glDisable(GL_CONVOLUTION_1D_EXT);
glDisable(GL_CONVOLUTION_2D_EXT);
glDisable(GL_SEPARABLE_2D_EXT);
}
#endif
#ifdef GL_EXT_histogram
if( GLEW_EXT_histogram ) {
glDisable(GL_HISTOGRAM_EXT);
glDisable(GL_MINMAX_EXT);
}
#endif
#endif
#ifdef GL_EXT_texture3D
if (GL_EXT_texture3D) {
glDisable(GL_TEXTURE_3D_EXT);
}
#endif
glPushMatrix();
glLoadIdentity();
glRasterPos2d(rect.x0, rect.y0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glDrawPixels(ras2->getLx(), ras2->getLy(), // Perform the over
TGL_FMT, TGL_TYPE, ras2->getRawData()); //
ras->unlock();
glPopMatrix();
glPopAttrib(); // Restore blending status
if (m_vs.m_showBBox && current > -1) {
glPushMatrix();
glLoadIdentity();
tglColor(TPixel(200, 200, 200));
tglMultMatrix(m_nodes[current].m_aff);
tglDrawRect(m_nodes[current].m_raster->getBounds());
glPopMatrix();
}
m_nodes.clear();
}
//-----------------------------------------------------------------------------
/*! Make frame visualization in QPainter.
\n Draw in painter mode just raster image in m_nodes.
\n Onon-skin or channel mode are not considered.
*/
void RasterPainter::drawRasterImages(QPainter &p, QPolygon cameraPol) {
if (m_nodes.empty()) return;
double delta = sqrt(fabs(m_nodes[0].m_aff.det()));
TRectD bbox = m_nodes[0].m_bbox.enlarge(delta);
int i;
for (i = 1; i < (int)m_nodes.size(); i++) {
delta = sqrt(fabs(m_nodes[i].m_aff.det()));
bbox += m_nodes[i].m_bbox.enlarge(delta);
}
TRect rect(tfloor(bbox.x0), tfloor(bbox.y0), tceil(bbox.x1), tceil(bbox.y1));
rect = rect * TRect(0, 0, m_dim.lx - 1, m_dim.ly - 1);
TRect r = rect - rect.getP00();
TAffine flipY(1, 0, 0, 0, -1, m_dim.ly);
p.setClipRegion(QRegion(cameraPol));
for (i = 0; i < (int)m_nodes.size(); i++) {
if (m_nodes[i].m_onionMode != Node::eOnionSkinNone) continue;
p.resetTransform();
TRasterP ras = m_nodes[i].m_raster;
TAffine aff = TTranslation(-rect.x0, -rect.y0) * flipY * m_nodes[i].m_aff;
QMatrix matrix(aff.a11, aff.a21, aff.a12, aff.a22, aff.a13, aff.a23);
QImage image = rasterToQImage(ras);
if (image.isNull()) continue;
p.setMatrix(matrix);
p.drawImage(rect.getP00().x, rect.getP00().y, image);
}
p.resetTransform();
m_nodes.clear();
}
static void buildAutocloseImage(
TVectorImage *vaux, TVectorImage *vi,
const std::vector<std::pair<int, double>> &startPoints,
const std::vector<std::pair<int, double>> &endPoints) {
for (UINT i = 0; i < startPoints.size(); i++) {
TThickPoint p1 = vi->getStroke(startPoints[i].first)
->getThickPoint(startPoints[i].second);
TThickPoint p2 =
vi->getStroke(endPoints[i].first)->getThickPoint(endPoints[i].second);
std::vector<TThickPoint> points(3);
points[0] = p1;
points[1] = 0.5 * (p1 + p2);
points[2] = p2;
points[0].thick = points[1].thick = points[2].thick = 0.0;
TStroke *auxStroke = new TStroke(points);
auxStroke->setStyle(2);
vaux->addStroke(auxStroke);
}
}
TEnv::DoubleVar AutocloseFactor("InknpaintAutocloseFactor", 4.0);
static void drawAutocloses(TVectorImage *vi, TVectorRenderData &rd) {
static TPalette *plt = 0;
if (!plt) {
plt = new TPalette();
plt->addStyle(TPixel::Magenta);
}
std::vector<std::pair<int, double>> startPoints, endPoints;
getClosingPoints(vi->getBBox(), AutocloseFactor, vi, startPoints, endPoints);
TVectorImage *vaux = new TVectorImage();
rd.m_palette = plt;
buildAutocloseImage(vaux, vi, startPoints, endPoints);
// temporarily disable fill check, to preserve the gap indicator color
bool tCheckEnabledOriginal = rd.m_tcheckEnabled;
rd.m_tcheckEnabled = false;
// draw
tglDraw(rd, vaux);
// restore original value
rd.m_tcheckEnabled = tCheckEnabledOriginal;
delete vaux;
}
//-----------------------------------------------------------------------------
/*! Take image from \b Stage::Player \b data and recall the right method for
this kind of image, for vector image recall \b onVectorImage(),
for raster
image recall \b onRasterImage() for toonz image recall \b
onToonzImage().
*/
void RasterPainter::onImage(const Stage::Player &player) {
if (m_singleColumnEnabled && !player.m_isCurrentColumn) return;
// Attempt Plastic-deformed drawing
// For now generating icons of plastic-deformed image causes crash as
// QOffscreenSurface is created outside the gui thread.
// As a quick workaround, ignore the deformation if this is called from
// non-gui thread (i.e. icon generator thread)
// 12/1/2018 Now the scene icon is rendered without deformation either
// since it causes unknown error with opengl contexts..
TStageObject *obj =
::plasticDeformedObj(player, m_vs.m_plasticVisualSettings);
if (obj && QThread::currentThread() == qGuiApp->thread() &&
!m_vs.m_forSceneIcon) {
flushRasterImages();
::onPlasticDeformedImage(obj, player, m_vs, m_viewAff);
} else {
// Common image draw
const TImageP &img = player.image();
if (TVectorImageP vi = img)
onVectorImage(vi.getPointer(), player);
else if (TRasterImageP ri = img)
onRasterImage(ri.getPointer(), player);
else if (TToonzImageP ti = img)
onToonzImage(ti.getPointer(), player);
else if (TMeshImageP mi = img) {
flushRasterImages();
::onMeshImage(mi.getPointer(), player, m_vs, m_viewAff);
}
}
}
//-----------------------------------------------------------------------------
/*! View a vector cell images.
\n If onion-skin is active compute \b TOnionFader value.
Create and boot a \b TVectorRenderData and recall \b tglDraw().
*/
void RasterPainter::onVectorImage(TVectorImage *vi,
const Stage::Player &player) {
flushRasterImages();
// When loaded, vectorimages needs to have regions recomputed, but doing that
// while loading them
// is quite slow (think about loading whole scenes!..). They are recomputed
// the first time they
// are selected and shown on screen...except when playing back, to avoid
// slowness!
// (Daniele) This function should *NOT* be responsible of that.
// It's the *image itself* that should recalculate or initialize
// said data
// if queried about it and turns out it's not available...
if (!player.m_isPlaying && player.m_isCurrentColumn)
vi->recomputeRegionsIfNeeded();
const Preferences &prefs = *Preferences::instance();
TColorFunction *cf = 0, *guidedCf = 0;
TPalette *vPalette = vi->getPalette();
TPixel32 bgColor = TPixel32::White;
int tc = (m_checkFlags && player.m_isCurrentColumn)
? ToonzCheck::instance()->getChecks()
: 0;
bool inksOnly = tc & ToonzCheck::eInksOnly;
int oldFrame = vPalette->getFrame();
vPalette->setFrame(player.m_frame);
if (player.m_onionSkinDistance != c_noOnionSkin) {
TPixel32 frontOnionColor, backOnionColor;
if (player.m_onionSkinDistance != 0 &&
(!player.m_isShiftAndTraceEnabled ||
Preferences::instance()->areOnionColorsUsedForShiftAndTraceGhosts())) {
prefs.getOnionData(frontOnionColor, backOnionColor, inksOnly);
bgColor =
(player.m_onionSkinDistance < 0) ? backOnionColor : frontOnionColor;
}
double m[4] = {1.0, 1.0, 1.0, 1.0}, c[4];
// Weighted addition to RGB and matte multiplication
m[3] = 1.0 -
((player.m_onionSkinDistance == 0)
? 0.1
: OnionSkinMask::getOnionSkinFade(player.m_onionSkinDistance));
c[0] = (1.0 - m[3]) * bgColor.r, c[1] = (1.0 - m[3]) * bgColor.g,
c[2] = (1.0 - m[3]) * bgColor.b;
c[3] = 0.0;
cf = new TGenericColorFunction(m, c);
} else if (player.m_filterColor != TPixel::Black) {
TPixel32 colorScale = player.m_filterColor;
colorScale.m = player.m_opacity;
cf = new TColumnColorFilterFunction(colorScale);
} else if (player.m_opacity < 255)
cf = new TTranspFader(player.m_opacity / 255.0);
TVectorRenderData rd(m_viewAff * player.m_placement, TRect(), vPalette, cf,
true // alpha enabled
);
rd.m_drawRegions = !inksOnly;
rd.m_inkCheckEnabled = tc & ToonzCheck::eInk;
rd.m_ink1CheckEnabled = tc & ToonzCheck::eInk1;
rd.m_paintCheckEnabled = tc & ToonzCheck::ePaint;
rd.m_blackBgEnabled = tc & ToonzCheck::eBlackBg;
rd.m_colorCheckIndex = ToonzCheck::instance()->getColorIndex();
rd.m_show0ThickStrokes = prefs.getShow0ThickLines();
rd.m_regionAntialias = prefs.getRegionAntialias();
rd.m_animatedGuidedDrawing = prefs.getAnimatedGuidedDrawing();
if (player.m_onionSkinDistance != 0 &&
(player.m_isCurrentColumn || player.m_isCurrentXsheetLevel)) {
if (player.m_isGuidedDrawingEnabled == 3 // show guides on all
|| (player.m_isGuidedDrawingEnabled == 1 && // show guides on closest
(player.m_onionSkinDistance == player.m_firstBackOnionSkin ||
player.m_onionSkinDistance == player.m_firstFrontOnionSkin)) ||
(player.m_isGuidedDrawingEnabled == 2 && // show guides on farthest
(player.m_onionSkinDistance == player.m_onionSkinBackSize ||
player.m_onionSkinDistance == player.m_onionSkinFrontSize)) ||
(player.m_isEditingLevel && // fix for level editing mode sending extra
// players
player.m_isGuidedDrawingEnabled == 2 &&
player.m_onionSkinDistance == player.m_lastBackVisibleSkin)) {
rd.m_showGuidedDrawing = player.m_isGuidedDrawingEnabled > 0;
int currentStrokeCount = 0;
int totalStrokes = vi->getStrokeCount();
TXshSimpleLevel *sl = player.m_sl;
if (sl) {
TImageP image = sl->getFrame(player.m_currentFrameId, false);
TVectorImageP vecImage = image;
if (vecImage) currentStrokeCount = vecImage->getStrokeCount();
if (currentStrokeCount < 0) currentStrokeCount = 0;
if (player.m_guidedFrontStroke != -1 &&
(player.m_onionSkinDistance == player.m_onionSkinFrontSize ||
player.m_onionSkinDistance == player.m_firstFrontOnionSkin))
rd.m_indexToHighlight = player.m_guidedFrontStroke;
else if (player.m_guidedBackStroke != -1 &&
(player.m_onionSkinDistance == player.m_onionSkinBackSize ||
player.m_onionSkinDistance == player.m_firstBackOnionSkin))
rd.m_indexToHighlight = player.m_guidedBackStroke;
else if (currentStrokeCount < totalStrokes)
rd.m_indexToHighlight = currentStrokeCount;
double guidedM[4] = {1.0, 1.0, 1.0, 1.0}, guidedC[4];
TPixel32 bgColor = TPixel32::Blue;
guidedM[3] =
1.0 -
((player.m_onionSkinDistance == 0)
? 0.1
: OnionSkinMask::getOnionSkinFade(player.m_onionSkinDistance));
guidedC[0] = (1.0 - guidedM[3]) * bgColor.r,
guidedC[1] = (1.0 - guidedM[3]) * bgColor.g,
guidedC[2] = (1.0 - guidedM[3]) * bgColor.b;
guidedC[3] = 0.0;
guidedCf = new TGenericColorFunction(guidedM, guidedC);
rd.m_guidedCf = guidedCf;
}
}
}
if (tc & (ToonzCheck::eTransparency | ToonzCheck::eGap)) {
TPixel dummy;
rd.m_tcheckEnabled = true;
if (rd.m_blackBgEnabled)
prefs.getTranspCheckData(rd.m_tCheckInk, dummy, rd.m_tCheckPaint);
else
prefs.getTranspCheckData(dummy, rd.m_tCheckInk, rd.m_tCheckPaint);
}
if (m_vs.m_colorMask != 0) {
glColorMask((m_vs.m_colorMask & TRop::RChan) ? GL_TRUE : GL_FALSE,
(m_vs.m_colorMask & TRop::GChan) ? GL_TRUE : GL_FALSE,
(m_vs.m_colorMask & TRop::BChan) ? GL_TRUE : GL_FALSE, GL_TRUE);
}
TVectorImageP viDelete;
if (tc & ToonzCheck::eGap) {
viDelete = vi->clone();
vi = viDelete.getPointer();
vi->selectFill(vi->getBBox(), 0, 1, true, true, false);
}
TStroke *guidedStroke = 0;
if (m_maskLevel > 0)
tglDrawMask(rd, vi);
else
tglDraw(rd, vi, &guidedStroke);
if (tc & ToonzCheck::eAutoclose) drawAutocloses(vi, rd);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
vPalette->setFrame(oldFrame);
delete cf;
delete guidedCf;
if (guidedStroke) m_guidedStrokes.push_back(guidedStroke);
}
//-----------------------------------------------------
/*! Create a \b Node and put it in \b m_nodes.
*/
void RasterPainter::onRasterImage(TRasterImage *ri,
const Stage::Player &player) {
TRasterP r = ri->getRaster();
TAffine aff;
aff = m_viewAff * player.m_placement * player.m_dpiAff;
aff = TTranslation(m_dim.lx * 0.5, m_dim.ly * 0.5) * aff *
TTranslation(-r->getCenterD() +