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" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 9 | #include "SkReadBuffer.h" |
| 10 | #include "SkWriteBuffer.h" |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 11 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 12 | static bool profile_type_is_valid(SkColorProfileType profileType) { |
| 13 | return (profileType >= 0) && (profileType <= kLastEnum_SkColorProfileType); |
| 14 | } |
| 15 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 16 | static bool alpha_type_is_valid(SkAlphaType alphaType) { |
| 17 | return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 18 | } |
| 19 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 20 | static bool color_type_is_valid(SkColorType colorType) { |
| 21 | return (colorType >= 0) && (colorType <= kLastEnum_SkColorType); |
commit-bot@chromium.org | ef74fa1 | 2013-12-17 20:49:46 +0000 | [diff] [blame] | 22 | } |
| 23 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 24 | void SkImageInfo::unflatten(SkReadBuffer& buffer) { |
| 25 | fWidth = buffer.read32(); |
| 26 | fHeight = buffer.read32(); |
| 27 | |
| 28 | uint32_t packed = buffer.read32(); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 29 | SkASSERT(0 == (packed >> 24)); |
| 30 | fProfileType = (SkColorProfileType)((packed >> 16) & 0xFF); |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 31 | fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF); |
| 32 | fColorType = (SkColorType)((packed >> 0) & 0xFF); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 33 | buffer.validate(profile_type_is_valid(fProfileType) && |
| 34 | alpha_type_is_valid(fAlphaType) && |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 35 | color_type_is_valid(fColorType)); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 36 | } |
| 37 | |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 38 | void SkImageInfo::flatten(SkWriteBuffer& buffer) const { |
| 39 | buffer.write32(fWidth); |
| 40 | buffer.write32(fHeight); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 41 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 42 | SkASSERT(0 == (fProfileType & ~0xFF)); |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 43 | SkASSERT(0 == (fAlphaType & ~0xFF)); |
| 44 | SkASSERT(0 == (fColorType & ~0xFF)); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 45 | uint32_t packed = (fProfileType << 16) | (fAlphaType << 8) | fColorType; |
reed | 2bdf1f5 | 2014-09-03 05:48:56 -0700 | [diff] [blame] | 46 | buffer.write32(packed); |
reed@google.com | f309dbc | 2013-12-09 22:09:41 +0000 | [diff] [blame] | 47 | } |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 48 | |
| 49 | bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType, |
| 50 | SkAlphaType* canonical) { |
| 51 | switch (colorType) { |
| 52 | case kUnknown_SkColorType: |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 53 | alphaType = kUnknown_SkAlphaType; |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 54 | break; |
| 55 | case kAlpha_8_SkColorType: |
| 56 | if (kUnpremul_SkAlphaType == alphaType) { |
| 57 | alphaType = kPremul_SkAlphaType; |
| 58 | } |
| 59 | // fall-through |
| 60 | case kIndex_8_SkColorType: |
| 61 | case kARGB_4444_SkColorType: |
| 62 | case kRGBA_8888_SkColorType: |
| 63 | case kBGRA_8888_SkColorType: |
reed | 4497748 | 2015-02-27 10:23:00 -0800 | [diff] [blame] | 64 | if (kUnknown_SkAlphaType == alphaType) { |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | break; |
| 68 | case kRGB_565_SkColorType: |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 69 | case kGray_8_SkColorType: |
scroggo | 2fd0d14 | 2014-07-01 07:08:19 -0700 | [diff] [blame] | 70 | alphaType = kOpaque_SkAlphaType; |
| 71 | break; |
| 72 | default: |
| 73 | return false; |
| 74 | } |
| 75 | if (canonical) { |
| 76 | *canonical = alphaType; |
| 77 | } |
| 78 | return true; |
| 79 | } |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | #include "SkReadPixelsRec.h" |
| 84 | |
| 85 | bool SkReadPixelsRec::trim(int srcWidth, int srcHeight) { |
| 86 | switch (fInfo.colorType()) { |
| 87 | case kUnknown_SkColorType: |
| 88 | case kIndex_8_SkColorType: |
| 89 | return false; |
| 90 | default: |
| 91 | break; |
| 92 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 93 | if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) { |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | if (0 == fInfo.width() || 0 == fInfo.height()) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | int x = fX; |
| 101 | int y = fY; |
| 102 | SkIRect srcR = SkIRect::MakeXYWH(x, y, fInfo.width(), fInfo.height()); |
| 103 | if (!srcR.intersect(0, 0, srcWidth, srcHeight)) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | // if x or y are negative, then we have to adjust pixels |
| 108 | if (x > 0) { |
| 109 | x = 0; |
| 110 | } |
| 111 | if (y > 0) { |
| 112 | y = 0; |
| 113 | } |
| 114 | // here x,y are either 0 or negative |
| 115 | fPixels = ((char*)fPixels - y * fRowBytes - x * fInfo.bytesPerPixel()); |
| 116 | // the intersect may have shrunk info's logical size |
| 117 | fInfo = fInfo.makeWH(srcR.width(), srcR.height()); |
| 118 | fX = srcR.x(); |
| 119 | fY = srcR.y(); |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |