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 kGray_8_SkColorType: return 1;
25 case kRGBA_F16Norm_SkColorType: return 8;
26 case kRGBA_F16_SkColorType: return 8;
27 case kRGBA_F32_SkColorType: return 16;
28 }
29 return 0;
30 }
31
SkColorTypeIsAlwaysOpaque(SkColorType ct)32 bool SkColorTypeIsAlwaysOpaque(SkColorType ct) {
33 return !(kAlpha_SkColorTypeComponentFlag & SkColorTypeComponentFlags(ct));
34 }
35
36 ///////////////////////////////////////////////////////////////////////////////////////////////////
37
bytesPerPixel() const38 int SkImageInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
39
shiftPerPixel() const40 int SkImageInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
41
computeOffset(int x,int y,size_t rowBytes) const42 size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
43 SkASSERT((unsigned)x < (unsigned)this->width());
44 SkASSERT((unsigned)y < (unsigned)this->height());
45 return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
46 }
47
computeByteSize(size_t rowBytes) const48 size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
49 if (0 == this->height()) {
50 return 0;
51 }
52 SkSafeMath safe;
53 size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
54 safe.mul(this->width(), this->bytesPerPixel()));
55 return safe ? bytes : SIZE_MAX;
56 }
57
MakeS32(int width,int height,SkAlphaType at)58 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) {
59 return SkImageInfo(width, height, kN32_SkColorType, at,
60 SkColorSpace::MakeSRGB());
61 }
62
SkColorTypeValidateAlphaType(SkColorType colorType,SkAlphaType alphaType,SkAlphaType * canonical)63 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
64 SkAlphaType* canonical) {
65 switch (colorType) {
66 case kUnknown_SkColorType:
67 alphaType = kUnknown_SkAlphaType;
68 break;
69 case kAlpha_8_SkColorType:
70 if (kUnpremul_SkAlphaType == alphaType) {
71 alphaType = kPremul_SkAlphaType;
72 }
73 // fall-through
74 case kARGB_4444_SkColorType:
75 case kRGBA_8888_SkColorType:
76 case kBGRA_8888_SkColorType:
77 case kRGBA_1010102_SkColorType:
78 case kRGBA_F16Norm_SkColorType:
79 case kRGBA_F16_SkColorType:
80 case kRGBA_F32_SkColorType:
81 if (kUnknown_SkAlphaType == alphaType) {
82 return false;
83 }
84 break;
85 case kGray_8_SkColorType:
86 case kRGB_565_SkColorType:
87 case kRGB_888x_SkColorType:
88 case kRGB_101010x_SkColorType:
89 alphaType = kOpaque_SkAlphaType;
90 break;
91 default:
92 return false;
93 }
94 if (canonical) {
95 *canonical = alphaType;
96 }
97 return true;
98 }
99
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101
102 #include "src/image/SkReadPixelsRec.h"
103
trim(int srcWidth,int srcHeight)104 bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) {
105 if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
106 return false;
107 }
108 if (0 >= fInfo.width() || 0 >= fInfo.height()) {
109 return false;
110 }
111
112 int x = fX;
113 int y = fY;
114 SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
115 if (!srcR.intersect(0, 0, srcWidth, srcHeight)) {
116 return false;
117 }
118
119 // if x or y are negative, then we have to adjust pixels
120 if (x > 0) {
121 x = 0;
122 }
123 if (y > 0) {
124 y = 0;
125 }
126 // here x,y are either 0 or negative
127 // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
128 fPixels = ((char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
129 // the intersect may have shrunk info's logical size
130 fInfo = fInfo.makeWH(srcR.width(), srcR.height());
131 fX = srcR.x();
132 fY = srcR.y();
133
134 return true;
135 }
136
137 ///////////////////////////////////////////////////////////////////////////////////////////////////
138
139 #include "src/core/SkWritePixelsRec.h"
140
trim(int dstWidth,int dstHeight)141 bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) {
142 if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
143 return false;
144 }
145 if (0 >= fInfo.width() || 0 >= fInfo.height()) {
146 return false;
147 }
148
149 int x = fX;
150 int y = fY;
151 SkIRect dstR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height());
152 if (!dstR.intersect(0, 0, dstWidth, dstHeight)) {
153 return false;
154 }
155
156 // if x or y are negative, then we have to adjust pixels
157 if (x > 0) {
158 x = 0;
159 }
160 if (y > 0) {
161 y = 0;
162 }
163 // here x,y are either 0 or negative
164 // we negate and add them so UBSAN (pointer-overflow) doesn't get confused.
165 fPixels = ((const char*)fPixels + -y*fRowBytes + -x*fInfo.bytesPerPixel());
166 // the intersect may have shrunk info's logical size
167 fInfo = fInfo.makeWH(dstR.width(), dstR.height());
168 fX = dstR.x();
169 fY = dstR.y();
170
171 return true;
172 }
173