blob: a5e474b6d4d8c0ec4658c82b00af005bfabb7b58 [file] [log] [blame]
reed92fc2ae2015-05-22 08:06:21 -07001/*
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
reed7aefe032015-06-08 10:22:22 -07008#include "SkColorPriv.h"
reed95d343f2015-05-23 13:21:06 -07009#include "SkConfig8888.h"
fmalita3a94c6c2016-02-04 13:09:59 -080010#include "SkData.h"
reed183b57f2015-06-05 14:33:17 -070011#include "SkMask.h"
reed92fc2ae2015-05-22 08:06:21 -070012#include "SkPixmap.h"
reed7aefe032015-06-08 10:22:22 -070013#include "SkUtils.h"
reeddd9ffea2016-02-18 12:39:14 -080014#include "SkPM4f.h"
reed92fc2ae2015-05-22 08:06:21 -070015
16void SkAutoPixmapUnlock::reset(const SkPixmap& pm, void (*unlock)(void*), void* ctx) {
halcanary96fcdcc2015-08-27 07:41:13 -070017 SkASSERT(pm.addr() != nullptr);
reed92fc2ae2015-05-22 08:06:21 -070018
19 this->unlock();
20 fPixmap = pm;
21 fUnlockProc = unlock;
22 fUnlockContext = ctx;
23 fIsLocked = true;
24}
reed95d343f2015-05-23 13:21:06 -070025
26/////////////////////////////////////////////////////////////////////////////////////////////////
27
reed884e97c2015-05-26 11:31:54 -070028void SkPixmap::reset() {
halcanary96fcdcc2015-08-27 07:41:13 -070029 fPixels = nullptr;
30 fCTable = nullptr;
reed884e97c2015-05-26 11:31:54 -070031 fRowBytes = 0;
32 fInfo = SkImageInfo::MakeUnknown();
33}
34
35void SkPixmap::reset(const SkImageInfo& info, const void* addr, size_t rowBytes, SkColorTable* ct) {
36 if (addr) {
37 SkASSERT(info.validRowBytes(rowBytes));
38 }
39 fPixels = addr;
40 fCTable = ct;
41 fRowBytes = rowBytes;
42 fInfo = info;
43}
44
reed183b57f2015-06-05 14:33:17 -070045bool SkPixmap::reset(const SkMask& src) {
46 if (SkMask::kA8_Format == src.fFormat) {
47 this->reset(SkImageInfo::MakeA8(src.fBounds.width(), src.fBounds.height()),
halcanary96fcdcc2015-08-27 07:41:13 -070048 src.fImage, src.fRowBytes, nullptr);
reed183b57f2015-06-05 14:33:17 -070049 return true;
50 }
51 this->reset();
52 return false;
53}
54
55bool SkPixmap::extractSubset(SkPixmap* result, const SkIRect& subset) const {
56 SkIRect srcRect, r;
57 srcRect.set(0, 0, this->width(), this->height());
58 if (!r.intersect(srcRect, subset)) {
59 return false; // r is empty (i.e. no intersection)
60 }
halcanary9d524f22016-03-29 09:03:52 -070061
reed183b57f2015-06-05 14:33:17 -070062 // If the upper left of the rectangle was outside the bounds of this SkBitmap, we should have
63 // exited above.
64 SkASSERT(static_cast<unsigned>(r.fLeft) < static_cast<unsigned>(this->width()));
65 SkASSERT(static_cast<unsigned>(r.fTop) < static_cast<unsigned>(this->height()));
66
halcanary96fcdcc2015-08-27 07:41:13 -070067 const void* pixels = nullptr;
reed183b57f2015-06-05 14:33:17 -070068 if (fPixels) {
69 const size_t bpp = fInfo.bytesPerPixel();
70 pixels = (const uint8_t*)fPixels + r.fTop * fRowBytes + r.fLeft * bpp;
71 }
72 result->reset(fInfo.makeWH(r.width(), r.height()), pixels, fRowBytes, fCTable);
73 return true;
74}
75
reed95d343f2015-05-23 13:21:06 -070076bool SkPixmap::readPixels(const SkImageInfo& requestedDstInfo, void* dstPixels, size_t dstRB,
77 int x, int y) const {
78 if (kUnknown_SkColorType == requestedDstInfo.colorType()) {
79 return false;
80 }
halcanary96fcdcc2015-08-27 07:41:13 -070081 if (nullptr == dstPixels || dstRB < requestedDstInfo.minRowBytes()) {
reed95d343f2015-05-23 13:21:06 -070082 return false;
83 }
84 if (0 == requestedDstInfo.width() || 0 == requestedDstInfo.height()) {
85 return false;
86 }
halcanary9d524f22016-03-29 09:03:52 -070087
reed95d343f2015-05-23 13:21:06 -070088 SkIRect srcR = SkIRect::MakeXYWH(x, y, requestedDstInfo.width(), requestedDstInfo.height());
89 if (!srcR.intersect(0, 0, this->width(), this->height())) {
90 return false;
91 }
halcanary9d524f22016-03-29 09:03:52 -070092
reed95d343f2015-05-23 13:21:06 -070093 // the intersect may have shrunk info's logical size
94 const SkImageInfo dstInfo = requestedDstInfo.makeWH(srcR.width(), srcR.height());
halcanary9d524f22016-03-29 09:03:52 -070095
reed95d343f2015-05-23 13:21:06 -070096 // if x or y are negative, then we have to adjust pixels
97 if (x > 0) {
98 x = 0;
99 }
100 if (y > 0) {
101 y = 0;
102 }
103 // here x,y are either 0 or negative
104 dstPixels = ((char*)dstPixels - y * dstRB - x * dstInfo.bytesPerPixel());
105
106 const SkImageInfo srcInfo = this->info().makeWH(dstInfo.width(), dstInfo.height());
107 const void* srcPixels = this->addr(srcR.x(), srcR.y());
108 return SkPixelInfo::CopyPixels(dstInfo, dstPixels, dstRB,
109 srcInfo, srcPixels, this->rowBytes(), this->ctable());
110}
reed183b57f2015-06-05 14:33:17 -0700111
reed7aefe032015-06-08 10:22:22 -0700112static uint16_t pack_8888_to_4444(unsigned a, unsigned r, unsigned g, unsigned b) {
113 unsigned pixel = (SkA32To4444(a) << SK_A4444_SHIFT) |
114 (SkR32To4444(r) << SK_R4444_SHIFT) |
115 (SkG32To4444(g) << SK_G4444_SHIFT) |
116 (SkB32To4444(b) << SK_B4444_SHIFT);
117 return SkToU16(pixel);
118}
119
120bool SkPixmap::erase(SkColor color, const SkIRect& inArea) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700121 if (nullptr == fPixels) {
reed7aefe032015-06-08 10:22:22 -0700122 return false;
123 }
124 SkIRect area;
125 if (!area.intersect(this->bounds(), inArea)) {
126 return false;
127 }
128
129 U8CPU a = SkColorGetA(color);
130 U8CPU r = SkColorGetR(color);
131 U8CPU g = SkColorGetG(color);
132 U8CPU b = SkColorGetB(color);
133
134 int height = area.height();
135 const int width = area.width();
136 const int rowBytes = this->rowBytes();
halcanary9d524f22016-03-29 09:03:52 -0700137
reed7aefe032015-06-08 10:22:22 -0700138 switch (this->colorType()) {
139 case kGray_8_SkColorType: {
140 if (255 != a) {
141 r = SkMulDiv255Round(r, a);
142 g = SkMulDiv255Round(g, a);
143 b = SkMulDiv255Round(b, a);
144 }
145 int gray = SkComputeLuminance(r, g, b);
146 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
147 while (--height >= 0) {
148 memset(p, gray, width);
149 p += rowBytes;
150 }
151 break;
152 }
153 case kAlpha_8_SkColorType: {
154 uint8_t* p = this->writable_addr8(area.fLeft, area.fTop);
155 while (--height >= 0) {
156 memset(p, a, width);
157 p += rowBytes;
158 }
159 break;
160 }
161 case kARGB_4444_SkColorType:
162 case kRGB_565_SkColorType: {
163 uint16_t* p = this->writable_addr16(area.fLeft, area.fTop);
164 uint16_t v;
halcanary9d524f22016-03-29 09:03:52 -0700165
reed7aefe032015-06-08 10:22:22 -0700166 // make rgb premultiplied
167 if (255 != a) {
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800168 r = SkMulDiv255Round(r, a);
169 g = SkMulDiv255Round(g, a);
170 b = SkMulDiv255Round(b, a);
reed7aefe032015-06-08 10:22:22 -0700171 }
halcanary9d524f22016-03-29 09:03:52 -0700172
reed7aefe032015-06-08 10:22:22 -0700173 if (kARGB_4444_SkColorType == this->colorType()) {
174 v = pack_8888_to_4444(a, r, g, b);
175 } else {
176 v = SkPackRGB16(r >> (8 - SK_R16_BITS),
177 g >> (8 - SK_G16_BITS),
178 b >> (8 - SK_B16_BITS));
179 }
180 while (--height >= 0) {
181 sk_memset16(p, v, width);
182 p = (uint16_t*)((char*)p + rowBytes);
183 }
184 break;
185 }
186 case kBGRA_8888_SkColorType:
187 case kRGBA_8888_SkColorType: {
188 uint32_t* p = this->writable_addr32(area.fLeft, area.fTop);
halcanary9d524f22016-03-29 09:03:52 -0700189
reed7aefe032015-06-08 10:22:22 -0700190 if (255 != a && kPremul_SkAlphaType == this->alphaType()) {
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800191 r = SkMulDiv255Round(r, a);
192 g = SkMulDiv255Round(g, a);
193 b = SkMulDiv255Round(b, a);
reed7aefe032015-06-08 10:22:22 -0700194 }
benjaminwagnera1bb8e02015-12-11 14:08:58 -0800195 uint32_t v = kRGBA_8888_SkColorType == this->colorType()
196 ? SkPackARGB_as_RGBA(a, r, g, b)
197 : SkPackARGB_as_BGRA(a, r, g, b);
198
reed7aefe032015-06-08 10:22:22 -0700199 while (--height >= 0) {
200 sk_memset32(p, v, width);
201 p = (uint32_t*)((char*)p + rowBytes);
202 }
203 break;
204 }
205 default:
206 return false; // no change, so don't call notifyPixelsChanged()
207 }
208 return true;
209}
210
reed3601f282016-02-05 11:18:39 -0800211#include "SkNx.h"
212#include "SkHalf.h"
213
reed3601f282016-02-05 11:18:39 -0800214bool SkPixmap::erase(const SkColor4f& origColor, const SkIRect* subset) const {
215 SkPixmap pm;
216 if (subset) {
217 if (!this->extractSubset(&pm, *subset)) {
218 return false;
219 }
220 } else {
221 pm = *this;
222 }
223
224 const SkColor4f color = origColor.pin();
225
226 if (kRGBA_F16_SkColorType != pm.colorType()) {
227 Sk4f c4 = Sk4f::Load(color.vec());
228 SkColor c;
229 (c4 * Sk4f(255) + Sk4f(0.5f)).store(&c);
230 return pm.erase(c);
231 }
232
233 const uint64_t half4 = color.premul().toF16();
234 for (int y = 0; y < pm.height(); ++y) {
235 sk_memset64(pm.writable_addr64(0, y), half4, pm.width());
236 }
237 return true;
238}
239
reed09553032015-11-23 12:32:16 -0800240#include "SkBitmap.h"
241#include "SkCanvas.h"
242#include "SkSurface.h"
243#include "SkXfermode.h"
244
245bool SkPixmap::scalePixels(const SkPixmap& dst, SkFilterQuality quality) const {
246 // Can't do anthing with empty src or dst
247 if (this->width() <= 0 || this->height() <= 0 || dst.width() <= 0 || dst.height() <= 0) {
248 return false;
249 }
250
251 // no scaling involved?
252 if (dst.width() == this->width() && dst.height() == this->height()) {
253 return this->readPixels(dst);
254 }
255
256 SkBitmap bitmap;
halcanarye36ec872015-12-09 11:36:59 -0800257 if (!bitmap.installPixels(*this)) {
reed09553032015-11-23 12:32:16 -0800258 return false;
259 }
260 bitmap.setIsVolatile(true); // so we don't try to cache it
261
reede8f30622016-03-23 18:59:25 -0700262 auto surface(SkSurface::MakeRasterDirect(dst.info(), dst.writable_addr(), dst.rowBytes()));
reed09553032015-11-23 12:32:16 -0800263 if (!surface) {
264 return false;
265 }
266
267 SkPaint paint;
268 paint.setFilterQuality(quality);
269 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
270 surface->getCanvas()->drawBitmapRect(bitmap, SkRect::MakeIWH(dst.width(), dst.height()),
271 &paint);
272 return true;
273}
274
reed183b57f2015-06-05 14:33:17 -0700275//////////////////////////////////////////////////////////////////////////////////////////////////