1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <basegfx/color/bcolortools.hxx>
#include <vcl/bitmap.hxx>
#include <vcl/bitmapex.hxx>
#include <vcl/bitmapaccess.hxx>
#include <vcl/BitmapGaussianSeparableBlurFilter.hxx>
#include <bitmapwriteaccess.hxx>
BitmapEx BitmapGaussianSeparableBlurFilter::execute(BitmapEx const& rBitmapEx)
{
Bitmap aBitmap(rBitmapEx.GetBitmap());
const long nWidth = aBitmap.GetSizePixel().Width();
const long nHeight = aBitmap.GetSizePixel().Height();
// Prepare Blur Vector
int aNumberOfContributions;
double* pBlurVector = makeBlurKernel(mfRadius, aNumberOfContributions);
double* pWeights;
int* pPixels;
int* pCount;
// Do horizontal filtering
blurContributions(nWidth, aNumberOfContributions, pBlurVector, pWeights, pPixels, pCount);
Bitmap::ScopedReadAccess pReadAcc(aBitmap);
// switch coordinates as convolution pass transposes result
Bitmap aNewBitmap(Size(nHeight, nWidth), 24);
bool bResult = convolutionPass(aBitmap, aNewBitmap, pReadAcc.get(), aNumberOfContributions,
pWeights, pPixels, pCount);
// Cleanup
pReadAcc.reset();
delete[] pWeights;
delete[] pPixels;
delete[] pCount;
if (!bResult)
{
delete[] pBlurVector;
}
else
{
// Swap current bitmap with new bitmap
aBitmap.ReassignWithSize(aNewBitmap);
// Do vertical filtering
blurContributions(nHeight, aNumberOfContributions, pBlurVector, pWeights, pPixels, pCount);
pReadAcc = Bitmap::ScopedReadAccess(aBitmap);
aNewBitmap = Bitmap(Size(nWidth, nHeight), 24);
bResult = convolutionPass(aBitmap, aNewBitmap, pReadAcc.get(), aNumberOfContributions,
pWeights, pPixels, pCount);
// Cleanup
pReadAcc.reset();
delete[] pWeights;
delete[] pCount;
delete[] pPixels;
delete[] pBlurVector;
if (bResult)
aBitmap.ReassignWithSize(aNewBitmap); // swap current bitmap with new bitmap
}
if (bResult)
return BitmapEx(aBitmap);
return BitmapEx();
}
bool BitmapGaussianSeparableBlurFilter::convolutionPass(Bitmap& rBitmap, Bitmap& aNewBitmap,
BitmapReadAccess const* pReadAcc,
int aNumberOfContributions,
const double* pWeights, int const* pPixels,
const int* pCount)
{
if (!pReadAcc)
return false;
BitmapScopedWriteAccess pWriteAcc(aNewBitmap);
if (!pWriteAcc)
return false;
const int nHeight = rBitmap.GetSizePixel().Height();
assert(rBitmap.GetSizePixel().Height() == aNewBitmap.GetSizePixel().Width());
const int nWidth = rBitmap.GetSizePixel().Width();
assert(rBitmap.GetSizePixel().Width() == aNewBitmap.GetSizePixel().Height());
BitmapColor aColor;
double aValueRed, aValueGreen, aValueBlue;
double aSum, aWeight;
int aBaseIndex, aIndex;
for (int nSourceY = 0; nSourceY < nHeight; ++nSourceY)
{
for (int nSourceX = 0; nSourceX < nWidth; ++nSourceX)
{
aBaseIndex = nSourceX * aNumberOfContributions;
aSum = aValueRed = aValueGreen = aValueBlue = 0.0;
for (int j = 0; j < pCount[nSourceX]; ++j)
{
aIndex = aBaseIndex + j;
aSum += aWeight = pWeights[aIndex];
aColor = pReadAcc->GetColor(nSourceY, pPixels[aIndex]);
aValueRed += aWeight * aColor.GetRed();
aValueGreen += aWeight * aColor.GetGreen();
aValueBlue += aWeight * aColor.GetBlue();
}
BitmapColor aResultColor(static_cast<sal_uInt8>(MinMax(aValueRed / aSum, 0, 255)),
static_cast<sal_uInt8>(MinMax(aValueGreen / aSum, 0, 255)),
static_cast<sal_uInt8>(MinMax(aValueBlue / aSum, 0, 255)));
int nDestX = nSourceY;
int nDestY = nSourceX;
pWriteAcc->SetPixel(nDestY, nDestX, aResultColor);
}
}
return true;
}
double* BitmapGaussianSeparableBlurFilter::makeBlurKernel(const double radius, int& rows)
{
int intRadius = static_cast<int>(radius + 1.0);
rows = intRadius * 2 + 1;
double* matrix = new double[rows];
double sigma = radius / 3;
double radius2 = radius * radius;
int index = 0;
for (int row = -intRadius; row <= intRadius; row++)
{
double distance = row * row;
if (distance > radius2)
{
matrix[index] = 0.0;
}
else
{
matrix[index] = exp(-distance / (2.0 * sigma * sigma)) / sqrt(2.0 * M_PI * sigma);
}
index++;
}
return matrix;
}
void BitmapGaussianSeparableBlurFilter::blurContributions(const int aSize,
const int aNumberOfContributions,
const double* pBlurVector,
double*& pWeights, int*& pPixels,
int*& pCount)
{
pWeights = new double[aSize * aNumberOfContributions];
pPixels = new int[aSize * aNumberOfContributions];
pCount = new int[aSize];
int aLeft, aRight, aCurrentCount, aPixelIndex;
double aWeight;
for (int i = 0; i < aSize; i++)
{
aLeft = i - aNumberOfContributions / 2;
aRight = i + aNumberOfContributions / 2;
aCurrentCount = 0;
for (int j = aLeft; j <= aRight; j++)
{
aWeight = pBlurVector[aCurrentCount];
// Mirror edges
if (j < 0)
{
aPixelIndex = -j;
}
else if (j >= aSize)
{
aPixelIndex = (aSize - j) + aSize - 1;
}
else
{
aPixelIndex = j;
}
// Edge case for small bitmaps
if (aPixelIndex < 0 || aPixelIndex >= aSize)
{
aWeight = 0.0;
}
pWeights[i * aNumberOfContributions + aCurrentCount] = aWeight;
pPixels[i * aNumberOfContributions + aCurrentCount] = aPixelIndex;
aCurrentCount++;
}
pCount[i] = aCurrentCount;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|