LibreOffice Module vcl (master) 1
impvect.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/log.hxx>
22#include <tools/link.hxx>
23#include <tools/poly.hxx>
24#include <tools/helpers.hxx>
25#include <vcl/gdimtf.hxx>
26#include <vcl/metaact.hxx>
27#include <vcl/virdev.hxx>
28#include "impvect.hxx"
29#include <array>
30#include <memory>
31
32#define VECT_POLY_MAX 8192
33
34#define VECT_FREE_INDEX 0
35#define VECT_CONT_INDEX 1
36#define VECT_DONE_INDEX 2
37
38#define VECT_POLY_INLINE_INNER 1UL
39#define VECT_POLY_INLINE_OUTER 2UL
40#define VECT_POLY_OUTLINE_INNER 4UL
41#define VECT_POLY_OUTLINE_OUTER 8UL
42
43static void VECT_MAP( const std::unique_ptr<sal_Int32 []> & pMapIn, const std::unique_ptr<sal_Int32 []>& pMapOut, tools::Long nVal )
44{
45 pMapIn[nVal] = (nVal * 4) + 1;
46 pMapOut[nVal] = pMapIn[nVal] + 5;
47}
48static constexpr tools::Long BACK_MAP( tools::Long _def_nVal )
49{
50 return ((_def_nVal + 2) >> 2) - 1;
51}
52static void VECT_PROGRESS( const Link<tools::Long, void>* pProgress, tools::Long _def_nVal )
53{
54 if(pProgress)
55 pProgress->Call(_def_nVal);
56}
57
58namespace {
59
60class ImplVectMap;
61class ImplChain;
62
63}
64
66{
67 static void ImplExpand( std::optional<ImplVectMap>& rMap, const BitmapReadAccess* pRAcc, const Color& rColor );
68 static void ImplCalculate( ImplVectMap& rMap, tools::PolyPolygon& rPolyPoly, sal_uInt8 cReduce );
69 static bool ImplGetChain( ImplVectMap& rMap, const Point& rStartPt, ImplChain& rChain );
70 static bool ImplIsUp( ImplVectMap const & rMap, tools::Long nY, tools::Long nX );
71 static void ImplLimitPolyPoly( tools::PolyPolygon& rPolyPoly );
72}
73
74namespace {
75
76struct ChainMove { tools::Long nDX; tools::Long nDY; };
77
78}
79
80const ChainMove aImplMove[ 8 ] = {
81 { 1, 0 },
82 { 0, -1 },
83 { -1, 0 },
84 { 0, 1 },
85 { 1, -1 },
86 { -1, -1 },
87 { -1, 1 },
88 { 1, 1 }
89 };
90
91const ChainMove aImplMoveInner[ 8 ] = {
92 { 0, 1 },
93 { 1, 0 },
94 { 0, -1 },
95 { -1, 0 },
96 { 0, 1 },
97 { 1, 0 },
98 { 0, -1 },
99 { -1, 0 }
100 };
101
102const ChainMove aImplMoveOuter[ 8 ] = {
103 { 0, -1 },
104 { -1, 0 },
105 { 0, 1 },
106 { 1, 0 },
107 { -1, 0 },
108 { 0, 1 },
109 { 1, 0 },
110 { 0, -1 }
111 };
112
113namespace {
114
115struct ImplColorSet
116{
118 sal_uInt16 mnIndex = 0;
119 bool mbSet = false;
120};
121
122}
123
124static bool ImplColorSetCmpFnc( const ImplColorSet& lhs, const ImplColorSet& rhs)
125{
126 if( lhs.mbSet && rhs.mbSet )
127 {
128 const sal_uInt8 cLum1 = lhs.maColor.GetLuminance();
129 const sal_uInt8 cLum2 = rhs.maColor.GetLuminance();
130 return cLum1 < cLum2;
131 }
132 return lhs.mbSet > rhs.mbSet;
133}
134
135namespace {
136
137class ImplPointArray
138{
139 std::unique_ptr<Point[]> mpArray;
141 sal_uLong mnRealSize;
142
143public:
144
145 ImplPointArray();
146
147 void ImplSetSize( sal_uLong nSize );
148 sal_uLong ImplGetRealSize() const { return mnRealSize; }
149 void ImplSetRealSize( sal_uLong nRealSize ) { mnRealSize = nRealSize; }
150 void ImplCreatePoly( tools::Polygon& rPoly ) const;
151
152 inline Point& operator[]( sal_uLong nPos );
153 inline const Point& operator[]( sal_uLong nPos ) const;
154
155};
156
157}
158
159ImplPointArray::ImplPointArray() :
160 mnSize ( 0 ),
161 mnRealSize ( 0 )
162
163{
164}
165
166void ImplPointArray::ImplSetSize( sal_uLong nSize )
167{
168 const sal_uLong nTotal = nSize * sizeof( Point );
169
170 mnSize = nSize;
171 mnRealSize = 0;
172
173 mpArray = std::make_unique<Point[]>( nTotal );
174}
175
176inline Point& ImplPointArray::operator[]( sal_uLong nPos )
177{
178 SAL_WARN_IF( nPos >= mnSize, "vcl", "ImplPointArray::operator[]: nPos out of range!" );
179 return mpArray[ nPos ];
180}
181
182inline const Point& ImplPointArray::operator[]( sal_uLong nPos ) const
183{
184 SAL_WARN_IF( nPos >= mnSize, "vcl", "ImplPointArray::operator[]: nPos out of range!" );
185 return mpArray[ nPos ];
186}
187
188void ImplPointArray::ImplCreatePoly( tools::Polygon& rPoly ) const
189{
190 rPoly = tools::Polygon( sal::static_int_cast<sal_uInt16>(mnRealSize), mpArray.get() );
191}
192
193namespace {
194
195class ImplVectMap
196{
197private:
198
199 Scanline mpBuf;
200 Scanline* mpScan;
203
204public:
205
206 ImplVectMap( tools::Long nWidth, tools::Long nHeight );
207 ~ImplVectMap();
208
209 tools::Long Width() const { return mnWidth; }
210 tools::Long Height() const { return mnHeight; }
211
212 inline void Set( tools::Long nY, tools::Long nX, sal_uInt8 cVal );
213 inline sal_uInt8 Get( tools::Long nY, tools::Long nX ) const;
214
215 inline bool IsFree( tools::Long nY, tools::Long nX ) const;
216 inline bool IsCont( tools::Long nY, tools::Long nX ) const;
217 inline bool IsDone( tools::Long nY, tools::Long nX ) const;
218
219};
220
221}
222
223ImplVectMap::ImplVectMap( tools::Long nWidth, tools::Long nHeight ) :
224 mpBuf ( static_cast<Scanline>(rtl_allocateZeroMemory(nWidth * nHeight)) ),
225 mpScan ( static_cast<Scanline*>(std::malloc(nHeight * sizeof(Scanline))) ),
226 mnWidth ( nWidth ),
227 mnHeight( nHeight )
228{
229 const tools::Long nWidthAl = ( nWidth >> 2 ) + 1;
230 Scanline pTmp = mpBuf;
231
232 for( tools::Long nY = 0; nY < nHeight; pTmp += nWidthAl )
233 mpScan[ nY++ ] = pTmp;
234}
235
236ImplVectMap::~ImplVectMap()
237{
238 std::free( mpBuf );
239 std::free( mpScan );
240}
241
242inline void ImplVectMap::Set( tools::Long nY, tools::Long nX, sal_uInt8 cVal )
243{
244 const sal_uInt8 cShift = sal::static_int_cast<sal_uInt8>(6 - ( ( nX & 3 ) << 1 ));
245 auto & rPixel = mpScan[ nY ][ nX >> 2 ];
246 rPixel = (rPixel & ~( 3 << cShift ) ) | ( cVal << cShift );
247}
248
249inline sal_uInt8 ImplVectMap::Get( tools::Long nY, tools::Long nX ) const
250{
251 return sal::static_int_cast<sal_uInt8>( ( ( mpScan[ nY ][ nX >> 2 ] ) >> ( 6 - ( ( nX & 3 ) << 1 ) ) ) & 3 );
252}
253
254inline bool ImplVectMap::IsFree( tools::Long nY, tools::Long nX ) const
255{
256 return( VECT_FREE_INDEX == Get( nY, nX ) );
257}
258
259inline bool ImplVectMap::IsCont( tools::Long nY, tools::Long nX ) const
260{
261 return( VECT_CONT_INDEX == Get( nY, nX ) );
262}
263
264inline bool ImplVectMap::IsDone( tools::Long nY, tools::Long nX ) const
265{
266 return( VECT_DONE_INDEX == Get( nY, nX ) );
267}
268
269namespace {
270
271class ImplChain
272{
273private:
274
275 tools::Polygon maPoly;
276 Point maStartPt;
277 sal_uLong mnArraySize;
279 std::unique_ptr<sal_uInt8[]>
280 mpCodes;
281
282 void ImplGetSpace();
283
284 void ImplPostProcess( const ImplPointArray& rArr );
285
286 ImplChain(const ImplChain&) = delete;
287 ImplChain& operator=(const ImplChain&) = delete;
288
289public:
290
291 ImplChain();
292
293 void ImplBeginAdd( const Point& rStartPt );
294 inline void ImplAdd( sal_uInt8 nCode );
295 void ImplEndAdd( sal_uLong nTypeFlag );
296
297 const tools::Polygon& ImplGetPoly() const { return maPoly; }
298};
299
300}
301
302ImplChain::ImplChain() :
303 mnArraySize ( 1024 ),
304 mnCount ( 0 ),
305 mpCodes ( new sal_uInt8[mnArraySize] )
306{
307}
308
309void ImplChain::ImplGetSpace()
310{
311 const sal_uLong nOldArraySize = mnArraySize;
312 sal_uInt8* pNewCodes;
313
314 mnArraySize = mnArraySize << 1;
315 pNewCodes = new sal_uInt8[ mnArraySize ];
316 memcpy( pNewCodes, mpCodes.get(), nOldArraySize );
317 mpCodes.reset( pNewCodes );
318}
319
320void ImplChain::ImplBeginAdd( const Point& rStartPt )
321{
322 maPoly = tools::Polygon();
323 maStartPt = rStartPt;
324 mnCount = 0;
325}
326
327inline void ImplChain::ImplAdd( sal_uInt8 nCode )
328{
329 if( mnCount == mnArraySize )
330 ImplGetSpace();
331
332 mpCodes[ mnCount++ ] = nCode;
333}
334
335void ImplChain::ImplEndAdd( sal_uLong nFlag )
336{
337 if( mnCount )
338 {
339 ImplPointArray aArr;
340
341 if( nFlag & VECT_POLY_INLINE_INNER )
342 {
343 tools::Long nFirstX, nFirstY;
344 tools::Long nLastX, nLastY;
345
346 nFirstX = nLastX = maStartPt.X();
347 nFirstY = nLastY = maStartPt.Y();
348 aArr.ImplSetSize( mnCount << 1 );
349
350 sal_uInt16 nPolyPos;
351 sal_uLong i;
352 for( i = 0, nPolyPos = 0; i < ( mnCount - 1 ); i++ )
353 {
354 const sal_uInt8 cMove = mpCodes[ i ];
355 const sal_uInt8 cNextMove = mpCodes[ i + 1 ];
356 const ChainMove& rMove = aImplMove[ cMove ];
357 const ChainMove& rMoveInner = aImplMoveInner[ cMove ];
358// Point& rPt = aArr[ nPolyPos ];
359 bool bDone = true;
360
361 nLastX += rMove.nDX;
362 nLastY += rMove.nDY;
363
364 if( cMove < 4 )
365 {
366 if( ( cMove == 0 && cNextMove == 3 ) ||
367 ( cMove == 3 && cNextMove == 2 ) ||
368 ( cMove == 2 && cNextMove == 1 ) ||
369 ( cMove == 1 && cNextMove == 0 ) )
370 {
371 }
372 else if( cMove == 2 && cNextMove == 3 )
373 {
374 aArr[ nPolyPos ].setX( nLastX );
375 aArr[ nPolyPos++ ].setY( nLastY - 1 );
376
377 aArr[ nPolyPos ].setX( nLastX - 1 );
378 aArr[ nPolyPos++ ].setY( nLastY - 1 );
379
380 aArr[ nPolyPos ].setX( nLastX - 1 );
381 aArr[ nPolyPos++ ].setY( nLastY );
382 }
383 else if( cMove == 3 && cNextMove == 0 )
384 {
385 aArr[ nPolyPos ].setX( nLastX - 1 );
386 aArr[ nPolyPos++ ].setY( nLastY );
387
388 aArr[ nPolyPos ].setX( nLastX - 1 );
389 aArr[ nPolyPos++ ].setY( nLastY + 1 );
390
391 aArr[ nPolyPos ].setX( nLastX );
392 aArr[ nPolyPos++ ].setY( nLastY + 1 );
393 }
394 else if( cMove == 0 && cNextMove == 1 )
395 {
396 aArr[ nPolyPos ].setX( nLastX );
397 aArr[ nPolyPos++ ].setY( nLastY + 1 );
398
399 aArr[ nPolyPos ].setX( nLastX + 1 );
400 aArr[ nPolyPos++ ].setY( nLastY + 1 );
401
402 aArr[ nPolyPos ].setX( nLastX + 1 );
403 aArr[ nPolyPos++ ].setY( nLastY );
404 }
405 else if( cMove == 1 && cNextMove == 2 )
406 {
407 aArr[ nPolyPos ].setX( nLastX + 1 );
408 aArr[ nPolyPos++ ].setY( nLastY + 1 );
409
410 aArr[ nPolyPos ].setX( nLastX + 1 );
411 aArr[ nPolyPos++ ].setY( nLastY - 1 );
412
413 aArr[ nPolyPos ].setX( nLastX );
414 aArr[ nPolyPos++ ].setY( nLastY - 1 );
415 }
416 else
417 bDone = false;
418 }
419 else if( cMove == 7 && cNextMove == 0 )
420 {
421 aArr[ nPolyPos ].setX( nLastX - 1 );
422 aArr[ nPolyPos++ ].setY( nLastY );
423
424 aArr[ nPolyPos ].setX( nLastX );
425 aArr[ nPolyPos++ ].setY( nLastY + 1 );
426 }
427 else if( cMove == 4 && cNextMove == 1 )
428 {
429 aArr[ nPolyPos ].setX( nLastX );
430 aArr[ nPolyPos++ ].setY( nLastY + 1 );
431
432 aArr[ nPolyPos ].setX( nLastX + 1 );
433 aArr[ nPolyPos++ ].setY( nLastY );
434 }
435 else
436 bDone = false;
437
438 if( !bDone )
439 {
440 aArr[ nPolyPos ].setX( nLastX + rMoveInner.nDX );
441 aArr[ nPolyPos++ ].setY( nLastY + rMoveInner.nDY );
442 }
443 }
444
445 aArr[ nPolyPos ].setX( nFirstX + 1 );
446 aArr[ nPolyPos++ ].setY( nFirstY + 1 );
447 aArr.ImplSetRealSize( nPolyPos );
448 }
449 else if( nFlag & VECT_POLY_INLINE_OUTER )
450 {
451 tools::Long nFirstX, nFirstY;
452 tools::Long nLastX, nLastY;
453
454 nFirstX = nLastX = maStartPt.X();
455 nFirstY = nLastY = maStartPt.Y();
456 aArr.ImplSetSize( mnCount << 1 );
457
458 sal_uInt16 nPolyPos;
459 sal_uLong i;
460 for( i = 0, nPolyPos = 0; i < ( mnCount - 1 ); i++ )
461 {
462 const sal_uInt8 cMove = mpCodes[ i ];
463 const sal_uInt8 cNextMove = mpCodes[ i + 1 ];
464 const ChainMove& rMove = aImplMove[ cMove ];
465 const ChainMove& rMoveOuter = aImplMoveOuter[ cMove ];
466// Point& rPt = aArr[ nPolyPos ];
467 bool bDone = true;
468
469 nLastX += rMove.nDX;
470 nLastY += rMove.nDY;
471
472 if( cMove < 4 )
473 {
474 if( ( cMove == 0 && cNextMove == 1 ) ||
475 ( cMove == 1 && cNextMove == 2 ) ||
476 ( cMove == 2 && cNextMove == 3 ) ||
477 ( cMove == 3 && cNextMove == 0 ) )
478 {
479 }
480 else if( cMove == 0 && cNextMove == 3 )
481 {
482 aArr[ nPolyPos ].setX( nLastX );
483 aArr[ nPolyPos++ ].setY( nLastY - 1 );
484
485 aArr[ nPolyPos ].setX( nLastX + 1 );
486 aArr[ nPolyPos++ ].setY( nLastY - 1 );
487
488 aArr[ nPolyPos ].setX( nLastX + 1 );
489 aArr[ nPolyPos++ ].setY( nLastY );
490 }
491 else if( cMove == 3 && cNextMove == 2 )
492 {
493 aArr[ nPolyPos ].setX( nLastX + 1 );
494 aArr[ nPolyPos++ ].setY( nLastY );
495
496 aArr[ nPolyPos ].setX( nLastX + 1 );
497 aArr[ nPolyPos++ ].setY( nLastY + 1 );
498
499 aArr[ nPolyPos ].setX( nLastX );
500 aArr[ nPolyPos++ ].setY( nLastY + 1 );
501 }
502 else if( cMove == 2 && cNextMove == 1 )
503 {
504 aArr[ nPolyPos ].setX( nLastX );
505 aArr[ nPolyPos++ ].setY( nLastY + 1 );
506
507 aArr[ nPolyPos ].setX( nLastX - 1 );
508 aArr[ nPolyPos++ ].setY( nLastY + 1 );
509
510 aArr[ nPolyPos ].setX( nLastX - 1 );
511 aArr[ nPolyPos++ ].setY( nLastY );
512 }
513 else if( cMove == 1 && cNextMove == 0 )
514 {
515 aArr[ nPolyPos ].setX( nLastX - 1 );
516 aArr[ nPolyPos++ ].setY( nLastY );
517
518 aArr[ nPolyPos ].setX( nLastX - 1 );
519 aArr[ nPolyPos++ ].setY( nLastY - 1 );
520
521 aArr[ nPolyPos ].setX( nLastX );
522 aArr[ nPolyPos++ ].setY( nLastY - 1 );
523 }
524 else
525 bDone = false;
526 }
527 else if( cMove == 7 && cNextMove == 3 )
528 {
529 aArr[ nPolyPos ].setX( nLastX );
530 aArr[ nPolyPos++ ].setY( nLastY - 1 );
531
532 aArr[ nPolyPos ].setX( nLastX + 1 );
533 aArr[ nPolyPos++ ].setY( nLastY );
534 }
535 else if( cMove == 6 && cNextMove == 2 )
536 {
537 aArr[ nPolyPos ].setX( nLastX + 1 );
538 aArr[ nPolyPos++ ].setY( nLastY );
539
540 aArr[ nPolyPos ].setX( nLastX );
541 aArr[ nPolyPos++ ].setY( nLastY + 1 );
542 }
543 else
544 bDone = false;
545
546 if( !bDone )
547 {
548 aArr[ nPolyPos ].setX( nLastX + rMoveOuter.nDX );
549 aArr[ nPolyPos++ ].setY( nLastY + rMoveOuter.nDY );
550 }
551 }
552
553 aArr[ nPolyPos ].setX( nFirstX - 1 );
554 aArr[ nPolyPos++ ].setY( nFirstY - 1 );
555 aArr.ImplSetRealSize( nPolyPos );
556 }
557 else
558 {
559 tools::Long nLastX = maStartPt.X(), nLastY = maStartPt.Y();
560
561 aArr.ImplSetSize( mnCount + 1 );
562 aArr[ 0 ] = Point( nLastX, nLastY );
563
564 for( sal_uLong i = 0; i < mnCount; )
565 {
566 const ChainMove& rMove = aImplMove[ mpCodes[ i ] ];
567 nLastX += rMove.nDX;
568 nLastY += rMove.nDY;
569 aArr[ ++i ] = Point( nLastX, nLastY );
570 }
571
572 aArr.ImplSetRealSize( mnCount + 1 );
573 }
574
575 ImplPostProcess( aArr );
576 }
577 else
578 maPoly.SetSize( 0 );
579}
580
581void ImplChain::ImplPostProcess( const ImplPointArray& rArr )
582{
583 ImplPointArray aNewArr1;
584 ImplPointArray aNewArr2;
585 Point* pLast;
586 Point* pLeast;
587 sal_uLong nNewPos;
588 sal_uLong nCount = rArr.ImplGetRealSize();
589 sal_uLong n;
590
591 // pass 1
592 aNewArr1.ImplSetSize( nCount );
593 pLast = &( aNewArr1[ 0 ] );
594 pLast->setX( BACK_MAP( rArr[ 0 ].X() ) );
595 pLast->setY( BACK_MAP( rArr[ 0 ].Y() ) );
596
597 for( n = nNewPos = 1; n < nCount; )
598 {
599 const Point& rPt = rArr[ n++ ];
600 const tools::Long nX = BACK_MAP( rPt.X() );
601 const tools::Long nY = BACK_MAP( rPt.Y() );
602
603 if( nX != pLast->X() || nY != pLast->Y() )
604 {
605 pLast = pLeast = &( aNewArr1[ nNewPos++ ] );
606 pLeast->setX( nX );
607 pLeast->setY( nY );
608 }
609 }
610
611 nCount = nNewPos;
612 aNewArr1.ImplSetRealSize( nCount );
613
614 // pass 2
615 aNewArr2.ImplSetSize( nCount );
616 pLast = &( aNewArr2[ 0 ] );
617 *pLast = aNewArr1[ 0 ];
618
619 for( n = nNewPos = 1; n < nCount; )
620 {
621 pLeast = &( aNewArr1[ n++ ] );
622
623 if( pLeast->X() == pLast->X() )
624 {
625 while( n < nCount && aNewArr1[ n ].X() == pLast->X() )
626 pLeast = &( aNewArr1[ n++ ] );
627 }
628 else if( pLeast->Y() == pLast->Y() )
629 {
630 while( n < nCount && aNewArr1[ n ].Y() == pLast->Y() )
631 pLeast = &( aNewArr1[ n++ ] );
632 }
633
634 pLast = pLeast;
635 aNewArr2[ nNewPos++ ] = *pLast;
636 }
637
638 aNewArr2.ImplSetRealSize( nNewPos );
639 aNewArr2.ImplCreatePoly( maPoly );
640}
641
642namespace ImplVectorizer {
643
644bool ImplVectorize( const Bitmap& rColorBmp, GDIMetaFile& rMtf,
645 sal_uInt8 cReduce, const Link<tools::Long,void>* pProgress )
646{
647 bool bRet = false;
648
649 VECT_PROGRESS( pProgress, 0 );
650
651 std::optional<Bitmap> xBmp(std::in_place, rColorBmp );
652 Bitmap::ScopedReadAccess pRAcc(*xBmp);
653
654 if( pRAcc )
655 {
656 double fPercent = 0.0;
657 double fPercentStep_2 = 0.0;
658 const tools::Long nWidth = pRAcc->Width();
659 const tools::Long nHeight = pRAcc->Height();
660 const sal_uInt16 nColorCount = pRAcc->GetPaletteEntryCount();
661 sal_uInt16 n;
662 std::array<ImplColorSet, 256> aColorSet;
663
664 rMtf.Clear();
665
666 // get used palette colors and sort them from light to dark colors
667 for( n = 0; n < nColorCount; n++ )
668 {
669 aColorSet[ n ].mnIndex = n;
670 aColorSet[ n ].maColor = pRAcc->GetPaletteColor( n );
671 }
672
673 for( tools::Long nY = 0; nY < nHeight; nY++ )
674 {
675 Scanline pScanlineRead = pRAcc->GetScanline( nY );
676 for( tools::Long nX = 0; nX < nWidth; nX++ )
677 aColorSet[ pRAcc->GetIndexFromData( pScanlineRead, nX ) ].mbSet = true;
678 }
679
680 std::sort( aColorSet.begin(), aColorSet.end(), ImplColorSetCmpFnc );
681
682 for( n = 0; n < 256; n++ )
683 if( !aColorSet[ n ].mbSet )
684 break;
685
686 if( n )
687 fPercentStep_2 = 45.0 / n;
688
689 fPercent += 10.0;
690 VECT_PROGRESS( pProgress, FRound( fPercent ) );
691
692 for( sal_uInt16 i = 0; i < n; i++ )
693 {
694 const BitmapColor aBmpCol( pRAcc->GetPaletteColor( aColorSet[ i ].mnIndex ) );
695 const Color aFindColor( aBmpCol.GetRed(), aBmpCol.GetGreen(), aBmpCol.GetBlue() );
696 std::optional<ImplVectMap> oMap;
697 ImplExpand( oMap, pRAcc.get(), aFindColor );
698
699 fPercent += fPercentStep_2;
700 VECT_PROGRESS( pProgress, FRound( fPercent ) );
701
702 if( oMap )
703 {
704 tools::PolyPolygon aPolyPoly;
705 ImplCalculate( *oMap, aPolyPoly, cReduce );
706 oMap.reset();
707
708 if( aPolyPoly.Count() )
709 {
710 ImplLimitPolyPoly( aPolyPoly );
711
712 aPolyPoly.Optimize( PolyOptimizeFlags::EDGES );
713
714 if( aPolyPoly.Count() )
715 {
716 rMtf.AddAction( new MetaLineColorAction( aFindColor, true ) );
717 rMtf.AddAction( new MetaFillColorAction( aFindColor, true ) );
718 rMtf.AddAction( new MetaPolyPolygonAction( std::move(aPolyPoly) ) );
719 }
720 }
721 }
722
723 fPercent += fPercentStep_2;
724 VECT_PROGRESS( pProgress, FRound( fPercent ) );
725 }
726
727 if( rMtf.GetActionSize() )
728 {
729 MapMode aMap( MapUnit::Map100thMM );
731 const Size aLogSize1( aVDev->PixelToLogic( Size( 1, 1 ), aMap ) );
732
733 rMtf.SetPrefMapMode( aMap );
734 rMtf.SetPrefSize( Size( nWidth + 2, nHeight + 2 ) );
735 rMtf.Move( 1, 1 );
736 rMtf.Scale( aLogSize1.Width(), aLogSize1.Height() );
737 bRet = true;
738 }
739 }
740
741 pRAcc.reset();
742 xBmp.reset();
743 VECT_PROGRESS( pProgress, 100 );
744
745 return bRet;
746}
747
749{
750 if( rPolyPoly.Count() <= VECT_POLY_MAX )
751 return;
752
753 tools::PolyPolygon aNewPolyPoly;
754 tools::Long nReduce = 0;
755 sal_uInt16 nNewCount;
756
757 do
758 {
759 aNewPolyPoly.Clear();
760 nReduce++;
761
762 for( sal_uInt16 i = 0, nCount = rPolyPoly.Count(); i < nCount; i++ )
763 {
764 const tools::Rectangle aBound( rPolyPoly[ i ].GetBoundRect() );
765
766 if( aBound.GetWidth() > nReduce && aBound.GetHeight() > nReduce )
767 {
768 if( rPolyPoly[ i ].GetSize() )
769 aNewPolyPoly.Insert( rPolyPoly[ i ] );
770 }
771 }
772
773 nNewCount = aNewPolyPoly.Count();
774 }
775 while( nNewCount > VECT_POLY_MAX );
776
777 rPolyPoly = aNewPolyPoly;
778}
779
780void ImplExpand( std::optional<ImplVectMap>& oMap, const BitmapReadAccess* pRAcc, const Color& rColor )
781{
782 if( !pRAcc || !pRAcc->Width() || !pRAcc->Height() )
783 return;
784
785 const tools::Long nOldWidth = pRAcc->Width();
786 const tools::Long nOldHeight = pRAcc->Height();
787 const tools::Long nNewWidth = ( nOldWidth << 2 ) + 4;
788 const tools::Long nNewHeight = ( nOldHeight << 2 ) + 4;
789 const BitmapColor aTest( pRAcc->GetBestMatchingColor( rColor ) );
790 std::unique_ptr<sal_Int32[]> pMapIn(new sal_Int32[ std::max( nOldWidth, nOldHeight ) ]);
791 std::unique_ptr<sal_Int32[]> pMapOut(new sal_Int32[ std::max( nOldWidth, nOldHeight ) ]);
792 tools::Long nX, nY, nTmpX, nTmpY;
793
794 oMap.emplace( nNewWidth, nNewHeight );
795
796 for( nX = 0; nX < nOldWidth; nX++ )
797 VECT_MAP( pMapIn, pMapOut, nX );
798
799 for( nY = 0, nTmpY = 5; nY < nOldHeight; nY++, nTmpY += 4 )
800 {
801 Scanline pScanlineRead = pRAcc->GetScanline( nY );
802 for( nX = 0; nX < nOldWidth; )
803 {
804 if( pRAcc->GetPixelFromData( pScanlineRead, nX ) == aTest )
805 {
806 nTmpX = pMapIn[ nX++ ];
807 nTmpY -= 3;
808
809 oMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
810 oMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
811 oMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
812 oMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
813
814 while( nX < nOldWidth && pRAcc->GetPixelFromData( pScanlineRead, nX ) == aTest )
815 nX++;
816
817 nTmpX = pMapOut[ nX - 1 ];
818 nTmpY -= 3;
819
820 oMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
821 oMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
822 oMap->Set( nTmpY++, nTmpX, VECT_CONT_INDEX );
823 oMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
824 }
825 else
826 nX++;
827 }
828 }
829
830 for( nY = 0; nY < nOldHeight; nY++ )
831 VECT_MAP( pMapIn, pMapOut, nY );
832
833 for( nX = 0, nTmpX = 5; nX < nOldWidth; nX++, nTmpX += 4 )
834 {
835 for( nY = 0; nY < nOldHeight; )
836 {
837 if( pRAcc->GetPixel( nY, nX ) == aTest )
838 {
839 nTmpX -= 3;
840 nTmpY = pMapIn[ nY++ ];
841
842 oMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
843 oMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
844 oMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
845 oMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
846
847 while( nY < nOldHeight && pRAcc->GetPixel( nY, nX ) == aTest )
848 nY++;
849
850 nTmpX -= 3;
851 nTmpY = pMapOut[ nY - 1 ];
852
853 oMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
854 oMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
855 oMap->Set( nTmpY, nTmpX++, VECT_CONT_INDEX );
856 oMap->Set( nTmpY, nTmpX, VECT_CONT_INDEX );
857 }
858 else
859 nY++;
860 }
861 }
862}
863
864void ImplCalculate( ImplVectMap& rMap, tools::PolyPolygon& rPolyPoly, sal_uInt8 cReduce )
865{
866 const tools::Long nWidth = rMap.Width(), nHeight = rMap.Height();
867
868 for( tools::Long nY = 0; nY < nHeight; nY++ )
869 {
870 tools::Long nX = 0;
871 bool bInner = true;
872
873 while( nX < nWidth )
874 {
875 // skip free
876 while( ( nX < nWidth ) && rMap.IsFree( nY, nX ) )
877 nX++;
878
879 if( nX == nWidth )
880 break;
881
882 if( rMap.IsCont( nY, nX ) )
883 {
884 // new contour
885 ImplChain aChain;
886 const Point aStartPt( nX++, nY );
887
888 // get chain code
889 aChain.ImplBeginAdd( aStartPt );
890 ImplGetChain( rMap, aStartPt, aChain );
891
892 aChain.ImplEndAdd( bInner ? VECT_POLY_OUTLINE_INNER : VECT_POLY_OUTLINE_OUTER );
893
894 const tools::Polygon& rPoly = aChain.ImplGetPoly();
895
896 if( rPoly.GetSize() > 2 )
897 {
898 if( cReduce )
899 {
900 const tools::Rectangle aBound( rPoly.GetBoundRect() );
901
902 if( aBound.GetWidth() > cReduce && aBound.GetHeight() > cReduce )
903 rPolyPoly.Insert( rPoly );
904 }
905 else
906 rPolyPoly.Insert( rPoly );
907 }
908
909 // skip rest of detected contour
910 while( rMap.IsCont( nY, nX ) )
911 nX++;
912 }
913 else
914 {
915 // process done segment
916 const tools::Long nStartSegX = nX++;
917
918 while( rMap.IsDone( nY, nX ) )
919 nX++;
920
921 if( ( ( nX - nStartSegX ) == 1 ) || ( ImplIsUp( rMap, nY, nStartSegX ) != ImplIsUp( rMap, nY, nX - 1 ) ) )
922 bInner = !bInner;
923 }
924 }
925 }
926}
927
928bool ImplGetChain( ImplVectMap& rMap, const Point& rStartPt, ImplChain& rChain )
929{
930 tools::Long nActX = rStartPt.X();
931 tools::Long nActY = rStartPt.Y();
932 sal_uLong nFound;
933 sal_uLong nLastDir = 0;
934 sal_uLong nDir;
935
936 do
937 {
938 nFound = 0;
939
940 // first try last direction
941 tools::Long nTryX = nActX + aImplMove[ nLastDir ].nDX;
942 tools::Long nTryY = nActY + aImplMove[ nLastDir ].nDY;
943
944 if( rMap.IsCont( nTryY, nTryX ) )
945 {
946 rChain.ImplAdd( static_cast<sal_uInt8>(nLastDir) );
947 nActY = nTryY;
948 nActX = nTryX;
949 rMap.Set( nActY, nActX, VECT_DONE_INDEX );
950 nFound = 1;
951 }
952 else
953 {
954 // try other directions
955 for( nDir = 0; nDir < 8; nDir++ )
956 {
957 // we already tried nLastDir
958 if( nDir != nLastDir )
959 {
960 nTryX = nActX + aImplMove[ nDir ].nDX;
961 nTryY = nActY + aImplMove[ nDir ].nDY;
962
963 if( rMap.IsCont( nTryY, nTryX ) )
964 {
965 rChain.ImplAdd( static_cast<sal_uInt8>(nDir) );
966 nActY = nTryY;
967 nActX = nTryX;
968 rMap.Set( nActY, nActX, VECT_DONE_INDEX );
969 nFound = 1;
970 nLastDir = nDir;
971 break;
972 }
973 }
974 }
975 }
976 }
977 while( nFound );
978
979 return true;
980}
981
982bool ImplIsUp( ImplVectMap const & rMap, tools::Long nY, tools::Long nX )
983{
984 if( rMap.IsDone( nY - 1, nX ) )
985 return true;
986 else if( rMap.IsDone( nY + 1, nX ) )
987 return false;
988 else if( rMap.IsDone( nY - 1, nX - 1 ) || rMap.IsDone( nY - 1, nX + 1 ) )
989 return true;
990 else
991 return false;
992}
993
994}
995
996/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
basegfx::BColor maColor
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
SbxDimArrayRef mpArray
tools::Long Height() const
tools::Long Width() const
BitmapColor GetBestMatchingColor(const BitmapColor &rBitmapColor) const
sal_uInt16 GetPaletteEntryCount() const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
BitmapColor GetPixel(tools::Long nY, tools::Long nX) const
BitmapColor GetPixelFromData(const sal_uInt8 *pData, tools::Long nX) const
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, tools::Long nX) const
Scanline GetScanline(tools::Long nY) const
sal_uInt8 GetBlue() const
sal_uInt8 GetRed() const
sal_uInt8 GetGreen() const
size_t GetActionSize() const
Definition: gdimtf.cxx:181
void Move(tools::Long nX, tools::Long nY)
Definition: gdimtf.cxx:653
void Scale(double fScaleX, double fScaleY)
Definition: gdimtf.cxx:748
void AddAction(const rtl::Reference< MetaAction > &pAction)
Definition: gdimtf.cxx:585
void Clear()
Definition: gdimtf.cxx:273
void SetPrefMapMode(const MapMode &rMapMode)
Definition: gdimtf.hxx:180
void SetPrefSize(const Size &rSize)
Definition: gdimtf.hxx:177
constexpr tools::Long Y() const
void setX(tools::Long nX)
void setY(tools::Long nY)
constexpr tools::Long X() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
sal_uInt16 Count() const
void Insert(const tools::Polygon &rPoly, sal_uInt16 nPos=POLYPOLY_APPEND)
void Optimize(PolyOptimizeFlags nOptimizeFlags)
sal_uInt16 GetSize() const
tools::Rectangle GetBoundRect() const
constexpr tools::Long GetWidth() const
constexpr tools::Long GetHeight() const
T * get() const
int nCount
sal_uInt32 mnSize
std::size_t mnCount
tools::Long FRound(double fVal)
static constexpr tools::Long BACK_MAP(tools::Long _def_nVal)
Definition: impvect.cxx:48
const ChainMove aImplMove[8]
Definition: impvect.cxx:80
static bool ImplColorSetCmpFnc(const ImplColorSet &lhs, const ImplColorSet &rhs)
Definition: impvect.cxx:124
#define VECT_POLY_OUTLINE_OUTER
Definition: impvect.cxx:41
#define VECT_CONT_INDEX
Definition: impvect.cxx:35
#define VECT_FREE_INDEX
Definition: impvect.cxx:34
#define VECT_DONE_INDEX
Definition: impvect.cxx:36
static void VECT_MAP(const std::unique_ptr< sal_Int32[]> &pMapIn, const std::unique_ptr< sal_Int32[]> &pMapOut, tools::Long nVal)
Definition: impvect.cxx:43
#define VECT_POLY_OUTLINE_INNER
Definition: impvect.cxx:40
static void VECT_PROGRESS(const Link< tools::Long, void > *pProgress, tools::Long _def_nVal)
Definition: impvect.cxx:52
#define VECT_POLY_INLINE_OUTER
Definition: impvect.cxx:39
const ChainMove aImplMoveInner[8]
Definition: impvect.cxx:91
#define VECT_POLY_MAX
Definition: impvect.cxx:32
#define VECT_POLY_INLINE_INNER
Definition: impvect.cxx:38
const ChainMove aImplMoveOuter[8]
Definition: impvect.cxx:102
sal_Int64 n
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
static bool ImplGetChain(ImplVectMap &rMap, const Point &rStartPt, ImplChain &rChain)
Definition: impvect.cxx:928
static void ImplCalculate(ImplVectMap &rMap, tools::PolyPolygon &rPolyPoly, sal_uInt8 cReduce)
Definition: impvect.cxx:864
static void ImplExpand(std::optional< ImplVectMap > &rMap, const BitmapReadAccess *pRAcc, const Color &rColor)
Definition: impvect.cxx:780
static bool ImplIsUp(ImplVectMap const &rMap, tools::Long nY, tools::Long nX)
Definition: impvect.cxx:982
static void ImplLimitPolyPoly(tools::PolyPolygon &rPolyPoly)
Definition: impvect.cxx:748
bool ImplVectorize(const Bitmap &rColorBmp, GDIMetaFile &rMtf, sal_uInt8 cReduce, const Link< tools::Long, void > *pProgress)
Definition: impvect.cxx:644
int i
const SvxPageUsage aArr[]
long Long
HashMap_OWString_Interface aMap
#define Y
double mnWidth
double mnHeight
sal_uIntPtr sal_uLong
sal_uInt32 mnIndex
unsigned char sal_uInt8