2015-10-21 17:27:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkAndroidCodec_DEFINED
|
|
|
|
#define SkAndroidCodec_DEFINED
|
|
|
|
|
|
|
|
#include "SkCodec.h"
|
2016-11-30 22:07:59 +00:00
|
|
|
#include "SkEncodedImageFormat.h"
|
2015-10-21 17:27:10 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract interface defining image codec functionality that is necessary for
|
|
|
|
* Android.
|
|
|
|
*/
|
|
|
|
class SkAndroidCodec : SkNoncopyable {
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* If this stream represents an encoded image that we know how to decode,
|
|
|
|
* return an SkAndroidCodec that can decode it. Otherwise return NULL.
|
|
|
|
*
|
2015-12-02 15:02:41 +00:00
|
|
|
* The SkPngChunkReader handles unknown chunks in PNGs.
|
|
|
|
* See SkCodec.h for more details.
|
|
|
|
*
|
2015-10-21 17:27:10 +00:00
|
|
|
* If NULL is returned, the stream is deleted immediately. Otherwise, the
|
|
|
|
* SkCodec takes ownership of it, and will delete it when done with it.
|
|
|
|
*/
|
2015-12-02 15:02:41 +00:00
|
|
|
static SkAndroidCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL);
|
2015-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If this data represents an encoded image that we know how to decode,
|
|
|
|
* return an SkAndroidCodec that can decode it. Otherwise return NULL.
|
|
|
|
*
|
2015-12-02 15:02:41 +00:00
|
|
|
* The SkPngChunkReader handles unknown chunks in PNGs.
|
|
|
|
* See SkCodec.h for more details.
|
2015-10-21 17:27:10 +00:00
|
|
|
*/
|
2016-09-12 19:01:44 +00:00
|
|
|
static SkAndroidCodec* NewFromData(sk_sp<SkData>, SkPngChunkReader* = NULL);
|
|
|
|
static SkAndroidCodec* NewFromData(SkData* data, SkPngChunkReader* reader) {
|
|
|
|
return NewFromData(sk_ref_sp(data), reader);
|
|
|
|
}
|
2015-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
virtual ~SkAndroidCodec() {}
|
|
|
|
|
|
|
|
|
|
|
|
const SkImageInfo& getInfo() const { return fInfo; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format of the encoded data.
|
|
|
|
*/
|
2016-11-30 22:07:59 +00:00
|
|
|
SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
|
2015-10-21 17:27:10 +00:00
|
|
|
|
2015-12-11 15:38:50 +00:00
|
|
|
/**
|
|
|
|
* @param requestedColorType Color type requested by the client
|
|
|
|
*
|
2016-12-14 15:23:41 +00:00
|
|
|
* |requestedColorType| may be overriden. We will default to kF16
|
2017-07-11 17:35:31 +00:00
|
|
|
* for high precision images.
|
2016-12-14 15:23:41 +00:00
|
|
|
*
|
|
|
|
* In the general case, if it is possible to decode to
|
|
|
|
* |requestedColorType|, this returns |requestedColorType|.
|
|
|
|
* Otherwise, this returns a color type that is an appropriate
|
|
|
|
* match for the the encoded data.
|
2015-12-11 15:38:50 +00:00
|
|
|
*/
|
|
|
|
SkColorType computeOutputColorType(SkColorType requestedColorType);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param requestedUnpremul Indicates if the client requested
|
|
|
|
* unpremultiplied output
|
|
|
|
*
|
|
|
|
* Returns the appropriate alpha type to decode to. If the image
|
|
|
|
* has alpha, the value of requestedUnpremul will be honored.
|
|
|
|
*/
|
|
|
|
SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
|
|
|
|
|
2016-12-12 21:30:13 +00:00
|
|
|
/**
|
2017-04-11 13:51:32 +00:00
|
|
|
* @param outputColorType Color type that the client will decode to.
|
|
|
|
* @param prefColorSpace Preferred color space to decode to.
|
|
|
|
* This may not return |prefColorSpace| for a couple reasons.
|
|
|
|
* (1) Android Principles: 565 must be sRGB, F16 must be
|
|
|
|
* linear sRGB, transfer function must be parametric.
|
|
|
|
* (2) Codec Limitations: F16 requires a linear color space.
|
2016-12-12 21:30:13 +00:00
|
|
|
*
|
|
|
|
* Returns the appropriate color space to decode to.
|
|
|
|
*/
|
2017-04-11 13:51:32 +00:00
|
|
|
sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
|
|
|
|
sk_sp<SkColorSpace> prefColorSpace = nullptr);
|
2016-12-12 21:30:13 +00:00
|
|
|
|
2015-10-21 17:27:10 +00:00
|
|
|
/**
|
|
|
|
* Returns the dimensions of the scaled output image, for an input
|
|
|
|
* sampleSize.
|
|
|
|
*
|
|
|
|
* When the sample size divides evenly into the original dimensions, the
|
|
|
|
* scaled output dimensions will simply be equal to the original
|
|
|
|
* dimensions divided by the sample size.
|
|
|
|
*
|
|
|
|
* When the sample size does not divide even into the original
|
|
|
|
* dimensions, the codec may round up or down, depending on what is most
|
|
|
|
* efficient to decode.
|
|
|
|
*
|
|
|
|
* Finally, the codec will always recommend a non-zero output, so the output
|
|
|
|
* dimension will always be one if the sampleSize is greater than the
|
|
|
|
* original dimension.
|
|
|
|
*/
|
|
|
|
SkISize getSampledDimensions(int sampleSize) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return (via desiredSubset) a subset which can decoded from this codec,
|
|
|
|
* or false if the input subset is invalid.
|
|
|
|
*
|
|
|
|
* @param desiredSubset in/out parameter
|
|
|
|
* As input, a desired subset of the original bounds
|
|
|
|
* (as specified by getInfo).
|
|
|
|
* As output, if true is returned, desiredSubset may
|
|
|
|
* have been modified to a subset which is
|
|
|
|
* supported. Although a particular change may have
|
|
|
|
* been made to desiredSubset to create something
|
|
|
|
* supported, it is possible other changes could
|
|
|
|
* result in a valid subset. If false is returned,
|
|
|
|
* desiredSubset's value is undefined.
|
|
|
|
* @return true If the input desiredSubset is valid.
|
|
|
|
* desiredSubset may be modified to a subset
|
|
|
|
* supported by the codec.
|
|
|
|
* false If desiredSubset is invalid (NULL or not fully
|
|
|
|
* contained within the image).
|
|
|
|
*/
|
|
|
|
bool getSupportedSubset(SkIRect* desiredSubset) const;
|
|
|
|
// TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the dimensions of the scaled, partial output image, for an
|
|
|
|
* input sampleSize and subset.
|
|
|
|
*
|
|
|
|
* @param sampleSize Factor to scale down by.
|
|
|
|
* @param subset Must be a valid subset of the original image
|
|
|
|
* dimensions and a subset supported by SkAndroidCodec.
|
|
|
|
* getSubset() can be used to obtain a subset supported
|
|
|
|
* by SkAndroidCodec.
|
|
|
|
* @return Size of the scaled partial image. Or zero size
|
|
|
|
* if either of the inputs is invalid.
|
|
|
|
*/
|
|
|
|
SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional options to pass to getAndroidPixels().
|
|
|
|
*/
|
|
|
|
// FIXME: It's a bit redundant to name these AndroidOptions when this class is already
|
|
|
|
// called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
|
|
|
|
// these Options when SkCodec has a slightly different set of Options. Maybe these
|
|
|
|
// should be DecodeOptions or SamplingOptions?
|
|
|
|
struct AndroidOptions {
|
|
|
|
AndroidOptions()
|
|
|
|
: fZeroInitialized(SkCodec::kNo_ZeroInitialized)
|
|
|
|
, fSubset(nullptr)
|
|
|
|
, fSampleSize(1)
|
|
|
|
{}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates is destination pixel memory is zero initialized.
|
2016-02-04 14:14:24 +00:00
|
|
|
*
|
|
|
|
* The default is SkCodec::kNo_ZeroInitialized.
|
2015-10-21 17:27:10 +00:00
|
|
|
*/
|
|
|
|
SkCodec::ZeroInitialized fZeroInitialized;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If not NULL, represents a subset of the original image to decode.
|
|
|
|
*
|
|
|
|
* Must be within the bounds returned by getInfo().
|
|
|
|
*
|
2016-11-23 15:55:18 +00:00
|
|
|
* If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
|
2015-10-21 17:27:10 +00:00
|
|
|
* values must be even.
|
2016-02-04 14:14:24 +00:00
|
|
|
*
|
|
|
|
* The default is NULL, meaning a decode of the entire image.
|
2015-10-21 17:27:10 +00:00
|
|
|
*/
|
|
|
|
SkIRect* fSubset;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The client may provide an integer downscale factor for the decode.
|
|
|
|
* The codec may implement this downscaling by sampling or another
|
|
|
|
* method if it is more efficient.
|
2016-02-04 14:14:24 +00:00
|
|
|
*
|
|
|
|
* The default is 1, representing no downscaling.
|
2015-10-21 17:27:10 +00:00
|
|
|
*/
|
|
|
|
int fSampleSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode into the given pixels, a block of memory of size at
|
|
|
|
* least (info.fHeight - 1) * rowBytes + (info.fWidth *
|
|
|
|
* bytesPerPixel)
|
|
|
|
*
|
|
|
|
* Repeated calls to this function should give the same results,
|
|
|
|
* allowing the PixelRef to be immutable.
|
|
|
|
*
|
|
|
|
* @param info A description of the format (config, size)
|
|
|
|
* expected by the caller. This can simply be identical
|
|
|
|
* to the info returned by getInfo().
|
|
|
|
*
|
|
|
|
* This contract also allows the caller to specify
|
|
|
|
* different output-configs, which the implementation can
|
|
|
|
* decide to support or not.
|
|
|
|
*
|
|
|
|
* A size that does not match getInfo() implies a request
|
|
|
|
* to scale or subset. If the codec cannot perform this
|
|
|
|
* scaling or subsetting, it will return an error code.
|
|
|
|
*
|
|
|
|
* The AndroidOptions object is also used to specify any requested scaling or subsetting
|
2016-02-04 14:14:24 +00:00
|
|
|
* using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
|
|
|
|
* for AndroidOptions) are used.
|
2015-10-21 17:27:10 +00:00
|
|
|
*
|
|
|
|
* @return Result kSuccess, or another value explaining the type of failure.
|
|
|
|
*/
|
|
|
|
// FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
|
|
|
|
// called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
|
|
|
|
// this getPixels() when it is a slightly different API than SkCodec's getPixels().
|
|
|
|
// Maybe this should be decode() or decodeSubset()?
|
|
|
|
SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
|
2015-11-04 12:31:12 +00:00
|
|
|
const AndroidOptions* options);
|
2015-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-04 14:14:24 +00:00
|
|
|
* Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
|
2017-07-11 17:35:31 +00:00
|
|
|
* specified above for AndroidOptions. It will not perform any scaling or subsetting.
|
2015-10-21 17:27:10 +00:00
|
|
|
*/
|
|
|
|
SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
|
|
|
|
|
2016-02-04 14:14:24 +00:00
|
|
|
SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
|
|
|
|
return this->getAndroidPixels(info, pixels, rowBytes);
|
|
|
|
}
|
|
|
|
|
2015-10-21 17:27:10 +00:00
|
|
|
protected:
|
|
|
|
|
2015-12-10 21:09:24 +00:00
|
|
|
SkAndroidCodec(SkCodec*);
|
2015-10-21 17:27:10 +00:00
|
|
|
|
2015-12-10 21:09:24 +00:00
|
|
|
SkCodec* codec() const { return fCodec.get(); }
|
2015-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
|
|
|
|
|
|
|
|
virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
|
|
|
|
|
|
|
|
virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
|
2015-11-04 12:31:12 +00:00
|
|
|
size_t rowBytes, const AndroidOptions& options) = 0;
|
2015-10-21 17:27:10 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// This will always be a reference to the info that is contained by the
|
|
|
|
// embedded SkCodec.
|
|
|
|
const SkImageInfo& fInfo;
|
2015-12-10 21:09:24 +00:00
|
|
|
|
2016-10-27 16:30:08 +00:00
|
|
|
std::unique_ptr<SkCodec> fCodec;
|
2015-10-21 17:27:10 +00:00
|
|
|
};
|
|
|
|
#endif // SkAndroidCodec_DEFINED
|