blob: b90f858562aa8b5f97a495c8ec25afcbec058fe7 [file] [log] [blame]
reed@google.comf309dbc2013-12-09 22:09:41 +00001/*
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.org8b0e8ac2014-01-30 18:58:24 +00009#include "SkReadBuffer.h"
10#include "SkWriteBuffer.h"
reed@google.comf309dbc2013-12-09 22:09:41 +000011
reed7c748852014-11-10 08:57:21 -080012static bool profile_type_is_valid(SkColorProfileType profileType) {
13 return (profileType >= 0) && (profileType <= kLastEnum_SkColorProfileType);
14}
15
reed2bdf1f52014-09-03 05:48:56 -070016static bool alpha_type_is_valid(SkAlphaType alphaType) {
17 return (alphaType >= 0) && (alphaType <= kLastEnum_SkAlphaType);
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +000018}
19
reed2bdf1f52014-09-03 05:48:56 -070020static bool color_type_is_valid(SkColorType colorType) {
21 return (colorType >= 0) && (colorType <= kLastEnum_SkColorType);
commit-bot@chromium.orgef74fa12013-12-17 20:49:46 +000022}
23
reed2bdf1f52014-09-03 05:48:56 -070024void SkImageInfo::unflatten(SkReadBuffer& buffer) {
25 fWidth = buffer.read32();
26 fHeight = buffer.read32();
27
28 uint32_t packed = buffer.read32();
reed7c748852014-11-10 08:57:21 -080029 SkASSERT(0 == (packed >> 24));
30 fProfileType = (SkColorProfileType)((packed >> 16) & 0xFF);
reed2bdf1f52014-09-03 05:48:56 -070031 fAlphaType = (SkAlphaType)((packed >> 8) & 0xFF);
32 fColorType = (SkColorType)((packed >> 0) & 0xFF);
reed7c748852014-11-10 08:57:21 -080033 buffer.validate(profile_type_is_valid(fProfileType) &&
34 alpha_type_is_valid(fAlphaType) &&
reed2bdf1f52014-09-03 05:48:56 -070035 color_type_is_valid(fColorType));
reed@google.comf309dbc2013-12-09 22:09:41 +000036}
37
reed2bdf1f52014-09-03 05:48:56 -070038void SkImageInfo::flatten(SkWriteBuffer& buffer) const {
39 buffer.write32(fWidth);
40 buffer.write32(fHeight);
reed@google.comf309dbc2013-12-09 22:09:41 +000041
reed7c748852014-11-10 08:57:21 -080042 SkASSERT(0 == (fProfileType & ~0xFF));
reed2bdf1f52014-09-03 05:48:56 -070043 SkASSERT(0 == (fAlphaType & ~0xFF));
44 SkASSERT(0 == (fColorType & ~0xFF));
reed7c748852014-11-10 08:57:21 -080045 uint32_t packed = (fProfileType << 16) | (fAlphaType << 8) | fColorType;
reed2bdf1f52014-09-03 05:48:56 -070046 buffer.write32(packed);
reed@google.comf309dbc2013-12-09 22:09:41 +000047}
scroggo2fd0d142014-07-01 07:08:19 -070048
49bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
50 SkAlphaType* canonical) {
51 switch (colorType) {
52 case kUnknown_SkColorType:
reed44977482015-02-27 10:23:00 -080053 alphaType = kUnknown_SkAlphaType;
scroggo2fd0d142014-07-01 07:08:19 -070054 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:
reed44977482015-02-27 10:23:00 -080064 if (kUnknown_SkAlphaType == alphaType) {
scroggo2fd0d142014-07-01 07:08:19 -070065 return false;
66 }
67 break;
68 case kRGB_565_SkColorType:
reed0c9b1a82015-03-17 17:44:06 -070069 case kGray_8_SkColorType:
scroggo2fd0d142014-07-01 07:08:19 -070070 alphaType = kOpaque_SkAlphaType;
71 break;
72 default:
73 return false;
74 }
75 if (canonical) {
76 *canonical = alphaType;
77 }
78 return true;
79}
reed96472de2014-12-10 09:53:42 -080080
81///////////////////////////////////////////////////////////////////////////////////////////////////
82
83#include "SkReadPixelsRec.h"
84
85bool 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 }
halcanary96fcdcc2015-08-27 07:41:13 -070093 if (nullptr == fPixels || fRowBytes < fInfo.minRowBytes()) {
reed96472de2014-12-10 09:53:42 -080094 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