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
|
|
|
|
|
2020-03-26 20:17:56 +00:00
|
|
|
#include "include/core/SkColor.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
2017-02-07 19:09:38 +00:00
|
|
|
|
2020-03-26 20:17:56 +00:00
|
|
|
static inline uint32_t SkColorTypeChannelFlags(SkColorType ct) {
|
2018-03-22 14:01:16 +00:00
|
|
|
switch (ct) {
|
2019-09-19 20:05:48 +00:00
|
|
|
case kUnknown_SkColorType: return 0;
|
2020-03-26 20:17:56 +00:00
|
|
|
case kAlpha_8_SkColorType: return kAlpha_SkColorChannelFlag;
|
|
|
|
case kRGB_565_SkColorType: return kRGB_SkColorChannelFlags;
|
|
|
|
case kARGB_4444_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kRGBA_8888_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kRGB_888x_SkColorType: return kRGB_SkColorChannelFlags;
|
|
|
|
case kBGRA_8888_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kRGBA_1010102_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kRGB_101010x_SkColorType: return kRGB_SkColorChannelFlags;
|
|
|
|
case kBGRA_1010102_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kBGR_101010x_SkColorType: return kRGB_SkColorChannelFlags;
|
|
|
|
case kGray_8_SkColorType: return kGray_SkColorChannelFlag;
|
|
|
|
case kRGBA_F16Norm_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kRGBA_F16_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kRGBA_F32_SkColorType: return kRGBA_SkColorChannelFlags;
|
|
|
|
case kR8G8_unorm_SkColorType: return kRG_SkColorChannelFlags;
|
|
|
|
case kA16_unorm_SkColorType: return kAlpha_SkColorChannelFlag;
|
|
|
|
case kR16G16_unorm_SkColorType: return kRG_SkColorChannelFlags;
|
|
|
|
case kA16_float_SkColorType: return kAlpha_SkColorChannelFlag;
|
|
|
|
case kR16G16_float_SkColorType: return kRG_SkColorChannelFlags;
|
|
|
|
case kR16G16B16A16_unorm_SkColorType: return kRGBA_SkColorChannelFlags;
|
2018-03-22 14:01:16 +00:00
|
|
|
}
|
2019-09-04 19:05:35 +00:00
|
|
|
SkUNREACHABLE;
|
2018-03-22 14:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool SkColorTypeIsAlphaOnly(SkColorType ct) {
|
2020-03-26 20:17:56 +00:00
|
|
|
return SkColorTypeChannelFlags(ct) == kAlpha_SkColorChannelFlag;
|
2018-03-22 14:01:16 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:26:46 +00:00
|
|
|
static inline bool SkAlphaTypeIsValid(unsigned value) {
|
|
|
|
return value <= kLastEnum_SkAlphaType;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int SkColorTypeShiftPerPixel(SkColorType ct) {
|
|
|
|
switch (ct) {
|
2019-09-19 20:05:48 +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;
|
2020-02-11 18:19:08 +00:00
|
|
|
case kBGRA_1010102_SkColorType: return 2;
|
|
|
|
case kBGR_101010x_SkColorType: return 2;
|
2019-09-19 20:05:48 +00:00
|
|
|
case kGray_8_SkColorType: return 0;
|
|
|
|
case kRGBA_F16Norm_SkColorType: return 3;
|
|
|
|
case kRGBA_F16_SkColorType: return 3;
|
|
|
|
case kRGBA_F32_SkColorType: return 4;
|
|
|
|
case kR8G8_unorm_SkColorType: return 1;
|
|
|
|
case kA16_unorm_SkColorType: return 1;
|
|
|
|
case kR16G16_unorm_SkColorType: return 2;
|
|
|
|
case kA16_float_SkColorType: return 1;
|
|
|
|
case kR16G16_float_SkColorType: return 2;
|
|
|
|
case kR16G16B16A16_unorm_SkColorType: return 3;
|
2018-02-09 18:26:46 +00:00
|
|
|
}
|
2019-09-04 19:05:35 +00:00
|
|
|
SkUNREACHABLE;
|
2018-02-09 18:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
|
2020-06-01 20:50:19 +00:00
|
|
|
return (size_t)(width * SkColorTypeBytesPerPixel(ct));
|
2018-02-09 18:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-06-01 20:50:19 +00:00
|
|
|
return (size_t)y * rowBytes + ((size_t)x << SkColorTypeShiftPerPixel(ct));
|
2018-02-09 18:26:46 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 16:42:13 +00:00
|
|
|
static inline bool SkColorTypeIsNormalized(SkColorType ct) {
|
|
|
|
switch (ct) {
|
|
|
|
case kUnknown_SkColorType:
|
|
|
|
case kAlpha_8_SkColorType:
|
|
|
|
case kRGB_565_SkColorType:
|
|
|
|
case kARGB_4444_SkColorType:
|
|
|
|
case kRGBA_8888_SkColorType:
|
|
|
|
case kRGB_888x_SkColorType:
|
|
|
|
case kBGRA_8888_SkColorType:
|
|
|
|
case kRGBA_1010102_SkColorType:
|
|
|
|
case kRGB_101010x_SkColorType:
|
2020-02-11 18:19:08 +00:00
|
|
|
case kBGRA_1010102_SkColorType:
|
|
|
|
case kBGR_101010x_SkColorType:
|
2019-11-08 16:42:13 +00:00
|
|
|
case kGray_8_SkColorType:
|
|
|
|
case kRGBA_F16Norm_SkColorType:
|
|
|
|
case kR8G8_unorm_SkColorType:
|
|
|
|
case kA16_unorm_SkColorType:
|
|
|
|
case kA16_float_SkColorType: /*subtle... alpha is always [0,1]*/
|
|
|
|
case kR16G16_unorm_SkColorType:
|
|
|
|
case kR16G16B16A16_unorm_SkColorType: return true;
|
|
|
|
|
|
|
|
case kRGBA_F16_SkColorType:
|
|
|
|
case kRGBA_F32_SkColorType:
|
|
|
|
case kR16G16_float_SkColorType: return false;
|
|
|
|
}
|
|
|
|
SkUNREACHABLE;
|
|
|
|
}
|
|
|
|
|
2017-01-17 15:48:53 +00:00
|
|
|
/**
|
2020-03-27 15:18:08 +00:00
|
|
|
* Returns true if |info| contains a valid colorType and alphaType.
|
|
|
|
*/
|
|
|
|
static inline bool SkColorInfoIsValid(const SkColorInfo& info) {
|
|
|
|
return info.colorType() != kUnknown_SkColorType && info.alphaType() != kUnknown_SkAlphaType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if |info| contains a valid combination of width, height and colorInfo.
|
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;
|
|
|
|
}
|
|
|
|
|
2020-03-27 15:18:08 +00:00
|
|
|
return SkColorInfoIsValid(info.colorInfo());
|
2017-05-02 20:04:56 +00:00
|
|
|
}
|
|
|
|
|
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
|