| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 "SkColorSpace.h" | 8 #include "SkColorSpace.h" |
| 9 #include "SkColorSpace_Base.h" | 9 #include "SkColorSpace_Base.h" |
| 10 #include "SkColorSpacePriv.h" | 10 #include "SkColorSpacePriv.h" |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 header.fFlags = flags; | 193 header.fFlags = flags; |
| 194 return header; | 194 return header; |
| 195 } | 195 } |
| 196 | 196 |
| 197 uint8_t fVersion; // Always zero | 197 uint8_t fVersion; // Always zero |
| 198 uint8_t fNamed; // Must be a SkColorSpace::Named | 198 uint8_t fNamed; // Must be a SkColorSpace::Named |
| 199 uint8_t fGammaNamed; // Must be a SkColorSpace::GammaNamed | 199 uint8_t fGammaNamed; // Must be a SkColorSpace::GammaNamed |
| 200 uint8_t fFlags; // Some combination of the flags listed above | 200 uint8_t fFlags; // Some combination of the flags listed above |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 sk_sp<SkData> SkColorSpace::serialize() const { | 203 size_t SkColorSpace::writeToMemory(void* memory) const { |
| 204 // If we have a named profile, only write the enum. | 204 // If we have a named profile, only write the enum. |
| 205 switch (fNamed) { | 205 switch (fNamed) { |
| 206 case kSRGB_Named: | 206 case kSRGB_Named: |
| 207 case kAdobeRGB_Named: { | 207 case kAdobeRGB_Named: { |
| 208 sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHead
er)); | 208 if (memory) { |
| 209 *((ColorSpaceHeader*) data->writable_data()) = | 209 *((ColorSpaceHeader*) memory) = |
| 210 ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed, 0); | 210 ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
0); |
| 211 return data; | 211 } |
| 212 return sizeof(ColorSpaceHeader); |
| 212 } | 213 } |
| 213 default: | 214 default: |
| 214 break; | 215 break; |
| 215 } | 216 } |
| 216 | 217 |
| 217 // If we have a named gamma, write the enum and the matrix. | 218 // If we have a named gamma, write the enum and the matrix. |
| 218 switch (fGammaNamed) { | 219 switch (fGammaNamed) { |
| 219 case kSRGB_GammaNamed: | 220 case kSRGB_GammaNamed: |
| 220 case k2Dot2Curve_GammaNamed: | 221 case k2Dot2Curve_GammaNamed: |
| 221 case kLinear_GammaNamed: { | 222 case kLinear_GammaNamed: { |
| 222 sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHead
er) + | 223 if (memory) { |
| 223 12 * sizeof(float)); | 224 *((ColorSpaceHeader*) memory) = |
| 224 void* dataPtr = data->writable_data(); | 225 ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed, |
| 225 | 226 ColorSpaceHeader::kMatrix_Flag); |
| 226 *((ColorSpaceHeader*) dataPtr) = ColorSpaceHeader::Pack(k0_Version,
fNamed, fGammaNamed, | 227 memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader)); |
| 227 ColorSpaceHe
ader::kMatrix_Flag); | 228 fToXYZD50.as4x3ColMajorf((float*) memory); |
| 228 dataPtr = SkTAddOffset<void>(dataPtr, sizeof(ColorSpaceHeader)); | 229 } |
| 229 | 230 return sizeof(ColorSpaceHeader) + 12 * sizeof(float); |
| 230 fToXYZD50.as4x3ColMajorf((float*) dataPtr); | |
| 231 return data; | |
| 232 } | 231 } |
| 233 default: | 232 default: |
| 234 break; | 233 break; |
| 235 } | 234 } |
| 236 | 235 |
| 237 // If we do not have a named gamma, this must have been created from an ICC
profile. | 236 // If we do not have a named gamma, this must have been created from an ICC
profile. |
| 238 // Since we were unable to recognize the gamma, we will have saved the ICC d
ata. | 237 // Since we were unable to recognize the gamma, we will have saved the ICC d
ata. |
| 239 SkASSERT(as_CSB(this)->fProfileData); | 238 SkASSERT(as_CSB(this)->fProfileData); |
| 240 | 239 |
| 241 size_t profileSize = as_CSB(this)->fProfileData->size(); | 240 size_t profileSize = as_CSB(this)->fProfileData->size(); |
| 242 if (SkAlign4(profileSize) != (uint32_t) SkAlign4(profileSize)) { | 241 if (SkAlign4(profileSize) != (uint32_t) SkAlign4(profileSize)) { |
| 242 return 0; |
| 243 } |
| 244 |
| 245 if (memory) { |
| 246 *((ColorSpaceHeader*) memory) = ColorSpaceHeader::Pack(k0_Version, fName
d, fGammaNamed, |
| 247 ColorSpaceHeader:
:kICC_Flag); |
| 248 memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader)); |
| 249 |
| 250 *((uint32_t*) memory) = (uint32_t) SkAlign4(profileSize); |
| 251 memory = SkTAddOffset<void>(memory, sizeof(uint32_t)); |
| 252 |
| 253 memcpy(memory, as_CSB(this)->fProfileData->data(), profileSize); |
| 254 memset(SkTAddOffset<void>(memory, profileSize), 0, SkAlign4(profileSize)
- profileSize); |
| 255 } |
| 256 return sizeof(ColorSpaceHeader) + sizeof(uint32_t) + SkAlign4(profileSize); |
| 257 } |
| 258 |
| 259 sk_sp<SkData> SkColorSpace::serialize() const { |
| 260 size_t size = this->writeToMemory(nullptr); |
| 261 if (0 == size) { |
| 243 return nullptr; | 262 return nullptr; |
| 244 } | 263 } |
| 245 | 264 |
| 246 sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHeader) + si
zeof(uint32_t) + | 265 sk_sp<SkData> data = SkData::MakeUninitialized(size); |
| 247 SkAlign4(profileSize)); | 266 this->writeToMemory(data->writable_data()); |
| 248 void* dataPtr = data->writable_data(); | |
| 249 | |
| 250 *((ColorSpaceHeader*) dataPtr) = ColorSpaceHeader::Pack(k0_Version, fNamed,
fGammaNamed, | |
| 251 ColorSpaceHeader::kI
CC_Flag); | |
| 252 dataPtr = SkTAddOffset<void>(dataPtr, sizeof(ColorSpaceHeader)); | |
| 253 | |
| 254 *((uint32_t*) dataPtr) = (uint32_t) SkAlign4(profileSize); | |
| 255 dataPtr = SkTAddOffset<void>(dataPtr, sizeof(uint32_t)); | |
| 256 | |
| 257 memcpy(dataPtr, as_CSB(this)->fProfileData->data(), profileSize); | |
| 258 memset(SkTAddOffset<void>(dataPtr, profileSize), 0, SkAlign4(profileSize) -
profileSize); | |
| 259 return data; | 267 return data; |
| 260 } | 268 } |
| 261 | 269 |
| 262 sk_sp<SkColorSpace> SkColorSpace::Deserialize(const void* data, size_t length) { | 270 sk_sp<SkColorSpace> SkColorSpace::Deserialize(const void* data, size_t length) { |
| 263 if (length < sizeof(ColorSpaceHeader)) { | 271 if (length < sizeof(ColorSpaceHeader)) { |
| 264 return nullptr; | 272 return nullptr; |
| 265 } | 273 } |
| 266 | 274 |
| 267 ColorSpaceHeader header = *((const ColorSpaceHeader*) data); | 275 ColorSpaceHeader header = *((const ColorSpaceHeader*) data); |
| 268 data = SkTAddOffset<const void>(data, sizeof(ColorSpaceHeader)); | 276 data = SkTAddOffset<const void>(data, sizeof(ColorSpaceHeader)); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 297 | 305 |
| 298 uint32_t profileSize = *((uint32_t*) data); | 306 uint32_t profileSize = *((uint32_t*) data); |
| 299 data = SkTAddOffset<const void>(data, sizeof(uint32_t)); | 307 data = SkTAddOffset<const void>(data, sizeof(uint32_t)); |
| 300 length -= sizeof(uint32_t); | 308 length -= sizeof(uint32_t); |
| 301 if (length < profileSize) { | 309 if (length < profileSize) { |
| 302 return nullptr; | 310 return nullptr; |
| 303 } | 311 } |
| 304 | 312 |
| 305 return NewICC(data, profileSize); | 313 return NewICC(data, profileSize); |
| 306 } | 314 } |
| OLD | NEW |