1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Downscaler is a high-quality, streaming image downscaler based upon Skia's
9 * scaling implementation.
12 #ifndef mozilla_image_Downscaler_h
13 #define mozilla_image_Downscaler_h
15 #include "mozilla/Maybe.h"
16 #include "mozilla/UniquePtr.h"
19 #ifdef MOZ_ENABLE_SKIA
20 # include "mozilla/gfx/ConvolutionFilter.h"
27 * DownscalerInvalidRect wraps two invalidation rects: one in terms of the
28 * original image size, and one in terms of the target size.
30 struct DownscalerInvalidRect
{
31 nsIntRect mOriginalSizeRect
;
32 nsIntRect mTargetSizeRect
;
35 #ifdef MOZ_ENABLE_SKIA
38 * Downscaler is a high-quality, streaming image downscaler based upon Skia's
39 * scaling implementation.
41 * Decoders can construct a Downscaler once they know their target size, then
42 * call BeginFrame() for each frame they decode. They should write a decoded row
43 * into the buffer returned by RowBuffer(), and then call CommitRow() to signal
44 * that they have finished.
47 * Because invalidations need to be computed in terms of the scaled version of
48 * the image, Downscaler also tracks them. Decoders can call HasInvalidation()
49 * and TakeInvalidRect() instead of tracking invalidations themselves.
53 /// Constructs a new Downscaler which to scale to size @aTargetSize.
54 explicit Downscaler(const nsIntSize
& aTargetSize
);
57 const nsIntSize
& OriginalSize() const { return mOriginalSize
; }
58 const nsIntSize
& TargetSize() const { return mTargetSize
; }
59 const nsIntSize
FrameSize() const {
60 return nsIntSize(mFrameRect
.Width(), mFrameRect
.Height());
62 const gfxSize
& Scale() const { return mScale
; }
65 * Begins a new frame and reinitializes the Downscaler.
67 * @param aOriginalSize The original size of this frame, before scaling.
68 * @param aFrameRect The region of the original image which has data.
69 * Every pixel outside @aFrameRect is considered blank and
71 * @param aOutputBuffer The buffer to which the Downscaler should write its
72 * output; this is the same buffer where the Decoder
73 * would write its output when not downscaling during
75 * @param aHasAlpha Whether or not this frame has an alpha channel.
76 * Performance is a little better if it doesn't have one.
77 * @param aFlipVertically If true, output rows will be written to the output
78 * buffer in reverse order vertically, which matches
79 * the way they are stored in some image formats.
81 nsresult
BeginFrame(const nsIntSize
& aOriginalSize
,
82 const Maybe
<nsIntRect
>& aFrameRect
,
83 uint8_t* aOutputBuffer
, bool aHasAlpha
,
84 bool aFlipVertically
= false);
86 bool IsFrameComplete() const {
87 return mCurrentInLine
>= mOriginalSize
.height
;
90 /// Retrieves the buffer into which the Decoder should write each row.
91 uint8_t* RowBuffer() {
92 return mRowBuffer
.get() + mFrameRect
.X() * sizeof(uint32_t);
95 /// Clears the current row buffer.
96 void ClearRow() { ClearRestOfRow(0); }
98 /// Clears the current row buffer starting at @aStartingAtCol.
99 void ClearRestOfRow(uint32_t aStartingAtCol
);
101 /// Signals that the decoder has finished writing a row into the row buffer.
104 /// Returns true if there is a non-empty invalid rect available.
105 bool HasInvalidation() const;
107 /// Takes the Downscaler's current invalid rect and resets it.
108 DownscalerInvalidRect
TakeInvalidRect();
111 * Resets the Downscaler's position in the image, for a new progressive pass
112 * over the same frame. Because the same data structures can be reused, this
113 * is more efficient than calling BeginFrame.
115 void ResetForNextProgressivePass();
118 void DownscaleInputLine();
119 void ReleaseWindow();
120 void SkipToRow(int32_t aRow
);
122 nsIntSize mOriginalSize
;
123 nsIntSize mTargetSize
;
124 nsIntRect mFrameRect
;
127 uint8_t* mOutputBuffer
;
129 UniquePtr
<uint8_t[]> mRowBuffer
;
130 UniquePtr
<uint8_t*[]> mWindow
;
132 gfx::ConvolutionFilter mXFilter
;
133 gfx::ConvolutionFilter mYFilter
;
135 int32_t mWindowCapacity
;
137 int32_t mLinesInBuffer
;
138 int32_t mPrevInvalidatedLine
;
139 int32_t mCurrentOutLine
;
140 int32_t mCurrentInLine
;
143 bool mFlipVertically
: 1;
149 * Downscaler requires Skia to work, so we provide a dummy implementation if
150 * Skia is disabled that asserts if constructed.
155 explicit Downscaler(const nsIntSize
&) : mScale(1.0, 1.0) {
156 MOZ_RELEASE_ASSERT(false, "Skia is not enabled");
159 const nsIntSize
& OriginalSize() const { return mSize
; }
160 const nsIntSize
& TargetSize() const { return mSize
; }
161 const gfxSize
& Scale() const { return mScale
; }
163 nsresult
BeginFrame(const nsIntSize
&, const Maybe
<nsIntRect
>&, uint8_t*, bool,
165 return NS_ERROR_FAILURE
;
168 bool IsFrameComplete() const { return false; }
169 uint8_t* RowBuffer() { return nullptr; }
171 void ClearRestOfRow(uint32_t) {}
173 bool HasInvalidation() const { return false; }
174 DownscalerInvalidRect
TakeInvalidRect() { return DownscalerInvalidRect(); }
175 void ResetForNextProgressivePass() {}
176 const nsIntSize
FrameSize() const { return nsIntSize(0, 0); }
183 #endif // MOZ_ENABLE_SKIA
186 } // namespace mozilla
188 #endif // mozilla_image_Downscaler_h