| 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 /* | |
| 13 * We store this as a byte in the ImageInfo flatten buffer. | |
| 14 */ | |
| 15 enum class SkFlattenColorSpaceEnum { | |
| 16 kUnspecified, | |
| 17 kSRGB, | |
| 18 kAdobe1998, | |
| 19 // ... add more here | |
| 20 kLastEnum = kAdobe1998, | |
| 21 // final value means the actual profile data follows the info | |
| 22 kICCProfile = 0xFF, | |
| 23 }; | |
| 24 | |
| 25 static sk_sp<SkColorSpace> make_from_enum(SkFlattenColorSpaceEnum value) { | |
| 26 switch (value) { | |
| 27 case SkFlattenColorSpaceEnum::kSRGB: | |
| 28 return SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); | |
| 29 case SkFlattenColorSpaceEnum::kAdobe1998: | |
| 30 return SkColorSpace::NewNamed(SkColorSpace::kAdobeRGB_Named); | |
| 31 default: | |
| 32 return nullptr; | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 SkColorSpace::Named sk_deduce_named_from_colorspace(SkColorSpace* cs) { | |
| 37 return cs->fNamed; | |
| 38 } | |
| 39 | |
| 40 static SkFlattenColorSpaceEnum deduce_from_colorspace(SkColorSpace* cs) { | |
| 41 if (!cs) { | |
| 42 return SkFlattenColorSpaceEnum::kUnspecified; | |
| 43 } | |
| 44 switch (sk_deduce_named_from_colorspace(cs)) { | |
| 45 case SkColorSpace::kSRGB_Named: | |
| 46 return SkFlattenColorSpaceEnum::kSRGB; | |
| 47 case SkColorSpace::kAdobeRGB_Named: | |
| 48 return SkFlattenColorSpaceEnum::kAdobe1998; | |
| 49 default: | |
| 50 return SkFlattenColorSpaceEnum::kICCProfile; | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
| 55 | |
| 56 #ifdef SK_SUPPORT_LEGACY_COLORPROFILETYPE | 12 #ifdef SK_SUPPORT_LEGACY_COLORPROFILETYPE |
| 57 SkColorProfileType SkImageInfo::profileType() const { | 13 SkColorProfileType SkImageInfo::profileType() const { |
| 58 return fColorSpace && fColorSpace->gammaCloseToSRGB() | 14 return fColorSpace && fColorSpace->gammaCloseToSRGB() |
| 59 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; | 15 ? kSRGB_SkColorProfileType : kLinear_SkColorProfileType; |
| 60 } | 16 } |
| 61 #endif | 17 #endif |
| 62 | 18 |
| 63 static bool alpha_type_is_valid(SkAlphaType alphaType) { | 19 static bool alpha_type_is_valid(SkAlphaType alphaType) { |
| 64 return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); | 20 return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); |
| 65 } | 21 } |
| 66 | 22 |
| 67 static bool color_type_is_valid(SkColorType colorType) { | 23 static bool color_type_is_valid(SkColorType colorType) { |
| 68 return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); | 24 return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); |
| 69 } | 25 } |
| 70 | 26 |
| 71 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) { | 27 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) { |
| 72 return SkImageInfo(width, height, kN32_SkColorType, at, | 28 return SkImageInfo(width, height, kN32_SkColorType, at, |
| 73 SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)); | 29 SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)); |
| 74 } | 30 } |
| 75 | 31 |
| 76 void SkImageInfo::unflatten(SkReadBuffer& buffer) { | 32 void SkImageInfo::unflatten(SkReadBuffer& buffer) { |
| 77 fWidth = buffer.read32(); | 33 fWidth = buffer.read32(); |
| 78 fHeight = buffer.read32(); | 34 fHeight = buffer.read32(); |
| 79 | 35 |
| 80 uint32_t packed = buffer.read32(); | 36 uint32_t packed = buffer.read32(); |
| 81 SkASSERT(0 == (packed >> 24)); | 37 SkASSERT(0 == (packed >> 24)); |
| 82 fColorType = (SkColorType)((packed >> 0) & 0xFF); | 38 fColorType = (SkColorType)((packed >> 0) & 0xFF); |
| 83 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); | 39 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); |
| 84 SkFlattenColorSpaceEnum csenum = (SkFlattenColorSpaceEnum)((packed >> 16) &
0xFF); | |
| 85 buffer.validate(alpha_type_is_valid(fAlphaType) && color_type_is_valid(fColo
rType)); | 40 buffer.validate(alpha_type_is_valid(fAlphaType) && color_type_is_valid(fColo
rType)); |
| 86 | 41 |
| 87 if (SkFlattenColorSpaceEnum::kICCProfile == csenum) { | 42 sk_sp<SkData> data = buffer.readByteArrayAsData(); |
| 88 SkASSERT(false); // we shouldn't hit this yet, as we don't write thes
e yet | 43 fColorSpace = SkColorSpace::Deserialize(data->data(), data->size()); |
| 89 fColorSpace.reset(); | |
| 90 } else { | |
| 91 if (csenum > SkFlattenColorSpaceEnum::kLastEnum) { | |
| 92 csenum = SkFlattenColorSpaceEnum::kUnspecified; | |
| 93 } | |
| 94 fColorSpace = make_from_enum(csenum); | |
| 95 } | |
| 96 } | 44 } |
| 97 | 45 |
| 98 void SkImageInfo::flatten(SkWriteBuffer& buffer) const { | 46 void SkImageInfo::flatten(SkWriteBuffer& buffer) const { |
| 99 buffer.write32(fWidth); | 47 buffer.write32(fWidth); |
| 100 buffer.write32(fHeight); | 48 buffer.write32(fHeight); |
| 101 | 49 |
| 102 SkFlattenColorSpaceEnum csenum = deduce_from_colorspace(fColorSpace.get()); | |
| 103 | |
| 104 // TODO: when we actually support flattening the colorspace to a profile blo
b, remove this | |
| 105 // hack (and write the blob after we write packed. | |
| 106 if (SkFlattenColorSpaceEnum::kICCProfile == csenum) { | |
| 107 csenum = SkFlattenColorSpaceEnum::kUnspecified; | |
| 108 } | |
| 109 | |
| 110 SkASSERT(0 == ((int)csenum & ~0xFF)); | |
| 111 SkASSERT(0 == (fAlphaType & ~0xFF)); | 50 SkASSERT(0 == (fAlphaType & ~0xFF)); |
| 112 SkASSERT(0 == (fColorType & ~0xFF)); | 51 SkASSERT(0 == (fColorType & ~0xFF)); |
| 113 uint32_t packed = ((int)csenum << 16) | (fAlphaType << 8) | fColorType; | 52 uint32_t packed = (fAlphaType << 8) | fColorType; |
| 114 buffer.write32(packed); | 53 buffer.write32(packed); |
| 115 | 54 |
| 116 if (SkFlattenColorSpaceEnum::kICCProfile == csenum) { | 55 if (fColorSpace) { |
| 117 // TODO: write the ICCProfile blob | 56 sk_sp<SkData> data = fColorSpace->serialize(); |
| 57 if (data) { |
| 58 buffer.writeDataAsByteArray(data.get()); |
| 59 } else { |
| 60 buffer.writeByteArray(nullptr, 0); |
| 61 } |
| 62 } else { |
| 63 sk_sp<SkData> data = SkData::MakeEmpty(); |
| 64 buffer.writeDataAsByteArray(data.get()); |
| 118 } | 65 } |
| 119 } | 66 } |
| 120 | 67 |
| 121 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, | 68 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
| 122 SkAlphaType* canonical) { | 69 SkAlphaType* canonical) { |
| 123 switch (colorType) { | 70 switch (colorType) { |
| 124 case kUnknown_SkColorType: | 71 case kUnknown_SkColorType: |
| 125 alphaType = kUnknown_SkAlphaType; | 72 alphaType = kUnknown_SkAlphaType; |
| 126 break; | 73 break; |
| 127 case kAlpha_8_SkColorType: | 74 case kAlpha_8_SkColorType: |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 } | 133 } |
| 187 // here x,y are either 0 or negative | 134 // here x,y are either 0 or negative |
| 188 fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); | 135 fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); |
| 189 // the intersect may have shrunk info's logical size | 136 // the intersect may have shrunk info's logical size |
| 190 fInfo = fInfo.makeWH(srcR.width(), srcR.height()); | 137 fInfo = fInfo.makeWH(srcR.width(), srcR.height()); |
| 191 fX = srcR.x(); | 138 fX = srcR.x(); |
| 192 fY = srcR.y(); | 139 fY = srcR.y(); |
| 193 | 140 |
| 194 return true; | 141 return true; |
| 195 } | 142 } |
| OLD | NEW |