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 nNewHeight1 = nNewHeight - 1;
56 
57  if (nNewWidth && nNewHeight)
58  {
59  const double nWidth = pReadAcc->Width();
60  const double nHeight = pReadAcc->Height();
61  std::unique_ptr<long[]> pLutX(new long[nNewWidth]);
62  std::unique_ptr<long[]> pLutY(new long[nNewHeight]);
63 
64  for (long nX = 0; nX < nNewWidth; nX++)
65  {
66  pLutX[nX] = long(nX * nWidth / nNewWidth);
67  }
68 
69  for (long nY = 0; nY < nNewHeight; nY++)
70  {
71  pLutY[nY] = long(nY * nHeight / nNewHeight);
72  }
73 
74  long nActY = 0;
75  while (nActY < nNewHeight)
76  {
77  long nMapY = pLutY[nActY];
78  Scanline pScanline = pWriteAcc->GetScanline(nActY);
79  Scanline pScanlineRead = pReadAcc->GetScanline(nMapY);
80 
81  for (long nX = 0; nX < nNewWidth; nX++)
82  {
83  pWriteAcc->SetPixelOnData(
84  pScanline, nX,
85  pReadAcc->GetPixelFromData(pScanlineRead, pLutX[nX]));
86  }
87 
88  while ((nActY < nNewHeight1) && (pLutY[nActY + 1] == nMapY))
89  {
90  memcpy(pWriteAcc->GetScanline(nActY + 1), pWriteAcc->GetScanline(nActY),
91  nScanlineSize);
92  nActY++;
93  }
94  nActY++;
95  }
96 
97  bRet = true;
98  }
99 
100  pWriteAcc.reset();
101  }
102  pReadAcc.reset();
103 
104  if (bRet)
105  {
106  aBitmap.ReassignWithSize(aNewBmp);
107  SAL_INFO("vcl.gdi", "Bitmap size: " << aBitmap.GetSizePixel());
108  }
109  else
110  {
111  SAL_WARN("vcl.gdi", "no resize");
112  }
113  }
114  }
115 
116  Bitmap aMask(rBitmapEx.GetMask());
117 
118  if (bRet && (rBitmapEx.GetTransparentType() == TransparentType::Bitmap) && !aMask.IsEmpty())
119  bRet = aMask.Scale(maSize, BmpScaleFlag::Fast);
120 
121  SAL_WARN_IF(!aMask.IsEmpty() && aBitmap.GetSizePixel() != aMask.GetSizePixel(), "vcl",
122  "BitmapEx::Scale(): size mismatch for bitmap and alpha mask.");
123 
124  if (bRet)
125  return BitmapEx(aBitmap, aMask);
126 
127  return BitmapEx();
128 }
129 
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Bitmap GetMask() const
Definition: bitmapex.cxx:254
long FRound(double fVal)
Scanline GetScanline(long nY) const
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
long Width() const
sal_uInt8 * Scanline
Definition: Scanline.hxx:25
TransparentType GetTransparentType() const
Definition: bitmapex.hxx:73
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: bitmapex.cxx:232
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:764
const BitmapPalette & GetPalette() const
#define SAL_INFO(area, stream)
BitmapColor GetPixelFromData(const sal_uInt8 *pData, long nX) const
#define SAL_WARN(area, stream)