OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight)); | 145 SkIntToScalar(fInfo.fWidth), SkIntToScalar(fInfo.fHeight)); |
146 } | 146 } |
147 | 147 |
148 void SkBitmap::getBounds(SkIRect* bounds) const { | 148 void SkBitmap::getBounds(SkIRect* bounds) const { |
149 SkASSERT(bounds); | 149 SkASSERT(bounds); |
150 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight); | 150 bounds->set(0, 0, fInfo.fWidth, fInfo.fHeight); |
151 } | 151 } |
152 | 152 |
153 /////////////////////////////////////////////////////////////////////////////// | 153 /////////////////////////////////////////////////////////////////////////////// |
154 | 154 |
155 static bool validate_alphaType(SkColorType colorType, SkAlphaType alphaType, | |
156 SkAlphaType* canonical = NULL) { | |
157 switch (colorType) { | |
158 case kUnknown_SkColorType: | |
159 alphaType = kIgnore_SkAlphaType; | |
160 break; | |
161 case kAlpha_8_SkColorType: | |
162 if (kUnpremul_SkAlphaType == alphaType) { | |
163 alphaType = kPremul_SkAlphaType; | |
164 } | |
165 // fall-through | |
166 case kIndex_8_SkColorType: | |
167 case kARGB_4444_SkColorType: | |
168 case kRGBA_8888_SkColorType: | |
169 case kBGRA_8888_SkColorType: | |
170 if (kIgnore_SkAlphaType == alphaType) { | |
171 return false; | |
172 } | |
173 break; | |
174 case kRGB_565_SkColorType: | |
175 alphaType = kOpaque_SkAlphaType; | |
176 break; | |
177 default: | |
178 return false; | |
179 } | |
180 if (canonical) { | |
181 *canonical = alphaType; | |
182 } | |
183 return true; | |
184 } | |
185 | |
186 bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) { | 155 bool SkBitmap::setInfo(const SkImageInfo& origInfo, size_t rowBytes) { |
187 SkImageInfo info = origInfo; | 156 SkImageInfo info = origInfo; |
188 | 157 |
189 if (!validate_alphaType(info.fColorType, info.fAlphaType, | 158 if (!SkColorTypeValidateAlphaType(info.fColorType, info.fAlphaType, |
190 &info.fAlphaType)) { | 159 &info.fAlphaType)) { |
191 return reset_return_false(this); | 160 return reset_return_false(this); |
192 } | 161 } |
193 | 162 |
194 // require that rowBytes fit in 31bits | 163 // require that rowBytes fit in 31bits |
195 int64_t mrb = info.minRowBytes64(); | 164 int64_t mrb = info.minRowBytes64(); |
196 if ((int32_t)mrb != mrb) { | 165 if ((int32_t)mrb != mrb) { |
197 return reset_return_false(this); | 166 return reset_return_false(this); |
198 } | 167 } |
199 if ((int64_t)rowBytes != (int32_t)rowBytes) { | 168 if ((int64_t)rowBytes != (int32_t)rowBytes) { |
200 return reset_return_false(this); | 169 return reset_return_false(this); |
(...skipping 20 matching lines...) Expand all Loading... |
221 | 190 |
222 #ifdef SK_SUPPORT_LEGACY_SETCONFIG | 191 #ifdef SK_SUPPORT_LEGACY_SETCONFIG |
223 bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes, | 192 bool SkBitmap::setConfig(Config config, int width, int height, size_t rowBytes, |
224 SkAlphaType alphaType) { | 193 SkAlphaType alphaType) { |
225 SkColorType ct = SkBitmapConfigToColorType(config); | 194 SkColorType ct = SkBitmapConfigToColorType(config); |
226 return this->setInfo(SkImageInfo::Make(width, height, ct, alphaType), rowByt
es); | 195 return this->setInfo(SkImageInfo::Make(width, height, ct, alphaType), rowByt
es); |
227 } | 196 } |
228 #endif | 197 #endif |
229 | 198 |
230 bool SkBitmap::setAlphaType(SkAlphaType alphaType) { | 199 bool SkBitmap::setAlphaType(SkAlphaType alphaType) { |
231 if (!validate_alphaType(fInfo.fColorType, alphaType, &alphaType)) { | 200 if (!SkColorTypeValidateAlphaType(fInfo.fColorType, alphaType, &alphaType))
{ |
232 return false; | 201 return false; |
233 } | 202 } |
234 if (fInfo.fAlphaType != alphaType) { | 203 if (fInfo.fAlphaType != alphaType) { |
235 fInfo.fAlphaType = alphaType; | 204 fInfo.fAlphaType = alphaType; |
236 if (fPixelRef) { | 205 if (fPixelRef) { |
237 fPixelRef->changeAlphaType(alphaType); | 206 fPixelRef->changeAlphaType(alphaType); |
238 } | 207 } |
239 } | 208 } |
240 return true; | 209 return true; |
241 } | 210 } |
(...skipping 1083 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1325 | 1294 |
1326 void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) { | 1295 void SkBitmap::legacyUnflatten(SkReadBuffer& buffer) { |
1327 this->reset(); | 1296 this->reset(); |
1328 | 1297 |
1329 SkImageInfo info; | 1298 SkImageInfo info; |
1330 info.unflatten(buffer); | 1299 info.unflatten(buffer); |
1331 size_t rowBytes = buffer.readInt(); | 1300 size_t rowBytes = buffer.readInt(); |
1332 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && | 1301 if (!buffer.validate((info.width() >= 0) && (info.height() >= 0) && |
1333 SkColorTypeIsValid(info.fColorType) && | 1302 SkColorTypeIsValid(info.fColorType) && |
1334 SkAlphaTypeIsValid(info.fAlphaType) && | 1303 SkAlphaTypeIsValid(info.fAlphaType) && |
1335 validate_alphaType(info.fColorType, info.fAlphaType) && | 1304 SkColorTypeValidateAlphaType(info.fColorType, info.fAlp
haType) && |
1336 info.validRowBytes(rowBytes))) { | 1305 info.validRowBytes(rowBytes))) { |
1337 return; | 1306 return; |
1338 } | 1307 } |
1339 | 1308 |
1340 bool configIsValid = this->setInfo(info, rowBytes); | 1309 bool configIsValid = this->setInfo(info, rowBytes); |
1341 buffer.validate(configIsValid); | 1310 buffer.validate(configIsValid); |
1342 | 1311 |
1343 int reftype = buffer.readInt(); | 1312 int reftype = buffer.readInt(); |
1344 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) || | 1313 if (buffer.validate((SERIALIZE_PIXELTYPE_REF_DATA == reftype) || |
1345 (SERIALIZE_PIXELTYPE_NONE == reftype))) { | 1314 (SERIALIZE_PIXELTYPE_NONE == reftype))) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1456 /////////////////////////////////////////////////////////////////////////////// | 1425 /////////////////////////////////////////////////////////////////////////////// |
1457 | 1426 |
1458 #ifdef SK_DEBUG | 1427 #ifdef SK_DEBUG |
1459 void SkImageInfo::validate() const { | 1428 void SkImageInfo::validate() const { |
1460 SkASSERT(fWidth >= 0); | 1429 SkASSERT(fWidth >= 0); |
1461 SkASSERT(fHeight >= 0); | 1430 SkASSERT(fHeight >= 0); |
1462 SkASSERT(SkColorTypeIsValid(fColorType)); | 1431 SkASSERT(SkColorTypeIsValid(fColorType)); |
1463 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); | 1432 SkASSERT(SkAlphaTypeIsValid(fAlphaType)); |
1464 } | 1433 } |
1465 #endif | 1434 #endif |
OLD | NEW |