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 sal_Int32 nNewWidth = FRound(aSizePix.Width() * mfScaleX);
36 const sal_Int32 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 > 0 && nNewHeight > 0)
42 {
43 Bitmap::ScopedReadAccess pReadAcc(aBitmap);
44
45 if (pReadAcc)
46 {
47 Bitmap aNewBmp(Size(nNewWidth, nNewHeight), aBitmap.getPixelFormat(),
48 &pReadAcc->GetPalette());
49 BitmapScopedWriteAccess pWriteAcc(aNewBmp);
50
51 if (pWriteAcc)
52 {
53 const sal_Int32 nScanlineSize = pWriteAcc->GetScanlineSize();
54 const sal_Int32 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<sal_Int32[]> pLutX(new sal_Int32[nNewWidth]);
61 std::unique_ptr<sal_Int32[]> pLutY(new sal_Int32[nNewHeight]);
62
63 for (sal_Int32 nX = 0; nX < nNewWidth; nX++)
64 {
65 pLutX[nX] = sal_Int32(nX * nWidth / nNewWidth);
66 }
67
68 for (sal_Int32 nY = 0; nY < nNewHeight; nY++)
69 {
70 pLutY[nY] = sal_Int32(nY * nHeight / nNewHeight);
71 }
72
73 sal_Int32 nActY = 0;
74 while (nActY < nNewHeight)
75 {
76 sal_Int32 nMapY = pLutY[nActY];
77 Scanline pScanline = pWriteAcc->GetScanline(nActY);
78 Scanline pScanlineRead = pReadAcc->GetScanline(nMapY);
79
80 for (sal_Int32 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 AlphaMask aMask(rBitmapEx.GetAlphaMask());
116
117 if (bRet && !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: */
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
bool IsEmpty() const
const AlphaMask & GetAlphaMask() const
Definition: bitmapex.hxx:71
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
virtual BitmapEx execute(BitmapEx const &rBitmapEx) const override
tools::Long Height() const
tools::Long Width() const
const BitmapPalette & GetPalette() const
BitmapColor GetPixelFromData(const sal_uInt8 *pData, tools::Long nX) const
Scanline GetScanline(tools::Long nY) const
SAL_DLLPRIVATE void ReassignWithSize(const Bitmap &rBitmap)
ReassignWithSize and recalculate bitmap.
Size GetSizePixel() const
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
vcl::PixelFormat getPixelFormat() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
tools::Long FRound(double fVal)
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)