aboutsummaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorGravatar scroggo <scroggo@chromium.org>2015-08-04 09:24:45 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-08-04 09:24:45 -0700
commit1c005e4a38e29d648ecebada25d3a718155043a3 (patch)
treeccc10aa351e4c58f9ffcd53b849d9a5afc46c28b /include
parentddc726f1de09426557983361a0e7838a83612315 (diff)
Create a scanline decoder without creating a codec
Prior to this CL, if a client wanted to decode scanlines, they had to create an SkCodec in order to get an SkScanlineDecoder. This introduces complications if input data is not easily shared between the two objects. Instead, add methods to SkScanlineDecoder for creating a new one from input data, and remove the creation functions from SkCodec. Update DM and tests. Review URL: https://codereview.chromium.org/1267583002
Diffstat (limited to 'include')
-rw-r--r--include/codec/SkCodec.h56
-rw-r--r--include/codec/SkScanlineDecoder.h72
2 files changed, 69 insertions, 59 deletions
diff --git a/include/codec/SkCodec.h b/include/codec/SkCodec.h
index 1cdc88d4ad..ce581632e1 100644
--- a/include/codec/SkCodec.h
+++ b/include/codec/SkCodec.h
@@ -17,7 +17,6 @@
#include "SkTypes.h"
class SkData;
-class SkScanlineDecoder;
/**
* Abstraction layer directly on top of an image codec.
@@ -202,37 +201,6 @@ public:
Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
/**
- * Create a new object which can be used to decode individual scanlines.
- *
- * The returned object has its own state, independent of the SkCodec, or any
- * previously spawned SkScanlineDecoders. At creation, it will be ready to
- * return the first scanline.
- *
- * @param dstInfo Info of the destination. If the dimensions do not match
- * those of getInfo, this implies a scale.
- * @param options Contains decoding options, including if memory is zero
- * initialized.
- * @param ctable A pointer to a color table. When dstInfo.colorType() is
- * kIndex8, this should be non-NULL and have enough storage for 256
- * colors. The color table will be populated after decoding the palette.
- * @param ctableCount A pointer to the size of the color table. When
- * dstInfo.colorType() is kIndex8, this should be non-NULL. It will
- * be modified to the true size of the color table (<= 256) after
- * decoding the palette.
- * @return New SkScanlineDecoder, or NULL on failure.
- *
- * NOTE: This requires duplicating the SkStream.
- */
- SkScanlineDecoder* getScanlineDecoder(const SkImageInfo& dstInfo, const Options* options,
- SkPMColor ctable[], int* ctableCount);
-
- /**
- * Simplified version of getScanlineDecoder() that asserts that info is NOT
- * kIndex8_SkColorType and uses the default Options.
- */
- SkScanlineDecoder* getScanlineDecoder(const SkImageInfo& dstInfo);
-
- /**
* Some images may initially report that they have alpha due to the format
* of the encoded data, but then never use any colors which have alpha
* less than 100%. This function can be called *after* decoding to
@@ -263,30 +231,6 @@ protected:
return false;
}
- /**
- * Override if your codec supports scanline decoding.
- *
- * @param dstInfo Info of the destination. If the dimensions do not match
- * those of getInfo, this implies a scale.
- * @param options Contains decoding options, including if memory is zero
- * initialized.
- * @param ctable A pointer to a color table. When dstInfo.colorType() is
- * kIndex8, this should be non-NULL and have enough storage for 256
- * colors. The color table will be populated after decoding the palette.
- * @param ctableCount A pointer to the size of the color table. When
- * dstInfo.colorType() is kIndex8, this should be non-NULL. It will
- * be modified to the true size of the color table (<= 256) after
- * decoding the palette.
- * @return New SkScanlineDecoder on success, NULL otherwise. The caller is
- * responsible for deleting the returned object.
- */
- virtual SkScanlineDecoder* onGetScanlineDecoder(const SkImageInfo& dstInfo,
- const Options& options,
- SkPMColor ctable[],
- int* ctableCount) {
- return NULL;
- }
-
virtual bool onReallyHasAlpha() const { return false; }
enum RewindState {
diff --git a/include/codec/SkScanlineDecoder.h b/include/codec/SkScanlineDecoder.h
index 8376e57c09..c233663dbd 100644
--- a/include/codec/SkScanlineDecoder.h
+++ b/include/codec/SkScanlineDecoder.h
@@ -16,6 +16,27 @@
class SkScanlineDecoder : public SkNoncopyable {
public:
/**
+ * If this stream represents an encoded image that we know how to decode
+ * in scanlines, return an SkScanlineDecoder that can decode it. Otherwise
+ * return NULL.
+ *
+ * start() must be called in order to decode any scanlines.
+ *
+ * If NULL is returned, the stream is deleted immediately. Otherwise, the
+ * SkScanlineDecoder takes ownership of it, and will delete it when done
+ * with it.
+ */
+ static SkScanlineDecoder* NewFromStream(SkStream*);
+
+ /**
+ * Similar to NewFromStream, but reads from an SkData.
+ *
+ * Will take a ref if it returns a scanline decoder, else will not affect
+ * the data.
+ */
+ static SkScanlineDecoder* NewFromData(SkData*);
+
+ /**
* Clean up after reading/skipping scanlines.
*
* It is possible that not all scanlines will have been read/skipped. In
@@ -25,8 +46,43 @@ public:
virtual ~SkScanlineDecoder() {}
/**
+ * Returns the default info, corresponding to the encoded data.
+ */
+ const SkImageInfo& getInfo() { return fSrcInfo; }
+
+ /**
+ * Initialize on the first scanline, with the specified options.
+ *
+ * This must be called in order to call getScanlnies or skipScanlines. If
+ * it has been called before, this will require rewinding the stream.
+ *
+ * @param dstInfo Info of the destination. If the dimensions do not match
+ * those of getInfo, this implies a scale.
+ * @param options Contains decoding options, including if memory is zero
+ * initialized.
+ * @param ctable A pointer to a color table. When dstInfo.colorType() is
+ * kIndex8, this should be non-NULL and have enough storage for 256
+ * colors. The color table will be populated after decoding the palette.
+ * @param ctableCount A pointer to the size of the color table. When
+ * dstInfo.colorType() is kIndex8, this should be non-NULL. It will
+ * be modified to the true size of the color table (<= 256) after
+ * decoding the palette.
+ * @return Enum representing success or reason for failure.
+ */
+ SkCodec::Result start(const SkImageInfo& dstInfo, const SkCodec::Options* options,
+ SkPMColor ctable[], int* ctableCount);
+
+ /**
+ * Simplified version of start() that asserts that info is NOT
+ * kIndex8_SkColorType and uses the default Options.
+ */
+ SkCodec::Result start(const SkImageInfo& dstInfo);
+
+ /**
* Write the next countLines scanlines into dst.
*
+ * Not valid to call before calling start().
+ *
* @param dst Must be non-null, and large enough to hold countLines
* scanlines of size rowBytes.
* @param countLines Number of lines to write.
@@ -34,6 +90,7 @@ public:
* a scanline based on the SkImageInfo used to create this object.
*/
SkCodec::Result getScanlines(void* dst, int countLines, size_t rowBytes) {
+ SkASSERT(!fDstInfo.isEmpty());
if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines <= 0
|| fCurrScanline + countLines > fDstInfo.height()) {
return SkCodec::kInvalidParameters;
@@ -46,12 +103,15 @@ public:
/**
* Skip count scanlines.
*
+ * Not valid to call before calling start().
+ *
* The default version just calls onGetScanlines and discards the dst.
* NOTE: If skipped lines are the only lines with alpha, this default
* will make reallyHasAlpha return true, when it could have returned
* false.
*/
SkCodec::Result skipScanlines(int countLines) {
+ SkASSERT(!fDstInfo.isEmpty());
if (fCurrScanline + countLines > fDstInfo.height()) {
// Arguably, we could just skip the scanlines which are remaining,
// and return kSuccess. We choose to return invalid so the client
@@ -76,8 +136,9 @@ public:
}
protected:
- SkScanlineDecoder(const SkImageInfo& requested)
- : fDstInfo(requested)
+ SkScanlineDecoder(const SkImageInfo& srcInfo)
+ : fSrcInfo(srcInfo)
+ , fDstInfo()
, fCurrScanline(0) {}
virtual bool onReallyHasAlpha() const { return false; }
@@ -85,9 +146,14 @@ protected:
const SkImageInfo& dstInfo() const { return fDstInfo; }
private:
- const SkImageInfo fDstInfo;
+ const SkImageInfo fSrcInfo;
+ SkImageInfo fDstInfo;
int fCurrScanline;
+ virtual SkCodec::Result onStart(const SkImageInfo& dstInfo,
+ const SkCodec::Options& options,
+ SkPMColor ctable[], int* ctableCount) = 0;
+
// Naive default version just calls onGetScanlines on temp memory.
virtual SkCodec::Result onSkipScanlines(int countLines) {
SkAutoMalloc storage(fDstInfo.minRowBytes());