2017-01-17 15:48:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2017-02-07 19:09:38 +00:00
|
|
|
#ifndef SkImageInfoPriv_DEFINED
|
|
|
|
#define SkImageInfoPriv_DEFINED
|
|
|
|
|
|
|
|
#include "SkImageInfo.h"
|
|
|
|
|
2018-03-22 14:01:16 +00:00
|
|
|
enum SkColorTypeComponentFlag {
|
|
|
|
kRed_SkColorTypeComponentFlag = 0x1,
|
|
|
|
kGreen_SkColorTypeComponentFlag = 0x2,
|
|
|
|
kBlue_SkColorTypeComponentFlag = 0x4,
|
|
|
|
kAlpha_SkColorTypeComponentFlag = 0x8,
|
|
|
|
kGray_SkColorTypeComponentFlag = 0x10,
|
|
|
|
kRGB_SkColorTypeComponentFlags = kRed_SkColorTypeComponentFlag |
|
|
|
|
kGreen_SkColorTypeComponentFlag |
|
|
|
|
kBlue_SkColorTypeComponentFlag,
|
|
|
|
kRGBA_SkColorTypeComponentFlags = kRGB_SkColorTypeComponentFlags |
|
|
|
|
kAlpha_SkColorTypeComponentFlag,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline uint32_t SkColorTypeComponentFlags(SkColorType ct) {
|
|
|
|
switch (ct) {
|
2019-02-28 16:03:27 +00:00
|
|
|
case kUnknown_SkColorType: return 0;
|
|
|
|
case kAlpha_8_SkColorType: return kAlpha_SkColorTypeComponentFlag;
|
|
|
|
case kRGB_565_SkColorType: return kRGB_SkColorTypeComponentFlags;
|
|
|
|
case kARGB_4444_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
|
|
|
case kRGBA_8888_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
|
|
|
case kRGB_888x_SkColorType: return kRGB_SkColorTypeComponentFlags;
|
|
|
|
case kBGRA_8888_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
|
|
|
case kRGBA_1010102_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
|
|
|
case kRGB_101010x_SkColorType: return kRGB_SkColorTypeComponentFlags;
|
|
|
|
case kGray_8_SkColorType: return kGray_SkColorTypeComponentFlag;
|
|
|
|
case kRGBA_F16Norm_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
|
|
|
case kRGBA_F16_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
|
|
|
case kRGBA_F32_SkColorType: return kRGBA_SkColorTypeComponentFlags;
|
2018-03-22 14:01:16 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
|
|
|
|
return kAlpha_SkColorTypeComponentFlag == SkColorTypeComponentFlags(ct);
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:26:46 +00:00
|
|
|
static inline bool SkAlphaTypeIsValid(unsigned value) {
|
|
|
|
return value <= kLastEnum_SkAlphaType;
|
|
|
|
}
|
|
|
|
|
2018-03-22 14:01:16 +00:00
|
|
|
static inline bool SkColorTypeIsGray(SkColorType ct) {
|
|
|
|
auto flags = SkColorTypeComponentFlags(ct);
|
|
|
|
// Currently assuming that a color type has only gray or does not have gray.
|
|
|
|
SkASSERT(!(kGray_SkColorTypeComponentFlag & flags) || kGray_SkColorTypeComponentFlag == flags);
|
|
|
|
return kGray_SkColorTypeComponentFlag == flags;
|
|
|
|
}
|
|
|
|
|
2018-02-09 18:26:46 +00:00
|
|
|
static int SkColorTypeShiftPerPixel(SkColorType ct) {
|
|
|
|
switch (ct) {
|
2019-02-28 16:03:27 +00:00
|
|
|
case kUnknown_SkColorType: return 0;
|
|
|
|
case kAlpha_8_SkColorType: return 0;
|
|
|
|
case kRGB_565_SkColorType: return 1;
|
|
|
|
case kARGB_4444_SkColorType: return 1;
|
|
|
|
case kRGBA_8888_SkColorType: return 2;
|
|
|
|
case kRGB_888x_SkColorType: return 2;
|
|
|
|
case kBGRA_8888_SkColorType: return 2;
|
|
|
|
case kRGBA_1010102_SkColorType: return 2;
|
|
|
|
case kRGB_101010x_SkColorType: return 2;
|
|
|
|
case kGray_8_SkColorType: return 0;
|
|
|
|
case kRGBA_F16Norm_SkColorType: return 3;
|
|
|
|
case kRGBA_F16_SkColorType: return 3;
|
|
|
|
case kRGBA_F32_SkColorType: return 4;
|
2018-02-09 18:26:46 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
|
|
|
|
return width * SkColorTypeBytesPerPixel(ct);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool SkColorTypeIsValid(unsigned value) {
|
|
|
|
return value <= kLastEnum_SkColorType;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
|
|
|
|
if (kUnknown_SkColorType == ct) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:48:53 +00:00
|
|
|
/**
|
2018-06-04 13:21:17 +00:00
|
|
|
* Returns true if |info| contains a valid combination of width, height, colorType, and alphaType.
|
2017-01-17 15:48:53 +00:00
|
|
|
*/
|
2018-06-04 13:21:17 +00:00
|
|
|
static inline bool SkImageInfoIsValid(const SkImageInfo& info) {
|
2017-01-17 15:48:53 +00:00
|
|
|
if (info.width() <= 0 || info.height() <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-14 16:21:02 +00:00
|
|
|
const int kMaxDimension = SK_MaxS32 >> 2;
|
|
|
|
if (info.width() > kMaxDimension || info.height() > kMaxDimension) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:48:53 +00:00
|
|
|
if (kUnknown_SkColorType == info.colorType() || kUnknown_SkAlphaType == info.alphaType()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:04:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:48:53 +00:00
|
|
|
/**
|
|
|
|
* Returns true if Skia has defined a pixel conversion from the |src| to the |dst|.
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
* Returns false otherwise.
|
2017-01-17 15:48:53 +00:00
|
|
|
*/
|
|
|
|
static inline bool SkImageInfoValidConversion(const SkImageInfo& dst, const SkImageInfo& src) {
|
let's like, chill out about all these rules, man
There's really no reason to prevent any of these conversions;
they all have somewhat reasonable behavior:
- converting between grey in different color spaces
should probably work just fine
- we'll convert color to gray using a fixed set of
luminance coefficients, but that's better than failing
- we'll invent {r,g,b} = {0,0,0} if we convert alpha
to something with color
- converting to opaque formats without thinking about
premul/unpremul is probably fine, better than just
not working at all
- a missing src color space can always be assumed to be sRGB
Updates to ReadPixelsTest:
- skip more supported test cases in test_conversion(),
each with a TODO
- conversions from non-opaque to opaque should now work
- conversion from A8 to non-A8 should sometimes now
work on GPUs, and the test needed a little bit of
a tweak to not expect A8 to carry around color somehow.
Updates to SRGBReadWritePixelsTest:
- writing untagged pixels shouldn't fail anymore;
instead, it should behave like it was tagged sRGB
Change-Id: I19e78f3a6c89ef74fbcbc985d3fbd77fa984b1c2
Reviewed-on: https://skia-review.googlesource.com/147815
Commit-Queue: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
2018-08-17 18:09:55 +00:00
|
|
|
return SkImageInfoIsValid(dst) && SkImageInfoIsValid(src);
|
2017-01-17 15:48:53 +00:00
|
|
|
}
|
2017-02-07 19:09:38 +00:00
|
|
|
#endif // SkImageInfoPriv_DEFINED
|