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