Reland "implement SkPixmap::getPixel() with this->readPixels()."
This is a reland of 0944ae8afb
Original change's description:
> implement SkPixmap::getPixel() with this->readPixels().
>
> Yet another big old switch statement I don't want to maintain.
>
> Once again, I think this at least makes it explicit that we're
> returning an SkColor in the colorspace of the pixmap.
>
> Change-Id: I34aa7312c0a1670910586fc5a4b1951275e73e2e
> Reviewed-on: https://skia-review.googlesource.com/154864
> Reviewed-by: Brian Osman <brianosman@google.com>
> Commit-Queue: Mike Klein <mtklein@google.com>
Change-Id: I07b0b11922a393072961edc7771d7a972ca1fd13
Reviewed-on: https://skia-review.googlesource.com/155166
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
This commit is contained in:
parent
beb8300d19
commit
1d6281d4bb
@ -350,98 +350,12 @@ SkColor SkPixmap::getColor(int x, int y) const {
|
|||||||
SkASSERT((unsigned)x < (unsigned)this->width());
|
SkASSERT((unsigned)x < (unsigned)this->width());
|
||||||
SkASSERT((unsigned)y < (unsigned)this->height());
|
SkASSERT((unsigned)y < (unsigned)this->height());
|
||||||
|
|
||||||
const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType());
|
SkColor result;
|
||||||
auto toColor = [needsUnpremul](uint32_t maybePremulColor) {
|
auto info = SkImageInfo::Make(1,1,
|
||||||
return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor)
|
kBGRA_8888_SkColorType,kUnpremul_SkAlphaType,
|
||||||
: SkSwizzle_BGRA_to_PMColor(maybePremulColor);
|
sk_ref_sp(this->colorSpace()));
|
||||||
};
|
SkAssertResult(this->readPixels(info, &result, sizeof(result), x,y));
|
||||||
|
return result;
|
||||||
switch (this->colorType()) {
|
|
||||||
case kGray_8_SkColorType: {
|
|
||||||
uint8_t value = *this->addr8(x, y);
|
|
||||||
return SkColorSetRGB(value, value, value);
|
|
||||||
}
|
|
||||||
case kAlpha_8_SkColorType: {
|
|
||||||
return SkColorSetA(0, *this->addr8(x, y));
|
|
||||||
}
|
|
||||||
case kRGB_565_SkColorType: {
|
|
||||||
return SkPixel16ToColor(*this->addr16(x, y));
|
|
||||||
}
|
|
||||||
case kARGB_4444_SkColorType: {
|
|
||||||
uint16_t value = *this->addr16(x, y);
|
|
||||||
SkPMColor c = SkPixel4444ToPixel32(value);
|
|
||||||
return toColor(c);
|
|
||||||
}
|
|
||||||
case kRGB_888x_SkColorType: {
|
|
||||||
uint32_t value = *this->addr32(x, y);
|
|
||||||
return SkSwizzle_RB(value | 0xff000000);
|
|
||||||
}
|
|
||||||
case kBGRA_8888_SkColorType: {
|
|
||||||
uint32_t value = *this->addr32(x, y);
|
|
||||||
SkPMColor c = SkSwizzle_BGRA_to_PMColor(value);
|
|
||||||
return toColor(c);
|
|
||||||
}
|
|
||||||
case kRGBA_8888_SkColorType: {
|
|
||||||
uint32_t value = *this->addr32(x, y);
|
|
||||||
SkPMColor c = SkSwizzle_RGBA_to_PMColor(value);
|
|
||||||
return toColor(c);
|
|
||||||
}
|
|
||||||
case kRGB_101010x_SkColorType: {
|
|
||||||
uint32_t value = *this->addr32(x, y);
|
|
||||||
// Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top.
|
|
||||||
return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16
|
|
||||||
| (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8
|
|
||||||
| (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0
|
|
||||||
| 0xff000000;
|
|
||||||
}
|
|
||||||
case kRGBA_1010102_SkColorType: {
|
|
||||||
uint32_t value = *this->addr32(x, y);
|
|
||||||
|
|
||||||
float r = ((value >> 0) & 0x3ff) * (1/1023.0f),
|
|
||||||
g = ((value >> 10) & 0x3ff) * (1/1023.0f),
|
|
||||||
b = ((value >> 20) & 0x3ff) * (1/1023.0f),
|
|
||||||
a = ((value >> 30) & 0x3 ) * (1/ 3.0f);
|
|
||||||
if (a != 0 && needsUnpremul) {
|
|
||||||
r *= (1.0f/a);
|
|
||||||
g *= (1.0f/a);
|
|
||||||
b *= (1.0f/a);
|
|
||||||
}
|
|
||||||
return (uint32_t)( r * 255.0f ) << 16
|
|
||||||
| (uint32_t)( g * 255.0f ) << 8
|
|
||||||
| (uint32_t)( b * 255.0f ) << 0
|
|
||||||
| (uint32_t)( a * 255.0f ) << 24;
|
|
||||||
}
|
|
||||||
case kRGBA_F16_SkColorType: {
|
|
||||||
const uint64_t* addr =
|
|
||||||
(const uint64_t*)fPixels + y * (fRowBytes >> 3) + x;
|
|
||||||
Sk4f p4 = SkHalfToFloat_finite_ftz(*addr);
|
|
||||||
if (p4[3] && needsUnpremul) {
|
|
||||||
float inva = 1 / p4[3];
|
|
||||||
p4 = p4 * Sk4f(inva, inva, inva, 1);
|
|
||||||
}
|
|
||||||
SkColor c;
|
|
||||||
SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
|
|
||||||
// p4 is RGBA, but we want BGRA, so we need to swap next
|
|
||||||
return SkSwizzle_RB(c);
|
|
||||||
}
|
|
||||||
case kRGBA_F32_SkColorType: {
|
|
||||||
const float* rgba =
|
|
||||||
(const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x;
|
|
||||||
Sk4f p4 = Sk4f::Load(rgba);
|
|
||||||
// From here on, just like F16:
|
|
||||||
if (p4[3] && needsUnpremul) {
|
|
||||||
float inva = 1 / p4[3];
|
|
||||||
p4 = p4 * Sk4f(inva, inva, inva, 1);
|
|
||||||
}
|
|
||||||
SkColor c;
|
|
||||||
SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
|
|
||||||
// p4 is RGBA, but we want BGRA, so we need to swap next
|
|
||||||
return SkSwizzle_RB(c);
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
SkDEBUGFAIL("");
|
|
||||||
return SkColorSetARGB(0, 0, 0, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SkPixmap::computeIsOpaque() const {
|
bool SkPixmap::computeIsOpaque() const {
|
||||||
|
Loading…
Reference in New Issue
Block a user