| 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 static bool color_type_supports_sRGB(SkColorType colorType) { |
| 21 switch (colorType) { |
| 22 case kRGBA_8888_SkColorType: |
| 23 case kBGRA_8888_SkColorType: |
| 24 return true; |
| 25 default: |
| 26 return false; |
| 27 } |
| 28 } |
| 29 |
| 30 static bool color_type_supports_gamma(SkColorType colorType) { |
| 31 switch (colorType) { |
| 32 case kRGBA_8888_SkColorType: |
| 33 case kBGRA_8888_SkColorType: |
| 34 // case kLuminance ... |
| 35 return true; |
| 36 default: |
| 37 return false; |
| 38 } |
| 39 } |
| 40 |
| 41 static bool pin_gamma_to_legal(float gamma) { |
| 42 if (!SkScalarIsFinite(gamma)) { |
| 43 return 1; |
| 44 } |
| 45 // these limits are just made up -- feel free to change them within reason |
| 46 const float min_gamma = 0.01f; |
| 47 const float max_gamma = 4.0; |
| 48 return SkScalarPin(gamma, min_gamma, max_gamma); |
| 49 } |
| 50 |
| 51 SkImageInfo SkImageInfo::MakeSRGB(int width, int height, SkColorType ct, SkAlpha
Type at) { |
| 52 Profile p = color_type_supports_sRGB(ct) ? kSRGB_Profile : kUnknown_Profile; |
| 53 return SkImageInfo(width, height, ct, at, p, 0); |
| 54 } |
| 55 |
| 56 SkImageInfo SkImageInfo::MakeWithGamma(int width, int height, SkColorType ct, Sk
AlphaType at, |
| 57 float gamma) { |
| 58 Profile p; |
| 59 if (color_type_supports_gamma(ct)) { |
| 60 gamma = pin_gamma_to_legal(gamma); |
| 61 p = kExponential_Profile; |
| 62 } else { |
| 63 p = kUnknown_Profile; |
| 64 gamma = 0; |
| 65 } |
| 66 return SkImageInfo(width, height, ct, at, p, gamma); |
| 67 } |
| 68 |
| 20 void SkImageInfo::unflatten(SkReadBuffer& buffer) { | 69 void SkImageInfo::unflatten(SkReadBuffer& buffer) { |
| 21 fWidth = buffer.read32(); | 70 fWidth = buffer.read32(); |
| 22 fHeight = buffer.read32(); | 71 fHeight = buffer.read32(); |
| 23 | 72 |
| 24 uint32_t packed = buffer.read32(); | 73 uint32_t packed = buffer.read32(); |
| 25 SkASSERT(0 == (packed >> 16)); | 74 SkASSERT(0 == (packed >> 16)); |
| 26 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); | 75 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); |
| 27 fColorType = (SkColorType)((packed >> 0) & 0xFF); | 76 fColorType = (SkColorType)((packed >> 0) & 0xFF); |
| 28 buffer.validate(alpha_type_is_valid(fAlphaType) && | 77 buffer.validate(alpha_type_is_valid(fAlphaType) && |
| 29 color_type_is_valid(fColorType)); | 78 color_type_is_valid(fColorType)); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 alphaType = kOpaque_SkAlphaType; | 111 alphaType = kOpaque_SkAlphaType; |
| 63 break; | 112 break; |
| 64 default: | 113 default: |
| 65 return false; | 114 return false; |
| 66 } | 115 } |
| 67 if (canonical) { | 116 if (canonical) { |
| 68 *canonical = alphaType; | 117 *canonical = alphaType; |
| 69 } | 118 } |
| 70 return true; | 119 return true; |
| 71 } | 120 } |
| OLD | NEW |