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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <stdlib.h>
#include <vcl/bitmapaccess.hxx>
#include <tools/poly.hxx>
#include <vcl/gdimtf.hxx>
#include <vcl/metaact.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/virdev.hxx>
#include <impvect.hxx>
#include <memory>
#define VECT_POLY_MAX 8192
#define VECT_FREE_INDEX 0
#define VECT_CONT_INDEX 1
#define VECT_DONE_INDEX 2
#define VECT_POLY_INLINE_INNER 1UL
#define VECT_POLY_INLINE_OUTER 2UL
#define VECT_POLY_OUTLINE_INNER 4UL
#define VECT_POLY_OUTLINE_OUTER 8UL
#define VECT_MAP( _def_pIn, _def_pOut, _def_nVal ) _def_pOut[_def_nVal]=(_def_pIn[_def_nVal]=((_def_nVal)*4)+1)+5;
#define BACK_MAP( _def_nVal ) ((((_def_nVal)+2)>>2)-1)
#define VECT_PROGRESS( _def_pProgress, _def_nVal ) \
if(_def_pProgress) \
(_def_pProgress->Call(_def_nVal));
class ImplVectMap;
class ImplChain;
namespace ImplVectorizer
{
ImplVectMap* ImplExpand( BitmapReadAccess* pRAcc, const Color& rColor );
void ImplCalculate( ImplVectMap* pMap, tools::PolyPolygon& rPolyPoly, sal_uInt8 cReduce );
bool ImplGetChain( ImplVectMap* pMap, const Point& rStartPt, ImplChain& rChain );
bool ImplIsUp( ImplVectMap* pMap, long nY, long nX );
void ImplLimitPolyPoly( tools::PolyPolygon& rPolyPoly );
}
struct ChainMove { long nDX; long nDY; };
static const ChainMove aImplMove[ 8 ] = {
{ 1, 0 },
{ 0, -1 },
{ -1, 0 },
{ 0, 1 },
{ 1, -1 },
{ -1, -1 },
{ -1, 1 },
{ 1, 1 }
};
static const ChainMove aImplMoveInner[ 8 ] = {
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 },
{ -1, 0 }
};
static const ChainMove aImplMoveOuter[ 8 ] = {
{ 0, -1 },
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 }
};
struct ImplColorSet
{
BitmapColor maColor;
sal_uInt16 mnIndex;
bool mbSet;
};
extern "C" int SAL_CALL ImplColorSetCmpFnc( const void* p1, const void* p2 )
{
ImplColorSet const * pSet1 = static_cast<ImplColorSet const *>(p1);
ImplColorSet const * pSet2 = static_cast<ImplColorSet const *>(p2);
int nRet;
if( pSet1->mbSet && pSet2->mbSet )
{
const sal_uInt8 cLum1 = pSet1->maColor.GetLuminance();
const sal_uInt8 cLum2 = pSet2->maColor.GetLuminance();
nRet = ( ( cLum1 > cLum2 ) ? -1 : ( ( cLum1 == cLum2 ) ? 0 : 1 ) );
}
else if( pSet1->mbSet )
nRet = -1;
else if( pSet2->mbSet )
nRet = 1;
else
nRet = 0;
return nRet;
}
class ImplPointArray
{
Point* mpArray;
sal_uLong mnSize;
sal_uLong mnRealSize;
public:
ImplPointArray();
~ImplPointArray();
void ImplSetSize( sal_uLong nSize );
sal_uLong ImplGetRealSize() const { return mnRealSize; }
void ImplSetRealSize( sal_uLong nRealSize ) { mnRealSize = nRealSize; }
void ImplCreatePoly( tools::Polygon& rPoly ) const;
inline Point& operator[]( sal_uLong nPos );
inline const Point& operator[]( sal_uLong nPos ) const;
};
ImplPointArray::ImplPointArray() :
mpArray ( nullptr ),
mnSize ( 0UL ),
mnRealSize ( 0UL )
{
}
ImplPointArray::~ImplPointArray()
{
if( mpArray )
rtl_freeMemory( mpArray );
}
void ImplPointArray::ImplSetSize( sal_uLong nSize )
{
const sal_uLong nTotal = nSize * sizeof( Point );
mnSize = nSize;
mnRealSize = 0UL;
if( mpArray )
rtl_freeMemory( mpArray );
mpArray = static_cast<Point*>(rtl_allocateMemory( nTotal ));
memset( mpArray, 0, nTotal );
}
inline Point& ImplPointArray::operator[]( sal_uLong nPos )
{
SAL_WARN_IF( nPos >= mnSize, "vcl", "ImplPointArray::operator[]: nPos out of range!" );
return mpArray[ nPos ];
}
inline const Point& ImplPointArray::operator[]( sal_uLong nPos ) const
{
SAL_WARN_IF( nPos >= mnSize, "vcl", "ImplPointArray::operator[]: nPos out of range!" );
return mpArray[ nPos ];
}
void ImplPointArray::ImplCreatePoly( tools::Polygon& rPoly ) const
{
rPoly = tools::Polygon( sal::static_int_cast<sal_uInt16>(mnRealSize), mpArray );
}
class ImplVectMap
{
private:
Scanline mpBuf;
Scanline* mpScan;
long mnWidth;
long mnHeight;
public:
ImplVectMap( long nWidth, long nHeight );
~ImplVectMap();
inline long Width() const { return mnWidth; }
inline long Height() const { return mnHeight; }
inline void Set( long nY, long nX, sal_uInt8 cVal );
inline sal_uInt8 Get( long nY, long nX ) const;
inline bool IsFree( long nY, long nX ) const;
inline bool IsCont( long nY, long nX ) const;
inline bool IsDone( long nY, long nX ) const;
};
ImplVectMap::ImplVectMap( long nWidth, long nHeight ) :
mnWidth ( nWidth ),
mnHeight( nHeight )
{
const long nWidthAl = ( nWidth >> 2 ) + 1;
const long nSize = nWidthAl * nHeight;
Scanline pTmp = mpBuf = static_cast<Scanline>(rtl_allocateMemory( nSize ));
memset( mpBuf, 0, nSize );
mpScan = static_cast<Scanline*>(rtl_allocateMemory( nHeight * sizeof( Scanline ) ));
for( long nY = 0; nY < nHeight; pTmp += nWidthAl )
mpScan[ nY++ ] = pTmp;
}
ImplVectMap::~ImplVectMap()
{
rtl_freeMemory( mpBuf );
rtl_freeMemory( mpScan );
}
inline void ImplVectMap::Set( long nY, long nX, sal_uInt8 cVal )
{
const sal_uInt8 cShift = sal::static_int_cast<sal_uInt8>(6 - ( ( nX & 3 ) << 1 ));
( ( mpScan[ nY ][ nX >> 2 ] ) &= ~( 3 << cShift ) ) |= ( cVal << cShift );
}
inline sal_uInt8 ImplVectMap::Get( long nY, long nX ) const
{
return sal::static_int_cast<sal_uInt8>( ( ( mpScan[ nY ][ nX >> 2 ] ) >> ( 6 - ( ( nX & 3 ) << 1 ) ) ) & 3 );
}
inline bool ImplVectMap::IsFree( long nY, long nX ) const
{
return( VECT_FREE_INDEX == Get( nY, nX ) );
}
inline bool ImplVectMap::IsCont( long nY, long nX ) const
{
return( VECT_CONT_INDEX == Get( nY, nX ) );
}
inline bool ImplVectMap::IsDone( long nY, long nX ) const
{
return( VECT_DONE_INDEX == Get( nY, nX ) );
}
class ImplChain
{
private:
tools::Polygon maPoly;
Point maStartPt;
sal_uLong mnArraySize;
sal_uLong mnCount;
std::unique_ptr<sal_uInt8[]>
mpCodes;
void ImplGetSpace();
void ImplPostProcess( const ImplPointArray& rArr );
ImplChain(const ImplChain&) = delete;
ImplChain& operator=(const ImplChain&) = delete;
public:
ImplChain();
void ImplBeginAdd( const Point& rStartPt );
inline void ImplAdd( sal_uInt8 nCode );
void ImplEndAdd( sal_uLong nTypeFlag );
const tools::Polygon& ImplGetPoly() const { return maPoly; }
};
ImplChain::ImplChain() :
mnArraySize ( 1024UL ),
mnCount ( 0UL )
{
mpCodes.reset( new sal_uInt8[ mnArraySize ] );
}
void ImplChain::ImplGetSpace()
{
const sal_uLong nOldArraySize = mnArraySize;
sal_uInt8* pNewCodes;
mnArraySize = mnArraySize << 1UL;
pNewCodes = new sal_uInt8[ mnArraySize ];
memcpy( pNewCodes, mpCodes.get(), nOldArraySize );
mpCodes.reset( pNewCodes );
}
void ImplChain::ImplBeginAdd( const Point& rStartPt )
{
maPoly = tools::Polygon();
maStartPt = rStartPt;
mnCount = 0UL;
}
inline void ImplChain::ImplAdd( sal_uInt8 nCode )
{
if( mnCount == mnArraySize )
ImplGetSpace();
mpCodes[ mnCount++ ] = nCode;
}
void ImplChain::ImplEndAdd( sal_uLong nFlag )
{
if( mnCount )
{
ImplPointArray aArr;
if( nFlag & VECT_POLY_INLINE_INNER )
{
long nFirstX, nFirstY;
long nLastX, nLastY;
nFirstX = nLastX = maStartPt.X();
nFirstY = nLastY = maStartPt.Y();
aArr.ImplSetSize( mnCount << 1 );
sal_uInt16 i, nPolyPos;
for( i = 0, nPolyPos = 0; i < ( mnCount - 1 ); i++ )
{
const sal_uInt8 cMove = mpCodes[ i ];
const sal_uInt8 cNextMove = mpCodes[ i + 1 ];
const ChainMove& rMove = aImplMove[ cMove ];
const ChainMove& rMoveInner = aImplMoveInner[ cMove ];
// Point& rPt = aArr[ nPolyPos ];
bool bDone = true;
nLastX += rMove.nDX;
nLastY += rMove.nDY;
if( cMove < 4 )
{
if( ( cMove == 0 && cNextMove == 3 ) ||
( cMove == 3 && cNextMove == 2 ) ||
( cMove == 2 && cNextMove == 1 ) ||
( cMove == 1 && cNextMove == 0 ) )
{
}
else if( cMove == 2 && cNextMove == 3 )
{
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY;
}
else if( cMove == 3 && cNextMove == 0 )
{
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY;
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
}
else if( cMove == 0 && cNextMove == 1 )
{
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY;
}
else if( cMove == 1 && cNextMove == 2 )
{
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
}
else
bDone = false;
}
else if( cMove == 7 && cNextMove == 0 )
{
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY;
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
}
else if( cMove == 4 && cNextMove == 1 )
{
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY;
}
else
bDone = false;
if( !bDone )
{
aArr[ nPolyPos ].X() = nLastX + rMoveInner.nDX;
aArr[ nPolyPos++ ].Y() = nLastY + rMoveInner.nDY;
}
}
aArr[ nPolyPos ].X() = nFirstX + 1;
aArr[ nPolyPos++ ].Y() = nFirstY + 1;
aArr.ImplSetRealSize( nPolyPos );
}
else if( nFlag & VECT_POLY_INLINE_OUTER )
{
long nFirstX, nFirstY;
long nLastX, nLastY;
nFirstX = nLastX = maStartPt.X();
nFirstY = nLastY = maStartPt.Y();
aArr.ImplSetSize( mnCount << 1 );
sal_uInt16 i, nPolyPos;
for( i = 0, nPolyPos = 0; i < ( mnCount - 1 ); i++ )
{
const sal_uInt8 cMove = mpCodes[ i ];
const sal_uInt8 cNextMove = mpCodes[ i + 1 ];
const ChainMove& rMove = aImplMove[ cMove ];
const ChainMove& rMoveOuter = aImplMoveOuter[ cMove ];
// Point& rPt = aArr[ nPolyPos ];
bool bDone = true;
nLastX += rMove.nDX;
nLastY += rMove.nDY;
if( cMove < 4 )
{
if( ( cMove == 0 && cNextMove == 1 ) ||
( cMove == 1 && cNextMove == 2 ) ||
( cMove == 2 && cNextMove == 3 ) ||
( cMove == 3 && cNextMove == 0 ) )
{
}
else if( cMove == 0 && cNextMove == 3 )
{
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY;
}
else if( cMove == 3 && cNextMove == 2 )
{
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
}
else if( cMove == 2 && cNextMove == 1 )
{
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY;
}
else if( cMove == 1 && cNextMove == 0 )
{
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY;
aArr[ nPolyPos ].X() = nLastX - 1;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
}
else
bDone = false;
}
else if( cMove == 7 && cNextMove == 3 )
{
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY - 1;
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY;
}
else if( cMove == 6 && cNextMove == 2 )
{
aArr[ nPolyPos ].X() = nLastX + 1;
aArr[ nPolyPos++ ].Y() = nLastY;
aArr[ nPolyPos ].X() = nLastX;
aArr[ nPolyPos++ ].Y() = nLastY + 1;
}
else
bDone = false;
if( !bDone )
{
aArr[ nPolyPos ].X() = nLastX + rMoveOuter.nDX;
aArr[ nPolyPos++ ].Y() = nLastY + rMoveOuter.nDY;
}
}
aArr[ nPolyPos ].X() = nFirstX - 1;
aArr[ nPolyPos++ ].Y() = nFirstY - 1;
aArr.ImplSetRealSize( nPolyPos );
}
else
{
long nLastX = maStartPt.X(), nLastY = maStartPt.Y();
aArr.ImplSetSize( mnCount + 1 );
aArr[ 0 ] = Point( nLastX, nLastY );
for( sal_uLong i = 0; i < mnCount; )
{
const ChainMove& rMove = aImplMove[ mpCodes[ i ] ];
aArr[ ++i ] = Point( nLastX += rMove.nDX, nLastY += rMove.nDY );
}
aArr.ImplSetRealSize( mnCount + 1 );
}
ImplPostProcess( aArr );
}
else
maPoly.SetSize( 0 );
}
void ImplChain::ImplPostProcess( const ImplPointArray& rArr )
{
ImplPointArray aNewArr1;
ImplPointArray aNewArr2;
Point* pLast;
Point* pLeast;
sal_uLong nNewPos;
sal_uLong nCount = rArr.ImplGetRealSize();
sal_uLong n;
// pass 1
aNewArr1.ImplSetSize( nCount );
pLast = &( aNewArr1[ 0 ] );
pLast->X() = BACK_MAP( rArr[ 0 ].X() );
pLast->Y() = BACK_MAP( rArr[ 0 ].Y() );
for( n = nNewPos = 1; n < nCount; )
{
const Point& rPt = rArr[ n++ ];
const long nX = BACK_MAP( rPt.X() );
const long nY = BACK_MAP( rPt.Y() );
if( nX != pLast->X() || nY != pLast->Y() )
{
pLast = pLeast = &( aNewArr1[ nNewPos++ ] );
pLeast->X() = nX;
pLeast->Y() = nY;
}
}
aNewArr1.ImplSetRealSize( nCount = nNewPos );
// pass 2
aNewArr2.ImplSetSize( nCount );
pLast = &( aNewArr2[ 0 ] );
*pLast = aNewArr1[ 0 ];
for( n = nNewPos = 1; n < nCount; )
{
pLeast = &( aNewArr1[ n++ ] );
if( pLeast->X() == pLast->X() )
{
while( n < nCount && aNewArr1[ n ].X() == pLast->X() )
pLeast = &( aNewArr1[ n++ ] );
}
else if( pLeast->Y() == pLast->Y() )
{
while( n < nCount && aNewArr1[ n ].Y() == pLast->Y() )
pLeast = &( aNewArr1[ n++ ] );
}
aNewArr2[ nNewPos++ ] = *( pLast = pLeast );
}
aNewArr2.ImplSetRealSize( nNewPos );
aNewArr2.ImplCreatePoly( maPoly );
}
namespace ImplVectorizer {
bool ImplVectorize( const Bitmap& rColorBmp, GDIMetaFile& rMtf,
sal_uInt8 cReduce, const Link<long,void>* pProgress )
{
bool bRet = false;
VECT_PROGRESS( pProgress, 0 );
std::unique_ptr<Bitmap> xBmp(new Bitmap( rColorBmp ));
Bitmap::ScopedReadAccess pRAcc(*xBmp);
if( pRAcc )
{
tools::PolyPolygon aPolyPoly;
double fPercent = 0.0;
double fPercentStep_2 = 0.0;
const long nWidth = pRAcc->Width();
const long nHeight = pRAcc->Height();
const sal_uInt16 nColorCount = pRAcc->GetPaletteEntryCount();
sal_uInt16 n;
ImplColorSet* pColorSet = reinterpret_cast<ImplColorSet*>(new sal_uInt8[ 256 * sizeof( ImplColorSet ) ]);
memset( pColorSet, 0, 256 * sizeof( ImplColorSet ) );
rMtf.Clear();
// get used palette colors and sort them from light to dark colors
for( n = 0; n < nColorCount; n++ )
{
pColorSet[ n ].mnIndex = n;
pColorSet[ n ].maColor = pRAcc->GetPaletteColor( n );
}
for( long nY = 0; nY < nHeight; nY++ )
for( long nX = 0; nX < nWidth; nX++ )
pColorSet[ pRAcc->GetPixel( nY, nX ).GetIndex() ].mbSet = true;
qsort( pColorSet, 256, sizeof( ImplColorSet ), ImplColorSetCmpFnc );
for( n = 0; n < 256; n++ )
if( !pColorSet[ n ].mbSet )
break;
if( n )
fPercentStep_2 = 45.0 / n;
VECT_PROGRESS( pProgress, FRound( fPercent += 10.0 ) );
for( sal_uInt16 i = 0; i < n; i++ )
{
const BitmapColor aBmpCol( pRAcc->GetPaletteColor( pColorSet[ i ].mnIndex ) );
const Color aFindColor( aBmpCol.GetRed(), aBmpCol.GetGreen(), aBmpCol.GetBlue() );
std::unique_ptr<ImplVectMap> xMap(ImplExpand( pRAcc.get(), aFindColor ));
VECT_PROGRESS( pProgress, FRound( fPercent += fPercentStep_2 ) );
if( xMap )
{
aPolyPoly.Clear();
ImplCalculate( xMap.get(), aPolyPoly, cReduce );
xMap.reset();
if( aPolyPoly.Count() )
{
ImplLimitPolyPoly( aPolyPoly );
aPolyPoly.Optimize( PolyOptimizeFlags::EDGES );
if( aPolyPoly.Count() )
{
rMtf.AddAction( new MetaLineColorAction( aFindColor, true ) );
rMtf.AddAction( new MetaFillColorAction( aFindColor, true ) );
rMtf.AddAction( new MetaPolyPolygonAction( aPolyPoly ) );
}
}
}
VECT_PROGRESS( pProgress, FRound( fPercent += fPercentStep_2 ) );
}
delete[] reinterpret_cast<sal_uInt8*>(pColorSet);
if( rMtf.GetActionSize() )
{
MapMode aMap( MapUnit::Map100thMM );
ScopedVclPtrInstance< VirtualDevice > aVDev;
const Size aLogSize1( aVDev->PixelToLogic( Size( 1, 1 ), aMap ) );
rMtf.SetPrefMapMode( aMap );
rMtf.SetPrefSize( Size( nWidth + 2, nHeight + 2 ) );
rMtf.Move( 1, 1 );
rMtf.Scale( aLogSize1.Width(), aLogSize1.Height() );
bRet = true;
}
}
pRAcc.reset();
xBmp.reset();
VECT_PROGRESS( pProgress, 100 );
return bRet;
}
bool ImplVectorize( const Bitmap& rMonoBmp,
tools::PolyPolygon& rPolyPoly )
{
std::unique_ptr<Bitmap> xBmp(new Bitmap( rMonoBmp ));
bool bRet = false;
if( xBmp->GetBitCount() > 1 )
xBmp->Convert( BmpConversion::N1BitThreshold );
Bitmap::ScopedReadAccess pRAcc(*xBmp);
std::unique_ptr <ImplVectMap> xMap(ImplExpand( pRAcc.get(), COL_BLACK ));
pRAcc.reset();
xBmp.reset();
if( xMap )
{
rPolyPoly.Clear();
ImplCalculate( xMap.get(), rPolyPoly, 0 );
xMap.reset();
ImplLimitPolyPoly( rPolyPoly );
rPolyPoly.Optimize( PolyOptimizeFlags::EDGES );
// #i14895#:setting the correct direction for polygons
// that represent holes and non-holes; non-hole polygons
// need to have a right orientation, holes need to have a
// left orientation in order to be treated correctly by
// several external tools like Flash viewers
sal_Int32 nFirstPoly = -1;
sal_uInt16 nCurPoly( 0 ), nCount( rPolyPoly.Count() );
for( ; nCurPoly < nCount; ++nCurPoly )
{
const tools::Polygon& rPoly = rPolyPoly.GetObject( nCurPoly );
const sal_uInt16 nSize( rPoly.GetSize() );
sal_uInt16 nDepth( 0 ), i( 0 );
const bool bRight( rPoly.IsRightOrientated() );
for( ; i < nCount; ++i )
if( ( i != nCurPoly ) && rPolyPoly.GetObject( i ).IsInside( rPoly[ 0 ] ) )
++nDepth;
const bool bHole( ( nDepth & 0x0001 ) == 1 );
if( nSize && ( ( !bRight && !bHole ) || ( bRight && bHole ) ) )
{
tools::Polygon aNewPoly( nSize );
sal_uInt16 nPrim( 0 ), nSec( nSize - 1 );
if( rPoly.HasFlags() )
{
while( nPrim < nSize )
{
aNewPoly.SetPoint( rPoly.GetPoint( nSec ), nPrim );
aNewPoly.SetFlags( nPrim++, rPoly.GetFlags( nSec-- ) );
}
}
else
while( nPrim < nSize )
aNewPoly.SetPoint( rPoly.GetPoint( nSec-- ), nPrim++ );
rPolyPoly.Replace( aNewPoly, nCurPoly );
}
if( ( 0 == nDepth ) && ( -1 == nFirstPoly ) )
nFirstPoly = nCurPoly;
}
// put outmost polygon to the front
if( nFirstPoly > 0 )
{
const tools::Polygon aFirst( rPolyPoly.GetObject( static_cast< sal_uInt16 >( nFirstPoly ) ) );
rPolyPoly.Remove( static_cast< sal_uInt16 >( nFirstPoly ) );
rPolyPoly.Insert( aFirst, 0 );
}
bRet = true;
}
return bRet;
}
void ImplLimitPolyPoly( tools::PolyPolygon& rPolyPoly )
{
if( rPolyPoly.Count() > VECT_POLY_MAX )
{
tools::PolyPolygon aNewPolyPoly;
long nReduce = 0;
sal_uInt16 nNewCount;
do
{
aNewPolyPoly.Clear();
nReduce++;
for( sal_uInt16 i = 0, nCount = rPolyPoly.Count(); i < nCount; i++ )
{
const Rectangle aBound( rPolyPoly[ i ].GetBoundRect() );
if( aBound.GetWidth() > nReduce && aBound.GetHeight() > nReduce )
{
if( rPolyPoly[ i ].GetSize() )
aNewPolyPoly.Insert( rPolyPoly[ i ] );
}
}
nNewCount = aNewPolyPoly.Count();
}
while( nNewCount > VECT_POLY_MAX );
rPolyPoly = aNewPolyPoly;
}
}
ImplVectMap* ImplExpand( BitmapReadAccess* pRAcc, const Color& rColor )
{
ImplVectMap* pMap = nullptr;
if( pRAcc && pRAcc->Width() && pRAcc->Height() )
{
const long nOldWidth = pRAcc->Width();
const long nOldHeight = pRAcc->Height();
const long nNewWidth = ( nOldWidth << 2 ) + 4;
const long nNewHeight = ( nOldHeight << 2 ) + 4;
const BitmapColor aTest( pRAcc->GetBestMatchingColor( rColor ) );
std::unique_ptr<long[]> pMapIn(new long[ std::max( nOldWidth, nOldHeight ) ]);
std::unique_ptr<long[]> pMapOut(new long[ std::max( nOldWidth, nOldHeight ) ]);
long nX, nY, nTmpX, nTmpY;
pMap = new ImplVectMap( nNewWidth, nNewHeight );
for( nX = 0; nX < nOldWidth; nX++ )
VECT_MAP( pMapIn, pMapOut, nX );
for( nY = 0, nTmpY = 5; nY < nOldHeight; nY++, nTmpY += 4 )
{
for( nX = 0; nX < nOldWidth; )
{
if( pRAcc->GetPixel( nY, nX ) == aTest )
{
nTmpX = pMapIn[ nX++ ];
nTmpY -= 3;
pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
while( nX < nOldWidth && pRAcc->GetPixel( nY, nX ) == aTest )
nX++;
nTmpX = pMapOut[ nX - 1 ];
nTmpY -= 3;
pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
pMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
}
else
nX++;
}
}
for( nY = 0; nY < nOldHeight; nY++ )
VECT_MAP( pMapIn, pMapOut, nY );
for( nX = 0, nTmpX = 5; nX < nOldWidth; nX++, nTmpX += 4 )
{
for( nY = 0; nY < nOldHeight; )
{
if( pRAcc->GetPixel( nY, nX ) == aTest )
{
nTmpX -= 3;
nTmpY = pMapIn[ nY++ ];
pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
while( nY < nOldHeight && pRAcc->GetPixel( nY, nX ) == aTest )
nY++;
nTmpX -= 3;
nTmpY = pMapOut[ nY - 1 ];
pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
pMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
}
else
nY++;
}
}
}
return pMap;
}
void ImplCalculate( ImplVectMap* pMap, tools::PolyPolygon& rPolyPoly, sal_uInt8 cReduce )
{
const long nWidth = pMap->Width(), nHeight= pMap->Height();
for( long nY = 0; nY < nHeight; nY++ )
{
long nX = 0;
bool bInner = true;
while( nX < nWidth )
{
// skip free
while( ( nX < nWidth ) && pMap->IsFree( nY, nX ) )
nX++;
if( nX == nWidth )
break;
if( pMap->IsCont( nY, nX ) )
{
// new contour
ImplChain aChain;
const Point aStartPt( nX++, nY );
// get chain code
aChain.ImplBeginAdd( aStartPt );
ImplGetChain( pMap, aStartPt, aChain );
aChain.ImplEndAdd( bInner ? VECT_POLY_OUTLINE_INNER : VECT_POLY_OUTLINE_OUTER );
const tools::Polygon& rPoly = aChain.ImplGetPoly();
if( rPoly.GetSize() > 2 )
{
if( cReduce )
{
const Rectangle aBound( rPoly.GetBoundRect() );
if( aBound.GetWidth() > cReduce && aBound.GetHeight() > cReduce )
rPolyPoly.Insert( rPoly );
}
else
rPolyPoly.Insert( rPoly );
}
// skip rest of detected contour
while( pMap->IsCont( nY, nX ) )
nX++;
}
else
{
// process done segment
const long nStartSegX = nX++;
while( pMap->IsDone( nY, nX ) )
nX++;
if( ( ( nX - nStartSegX ) == 1 ) || ( ImplIsUp( pMap, nY, nStartSegX ) != ImplIsUp( pMap, nY, nX - 1 ) ) )
bInner = !bInner;
}
}
}
}
bool ImplGetChain( ImplVectMap* pMap, const Point& rStartPt, ImplChain& rChain )
{
long nActX = rStartPt.X();
long nActY = rStartPt.Y();
sal_uLong nFound;
sal_uLong nLastDir = 0UL;
sal_uLong nDir;
do
{
nFound = 0UL;
// first try last direction
long nTryX = nActX + aImplMove[ nLastDir ].nDX;
long nTryY = nActY + aImplMove[ nLastDir ].nDY;
if( pMap->IsCont( nTryY, nTryX ) )
{
rChain.ImplAdd( (sal_uInt8) nLastDir );
pMap->Set( nActY = nTryY, nActX = nTryX, VECT_DONE_INDEX );
nFound = 1UL;
}
else
{
// try other directions
for( nDir = 0UL; nDir < 8UL; nDir++ )
{
// we already tried nLastDir
if( nDir != nLastDir )
{
nTryX = nActX + aImplMove[ nDir ].nDX;
nTryY = nActY + aImplMove[ nDir ].nDY;
if( pMap->IsCont( nTryY, nTryX ) )
{
rChain.ImplAdd( (sal_uInt8) nDir );
pMap->Set( nActY = nTryY, nActX = nTryX, VECT_DONE_INDEX );
nFound = 1UL;
nLastDir = nDir;
break;
}
}
}
}
}
while( nFound );
return true;
}
bool ImplIsUp( ImplVectMap* pMap, long nY, long nX )
{
if( pMap->IsDone( nY - 1, nX ) )
return true;
else if( pMap->IsDone( nY + 1, nX ) )
return false;
else if( pMap->IsDone( nY - 1, nX - 1 ) || pMap->IsDone( nY - 1, nX + 1 ) )
return true;
else
return false;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|