2013-04-19 14:13:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_BLUR_H_
|
|
|
|
#define MOZILLA_GFX_BLUR_H_
|
|
|
|
|
|
|
|
#include "mozilla/gfx/Rect.h"
|
|
|
|
#include "mozilla/gfx/Point.h"
|
2012-11-07 12:29:54 +04:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
2012-09-28 21:21:40 +04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning( disable : 4251 )
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
/**
|
|
|
|
* Implementation of a triple box blur approximation of a Gaussian blur.
|
|
|
|
*
|
|
|
|
* A Gaussian blur is good for blurring because, when done independently
|
|
|
|
* in the horizontal and vertical directions, it matches the result that
|
|
|
|
* would be obtained using a different (rotated) set of axes. A triple
|
|
|
|
* box blur is a very close approximation of a Gaussian.
|
|
|
|
*
|
2013-04-19 14:13:18 +04:00
|
|
|
* This is a "service" class; the constructors set up all the information
|
|
|
|
* based on the values and compute the minimum size for an 8-bit alpha
|
|
|
|
* channel context.
|
|
|
|
* The callers are responsible for creating and managing the backing surface
|
|
|
|
* and passing the pointer to the data to the Blur() method. This class does
|
|
|
|
* not retain the pointer to the data outside of the Blur() call.
|
2011-11-18 13:19:24 +04:00
|
|
|
*
|
|
|
|
* A spread N makes each output pixel the maximum value of all source
|
|
|
|
* pixels within a square of side length 2N+1 centered on the output pixel.
|
|
|
|
*/
|
2012-05-03 23:21:52 +04:00
|
|
|
class GFX2D_API AlphaBoxBlur
|
2011-11-18 13:19:24 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2013-04-19 14:13:18 +04:00
|
|
|
/** Constructs a box blur and computes the backing surface size.
|
2011-11-18 13:19:24 +04:00
|
|
|
*
|
|
|
|
* @param aRect The coordinates of the surface to create in device units.
|
|
|
|
*
|
|
|
|
* @param aBlurRadius The blur radius in pixels. This is the radius of the
|
|
|
|
* entire (triple) kernel function. Each individual box blur has radius
|
|
|
|
* approximately 1/3 this value, or diameter approximately 2/3 this value.
|
|
|
|
* This parameter should nearly always be computed using CalculateBlurRadius,
|
|
|
|
* below.
|
|
|
|
*
|
|
|
|
* @param aDirtyRect A pointer to a dirty rect, measured in device units, if
|
|
|
|
* available. This will be used for optimizing the blur operation. It is
|
2012-08-14 22:06:12 +04:00
|
|
|
* safe to pass nullptr here.
|
2011-11-18 13:19:24 +04:00
|
|
|
*
|
|
|
|
* @param aSkipRect A pointer to a rect, measured in device units, that
|
|
|
|
* represents an area where blurring is unnecessary and shouldn't be done for
|
2012-08-14 22:06:12 +04:00
|
|
|
* speed reasons. It is safe to pass nullptr here.
|
2011-11-18 13:19:24 +04:00
|
|
|
*/
|
|
|
|
AlphaBoxBlur(const Rect& aRect,
|
|
|
|
const IntSize& aSpreadRadius,
|
|
|
|
const IntSize& aBlurRadius,
|
|
|
|
const Rect* aDirtyRect,
|
|
|
|
const Rect* aSkipRect);
|
|
|
|
|
2013-04-19 14:13:18 +04:00
|
|
|
AlphaBoxBlur(const Rect& aRect,
|
2012-09-03 03:07:05 +04:00
|
|
|
int32_t aStride,
|
2013-11-27 15:22:36 +04:00
|
|
|
float aSigmaX,
|
|
|
|
float aSigmaY);
|
2012-09-03 03:07:05 +04:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
AlphaBoxBlur();
|
|
|
|
|
|
|
|
void Init(const Rect& aRect,
|
|
|
|
const IntSize& aSpreadRadius,
|
|
|
|
const IntSize& aBlurRadius,
|
|
|
|
const Rect* aDirtyRect,
|
|
|
|
const Rect* aSkipRect);
|
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
~AlphaBoxBlur();
|
|
|
|
|
|
|
|
/**
|
2013-04-19 14:13:18 +04:00
|
|
|
* Return the size, in pixels, of the 8-bit alpha surface we'd use.
|
2011-11-18 13:19:24 +04:00
|
|
|
*/
|
|
|
|
IntSize GetSize();
|
|
|
|
|
|
|
|
/**
|
2013-04-19 14:13:18 +04:00
|
|
|
* Return the stride, in bytes, of the 8-bit alpha surface we'd use.
|
2011-11-18 13:19:24 +04:00
|
|
|
*/
|
|
|
|
int32_t GetStride();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the device-space rectangle the 8-bit alpha surface covers.
|
|
|
|
*/
|
|
|
|
IntRect GetRect();
|
|
|
|
|
|
|
|
/**
|
2012-08-14 22:06:12 +04:00
|
|
|
* Return a pointer to a dirty rect, as passed in to the constructor, or nullptr
|
2011-11-18 13:19:24 +04:00
|
|
|
* if none was passed in.
|
|
|
|
*/
|
|
|
|
Rect* GetDirtyRect();
|
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
/**
|
|
|
|
* Return the spread radius, in pixels.
|
|
|
|
*/
|
|
|
|
IntSize GetSpreadRadius() const { return mSpreadRadius; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the blur radius, in pixels.
|
|
|
|
*/
|
|
|
|
IntSize GetBlurRadius() const { return mBlurRadius; }
|
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
/**
|
2013-04-19 14:13:18 +04:00
|
|
|
* Return the minimum buffer size that should be given to Blur() method. If
|
2014-09-17 11:46:42 +04:00
|
|
|
* zero, the class is not properly setup for blurring. Note that this
|
2013-04-19 14:13:18 +04:00
|
|
|
* includes the extra three bytes on top of the stride*width, where something
|
|
|
|
* like gfxImageSurface::GetDataSize() would report without it, even if it
|
|
|
|
* happens to have the extra bytes.
|
2011-11-18 13:19:24 +04:00
|
|
|
*/
|
2014-09-17 11:46:42 +04:00
|
|
|
size_t GetSurfaceAllocationSize() const;
|
2013-04-19 14:13:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the blur in-place on the surface backed by specified 8-bit
|
|
|
|
* alpha surface data. The size must be at least that returned by
|
|
|
|
* GetSurfaceAllocationSize() or bad things will happen.
|
|
|
|
*/
|
|
|
|
void Blur(uint8_t* aData);
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates a blur radius that, when used with box blur, approximates a
|
|
|
|
* Gaussian blur with the given standard deviation. The result of this
|
|
|
|
* function should be used as the aBlurRadius parameter to AlphaBoxBlur's
|
|
|
|
* constructor, above.
|
|
|
|
*/
|
|
|
|
static IntSize CalculateBlurRadius(const Point& aStandardDeviation);
|
2016-11-21 21:17:09 +03:00
|
|
|
static Float CalculateBlurSigma(int32_t aBlurRadius);
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-04-19 14:13:18 +04:00
|
|
|
void BoxBlur_C(uint8_t* aData,
|
|
|
|
int32_t aLeftLobe, int32_t aRightLobe, int32_t aTopLobe,
|
2012-11-07 12:29:54 +04:00
|
|
|
int32_t aBottomLobe, uint32_t *aIntegralImage, size_t aIntegralImageStride);
|
2013-04-19 14:13:18 +04:00
|
|
|
void BoxBlur_SSE2(uint8_t* aData,
|
|
|
|
int32_t aLeftLobe, int32_t aRightLobe, int32_t aTopLobe,
|
2012-11-07 12:29:54 +04:00
|
|
|
int32_t aBottomLobe, uint32_t *aIntegralImage, size_t aIntegralImageStride);
|
2015-01-19 00:08:00 +03:00
|
|
|
#ifdef BUILD_ARM_NEON
|
|
|
|
void BoxBlur_NEON(uint8_t* aData,
|
|
|
|
int32_t aLeftLobe, int32_t aRightLobe, int32_t aTopLobe,
|
2015-04-22 02:08:00 +03:00
|
|
|
int32_t aBottomLobe, uint32_t *aIntegralImage, size_t aIntegralImageStride);
|
|
|
|
#endif
|
|
|
|
#ifdef _MIPS_ARCH_LOONGSON3A
|
|
|
|
void BoxBlur_LS3(uint8_t* aData,
|
|
|
|
int32_t aLeftLobe, int32_t aRightLobe, int32_t aTopLobe,
|
2015-01-19 00:08:00 +03:00
|
|
|
int32_t aBottomLobe, uint32_t *aIntegralImage, size_t aIntegralImageStride);
|
|
|
|
#endif
|
2012-11-07 12:29:54 +04:00
|
|
|
|
|
|
|
static CheckedInt<int32_t> RoundUpToMultipleOf4(int32_t aVal);
|
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
/**
|
|
|
|
* A rect indicating the area where blurring is unnecessary, and the blur
|
|
|
|
* algorithm should skip over it.
|
|
|
|
*/
|
|
|
|
IntRect mSkipRect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The device-space rectangle the the backing 8-bit alpha surface covers.
|
|
|
|
*/
|
|
|
|
IntRect mRect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A copy of the dirty rect passed to the constructor. This will only be valid if
|
|
|
|
* mHasDirtyRect is true.
|
|
|
|
*/
|
|
|
|
Rect mDirtyRect;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The spread radius, in pixels.
|
|
|
|
*/
|
|
|
|
IntSize mSpreadRadius;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The blur radius, in pixels.
|
|
|
|
*/
|
|
|
|
IntSize mBlurRadius;
|
|
|
|
|
|
|
|
/**
|
2013-04-19 14:13:18 +04:00
|
|
|
* The stride of the data passed to Blur()
|
2012-09-03 03:07:05 +04:00
|
|
|
*/
|
2013-04-19 14:13:18 +04:00
|
|
|
int32_t mStride;
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
/**
|
2013-04-19 14:13:18 +04:00
|
|
|
* The minimum size of the buffer needed for the Blur() operation.
|
2011-11-18 13:19:24 +04:00
|
|
|
*/
|
2014-09-17 11:46:42 +04:00
|
|
|
size_t mSurfaceAllocationSize;
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether mDirtyRect contains valid data.
|
|
|
|
*/
|
|
|
|
bool mHasDirtyRect;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_BLUR_H_ */
|