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