blob: 6fbaf6bbce19b4039683e75f90d123176398d1ab [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 {
22 /**
23 * All pixels should be treated as opaque, regardless of the value stored
24 * in their alpha field. Used for legacy images that wrote 0 or garbarge
25 * in their alpha field, but intended the RGB to be treated as opaque.
26 */
27 kIgnore_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000028
reed@google.com3443fd82013-11-13 19:09:13 +000029 /**
30 * All pixels are stored as opaque. This differs slightly from kIgnore in
31 * that kOpaque has correct "opaque" values stored in the pixels, while
32 * kIgnore may not, but in both cases the caller should treat the pixels
33 * as opaque.
34 */
35 kOpaque_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000036
reed@google.com3443fd82013-11-13 19:09:13 +000037 /**
38 * All pixels have their alpha premultiplied in their color components.
39 * This is the natural format for the rendering target pixels.
40 */
41 kPremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000042
reed@google.com3443fd82013-11-13 19:09:13 +000043 /**
44 * All pixels have their color components stored without any regard to the
45 * alpha. e.g. this is the default configuration for PNG images.
46 *
47 * This alpha-type is ONLY supported for input images. Rendering cannot
48 * generate this on output.
49 */
50 kUnpremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000051
reed@google.com3443fd82013-11-13 19:09:13 +000052 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
53};
54
55static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
56 SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_order);
57 SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order);
58 SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order);
59
60 return (unsigned)at <= kOpaque_SkAlphaType;
61}
62
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000063static inline bool SkAlphaTypeIsValid(unsigned value) {
64 return value <= kLastEnum_SkAlphaType;
65}
66
reed@google.com3443fd82013-11-13 19:09:13 +000067///////////////////////////////////////////////////////////////////////////////
68
69/**
70 * Describes how to interpret the components of a pixel.
reed6c6ddb82014-06-30 12:44:03 -070071 *
72 * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native"
73 * form for skia's blitters. Use this if you don't have a swizzle preference
74 * for 32bit pixels.
reed@google.com3443fd82013-11-13 19:09:13 +000075 */
76enum SkColorType {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000077 kUnknown_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000078 kAlpha_8_SkColorType,
79 kRGB_565_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000080 kARGB_4444_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000081 kRGBA_8888_SkColorType,
82 kBGRA_8888_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000083 kIndex_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000084
reed@google.com9230ea22013-12-09 22:01:03 +000085 kLastEnum_SkColorType = kIndex_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000086
87#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000088 kN32_SkColorType = kBGRA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000089#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000090 kN32_SkColorType = kRGBA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000091#else
92#error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
93#endif
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000094
95#ifdef SK_SUPPORT_LEGACY_N32_NAME
96 kPMColor_SkColorType = kN32_SkColorType
97#endif
reed@google.com3443fd82013-11-13 19:09:13 +000098};
99
100static int SkColorTypeBytesPerPixel(SkColorType ct) {
101 static const uint8_t gSize[] = {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000102 0, // Unknown
reed@google.com3443fd82013-11-13 19:09:13 +0000103 1, // Alpha_8
104 2, // RGB_565
reed@google.com9230ea22013-12-09 22:01:03 +0000105 2, // ARGB_4444
reed@google.com3443fd82013-11-13 19:09:13 +0000106 4, // RGBA_8888
107 4, // BGRA_8888
108 1, // kIndex_8
109 };
110 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
111 size_mismatch_with_SkColorType_enum);
112
113 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
114 return gSize[ct];
115}
116
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000117static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
118 return width * SkColorTypeBytesPerPixel(ct);
119}
120
121static inline bool SkColorTypeIsValid(unsigned value) {
122 return value <= kLastEnum_SkColorType;
123}
124
reed@google.com3443fd82013-11-13 19:09:13 +0000125///////////////////////////////////////////////////////////////////////////////
126
127/**
scroggo2fd0d142014-07-01 07:08:19 -0700128 * Return true if alphaType is supported by colorType. If there is a canonical
129 * alphaType for this colorType, return it in canonical.
130 */
131bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
132 SkAlphaType* canonical = NULL);
133
134///////////////////////////////////////////////////////////////////////////////
135
136/**
rileyaabaef862014-09-12 17:45:58 -0700137 * Describes the color space a YUV pixel.
138 */
139enum SkYUVColorSpace {
140 /** Standard JPEG color space. */
141 kJPEG_SkYUVColorSpace,
142 /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color
143 range. See http://en.wikipedia.org/wiki/Rec._601 for details. */
144 kRec601_SkYUVColorSpace,
145
146 kLastEnum_SkYUVColorSpace = kRec601_SkYUVColorSpace
147};
148
149///////////////////////////////////////////////////////////////////////////////
150
151/**
reed@google.com3443fd82013-11-13 19:09:13 +0000152 * Describe an image's dimensions and pixel type.
153 */
reed2bdf1f52014-09-03 05:48:56 -0700154struct SkImageInfo {
reede5ea5002014-09-03 11:54:58 -0700155public:
156 SkImageInfo()
157 : fWidth(0)
158 , fHeight(0)
159 , fColorType(kUnknown_SkColorType)
160 , fAlphaType(kIgnore_SkAlphaType)
161 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000162
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000163 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at) {
reede5ea5002014-09-03 11:54:58 -0700164 return SkImageInfo(width, height, ct, at);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000165 }
166
167 /**
168 * Sets colortype to the native ARGB32 type.
169 */
170 static SkImageInfo MakeN32(int width, int height, SkAlphaType at) {
reede5ea5002014-09-03 11:54:58 -0700171 return SkImageInfo(width, height, kN32_SkColorType, at);
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 */
177 static SkImageInfo MakeN32Premul(int width, int height) {
reede5ea5002014-09-03 11:54:58 -0700178 return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000179 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000180
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000181 /**
182 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
183 */
184 static SkImageInfo MakeN32Premul(const SkISize& size) {
185 return MakeN32Premul(size.width(), size.height());
186 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000187
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000188 static SkImageInfo MakeA8(int width, int height) {
reede5ea5002014-09-03 11:54:58 -0700189 return SkImageInfo(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000190 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000191
reed@google.com900ecf22014-02-20 20:55:37 +0000192 static SkImageInfo MakeUnknown(int width, int height) {
reede5ea5002014-09-03 11:54:58 -0700193 return SkImageInfo(width, height, kUnknown_SkColorType, kIgnore_SkAlphaType);
reed@google.com900ecf22014-02-20 20:55:37 +0000194 }
195
reedf252f642014-06-14 04:24:56 -0700196 static SkImageInfo MakeUnknown() {
reede5ea5002014-09-03 11:54:58 -0700197 return SkImageInfo();
reedf252f642014-06-14 04:24:56 -0700198 }
199
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000200 int width() const { return fWidth; }
201 int height() const { return fHeight; }
202 SkColorType colorType() const { return fColorType; }
203 SkAlphaType alphaType() const { return fAlphaType; }
204
205 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
206
reed@google.com3443fd82013-11-13 19:09:13 +0000207 bool isOpaque() const {
208 return SkAlphaTypeIsOpaque(fAlphaType);
209 }
210
halcanaryf622a6c2014-10-24 12:54:53 -0700211 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000212 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
213
reedc77392e2014-06-02 13:07:26 -0700214 /**
215 * Return a new ImageInfo with the same colortype and alphatype as this info,
216 * but with the specified width and height.
217 */
218 SkImageInfo makeWH(int newWidth, int newHeight) const {
219 return SkImageInfo::Make(newWidth, newHeight, fColorType, fAlphaType);
220 }
221
reede5ea5002014-09-03 11:54:58 -0700222 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
223 return SkImageInfo::Make(fWidth, fHeight, fColorType, newAlphaType);
224 }
225
226 SkImageInfo makeColorType(SkColorType newColorType) const {
227 return SkImageInfo::Make(fWidth, fHeight, newColorType, fAlphaType);
228 }
229
reed@google.com3443fd82013-11-13 19:09:13 +0000230 int bytesPerPixel() const {
231 return SkColorTypeBytesPerPixel(fColorType);
232 }
233
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000234 uint64_t minRowBytes64() const {
235 return sk_64_mul(fWidth, this->bytesPerPixel());
236 }
237
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000238 size_t minRowBytes() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000239 return (size_t)this->minRowBytes64();
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000240 }
skia.committer@gmail.com8ed64432014-02-11 03:02:13 +0000241
reed@google.com3443fd82013-11-13 19:09:13 +0000242 bool operator==(const SkImageInfo& other) const {
243 return 0 == memcmp(this, &other, sizeof(other));
244 }
245 bool operator!=(const SkImageInfo& other) const {
246 return 0 != memcmp(this, &other, sizeof(other));
247 }
reed@google.com9230ea22013-12-09 22:01:03 +0000248
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000249 void unflatten(SkReadBuffer&);
250 void flatten(SkWriteBuffer&) const;
reed@google.com9230ea22013-12-09 22:01:03 +0000251
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000252 int64_t getSafeSize64(size_t rowBytes) const {
reed@google.com9230ea22013-12-09 22:01:03 +0000253 if (0 == fHeight) {
254 return 0;
255 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000256 return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel();
reed@google.com9230ea22013-12-09 22:01:03 +0000257 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000258
259 size_t getSafeSize(size_t rowBytes) const {
260 return (size_t)this->getSafeSize64(rowBytes);
261 }
262
263 bool validRowBytes(size_t rowBytes) const {
264 uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
265 return rowBytes >= rb;
266 }
267
268 SkDEBUGCODE(void validate() const;)
reede5ea5002014-09-03 11:54:58 -0700269
270#ifdef SK_SUPPORT_LEGACY_PUBLIC_IMAGEINFO_FIELDS
271public:
272#else
273private:
274#endif
275 int fWidth;
276 int fHeight;
277 SkColorType fColorType;
278 SkAlphaType fAlphaType;
279
280private:
281 SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at)
282 : fWidth(width)
283 , fHeight(height)
284 , fColorType(ct)
285 , fAlphaType(at)
286 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000287};
288
289#endif