LibreOffice Module vcl (master) 1
bitmappaint.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 <tools/poly.hxx>
21#include <tools/helpers.hxx>
22
23#include <vcl/bitmap.hxx>
24#include <vcl/alpha.hxx>
25
27#include <salbmp.hxx>
28#include <svdata.hxx>
29#include <salinst.hxx>
30
31#include <algorithm>
32#include <memory>
33
34bool Bitmap::Erase(const Color& rFillColor)
35{
36 if (IsEmpty())
37 return true;
38
39 // implementation specific replace
40 std::shared_ptr<SalBitmap> xImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
41 if (xImpBmp->Create(*mxSalBmp) && xImpBmp->Erase(rFillColor))
42 {
43 ImplSetSalBitmap(xImpBmp);
44 maPrefMapMode = MapMode(MapUnit::MapPixel);
45 maPrefSize = xImpBmp->GetSize();
46 return true;
47 }
48
49 BitmapScopedWriteAccess pWriteAcc(*this);
50 bool bRet = false;
51
52 if (pWriteAcc)
53 {
54 pWriteAcc->Erase(rFillColor);
55 bRet = true;
56 }
57
58 return bRet;
59}
60
62{
63 BitmapScopedWriteAccess pAcc(*this);
64 bool bRet = false;
65
66 if (pAcc)
67 {
68 if (pAcc->HasPalette())
69 {
70 BitmapPalette aBmpPal(pAcc->GetPalette());
71 const sal_uInt16 nCount = aBmpPal.GetEntryCount();
72
73 for (sal_uInt16 i = 0; i < nCount; i++)
74 {
75 aBmpPal[i].Invert();
76 }
77
78 pAcc->SetPalette(aBmpPal);
79 }
80 else
81 {
82 const tools::Long nWidth = pAcc->Width();
83 const tools::Long nHeight = pAcc->Height();
84
85 for (tools::Long nY = 0; nY < nHeight; nY++)
86 {
87 Scanline pScanline = pAcc->GetScanline(nY);
88 for (tools::Long nX = 0; nX < nWidth; nX++)
89 {
90 BitmapColor aBmpColor = pAcc->GetPixelFromData(pScanline, nX);
91 aBmpColor.Invert();
92 pAcc->SetPixelOnData(pScanline, nX, aBmpColor);
93 }
94 }
95 }
96
97 mxSalBmp->InvalidateChecksum();
98 pAcc.reset();
99 bRet = true;
100 }
101
102 return bRet;
103}
104
105namespace
106{
107// Put each scanline's content horizontally mirrored into the other one.
108// (optimized version accessing pixel values directly).
109template <int bitCount>
110void mirrorScanlines(Scanline scanline1, Scanline scanline2, tools::Long nWidth)
111{
112 constexpr int byteCount = bitCount / 8;
113 Scanline pos1 = scanline1;
114 Scanline pos2 = scanline2 + (nWidth - 1) * byteCount; // last in second scanline
115 sal_uInt8 tmp[byteCount];
116 for (tools::Long i = 0; i < nWidth; ++i)
117 {
118 memcpy(tmp, pos1, byteCount);
119 memcpy(pos1, pos2, byteCount);
120 memcpy(pos2, tmp, byteCount);
121 pos1 += byteCount;
122 pos2 -= byteCount;
123 }
124}
125}
126
128{
129 bool bHorz(nMirrorFlags & BmpMirrorFlags::Horizontal);
130 bool bVert(nMirrorFlags & BmpMirrorFlags::Vertical);
131 bool bRet = false;
132
133 if (bHorz && !bVert)
134 {
135 BitmapScopedWriteAccess pAcc(*this);
136
137 if (pAcc)
138 {
139 const tools::Long nWidth = pAcc->Width();
140 const tools::Long nHeight = pAcc->Height();
141 const tools::Long nWidth1 = nWidth - 1;
142 const tools::Long nWidth_2 = nWidth / 2;
143 const tools::Long nSecondHalf = nWidth - nWidth_2;
144
145 switch (pAcc->GetBitCount())
146 {
147 // Special-case these, swap the halves of scanlines while mirroring them.
148 case 32:
149 for (tools::Long nY = 0; nY < nHeight; nY++)
150 mirrorScanlines<32>(pAcc->GetScanline(nY),
151 pAcc->GetScanline(nY) + 4 * nSecondHalf, nWidth_2);
152 break;
153 case 24:
154 for (tools::Long nY = 0; nY < nHeight; nY++)
155 mirrorScanlines<24>(pAcc->GetScanline(nY),
156 pAcc->GetScanline(nY) + 3 * nSecondHalf, nWidth_2);
157 break;
158 case 8:
159 for (tools::Long nY = 0; nY < nHeight; nY++)
160 mirrorScanlines<8>(pAcc->GetScanline(nY),
161 pAcc->GetScanline(nY) + nSecondHalf, nWidth_2);
162 break;
163 default:
164 for (tools::Long nY = 0; nY < nHeight; nY++)
165 {
166 Scanline pScanline = pAcc->GetScanline(nY);
167 for (tools::Long nX = 0, nOther = nWidth1; nX < nWidth_2; nX++, nOther--)
168 {
169 const BitmapColor aTemp(pAcc->GetPixelFromData(pScanline, nX));
170
171 pAcc->SetPixelOnData(pScanline, nX,
172 pAcc->GetPixelFromData(pScanline, nOther));
173 pAcc->SetPixelOnData(pScanline, nOther, aTemp);
174 }
175 }
176 }
177
178 pAcc.reset();
179 bRet = true;
180 }
181 }
182 else if (bVert && !bHorz)
183 {
184 BitmapScopedWriteAccess pAcc(*this);
185
186 if (pAcc)
187 {
188 const tools::Long nScanSize = pAcc->GetScanlineSize();
189 std::unique_ptr<sal_uInt8[]> pBuffer(new sal_uInt8[nScanSize]);
190 const tools::Long nHeight = pAcc->Height();
191 const tools::Long nHeight1 = nHeight - 1;
192 const tools::Long nHeight_2 = nHeight >> 1;
193
194 for (tools::Long nY = 0, nOther = nHeight1; nY < nHeight_2; nY++, nOther--)
195 {
196 memcpy(pBuffer.get(), pAcc->GetScanline(nY), nScanSize);
197 memcpy(pAcc->GetScanline(nY), pAcc->GetScanline(nOther), nScanSize);
198 memcpy(pAcc->GetScanline(nOther), pBuffer.get(), nScanSize);
199 }
200
201 pAcc.reset();
202 bRet = true;
203 }
204 }
205 else if (bHorz && bVert)
206 {
207 BitmapScopedWriteAccess pAcc(*this);
208
209 if (pAcc)
210 {
211 const tools::Long nWidth = pAcc->Width();
212 const tools::Long nWidth1 = nWidth - 1;
213 const tools::Long nHeight = pAcc->Height();
214 tools::Long nHeight_2 = nHeight / 2;
215 const tools::Long nWidth_2 = nWidth / 2;
216 const tools::Long nSecondHalf = nWidth - nWidth_2;
217
218 switch (pAcc->GetBitCount())
219 {
220 case 32:
221 for (tools::Long nY = 0, nOtherY = nHeight - 1; nY < nHeight_2; nY++, nOtherY--)
222 mirrorScanlines<32>(pAcc->GetScanline(nY), pAcc->GetScanline(nOtherY),
223 nWidth);
224 if (nHeight & 1)
225 mirrorScanlines<32>(pAcc->GetScanline(nHeight_2),
226 pAcc->GetScanline(nHeight_2) + 4 * nSecondHalf,
227 nWidth_2);
228 break;
229 case 24:
230 for (tools::Long nY = 0, nOtherY = nHeight - 1; nY < nHeight_2; nY++, nOtherY--)
231 mirrorScanlines<24>(pAcc->GetScanline(nY), pAcc->GetScanline(nOtherY),
232 nWidth);
233 if (nHeight & 1)
234 mirrorScanlines<24>(pAcc->GetScanline(nHeight_2),
235 pAcc->GetScanline(nHeight_2) + 3 * nSecondHalf,
236 nWidth_2);
237 break;
238 case 8:
239 for (tools::Long nY = 0, nOtherY = nHeight - 1; nY < nHeight_2; nY++, nOtherY--)
240 mirrorScanlines<8>(pAcc->GetScanline(nY), pAcc->GetScanline(nOtherY),
241 nWidth);
242 if (nHeight & 1)
243 mirrorScanlines<8>(pAcc->GetScanline(nHeight_2),
244 pAcc->GetScanline(nHeight_2) + nSecondHalf, nWidth_2);
245 break;
246 default:
247 for (tools::Long nY = 0, nOtherY = nHeight - 1; nY < nHeight_2; nY++, nOtherY--)
248 {
249 Scanline pScanline = pAcc->GetScanline(nY);
250 Scanline pScanlineOther = pAcc->GetScanline(nOtherY);
251 for (tools::Long nX = 0, nOtherX = nWidth1; nX < nWidth; nX++, nOtherX--)
252 {
253 const BitmapColor aTemp(pAcc->GetPixelFromData(pScanline, nX));
254
255 pAcc->SetPixelOnData(pScanline, nX,
256 pAcc->GetPixelFromData(pScanlineOther, nOtherX));
257 pAcc->SetPixelOnData(pScanlineOther, nOtherX, aTemp);
258 }
259 }
260
261 // if necessary, also mirror the middle line horizontally
262 if (nHeight & 1)
263 {
264 Scanline pScanline = pAcc->GetScanline(nHeight_2);
265 for (tools::Long nX = 0, nOtherX = nWidth1; nX < nWidth_2; nX++, nOtherX--)
266 {
267 const BitmapColor aTemp(pAcc->GetPixelFromData(pScanline, nX));
268 pAcc->SetPixelOnData(pScanline, nX,
269 pAcc->GetPixelFromData(pScanline, nOtherX));
270 pAcc->SetPixelOnData(pScanline, nOtherX, aTemp);
271 }
272 }
273 }
274
275 pAcc.reset();
276 bRet = true;
277 }
278 }
279 else
280 bRet = true;
281
282 return bRet;
283}
284
285bool Bitmap::Rotate(Degree10 nAngle10, const Color& rFillColor)
286{
287 nAngle10 %= 3600_deg10;
288 nAngle10 = (nAngle10 < 0_deg10) ? (Degree10(3599) + nAngle10) : nAngle10;
289
290 if (!nAngle10)
291 return true;
292 if (nAngle10 == 1800_deg10)
294
295 ScopedReadAccess pReadAcc(*this);
296 Bitmap aRotatedBmp;
297 bool bRet = false;
298
299 if (pReadAcc)
300 {
301 const Size aSizePix(GetSizePixel());
302
303 if (nAngle10 == 900_deg10 || nAngle10 == 2700_deg10)
304 {
305 const Size aNewSizePix(aSizePix.Height(), aSizePix.Width());
306 Bitmap aNewBmp(aNewSizePix, getPixelFormat(), &pReadAcc->GetPalette());
307 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
308
309 if (pWriteAcc)
310 {
311 const tools::Long nWidth = aSizePix.Width();
312 const tools::Long nWidth1 = nWidth - 1;
313 const tools::Long nHeight = aSizePix.Height();
314 const tools::Long nHeight1 = nHeight - 1;
315 const tools::Long nNewWidth = aNewSizePix.Width();
316 const tools::Long nNewHeight = aNewSizePix.Height();
317
318 if (nAngle10 == 900_deg10)
319 {
320 for (tools::Long nY = 0, nOtherX = nWidth1; nY < nNewHeight; nY++, nOtherX--)
321 {
322 Scanline pScanline = pWriteAcc->GetScanline(nY);
323 for (tools::Long nX = 0, nOtherY = 0; nX < nNewWidth; nX++)
324 {
325 pWriteAcc->SetPixelOnData(pScanline, nX,
326 pReadAcc->GetPixel(nOtherY++, nOtherX));
327 }
328 }
329 }
330 else if (nAngle10 == 2700_deg10)
331 {
332 for (tools::Long nY = 0, nOtherX = 0; nY < nNewHeight; nY++, nOtherX++)
333 {
334 Scanline pScanline = pWriteAcc->GetScanline(nY);
335 for (tools::Long nX = 0, nOtherY = nHeight1; nX < nNewWidth; nX++)
336 {
337 pWriteAcc->SetPixelOnData(pScanline, nX,
338 pReadAcc->GetPixel(nOtherY--, nOtherX));
339 }
340 }
341 }
342
343 pWriteAcc.reset();
344 }
345
346 aRotatedBmp = aNewBmp;
347 }
348 else
349 {
350 Point aTmpPoint;
351 tools::Rectangle aTmpRectangle(aTmpPoint, aSizePix);
352 tools::Polygon aPoly(aTmpRectangle);
353 aPoly.Rotate(aTmpPoint, nAngle10);
354
355 tools::Rectangle aNewBound(aPoly.GetBoundRect());
356 const Size aNewSizePix(aNewBound.GetSize());
357 Bitmap aNewBmp(aNewSizePix, getPixelFormat(), &pReadAcc->GetPalette());
358 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
359
360 if (pWriteAcc)
361 {
362 const BitmapColor aFillColor(pWriteAcc->GetBestMatchingColor(rFillColor));
363 const double fCosAngle = cos(toRadians(nAngle10));
364 const double fSinAngle = sin(toRadians(nAngle10));
365 const double fXMin = aNewBound.Left();
366 const double fYMin = aNewBound.Top();
367 const sal_Int32 nWidth = aSizePix.Width();
368 const sal_Int32 nHeight = aSizePix.Height();
369 const sal_Int32 nNewWidth = aNewSizePix.Width();
370 const sal_Int32 nNewHeight = aNewSizePix.Height();
371 // we store alternating values of cos/sin. We do this instead of
372 // separate arrays to improve cache hit.
373 std::unique_ptr<sal_Int32[]> pCosSinX(new sal_Int32[nNewWidth * 2]);
374 std::unique_ptr<sal_Int32[]> pCosSinY(new sal_Int32[nNewHeight * 2]);
375
376 for (sal_Int32 nIdx = 0, nX = 0; nX < nNewWidth; nX++)
377 {
378 const double fTmp = (fXMin + nX) * 64;
379
380 pCosSinX[nIdx++] = std::round(fCosAngle * fTmp);
381 pCosSinX[nIdx++] = std::round(fSinAngle * fTmp);
382 }
383
384 for (sal_Int32 nIdx = 0, nY = 0; nY < nNewHeight; nY++)
385 {
386 const double fTmp = (fYMin + nY) * 64;
387
388 pCosSinY[nIdx++] = std::round(fCosAngle * fTmp);
389 pCosSinY[nIdx++] = std::round(fSinAngle * fTmp);
390 }
391
392 for (sal_Int32 nCosSinYIdx = 0, nY = 0; nY < nNewHeight; nY++)
393 {
394 sal_Int32 nCosY = pCosSinY[nCosSinYIdx++];
395 sal_Int32 nSinY = pCosSinY[nCosSinYIdx++];
396 Scanline pScanline = pWriteAcc->GetScanline(nY);
397
398 for (sal_Int32 nCosSinXIdx = 0, nX = 0; nX < nNewWidth; nX++)
399 {
400 sal_Int32 nRotX = (pCosSinX[nCosSinXIdx++] - nSinY) >> 6;
401 sal_Int32 nRotY = (pCosSinX[nCosSinXIdx++] + nCosY) >> 6;
402
403 if ((nRotX > -1) && (nRotX < nWidth) && (nRotY > -1) && (nRotY < nHeight))
404 {
405 pWriteAcc->SetPixelOnData(pScanline, nX,
406 pReadAcc->GetPixel(nRotY, nRotX));
407 }
408 else
409 {
410 pWriteAcc->SetPixelOnData(pScanline, nX, aFillColor);
411 }
412 }
413 }
414
415 pWriteAcc.reset();
416 }
417
418 aRotatedBmp = aNewBmp;
419 }
420
421 pReadAcc.reset();
422 }
423
424 bRet = !aRotatedBmp.IsEmpty();
425 if (bRet)
426 ReassignWithSize(aRotatedBmp);
427
428 return bRet;
429};
430
431Bitmap Bitmap::CreateMask(const Color& rTransColor) const
432{
433 ScopedReadAccess pReadAcc(const_cast<Bitmap&>(*this));
434 if (!pReadAcc)
435 return Bitmap();
436
437 // Historically LO used 1bpp masks, but 8bpp masks are much faster,
438 // better supported by hardware, and the memory savings are not worth
439 // it anymore.
440 // TODO: Possibly remove the 1bpp code later.
441
444 && pReadAcc->GetBestMatchingColor(COL_WHITE) == pReadAcc->GetBestMatchingColor(rTransColor))
445 {
446 // if we're a 1 bit pixel already, and the transcolor matches the color that would replace it
447 // already, then just return a copy
448 return *this;
449 }
450
451 auto ePixelFormat = vcl::PixelFormat::N8_BPP;
452 Bitmap aNewBmp(GetSizePixel(), ePixelFormat, &Bitmap::GetGreyPalette(256));
453 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
454 if (!pWriteAcc)
455 return Bitmap();
456
457 const tools::Long nWidth = pReadAcc->Width();
458 const tools::Long nHeight = pReadAcc->Height();
459 const BitmapColor aBlack(pWriteAcc->GetBestMatchingColor(COL_BLACK));
460 const BitmapColor aWhite(pWriteAcc->GetBestMatchingColor(COL_WHITE));
461
462 const BitmapColor aTest(pReadAcc->GetBestMatchingColor(rTransColor));
463
464 if (pWriteAcc->GetScanlineFormat() == pReadAcc->GetScanlineFormat() && aWhite.GetIndex() == 1
467 {
468 for (tools::Long nY = 0; nY < nHeight; ++nY)
469 {
470 Scanline pSrc = pReadAcc->GetScanline(nY);
471 Scanline pDst = pWriteAcc->GetScanline(nY);
472 assert(pWriteAcc->GetScanlineSize() == pReadAcc->GetScanlineSize());
473 const tools::Long nScanlineSize = pWriteAcc->GetScanlineSize();
474 for (tools::Long nX = 0; nX < nScanlineSize; ++nX)
475 pDst[nX] = ~pSrc[nX];
476 }
477 }
478 else if (pReadAcc->GetScanlineFormat() == ScanlineFormat::N8BitPal)
479 {
480 // optimized for 8Bit source palette
481 const sal_uInt8 cTest = aTest.GetIndex();
482
483 for (tools::Long nY = 0; nY < nHeight; ++nY)
484 {
485 Scanline pSrc = pReadAcc->GetScanline(nY);
486 Scanline pDst = pWriteAcc->GetScanline(nY);
487 for (tools::Long nX = 0; nX < nWidth; ++nX)
488 {
489 if (cTest == pSrc[nX])
490 pDst[nX] = aWhite.GetIndex();
491 else
492 pDst[nX] = aBlack.GetIndex();
493 }
494 }
495 }
496 else
497 {
498 // not optimized
499 for (tools::Long nY = 0; nY < nHeight; ++nY)
500 {
501 Scanline pScanline = pWriteAcc->GetScanline(nY);
502 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
503 for (tools::Long nX = 0; nX < nWidth; ++nX)
504 {
505 if (aTest == pReadAcc->GetPixelFromData(pScanlineRead, nX))
506 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
507 else
508 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
509 }
510 }
511 }
512
513 pWriteAcc.reset();
514 pReadAcc.reset();
515
516 aNewBmp.maPrefSize = maPrefSize;
518
519 return aNewBmp;
520}
521
522Bitmap Bitmap::CreateMask(const Color& rTransColor, sal_uInt8 nTol) const
523{
524 if (nTol == 0)
525 return CreateMask(rTransColor);
526
527 ScopedReadAccess pReadAcc(const_cast<Bitmap&>(*this));
528 if (!pReadAcc)
529 return Bitmap();
530
531 // Historically LO used 1bpp masks, but 8bpp masks are much faster,
532 // better supported by hardware, and the memory savings are not worth
533 // it anymore.
534 // TODO: Possibly remove the 1bpp code later.
535
536 auto ePixelFormat = vcl::PixelFormat::N8_BPP;
537 Bitmap aNewBmp(GetSizePixel(), ePixelFormat, &Bitmap::GetGreyPalette(256));
538 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
539 if (!pWriteAcc)
540 return Bitmap();
541
542 const tools::Long nWidth = pReadAcc->Width();
543 const tools::Long nHeight = pReadAcc->Height();
544 const BitmapColor aBlack(pWriteAcc->GetBestMatchingColor(COL_BLACK));
545 const BitmapColor aWhite(pWriteAcc->GetBestMatchingColor(COL_WHITE));
546
547 BitmapColor aCol;
548 tools::Long nR, nG, nB;
549 const tools::Long nMinR = MinMax<tools::Long>(rTransColor.GetRed() - nTol, 0, 255);
550 const tools::Long nMaxR = MinMax<tools::Long>(rTransColor.GetRed() + nTol, 0, 255);
551 const tools::Long nMinG = MinMax<tools::Long>(rTransColor.GetGreen() - nTol, 0, 255);
552 const tools::Long nMaxG = MinMax<tools::Long>(rTransColor.GetGreen() + nTol, 0, 255);
553 const tools::Long nMinB = MinMax<tools::Long>(rTransColor.GetBlue() - nTol, 0, 255);
554 const tools::Long nMaxB = MinMax<tools::Long>(rTransColor.GetBlue() + nTol, 0, 255);
555
556 if (pReadAcc->HasPalette())
557 {
558 for (tools::Long nY = 0; nY < nHeight; nY++)
559 {
560 Scanline pScanline = pWriteAcc->GetScanline(nY);
561 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
562 for (tools::Long nX = 0; nX < nWidth; nX++)
563 {
564 aCol = pReadAcc->GetPaletteColor(pReadAcc->GetIndexFromData(pScanlineRead, nX));
565 nR = aCol.GetRed();
566 nG = aCol.GetGreen();
567 nB = aCol.GetBlue();
568
569 if (nMinR <= nR && nMaxR >= nR && nMinG <= nG && nMaxG >= nG && nMinB <= nB
570 && nMaxB >= nB)
571 {
572 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
573 }
574 else
575 {
576 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
577 }
578 }
579 }
580 }
581 else
582 {
583 for (tools::Long nY = 0; nY < nHeight; nY++)
584 {
585 Scanline pScanline = pWriteAcc->GetScanline(nY);
586 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
587 for (tools::Long nX = 0; nX < nWidth; nX++)
588 {
589 aCol = pReadAcc->GetPixelFromData(pScanlineRead, nX);
590 nR = aCol.GetRed();
591 nG = aCol.GetGreen();
592 nB = aCol.GetBlue();
593
594 if (nMinR <= nR && nMaxR >= nR && nMinG <= nG && nMaxG >= nG && nMinB <= nB
595 && nMaxB >= nB)
596 {
597 pWriteAcc->SetPixelOnData(pScanline, nX, aWhite);
598 }
599 else
600 {
601 pWriteAcc->SetPixelOnData(pScanline, nX, aBlack);
602 }
603 }
604 }
605 }
606
607 pWriteAcc.reset();
608 pReadAcc.reset();
609
610 aNewBmp.maPrefSize = maPrefSize;
612
613 return aNewBmp;
614}
615
616vcl::Region Bitmap::CreateRegion(const Color& rColor, const tools::Rectangle& rRect) const
617{
618 tools::Rectangle aRect(rRect);
619 ScopedReadAccess pReadAcc(const_cast<Bitmap&>(*this));
620
622 aRect.Normalize();
623
624 if (!pReadAcc)
625 return vcl::Region(aRect);
626
627 vcl::Region aRegion;
628 const tools::Long nLeft = aRect.Left();
629 const tools::Long nTop = aRect.Top();
630 const tools::Long nRight = aRect.Right();
631 const tools::Long nBottom = aRect.Bottom();
632 const BitmapColor aMatch(pReadAcc->GetBestMatchingColor(rColor));
633
634 std::vector<tools::Long> aLine;
635 tools::Long nYStart(nTop);
636 tools::Long nY(nTop);
637
638 for (; nY <= nBottom; nY++)
639 {
640 std::vector<tools::Long> aNewLine;
641 tools::Long nX(nLeft);
642 Scanline pScanlineRead = pReadAcc->GetScanline(nY);
643
644 for (; nX <= nRight;)
645 {
646 while ((nX <= nRight) && (aMatch != pReadAcc->GetPixelFromData(pScanlineRead, nX)))
647 nX++;
648
649 if (nX <= nRight)
650 {
651 aNewLine.push_back(nX);
652
653 while ((nX <= nRight) && (aMatch == pReadAcc->GetPixelFromData(pScanlineRead, nX)))
654 {
655 nX++;
656 }
657
658 aNewLine.push_back(nX - 1);
659 }
660 }
661
662 if (aNewLine != aLine)
663 {
664 // need to write aLine, it's different from the next line
665 if (!aLine.empty())
666 {
667 tools::Rectangle aSubRect;
668
669 // enter y values and proceed ystart
670 aSubRect.SetTop(nYStart);
671 aSubRect.SetBottom(nY ? nY - 1 : 0);
672
673 for (size_t a(0); a < aLine.size();)
674 {
675 aSubRect.SetLeft(aLine[a++]);
676 aSubRect.SetRight(aLine[a++]);
677 aRegion.Union(aSubRect);
678 }
679 }
680
681 // copy line as new line
682 aLine = aNewLine;
683 nYStart = nY;
684 }
685 }
686
687 // write last line if used
688 if (!aLine.empty())
689 {
690 tools::Rectangle aSubRect;
691
692 // enter y values
693 aSubRect.SetTop(nYStart);
694 aSubRect.SetBottom(nY ? nY - 1 : 0);
695
696 for (size_t a(0); a < aLine.size();)
697 {
698 aSubRect.SetLeft(aLine[a++]);
699 aSubRect.SetRight(aLine[a++]);
700 aRegion.Union(aSubRect);
701 }
702 }
703
704 pReadAcc.reset();
705
706 return aRegion;
707}
708
709bool Bitmap::Replace(const Bitmap& rMask, const Color& rReplaceColor)
710{
711 ScopedReadAccess pMaskAcc(const_cast<Bitmap&>(rMask));
712 BitmapScopedWriteAccess pAcc(*this);
713
714 if (!pMaskAcc || !pAcc)
715 return false;
716
717 const tools::Long nWidth = std::min(pMaskAcc->Width(), pAcc->Width());
718 const tools::Long nHeight = std::min(pMaskAcc->Height(), pAcc->Height());
719 const BitmapColor aMaskWhite(pMaskAcc->GetBestMatchingColor(COL_WHITE));
720 BitmapColor aReplace;
721
722 if (pAcc->HasPalette())
723 {
724 const sal_uInt16 nActColors = pAcc->GetPaletteEntryCount();
725 const sal_uInt16 nMaxColors = 1 << pAcc->GetBitCount();
726
727 // default to the nearest color
728 aReplace = pAcc->GetBestMatchingColor(rReplaceColor);
729
730 // for paletted images without a matching palette entry
731 // look for an unused palette entry (NOTE: expensive!)
732 if (pAcc->GetPaletteColor(aReplace.GetIndex()) != BitmapColor(rReplaceColor))
733 {
734 // if the palette has empty entries use the last one
735 if (nActColors < nMaxColors)
736 {
737 pAcc->SetPaletteEntryCount(nActColors + 1);
738 pAcc->SetPaletteColor(nActColors, rReplaceColor);
739 aReplace = BitmapColor(static_cast<sal_uInt8>(nActColors));
740 }
741 else
742 {
743 std::unique_ptr<bool[]> pFlags(new bool[nMaxColors]);
744
745 // Set all entries to false
746 std::fill(pFlags.get(), pFlags.get() + nMaxColors, false);
747
748 for (tools::Long nY = 0; nY < nHeight; nY++)
749 {
750 Scanline pScanline = pAcc->GetScanline(nY);
751 for (tools::Long nX = 0; nX < nWidth; nX++)
752 pFlags[pAcc->GetIndexFromData(pScanline, nX)] = true;
753 }
754
755 for (sal_uInt16 i = 0; i < nMaxColors; i++)
756 {
757 // Hurray, we do have an unused entry
758 if (!pFlags[i])
759 {
760 pAcc->SetPaletteColor(i, rReplaceColor);
761 aReplace = BitmapColor(static_cast<sal_uInt8>(i));
762 }
763 }
764 }
765 }
766 }
767 else
768 aReplace = rReplaceColor;
769
770 for (tools::Long nY = 0; nY < nHeight; nY++)
771 {
772 Scanline pScanline = pAcc->GetScanline(nY);
773 Scanline pScanlineMask = pMaskAcc->GetScanline(nY);
774 for (tools::Long nX = 0; nX < nWidth; nX++)
775 {
776 if (pMaskAcc->GetPixelFromData(pScanlineMask, nX) == aMaskWhite)
777 pAcc->SetPixelOnData(pScanline, nX, aReplace);
778 }
779 }
780
781 return true;
782}
783
784bool Bitmap::Replace(const AlphaMask& rAlpha, const Color& rMergeColor)
785{
787 ScopedReadAccess pAcc(*this);
788 AlphaMask::ScopedReadAccess pAlphaAcc(const_cast<AlphaMask&>(rAlpha));
789 BitmapScopedWriteAccess pNewAcc(aNewBmp);
790
791 if (!pAcc || !pAlphaAcc || !pNewAcc)
792 return false;
793
794 BitmapColor aCol;
795 const tools::Long nWidth = std::min(pAlphaAcc->Width(), pAcc->Width());
796 const tools::Long nHeight = std::min(pAlphaAcc->Height(), pAcc->Height());
797
798 for (tools::Long nY = 0; nY < nHeight; nY++)
799 {
800 Scanline pScanline = pNewAcc->GetScanline(nY);
801 Scanline pScanlineAlpha = pAlphaAcc->GetScanline(nY);
802 for (tools::Long nX = 0; nX < nWidth; nX++)
803 {
804 aCol = pAcc->GetColor(nY, nX);
805 aCol.Merge(rMergeColor, 255 - pAlphaAcc->GetIndexFromData(pScanlineAlpha, nX));
806 pNewAcc->SetPixelOnData(pScanline, nX, aCol);
807 }
808 }
809
810 pAcc.reset();
811 pAlphaAcc.reset();
812 pNewAcc.reset();
813
815 const Size aSize(maPrefSize);
816
817 *this = aNewBmp;
818
820 maPrefSize = aSize;
821
822 return true;
823}
824
825bool Bitmap::Replace(const Color& rSearchColor, const Color& rReplaceColor, sal_uInt8 nTol)
826{
827 if (mxSalBmp)
828 {
829 // implementation specific replace
830 std::shared_ptr<SalBitmap> xImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
831 if (xImpBmp->Create(*mxSalBmp) && xImpBmp->Replace(rSearchColor, rReplaceColor, nTol))
832 {
833 ImplSetSalBitmap(xImpBmp);
834 maPrefMapMode = MapMode(MapUnit::MapPixel);
835 maPrefSize = xImpBmp->GetSize();
836 return true;
837 }
838 }
839
840 BitmapScopedWriteAccess pAcc(*this);
841 bool bRet = false;
842
843 if (pAcc)
844 {
845 const tools::Long nMinR = MinMax<tools::Long>(rSearchColor.GetRed() - nTol, 0, 255);
846 const tools::Long nMaxR = MinMax<tools::Long>(rSearchColor.GetRed() + nTol, 0, 255);
847 const tools::Long nMinG = MinMax<tools::Long>(rSearchColor.GetGreen() - nTol, 0, 255);
848 const tools::Long nMaxG = MinMax<tools::Long>(rSearchColor.GetGreen() + nTol, 0, 255);
849 const tools::Long nMinB = MinMax<tools::Long>(rSearchColor.GetBlue() - nTol, 0, 255);
850 const tools::Long nMaxB = MinMax<tools::Long>(rSearchColor.GetBlue() + nTol, 0, 255);
851
852 if (pAcc->HasPalette())
853 {
854 for (sal_uInt16 i = 0, nPalCount = pAcc->GetPaletteEntryCount(); i < nPalCount; i++)
855 {
856 const BitmapColor& rCol = pAcc->GetPaletteColor(i);
857
858 if (nMinR <= rCol.GetRed() && nMaxR >= rCol.GetRed() && nMinG <= rCol.GetGreen()
859 && nMaxG >= rCol.GetGreen() && nMinB <= rCol.GetBlue()
860 && nMaxB >= rCol.GetBlue())
861 {
862 pAcc->SetPaletteColor(i, rReplaceColor);
863 }
864 }
865 }
866 else
867 {
868 BitmapColor aCol;
869 const BitmapColor aReplace(pAcc->GetBestMatchingColor(rReplaceColor));
870
871 for (tools::Long nY = 0, nHeight = pAcc->Height(); nY < nHeight; nY++)
872 {
873 Scanline pScanline = pAcc->GetScanline(nY);
874 for (tools::Long nX = 0, nWidth = pAcc->Width(); nX < nWidth; nX++)
875 {
876 aCol = pAcc->GetPixelFromData(pScanline, nX);
877
878 if (nMinR <= aCol.GetRed() && nMaxR >= aCol.GetRed() && nMinG <= aCol.GetGreen()
879 && nMaxG >= aCol.GetGreen() && nMinB <= aCol.GetBlue()
880 && nMaxB >= aCol.GetBlue())
881 {
882 pAcc->SetPixelOnData(pScanline, nX, aReplace);
883 }
884 }
885 }
886 }
887
888 pAcc.reset();
889 bRet = true;
890 }
891
892 return bRet;
893}
894
895bool Bitmap::Replace(const Color* pSearchColors, const Color* pReplaceColors, size_t nColorCount,
896 sal_uInt8 const* pTols)
897{
898 BitmapScopedWriteAccess pAcc(*this);
899 bool bRet = false;
900
901 if (pAcc)
902 {
903 std::vector<sal_uInt8> aMinR(nColorCount);
904 std::vector<sal_uInt8> aMaxR(nColorCount);
905 std::vector<sal_uInt8> aMinG(nColorCount);
906 std::vector<sal_uInt8> aMaxG(nColorCount);
907 std::vector<sal_uInt8> aMinB(nColorCount);
908 std::vector<sal_uInt8> aMaxB(nColorCount);
909
910 if (pTols)
911 {
912 for (size_t i = 0; i < nColorCount; ++i)
913 {
914 const Color& rCol = pSearchColors[i];
915 const sal_uInt8 nTol = pTols[i];
916
917 aMinR[i] = std::clamp(rCol.GetRed() - nTol, 0, 255);
918 aMaxR[i] = std::clamp(rCol.GetRed() + nTol, 0, 255);
919 aMinG[i] = std::clamp(rCol.GetGreen() - nTol, 0, 255);
920 aMaxG[i] = std::clamp(rCol.GetGreen() + nTol, 0, 255);
921 aMinB[i] = std::clamp(rCol.GetBlue() - nTol, 0, 255);
922 aMaxB[i] = std::clamp(rCol.GetBlue() + nTol, 0, 255);
923 }
924 }
925 else
926 {
927 for (size_t i = 0; i < nColorCount; ++i)
928 {
929 const Color& rCol = pSearchColors[i];
930
931 aMinR[i] = rCol.GetRed();
932 aMaxR[i] = rCol.GetRed();
933 aMinG[i] = rCol.GetGreen();
934 aMaxG[i] = rCol.GetGreen();
935 aMinB[i] = rCol.GetBlue();
936 aMaxB[i] = rCol.GetBlue();
937 }
938 }
939
940 if (pAcc->HasPalette())
941 {
942 for (sal_uInt16 nEntry = 0, nPalCount = pAcc->GetPaletteEntryCount();
943 nEntry < nPalCount; nEntry++)
944 {
945 const BitmapColor& rCol = pAcc->GetPaletteColor(nEntry);
946
947 for (size_t i = 0; i < nColorCount; ++i)
948 {
949 if (aMinR[i] <= rCol.GetRed() && aMaxR[i] >= rCol.GetRed()
950 && aMinG[i] <= rCol.GetGreen() && aMaxG[i] >= rCol.GetGreen()
951 && aMinB[i] <= rCol.GetBlue() && aMaxB[i] >= rCol.GetBlue())
952 {
953 pAcc->SetPaletteColor(nEntry, pReplaceColors[i]);
954 break;
955 }
956 }
957 }
958 }
959 else
960 {
961 std::vector<BitmapColor> aReplaces(nColorCount);
962
963 for (size_t i = 0; i < nColorCount; ++i)
964 aReplaces[i] = pAcc->GetBestMatchingColor(pReplaceColors[i]);
965
966 for (tools::Long nY = 0, nHeight = pAcc->Height(); nY < nHeight; nY++)
967 {
968 Scanline pScanline = pAcc->GetScanline(nY);
969 for (tools::Long nX = 0, nWidth = pAcc->Width(); nX < nWidth; nX++)
970 {
971 BitmapColor aCol = pAcc->GetPixelFromData(pScanline, nX);
972
973 for (size_t i = 0; i < nColorCount; ++i)
974 {
975 if (aMinR[i] <= aCol.GetRed() && aMaxR[i] >= aCol.GetRed()
976 && aMinG[i] <= aCol.GetGreen() && aMaxG[i] >= aCol.GetGreen()
977 && aMinB[i] <= aCol.GetBlue() && aMaxB[i] >= aCol.GetBlue())
978 {
979 pAcc->SetPixelOnData(pScanline, nX, aReplaces[i]);
980 break;
981 }
982 }
983 }
984 }
985 }
986
987 pAcc.reset();
988 bRet = true;
989 }
990
991 return bRet;
992}
993
994bool Bitmap::CombineOr(const Bitmap& rMask)
995{
996 ScopedReadAccess pMaskAcc(const_cast<Bitmap&>(rMask));
997 BitmapScopedWriteAccess pAcc(*this);
998
999 if (!pMaskAcc || !pAcc)
1000 return false;
1001
1002 const tools::Long nWidth = std::min(pMaskAcc->Width(), pAcc->Width());
1003 const tools::Long nHeight = std::min(pMaskAcc->Height(), pAcc->Height());
1004 const Color aColBlack(COL_BLACK);
1005 const BitmapColor aWhite(pAcc->GetBestMatchingColor(COL_WHITE));
1006 const BitmapColor aBlack(pAcc->GetBestMatchingColor(aColBlack));
1007 const BitmapColor aMaskBlack(pMaskAcc->GetBestMatchingColor(aColBlack));
1008
1009 for (tools::Long nY = 0; nY < nHeight; nY++)
1010 {
1011 Scanline pScanline = pAcc->GetScanline(nY);
1012 Scanline pScanlineMask = pMaskAcc->GetScanline(nY);
1013 for (tools::Long nX = 0; nX < nWidth; nX++)
1014 {
1015 if (pMaskAcc->GetPixelFromData(pScanlineMask, nX) != aMaskBlack
1016 || pAcc->GetPixelFromData(pScanline, nX) != aBlack)
1017 {
1018 pAcc->SetPixelOnData(pScanline, nX, aWhite);
1019 }
1020 else
1021 {
1022 pAcc->SetPixelOnData(pScanline, nX, aBlack);
1023 }
1024 }
1025 }
1026
1027 return true;
1028}
1029
1030// TODO: Have a look at OutputDevice::ImplDrawAlpha() for some
1031// optimizations. Might even consolidate the code here and there.
1032bool Bitmap::Blend(const AlphaMask& rAlpha, const Color& rBackgroundColor)
1033{
1034 // Convert to a truecolor bitmap, if we're a paletted one. There's room for tradeoff decision here,
1035 // maybe later for an overload (or a flag)
1038
1039 AlphaMask::ScopedReadAccess pAlphaAcc(const_cast<AlphaMask&>(rAlpha));
1040
1041 BitmapScopedWriteAccess pAcc(*this);
1042
1043 if (!pAlphaAcc || !pAcc)
1044 return false;
1045
1046 const tools::Long nWidth = std::min(pAlphaAcc->Width(), pAcc->Width());
1047 const tools::Long nHeight = std::min(pAlphaAcc->Height(), pAcc->Height());
1048
1049 for (tools::Long nY = 0; nY < nHeight; ++nY)
1050 {
1051 Scanline pScanline = pAcc->GetScanline(nY);
1052 Scanline pScanlineAlpha = pAlphaAcc->GetScanline(nY);
1053 for (tools::Long nX = 0; nX < nWidth; ++nX)
1054 {
1055 BitmapColor aBmpColor = pAcc->GetPixelFromData(pScanline, nX);
1056 aBmpColor.Merge(rBackgroundColor,
1057 255 - pAlphaAcc->GetIndexFromData(pScanlineAlpha, nX));
1058 pAcc->SetPixelOnData(pScanline, nX, aBmpColor);
1059 }
1060 }
1061
1062 return true;
1063}
1064
1065/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:70
tools::Long Height() const
tools::Long Width() const
const BitmapPalette & GetPalette() const
BitmapColor GetBestMatchingColor(const BitmapColor &rBitmapColor) const
bool HasPalette() const
ScanlineFormat GetScanlineFormat() const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
sal_uInt32 GetScanlineSize() const
sal_uInt16 GetEntryCount() 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 CombineOr(const Bitmap &rMask)
Perform boolean OR operation with another bitmap.
bool Blend(const AlphaMask &rAlpha, const Color &rBackgroundColor)
Alpha-blend the given bitmap against a specified uniform background color.
bool Rotate(Degree10 nAngle10, const Color &rFillColor)
Rotate bitmap by the specified angle.
SAL_DLLPRIVATE void ImplSetSalBitmap(const std::shared_ptr< SalBitmap > &xImpBmp)
Bitmap CreateMask(const Color &rTransColor) const
Create on-off mask from bitmap.
bool Convert(BmpConversion eConversion)
Convert bitmap format.
static const BitmapPalette & GetGreyPalette(int nEntries)
SAL_DLLPRIVATE void ReassignWithSize(const Bitmap &rBitmap)
ReassignWithSize and recalculate bitmap.
Size GetSizePixel() const
bool IsEmpty() const
bool Invert()
Perform the Invert operation on every pixel.
Definition: bitmappaint.cxx:61
vcl::Region CreateRegion(const Color &rColor, const tools::Rectangle &rRect) const
Create region of similar colors in a given rectangle.
bool Erase(const Color &rFillColor)
Fill the entire bitmap with the given color.
Definition: bitmappaint.cxx:34
MapMode maPrefMapMode
bool Replace(const Bitmap &rMask, const Color &rReplaceColor)
Replace all pixel where the given mask is on with the specified color.
vcl::PixelFormat getPixelFormat() const
std::shared_ptr< SalBitmap > mxSalBmp
bool Mirror(BmpMirrorFlags nMirrorFlags)
Mirror the bitmap.
sal_uInt8 GetBlue() const
void Merge(const Color &rMergeColor, sal_uInt8 cTransparency)
void Invert()
sal_uInt8 GetRed() const
sal_uInt8 GetGreen() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
tools::Rectangle GetBoundRect() const
void Rotate(const Point &rCenter, double fSin, double fCos)
constexpr void SetLeft(tools::Long v)
constexpr void SetTop(tools::Long v)
constexpr tools::Long Top() const
constexpr void SetRight(tools::Long v)
constexpr Size GetSize() const
constexpr tools::Long Right() const
constexpr void SetBottom(tools::Long v)
tools::Rectangle & Intersection(const tools::Rectangle &rRect)
constexpr tools::Long Left() const
constexpr tools::Long Bottom() const
void Union(const tools::Rectangle &rRegion)
Definition: region.cxx:507
This template handles BitmapAccess the RAII way.
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
int nCount
double toRadians(D x)
BmpMirrorFlags
uno_Any a
int i
long Long
constexpr bool isPalettePixelFormat(PixelFormat ePixelFormat)
Is it a pixel format that forces creation of a palette.
Definition: BitmapTypes.hxx:28
HashMap_OWString_Interface aMap
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
unsigned char sal_uInt8