• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 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 
8 #include "include/private/SkImageInfoPriv.h"
9 #include "src/core/SkReadBuffer.h"
10 #include "src/core/SkSafeMath.h"
11 #include "src/core/SkWriteBuffer.h"
12 
SkColorTypeBytesPerPixel(SkColorType ct)13 int SkColorTypeBytesPerPixel(SkColorType ct) {
14     switch (ct) {
15         case kUnknown_SkColorType:            return 0;
16         case kAlpha_8_SkColorType:            return 1;
17         case kRGB_565_SkColorType:            return 2;
18         case kARGB_4444_SkColorType:          return 2;
19         case kRGBA_8888_SkColorType:          return 4;
20         case kBGRA_8888_SkColorType:          return 4;
21         case kRGB_888x_SkColorType:           return 4;
22         case kRGBA_1010102_SkColorType:       return 4;
23         case kRGB_101010x_SkColorType:        return 4;
24         case kBGRA_1010102_SkColorType:       return 4;
25         case kBGR_101010x_SkColorType:        return 4;
26         case kGray_8_SkColorType:             return 1;
27         case kRGBA_F16Norm_SkColorType:       return 8;
28         case kRGBA_F16_SkColorType:           return 8;
29         case kRGBA_F32_SkColorType:           return 16;
30         case kR8G8_unorm_SkColorType:         return 2;
31         case kA16_unorm_SkColorType:          return 2;
32         case kR16G16_unorm_SkColorType:       return 4;
33         case kA16_float_SkColorType:          return 2;
34         case kR16G16_float_SkColorType:       return 4;
35         case kR16G16B16A16_unorm_SkColorType: return 8;
36         case kSRGBA_8888_SkColorType:         return 4;
37     }
38     SkUNREACHABLE;
39 }
40 
SkColorTypeIsAlwaysOpaque(SkColorType ct)41 bool SkColorTypeIsAlwaysOpaque(SkColorType ct) {
42     return !(SkColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag);
43 }
44 
45 ///////////////////////////////////////////////////////////////////////////////////////////////////
46 
bytesPerPixel() const47 int SkColorInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
48 
shiftPerPixel() const49 int SkColorInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
50 
51 ///////////////////////////////////////////////////////////////////////////////////////////////////
52 
computeOffset(int x,int y,size_t rowBytes) const53 size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
54     SkASSERT((unsigned)x < (unsigned)this->width());
55     SkASSERT((unsigned)y < (unsigned)this->height());
56     return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
57 }
58 
computeByteSize(size_t rowBytes) const59 size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
60     if (0 == this->height()) {
61         return 0;
62     }
63     SkSafeMath safe;
64     size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
65                             safe.mul(this->width(), this->bytesPerPixel()));
66 
67     // The CPU backend implements some memory operations on images using instructions that take a
68     // signed 32-bit offset from the base. If we ever make an image larger than that, overflow can
69     // cause us to read/write memory that starts 2GB *before* the buffer. (crbug.com/1264705)
70     constexpr size_t kMaxSigned32BitSize = SK_MaxS32;
71     return (safe.ok() && (bytes <= kMaxSigned32BitSize)) ? bytes : SIZE_MAX;
72 }
73 
MakeS32(int width,int height,SkAlphaType at)74 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) {
75     return SkImageInfo({width, height}, {kN32_SkColorType, at, SkColorSpace::MakeSRGB()});
76 }
77 
78 #ifdef SK_DEBUG
validate() const79 void SkImageInfo::validate() const {
80     SkASSERT(fDimensions.width() >= 0);
81     SkASSERT(fDimensions.height() >= 0);
82     SkASSERT(SkColorTypeIsValid(this->colorType()));
83     SkASSERT(SkAlphaTypeIsValid(this->alphaType()));
84 }
85 #endif
86 
SkColorTypeValidateAlphaType(SkColorType colorType,SkAlphaType alphaType,SkAlphaType * canonical)87 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
88                                   SkAlphaType* canonical) {
89     switch (colorType) {
90         case kUnknown_SkColorType:
91             alphaType = kUnknown_SkAlphaType;
92             break;
93         case kAlpha_8_SkColorType:         // fall-through
94         case kA16_unorm_SkColorType:       // fall-through
95         case kA16_float_SkColorType:
96             if (kUnpremul_SkAlphaType == alphaType) {
97                 alphaType = kPremul_SkAlphaType;
98             }
99             [[fallthrough]];
100         case kARGB_4444_SkColorType:
101         case kRGBA_8888_SkColorType:
102         case kSRGBA_8888_SkColorType:
103         case kBGRA_8888_SkColorType:
104         case kRGBA_1010102_SkColorType:
105         case kBGRA_1010102_SkColorType:
106         case kRGBA_F16Norm_SkColorType:
107         case kRGBA_F16_SkColorType:
108         case kRGBA_F32_SkColorType:
109         case kR16G16B16A16_unorm_SkColorType:
110             if (kUnknown_SkAlphaType == alphaType) {
111                 return false;
112             }
113             break;
114         case kGray_8_SkColorType:
115         case kR8G8_unorm_SkColorType:
116         case kR16G16_unorm_SkColorType:
117         case kR16G16_float_SkColorType:
118         case kRGB_565_SkColorType:
119         case kRGB_888x_SkColorType:
120         case kRGB_101010x_SkColorType:
121         case kBGR_101010x_SkColorType:
122             alphaType = kOpaque_SkAlphaType;
123             break;
124     }
125     if (canonical) {
126         *canonical = alphaType;
127     }
128     return true;
129 }
130 
131 ///////////////////////////////////////////////////////////////////////////////////////////////////
132 
133 #include "src/image/SkReadPixelsRec.h"
134 
trim(int srcWidth,int srcHeight)135 bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) {
136     if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
137         return false;
138     }
139     if (0 >= fInfo.width() || 0 >= fInfo.height()) {
140         return false;
141     }
142 
143     int x = fX;
144     int y = fY;
145     SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
146     if (!srcR.intersect({0, 0, srcWidth, srcHeight})) {
147         return false;
148     }
149 
150     // if x or y are negative, then we have to adjust pixels
151     if (x > 0) {
152         x = 0;
153     }
154     if (y > 0) {
155         y = 0;
156     }
157     // here x,y are either 0 or negative
158     // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
159     fPixels = ((char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
160     // the intersect may have shrunk info's logical size
161     fInfo = fInfo.makeDimensions(srcR.size());
162     fX = srcR.x();
163     fY = srcR.y();
164 
165     return true;
166 }
167 
168 ///////////////////////////////////////////////////////////////////////////////////////////////////
169 
170 #include "src/core/SkWritePixelsRec.h"
171 
trim(int dstWidth,int dstHeight)172 bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) {
173     if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
174         return false;
175     }
176     if (0 >= fInfo.width() || 0 >= fInfo.height()) {
177         return false;
178     }
179 
180     int x = fX;
181     int y = fY;
182     SkIRect dstR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
183     if (!dstR.intersect({0, 0, dstWidth, dstHeight})) {
184         return false;
185     }
186 
187     // if x or y are negative, then we have to adjust pixels
188     if (x > 0) {
189         x = 0;
190     }
191     if (y > 0) {
192         y = 0;
193     }
194     // here x,y are either 0 or negative
195     // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
196     fPixels = ((const char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
197     // the intersect may have shrunk info's logical size
198     fInfo = fInfo.makeDimensions(dstR.size());
199     fX = dstR.x();
200     fY = dstR.y();
201 
202     return true;
203 }
204