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 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 8 | #include "SkImageInfoPriv.h" |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 9 | #include "SkSafeMath.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 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 13 | int SkColorTypeBytesPerPixel(SkColorType ct) { |
| 14 | switch (ct) { |
| 15 | case kUnknown_SkColorType: return 0; |
| 16 | case kAlpha_8_SkColorType: return 1; |
| 17 | case kRGB_565_SkColorType: return 2; |
| 18 | case kARGB_4444_SkColorType: return 2; |
| 19 | case kRGBA_8888_SkColorType: return 4; |
| 20 | case kBGRA_8888_SkColorType: return 4; |
| 21 | case kRGB_888x_SkColorType: return 4; |
| 22 | case kRGBA_1010102_SkColorType: return 4; |
| 23 | case kRGB_101010x_SkColorType: return 4; |
| 24 | case kGray_8_SkColorType: return 1; |
| 25 | case kRGBA_F16_SkColorType: return 8; |
| 26 | } |
| 27 | return 0; |
| 28 | } |
| 29 | |
Mike Reed | 5805013 | 2017-07-12 22:10:29 -0400 | [diff] [blame] | 30 | // These values must be constant over revisions, though they can be renamed to reflect if/when |
| 31 | // they are deprecated. |
| 32 | enum Stored_SkColorType { |
| 33 | kUnknown_Stored_SkColorType = 0, |
| 34 | kAlpha_8_Stored_SkColorType = 1, |
| 35 | kRGB_565_Stored_SkColorType = 2, |
| 36 | kARGB_4444_Stored_SkColorType = 3, |
| 37 | kRGBA_8888_Stored_SkColorType = 4, |
| 38 | kBGRA_8888_Stored_SkColorType = 5, |
| 39 | kIndex_8_Stored_SkColorType_DEPRECATED = 6, |
| 40 | kGray_8_Stored_SkColorType = 7, |
| 41 | kRGBA_F16_Stored_SkColorType = 8, |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 42 | kRGB_888x_Stored_SkColorType = 9, |
| 43 | kRGBA_1010102_Stored_SkColorType = 10, |
| 44 | kRGB_101010x_Stored_SkColorType = 11, |
Mike Reed | 5805013 | 2017-07-12 22:10:29 -0400 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static uint8_t live_to_stored(unsigned ct) { |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 48 | switch (ct) { |
| 49 | case kUnknown_SkColorType: return kUnknown_Stored_SkColorType; |
| 50 | case kAlpha_8_SkColorType: return kAlpha_8_Stored_SkColorType; |
| 51 | case kRGB_565_SkColorType: return kRGB_565_Stored_SkColorType; |
| 52 | case kARGB_4444_SkColorType: return kARGB_4444_Stored_SkColorType; |
| 53 | case kRGBA_8888_SkColorType: return kRGBA_8888_Stored_SkColorType; |
| 54 | case kRGB_888x_SkColorType: return kRGB_888x_Stored_SkColorType; |
| 55 | case kBGRA_8888_SkColorType: return kBGRA_8888_Stored_SkColorType; |
| 56 | case kRGBA_1010102_SkColorType: return kRGBA_1010102_Stored_SkColorType; |
| 57 | case kRGB_101010x_SkColorType: return kRGB_101010x_Stored_SkColorType; |
| 58 | case kGray_8_SkColorType: return kGray_8_Stored_SkColorType; |
| 59 | case kRGBA_F16_SkColorType: return kRGBA_F16_Stored_SkColorType; |
Mike Reed | b3d1d90 | 2017-07-17 10:36:33 -0400 | [diff] [blame] | 60 | } |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 61 | return kUnknown_Stored_SkColorType; |
Mike Reed | 5805013 | 2017-07-12 22:10:29 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static SkColorType stored_to_live(unsigned stored) { |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 65 | switch (stored) { |
| 66 | case kUnknown_Stored_SkColorType: return kUnknown_SkColorType; |
| 67 | case kAlpha_8_Stored_SkColorType: return kAlpha_8_SkColorType; |
| 68 | case kRGB_565_Stored_SkColorType: return kRGB_565_SkColorType; |
| 69 | case kARGB_4444_Stored_SkColorType: return kARGB_4444_SkColorType; |
| 70 | case kRGBA_8888_Stored_SkColorType: return kRGBA_8888_SkColorType; |
| 71 | case kRGB_888x_Stored_SkColorType: return kRGB_888x_SkColorType; |
| 72 | case kBGRA_8888_Stored_SkColorType: return kBGRA_8888_SkColorType; |
| 73 | case kRGBA_1010102_Stored_SkColorType: return kRGBA_1010102_SkColorType; |
| 74 | case kRGB_101010x_Stored_SkColorType: return kRGB_101010x_SkColorType; |
| 75 | case kIndex_8_Stored_SkColorType_DEPRECATED: return kUnknown_SkColorType; |
| 76 | case kGray_8_Stored_SkColorType: return kGray_8_SkColorType; |
| 77 | case kRGBA_F16_Stored_SkColorType: return kRGBA_F16_SkColorType; |
Mike Reed | b3d1d90 | 2017-07-17 10:36:33 -0400 | [diff] [blame] | 78 | } |
Brian Salomon | e41e176 | 2018-01-25 14:07:47 -0500 | [diff] [blame] | 79 | return kUnknown_SkColorType; |
Mike Reed | 5805013 | 2017-07-12 22:10:29 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 83 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame^] | 84 | int SkImageInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); } |
| 85 | |
| 86 | int SkImageInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); } |
| 87 | |
| 88 | size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const { |
| 89 | SkASSERT((unsigned)x < (unsigned)fWidth); |
| 90 | SkASSERT((unsigned)y < (unsigned)fHeight); |
| 91 | return SkColorTypeComputeOffset(fColorType, x, y, rowBytes); |
| 92 | } |
| 93 | |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 94 | size_t SkImageInfo::computeByteSize(size_t rowBytes) const { |
| 95 | if (0 == fHeight) { |
| 96 | return 0; |
| 97 | } |
| 98 | SkSafeMath safe; |
| 99 | size_t bytes = safe.add(safe.mul(fHeight - 1, rowBytes), |
| 100 | safe.mul(fWidth, this->bytesPerPixel())); |
Mike Reed | 7281801 | 2017-10-09 11:37:44 -0400 | [diff] [blame] | 101 | return safe ? bytes : SK_MaxSizeT; |
Mike Reed | e74dafc | 2017-09-29 13:41:58 -0400 | [diff] [blame] | 102 | } |
| 103 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 104 | static bool alpha_type_is_valid(SkAlphaType alphaType) { |
Petr Hosek | 8bc6917 | 2017-09-09 01:54:43 -0700 | [diff] [blame] | 105 | return (alphaType >= kUnknown_SkAlphaType) && (alphaType <= kLastEnum_SkAlphaType); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 108 | static bool color_type_is_valid(SkColorType colorType) { |
Petr Hosek | 8bc6917 | 2017-09-09 01:54:43 -0700 | [diff] [blame] | 109 | return (colorType >= kUnknown_SkColorType) && (colorType <= kLastEnum_SkColorType); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 110 | } |
| 111 | |
reed | fbce71f | 2016-06-02 12:40:22 -0700 | [diff] [blame] | 112 | SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) { |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 113 | return SkImageInfo(width, height, kN32_SkColorType, at, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 114 | SkColorSpace::MakeSRGB()); |
reed | fbce71f | 2016-06-02 12:40:22 -0700 | [diff] [blame] | 115 | } |
| 116 | |
robertphillips | c31bb87 | 2016-06-28 11:02:30 -0700 | [diff] [blame] | 117 | static const int kColorTypeMask = 0x0F; |
| 118 | static const int kAlphaTypeMask = 0x03; |
| 119 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 120 | void SkImageInfo::unflatten(SkReadBuffer& buffer) { |
| 121 | fWidth = buffer.read32(); |
| 122 | fHeight = buffer.read32(); |
| 123 | |
| 124 | uint32_t packed = buffer.read32(); |
Mike Reed | b3d1d90 | 2017-07-17 10:36:33 -0400 | [diff] [blame] | 125 | fColorType = stored_to_live((packed >> 0) & kColorTypeMask); |
robertphillips | c31bb87 | 2016-06-28 11:02:30 -0700 | [diff] [blame] | 126 | fAlphaType = (SkAlphaType)((packed >> 8) & kAlphaTypeMask); |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 127 | buffer.validate(alpha_type_is_valid(fAlphaType) && color_type_is_valid(fColorType)); |
| 128 | |
msarett | 111a42d | 2016-06-22 08:18:54 -0700 | [diff] [blame] | 129 | sk_sp<SkData> data = buffer.readByteArrayAsData(); |
| 130 | fColorSpace = SkColorSpace::Deserialize(data->data(), data->size()); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 131 | } |
| 132 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 133 | void SkImageInfo::flatten(SkWriteBuffer& buffer) const { |
| 134 | buffer.write32(fWidth); |
| 135 | buffer.write32(fHeight); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 136 | |
robertphillips | c31bb87 | 2016-06-28 11:02:30 -0700 | [diff] [blame] | 137 | SkASSERT(0 == (fAlphaType & ~kAlphaTypeMask)); |
| 138 | SkASSERT(0 == (fColorType & ~kColorTypeMask)); |
Mike Reed | 5805013 | 2017-07-12 22:10:29 -0400 | [diff] [blame] | 139 | uint32_t packed = (fAlphaType << 8) | live_to_stored(fColorType); |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 140 | buffer.write32(packed); |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 141 | |
msarett | 111a42d | 2016-06-22 08:18:54 -0700 | [diff] [blame] | 142 | if (fColorSpace) { |
| 143 | sk_sp<SkData> data = fColorSpace->serialize(); |
| 144 | if (data) { |
| 145 | buffer.writeDataAsByteArray(data.get()); |
| 146 | } else { |
| 147 | buffer.writeByteArray(nullptr, 0); |
| 148 | } |
| 149 | } else { |
| 150 | sk_sp<SkData> data = SkData::MakeEmpty(); |
| 151 | buffer.writeDataAsByteArray(data.get()); |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 152 | } |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 153 | } |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 154 | |
| 155 | bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
| 156 | SkAlphaType* canonical) { |
| 157 | switch (colorType) { |
| 158 | case kUnknown_SkColorType: |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 159 | alphaType = kUnknown_SkAlphaType; |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 160 | break; |
| 161 | case kAlpha_8_SkColorType: |
| 162 | if (kUnpremul_SkAlphaType == alphaType) { |
| 163 | alphaType = kPremul_SkAlphaType; |
| 164 | } |
| 165 | // fall-through |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 166 | case kARGB_4444_SkColorType: |
| 167 | case kRGBA_8888_SkColorType: |
| 168 | case kBGRA_8888_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 169 | case kRGBA_1010102_SkColorType: |
reed | 9e01ac7 | 2016-02-06 20:38:45 -0800 | [diff] [blame] | 170 | case kRGBA_F16_SkColorType: |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 171 | if (kUnknown_SkAlphaType == alphaType) { |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | break; |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 175 | case kGray_8_SkColorType: |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 176 | case kRGB_565_SkColorType: |
| 177 | case kRGB_888x_SkColorType: |
| 178 | case kRGB_101010x_SkColorType: |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 179 | alphaType = kOpaque_SkAlphaType; |
| 180 | break; |
| 181 | default: |
| 182 | return false; |
| 183 | } |
| 184 | if (canonical) { |
| 185 | *canonical = alphaType; |
| 186 | } |
| 187 | return true; |
| 188 | } |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 189 | |
| 190 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 191 | |
| 192 | #include "SkReadPixelsRec.h" |
| 193 | |
| 194 | bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 196 | return false; |
| 197 | } |
Matt Sarett | cb6266b | 2017-01-17 10:48:53 -0500 | [diff] [blame] | 198 | if (0 >= fInfo.width() || 0 >= fInfo.height()) { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | |
| 202 | int x = fX; |
| 203 | int y = fY; |
| 204 | SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height()); |
| 205 | if (!srcR.intersect(0, 0, srcWidth, srcHeight)) { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // if x or y are negative, then we have to adjust pixels |
| 210 | if (x > 0) { |
| 211 | x = 0; |
| 212 | } |
| 213 | if (y > 0) { |
| 214 | y = 0; |
| 215 | } |
| 216 | // here x,y are either 0 or negative |
| 217 | fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); |
| 218 | // the intersect may have shrunk info's logical size |
| 219 | fInfo = fInfo.makeWH(srcR.width(), srcR.height()); |
| 220 | fX = srcR.x(); |
| 221 | fY = srcR.y(); |
| 222 | |
| 223 | return true; |
| 224 | } |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 225 | |
| 226 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | #include "SkWritePixelsRec.h" |
| 229 | |
| 230 | bool SkWritePixelsRec::trim(int dstWidth, int dstHeight) { |
| 231 | if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { |
| 232 | return false; |
| 233 | } |
| 234 | if (0 >= fInfo.width() || 0 >= fInfo.height()) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | int x = fX; |
| 239 | int y = fY; |
| 240 | SkIRect dstR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height()); |
| 241 | if (!dstR.intersect(0, 0, dstWidth, dstHeight)) { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | // if x or y are negative, then we have to adjust pixels |
| 246 | if (x > 0) { |
| 247 | x = 0; |
| 248 | } |
| 249 | if (y > 0) { |
| 250 | y = 0; |
| 251 | } |
| 252 | // here x,y are either 0 or negative |
| 253 | fPixels = ((const char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); |
| 254 | // the intersect may have shrunk info's logical size |
| 255 | fInfo = fInfo.makeWH(dstR.width(), dstR.height()); |
| 256 | fX = dstR.x(); |
| 257 | fY = dstR.y(); |
| 258 | |
| 259 | return true; |
| 260 | } |