LibreOffice Module vcl (master)  1
BitmapFastScaleFilter.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/helpers.hxx>
21 
22 #include <vcl/bitmapex.hxx>
23 #include <vcl/bitmapaccess.hxx>
24 
25 #include <bitmapwriteaccess.hxx>
27 #include <sal/log.hxx>
28 
30 {
31  SAL_INFO("vcl.gdi", "BitmapFastScaleFilter::execute()");
32 
33  Bitmap aBitmap(rBitmapEx.GetBitmap());
34 
35  const Size aSizePix(aBitmap.GetSizePixel());
36  const long nNewWidth = FRound(aSizePix.Width() * mfScaleX);
37  const long nNewHeight = FRound(aSizePix.Height() * mfScaleY);
38  bool bRet = false;
39 
40  SAL_INFO("vcl.gdi", "New width: " << nNewWidth << "\nNew height: " << nNewHeight);
41 
42  if (nNewWidth && nNewHeight)
43  {
44  Bitmap::ScopedReadAccess pReadAcc(aBitmap);
45 
46  if (pReadAcc)
47  {
48  Bitmap aNewBmp(Size(nNewWidth, nNewHeight), aBitmap.GetBitCount(),
49  &pReadAcc->GetPalette());
50  BitmapScopedWriteAccess pWriteAcc(aNewBmp);
51 
52  if (pWriteAcc)
53  {
54  const long nScanlineSize = pWriteAcc->GetScanlineSize();
55  const long nNewWidth1 = nNewWidth - 1;
56  const long nNewHeight1 = nNewHeight - 1;
57 
58  if (nNewWidth1 && nNewHeight1)
59  {
60  const double nWidth = pReadAcc->Width();
61  const double nHeight = pReadAcc->Height();
62  std::unique_ptr<long[]> pLutX(new long[nNewWidth]);
63  std::unique_ptr<long[]> pLutY(new long[nNewHeight]);
64 
65  for (long nX = 0; nX < nNewWidth; nX++)
66  {
67  pLutX[nX] = long(nX * nWidth / nNewWidth);
68  }
69 
70  for (long nY = 0; nY < nNewHeight; nY++)
71  {
72  pLutY[nY] = long(nY * nHeight / nNewHeight);
73  }
74 
75  long nActY = 0;
76  while (nActY < nNewHeight)
77  {
78  long nMapY = pLutY[nActY];
79  Scanline pScanline = pWriteAcc->GetScanline(nActY);
80  Scanline pScanlineRead = pReadAcc->GetScanline(nMapY);
81 
82  for (long nX = 0; nX < nNewWidth; nX++)
83  {
84  pWriteAcc->SetPixelOnData(
85  pScanline, nX,
86  pReadAcc->GetPixelFromData(pScanlineRead, pLutX[nX]));
87  }
88 
89  while ((nActY < nNewHeight1) && (pLutY[nActY + 1] == nMapY))
90  {
91  memcpy(pWriteAcc->GetScanline(nActY + 1), pWriteAcc->GetScanline(nActY),
92  nScanlineSize);
93  nActY++;
94  }
95  nActY++;
96  }
97 
98  bRet = true;
99  }
100 
101  pWriteAcc.reset();
102  }
103  pReadAcc.reset();
104 
105  if (bRet)
106  {
107  aBitmap.ReassignWithSize(aNewBmp);
108  SAL_INFO("vcl.gdi", "Bitmap size: " << aBitmap.GetSizePixel());
109  }
110  else
111  {
112  SAL_WARN("vcl.gdi", "no resize");
113  }
114  }
115  }
116 
117  Bitmap aMask(rBitmapEx.GetMask());
118 
119  if (bRet && (rBitmapEx.GetTransparentType() == TransparentType::Bitmap) && !aMask.IsEmpty())
120  bRet = aMask.Scale(maSize, BmpScaleFlag::Fast);
121 
122  SAL_WARN_IF(!aMask.IsEmpty() && aBitmap.GetSizePixel() != aMask.GetSizePixel(), "vcl",
123  "BitmapEx::Scale(): size mismatch for bitmap and alpha mask.");
124 
125  if (bRet)
126  return BitmapEx(aBitmap, aMask);
127 
128  return BitmapEx();
129 }
130 
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Bitmap GetMask() const
Definition: bitmapex.cxx:258
long FRound(double fVal)
Scanline GetScanline(long nY) const
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
long Width() const
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
TransparentType GetTransparentType() const
Definition: bitmapex.hxx:73
long Height() const
#define SAL_WARN_IF(condition, area, stream)
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
Definition: bitmap3.cxx:818
const BitmapPalette & GetPalette() const
#define SAL_INFO(area, stream)
BitmapColor GetPixelFromData(const sal_uInt8 *pData, long nX) const
#define SAL_WARN(area, stream)