LibreOffice Module vcl (master) 1
BitmapTools.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 */
10
11#include <sal/config.h>
12
13#include <array>
14#include <utility>
15
16#include <tools/helpers.hxx>
17#include <vcl/BitmapTools.hxx>
18
19#include <sal/log.hxx>
22#include <vcl/canvastools.hxx>
24
25#include <com/sun/star/graphic/SvgTools.hpp>
26#include <com/sun/star/graphic/Primitive2DTools.hpp>
27
29
30#include <com/sun/star/rendering/XIntegerReadOnlyBitmap.hpp>
31
32#include <vcl/dibtools.hxx>
33#include <vcl/settings.hxx>
34#include <vcl/svapp.hxx>
35#include <vcl/virdev.hxx>
36#if ENABLE_CAIRO_CANVAS
37#include <cairo.h>
38#endif
40#include <tools/fract.hxx>
41#include <tools/stream.hxx>
43
44using namespace css;
45
48
49namespace vcl::bitmap
50{
51
52BitmapEx loadFromName(const OUString& rFileName, const ImageLoadFlags eFlags)
53{
54 bool bSuccess = true;
55 OUString aIconTheme;
56 BitmapEx aBitmapEx;
57 try
58 {
60 ImageTree::get().loadImage(rFileName, aIconTheme, aBitmapEx, true, eFlags);
61 }
62 catch (...)
63 {
64 bSuccess = false;
65 }
66
67 SAL_WARN_IF(!bSuccess, "vcl", "vcl::bitmap::loadFromName : could not load image " << rFileName << " via icon theme " << aIconTheme);
68
69 return aBitmapEx;
70}
71
72void loadFromSvg(SvStream& rStream, const OUString& sPath, BitmapEx& rBitmapEx, double fScalingFactor)
73{
74 uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());
75 const uno::Reference<graphic::XSvgParser> xSvgParser = graphic::SvgTools::create(xContext);
76
77 std::size_t nSize = rStream.remainingSize();
78 std::vector<sal_Int8> aBuffer(nSize + 1);
79 rStream.ReadBytes(aBuffer.data(), nSize);
80 aBuffer[nSize] = 0;
81
82 uno::Sequence<sal_Int8> aData(aBuffer.data(), nSize + 1);
83 uno::Reference<io::XInputStream> aInputStream(new comphelper::SequenceInputStream(aData));
84
85 const Primitive2DSequence aPrimitiveSequence = xSvgParser->getDecomposition(aInputStream, sPath);
86
87 if (!aPrimitiveSequence.hasElements())
88 return;
89
90 uno::Sequence<beans::PropertyValue> aViewParameters;
91
92 geometry::RealRectangle2D aRealRect;
93 basegfx::B2DRange aRange;
94 for (css::uno::Reference<css::graphic::XPrimitive2D> const & xReference : aPrimitiveSequence)
95 {
96 if (xReference.is())
97 {
98 aRealRect = xReference->getRange(aViewParameters);
99 aRange.expand(basegfx::B2DRange(aRealRect.X1, aRealRect.Y1, aRealRect.X2, aRealRect.Y2));
100 }
101 }
102
103 aRealRect.X1 = aRange.getMinX();
104 aRealRect.Y1 = aRange.getMinY();
105 aRealRect.X2 = aRange.getMaxX();
106 aRealRect.Y2 = aRange.getMaxY();
107
108 double nDPI = 96 * fScalingFactor;
109
110 const css::uno::Reference<css::graphic::XPrimitive2DRenderer> xPrimitive2DRenderer = css::graphic::Primitive2DTools::create(xContext);
111 const css::uno::Reference<css::rendering::XBitmap> xBitmap(
112 xPrimitive2DRenderer->rasterize(aPrimitiveSequence, aViewParameters, nDPI, nDPI, aRealRect, 256*256));
113
114 if (xBitmap.is())
115 {
116 const css::uno::Reference<css::rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
117 rBitmapEx = vcl::unotools::bitmapExFromXBitmap(xIntBmp);
118 }
119
120}
121
132BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight,
133 sal_Int32 nStride, sal_Int8 nBitCount,
134 bool bReversColors, bool bReverseAlpha)
135{
136 assert(nStride >= (nWidth * nBitCount / 8));
137 assert(nBitCount == 1 || nBitCount == 8 || nBitCount == 24 || nBitCount == 32);
138
139 PixelFormat ePixelFormat;
140 if (nBitCount == 1)
141 ePixelFormat = PixelFormat::N8_BPP; // we convert 1-bit input data to 8-bit format
142 else if (nBitCount == 8)
143 ePixelFormat = PixelFormat::N8_BPP;
144 else if (nBitCount == 24)
145 ePixelFormat = PixelFormat::N24_BPP;
146 else if (nBitCount == 32)
147 ePixelFormat = PixelFormat::N32_BPP;
148 else
149 std::abort();
150 Bitmap aBmp;
151 if (nBitCount == 1)
152 {
153 BitmapPalette aBiLevelPalette { COL_BLACK, COL_WHITE };
154 aBmp = Bitmap(Size(nWidth, nHeight), PixelFormat::N8_BPP, &aBiLevelPalette);
155 }
156 else
157 aBmp = Bitmap(Size(nWidth, nHeight), ePixelFormat);
158
159 BitmapScopedWriteAccess pWrite(aBmp);
160 assert(pWrite.get());
161 if( !pWrite )
162 return BitmapEx();
163 std::optional<AlphaMask> pAlphaMask;
164 AlphaScopedWriteAccess xMaskAcc;
165 if (nBitCount == 32)
166 {
167 pAlphaMask.emplace( Size(nWidth, nHeight) );
168 xMaskAcc = AlphaScopedWriteAccess(*pAlphaMask);
169 }
170 if (nBitCount == 1)
171 {
172 for( tools::Long y = 0; y < nHeight; ++y )
173 {
174 sal_uInt8 const *p = pData + y * nStride / 8;
175 Scanline pScanline = pWrite->GetScanline(y);
176 for (tools::Long x = 0; x < nWidth; ++x)
177 {
178 int bitIndex = (y * nStride + x) % 8;
179
180 pWrite->SetPixelOnData(pScanline, x, BitmapColor((*p >> bitIndex) & 1));
181 }
182 }
183 }
184 else
185 {
186 for( tools::Long y = 0; y < nHeight; ++y )
187 {
188 sal_uInt8 const *p = pData + (y * nStride);
189 Scanline pScanline = pWrite->GetScanline(y);
190 for (tools::Long x = 0; x < nWidth; ++x)
191 {
193 if (nBitCount == 8)
194 col = BitmapColor( *p );
195 else if ( bReversColors )
196 col = BitmapColor( p[2], p[1], p[0] );
197 else
198 col = BitmapColor( p[0], p[1], p[2] );
199 pWrite->SetPixelOnData(pScanline, x, col);
200 p += nBitCount/8;
201 }
202 if (nBitCount == 32)
203 {
204 p = pData + (y * nStride) + 3;
205 Scanline pMaskScanLine = xMaskAcc->GetScanline(y);
206 for (tools::Long x = 0; x < nWidth; ++x)
207 {
208 const sal_uInt8 nValue = bReverseAlpha ? 0xff - *p : *p;
209 xMaskAcc->SetPixelOnData(pMaskScanLine, x, BitmapColor(nValue));
210 p += 4;
211 }
212 }
213 }
214 }
215 // Avoid further bitmap use with unfinished write access
216 pWrite.reset();
217 xMaskAcc.reset();
218 if (nBitCount == 32)
219 return BitmapEx(aBmp, *pAlphaMask);
220 else
221 return BitmapEx(aBmp);
222}
223
228{
229 auto nBitCount = rawBitmap.GetBitCount();
230 assert( nBitCount == 24 || nBitCount == 32);
231
232 auto ePixelFormat = vcl::PixelFormat::INVALID;
233
234 if (nBitCount == 24)
235 ePixelFormat = vcl::PixelFormat::N24_BPP;
236 else if (nBitCount == 32)
237 ePixelFormat = vcl::PixelFormat::N32_BPP;
238
239 assert(ePixelFormat != vcl::PixelFormat::INVALID);
240
241 Bitmap aBmp(rawBitmap.maSize, ePixelFormat);
242
243 BitmapScopedWriteAccess pWrite(aBmp);
244 assert(pWrite.get());
245 if( !pWrite )
246 return BitmapEx();
247 std::optional<AlphaMask> pAlphaMask;
248 AlphaScopedWriteAccess xMaskAcc;
249 if (nBitCount == 32)
250 {
251 pAlphaMask.emplace( rawBitmap.maSize );
252 xMaskAcc = AlphaScopedWriteAccess(*pAlphaMask);
253 }
254
255 auto nHeight = rawBitmap.maSize.getHeight();
256 auto nWidth = rawBitmap.maSize.getWidth();
257 auto nStride = nWidth * nBitCount / 8;
258 for( tools::Long y = 0; y < nHeight; ++y )
259 {
260 sal_uInt8 const *p = rawBitmap.mpData.get() + (y * nStride);
261 Scanline pScanline = pWrite->GetScanline(y);
262 for (tools::Long x = 0; x < nWidth; ++x)
263 {
264 BitmapColor col(p[0], p[1], p[2]);
265 pWrite->SetPixelOnData(pScanline, x, col);
266 p += nBitCount/8;
267 }
268 if (nBitCount == 32)
269 {
270 p = rawBitmap.mpData.get() + (y * nStride) + 3;
271 Scanline pMaskScanLine = xMaskAcc->GetScanline(y);
272 for (tools::Long x = 0; x < nWidth; ++x)
273 {
274 xMaskAcc->SetPixelOnData(pMaskScanLine, x, BitmapColor(255 - *p));
275 p += 4;
276 }
277 }
278 }
279 if (nBitCount == 32)
280 return BitmapEx(aBmp, *pAlphaMask);
281 else
282 return BitmapEx(aBmp);
283}
284
285#if ENABLE_CAIRO_CANVAS
286BitmapEx* CreateFromCairoSurface(Size aSize, cairo_surface_t * pSurface)
287{
288 // FIXME: if we could teach VCL/ about cairo handles, life could
289 // be significantly better here perhaps.
290
291 cairo_surface_t *pPixels = cairo_surface_create_similar_image(pSurface,
292 CAIRO_FORMAT_ARGB32, aSize.Width(), aSize.Height());
293 cairo_t *pCairo = cairo_create( pPixels );
294 if( !pPixels || !pCairo || cairo_status(pCairo) != CAIRO_STATUS_SUCCESS )
295 return nullptr;
296
297 // suck ourselves from the X server to this buffer so then we can fiddle with
298 // Alpha to turn it into the ultra-lame vcl required format and then push it
299 // all back again later at vast expense [ urgh ]
300 cairo_set_source_surface( pCairo, pSurface, 0, 0 );
301 cairo_set_operator( pCairo, CAIRO_OPERATOR_SOURCE );
302 cairo_paint( pCairo );
303
305 ::AlphaMask aMask( aSize );
306
307 BitmapScopedWriteAccess pRGBWrite(aRGB);
308 assert(pRGBWrite);
309 if (!pRGBWrite)
310 return nullptr;
311
312 AlphaScopedWriteAccess pMaskWrite(aMask);
313 assert(pMaskWrite);
314 if (!pMaskWrite)
315 return nullptr;
316
317 cairo_surface_flush(pPixels);
318 unsigned char *pSrc = cairo_image_surface_get_data( pPixels );
319 unsigned int nStride = cairo_image_surface_get_stride( pPixels );
320#if !ENABLE_WASM_STRIP_PREMULTIPLY
322#endif
323 for( tools::Long y = 0; y < aSize.Height(); y++ )
324 {
325 sal_uInt32 *pPix = reinterpret_cast<sal_uInt32 *>(pSrc + nStride * y);
326 for( tools::Long x = 0; x < aSize.Width(); x++ )
327 {
328#if defined OSL_BIGENDIAN
329 sal_uInt8 nB = (*pPix >> 24);
330 sal_uInt8 nG = (*pPix >> 16) & 0xff;
331 sal_uInt8 nR = (*pPix >> 8) & 0xff;
332 sal_uInt8 nAlpha = *pPix & 0xff;
333#else
334 sal_uInt8 nAlpha = (*pPix >> 24);
335 sal_uInt8 nR = (*pPix >> 16) & 0xff;
336 sal_uInt8 nG = (*pPix >> 8) & 0xff;
337 sal_uInt8 nB = *pPix & 0xff;
338#endif
339 if( nAlpha != 0 && nAlpha != 255 )
340 {
341 // Cairo uses pre-multiplied alpha - we do not => re-multiply
342#if ENABLE_WASM_STRIP_PREMULTIPLY
343 nR = vcl::bitmap::unpremultiply(nAlpha, nR);
344 nG = vcl::bitmap::unpremultiply(nAlpha, nG);
345 nB = vcl::bitmap::unpremultiply(nAlpha, nB);
346#else
347 nR = unpremultiply_table[nAlpha][nR];
348 nG = unpremultiply_table[nAlpha][nG];
349 nB = unpremultiply_table[nAlpha][nB];
350#endif
351 }
352 pRGBWrite->SetPixel( y, x, BitmapColor( nR, nG, nB ) );
353 pMaskWrite->SetPixelIndex( y, x, 255 - nAlpha );
354 pPix++;
355 }
356 }
357
358 // ignore potential errors above. will get caller a
359 // uniformly white bitmap, but not that there would
360 // be error handling in calling code ...
361 ::BitmapEx *pBitmapEx = new ::BitmapEx( aRGB, aMask );
362
363 cairo_destroy( pCairo );
364 cairo_surface_destroy( pPixels );
365 return pBitmapEx;
366}
367#endif
368
370 const ::basegfx::B2DHomMatrix& rTransform,
371 ::basegfx::B2DRectangle const & rDestRect,
372 ::basegfx::B2DHomMatrix const & rLocalTransform )
373{
374 const Size aBmpSize( rBitmap.GetSizePixel() );
375 Bitmap aSrcBitmap( rBitmap.GetBitmap() );
376 Bitmap aSrcAlpha;
377
378 // differentiate mask and alpha channel (on-off
379 // vs. multi-level transparency)
380 if( rBitmap.IsAlpha() )
381 {
382 aSrcAlpha = rBitmap.GetAlphaMask().GetBitmap();
383 }
384
385 Bitmap::ScopedReadAccess pReadAccess( aSrcBitmap );
386 Bitmap::ScopedReadAccess pAlphaReadAccess( rBitmap.IsAlpha() ?
387 aSrcAlpha.AcquireReadAccess() :
388 nullptr,
389 aSrcAlpha );
390
391 if( pReadAccess.get() == nullptr ||
392 (pAlphaReadAccess.get() == nullptr && rBitmap.IsAlpha()) )
393 {
394 // TODO(E2): Error handling!
395 ENSURE_OR_THROW( false,
396 "transformBitmap(): could not access source bitmap" );
397 }
398
399 // mapping table, to translate pAlphaReadAccess' pixel
400 // values into destination alpha values (needed e.g. for
401 // paletted 1-bit masks).
402 sal_uInt8 aAlphaMap[256];
403
404 if( rBitmap.IsAlpha() )
405 {
406 // source already has alpha channel - 1:1 mapping,
407 // i.e. aAlphaMap[0]=0,...,aAlphaMap[255]=255.
408 sal_uInt8 val=0;
409 sal_uInt8* pCur=aAlphaMap;
410 sal_uInt8* const pEnd=&aAlphaMap[256];
411 while(pCur != pEnd)
412 *pCur++ = val++;
413 }
414 // else: mapping table is not used
415
416 const Size aDestBmpSize( ::basegfx::fround( rDestRect.getWidth() ),
417 ::basegfx::fround( rDestRect.getHeight() ) );
418
419 if( aDestBmpSize.IsEmpty() )
420 return BitmapEx();
421
422 Bitmap aDstBitmap(aDestBmpSize, aSrcBitmap.getPixelFormat(), &pReadAccess->GetPalette());
423 Bitmap aDstAlpha( AlphaMask( aDestBmpSize ).GetBitmap() );
424
425 {
426 // just to be on the safe side: let the
427 // ScopedAccessors get destructed before
428 // copy-constructing the resulting bitmap. This will
429 // rule out the possibility that cached accessor data
430 // is not yet written back.
431 BitmapScopedWriteAccess pWriteAccess( aDstBitmap );
432 BitmapScopedWriteAccess pAlphaWriteAccess( aDstAlpha );
433
434
435 if( pWriteAccess.get() != nullptr &&
436 pAlphaWriteAccess.get() != nullptr &&
437 rTransform.isInvertible() )
438 {
439 // we're doing inverse mapping here, i.e. mapping
440 // points from the destination bitmap back to the
441 // source
442 ::basegfx::B2DHomMatrix aTransform( rLocalTransform );
443 aTransform.invert();
444
445 // for the time being, always read as ARGB
446 for( tools::Long y=0; y<aDestBmpSize.Height(); ++y )
447 {
448 // differentiate mask and alpha channel (on-off
449 // vs. multi-level transparency)
450 if( rBitmap.IsAlpha() )
451 {
452 Scanline pScan = pWriteAccess->GetScanline( y );
453 Scanline pScanAlpha = pAlphaWriteAccess->GetScanline( y );
454 // Handling alpha and mask just the same...
455 for( tools::Long x=0; x<aDestBmpSize.Width(); ++x )
456 {
457 ::basegfx::B2DPoint aPoint(x,y);
458 aPoint *= aTransform;
459
460 const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
461 const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
462 if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
463 nSrcY < 0 || nSrcY >= aBmpSize.Height() )
464 {
465 pAlphaWriteAccess->SetPixelOnData( pScanAlpha, x, BitmapColor(255) );
466 }
467 else
468 {
469 const sal_uInt8 cAlphaIdx = pAlphaReadAccess->GetPixelIndex( nSrcY, nSrcX );
470 pAlphaWriteAccess->SetPixelOnData( pScanAlpha, x, BitmapColor(aAlphaMap[ cAlphaIdx ]) );
471 pWriteAccess->SetPixelOnData( pScan, x, pReadAccess->GetPixel( nSrcY, nSrcX ) );
472 }
473 }
474 }
475 else
476 {
477 Scanline pScan = pWriteAccess->GetScanline( y );
478 Scanline pScanAlpha = pAlphaWriteAccess->GetScanline( y );
479 for( tools::Long x=0; x<aDestBmpSize.Width(); ++x )
480 {
481 ::basegfx::B2DPoint aPoint(x,y);
482 aPoint *= aTransform;
483
484 const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
485 const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
486 if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
487 nSrcY < 0 || nSrcY >= aBmpSize.Height() )
488 {
489 pAlphaWriteAccess->SetPixelOnData( pScanAlpha, x, BitmapColor(255) );
490 }
491 else
492 {
493 pAlphaWriteAccess->SetPixelOnData( pScanAlpha, x, BitmapColor(0) );
494 pWriteAccess->SetPixelOnData( pScan, x, pReadAccess->GetPixel( nSrcY,
495 nSrcX ) );
496 }
497 }
498 }
499 }
500 }
501 else
502 {
503 // TODO(E2): Error handling!
504 ENSURE_OR_THROW( false,
505 "transformBitmap(): could not access bitmap" );
506 }
507 }
508
509 return BitmapEx(aDstBitmap, AlphaMask(aDstAlpha));
510}
511
512
513void DrawAlphaBitmapAndAlphaGradient(BitmapEx & rBitmapEx, bool bFixedTransparence, float fTransparence, AlphaMask & rNewMask)
514{
515 // mix existing and new alpha mask
516 AlphaMask aOldMask;
517
518 if(rBitmapEx.IsAlpha())
519 {
520 aOldMask = rBitmapEx.GetAlphaMask();
521 }
522
523 {
524 AlphaScopedWriteAccess pOld(aOldMask);
525
526 assert(pOld && "Got no access to old alpha mask (!)");
527
528 const double fFactor(1.0 / 255.0);
529
530 if(bFixedTransparence)
531 {
532 const double fOpNew(1.0 - fTransparence);
533
534 for(tools::Long y(0); y < pOld->Height(); y++)
535 {
536 Scanline pScanline = pOld->GetScanline( y );
537 for(tools::Long x(0); x < pOld->Width(); x++)
538 {
539 const double fOpOld(1.0 - (pOld->GetIndexFromData(pScanline, x) * fFactor));
540 const sal_uInt8 aCol(basegfx::fround((1.0 - (fOpOld * fOpNew)) * 255.0));
541
542 pOld->SetPixelOnData(pScanline, x, BitmapColor(aCol));
543 }
544 }
545 }
546 else
547 {
548 AlphaMask::ScopedReadAccess pNew(rNewMask);
549
550 assert(pNew && "Got no access to new alpha mask (!)");
551
552 assert(pOld->Width() == pNew->Width() && pOld->Height() == pNew->Height() &&
553 "Alpha masks have different sizes (!)");
554
555 for(tools::Long y(0); y < pOld->Height(); y++)
556 {
557 Scanline pScanline = pOld->GetScanline( y );
558 for(tools::Long x(0); x < pOld->Width(); x++)
559 {
560 const double fOpOld(1.0 - (pOld->GetIndexFromData(pScanline, x) * fFactor));
561 const double fOpNew(1.0 - (pNew->GetIndexFromData(pScanline, x) * fFactor));
562 const sal_uInt8 aCol(basegfx::fround((1.0 - (fOpOld * fOpNew)) * 255.0));
563
564 pOld->SetPixelOnData(pScanline, x, BitmapColor(aCol));
565 }
566 }
567 }
568
569 }
570
571 // apply combined bitmap as mask
572 rBitmapEx = BitmapEx(rBitmapEx.GetBitmap(), aOldMask);
573}
574
575
576void DrawAndClipBitmap(const Point& rPos, const Size& rSize, const BitmapEx& rBitmap, BitmapEx & aBmpEx, basegfx::B2DPolyPolygon const & rClipPath)
577{
579 MapMode aMapMode( MapUnit::Map100thMM );
580 aMapMode.SetOrigin( Point( -rPos.X(), -rPos.Y() ) );
581 const Size aOutputSizePixel( pVDev->LogicToPixel( rSize, aMapMode ) );
582 const Size aSizePixel( rBitmap.GetSizePixel() );
583 if ( aOutputSizePixel.Width() && aOutputSizePixel.Height() )
584 {
585 aMapMode.SetScaleX( Fraction( aSizePixel.Width(), aOutputSizePixel.Width() ) );
586 aMapMode.SetScaleY( Fraction( aSizePixel.Height(), aOutputSizePixel.Height() ) );
587 }
588 pVDev->SetMapMode( aMapMode );
589 pVDev->SetOutputSizePixel( aSizePixel );
590 pVDev->SetFillColor( COL_BLACK );
591 const tools::PolyPolygon aClip( rClipPath );
592 pVDev->DrawPolyPolygon( aClip );
593
594 // #i50672# Extract whole VDev content (to match size of rBitmap)
595 pVDev->EnableMapMode( false );
596 const Bitmap aVDevMask(pVDev->GetBitmap(Point(), aSizePixel));
597
598 if(aBmpEx.IsAlpha())
599 {
600 // bitmap already uses a Mask or Alpha, we need to blend that with
601 // the new masking in pVDev.
602 // need to blend in AlphaMask quality (8Bit)
603 AlphaMask fromVDev(aVDevMask);
604 AlphaMask fromBmpEx(aBmpEx.GetAlphaMask());
605 AlphaMask::ScopedReadAccess pR(fromVDev);
606 AlphaScopedWriteAccess pW(fromBmpEx);
607
608 if(pR && pW)
609 {
610 const tools::Long nWidth(std::min(pR->Width(), pW->Width()));
611 const tools::Long nHeight(std::min(pR->Height(), pW->Height()));
612
613 for(tools::Long nY(0); nY < nHeight; nY++)
614 {
615 Scanline pScanlineR = pR->GetScanline( nY );
616 Scanline pScanlineW = pW->GetScanline( nY );
617 for(tools::Long nX(0); nX < nWidth; nX++)
618 {
619 const sal_uInt8 nIndR(pR->GetIndexFromData(pScanlineR, nX));
620 const sal_uInt8 nIndW(pW->GetIndexFromData(pScanlineW, nX));
621
622 // these values represent transparency (0 == no, 255 == fully transparent),
623 // so to blend these we have to multiply the inverse (opacity)
624 // and re-invert the result to transparence
625 const sal_uInt8 nCombined(0x00ff - (((0x00ff - nIndR) * (0x00ff - nIndW)) >> 8));
626
627 pW->SetPixelOnData(pScanlineW, nX, BitmapColor(nCombined));
628 }
629 }
630 }
631
632 pR.reset();
633 pW.reset();
634 aBmpEx = BitmapEx(aBmpEx.GetBitmap(), fromBmpEx);
635 }
636 else
637 {
638 // no mask yet, create and add new mask. For better quality, use Alpha,
639 // this allows the drawn mask being processed with AntiAliasing (AAed)
640 aBmpEx = BitmapEx(rBitmap.GetBitmap(), aVDevMask);
641 }
642}
643
644
645css::uno::Sequence< sal_Int8 > GetMaskDIB(BitmapEx const & aBmpEx)
646{
647 if ( aBmpEx.IsAlpha() )
648 {
649 SvMemoryStream aMem;
650 WriteDIB(aBmpEx.GetAlphaMask().GetBitmap(), aMem, false, true);
651 return css::uno::Sequence< sal_Int8 >( static_cast<sal_Int8 const *>(aMem.GetData()), aMem.Tell() );
652 }
653
654 return css::uno::Sequence< sal_Int8 >();
655}
656
657static bool readAlpha( BitmapReadAccess const * pAlphaReadAcc, tools::Long nY, const tools::Long nWidth, unsigned char* data, tools::Long nOff )
658{
659 bool bIsAlpha = false;
660 tools::Long nX;
661 int nAlpha;
662 Scanline pReadScan;
663
664 nOff += 3;
665
666 switch( pAlphaReadAcc->GetScanlineFormat() )
667 {
669 pReadScan = pAlphaReadAcc->GetScanline( nY );
670 for( nX = 0; nX < nWidth; nX++ )
671 {
672 BitmapColor const& rColor(
673 pAlphaReadAcc->GetPaletteColor(*pReadScan));
674 pReadScan++;
675 nAlpha = data[ nOff ] = 255 - rColor.GetIndex();
676 if( nAlpha != 255 )
677 bIsAlpha = true;
678 nOff += 4;
679 }
680 break;
681 default:
682 SAL_INFO( "canvas.cairo", "fallback to GetColor for alpha - slow, format: " << static_cast<int>(pAlphaReadAcc->GetScanlineFormat()) );
683 for( nX = 0; nX < nWidth; nX++ )
684 {
685 nAlpha = data[ nOff ] = 255 - pAlphaReadAcc->GetColor( nY, nX ).GetIndex();
686 if( nAlpha != 255 )
687 bIsAlpha = true;
688 nOff += 4;
689 }
690 }
691
692 return bIsAlpha;
693}
694
695
696
701void CanvasCairoExtractBitmapData( BitmapEx const & aBmpEx, Bitmap & aBitmap, unsigned char*& data, bool& bHasAlpha, tools::Long& rnWidth, tools::Long& rnHeight )
702{
703 AlphaMask aAlpha = aBmpEx.GetAlphaMask();
704
705 ::BitmapReadAccess* pBitmapReadAcc = aBitmap.AcquireReadAccess();
706 ::BitmapReadAccess* pAlphaReadAcc = nullptr;
707 const tools::Long nWidth = rnWidth = pBitmapReadAcc->Width();
708 const tools::Long nHeight = rnHeight = pBitmapReadAcc->Height();
709 tools::Long nX, nY;
710 bool bIsAlpha = false;
711
712 if( aBmpEx.IsAlpha() )
713 pAlphaReadAcc = aAlpha.AcquireReadAccess();
714
715 data = static_cast<unsigned char*>(malloc( nWidth*nHeight*4 ));
716
717 tools::Long nOff = 0;
718 ::Color aColor;
719 unsigned int nAlpha = 255;
720
721#if !ENABLE_WASM_STRIP_PREMULTIPLY
723#endif
724 for( nY = 0; nY < nHeight; nY++ )
725 {
726 ::Scanline pReadScan;
727
728 switch( pBitmapReadAcc->GetScanlineFormat() )
729 {
731 pReadScan = pBitmapReadAcc->GetScanline( nY );
732 if( pAlphaReadAcc )
733 if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
734 bIsAlpha = true;
735
736 for( nX = 0; nX < nWidth; nX++ )
737 {
738#ifdef OSL_BIGENDIAN
739 if( pAlphaReadAcc )
740 nAlpha = data[ nOff++ ];
741 else
742 nAlpha = data[ nOff++ ] = 255;
743#else
744 if( pAlphaReadAcc )
745 nAlpha = data[ nOff + 3 ];
746 else
747 nAlpha = data[ nOff + 3 ] = 255;
748#endif
749 aColor = pBitmapReadAcc->GetPaletteColor(*pReadScan++);
750
751#ifdef OSL_BIGENDIAN
752#if ENABLE_WASM_STRIP_PREMULTIPLY
753 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetRed());
754 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetGreen());
755 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetBlue());
756#else
757 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetRed()];
758 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetGreen()];
759 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetBlue()];
760#endif
761#else
762#if ENABLE_WASM_STRIP_PREMULTIPLY
763 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetBlue());
764 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetGreen());
765 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetRed());
766#else
767 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetBlue()];
768 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetGreen()];
769 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetRed()];
770#endif
771 nOff++;
772#endif
773 }
774 break;
776 pReadScan = pBitmapReadAcc->GetScanline( nY );
777 if( pAlphaReadAcc )
778 if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
779 bIsAlpha = true;
780
781 for( nX = 0; nX < nWidth; nX++ )
782 {
783#ifdef OSL_BIGENDIAN
784 if( pAlphaReadAcc )
785 nAlpha = data[ nOff ];
786 else
787 nAlpha = data[ nOff ] = 255;
788#if ENABLE_WASM_STRIP_PREMULTIPLY
789 data[ nOff + 3 ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
790 data[ nOff + 2 ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
791 data[ nOff + 1 ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
792#else
793 data[ nOff + 3 ] = premultiply_table[nAlpha][*pReadScan++];
794 data[ nOff + 2 ] = premultiply_table[nAlpha][*pReadScan++];
795 data[ nOff + 1 ] = premultiply_table[nAlpha][*pReadScan++];
796#endif
797 nOff += 4;
798#else
799 if( pAlphaReadAcc )
800 nAlpha = data[ nOff + 3 ];
801 else
802 nAlpha = data[ nOff + 3 ] = 255;
803#if ENABLE_WASM_STRIP_PREMULTIPLY
804 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
805 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
806 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
807#else
808 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
809 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
810 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
811#endif
812 nOff++;
813#endif
814 }
815 break;
817 pReadScan = pBitmapReadAcc->GetScanline( nY );
818 if( pAlphaReadAcc )
819 if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
820 bIsAlpha = true;
821
822 for( nX = 0; nX < nWidth; nX++ )
823 {
824#ifdef OSL_BIGENDIAN
825 if( pAlphaReadAcc )
826 nAlpha = data[ nOff++ ];
827 else
828 nAlpha = data[ nOff++ ] = 255;
829#if ENABLE_WASM_STRIP_PREMULTIPLY
830 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
831 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
832 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
833#else
834 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
835 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
836 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
837#endif
838#else
839 if( pAlphaReadAcc )
840 nAlpha = data[ nOff + 3 ];
841 else
842 nAlpha = data[ nOff + 3 ] = 255;
843#if ENABLE_WASM_STRIP_PREMULTIPLY
844 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 2 ]);
845 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 1 ]);
846 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 0 ]);
847#else
848 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 2 ]];
849 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 1 ]];
850 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 0 ]];
851#endif
852 pReadScan += 3;
853 nOff++;
854#endif
855 }
856 break;
858 pReadScan = pBitmapReadAcc->GetScanline( nY );
859 if( pAlphaReadAcc )
860 if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
861 bIsAlpha = true;
862
863 for( nX = 0; nX < nWidth; nX++ )
864 {
865#ifdef OSL_BIGENDIAN
866 if( pAlphaReadAcc )
867 nAlpha = data[ nOff++ ];
868 else
869 nAlpha = data[ nOff++ ] = 255;
870#if ENABLE_WASM_STRIP_PREMULTIPLY
871 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 2 ]);
872 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 1 ]);
873 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 0 ]);
874#else
875 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 2 ]];
876 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 1 ]];
877 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 0 ]];
878#endif
879 pReadScan += 4;
880#else
881 if( pAlphaReadAcc )
882 nAlpha = data[ nOff + 3 ];
883 else
884 nAlpha = data[ nOff + 3 ] = 255;
885#if ENABLE_WASM_STRIP_PREMULTIPLY
886 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
887 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
888 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
889#else
890 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
891 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
892 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
893#endif
894 pReadScan++;
895 nOff++;
896#endif
897 }
898 break;
900 pReadScan = pBitmapReadAcc->GetScanline( nY );
901 if( pAlphaReadAcc )
902 if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
903 bIsAlpha = true;
904
905 for( nX = 0; nX < nWidth; nX++ )
906 {
907#ifdef OSL_BIGENDIAN
908 if( pAlphaReadAcc )
909 nAlpha = data[ nOff ++ ];
910 else
911 nAlpha = data[ nOff ++ ] = 255;
912#if ENABLE_WASM_STRIP_PREMULTIPLY
913 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
914 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
915 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, *pReadScan++);
916#else
917 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
918 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
919 data[ nOff++ ] = premultiply_table[nAlpha][*pReadScan++];
920#endif
921 pReadScan++;
922#else
923 if( pAlphaReadAcc )
924 nAlpha = data[ nOff + 3 ];
925 else
926 nAlpha = data[ nOff + 3 ] = 255;
927#if ENABLE_WASM_STRIP_PREMULTIPLY
928 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 2 ]);
929 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 1 ]);
930 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, pReadScan[ 0 ]);
931#else
932 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 2 ]];
933 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 1 ]];
934 data[ nOff++ ] = premultiply_table[nAlpha][pReadScan[ 0 ]];
935#endif
936 pReadScan += 4;
937 nOff++;
938#endif
939 }
940 break;
941 default:
942 SAL_INFO( "canvas.cairo", "fallback to GetColor - slow, format: " << static_cast<int>(pBitmapReadAcc->GetScanlineFormat()) );
943
944 if( pAlphaReadAcc )
945 if( readAlpha( pAlphaReadAcc, nY, nWidth, data, nOff ) )
946 bIsAlpha = true;
947
948 for( nX = 0; nX < nWidth; nX++ )
949 {
950 aColor = pBitmapReadAcc->GetColor( nY, nX );
951
952 // cairo need premultiplied color values
953 // TODO(rodo) handle endianness
954#ifdef OSL_BIGENDIAN
955 if( pAlphaReadAcc )
956 nAlpha = data[ nOff++ ];
957 else
958 nAlpha = data[ nOff++ ] = 255;
959#if ENABLE_WASM_STRIP_PREMULTIPLY
960 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetRed());
961 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetGreen());
962 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetBlue());
963#else
964 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetRed()];
965 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetGreen()];
966 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetBlue()];
967#endif
968#else
969 if( pAlphaReadAcc )
970 nAlpha = data[ nOff + 3 ];
971 else
972 nAlpha = data[ nOff + 3 ] = 255;
973#if ENABLE_WASM_STRIP_PREMULTIPLY
974 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetBlue());
975 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetGreen());
976 data[ nOff++ ] = vcl::bitmap::premultiply(nAlpha, aColor.GetRed());
977#else
978 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetBlue()];
979 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetGreen()];
980 data[ nOff++ ] = premultiply_table[nAlpha][aColor.GetRed()];
981#endif
982 nOff ++;
983#endif
984 }
985 }
986 }
987
988 ::Bitmap::ReleaseAccess( pBitmapReadAcc );
989 if( pAlphaReadAcc )
990 aAlpha.ReleaseAccess( pAlphaReadAcc );
991
992 bHasAlpha = bIsAlpha;
993
994}
995
996 uno::Sequence< sal_Int8 > CanvasExtractBitmapData(BitmapEx const & rBitmapEx, const geometry::IntegerRectangle2D& rect)
997 {
998 Bitmap aBitmap( rBitmapEx.GetBitmap() );
999 Bitmap aAlpha( rBitmapEx.GetAlphaMask().GetBitmap() );
1000
1001 Bitmap::ScopedReadAccess pReadAccess( aBitmap );
1002 Bitmap::ScopedReadAccess pAlphaReadAccess( aAlpha.IsEmpty() ?
1003 nullptr : aAlpha.AcquireReadAccess(),
1004 aAlpha );
1005
1006 assert( pReadAccess );
1007
1008 // TODO(F1): Support more formats.
1009 const Size aBmpSize( aBitmap.GetSizePixel() );
1010
1011 // for the time being, always return as BGRA
1012 uno::Sequence< sal_Int8 > aRes( 4*aBmpSize.Width()*aBmpSize.Height() );
1013 sal_Int8* pRes = aRes.getArray();
1014
1015 int nCurrPos(0);
1016 for( tools::Long y=rect.Y1;
1017 y<aBmpSize.Height() && y<rect.Y2;
1018 ++y )
1019 {
1020 if( pAlphaReadAccess.get() != nullptr )
1021 {
1022 Scanline pScanlineReadAlpha = pAlphaReadAccess->GetScanline( y );
1023 for( tools::Long x=rect.X1;
1024 x<aBmpSize.Width() && x<rect.X2;
1025 ++x )
1026 {
1027 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetRed();
1028 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetGreen();
1029 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetBlue();
1030 pRes[ nCurrPos++ ] = pAlphaReadAccess->GetIndexFromData( pScanlineReadAlpha, x );
1031 }
1032 }
1033 else
1034 {
1035 for( tools::Long x=rect.X1;
1036 x<aBmpSize.Width() && x<rect.X2;
1037 ++x )
1038 {
1039 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetRed();
1040 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetGreen();
1041 pRes[ nCurrPos++ ] = pReadAccess->GetColor( y, x ).GetBlue();
1042 pRes[ nCurrPos++ ] = sal_uInt8(255);
1043 }
1044 }
1045 }
1046 return aRes;
1047 }
1048
1049 BitmapEx createHistorical8x8FromArray(std::array<sal_uInt8,64> const & pArray, Color aColorPix, Color aColorBack)
1050 {
1051 BitmapPalette aPalette(2);
1052
1053 aPalette[0] = BitmapColor(aColorBack);
1054 aPalette[1] = BitmapColor(aColorPix);
1055
1056 Bitmap aBitmap(Size(8, 8), vcl::PixelFormat::N8_BPP, &aPalette);
1057 BitmapScopedWriteAccess pContent(aBitmap);
1058
1059 for(sal_uInt16 a(0); a < 8; a++)
1060 {
1061 for(sal_uInt16 b(0); b < 8; b++)
1062 {
1063 if(pArray[(a * 8) + b])
1064 {
1065 pContent->SetPixelIndex(a, b, 1);
1066 }
1067 else
1068 {
1069 pContent->SetPixelIndex(a, b, 0);
1070 }
1071 }
1072 }
1073
1074 return BitmapEx(aBitmap);
1075 }
1076
1077 bool isHistorical8x8(const BitmapEx& rBitmapEx, Color& o_rBack, Color& o_rFront)
1078 {
1079 bool bRet(false);
1080
1081 if(!rBitmapEx.IsAlpha())
1082 {
1083 Bitmap aBitmap(rBitmapEx.GetBitmap());
1084
1085 if(8 == aBitmap.GetSizePixel().Width() && 8 == aBitmap.GetSizePixel().Height())
1086 {
1087 // Historical 1bpp images are getting really historical,
1088 // even to the point that e.g. the png loader actually loads
1089 // them as RGB. But the pattern code in svx relies on this
1090 // assumption that any 2-color 1bpp bitmap is a pattern, and so it would
1091 // get confused by RGB. Try to detect if this image is really
1092 // just two colors and say it's a pattern bitmap if so.
1093 Bitmap::ScopedReadAccess access(aBitmap);
1094 o_rBack = access->GetColor(0,0);
1095 bool foundSecondColor = false;;
1096 for(tools::Long y = 0; y < access->Height(); ++y)
1097 for(tools::Long x = 0; x < access->Width(); ++x)
1098 {
1099 if(!foundSecondColor)
1100 {
1101 if( access->GetColor(y,x) != o_rBack )
1102 {
1103 o_rFront = access->GetColor(y,x);
1104 foundSecondColor = true;
1105 // Hard to know which of the two colors is the background,
1106 // select the lighter one.
1107 if( o_rFront.GetLuminance() > o_rBack.GetLuminance())
1108 std::swap( o_rFront, o_rBack );
1109 }
1110 }
1111 else
1112 {
1113 if( access->GetColor(y,x) != o_rBack && access->GetColor(y,x) != o_rFront)
1114 return false;
1115 }
1116 }
1117 return true;
1118 }
1119 }
1120
1121 return bRet;
1122 }
1123
1124#if ENABLE_WASM_STRIP_PREMULTIPLY
1126 {
1127 return (a == 0) ? 0 : (c * 255 + a / 2) / a;
1128 }
1129
1131 {
1132 return (c * a + 127) / 255;
1133 }
1134#else
1136 {
1137 return get_unpremultiply_table()[a][c];
1138 }
1139
1141 {
1142 return (a == 0) ? 0 : (c * 255 + a / 2) / a;
1143 }
1144
1146 {
1147 return get_premultiply_table()[a][c];
1148 }
1149
1151 {
1152 return (c * a + 127) / 255;
1153 }
1154
1155 template<int... Is> static constexpr std::array<sal_uInt8, 256> make_unpremultiply_table_row_(
1156 int a, std::integer_sequence<int, Is...>)
1157 {
1158 return {unpremultiplyImpl(Is, a)...};
1159 }
1160
1161 template<int... Is> static constexpr lookup_table make_unpremultiply_table_(
1162 std::integer_sequence<int, Is...>)
1163 {
1164 return {make_unpremultiply_table_row_(Is, std::make_integer_sequence<int, 256>{})...};
1165 }
1166
1168 {
1169 static constexpr auto unpremultiply_table = make_unpremultiply_table_(
1170 std::make_integer_sequence<int, 256>{});
1171 return unpremultiply_table;
1172 }
1173
1174 template<int... Is> static constexpr std::array<sal_uInt8, 256> make_premultiply_table_row_(
1175 int a, std::integer_sequence<int, Is...>)
1176 {
1177 return {premultiplyImpl(Is, a)...};
1178 }
1179
1180 template<int... Is> static constexpr lookup_table make_premultiply_table_(
1181 std::integer_sequence<int, Is...>)
1182 {
1183 return {make_premultiply_table_row_(Is, std::make_integer_sequence<int, 256>{})...};
1184 }
1185
1187 {
1188 static constexpr auto premultiply_table = make_premultiply_table_(
1189 std::make_integer_sequence<int, 256>{});
1190 return premultiply_table;
1191 }
1192#endif
1193
1194bool convertBitmap32To24Plus8(BitmapEx const & rInput, BitmapEx & rResult)
1195{
1196 Bitmap aBitmap(rInput.GetBitmap());
1198 return false;
1199
1200 Size aSize = aBitmap.GetSizePixel();
1201 Bitmap aResultBitmap(aSize, vcl::PixelFormat::N24_BPP);
1202 AlphaMask aResultAlpha(aSize);
1203 {
1204 BitmapScopedWriteAccess pResultBitmapAccess(aResultBitmap);
1205 AlphaScopedWriteAccess pResultAlphaAccess(aResultAlpha);
1206
1207 Bitmap::ScopedReadAccess pReadAccess(aBitmap);
1208
1209 for (tools::Long nY = 0; nY < aSize.Height(); ++nY)
1210 {
1211 Scanline aResultScan = pResultBitmapAccess->GetScanline(nY);
1212 Scanline aResultScanAlpha = pResultAlphaAccess->GetScanline(nY);
1213
1214 Scanline aReadScan = pReadAccess->GetScanline(nY);
1215
1216 for (tools::Long nX = 0; nX < aSize.Width(); ++nX)
1217 {
1218 const BitmapColor aColor = pReadAccess->GetPixelFromData(aReadScan, nX);
1219 BitmapColor aResultColor(aColor.GetRed(), aColor.GetGreen(), aColor.GetBlue());
1220 BitmapColor aResultColorAlpha(255 - aColor.GetAlpha(), 255 - aColor.GetAlpha(), 255 - aColor.GetAlpha());
1221
1222 pResultBitmapAccess->SetPixelOnData(aResultScan, nX, aResultColor);
1223 pResultAlphaAccess->SetPixelOnData(aResultScanAlpha, nX, aResultColorAlpha);
1224 }
1225 }
1226 }
1227 if (rInput.IsAlpha())
1228 rResult = BitmapEx(aResultBitmap, rInput.GetAlphaMask());
1229 else
1230 rResult = BitmapEx(aResultBitmap, aResultAlpha);
1231 return true;
1232}
1233
1234Bitmap GetDownsampledBitmap(Size const& rDstSizeTwip, Point const& rSrcPt, Size const& rSrcSz,
1235 Bitmap const& rBmp, tools::Long nMaxBmpDPIX, tools::Long nMaxBmpDPIY)
1236{
1237 Bitmap aBmp(rBmp);
1238
1239 if (!aBmp.IsEmpty())
1240 {
1241 const tools::Rectangle aBmpRect( Point(), aBmp.GetSizePixel() );
1242 tools::Rectangle aSrcRect( rSrcPt, rSrcSz );
1243
1244 // do cropping if necessary
1245 if( aSrcRect.Intersection( aBmpRect ) != aBmpRect )
1246 {
1247 if( !aSrcRect.IsEmpty() )
1248 aBmp.Crop( aSrcRect );
1249 else
1250 aBmp.SetEmpty();
1251 }
1252
1253 if( !aBmp.IsEmpty() )
1254 {
1255 // do downsampling if necessary
1256 // #103209# Normalize size (mirroring has to happen outside of this method)
1257 Size aDstSizeTwip(std::abs(rDstSizeTwip.Width()), std::abs(rDstSizeTwip.Height()));
1258
1259 const Size aBmpSize( aBmp.GetSizePixel() );
1260 const double fBmpPixelX = aBmpSize.Width();
1261 const double fBmpPixelY = aBmpSize.Height();
1262 const double fMaxPixelX
1263 = o3tl::convert<double>(aDstSizeTwip.Width(), o3tl::Length::twip, o3tl::Length::in)
1264 * nMaxBmpDPIX;
1265 const double fMaxPixelY
1266 = o3tl::convert<double>(aDstSizeTwip.Height(), o3tl::Length::twip, o3tl::Length::in)
1267 * nMaxBmpDPIY;
1268
1269 // check, if the bitmap DPI exceeds the maximum DPI (allow 4 pixel rounding tolerance)
1270 if (((fBmpPixelX > (fMaxPixelX + 4)) ||
1271 (fBmpPixelY > (fMaxPixelY + 4))) &&
1272 (fBmpPixelY > 0.0) && (fMaxPixelY > 0.0))
1273 {
1274 // do scaling
1275 Size aNewBmpSize;
1276 const double fBmpWH = fBmpPixelX / fBmpPixelY;
1277 const double fMaxWH = fMaxPixelX / fMaxPixelY;
1278
1279 if (fBmpWH < fMaxWH)
1280 {
1281 aNewBmpSize.setWidth(FRound(fMaxPixelY * fBmpWH));
1282 aNewBmpSize.setHeight(FRound(fMaxPixelY));
1283 }
1284 else if (fBmpWH > 0.0)
1285 {
1286 aNewBmpSize.setWidth(FRound(fMaxPixelX));
1287 aNewBmpSize.setHeight(FRound(fMaxPixelX / fBmpWH));
1288 }
1289
1290 if( aNewBmpSize.Width() && aNewBmpSize.Height() )
1291 aBmp.Scale(aNewBmpSize);
1292 else
1293 aBmp.SetEmpty();
1294 }
1295 }
1296 }
1297
1298 return aBmp;
1299}
1300
1301} // end vcl::bitmap
1302
1303/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
vcl::ScopedBitmapAccess< BitmapWriteAccess, AlphaMask, &AlphaMask::AcquireAlphaWriteAccess > AlphaScopedWriteAccess
struct _cairo_surface cairo_surface_t
Definition: CairoCommon.hxx:46
struct _cairo cairo_t
Definition: CairoCommon.hxx:45
ImageLoadFlags
Definition: ImageTree.hxx:32
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
const StyleSettings & GetStyleSettings() const
Bitmap const & GetBitmap() const
Definition: alpha.cxx:66
void ReleaseAccess(BitmapReadAccess *pAccess)
Definition: alpha.cxx:139
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:655
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:70
const AlphaMask & GetAlphaMask() const
Definition: bitmapex.hxx:70
bool IsAlpha() const
Definition: BitmapEx.cxx:193
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:203
const Size & GetSizePixel() const
Definition: bitmapex.hxx:72
tools::Long Height() const
tools::Long Width() const
const BitmapPalette & GetPalette() const
ScanlineFormat GetScanlineFormat() const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
sal_uInt8 GetPixelIndex(tools::Long nY, tools::Long nX) const
BitmapColor GetPixel(tools::Long nY, tools::Long nX) const
BitmapColor GetColor(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
bool Crop(const tools::Rectangle &rRectPixel)
Crop the bitmap.
Size GetSizePixel() const
static void ReleaseAccess(BitmapInfoAccess *pAccess)
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
bool IsEmpty() const
BitmapReadAccess * AcquireReadAccess()
vcl::PixelFormat getPixelFormat() const
void SetEmpty()
sal_uInt8 GetLuminance() const
sal_uInt8 GetBlue() const
sal_uInt8 GetAlpha() const
sal_uInt8 GetRed() const
sal_uInt8 GetGreen() const
static VCL_DLLPUBLIC ImageTree & get()
Definition: ImageTree.cxx:16
VCL_DLLPUBLIC bool loadImage(OUString const &name, OUString const &style, BitmapEx &bitmap, bool localized, const ImageLoadFlags eFlags=ImageLoadFlags::NONE)
Definition: ImageTree.cxx:50
void SetOrigin(const Point &rOrigin)
Definition: mapmod.cxx:103
void SetScaleY(const Fraction &rScaleY)
Definition: mapmod.cxx:115
void SetScaleX(const Fraction &rScaleX)
Definition: mapmod.cxx:109
bool IsEmpty() const
constexpr tools::Long Height() const
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
constexpr tools::Long Width() const
OUString DetermineIconTheme() const
Determine which icon theme should be used.
const void * GetData()
sal_uInt64 Tell() const
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt64 remainingSize()
B2DVector getRange() const
TYPE getMaxX() const
TYPE getWidth() const
TYPE getMinX() const
TYPE getMinY() const
void expand(const Tuple2D< TYPE > &rTuple)
TYPE getMaxY() const
TYPE getHeight() const
TYPE getX() const
TYPE getY() const
tools::Rectangle & Intersection(const tools::Rectangle &rRect)
constexpr bool IsEmpty() const
This template handles BitmapAccess the RAII way.
Intended to be used to feed into CreateFromData to create a BitmapEx.
Definition: RawBitmap.hxx:22
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
#define ENSURE_OR_THROW(c, m)
bool WriteDIB(const Bitmap &rSource, SvStream &rOStm, bool bCompressed, bool bFileHeader)
Definition: dibtools.cxx:1734
float y
float x
sal_Int16 nValue
tools::Long FRound(double fVal)
short nBitCount
Definition: ipict.cxx:80
void * p
uno_Any a
#define SAL_WARN_IF(condition, area, stream)
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
RttiCompleteObjectLocator col
constexpr OUStringLiteral aData
B2IRange fround(const B2DRange &rRange)
Reference< XComponentContext > getProcessComponentContext()
rtl::Reference< BasePrimitive2D > Primitive2DReference
css::uno::Sequence< css::uno::Reference< css::graphic::XPrimitive2D > > Primitive2DSequence
long Long
void CanvasCairoExtractBitmapData(BitmapEx const &aBmpEx, Bitmap &aBitmap, unsigned char *&data, bool &bHasAlpha, tools::Long &rnWidth, tools::Long &rnHeight)
bool isHistorical8x8(const BitmapEx &rBitmapEx, Color &o_rBack, Color &o_rFront)
BitmapEx loadFromName(const OUString &rFileName, const ImageLoadFlags eFlags)
Definition: BitmapTools.cxx:52
lookup_table const & get_unpremultiply_table()
static constexpr lookup_table make_unpremultiply_table_(std::integer_sequence< int, Is... >)
Bitmap GetDownsampledBitmap(Size const &rDstSizeTwip, Point const &rSrcPt, Size const &rSrcSz, Bitmap const &rBmp, tools::Long nMaxBmpDPIX, tools::Long nMaxBmpDPIY)
Retrieve downsampled and cropped bitmap.
static constexpr std::array< sal_uInt8, 256 > make_premultiply_table_row_(int a, std::integer_sequence< int, Is... >)
BitmapEx CanvasTransformBitmap(const BitmapEx &rBitmap, const ::basegfx::B2DHomMatrix &rTransform, ::basegfx::B2DRectangle const &rDestRect, ::basegfx::B2DHomMatrix const &rLocalTransform)
void DrawAlphaBitmapAndAlphaGradient(BitmapEx &rBitmapEx, bool bFixedTransparence, float fTransparence, AlphaMask &rNewMask)
void loadFromSvg(SvStream &rStream, const OUString &sPath, BitmapEx &rBitmapEx, double fScalingFactor)
Definition: BitmapTools.cxx:72
static constexpr std::array< sal_uInt8, 256 > make_unpremultiply_table_row_(int a, std::integer_sequence< int, Is... >)
void DrawAndClipBitmap(const Point &rPos, const Size &rSize, const BitmapEx &rBitmap, BitmapEx &aBmpEx, basegfx::B2DPolyPolygon const &rClipPath)
static constexpr sal_uInt8 premultiplyImpl(sal_uInt8 c, sal_uInt8 a)
sal_uInt8 unpremultiply(sal_uInt8 c, sal_uInt8 a)
BitmapEx createHistorical8x8FromArray(std::array< sal_uInt8, 64 > const &pArray, Color aColorPix, Color aColorBack)
static bool readAlpha(BitmapReadAccess const *pAlphaReadAcc, tools::Long nY, const tools::Long nWidth, unsigned char *data, tools::Long nOff)
sal_uInt8 premultiply(sal_uInt8 c, sal_uInt8 a)
css::uno::Sequence< sal_Int8 > GetMaskDIB(BitmapEx const &aBmpEx)
uno::Sequence< sal_Int8 > CanvasExtractBitmapData(BitmapEx const &rBitmapEx, const geometry::IntegerRectangle2D &rect)
lookup_table const & get_premultiply_table()
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_Int8 nBitCount, bool bReversColors, bool bReverseAlpha)
Copy block of image data into the bitmap.
std::array< std::array< sal_uInt8, 256 >, 256 > lookup_table
Definition: BitmapTools.hxx:32
bool convertBitmap32To24Plus8(BitmapEx const &rInput, BitmapEx &rResult)
static constexpr lookup_table make_premultiply_table_(std::integer_sequence< int, Is... >)
static constexpr sal_uInt8 unpremultiplyImpl(sal_uInt8 c, sal_uInt8 a)
::BitmapEx bitmapExFromXBitmap(const uno::Reference< rendering::XIntegerReadOnlyBitmap > &xInputBitmap)
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:20
unsigned char sal_uInt8
signed char sal_Int8
std::unique_ptr< char[]> aBuffer