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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <osl/diagnose.h>
#include <vcl/bitmapaccess.hxx>
#include <bitmapwriteaccess.hxx>
#include <BitmapScaleConvolutionFilter.hxx>
#include <algorithm>
#include <memory>
namespace vcl
{
namespace
{
void ImplCalculateContributions(
const long aSourceSize,
const long aDestinationSize,
long& aNumberOfContributions,
double*& pWeights,
long*& pPixels,
long*& pCount,
const Kernel& aKernel)
{
const double fSamplingRadius(aKernel.GetWidth());
const double fScale(aDestinationSize / static_cast< double >(aSourceSize));
const double fScaledRadius((fScale < 1.0) ? fSamplingRadius / fScale : fSamplingRadius);
const double fFilterFactor(std::min(fScale, 1.0));
aNumberOfContributions = (long(fabs(ceil(fScaledRadius))) * 2) + 1;
const long nAllocSize(aDestinationSize * aNumberOfContributions);
pWeights = new double[nAllocSize];
pPixels = new long[nAllocSize];
pCount = new long[aDestinationSize];
for(long i(0); i < aDestinationSize; i++)
{
const long aIndex(i * aNumberOfContributions);
const double aCenter(i / fScale);
const sal_Int32 aLeft(static_cast< sal_Int32 >(floor(aCenter - fScaledRadius)));
const sal_Int32 aRight(static_cast< sal_Int32 >(ceil(aCenter + fScaledRadius)));
long aCurrentCount(0);
for(sal_Int32 j(aLeft); j <= aRight; j++)
{
const double aWeight(aKernel.Calculate(fFilterFactor * (aCenter - static_cast< double>(j))));
// Reduce calculations with ignoring weights of 0.0
if(fabs(aWeight) < 0.0001)
{
continue;
}
// Handling on edges
const long aPixelIndex(MinMax(j, 0, aSourceSize - 1));
const long nIndex(aIndex + aCurrentCount);
pWeights[nIndex] = aWeight;
pPixels[nIndex] = aPixelIndex;
aCurrentCount++;
}
pCount[i] = aCurrentCount;
}
}
bool ImplScaleConvolutionHor(Bitmap& rSource, Bitmap& rTarget, const double& rScaleX, const Kernel& aKernel)
{
// Do horizontal filtering
OSL_ENSURE(rScaleX > 0.0, "Error in scaling: Mirror given in non-mirror-capable method (!)");
const long nWidth(rSource.GetSizePixel().Width());
const long nNewWidth(FRound(nWidth * rScaleX));
if(nWidth == nNewWidth)
{
return true;
}
Bitmap::ScopedReadAccess pReadAcc(rSource);
if(pReadAcc)
{
double* pWeights = nullptr;
long* pPixels = nullptr;
long* pCount = nullptr;
long aNumberOfContributions(0);
const long nHeight(rSource.GetSizePixel().Height());
ImplCalculateContributions(nWidth, nNewWidth, aNumberOfContributions, pWeights, pPixels, pCount, aKernel);
rTarget = Bitmap(Size(nNewWidth, nHeight), 24);
BitmapScopedWriteAccess pWriteAcc(rTarget);
bool bResult(pWriteAcc);
if(bResult)
{
for(long y(0); y < nHeight; y++)
{
Scanline pScanline = pWriteAcc->GetScanline( y );
Scanline pScanlineRead = pReadAcc->GetScanline( y );
for(long x(0); x < nNewWidth; x++)
{
const long aBaseIndex(x * aNumberOfContributions);
double aSum(0.0);
double aValueRed(0.0);
double aValueGreen(0.0);
double aValueBlue(0.0);
for(long j(0); j < pCount[x]; j++)
{
const long aIndex(aBaseIndex + j);
const double aWeight(pWeights[aIndex]);
BitmapColor aColor;
aSum += aWeight;
if(pReadAcc->HasPalette())
{
aColor = pReadAcc->GetPaletteColor(pReadAcc->GetIndexFromData(pScanlineRead, pPixels[aIndex]));
}
else
{
aColor = pReadAcc->GetPixelFromData(pScanlineRead, pPixels[aIndex]);
}
aValueRed += aWeight * aColor.GetRed();
aValueGreen += aWeight * aColor.GetGreen();
aValueBlue += aWeight * aColor.GetBlue();
}
const BitmapColor aResultColor(
static_cast< sal_uInt8 >(MinMax(static_cast< sal_Int32 >(aValueRed / aSum), 0, 255)),
static_cast< sal_uInt8 >(MinMax(static_cast< sal_Int32 >(aValueGreen / aSum), 0, 255)),
static_cast< sal_uInt8 >(MinMax(static_cast< sal_Int32 >(aValueBlue / aSum), 0, 255)));
pWriteAcc->SetPixelOnData(pScanline, x, aResultColor);
}
}
pWriteAcc.reset();
}
delete[] pWeights;
delete[] pCount;
delete[] pPixels;
if(bResult)
{
return true;
}
}
return false;
}
bool ImplScaleConvolutionVer(Bitmap& rSource, Bitmap& rTarget, const double& rScaleY, const Kernel& aKernel)
{
// Do vertical filtering
OSL_ENSURE(rScaleY > 0.0, "Error in scaling: Mirror given in non-mirror-capable method (!)");
const long nHeight(rSource.GetSizePixel().Height());
const long nNewHeight(FRound(nHeight * rScaleY));
if(nHeight == nNewHeight)
{
return true;
}
Bitmap::ScopedReadAccess pReadAcc(rSource);
if(pReadAcc)
{
double* pWeights = nullptr;
long* pPixels = nullptr;
long* pCount = nullptr;
long aNumberOfContributions(0);
const long nWidth(rSource.GetSizePixel().Width());
ImplCalculateContributions(nHeight, nNewHeight, aNumberOfContributions, pWeights, pPixels, pCount, aKernel);
rTarget = Bitmap(Size(nWidth, nNewHeight), 24);
BitmapScopedWriteAccess pWriteAcc(rTarget);
bool bResult(pWriteAcc);
if(pWriteAcc)
{
for(long x(0); x < nWidth; x++)
{
for(long y(0); y < nNewHeight; y++)
{
const long aBaseIndex(y * aNumberOfContributions);
double aSum(0.0);
double aValueRed(0.0);
double aValueGreen(0.0);
double aValueBlue(0.0);
for(long j(0); j < pCount[y]; j++)
{
const long aIndex(aBaseIndex + j);
const double aWeight(pWeights[aIndex]);
BitmapColor aColor;
aSum += aWeight;
if(pReadAcc->HasPalette())
{
aColor = pReadAcc->GetPaletteColor(pReadAcc->GetPixelIndex(pPixels[aIndex], x));
}
else
{
aColor = pReadAcc->GetPixel(pPixels[aIndex], x);
}
aValueRed += aWeight * aColor.GetRed();
aValueGreen += aWeight * aColor.GetGreen();
aValueBlue += aWeight * aColor.GetBlue();
}
const BitmapColor aResultColor(
static_cast< sal_uInt8 >(MinMax(static_cast< sal_Int32 >(aValueRed / aSum), 0, 255)),
static_cast< sal_uInt8 >(MinMax(static_cast< sal_Int32 >(aValueGreen / aSum), 0, 255)),
static_cast< sal_uInt8 >(MinMax(static_cast< sal_Int32 >(aValueBlue / aSum), 0, 255)));
if(pWriteAcc->HasPalette())
{
pWriteAcc->SetPixelIndex(y, x, static_cast< sal_uInt8 >(pWriteAcc->GetBestPaletteIndex(aResultColor)));
}
else
{
pWriteAcc->SetPixel(y, x, aResultColor);
}
}
}
}
delete[] pWeights;
delete[] pCount;
delete[] pPixels;
if(bResult)
{
return true;
}
}
return false;
}
bool ImplScaleConvolution(Bitmap& rBitmap, const double& rScaleX, const double& rScaleY, const Kernel& aKernel)
{
const bool bMirrorHor(rScaleX < 0.0);
const bool bMirrorVer(rScaleY < 0.0);
const double fScaleX(bMirrorHor ? -rScaleX : rScaleX);
const double fScaleY(bMirrorVer ? -rScaleY : rScaleY);
const long nWidth(rBitmap.GetSizePixel().Width());
const long nHeight(rBitmap.GetSizePixel().Height());
const long nNewWidth(FRound(nWidth * fScaleX));
const long nNewHeight(FRound(nHeight * fScaleY));
const bool bScaleHor(nWidth != nNewWidth);
const bool bScaleVer(nHeight != nNewHeight);
const bool bMirror(bMirrorHor || bMirrorVer);
if (!bMirror && !bScaleHor && !bScaleVer)
{
return true;
}
bool bResult(true);
BmpMirrorFlags nMirrorFlags(BmpMirrorFlags::NONE);
bool bMirrorAfter(false);
if (bMirror)
{
if(bMirrorHor)
{
nMirrorFlags |= BmpMirrorFlags::Horizontal;
}
if(bMirrorVer)
{
nMirrorFlags |= BmpMirrorFlags::Vertical;
}
const long nStartSize(nWidth * nHeight);
const long nEndSize(nNewWidth * nNewHeight);
bMirrorAfter = nStartSize > nEndSize;
if(!bMirrorAfter)
{
bResult = rBitmap.Mirror(nMirrorFlags);
}
}
Bitmap aResult;
if (bResult)
{
const long nInBetweenSizeHorFirst(nHeight * nNewWidth);
const long nInBetweenSizeVerFirst(nNewHeight * nWidth);
Bitmap aSource(rBitmap);
if(nInBetweenSizeHorFirst < nInBetweenSizeVerFirst)
{
if(bScaleHor)
{
bResult = ImplScaleConvolutionHor(aSource, aResult, fScaleX, aKernel);
}
if(bResult && bScaleVer)
{
if(bScaleHor)
{
// copy partial result, independent of color depth
aSource = aResult;
}
bResult = ImplScaleConvolutionVer(aSource, aResult, fScaleY, aKernel);
}
}
else
{
if(bScaleVer)
{
bResult = ImplScaleConvolutionVer(aSource, aResult, fScaleY, aKernel);
}
if(bResult && bScaleHor)
{
if(bScaleVer)
{
// copy partial result, independent of color depth
aSource = aResult;
}
bResult = ImplScaleConvolutionHor(aSource, aResult, fScaleX, aKernel);
}
}
}
if(bResult && bMirrorAfter)
{
bResult = aResult.Mirror(nMirrorFlags);
}
if(bResult)
{
rBitmap.AdaptBitCount(aResult);
rBitmap = aResult;
}
return bResult;
}
} // end anonymous namespace
BitmapEx BitmapScaleConvolutionFilter::execute(BitmapEx const& rBitmapEx)
{
bool bRetval = false;
Bitmap aBitmap(rBitmapEx.GetBitmap());
bRetval = ImplScaleConvolution(aBitmap, mrScaleX, mrScaleY, *mxKernel);
if (bRetval)
return BitmapEx(aBitmap);
return BitmapEx();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|