blob: 01318fd1aa95e5b64f445bcf952a2b8adadc4a88 [file] [log] [blame]
reed@google.com3443fd82013-11-13 19:09:13 +00001/*
2 * Copyright 2013 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#ifndef SkImageInfo_DEFINED
9#define SkImageInfo_DEFINED
10
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000011#include "SkMath.h"
halcanaryf622a6c2014-10-24 12:54:53 -070012#include "SkRect.h"
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000013#include "SkSize.h"
reed@google.com3443fd82013-11-13 19:09:13 +000014
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000015class SkReadBuffer;
halcanaryf622a6c2014-10-24 12:54:53 -070016class SkWriteBuffer;
reed@google.com9230ea22013-12-09 22:01:03 +000017
reed@google.com3443fd82013-11-13 19:09:13 +000018/**
19 * Describes how to interpret the alpha compoent of a pixel.
20 */
21enum SkAlphaType {
reed44977482015-02-27 10:23:00 -080022 kUnknown_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000023
reed@google.com3443fd82013-11-13 19:09:13 +000024 /**
25 * All pixels are stored as opaque. This differs slightly from kIgnore in
26 * that kOpaque has correct "opaque" values stored in the pixels, while
27 * kIgnore may not, but in both cases the caller should treat the pixels
28 * as opaque.
29 */
30 kOpaque_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000031
reed@google.com3443fd82013-11-13 19:09:13 +000032 /**
33 * All pixels have their alpha premultiplied in their color components.
34 * This is the natural format for the rendering target pixels.
35 */
36 kPremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000037
reed@google.com3443fd82013-11-13 19:09:13 +000038 /**
39 * All pixels have their color components stored without any regard to the
40 * alpha. e.g. this is the default configuration for PNG images.
41 *
42 * This alpha-type is ONLY supported for input images. Rendering cannot
43 * generate this on output.
44 */
45 kUnpremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000046
reed@google.com3443fd82013-11-13 19:09:13 +000047 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
48};
49
50static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
reed44977482015-02-27 10:23:00 -080051 return kOpaque_SkAlphaType == at;
reed@google.com3443fd82013-11-13 19:09:13 +000052}
53
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000054static inline bool SkAlphaTypeIsValid(unsigned value) {
55 return value <= kLastEnum_SkAlphaType;
56}
57
reed@google.com3443fd82013-11-13 19:09:13 +000058///////////////////////////////////////////////////////////////////////////////
59
60/**
61 * Describes how to interpret the components of a pixel.
reed6c6ddb82014-06-30 12:44:03 -070062 *
63 * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native"
64 * form for skia's blitters. Use this if you don't have a swizzle preference
65 * for 32bit pixels.
reed@google.com3443fd82013-11-13 19:09:13 +000066 */
67enum SkColorType {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000068 kUnknown_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000069 kAlpha_8_SkColorType,
70 kRGB_565_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000071 kARGB_4444_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000072 kRGBA_8888_SkColorType,
73 kBGRA_8888_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000074 kIndex_8_SkColorType,
reed0c9b1a82015-03-17 17:44:06 -070075 kGray_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000076
reed0c9b1a82015-03-17 17:44:06 -070077 kLastEnum_SkColorType = kGray_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000078
79#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000080 kN32_SkColorType = kBGRA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000081#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000082 kN32_SkColorType = kRGBA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000083#else
reed3b40ee62014-12-21 14:29:04 -080084 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000085#endif
reed@google.com3443fd82013-11-13 19:09:13 +000086};
87
88static int SkColorTypeBytesPerPixel(SkColorType ct) {
89 static const uint8_t gSize[] = {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000090 0, // Unknown
reed@google.com3443fd82013-11-13 19:09:13 +000091 1, // Alpha_8
92 2, // RGB_565
reed@google.com9230ea22013-12-09 22:01:03 +000093 2, // ARGB_4444
reed@google.com3443fd82013-11-13 19:09:13 +000094 4, // RGBA_8888
95 4, // BGRA_8888
96 1, // kIndex_8
reed0c9b1a82015-03-17 17:44:06 -070097 1, // kGray_8
reed@google.com3443fd82013-11-13 19:09:13 +000098 };
99 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
100 size_mismatch_with_SkColorType_enum);
101
102 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
103 return gSize[ct];
104}
105
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000106static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
107 return width * SkColorTypeBytesPerPixel(ct);
108}
109
110static inline bool SkColorTypeIsValid(unsigned value) {
111 return value <= kLastEnum_SkColorType;
112}
113
reed@google.com3443fd82013-11-13 19:09:13 +0000114///////////////////////////////////////////////////////////////////////////////
115
116/**
scroggo2fd0d142014-07-01 07:08:19 -0700117 * Return true if alphaType is supported by colorType. If there is a canonical
118 * alphaType for this colorType, return it in canonical.
119 */
120bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
121 SkAlphaType* canonical = NULL);
122
123///////////////////////////////////////////////////////////////////////////////
124
125/**
rileyaabaef862014-09-12 17:45:58 -0700126 * Describes the color space a YUV pixel.
127 */
128enum SkYUVColorSpace {
129 /** Standard JPEG color space. */
130 kJPEG_SkYUVColorSpace,
131 /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color
132 range. See http://en.wikipedia.org/wiki/Rec._601 for details. */
133 kRec601_SkYUVColorSpace,
134
135 kLastEnum_SkYUVColorSpace = kRec601_SkYUVColorSpace
136};
137
138///////////////////////////////////////////////////////////////////////////////
139
reed7c748852014-11-10 08:57:21 -0800140enum SkColorProfileType {
141 kLinear_SkColorProfileType,
142 kSRGB_SkColorProfileType,
143
144 kLastEnum_SkColorProfileType = kSRGB_SkColorProfileType
145};
146
rileyaabaef862014-09-12 17:45:58 -0700147/**
reed@google.com3443fd82013-11-13 19:09:13 +0000148 * Describe an image's dimensions and pixel type.
reed7c748852014-11-10 08:57:21 -0800149 * Used for both src images and render-targets (surfaces).
reed@google.com3443fd82013-11-13 19:09:13 +0000150 */
reed41814922015-01-30 14:54:38 -0800151struct SK_API SkImageInfo {
reede5ea5002014-09-03 11:54:58 -0700152public:
153 SkImageInfo()
154 : fWidth(0)
155 , fHeight(0)
156 , fColorType(kUnknown_SkColorType)
reed44977482015-02-27 10:23:00 -0800157 , fAlphaType(kUnknown_SkAlphaType)
reed7c748852014-11-10 08:57:21 -0800158 , fProfileType(kLinear_SkColorProfileType)
reede5ea5002014-09-03 11:54:58 -0700159 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000160
reed7c748852014-11-10 08:57:21 -0800161 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
162 SkColorProfileType pt = kLinear_SkColorProfileType) {
163 return SkImageInfo(width, height, ct, at, pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000164 }
165
166 /**
167 * Sets colortype to the native ARGB32 type.
168 */
reed7c748852014-11-10 08:57:21 -0800169 static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
170 SkColorProfileType pt = kLinear_SkColorProfileType) {
171 return SkImageInfo(width, height, kN32_SkColorType, at, pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000172 }
173
174 /**
175 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
176 */
reed7c748852014-11-10 08:57:21 -0800177 static SkImageInfo MakeN32Premul(int width, int height,
178 SkColorProfileType pt = kLinear_SkColorProfileType) {
179 return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType, pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000180 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000181
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000182 /**
183 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
184 */
reed7c748852014-11-10 08:57:21 -0800185 static SkImageInfo MakeN32Premul(const SkISize& size,
186 SkColorProfileType pt = kLinear_SkColorProfileType) {
187 return MakeN32Premul(size.width(), size.height(), pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000188 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000189
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000190 static SkImageInfo MakeA8(int width, int height) {
reed7c748852014-11-10 08:57:21 -0800191 return SkImageInfo(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType,
192 kLinear_SkColorProfileType);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000193 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000194
reed@google.com900ecf22014-02-20 20:55:37 +0000195 static SkImageInfo MakeUnknown(int width, int height) {
reed44977482015-02-27 10:23:00 -0800196 return SkImageInfo(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType,
reed7c748852014-11-10 08:57:21 -0800197 kLinear_SkColorProfileType);
reed@google.com900ecf22014-02-20 20:55:37 +0000198 }
199
reedf252f642014-06-14 04:24:56 -0700200 static SkImageInfo MakeUnknown() {
reede5ea5002014-09-03 11:54:58 -0700201 return SkImageInfo();
reedf252f642014-06-14 04:24:56 -0700202 }
203
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000204 int width() const { return fWidth; }
205 int height() const { return fHeight; }
206 SkColorType colorType() const { return fColorType; }
207 SkAlphaType alphaType() const { return fAlphaType; }
reed7c748852014-11-10 08:57:21 -0800208 SkColorProfileType profileType() const { return fProfileType; }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000209
210 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
211
reed@google.com3443fd82013-11-13 19:09:13 +0000212 bool isOpaque() const {
213 return SkAlphaTypeIsOpaque(fAlphaType);
214 }
215
reed7c748852014-11-10 08:57:21 -0800216 bool isLinear() const { return kLinear_SkColorProfileType == fProfileType; }
217 bool isSRGB() const { return kSRGB_SkColorProfileType == fProfileType; }
218
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000219 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
reed7c748852014-11-10 08:57:21 -0800220 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000221
reedc77392e2014-06-02 13:07:26 -0700222 /**
223 * Return a new ImageInfo with the same colortype and alphatype as this info,
224 * but with the specified width and height.
225 */
226 SkImageInfo makeWH(int newWidth, int newHeight) const {
reed7c748852014-11-10 08:57:21 -0800227 return SkImageInfo::Make(newWidth, newHeight, fColorType, fAlphaType, fProfileType);
reedc77392e2014-06-02 13:07:26 -0700228 }
229
reede5ea5002014-09-03 11:54:58 -0700230 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
reed7c748852014-11-10 08:57:21 -0800231 return SkImageInfo::Make(fWidth, fHeight, fColorType, newAlphaType, fProfileType);
reede5ea5002014-09-03 11:54:58 -0700232 }
233
234 SkImageInfo makeColorType(SkColorType newColorType) const {
reed7c748852014-11-10 08:57:21 -0800235 return SkImageInfo::Make(fWidth, fHeight, newColorType, fAlphaType, fProfileType);
reede5ea5002014-09-03 11:54:58 -0700236 }
reed7c748852014-11-10 08:57:21 -0800237
reed@google.com3443fd82013-11-13 19:09:13 +0000238 int bytesPerPixel() const {
239 return SkColorTypeBytesPerPixel(fColorType);
240 }
241
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000242 uint64_t minRowBytes64() const {
243 return sk_64_mul(fWidth, this->bytesPerPixel());
244 }
245
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000246 size_t minRowBytes() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000247 return (size_t)this->minRowBytes64();
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000248 }
skia.committer@gmail.com8ed64432014-02-11 03:02:13 +0000249
reed@google.com3443fd82013-11-13 19:09:13 +0000250 bool operator==(const SkImageInfo& other) const {
251 return 0 == memcmp(this, &other, sizeof(other));
252 }
253 bool operator!=(const SkImageInfo& other) const {
254 return 0 != memcmp(this, &other, sizeof(other));
255 }
reed@google.com9230ea22013-12-09 22:01:03 +0000256
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000257 void unflatten(SkReadBuffer&);
258 void flatten(SkWriteBuffer&) const;
reed@google.com9230ea22013-12-09 22:01:03 +0000259
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000260 int64_t getSafeSize64(size_t rowBytes) const {
reed@google.com9230ea22013-12-09 22:01:03 +0000261 if (0 == fHeight) {
262 return 0;
263 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000264 return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel();
reed@google.com9230ea22013-12-09 22:01:03 +0000265 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000266
267 size_t getSafeSize(size_t rowBytes) const {
268 return (size_t)this->getSafeSize64(rowBytes);
269 }
270
271 bool validRowBytes(size_t rowBytes) const {
272 uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
273 return rowBytes >= rb;
274 }
275
276 SkDEBUGCODE(void validate() const;)
reede5ea5002014-09-03 11:54:58 -0700277
278#ifdef SK_SUPPORT_LEGACY_PUBLIC_IMAGEINFO_FIELDS
279public:
280#else
281private:
282#endif
reed7c748852014-11-10 08:57:21 -0800283 int fWidth;
284 int fHeight;
285 SkColorType fColorType;
286 SkAlphaType fAlphaType;
reede5ea5002014-09-03 11:54:58 -0700287
288private:
reed7c748852014-11-10 08:57:21 -0800289 SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, SkColorProfileType pt)
reede5ea5002014-09-03 11:54:58 -0700290 : fWidth(width)
291 , fHeight(height)
292 , fColorType(ct)
293 , fAlphaType(at)
reed7c748852014-11-10 08:57:21 -0800294 , fProfileType(pt)
reede5ea5002014-09-03 11:54:58 -0700295 {}
reed7c748852014-11-10 08:57:21 -0800296
297 SkColorProfileType fProfileType;
reed@google.com3443fd82013-11-13 19:09:13 +0000298};
299
300#endif