reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkPixmap.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkSurface.h" |
| 13 | #include "include/core/SkUnPreMultiply.h" |
| 14 | #include "include/private/SkColorData.h" |
| 15 | #include "include/private/SkHalf.h" |
| 16 | #include "include/private/SkImageInfoPriv.h" |
| 17 | #include "include/private/SkNx.h" |
| 18 | #include "include/private/SkTemplates.h" |
| 19 | #include "include/private/SkTo.h" |
| 20 | #include "src/core/SkConvertPixels.h" |
| 21 | #include "src/core/SkDraw.h" |
| 22 | #include "src/core/SkMask.h" |
| 23 | #include "src/core/SkPixmapPriv.h" |
| 24 | #include "src/core/SkRasterClip.h" |
| 25 | #include "src/core/SkUtils.h" |
| 26 | #include "src/image/SkReadPixelsRec.h" |
| 27 | #include "src/shaders/SkImageShader.h" |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 28 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 29 | #include <utility> |
| 30 | |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 31 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 32 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 33 | void SkPixmap::reset() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | fPixels = nullptr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 35 | fRowBytes = 0; |
| 36 | fInfo = SkImageInfo::MakeUnknown(); |
| 37 | } |
| 38 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 39 | void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 40 | if (addr) { |
| 41 | SkASSERT(info.validRowBytes(rowBytes)); |
| 42 | } |
| 43 | fPixels = addr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 44 | fRowBytes = rowBytes; |
| 45 | fInfo = info; |
| 46 | } |
| 47 | |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 48 | bool SkPixmap::reset(const SkMask& src) { |
| 49 | if (SkMask::kA8_Format == src.fFormat) { |
| 50 | this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()), |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 51 | src.fImage, src.fRowBytes); |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | this->reset(); |
| 55 | return false; |
| 56 | } |
| 57 | |
msarett | 804b461 | 2016-06-09 11:03:45 -0700 | [diff] [blame] | 58 | void SkPixmap::setColorSpace(sk_sp<SkColorSpace> cs) { |
| 59 | fInfo = fInfo.makeColorSpace(std::move(cs)); |
| 60 | } |
| 61 | |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 62 | bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const { |
| 63 | SkIRect srcRect, r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 64 | srcRect.setWH(this->width(), this->height()); |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 65 | if (!r.intersect(srcRect, subset)) { |
| 66 | return false; // r is empty (i.e. no intersection) |
| 67 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 68 | |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 69 | // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have |
| 70 | // exited above. |
| 71 | SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width())); |
| 72 | SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height())); |
| 73 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | const void* pixels = nullptr; |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 75 | if (fPixels) { |
| 76 | const size_t bpp = fInfo.bytesPerPixel(); |
| 77 | pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp; |
| 78 | } |
Mike Reed | 26e9ddd | 2017-07-17 17:33:53 -0400 | [diff] [blame] | 79 | result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes); |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 83 | // This is the same as SkPixmap::addr(x,y), but this version gets inlined, while the public |
| 84 | // method does not. Perhaps we could bloat it so it can be inlined, but that would grow code-size |
| 85 | // everywhere, instead of just here (on behalf of getAlphaf()). |
| 86 | static const void* fast_getaddr(const SkPixmap& pm, int x, int y) { |
| 87 | x <<= SkColorTypeShiftPerPixel(pm.colorType()); |
| 88 | return static_cast<const char*>(pm.addr()) + y * pm.rowBytes() + x; |
| 89 | } |
| 90 | |
| 91 | float SkPixmap::getAlphaf(int x, int y) const { |
| 92 | SkASSERT(this->addr()); |
| 93 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 94 | SkASSERT((unsigned)y < (unsigned)this->height()); |
| 95 | |
| 96 | float value = 0; |
| 97 | const void* srcPtr = fast_getaddr(*this, x, y); |
| 98 | |
| 99 | switch (this->colorType()) { |
| 100 | case kUnknown_SkColorType: |
| 101 | return 0; |
| 102 | case kGray_8_SkColorType: |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 103 | case kR8G8_unorm_SkColorType: |
| 104 | case kR16G16_unorm_SkColorType: |
| 105 | case kR16G16_float_SkColorType: |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 106 | case kRGB_565_SkColorType: |
| 107 | case kRGB_888x_SkColorType: |
| 108 | case kRGB_101010x_SkColorType: |
| 109 | return 1; |
| 110 | case kAlpha_8_SkColorType: |
| 111 | value = static_cast<const uint8_t*>(srcPtr)[0] * (1.0f/255); |
| 112 | break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 113 | case kA16_unorm_SkColorType: |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 114 | value = static_cast<const uint16_t*>(srcPtr)[0] * (1.0f/65535); |
| 115 | break; |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 116 | case kA16_float_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 117 | SkHalf half = static_cast<const SkHalf*>(srcPtr)[0]; |
| 118 | value = SkHalfToFloat(half); |
| 119 | break; |
| 120 | } |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 121 | case kARGB_4444_SkColorType: { |
| 122 | uint16_t u16 = static_cast<const uint16_t*>(srcPtr)[0]; |
| 123 | value = SkGetPackedA4444(u16) * (1.0f/15); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 124 | break; |
| 125 | } |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 126 | case kRGBA_8888_SkColorType: |
| 127 | case kBGRA_8888_SkColorType: |
| 128 | value = static_cast<const uint8_t*>(srcPtr)[3] * (1.0f/255); |
| 129 | break; |
| 130 | case kRGBA_1010102_SkColorType: { |
| 131 | uint32_t u32 = static_cast<const uint32_t*>(srcPtr)[0]; |
| 132 | value = (u32 >> 30) * (1.0f/3); |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 133 | break; |
| 134 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 135 | case kR16G16B16A16_unorm_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 136 | uint64_t u64 = static_cast<const uint64_t*>(srcPtr)[0]; |
| 137 | value = (u64 >> 48) * (1.0f/65535); |
| 138 | break; |
| 139 | } |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 140 | case kRGBA_F16Norm_SkColorType: |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 141 | case kRGBA_F16_SkColorType: { |
| 142 | uint64_t px; |
| 143 | memcpy(&px, srcPtr, sizeof(px)); |
| 144 | value = SkHalfToFloat_finite_ftz(px)[3]; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 145 | break; |
| 146 | } |
Mike Reed | c25f440 | 2018-09-20 15:00:56 -0400 | [diff] [blame] | 147 | case kRGBA_F32_SkColorType: |
| 148 | value = static_cast<const float*>(srcPtr)[3]; |
| 149 | break; |
| 150 | } |
| 151 | return value; |
| 152 | } |
| 153 | |
Brian Osman | f6db495 | 2018-07-16 13:06:02 -0400 | [diff] [blame] | 154 | bool SkPixmap::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, |
| 155 | int x, int y) const { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 156 | if (!SkImageInfoValidConversion(dstInfo, fInfo)) { |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 159 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 160 | SkReadPixelsRec rec(dstInfo, dstPixels, dstRB, x, y); |
| 161 | if (!rec.trim(fInfo.width(), fInfo.height())) { |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 164 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 165 | const void* srcPixels = this->addr(rec.fX, rec.fY); |
| 166 | const SkImageInfo srcInfo = fInfo.makeWH(rec.fInfo.width(), rec.fInfo.height()); |
Brian Osman | b62f50c | 2018-07-12 14:44:27 -0400 | [diff] [blame] | 167 | SkConvertPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, srcInfo, srcPixels, this->rowBytes()); |
Matt Sarett | 8572d85 | 2017-02-14 11:21:02 -0500 | [diff] [blame] | 168 | return true; |
reed | 95d343f | 2015-05-23 13:21:06 -0700 | [diff] [blame] | 169 | } |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 170 | |
Mike Klein | 29a85bb | 2018-10-30 16:14:53 -0400 | [diff] [blame] | 171 | bool SkPixmap::erase(SkColor color, const SkIRect& subset) const { |
| 172 | return this->erase(SkColor4f::FromColor(color), &subset); |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mike Klein | 29a85bb | 2018-10-30 16:14:53 -0400 | [diff] [blame] | 175 | bool SkPixmap::erase(const SkColor4f& color, const SkIRect* subset) const { |
| 176 | SkPaint paint; |
| 177 | paint.setBlendMode(SkBlendMode::kSrc); |
Robert Phillips | 832c931 | 2019-01-08 13:04:10 +0000 | [diff] [blame] | 178 | paint.setColor4f(color, this->colorSpace()); |
Mike Klein | 29a85bb | 2018-10-30 16:14:53 -0400 | [diff] [blame] | 179 | |
| 180 | SkIRect clip = this->bounds(); |
| 181 | if (subset && !clip.intersect(*subset)) { |
Mike Klein | 9ec77b5 | 2018-09-18 12:19:57 +0000 | [diff] [blame] | 182 | return false; |
| 183 | } |
Mike Klein | 29a85bb | 2018-10-30 16:14:53 -0400 | [diff] [blame] | 184 | SkRasterClip rc{clip}; |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 185 | |
Mike Klein | 29a85bb | 2018-10-30 16:14:53 -0400 | [diff] [blame] | 186 | SkDraw draw; |
| 187 | draw.fDst = *this; |
| 188 | draw.fMatrix = &SkMatrix::I(); |
| 189 | draw.fRC = &rc; |
reed | 7aefe03 | 2015-06-08 10:22:22 -0700 | [diff] [blame] | 190 | |
Mike Klein | 29a85bb | 2018-10-30 16:14:53 -0400 | [diff] [blame] | 191 | draw.drawPaint(paint); |
Mike Klein | 9ec77b5 | 2018-09-18 12:19:57 +0000 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 195 | bool SkPixmap::scalePixels(const SkPixmap& actualDst, SkFilterQuality quality) const { |
| 196 | // We may need to tweak how we interpret these just a little below, so we make copies. |
| 197 | SkPixmap src = *this, |
| 198 | dst = actualDst; |
Mike Klein | 47cf048 | 2018-02-09 18:57:54 +0000 | [diff] [blame] | 199 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 200 | // Can't do anthing with empty src or dst |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 201 | if (src.width() <= 0 || src.height() <= 0 || |
| 202 | dst.width() <= 0 || dst.height() <= 0) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
| 206 | // no scaling involved? |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 207 | if (src.width() == dst.width() && src.height() == dst.height()) { |
| 208 | return src.readPixels(dst); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Mike Klein | fc9624c | 2018-09-06 11:25:21 -0400 | [diff] [blame] | 211 | // If src and dst are both unpremul, we'll fake the source out to appear as if premul, |
| 212 | // and mark the destination as opaque. This odd combination allows us to scale unpremul |
| 213 | // pixels without ever premultiplying them (perhaps losing information in the color channels). |
| 214 | // This is an idiosyncratic feature of scalePixels(), and is tested by scalepixels_unpremul GM. |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 215 | bool clampAsIfUnpremul = false; |
| 216 | if (src.alphaType() == kUnpremul_SkAlphaType && |
| 217 | dst.alphaType() == kUnpremul_SkAlphaType) { |
| 218 | src.reset(src.info().makeAlphaType(kPremul_SkAlphaType), src.addr(), src.rowBytes()); |
Mike Klein | fc9624c | 2018-09-06 11:25:21 -0400 | [diff] [blame] | 219 | dst.reset(dst.info().makeAlphaType(kOpaque_SkAlphaType), dst.addr(), dst.rowBytes()); |
Mike Klein | 47cf048 | 2018-02-09 18:57:54 +0000 | [diff] [blame] | 220 | |
Mike Klein | fc9624c | 2018-09-06 11:25:21 -0400 | [diff] [blame] | 221 | // We'll need to tell the image shader to clamp to [0,1] instead of the |
| 222 | // usual [0,a] when using a bicubic scaling (kHigh_SkFilterQuality). |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 223 | clampAsIfUnpremul = true; |
Mike Klein | 47cf048 | 2018-02-09 18:57:54 +0000 | [diff] [blame] | 224 | } |
| 225 | |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 226 | SkBitmap bitmap; |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 227 | if (!bitmap.installPixels(src)) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 230 | bitmap.setImmutable(); // Don't copy when we create an image. |
| 231 | bitmap.setIsVolatile(true); // Disable any caching. |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 232 | |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 233 | SkMatrix scale = SkMatrix::MakeRectToRect(SkRect::Make(src.bounds()), |
| 234 | SkRect::Make(dst.bounds()), |
| 235 | SkMatrix::kFill_ScaleToFit); |
| 236 | |
| 237 | // We'll create a shader to do this draw so we have control over the bicubic clamp. |
| 238 | sk_sp<SkShader> shader = SkImageShader::Make(SkImage::MakeFromBitmap(bitmap), |
Mike Reed | e25b447 | 2019-04-02 17:49:12 -0400 | [diff] [blame] | 239 | SkTileMode::kClamp, |
| 240 | SkTileMode::kClamp, |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 241 | &scale, |
| 242 | clampAsIfUnpremul); |
| 243 | |
| 244 | sk_sp<SkSurface> surface = SkSurface::MakeRasterDirect(dst.info(), |
| 245 | dst.writable_addr(), |
| 246 | dst.rowBytes()); |
| 247 | if (!shader || !surface) { |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | |
| 251 | SkPaint paint; |
Mike Klein | 47cf048 | 2018-02-09 18:57:54 +0000 | [diff] [blame] | 252 | paint.setBlendMode(SkBlendMode::kSrc); |
Mike Klein | 1f31309 | 2018-01-03 10:30:21 -0500 | [diff] [blame] | 253 | paint.setFilterQuality(quality); |
| 254 | paint.setShader(std::move(shader)); |
| 255 | surface->getCanvas()->drawPaint(paint); |
reed | 0955303 | 2015-11-23 12:32:16 -0800 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | |
reed | 183b57f | 2015-06-05 14:33:17 -0700 | [diff] [blame] | 259 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
Hal Canary | 94e1a2f | 2016-10-31 09:38:12 -0400 | [diff] [blame] | 260 | |
| 261 | SkColor SkPixmap::getColor(int x, int y) const { |
| 262 | SkASSERT(this->addr()); |
| 263 | SkASSERT((unsigned)x < (unsigned)this->width()); |
| 264 | SkASSERT((unsigned)y < (unsigned)this->height()); |
Matt Sarett | 9466bf5 | 2017-06-09 09:54:55 -0400 | [diff] [blame] | 265 | |
Mike Klein | 9e7f504 | 2018-09-19 14:39:25 +0000 | [diff] [blame] | 266 | const bool needsUnpremul = (kPremul_SkAlphaType == fInfo.alphaType()); |
| 267 | auto toColor = [needsUnpremul](uint32_t maybePremulColor) { |
| 268 | return needsUnpremul ? SkUnPreMultiply::PMColorToColor(maybePremulColor) |
| 269 | : SkSwizzle_BGRA_to_PMColor(maybePremulColor); |
| 270 | }; |
| 271 | |
| 272 | switch (this->colorType()) { |
| 273 | case kGray_8_SkColorType: { |
| 274 | uint8_t value = *this->addr8(x, y); |
| 275 | return SkColorSetRGB(value, value, value); |
| 276 | } |
| 277 | case kAlpha_8_SkColorType: { |
| 278 | return SkColorSetA(0, *this->addr8(x, y)); |
| 279 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 280 | case kA16_unorm_SkColorType: { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 281 | uint16_t value = *this->addr16(x, y); |
| 282 | return SkColorSetA(0, value * (255 / 65535.0f)); |
| 283 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 284 | case kA16_float_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 285 | SkHalf value = *this->addr16(x, y); |
| 286 | return SkColorSetA(0, 255 * SkHalfToFloat(value)); |
| 287 | } |
Mike Klein | 9e7f504 | 2018-09-19 14:39:25 +0000 | [diff] [blame] | 288 | case kRGB_565_SkColorType: { |
| 289 | return SkPixel16ToColor(*this->addr16(x, y)); |
| 290 | } |
| 291 | case kARGB_4444_SkColorType: { |
| 292 | uint16_t value = *this->addr16(x, y); |
| 293 | SkPMColor c = SkPixel4444ToPixel32(value); |
| 294 | return toColor(c); |
| 295 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 296 | case kR8G8_unorm_SkColorType: { |
Robert Phillips | d470e1b | 2019-09-04 15:05:35 -0400 | [diff] [blame] | 297 | uint16_t value = *this->addr16(x, y); |
| 298 | return (uint32_t)( ((value >> 0) & 0xff) ) << 16 |
| 299 | | (uint32_t)( ((value >> 8) & 0xff) ) << 8 |
| 300 | | 0xff000000; |
| 301 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 302 | case kR16G16_unorm_SkColorType: { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 303 | uint32_t value = *this->addr32(x, y); |
| 304 | return (uint32_t)( ((value >> 0) & 0xffff) * (255/65535.0f) ) << 16 |
| 305 | | (uint32_t)( ((value >> 16) & 0xffff) * (255/65535.0f) ) << 8 |
| 306 | | 0xff000000; |
| 307 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 308 | case kR16G16_float_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 309 | uint32_t value = *this->addr32(x, y); |
| 310 | uint32_t r = 255 * SkHalfToFloat((value >> 0) & 0xffff); |
| 311 | uint32_t g = 255 * SkHalfToFloat((value >> 16) & 0xffff); |
| 312 | return (r << 16) | (g << 8) | 0xff000000; |
| 313 | } |
Mike Klein | 9e7f504 | 2018-09-19 14:39:25 +0000 | [diff] [blame] | 314 | case kRGB_888x_SkColorType: { |
| 315 | uint32_t value = *this->addr32(x, y); |
| 316 | return SkSwizzle_RB(value | 0xff000000); |
| 317 | } |
| 318 | case kBGRA_8888_SkColorType: { |
| 319 | uint32_t value = *this->addr32(x, y); |
| 320 | SkPMColor c = SkSwizzle_BGRA_to_PMColor(value); |
| 321 | return toColor(c); |
| 322 | } |
| 323 | case kRGBA_8888_SkColorType: { |
| 324 | uint32_t value = *this->addr32(x, y); |
| 325 | SkPMColor c = SkSwizzle_RGBA_to_PMColor(value); |
| 326 | return toColor(c); |
| 327 | } |
| 328 | case kRGB_101010x_SkColorType: { |
| 329 | uint32_t value = *this->addr32(x, y); |
| 330 | // Convert 10-bit rgb to 8-bit bgr, and mask in 0xff alpha at the top. |
| 331 | return (uint32_t)( ((value >> 0) & 0x3ff) * (255/1023.0f) ) << 16 |
| 332 | | (uint32_t)( ((value >> 10) & 0x3ff) * (255/1023.0f) ) << 8 |
| 333 | | (uint32_t)( ((value >> 20) & 0x3ff) * (255/1023.0f) ) << 0 |
| 334 | | 0xff000000; |
| 335 | } |
| 336 | case kRGBA_1010102_SkColorType: { |
| 337 | uint32_t value = *this->addr32(x, y); |
| 338 | |
| 339 | float r = ((value >> 0) & 0x3ff) * (1/1023.0f), |
| 340 | g = ((value >> 10) & 0x3ff) * (1/1023.0f), |
| 341 | b = ((value >> 20) & 0x3ff) * (1/1023.0f), |
| 342 | a = ((value >> 30) & 0x3 ) * (1/ 3.0f); |
| 343 | if (a != 0 && needsUnpremul) { |
Robert Phillips | 7f36798 | 2019-09-26 14:01:36 -0400 | [diff] [blame^] | 344 | r = SkTPin(r/a, 0.0f, 1.0f); |
| 345 | g = SkTPin(g/a, 0.0f, 1.0f); |
| 346 | b = SkTPin(b/a, 0.0f, 1.0f); |
Mike Klein | 9e7f504 | 2018-09-19 14:39:25 +0000 | [diff] [blame] | 347 | } |
| 348 | return (uint32_t)( r * 255.0f ) << 16 |
| 349 | | (uint32_t)( g * 255.0f ) << 8 |
| 350 | | (uint32_t)( b * 255.0f ) << 0 |
| 351 | | (uint32_t)( a * 255.0f ) << 24; |
| 352 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 353 | case kR16G16B16A16_unorm_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 354 | uint64_t value = *this->addr64(x, y); |
| 355 | |
| 356 | float r = ((value ) & 0xffff) * (1/65535.0f), |
| 357 | g = ((value >> 16) & 0xffff) * (1/65535.0f), |
| 358 | b = ((value >> 32) & 0xffff) * (1/65535.0f), |
| 359 | a = ((value >> 48) & 0xffff) * (1/65535.0f); |
| 360 | if (a != 0 && needsUnpremul) { |
| 361 | r *= (1.0f/a); |
| 362 | g *= (1.0f/a); |
| 363 | b *= (1.0f/a); |
| 364 | } |
| 365 | return (uint32_t)( r * 255.0f ) << 16 |
| 366 | | (uint32_t)( g * 255.0f ) << 8 |
| 367 | | (uint32_t)( b * 255.0f ) << 0 |
| 368 | | (uint32_t)( a * 255.0f ) << 24; |
| 369 | } |
Brian Osman | 1132f74 | 2019-03-07 14:03:58 -0500 | [diff] [blame] | 370 | case kRGBA_F16Norm_SkColorType: |
Mike Klein | 9e7f504 | 2018-09-19 14:39:25 +0000 | [diff] [blame] | 371 | case kRGBA_F16_SkColorType: { |
| 372 | const uint64_t* addr = |
| 373 | (const uint64_t*)fPixels + y * (fRowBytes >> 3) + x; |
| 374 | Sk4f p4 = SkHalfToFloat_finite_ftz(*addr); |
| 375 | if (p4[3] && needsUnpremul) { |
| 376 | float inva = 1 / p4[3]; |
| 377 | p4 = p4 * Sk4f(inva, inva, inva, 1); |
| 378 | } |
| 379 | SkColor c; |
| 380 | SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c); |
| 381 | // p4 is RGBA, but we want BGRA, so we need to swap next |
| 382 | return SkSwizzle_RB(c); |
| 383 | } |
| 384 | case kRGBA_F32_SkColorType: { |
| 385 | const float* rgba = |
| 386 | (const float*)fPixels + 4*y*(fRowBytes >> 4) + 4*x; |
| 387 | Sk4f p4 = Sk4f::Load(rgba); |
| 388 | // From here on, just like F16: |
| 389 | if (p4[3] && needsUnpremul) { |
| 390 | float inva = 1 / p4[3]; |
| 391 | p4 = p4 * Sk4f(inva, inva, inva, 1); |
| 392 | } |
| 393 | SkColor c; |
| 394 | SkNx_cast<uint8_t>(p4 * Sk4f(255) + Sk4f(0.5f)).store(&c); |
| 395 | // p4 is RGBA, but we want BGRA, so we need to swap next |
| 396 | return SkSwizzle_RB(c); |
| 397 | } |
| 398 | default: |
| 399 | SkDEBUGFAIL(""); |
| 400 | return SkColorSetARGB(0, 0, 0, 0); |
| 401 | } |
Hal Canary | 94e1a2f | 2016-10-31 09:38:12 -0400 | [diff] [blame] | 402 | } |
Hal Canary | 58a7694 | 2016-12-07 15:24:59 -0500 | [diff] [blame] | 403 | |
| 404 | bool SkPixmap::computeIsOpaque() const { |
| 405 | const int height = this->height(); |
| 406 | const int width = this->width(); |
| 407 | |
| 408 | switch (this->colorType()) { |
| 409 | case kAlpha_8_SkColorType: { |
| 410 | unsigned a = 0xFF; |
| 411 | for (int y = 0; y < height; ++y) { |
| 412 | const uint8_t* row = this->addr8(0, y); |
| 413 | for (int x = 0; x < width; ++x) { |
| 414 | a &= row[x]; |
| 415 | } |
| 416 | if (0xFF != a) { |
| 417 | return false; |
| 418 | } |
| 419 | } |
| 420 | return true; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 421 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 422 | case kA16_unorm_SkColorType: { |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 423 | unsigned a = 0xFFFF; |
| 424 | for (int y = 0; y < height; ++y) { |
| 425 | const uint16_t* row = this->addr16(0, y); |
| 426 | for (int x = 0; x < width; ++x) { |
| 427 | a &= row[x]; |
| 428 | } |
| 429 | if (0xFFFF != a) { |
| 430 | return false; |
| 431 | } |
| 432 | } |
| 433 | return true; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 434 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 435 | case kA16_float_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 436 | for (int y = 0; y < height; ++y) { |
| 437 | const SkHalf* row = this->addr16(0, y); |
| 438 | for (int x = 0; x < width; ++x) { |
| 439 | if (row[x] < SK_Half1) { |
| 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | return true; |
| 445 | } |
Hal Canary | 58a7694 | 2016-12-07 15:24:59 -0500 | [diff] [blame] | 446 | case kRGB_565_SkColorType: |
| 447 | case kGray_8_SkColorType: |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 448 | case kR8G8_unorm_SkColorType: |
| 449 | case kR16G16_unorm_SkColorType: |
| 450 | case kR16G16_float_SkColorType: |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 451 | case kRGB_888x_SkColorType: |
| 452 | case kRGB_101010x_SkColorType: |
Hal Canary | 58a7694 | 2016-12-07 15:24:59 -0500 | [diff] [blame] | 453 | return true; |
| 454 | break; |
| 455 | case kARGB_4444_SkColorType: { |
| 456 | unsigned c = 0xFFFF; |
| 457 | for (int y = 0; y < height; ++y) { |
| 458 | const SkPMColor16* row = this->addr16(0, y); |
| 459 | for (int x = 0; x < width; ++x) { |
| 460 | c &= row[x]; |
| 461 | } |
| 462 | if (0xF != SkGetPackedA4444(c)) { |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | return true; |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 467 | } |
Hal Canary | 58a7694 | 2016-12-07 15:24:59 -0500 | [diff] [blame] | 468 | case kBGRA_8888_SkColorType: |
| 469 | case kRGBA_8888_SkColorType: { |
| 470 | SkPMColor c = (SkPMColor)~0; |
| 471 | for (int y = 0; y < height; ++y) { |
| 472 | const SkPMColor* row = this->addr32(0, y); |
| 473 | for (int x = 0; x < width; ++x) { |
| 474 | c &= row[x]; |
| 475 | } |
| 476 | if (0xFF != SkGetPackedA32(c)) { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | return true; |
| 481 | } |
Mike Klein | b70990e | 2019-02-28 10:03:27 -0600 | [diff] [blame] | 482 | case kRGBA_F16Norm_SkColorType: |
Hal Canary | 58a7694 | 2016-12-07 15:24:59 -0500 | [diff] [blame] | 483 | case kRGBA_F16_SkColorType: { |
| 484 | const SkHalf* row = (const SkHalf*)this->addr(); |
| 485 | for (int y = 0; y < height; ++y) { |
| 486 | for (int x = 0; x < width; ++x) { |
| 487 | if (row[4 * x + 3] < SK_Half1) { |
| 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | row += this->rowBytes() >> 1; |
| 492 | } |
| 493 | return true; |
| 494 | } |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 495 | case kRGBA_F32_SkColorType: { |
| 496 | const float* row = (const float*)this->addr(); |
| 497 | for (int y = 0; y < height; ++y) { |
| 498 | for (int x = 0; x < width; ++x) { |
| 499 | if (row[4 * x + 3] < 1.0f) { |
| 500 | return false; |
| 501 | } |
| 502 | } |
| 503 | row += this->rowBytes() >> 2; |
| 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | case kRGBA_1010102_SkColorType: { |
| 508 | uint32_t c = ~0; |
| 509 | for (int y = 0; y < height; ++y) { |
| 510 | const uint32_t* row = this->addr32(0, y); |
| 511 | for (int x = 0; x < width; ++x) { |
| 512 | c &= row[x]; |
| 513 | } |
| 514 | if (0b11 != c >> 30) { |
| 515 | return false; |
| 516 | } |
| 517 | } |
| 518 | return true; |
| 519 | } |
Robert Phillips | ea1b30b | 2019-09-19 16:05:48 -0400 | [diff] [blame] | 520 | case kR16G16B16A16_unorm_SkColorType: { |
Robert Phillips | 17a3a0b | 2019-09-18 13:56:54 -0400 | [diff] [blame] | 521 | uint16_t acc = 0xFFFF; |
| 522 | for (int y = 0; y < height; ++y) { |
| 523 | const uint64_t* row = this->addr64(0, y); |
| 524 | for (int x = 0; x < width; ++x) { |
| 525 | acc &= (row[x] >> 48); |
| 526 | } |
| 527 | if (0xFFFF != acc) { |
| 528 | return false; |
| 529 | } |
| 530 | } |
| 531 | return true; |
| 532 | } |
Brian Osman | 2091fbb | 2018-10-11 11:05:37 -0400 | [diff] [blame] | 533 | case kUnknown_SkColorType: |
| 534 | SkDEBUGFAIL(""); |
Hal Canary | 58a7694 | 2016-12-07 15:24:59 -0500 | [diff] [blame] | 535 | break; |
| 536 | } |
| 537 | return false; |
| 538 | } |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 539 | |
| 540 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 541 | |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 542 | static bool draw_orientation(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) { |
Mike Reed | 7306bcd | 2017-10-25 10:37:30 -0400 | [diff] [blame] | 543 | auto surf = SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes()); |
| 544 | if (!surf) { |
| 545 | return false; |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 546 | } |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 547 | |
Mike Reed | 7306bcd | 2017-10-25 10:37:30 -0400 | [diff] [blame] | 548 | SkBitmap bm; |
| 549 | bm.installPixels(src); |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 550 | |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 551 | SkMatrix m = SkEncodedOriginToMatrix(origin, src.width(), src.height()); |
Mike Reed | 7306bcd | 2017-10-25 10:37:30 -0400 | [diff] [blame] | 552 | |
Mike Reed | 7306bcd | 2017-10-25 10:37:30 -0400 | [diff] [blame] | 553 | SkPaint p; |
| 554 | p.setBlendMode(SkBlendMode::kSrc); |
| 555 | surf->getCanvas()->concat(m); |
| 556 | surf->getCanvas()->drawBitmap(bm, 0, 0, &p); |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 560 | bool SkPixmapPriv::Orient(const SkPixmap& dst, const SkPixmap& src, SkEncodedOrigin origin) { |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 561 | if (src.colorType() != dst.colorType()) { |
| 562 | return false; |
| 563 | } |
| 564 | // note: we just ignore alphaType and colorSpace for this transformation |
| 565 | |
| 566 | int w = src.width(); |
| 567 | int h = src.height(); |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 568 | if (ShouldSwapWidthHeight(origin)) { |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 569 | using std::swap; |
| 570 | swap(w, h); |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 571 | } |
| 572 | if (dst.width() != w || dst.height() != h) { |
| 573 | return false; |
| 574 | } |
| 575 | if (w == 0 || h == 0) { |
| 576 | return true; |
| 577 | } |
| 578 | |
| 579 | // check for aliasing to self |
| 580 | if (src.addr() == dst.addr()) { |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 581 | return kTopLeft_SkEncodedOrigin == origin; |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 582 | } |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 583 | return draw_orientation(dst, src, origin); |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Brian Osman | c337a63 | 2018-11-30 10:39:32 -0500 | [diff] [blame] | 586 | bool SkPixmapPriv::ShouldSwapWidthHeight(SkEncodedOrigin origin) { |
| 587 | // The last four SkEncodedOrigin values involve 90 degree rotations |
| 588 | return origin >= kLeftTop_SkEncodedOrigin; |
Leon Scroggins III | 0cbc10f | 2017-10-30 09:07:53 -0400 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | SkImageInfo SkPixmapPriv::SwapWidthHeight(const SkImageInfo& info) { |
| 592 | return info.makeWH(info.height(), info.width()); |
| 593 | } |
| 594 | |