gecko-dev/image/Downscaler.h

189 lines
5.8 KiB
C
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* 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/. */
/**
* Downscaler is a high-quality, streaming image downscaler based upon Skia's
* scaling implementation.
*/
Bug 1038536 - Flatten image/src/ directory. r=seth --HG-- rename : image/src/BMPFileHeaders.h => image/BMPFileHeaders.h rename : image/src/ClippedImage.cpp => image/ClippedImage.cpp rename : image/src/ClippedImage.h => image/ClippedImage.h rename : image/src/DecodePool.cpp => image/DecodePool.cpp rename : image/src/DecodePool.h => image/DecodePool.h rename : image/src/Decoder.cpp => image/Decoder.cpp rename : image/src/Decoder.h => image/Decoder.h rename : image/src/Downscaler.cpp => image/Downscaler.cpp rename : image/src/Downscaler.h => image/Downscaler.h rename : image/src/DynamicImage.cpp => image/DynamicImage.cpp rename : image/src/DynamicImage.h => image/DynamicImage.h rename : image/src/FrameAnimator.cpp => image/FrameAnimator.cpp rename : image/src/FrameAnimator.h => image/FrameAnimator.h rename : image/src/FrozenImage.cpp => image/FrozenImage.cpp rename : image/src/FrozenImage.h => image/FrozenImage.h rename : image/src/ICOFileHeaders.h => image/ICOFileHeaders.h rename : image/src/IProgressObserver.h => image/IProgressObserver.h rename : image/src/Image.cpp => image/Image.cpp rename : image/src/Image.h => image/Image.h rename : image/src/ImageFactory.cpp => image/ImageFactory.cpp rename : image/src/ImageFactory.h => image/ImageFactory.h rename : image/src/ImageMetadata.cpp => image/ImageMetadata.cpp rename : image/src/ImageMetadata.h => image/ImageMetadata.h rename : image/src/ImageOps.cpp => image/ImageOps.cpp rename : image/src/ImageOps.h => image/ImageOps.h rename : image/src/ImageRegion.h => image/ImageRegion.h rename : image/src/ImageURL.h => image/ImageURL.h rename : image/src/ImageWrapper.cpp => image/ImageWrapper.cpp rename : image/src/ImageWrapper.h => image/ImageWrapper.h rename : image/src/MultipartImage.cpp => image/MultipartImage.cpp rename : image/src/MultipartImage.h => image/MultipartImage.h rename : image/src/Orientation.h => image/Orientation.h rename : image/src/OrientedImage.cpp => image/OrientedImage.cpp rename : image/src/OrientedImage.h => image/OrientedImage.h rename : image/src/ProgressTracker.cpp => image/ProgressTracker.cpp rename : image/src/ProgressTracker.h => image/ProgressTracker.h rename : image/src/RasterImage.cpp => image/RasterImage.cpp rename : image/src/RasterImage.h => image/RasterImage.h rename : image/src/SVGDocumentWrapper.cpp => image/SVGDocumentWrapper.cpp rename : image/src/SVGDocumentWrapper.h => image/SVGDocumentWrapper.h rename : image/src/ScriptedNotificationObserver.cpp => image/ScriptedNotificationObserver.cpp rename : image/src/ScriptedNotificationObserver.h => image/ScriptedNotificationObserver.h rename : image/src/ShutdownTracker.cpp => image/ShutdownTracker.cpp rename : image/src/ShutdownTracker.h => image/ShutdownTracker.h rename : image/src/SourceBuffer.cpp => image/SourceBuffer.cpp rename : image/src/SourceBuffer.h => image/SourceBuffer.h rename : image/src/SurfaceCache.cpp => image/SurfaceCache.cpp rename : image/src/SurfaceCache.h => image/SurfaceCache.h rename : image/src/VectorImage.cpp => image/VectorImage.cpp rename : image/src/VectorImage.h => image/VectorImage.h rename : image/src/imgFrame.cpp => image/imgFrame.cpp rename : image/src/imgFrame.h => image/imgFrame.h rename : image/src/imgLoader.cpp => image/imgLoader.cpp rename : image/src/imgLoader.h => image/imgLoader.h rename : image/src/imgRequest.cpp => image/imgRequest.cpp rename : image/src/imgRequest.h => image/imgRequest.h rename : image/src/imgRequestProxy.cpp => image/imgRequestProxy.cpp rename : image/src/imgRequestProxy.h => image/imgRequestProxy.h rename : image/src/imgTools.cpp => image/imgTools.cpp rename : image/src/imgTools.h => image/imgTools.h
2015-05-15 03:52:05 +00:00
#ifndef mozilla_image_Downscaler_h
#define mozilla_image_Downscaler_h
#include "mozilla/Maybe.h"
#include "mozilla/UniquePtr.h"
#include "nsRect.h"
namespace skia {
class ConvolutionFilter1D;
} // namespace skia
namespace mozilla {
namespace image {
/**
* DownscalerInvalidRect wraps two invalidation rects: one in terms of the
* original image size, and one in terms of the target size.
*/
struct DownscalerInvalidRect
{
nsIntRect mOriginalSizeRect;
nsIntRect mTargetSizeRect;
};
#ifdef MOZ_ENABLE_SKIA
/**
* Downscaler is a high-quality, streaming image downscaler based upon Skia's
* scaling implementation.
*
* Decoders can construct a Downscaler once they know their target size, then
* call BeginFrame() for each frame they decode. They should write a decoded row
* into the buffer returned by RowBuffer(), and then call CommitRow() to signal
* that they have finished.
*
* Because invalidations need to be computed in terms of the scaled version of
* the image, Downscaler also tracks them. Decoders can call HasInvalidation()
* and TakeInvalidRect() instead of tracking invalidations themselves.
*/
class Downscaler
{
public:
/// Constructs a new Downscaler which to scale to size @aTargetSize.
explicit Downscaler(const nsIntSize& aTargetSize);
~Downscaler();
const nsIntSize& OriginalSize() const { return mOriginalSize; }
const nsIntSize& TargetSize() const { return mTargetSize; }
const nsIntSize FrameSize() const { return nsIntSize(mFrameRect.width, mFrameRect.height); }
const gfxSize& Scale() const { return mScale; }
/**
* Begins a new frame and reinitializes the Downscaler.
*
* @param aOriginalSize The original size of this frame, before scaling.
* @param aFrameRect The region of the original image which has data.
* Every pixel outside @aFrameRect is considered blank and
* has zero alpha.
* @param aOutputBuffer The buffer to which the Downscaler should write its
* output; this is the same buffer where the Decoder
* would write its output when not downscaling during
* decode.
* @param aHasAlpha Whether or not this frame has an alpha channel.
* Performance is a little better if it doesn't have one.
* @param aFlipVertically If true, output rows will be written to the output
* buffer in reverse order vertically, which matches
* the way they are stored in some image formats.
*/
nsresult BeginFrame(const nsIntSize& aOriginalSize,
const Maybe<nsIntRect>& aFrameRect,
uint8_t* aOutputBuffer,
bool aHasAlpha,
bool aFlipVertically = false);
bool IsFrameComplete() const { return mCurrentInLine >= mOriginalSize.height; }
/// Retrieves the buffer into which the Decoder should write each row.
uint8_t* RowBuffer()
{
return mRowBuffer.get() + mFrameRect.x * sizeof(uint32_t);
}
/// Clears the current row buffer.
void ClearRow() { ClearRestOfRow(0); }
/// Clears the current row buffer starting at @aStartingAtCol.
void ClearRestOfRow(uint32_t aStartingAtCol);
/// Signals that the decoder has finished writing a row into the row buffer.
void CommitRow();
/// Returns true if there is a non-empty invalid rect available.
bool HasInvalidation() const;
/// Takes the Downscaler's current invalid rect and resets it.
DownscalerInvalidRect TakeInvalidRect();
/**
* Resets the Downscaler's position in the image, for a new progressive pass
* over the same frame. Because the same data structures can be reused, this
* is more efficient than calling BeginFrame.
*/
void ResetForNextProgressivePass();
private:
void DownscaleInputLine();
void ReleaseWindow();
void SkipToRow(int32_t aRow);
nsIntSize mOriginalSize;
nsIntSize mTargetSize;
nsIntRect mFrameRect;
gfxSize mScale;
uint8_t* mOutputBuffer;
UniquePtr<uint8_t[]> mRowBuffer;
UniquePtr<uint8_t*[]> mWindow;
UniquePtr<skia::ConvolutionFilter1D> mXFilter;
UniquePtr<skia::ConvolutionFilter1D> mYFilter;
int32_t mWindowCapacity;
int32_t mLinesInBuffer;
int32_t mPrevInvalidatedLine;
int32_t mCurrentOutLine;
int32_t mCurrentInLine;
bool mHasAlpha : 1;
bool mFlipVertically : 1;
};
#else
/**
* Downscaler requires Skia to work, so we provide a dummy implementation if
* Skia is disabled that asserts if constructed.
*/
class Downscaler
{
public:
explicit Downscaler(const nsIntSize&)
{
MOZ_RELEASE_ASSERT(false, "Skia is not enabled");
}
const nsIntSize& OriginalSize() const { return nsIntSize(); }
const nsIntSize& TargetSize() const { return nsIntSize(); }
const gfxSize& Scale() const { return gfxSize(1.0, 1.0); }
nsresult BeginFrame(const nsIntSize&, const Maybe<nsIntRect>&, uint8_t*, bool, bool = false)
{
return NS_ERROR_FAILURE;
}
bool IsFrameComplete() const { return false; }
uint8_t* RowBuffer() { return nullptr; }
void ClearRow() { }
void ClearRestOfRow(uint32_t) { }
void CommitRow() { }
bool HasInvalidation() const { return false; }
DownscalerInvalidRect TakeInvalidRect() { return DownscalerInvalidRect(); }
void ResetForNextProgressivePass() { }
const nsIntSize FrameSize() const { return nsIntSize(0, 0); }
};
#endif // MOZ_ENABLE_SKIA
} // namespace image
} // namespace mozilla
Bug 1038536 - Flatten image/src/ directory. r=seth --HG-- rename : image/src/BMPFileHeaders.h => image/BMPFileHeaders.h rename : image/src/ClippedImage.cpp => image/ClippedImage.cpp rename : image/src/ClippedImage.h => image/ClippedImage.h rename : image/src/DecodePool.cpp => image/DecodePool.cpp rename : image/src/DecodePool.h => image/DecodePool.h rename : image/src/Decoder.cpp => image/Decoder.cpp rename : image/src/Decoder.h => image/Decoder.h rename : image/src/Downscaler.cpp => image/Downscaler.cpp rename : image/src/Downscaler.h => image/Downscaler.h rename : image/src/DynamicImage.cpp => image/DynamicImage.cpp rename : image/src/DynamicImage.h => image/DynamicImage.h rename : image/src/FrameAnimator.cpp => image/FrameAnimator.cpp rename : image/src/FrameAnimator.h => image/FrameAnimator.h rename : image/src/FrozenImage.cpp => image/FrozenImage.cpp rename : image/src/FrozenImage.h => image/FrozenImage.h rename : image/src/ICOFileHeaders.h => image/ICOFileHeaders.h rename : image/src/IProgressObserver.h => image/IProgressObserver.h rename : image/src/Image.cpp => image/Image.cpp rename : image/src/Image.h => image/Image.h rename : image/src/ImageFactory.cpp => image/ImageFactory.cpp rename : image/src/ImageFactory.h => image/ImageFactory.h rename : image/src/ImageMetadata.cpp => image/ImageMetadata.cpp rename : image/src/ImageMetadata.h => image/ImageMetadata.h rename : image/src/ImageOps.cpp => image/ImageOps.cpp rename : image/src/ImageOps.h => image/ImageOps.h rename : image/src/ImageRegion.h => image/ImageRegion.h rename : image/src/ImageURL.h => image/ImageURL.h rename : image/src/ImageWrapper.cpp => image/ImageWrapper.cpp rename : image/src/ImageWrapper.h => image/ImageWrapper.h rename : image/src/MultipartImage.cpp => image/MultipartImage.cpp rename : image/src/MultipartImage.h => image/MultipartImage.h rename : image/src/Orientation.h => image/Orientation.h rename : image/src/OrientedImage.cpp => image/OrientedImage.cpp rename : image/src/OrientedImage.h => image/OrientedImage.h rename : image/src/ProgressTracker.cpp => image/ProgressTracker.cpp rename : image/src/ProgressTracker.h => image/ProgressTracker.h rename : image/src/RasterImage.cpp => image/RasterImage.cpp rename : image/src/RasterImage.h => image/RasterImage.h rename : image/src/SVGDocumentWrapper.cpp => image/SVGDocumentWrapper.cpp rename : image/src/SVGDocumentWrapper.h => image/SVGDocumentWrapper.h rename : image/src/ScriptedNotificationObserver.cpp => image/ScriptedNotificationObserver.cpp rename : image/src/ScriptedNotificationObserver.h => image/ScriptedNotificationObserver.h rename : image/src/ShutdownTracker.cpp => image/ShutdownTracker.cpp rename : image/src/ShutdownTracker.h => image/ShutdownTracker.h rename : image/src/SourceBuffer.cpp => image/SourceBuffer.cpp rename : image/src/SourceBuffer.h => image/SourceBuffer.h rename : image/src/SurfaceCache.cpp => image/SurfaceCache.cpp rename : image/src/SurfaceCache.h => image/SurfaceCache.h rename : image/src/VectorImage.cpp => image/VectorImage.cpp rename : image/src/VectorImage.h => image/VectorImage.h rename : image/src/imgFrame.cpp => image/imgFrame.cpp rename : image/src/imgFrame.h => image/imgFrame.h rename : image/src/imgLoader.cpp => image/imgLoader.cpp rename : image/src/imgLoader.h => image/imgLoader.h rename : image/src/imgRequest.cpp => image/imgRequest.cpp rename : image/src/imgRequest.h => image/imgRequest.h rename : image/src/imgRequestProxy.cpp => image/imgRequestProxy.cpp rename : image/src/imgRequestProxy.h => image/imgRequestProxy.h rename : image/src/imgTools.cpp => image/imgTools.cpp rename : image/src/imgTools.h => image/imgTools.h
2015-05-15 03:52:05 +00:00
#endif // mozilla_image_Downscaler_h