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 #include "mozilla/gfx/ConvolutionFilter.h"
25 * DownscalerInvalidRect wraps two invalidation rects: one in terms of the
26 * original image size, and one in terms of the target size.
28 struct DownscalerInvalidRect
{
29 nsIntRect mOriginalSizeRect
;
30 nsIntRect mTargetSizeRect
;
34 * Downscaler is a high-quality, streaming image downscaler based upon Skia's
35 * scaling implementation.
37 * Decoders can construct a Downscaler once they know their target size, then
38 * call BeginFrame() for each frame they decode. They should write a decoded row
39 * into the buffer returned by RowBuffer(), and then call CommitRow() to signal
40 * that they have finished.
43 * Because invalidations need to be computed in terms of the scaled version of
44 * the image, Downscaler also tracks them. Decoders can call HasInvalidation()
45 * and TakeInvalidRect() instead of tracking invalidations themselves.
49 /// Constructs a new Downscaler which to scale to size @aTargetSize.
50 explicit Downscaler(const nsIntSize
& aTargetSize
);
53 const nsIntSize
& OriginalSize() const { return mOriginalSize
; }
54 const nsIntSize
& TargetSize() const { return mTargetSize
; }
55 const nsIntSize
FrameSize() const {
56 return nsIntSize(mFrameRect
.Width(), mFrameRect
.Height());
58 const gfxSize
& Scale() const { return mScale
; }
61 * Begins a new frame and reinitializes the Downscaler.
63 * @param aOriginalSize The original size of this frame, before scaling.
64 * @param aFrameRect The region of the original image which has data.
65 * Every pixel outside @aFrameRect is considered blank and
67 * @param aOutputBuffer The buffer to which the Downscaler should write its
68 * output; this is the same buffer where the Decoder
69 * would write its output when not downscaling during
71 * @param aHasAlpha Whether or not this frame has an alpha channel.
72 * Performance is a little better if it doesn't have one.
73 * @param aFlipVertically If true, output rows will be written to the output
74 * buffer in reverse order vertically, which matches
75 * the way they are stored in some image formats.
77 nsresult
BeginFrame(const nsIntSize
& aOriginalSize
,
78 const Maybe
<nsIntRect
>& aFrameRect
,
79 uint8_t* aOutputBuffer
, bool aHasAlpha
,
80 bool aFlipVertically
= false);
82 bool IsFrameComplete() const {
83 return mCurrentInLine
>= mOriginalSize
.height
;
86 /// Retrieves the buffer into which the Decoder should write each row.
87 uint8_t* RowBuffer() {
88 return mRowBuffer
.get() + mFrameRect
.X() * sizeof(uint32_t);
91 /// Clears the current row buffer.
92 void ClearRow() { ClearRestOfRow(0); }
94 /// Clears the current row buffer starting at @aStartingAtCol.
95 void ClearRestOfRow(uint32_t aStartingAtCol
);
97 /// Signals that the decoder has finished writing a row into the row buffer.
100 /// Returns true if there is a non-empty invalid rect available.
101 bool HasInvalidation() const;
103 /// Takes the Downscaler's current invalid rect and resets it.
104 DownscalerInvalidRect
TakeInvalidRect();
107 * Resets the Downscaler's position in the image, for a new progressive pass
108 * over the same frame. Because the same data structures can be reused, this
109 * is more efficient than calling BeginFrame.
111 void ResetForNextProgressivePass();
114 void DownscaleInputLine();
115 void ReleaseWindow();
116 void SkipToRow(int32_t aRow
);
118 nsIntSize mOriginalSize
;
119 nsIntSize mTargetSize
;
120 nsIntRect mFrameRect
;
123 uint8_t* mOutputBuffer
;
125 UniquePtr
<uint8_t[]> mRowBuffer
;
126 UniquePtr
<uint8_t*[]> mWindow
;
128 gfx::ConvolutionFilter mXFilter
;
129 gfx::ConvolutionFilter mYFilter
;
131 int32_t mWindowCapacity
;
133 int32_t mLinesInBuffer
;
134 int32_t mPrevInvalidatedLine
;
135 int32_t mCurrentOutLine
;
136 int32_t mCurrentInLine
;
139 bool mFlipVertically
: 1;
143 } // namespace mozilla
145 #endif // mozilla_image_Downscaler_h