#include "../private/SkTemplates.h"
#include "SkColor.h"
#include "SkEncodedFormat.h"
-#include "SkEncodedInfo.h"
#include "SkImageInfo.h"
#include "SkSize.h"
#include "SkStream.h"
*/
const SkImageInfo& getInfo() const { return fSrcInfo; }
- const SkEncodedInfo& getEncodedInfo() const { return fEncodedInfo; }
-
/**
* Returns the color space associated with the codec.
* Does not affect ownership.
/**
* Takes ownership of SkStream*
*/
- SkCodec(int width,
- int height,
- const SkEncodedInfo&,
+ SkCodec(const SkImageInfo&,
SkStream*,
sk_sp<SkColorSpace> = nullptr,
Origin = kTopLeft_Origin);
virtual int onOutputScanline(int inputScanline) const;
private:
- const SkEncodedInfo fEncodedInfo;
const SkImageInfo fSrcInfo;
SkAutoTDelete<SkStream> fStream;
bool fNeedsRewind;
+++ /dev/null
-/*
- * Copyright 2016 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef SkEncodedInfo_DEFINED
-#define SkEncodedInfo_DEFINED
-
-#include "SkImageInfo.h"
-
-struct SkEncodedInfo {
-public:
-
- enum Alpha {
- kOpaque_Alpha,
- kUnpremul_Alpha,
-
- // Each pixel is either fully opaque or fully transparent.
- // There is no difference between requesting kPremul or kUnpremul.
- kBinary_Alpha,
-
- // Allows us to have a default constructor. Should be treated as
- // invalid.
- kUnknown_Alpha,
- };
-
- /*
- * We strive to make the number of components per pixel obvious through
- * our naming conventions.
- * Ex: kRGB has 3 components. kRGBA has 4 components.
- *
- * This sometimes results in redundant Alpha and Color information.
- * Ex: kRGB images must also be kOpaque.
- */
- enum Color {
- // PNG, WBMP
- kGray_Color,
-
- // PNG
- kGrayAlpha_Color,
-
- // PNG, GIF, BMP
- kPalette_Color,
-
- // PNG, RAW
- kRGB_Color,
- kRGBA_Color,
-
- // BMP
- kBGR_Color,
- kBGRX_Color,
- kBGRA_Color,
-
- // JPEG, WEBP
- kYUV_Color,
-
- // WEBP
- kYUVA_Color,
-
- // JPEG
- // Photoshop actually writes inverted CMYK data into JPEGs, where zero
- // represents 100% ink coverage. For this reason, we treat CMYK JPEGs
- // as having inverted CMYK. libjpeg-turbo warns that this may break
- // other applications, but the CMYK JPEGs we see on the web expect to
- // be treated as inverted CMYK.
- kInvertedCMYK_Color,
- kYCCK_Color,
-
- // Allows us to have a default constructor. Should be treated as
- // invalid.
- kUnknown_Color,
- };
-
- static SkEncodedInfo Make(Color color, Alpha alpha, int bitsPerComponent) {
- SkASSERT(1 == bitsPerComponent ||
- 2 == bitsPerComponent ||
- 4 == bitsPerComponent ||
- 8 == bitsPerComponent ||
- 16 == bitsPerComponent);
-
- switch (color) {
- case kGray_Color:
- SkASSERT(kOpaque_Alpha == alpha);
- break;
- case kGrayAlpha_Color:
- SkASSERT(kOpaque_Alpha != alpha);
- break;
- case kPalette_Color:
- SkASSERT(16 != bitsPerComponent);
- break;
- case kRGB_Color:
- case kBGR_Color:
- case kBGRX_Color:
- SkASSERT(kOpaque_Alpha == alpha);
- SkASSERT(bitsPerComponent >= 8);
- break;
- case kYUV_Color:
- case kInvertedCMYK_Color:
- case kYCCK_Color:
- SkASSERT(kOpaque_Alpha == alpha);
- SkASSERT(8 == bitsPerComponent);
- break;
- case kRGBA_Color:
- SkASSERT(kOpaque_Alpha != alpha);
- SkASSERT(bitsPerComponent >= 8);
- break;
- case kBGRA_Color:
- case kYUVA_Color:
- SkASSERT(kOpaque_Alpha != alpha);
- SkASSERT(8 == bitsPerComponent);
- break;
- default:
- SkASSERT(false);
- break;
- }
-
- return SkEncodedInfo(color, alpha, bitsPerComponent);
- }
-
- /*
- * Returns an SkImageInfo with Skia color and alpha types that are the
- * closest possible match to the encoded info.
- */
- SkImageInfo makeImageInfo(int width, int height) const {
- switch (fColor) {
- case kGray_Color:
- SkASSERT(kOpaque_Alpha == fAlpha);
- return SkImageInfo::Make(width, height, kGray_8_SkColorType, kOpaque_SkAlphaType);
- case kGrayAlpha_Color:
- SkASSERT(kOpaque_Alpha != fAlpha);
- return SkImageInfo::Make(width, height, kN32_SkColorType,
- kUnpremul_SkAlphaType);
- case kPalette_Color: {
- SkAlphaType alphaType = (kOpaque_Alpha == fAlpha) ? kOpaque_SkAlphaType :
- kUnpremul_SkAlphaType;
- return SkImageInfo::Make(width, height, kIndex_8_SkColorType, alphaType);
- }
- case kRGB_Color:
- case kBGR_Color:
- case kBGRX_Color:
- case kYUV_Color:
- case kInvertedCMYK_Color:
- case kYCCK_Color:
- SkASSERT(kOpaque_Alpha == fAlpha);
- return SkImageInfo::Make(width, height, kN32_SkColorType, kOpaque_SkAlphaType);
- case kRGBA_Color:
- case kBGRA_Color:
- case kYUVA_Color:
- SkASSERT(kOpaque_Alpha != fAlpha);
- return SkImageInfo::Make(width, height, kN32_SkColorType, kUnpremul_SkAlphaType);
- default:
- SkASSERT(false);
- return SkImageInfo::MakeUnknown();
- }
- }
-
- SkEncodedInfo()
- : fColor(kUnknown_Color)
- , fAlpha(kUnknown_Alpha)
- , fBitsPerComponent(0)
- {}
-
-private:
-
- SkEncodedInfo(Color color, Alpha alpha, uint8_t bitsPerComponent)
- : fColor(color)
- , fAlpha(alpha)
- , fBitsPerComponent(bitsPerComponent)
- {}
-
- Color fColor;
- Alpha fAlpha;
- uint8_t fBitsPerComponent;
-};
-
-#endif
switch (inputFormat) {
case kStandard_BmpInputFormat: {
- // BMPs are generally opaque, however BMPs-in-ICOs may contain
- // a transparency mask after the image. Therefore, we mark the
- // alpha as kBinary if the BMP is contained in an ICO.
- // We use |isOpaque| to indicate if the BMP itself is opaque.
- SkEncodedInfo::Alpha alpha = inIco ? SkEncodedInfo::kBinary_Alpha :
- SkEncodedInfo::kOpaque_Alpha;
+ // BMPs-in-ICOs often contain an alpha mask after the image, which
+ // means we cannot guarantee that an image is opaque, even if the
+ // embedded bmp is opaque.
+ // We use |isOpaque| to indicate if the BMP itself is opaque, but
+ // still need to recommend kUnpremul when it is contained in an ICO.
+ SkColorType colorType = kN32_SkColorType;
+ SkAlphaType alphaType = inIco ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
bool isOpaque = true;
-
- SkEncodedInfo::Color color;
- uint8_t bitsPerComponent;
switch (bitsPerPixel) {
// Palette formats
case 1:
case 2:
case 4:
case 8:
- // In the case of ICO, kBGRA is actually the closest match,
- // since we will need to apply a transparency mask.
- if (inIco) {
- color = SkEncodedInfo::kBGRA_Color;
- bitsPerComponent = 8;
- } else {
- color = SkEncodedInfo::kPalette_Color;
- bitsPerComponent = (uint8_t) bitsPerPixel;
+ // We cannot recommend a palette color type for ICOs because they
+ // may contain a transparency mask.
+ if (!inIco) {
+ colorType = kIndex_8_SkColorType;
}
break;
case 24:
- color = SkEncodedInfo::kBGR_Color;
- bitsPerComponent = 8;
- break;
case 32:
// 32-bit BMP-in-ICOs actually use the alpha channel in place of a
// transparency mask.
if (inIco) {
isOpaque = false;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
- color = SkEncodedInfo::kBGRA_Color;
- } else {
- color = SkEncodedInfo::kBGRX_Color;
}
- bitsPerComponent = 8;
break;
default:
SkCodecPrintf("Error: invalid input value for bits per pixel.\n");
SkASSERT(!inIco || nullptr != stream->getMemoryBase());
// Set the image info and create a codec.
- const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, bitsPerComponent);
- *codecOut = new SkBmpStandardCodec(width, height, info, stream, bitsPerPixel,
- numColors, bytesPerColor, offset - bytesRead, rowOrder, isOpaque, inIco);
+ const SkImageInfo imageInfo = SkImageInfo::Make(width, height, colorType,
+ alphaType);
+ *codecOut = new SkBmpStandardCodec(imageInfo, stream, bitsPerPixel, numColors,
+ bytesPerColor, offset - bytesRead, rowOrder, isOpaque, inIco);
+
}
return true;
}
return false;
}
- // Masked bmps are not a great fit for SkEncodedInfo, since they have
- // arbitrary component orderings and bits per component. Here we choose
- // somewhat reasonable values - it's ok that we don't match exactly
- // because SkBmpMaskCodec has its own mask swizzler anyway.
- SkEncodedInfo::Color color;
- SkEncodedInfo::Alpha alpha;
- if (masks->getAlphaMask()) {
- color = SkEncodedInfo::kBGRA_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
- } else {
- color = SkEncodedInfo::kBGR_Color;
- alpha = SkEncodedInfo::kOpaque_Alpha;
- }
- const SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
- *codecOut = new SkBmpMaskCodec(width, height, info, stream, bitsPerPixel,
- masks.release(), rowOrder);
+ // Set the image info
+ SkAlphaType alphaType = masks->getAlphaMask() ? kUnpremul_SkAlphaType :
+ kOpaque_SkAlphaType;
+ const SkImageInfo imageInfo = SkImageInfo::Make(width, height, kN32_SkColorType,
+ alphaType);
+ *codecOut = new SkBmpMaskCodec(imageInfo, stream, bitsPerPixel, masks.release(),
+ rowOrder);
}
return true;
}
if (codecOut) {
// RLE inputs may skip pixels, leaving them as transparent. This
// is uncommon, but we cannot be certain that an RLE bmp will be
- // opaque or that we will be able to represent it with a palette.
- // For that reason, we always indicate that we are kBGRA.
- const SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kBGRA_Color,
- SkEncodedInfo::kBinary_Alpha, 8);
- *codecOut = new SkBmpRLECodec(width, height, info, stream, bitsPerPixel, numColors,
+ // opaque.
+ const SkImageInfo imageInfo = SkImageInfo::Make(width, height, kN32_SkColorType,
+ kUnpremul_SkAlphaType);
+ *codecOut = new SkBmpRLECodec(imageInfo, stream, bitsPerPixel, numColors,
bytesPerColor, offset - bytesRead, rowOrder, RLEBytes);
}
return true;
return nullptr;
}
-SkBmpCodec::SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpCodec::SkBmpCodec(const SkImageInfo& info, SkStream* stream,
uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder)
- : INHERITED(width, height, info, stream)
+ : INHERITED(info, stream)
, fBitsPerPixel(bitsPerPixel)
, fRowOrder(rowOrder)
- , fSrcRowBytes(SkAlign4(compute_row_bytes(width, fBitsPerPixel)))
+ , fSrcRowBytes(SkAlign4(compute_row_bytes(info.width(), fBitsPerPixel)))
{}
bool SkBmpCodec::onRewind() {
protected:
- SkBmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
- uint16_t bitsPerPixel, SkCodec::SkScanlineOrder rowOrder);
+ SkBmpCodec(const SkImageInfo& info, SkStream* stream, uint16_t bitsPerPixel,
+ SkCodec::SkScanlineOrder rowOrder);
SkEncodedFormat onGetEncodedFormat() const override { return kBMP_SkEncodedFormat; }
/*
* Creates an instance of the decoder
*/
-SkBmpMaskCodec::SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpMaskCodec::SkBmpMaskCodec(const SkImageInfo& info, SkStream* stream,
uint16_t bitsPerPixel, SkMasks* masks,
SkCodec::SkScanlineOrder rowOrder)
- : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+ : INHERITED(info, stream, bitsPerPixel, rowOrder)
, fMasks(masks)
, fMaskSwizzler(nullptr)
, fSrcBuffer(new uint8_t [this->srcRowBytes()])
* Called only by SkBmpCodec::NewFromStream
* There should be no other callers despite this being public
*
- * @param info contains properties of the encoded data
+ * @param srcInfo contains the source width and height
* @param stream the stream of encoded image data
* @param bitsPerPixel the number of bits used to store each pixel
* @param masks color masks for certain bmp formats
* @param rowOrder indicates whether rows are ordered top-down or bottom-up
*/
- SkBmpMaskCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+ SkBmpMaskCodec(const SkImageInfo& srcInfo, SkStream* stream,
uint16_t bitsPerPixel, SkMasks* masks,
SkCodec::SkScanlineOrder rowOrder);
* Creates an instance of the decoder
* Called only by NewFromStream
*/
-SkBmpRLECodec::SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkBmpRLECodec::SkBmpRLECodec(const SkImageInfo& info, SkStream* stream,
uint16_t bitsPerPixel, uint32_t numColors,
uint32_t bytesPerColor, uint32_t offset,
SkCodec::SkScanlineOrder rowOrder,
size_t RLEBytes)
- : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+ : INHERITED(info, stream, bitsPerPixel, rowOrder)
, fColorTable(nullptr)
, fNumColors(numColors)
, fBytesPerColor(bytesPerColor)
* Called only by SkBmpCodec::NewFromStream
* There should be no other callers despite this being public
*
- * @param info contains properties of the encoded data
+ * @param srcInfo contains the source width and height
* @param stream the stream of encoded image data
* @param bitsPerPixel the number of bits used to store each pixel
* @param numColors the number of colors in the color table
* @param RLEBytes indicates the amount of data left in the stream
* after decoding the headers
*/
- SkBmpRLECodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+ SkBmpRLECodec(const SkImageInfo& srcInfo, SkStream* stream,
uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
uint32_t offset, SkCodec::SkScanlineOrder rowOrder,
size_t RLEBytes);
* Creates an instance of the decoder
* Called only by NewFromStream
*/
-SkBmpStandardCodec::SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info,
- SkStream* stream, uint16_t bitsPerPixel, uint32_t numColors,
+SkBmpStandardCodec::SkBmpStandardCodec(const SkImageInfo& info, SkStream* stream,
+ uint16_t bitsPerPixel, uint32_t numColors,
uint32_t bytesPerColor, uint32_t offset,
SkCodec::SkScanlineOrder rowOrder,
bool isOpaque, bool inIco)
- : INHERITED(width, height, info, stream, bitsPerPixel, rowOrder)
+ : INHERITED(info, stream, bitsPerPixel, rowOrder)
, fColorTable(nullptr)
, fNumColors(numColors)
, fBytesPerColor(bytesPerColor)
* Called only by SkBmpCodec::NewFromStream
* There should be no other callers despite this being public
*
- * @param info contains properties of the encoded data
+ * @param srcInfo contains the source width and height
* @param stream the stream of encoded image data
* @param bitsPerPixel the number of bits used to store each pixel
* @param numColors the number of colors in the color table
* the icp mask, if there is one)
* @param inIco indicates if the bmp is embedded in an ico file
*/
- SkBmpStandardCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+ SkBmpStandardCodec(const SkImageInfo& srcInfo, SkStream* stream,
uint16_t bitsPerPixel, uint32_t numColors, uint32_t bytesPerColor,
uint32_t offset, SkCodec::SkScanlineOrder rowOrder, bool isOpaque,
bool inIco);
return NewFromStream(new SkMemoryStream(data), reader);
}
-SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
- sk_sp<SkColorSpace> colorSpace, Origin origin)
- : fEncodedInfo(info)
- , fSrcInfo(info.makeImageInfo(width, height))
+SkCodec::SkCodec(const SkImageInfo& info, SkStream* stream, sk_sp<SkColorSpace> colorSpace,
+ Origin origin)
+ : fSrcInfo(info)
, fStream(stream)
, fNeedsRewind(false)
, fColorSpace(colorSpace)
}
bool frameIsSubset = (size != frameRect.size());
- // Determine the encoded alpha type. The transIndex might be valid if it less
+ // Determine the recommended alpha type. The transIndex might be valid if it less
// than 256. We are not certain that the index is valid until we process the color
// table, since some gifs have color tables with less than 256 colors. If
// there might be a valid transparent index, we must indicate that the image has
// alpha.
- // In the case where we must support alpha, we indicate kBinary, since every
- // pixel will either be fully opaque or fully transparent.
- SkEncodedInfo::Alpha alpha = (transIndex < 256) ? SkEncodedInfo::kBinary_Alpha :
- SkEncodedInfo::kOpaque_Alpha;
+ // In the case where we must support alpha, we have the option to set the
+ // suggested alpha type to kPremul or kUnpremul. Both are valid since the alpha
+ // component will always be 0xFF or the entire 32-bit pixel will be set to zero.
+ // We prefer kPremul because we support kPremul, and it is more efficient to use
+ // kPremul directly even when kUnpremul is supported.
+ SkAlphaType alphaType = (transIndex < 256) ? kPremul_SkAlphaType : kOpaque_SkAlphaType;
// Return the codec
- // Use kPalette since Gifs are encoded with a color table.
- // Use 8-bits per component, since this is the output we get from giflib.
- // FIXME: Gifs can actually be encoded with 4-bits per pixel. Can we support this?
- SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
- *codecOut = new SkGifCodec(size.width(), size.height(), info, streamDeleter.release(),
- gif.release(), transIndex, frameRect, frameIsSubset);
+ // kIndex is the most natural color type for gifs, so we set this as
+ // the default.
+ SkImageInfo imageInfo = SkImageInfo::Make(size.width(), size.height(), kIndex_8_SkColorType,
+ alphaType);
+ *codecOut = new SkGifCodec(imageInfo, streamDeleter.release(), gif.release(), transIndex,
+ frameRect, frameIsSubset);
} else {
SkASSERT(nullptr != gifOut);
streamDeleter.release();
return nullptr;
}
-SkGifCodec::SkGifCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
- GifFileType* gif, uint32_t transIndex, const SkIRect& frameRect, bool frameIsSubset)
- : INHERITED(width, height, info, stream)
+SkGifCodec::SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif,
+ uint32_t transIndex, const SkIRect& frameRect, bool frameIsSubset)
+ : INHERITED(srcInfo, stream)
, fGif(gif)
, fSrcBuffer(new uint8_t[this->getInfo().width()])
, fFrameRect(frameRect)
* Creates an instance of the decoder
* Called only by NewFromStream
*
- * @param info contains properties of the encoded data
+ * @param srcInfo contains the source width and height
* @param stream the stream of image data
* @param gif pointer to library type that manages gif decode
* takes ownership
* @param transIndex The transparent index. An invalid value
* indicates that there is no transparent index.
*/
- SkGifCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
- GifFileType* gif, uint32_t transIndex, const SkIRect& frameRect, bool frameIsSubset);
+ SkGifCodec(const SkImageInfo& srcInfo, SkStream* stream, GifFileType* gif, uint32_t transIndex,
+ const SkIRect& frameRect, bool frameIsSubset);
SkAutoTCallVProc<GifFileType, CloseGif> fGif; // owned
SkAutoTDeleteArray<uint8_t> fSrcBuffer;
maxIndex = i;
}
}
- int width = codecs->operator[](maxIndex)->getInfo().width();
- int height = codecs->operator[](maxIndex)->getInfo().height();
- SkEncodedInfo info = codecs->operator[](maxIndex)->getEncodedInfo();
+ SkImageInfo info = codecs->operator[](maxIndex)->getInfo();
// Note that stream is owned by the embedded codec, the ico does not need
// direct access to the stream.
- return new SkIcoCodec(width, height, info, codecs.release());
+ return new SkIcoCodec(info, codecs.release());
}
/*
* Creates an instance of the decoder
* Called only by NewFromStream
*/
-SkIcoCodec::SkIcoCodec(int width, int height, const SkEncodedInfo& info,
+SkIcoCodec::SkIcoCodec(const SkImageInfo& info,
SkTArray<SkAutoTDelete<SkCodec>, true>* codecs)
- : INHERITED(width, height, info, nullptr)
+ : INHERITED(info, nullptr)
, fEmbeddedCodecs(codecs)
, fCurrScanlineCodec(nullptr)
{}
* Constructor called by NewFromStream
* @param embeddedCodecs codecs for the embedded images, takes ownership
*/
- SkIcoCodec(int width, int height, const SkEncodedInfo& info,
- SkTArray<SkAutoTDelete<SkCodec>, true>* embeddedCodecs);
+ SkIcoCodec(const SkImageInfo& srcInfo, SkTArray<SkAutoTDelete<SkCodec>, true>* embeddedCodecs);
SkAutoTDelete<SkTArray<SkAutoTDelete<SkCodec>, true>> fEmbeddedCodecs; // owned
}
if (codecOut) {
- // Get the encoded color type
- SkEncodedInfo::Color color = decoderMgr->getEncodedColor();
- if (SkEncodedInfo::kUnknown_Color == color) {
- return false;
- }
+ // Recommend the color type to decode to
+ const SkColorType colorType = decoderMgr->getColorType();
// Create image info object and the codec
- SkEncodedInfo info = SkEncodedInfo::Make(color, SkEncodedInfo::kOpaque_Alpha, 8);
+ const SkImageInfo& imageInfo = SkImageInfo::Make(decoderMgr->dinfo()->image_width,
+ decoderMgr->dinfo()->image_height, colorType, kOpaque_SkAlphaType);
Origin orientation = get_exif_orientation(decoderMgr->dinfo());
sk_sp<SkColorSpace> colorSpace = get_icc_profile(decoderMgr->dinfo());
- *codecOut = new SkJpegCodec(decoderMgr->dinfo()->image_width,
- decoderMgr->dinfo()->image_height, info, stream, decoderMgr.release(), colorSpace,
+ *codecOut = new SkJpegCodec(imageInfo, stream, decoderMgr.release(), colorSpace,
orientation);
} else {
SkASSERT(nullptr != decoderMgrOut);
return nullptr;
}
-SkJpegCodec::SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin)
- : INHERITED(width, height, info, stream, colorSpace, origin)
+ : INHERITED(srcInfo, stream, colorSpace, origin)
, fDecoderMgr(decoderMgr)
, fReadyState(decoderMgr->dinfo()->global_state)
, fSwizzlerSubset(SkIRect::MakeEmpty())
* Creates an instance of the decoder
* Called only by NewFromStream
*
- * @param info contains properties of the encoded data
+ * @param srcInfo contains the source width and height
* @param stream the encoded image data
* @param decoderMgr holds decompress struct, src manager, and error manager
* takes ownership
*/
- SkJpegCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
- JpegDecoderMgr* decoderMgr, sk_sp<SkColorSpace> colorSpace, Origin origin);
+ SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, JpegDecoderMgr* decoderMgr,
+ sk_sp<SkColorSpace> colorSpace, Origin origin);
/*
* Checks if the conversion between the input image and the requested output
return result;
}
-SkEncodedInfo::Color JpegDecoderMgr::getEncodedColor() {
+SkColorType JpegDecoderMgr::getColorType() {
switch (fDInfo.jpeg_color_space) {
case JCS_GRAYSCALE:
- return SkEncodedInfo::kGray_Color;
- case JCS_YCbCr:
- return SkEncodedInfo::kYUV_Color;
- case JCS_RGB:
- return SkEncodedInfo::kRGB_Color;
- case JCS_YCCK:
- return SkEncodedInfo::kYCCK_Color;
- case JCS_CMYK:
- return SkEncodedInfo::kInvertedCMYK_Color;
+ return kGray_8_SkColorType;
default:
- return SkEncodedInfo::kUnknown_Color;
+ return kN32_SkColorType;
}
}
void init();
/*
- * Returns the encoded color type of the jpeg, or kUnknown if the
- * color type can't be determined
+ * Recommend a color type based on the encoded format
*/
- SkEncodedInfo::Color getEncodedColor();
+ SkColorType getColorType();
/*
* Free memory used by the decode manager
// png_structp on success.
// @param info_ptrp Optional output variable. If non-NULL, will be set to a new
// png_infop on success;
-// @param info Optional output variable. If non-NULL, will be set to
+// @param imageInfo Optional output variable. If non-NULL, will be set to
// reflect the properties of the encoded image on success.
// @param bitDepthPtr Optional output variable. If non-NULL, will be set to the
// bit depth of the encoded image on success.
// If it returns false, the passed in fields (except stream) are unchanged.
static bool read_header(SkStream* stream, SkPngChunkReader* chunkReader,
png_structp* png_ptrp, png_infop* info_ptrp,
- int* width, int* height, SkEncodedInfo* info, int* bitDepthPtr,
- int* numberPassesPtr) {
+ SkImageInfo* imageInfo, int* bitDepthPtr, int* numberPassesPtr) {
// The image is known to be a PNG. Decode enough to know the SkImageInfo.
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
sk_error_fn, sk_warning_fn);
// Now determine the default colorType and alphaType and set the required transforms.
// Often, we depend on SkSwizzler to perform any transforms that we need. However, we
// still depend on libpng for many of the rare and PNG-specific cases.
- SkEncodedInfo::Color color;
- SkEncodedInfo::Alpha alpha;
+ SkColorType colorType = kUnknown_SkColorType;
+ SkAlphaType alphaType = kUnknown_SkAlphaType;
switch (encodedColorType) {
case PNG_COLOR_TYPE_PALETTE:
// Extract multiple pixels with bit depths of 1, 2, and 4 from a single
png_set_packing(png_ptr);
}
- color = SkEncodedInfo::kPalette_Color;
- // Set the alpha depending on if a transparency chunk exists.
- alpha = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ?
- SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha;
+ colorType = kIndex_8_SkColorType;
+ // Set the alpha type depending on if a transparency chunk exists.
+ alphaType = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ?
+ kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
break;
case PNG_COLOR_TYPE_RGB:
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
// Convert to RGBA if transparency chunk exists.
png_set_tRNS_to_alpha(png_ptr);
- color = SkEncodedInfo::kRGBA_Color;
- alpha = SkEncodedInfo::kBinary_Alpha;
+ alphaType = kUnpremul_SkAlphaType;
} else {
- color = SkEncodedInfo::kRGB_Color;
- alpha = SkEncodedInfo::kOpaque_Alpha;
+ alphaType = kOpaque_SkAlphaType;
}
+ colorType = kN32_SkColorType;
break;
case PNG_COLOR_TYPE_GRAY:
// Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel.
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
- color = SkEncodedInfo::kGrayAlpha_Color;
- alpha = SkEncodedInfo::kBinary_Alpha;
+
+ // We will recommend kN32 here since we do not support kGray
+ // with alpha.
+ colorType = kN32_SkColorType;
+ alphaType = kUnpremul_SkAlphaType;
} else {
- color = SkEncodedInfo::kGray_Color;
- alpha = SkEncodedInfo::kOpaque_Alpha;
+ colorType = kGray_8_SkColorType;
+ alphaType = kOpaque_SkAlphaType;
}
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
- color = SkEncodedInfo::kGrayAlpha_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
+ // We will recommend kN32 here since we do not support anything
+ // similar to GRAY_ALPHA.
+ colorType = kN32_SkColorType;
+ alphaType = kUnpremul_SkAlphaType;
break;
case PNG_COLOR_TYPE_RGBA:
- color = SkEncodedInfo::kRGBA_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
+ colorType = kN32_SkColorType;
+ alphaType = kUnpremul_SkAlphaType;
break;
default:
// All the color types have been covered above.
SkASSERT(false);
- color = SkEncodedInfo::kRGBA_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
}
int numberPasses = png_set_interlace_handling(png_ptr);
*numberPassesPtr = numberPasses;
}
- if (info) {
- *info = SkEncodedInfo::Make(color, alpha, 8);
- }
- if (width) {
- *width = origWidth;
+ SkColorProfileType profileType = kLinear_SkColorProfileType;
+ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
+ profileType = kSRGB_SkColorProfileType;
}
- if (height) {
- *height = origHeight;
+
+ if (imageInfo) {
+ *imageInfo = SkImageInfo::Make(origWidth, origHeight, colorType, alphaType, profileType);
}
autoClean.release();
if (png_ptrp) {
return true;
}
-SkPngCodec::SkPngCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream,
- SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr,
- int bitDepth, int numberPasses, sk_sp<SkColorSpace> colorSpace)
- : INHERITED(width, height, info, stream, colorSpace)
+SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, SkPngChunkReader* chunkReader,
+ png_structp png_ptr, png_infop info_ptr, int bitDepth, int numberPasses,
+ sk_sp<SkColorSpace> colorSpace)
+ : INHERITED(info, stream, colorSpace)
, fPngChunkReader(SkSafeRef(chunkReader))
, fPng_ptr(png_ptr)
, fInfo_ptr(info_ptr)
png_structp png_ptr;
png_infop info_ptr;
if (!read_header(this->stream(), fPngChunkReader.get(), &png_ptr, &info_ptr,
- nullptr, nullptr, nullptr, nullptr, nullptr)) {
+ nullptr, nullptr, nullptr)) {
return false;
}
// Subclass of SkPngCodec which supports scanline decoding
class SkPngScanlineDecoder : public SkPngCodec {
public:
- SkPngScanlineDecoder(int width, int height, const SkEncodedInfo& info, SkStream* stream,
+ SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr, int bitDepth,
sk_sp<SkColorSpace> colorSpace)
- : INHERITED(width, height, info, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1,
- colorSpace)
+ : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1, colorSpace)
, fSrcRow(nullptr)
{}
class SkPngInterlacedScanlineDecoder : public SkPngCodec {
public:
- SkPngInterlacedScanlineDecoder(int width, int height, const SkEncodedInfo& info,
- SkStream* stream, SkPngChunkReader* chunkReader, png_structp png_ptr,
- png_infop info_ptr, int bitDepth, int numberPasses, sk_sp<SkColorSpace> colorSpace)
- : INHERITED(width, height, info, stream, chunkReader, png_ptr, info_ptr, bitDepth,
- numberPasses, colorSpace)
+ SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
+ SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr,
+ int bitDepth, int numberPasses, sk_sp<SkColorSpace> colorSpace)
+ : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, numberPasses,
+ colorSpace)
, fHeight(-1)
, fCanSkipRewind(false)
{
SkAutoTDelete<SkStream> streamDeleter(stream);
png_structp png_ptr;
png_infop info_ptr;
- int width, height;
- SkEncodedInfo imageInfo;
+ SkImageInfo imageInfo;
int bitDepth;
int numberPasses;
- if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &width, &height, &imageInfo,
- &bitDepth, &numberPasses)) {
+ if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &imageInfo, &bitDepth,
+ &numberPasses)) {
return nullptr;
}
auto colorSpace = read_color_space(png_ptr, info_ptr);
if (1 == numberPasses) {
- return new SkPngScanlineDecoder(width, height, imageInfo, streamDeleter.release(),
- chunkReader, png_ptr, info_ptr, bitDepth, colorSpace);
+ return new SkPngScanlineDecoder(imageInfo, streamDeleter.release(), chunkReader,
+ png_ptr, info_ptr, bitDepth, colorSpace);
}
- return new SkPngInterlacedScanlineDecoder(width, height, imageInfo, streamDeleter.release(),
- chunkReader, png_ptr, info_ptr, bitDepth,
- numberPasses, colorSpace);
+ return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.release(), chunkReader,
+ png_ptr, info_ptr, bitDepth, numberPasses,
+ colorSpace);
}
return fSwizzler;
}
- SkPngCodec(int width, int height, const SkEncodedInfo&, SkStream*, SkPngChunkReader*,
- png_structp, png_infop, int, int, sk_sp<SkColorSpace>);
+ SkPngCodec(const SkImageInfo&, SkStream*, SkPngChunkReader*, png_structp, png_infop, int, int,
+ sk_sp<SkColorSpace>);
png_structp png_ptr() { return fPng_ptr; }
SkSwizzler* swizzler() { return fSwizzler; }
}
}
- const SkEncodedInfo& getEncodedInfo() const {
- return fEncodedInfo;
- }
-
- int width() const {
- return fWidth;
- }
-
- int height() const {
- return fHeight;
+ const SkImageInfo& getImageInfo() const {
+ return fImageInfo;
}
bool isScalable() const {
return 0x2A == get_endian_short(header + 2, littleEndian);
}
- void init(int width, int height, const dng_point& cfaPatternSize) {
- fWidth = width;
- fHeight = height;
- fEncodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kRGB_Color,
- SkEncodedInfo::kOpaque_Alpha, 8);
+ void init(const int width, const int height, const dng_point& cfaPatternSize) {
+ fImageInfo = SkImageInfo::Make(width, height, kN32_SkColorType, kOpaque_SkAlphaType);
// The DNG SDK scales only during demosaicing, so scaling is only possible when
// a mosaic info is available.
SkAutoTDelete<dng_negative> fNegative;
SkAutoTDelete<dng_stream> fDngStream;
- int fWidth;
- int fHeight;
- SkEncodedInfo fEncodedInfo;
+ SkImageInfo fImageInfo;
bool fIsScalable;
bool fIsXtransImage;
};
SkRawCodec::~SkRawCodec() {}
SkRawCodec::SkRawCodec(SkDngImage* dngImage)
- : INHERITED(dngImage->width(), dngImage->height(), dngImage->getEncodedInfo(), nullptr)
+ : INHERITED(dngImage->getImageInfo(), nullptr)
, fDngImage(dngImage) {}
return this->stream()->read(row, fSrcRowBytes) == fSrcRowBytes;
}
-SkWbmpCodec::SkWbmpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
- : INHERITED(width, height, info, stream)
+SkWbmpCodec::SkWbmpCodec(const SkImageInfo& info, SkStream* stream)
+ : INHERITED(info, stream)
, fSrcRowBytes(get_src_row_bytes(this->getInfo().width()))
, fSwizzler(nullptr)
, fColorTable(nullptr)
if (!read_header(stream, &size)) {
return nullptr;
}
- SkEncodedInfo info = SkEncodedInfo::Make(SkEncodedInfo::kGray_Color,
- SkEncodedInfo::kOpaque_Alpha, 1);
- return new SkWbmpCodec(size.width(), size.height(), info, streamDeleter.release());
+ SkImageInfo info = SkImageInfo::Make(size.width(), size.height(),
+ kGray_8_SkColorType, kOpaque_SkAlphaType);
+ return new SkWbmpCodec(info, streamDeleter.release());
}
int SkWbmpCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
*/
bool readRow(uint8_t* row);
- SkWbmpCodec(int width, int height, const SkEncodedInfo&, SkStream*);
+ SkWbmpCodec(const SkImageInfo&, SkStream*);
const size_t fSrcRowBytes;
// Parse headers of RIFF container, and check for valid Webp (VP8) content.
// NOTE: This calls peek instead of read, since onGetPixels will need these
// bytes again.
-static bool webp_parse_header(SkStream* stream, int* width, int* height, SkEncodedInfo* info) {
+static bool webp_parse_header(SkStream* stream, SkImageInfo* info) {
unsigned char buffer[WEBP_VP8_HEADER_SIZE];
SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded());
}
if (info) {
- SkEncodedInfo::Color color;
- SkEncodedInfo::Alpha alpha;
- switch (features.format) {
- case 0:
- // This indicates a "mixed" format. We would see this for
- // animated webps or for webps encoded in multiple fragments.
- // I believe that this is a rare case.
- // We could also guess kYUV here, but I think it makes more
- // sense to guess kBGRA which is likely closer to the final
- // output. Otherwise, we might end up converting
- // BGRA->YUVA->BGRA.
- color = SkEncodedInfo::kBGRA_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
- break;
- case 1:
- // This is the lossy format (YUV).
- if (SkToBool(features.has_alpha)) {
- color = SkEncodedInfo::kYUVA_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
- } else {
- color = SkEncodedInfo::kYUV_Color;
- alpha = SkEncodedInfo::kOpaque_Alpha;
- }
- break;
- case 2:
- // This is the lossless format (BGRA).
- // FIXME: Should we check the has_alpha flag here? It looks
- // like the image is encoded with an alpha channel
- // regardless of whether or not the alpha flag is set.
- color = SkEncodedInfo::kBGRA_Color;
- alpha = SkEncodedInfo::kUnpremul_Alpha;
- break;
- default:
- return false;
- }
-
- *width = features.width;
- *height = features.height;
- *info = SkEncodedInfo::Make(color, alpha, 8);
+ // FIXME: Is N32 the right type?
+ // Is unpremul the right type? Clients of SkCodec may assume it's the
+ // best type, when Skia currently cannot draw unpremul (and raster is faster
+ // with premul).
+ *info = SkImageInfo::Make(features.width, features.height, kN32_SkColorType,
+ SkToBool(features.has_alpha) ? kUnpremul_SkAlphaType
+ : kOpaque_SkAlphaType);
}
return true;
}
SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
SkAutoTDelete<SkStream> streamDeleter(stream);
- int width, height;
- SkEncodedInfo info;
- if (webp_parse_header(stream, &width, &height, &info)) {
- return new SkWebpCodec(width, height, info, streamDeleter.release());
+ SkImageInfo info;
+ if (webp_parse_header(stream, &info)) {
+ return new SkWebpCodec(info, streamDeleter.release());
}
return nullptr;
}
}
}
-SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
+SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream)
// The spec says an unmarked image is sRGB, so we return that space here.
// TODO: Add support for parsing ICC profiles from webps.
- : INHERITED(width, height, info, stream, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)) {}
+ : INHERITED(info, stream, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)) {}
bool onGetValidSubset(SkIRect* /* desiredSubset */) const override;
private:
- SkWebpCodec(int width, int height, const SkEncodedInfo&, SkStream*);
+ SkWebpCodec(const SkImageInfo&, SkStream*);
typedef SkCodec INHERITED;
};