reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkImageInfo.h" |
halcanary | 484b3d0 | 2016-04-25 10:32:23 -0700 | [diff] [blame] | 9 | #include "SkImageInfoPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 12 | |
halcanary | 484b3d0 | 2016-04-25 10:32:23 -0700 | [diff] [blame] | 13 | // Indicate how images and gradients should interpret colors by default. |
herb | 835acc7 | 2016-05-17 06:50:15 -0700 | [diff] [blame] | 14 | bool gDefaultProfileIsSRGB; |
halcanary | 484b3d0 | 2016-04-25 10:32:23 -0700 | [diff] [blame] | 15 | |
| 16 | SkColorProfileType SkDefaultColorProfile() { |
| 17 | return gDefaultProfileIsSRGB ? kSRGB_SkColorProfileType |
| 18 | : kLinear_SkColorProfileType; |
| 19 | } |
herb | 9bc2235 | 2016-04-20 15:07:32 -0700 | [diff] [blame] | 20 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 21 | static bool profile_type_is_valid(SkColorProfileType profileType) { |
| 22 | return (profileType >= 0) && (profileType <= kLastEnum_SkColorProfileType); |
| 23 | } |
| 24 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 25 | static bool alpha_type_is_valid(SkAlphaType alphaType) { |
| 26 | return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 27 | } |
| 28 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 29 | static bool color_type_is_valid(SkColorType colorType) { |
| 30 | return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 31 | } |
| 32 | |
msarett | 23c5110 | 2016-05-27 07:39:02 -0700 | [diff] [blame] | 33 | SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at, |
| 34 | sk_sp<SkColorSpace> cs) { |
| 35 | return SkImageInfo(width, height, ct, at, SkDefaultColorProfile(), std::move(cs)); |
| 36 | } |
| 37 | |
reed | fbce71f | 2016-06-02 12:40:22 -0700 | [diff] [blame] | 38 | SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) { |
| 39 | return SkImageInfo(width, height, kN32_SkColorType, at, kSRGB_SkColorProfileType, |
| 40 | SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)); |
| 41 | } |
| 42 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 43 | void SkImageInfo::unflatten(SkReadBuffer& buffer) { |
| 44 | fWidth = buffer.read32(); |
| 45 | fHeight = buffer.read32(); |
| 46 | |
| 47 | uint32_t packed = buffer.read32(); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 48 | SkASSERT(0 == (packed >> 24)); |
| 49 | fProfileType = (SkColorProfileType)((packed >> 16) & 0xFF); |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 50 | fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); |
| 51 | fColorType = (SkColorType)((packed >> 0) & 0xFF); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 52 | buffer.validate(profile_type_is_valid(fProfileType) && |
| 53 | alpha_type_is_valid(fAlphaType) && |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 54 | color_type_is_valid(fColorType)); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 55 | } |
| 56 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 57 | void SkImageInfo::flatten(SkWriteBuffer& buffer) const { |
| 58 | buffer.write32(fWidth); |
| 59 | buffer.write32(fHeight); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 60 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 61 | SkASSERT(0 == (fProfileType & ~0xFF)); |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 62 | SkASSERT(0 == (fAlphaType & ~0xFF)); |
| 63 | SkASSERT(0 == (fColorType & ~0xFF)); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 64 | uint32_t packed = (fProfileType << 16) | (fAlphaType << 8) | fColorType; |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 65 | buffer.write32(packed); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 66 | } |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 67 | |
| 68 | bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
| 69 | SkAlphaType* canonical) { |
| 70 | switch (colorType) { |
| 71 | case kUnknown_SkColorType: |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 72 | alphaType = kUnknown_SkAlphaType; |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 73 | break; |
| 74 | case kAlpha_8_SkColorType: |
| 75 | if (kUnpremul_SkAlphaType == alphaType) { |
| 76 | alphaType = kPremul_SkAlphaType; |
| 77 | } |
| 78 | // fall-through |
| 79 | case kIndex_8_SkColorType: |
| 80 | case kARGB_4444_SkColorType: |
| 81 | case kRGBA_8888_SkColorType: |
| 82 | case kBGRA_8888_SkColorType: |
reed | 9e01ac7 | 2016-02-06 20:38:45 -0800 | [diff] [blame] | 83 | case kRGBA_F16_SkColorType: |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 84 | if (kUnknown_SkAlphaType == alphaType) { |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | break; |
| 88 | case kRGB_565_SkColorType: |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 89 | case kGray_8_SkColorType: |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 90 | alphaType = kOpaque_SkAlphaType; |
| 91 | break; |
| 92 | default: |
| 93 | return false; |
| 94 | } |
| 95 | if (canonical) { |
| 96 | *canonical = alphaType; |
| 97 | } |
| 98 | return true; |
| 99 | } |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | #include "SkReadPixelsRec.h" |
| 104 | |
| 105 | bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) { |
| 106 | switch (fInfo.colorType()) { |
| 107 | case kUnknown_SkColorType: |
| 108 | case kIndex_8_SkColorType: |
| 109 | return false; |
| 110 | default: |
| 111 | break; |
| 112 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | if (0 == fInfo.width() || 0 == fInfo.height()) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | int x = fX; |
| 121 | int y = fY; |
| 122 | SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height()); |
| 123 | if (!srcR.intersect(0, 0, srcWidth, srcHeight)) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | // if x or y are negative, then we have to adjust pixels |
| 128 | if (x > 0) { |
| 129 | x = 0; |
| 130 | } |
| 131 | if (y > 0) { |
| 132 | y = 0; |
| 133 | } |
| 134 | // here x,y are either 0 or negative |
| 135 | fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); |
| 136 | // the intersect may have shrunk info's logical size |
| 137 | fInfo = fInfo.makeWH(srcR.width(), srcR.height()); |
| 138 | fX = srcR.x(); |
| 139 | fY = srcR.y(); |
| 140 | |
| 141 | return true; |
| 142 | } |