OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImageInfo.h" | 8 #include "SkImageInfo.h" |
9 #include "SkReadBuffer.h" | 9 #include "SkReadBuffer.h" |
10 #include "SkWriteBuffer.h" | 10 #include "SkWriteBuffer.h" |
11 | 11 |
12 static bool alpha_type_is_valid(SkAlphaType alphaType) { | 12 static bool alpha_type_is_valid(SkAlphaType alphaType) { |
13 return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); | 13 return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); |
14 } | 14 } |
15 | 15 |
16 static bool color_type_is_valid(SkColorType colorType) { | 16 static bool color_type_is_valid(SkColorType colorType) { |
17 return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); | 17 return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); |
18 } | 18 } |
19 | 19 |
20 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) { | 20 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) { |
21 return SkImageInfo(width, height, kN32_SkColorType, at, | 21 return SkImageInfo(width, height, kN32_SkColorType, at, |
22 SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)); | 22 SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)); |
23 } | 23 } |
24 | 24 |
| 25 static const int kColorTypeMask = 0x0F; |
| 26 static const int kAlphaTypeMask = 0x03; |
| 27 |
25 void SkImageInfo::unflatten(SkReadBuffer& buffer) { | 28 void SkImageInfo::unflatten(SkReadBuffer& buffer) { |
26 fWidth = buffer.read32(); | 29 fWidth = buffer.read32(); |
27 fHeight = buffer.read32(); | 30 fHeight = buffer.read32(); |
28 | 31 |
29 uint32_t packed = buffer.read32(); | 32 uint32_t packed = buffer.read32(); |
30 SkASSERT(0 == (packed >> 24)); | 33 fColorType = (SkColorType)((packed >> 0) & kColorTypeMask); |
31 fColorType = (SkColorType)((packed >> 0) & 0xFF); | 34 fAlphaType = (SkAlphaType)((packed >> 8) & kAlphaTypeMask); |
32 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); | |
33 buffer.validate(alpha_type_is_valid(fAlphaType) && color_type_is_valid(fColo
rType)); | 35 buffer.validate(alpha_type_is_valid(fAlphaType) && color_type_is_valid(fColo
rType)); |
34 | 36 |
35 sk_sp<SkData> data = buffer.readByteArrayAsData(); | 37 sk_sp<SkData> data = buffer.readByteArrayAsData(); |
36 fColorSpace = SkColorSpace::Deserialize(data->data(), data->size()); | 38 fColorSpace = SkColorSpace::Deserialize(data->data(), data->size()); |
37 } | 39 } |
38 | 40 |
39 void SkImageInfo::flatten(SkWriteBuffer& buffer) const { | 41 void SkImageInfo::flatten(SkWriteBuffer& buffer) const { |
40 buffer.write32(fWidth); | 42 buffer.write32(fWidth); |
41 buffer.write32(fHeight); | 43 buffer.write32(fHeight); |
42 | 44 |
43 SkASSERT(0 == (fAlphaType & ~0xFF)); | 45 SkASSERT(0 == (fAlphaType & ~kAlphaTypeMask)); |
44 SkASSERT(0 == (fColorType & ~0xFF)); | 46 SkASSERT(0 == (fColorType & ~kColorTypeMask)); |
45 uint32_t packed = (fAlphaType << 8) | fColorType; | 47 uint32_t packed = (fAlphaType << 8) | fColorType; |
46 buffer.write32(packed); | 48 buffer.write32(packed); |
47 | 49 |
48 if (fColorSpace) { | 50 if (fColorSpace) { |
49 sk_sp<SkData> data = fColorSpace->serialize(); | 51 sk_sp<SkData> data = fColorSpace->serialize(); |
50 if (data) { | 52 if (data) { |
51 buffer.writeDataAsByteArray(data.get()); | 53 buffer.writeDataAsByteArray(data.get()); |
52 } else { | 54 } else { |
53 buffer.writeByteArray(nullptr, 0); | 55 buffer.writeByteArray(nullptr, 0); |
54 } | 56 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 128 } |
127 // here x,y are either 0 or negative | 129 // here x,y are either 0 or negative |
128 fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); | 130 fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); |
129 // the intersect may have shrunk info's logical size | 131 // the intersect may have shrunk info's logical size |
130 fInfo = fInfo.makeWH(srcR.width(), srcR.height()); | 132 fInfo = fInfo.makeWH(srcR.width(), srcR.height()); |
131 fX = srcR.x(); | 133 fX = srcR.x(); |
132 fY = srcR.y(); | 134 fY = srcR.y(); |
133 | 135 |
134 return true; | 136 return true; |
135 } | 137 } |
OLD | NEW |