blob: 1cac7ebc4deb44b1394b391c6151e4240a437478 [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/**
bsalomonafa95e22015-10-12 10:39:46 -070019 * Describes how to interpret the alpha component of a pixel.
reed@google.com3443fd82013-11-13 19:09:13 +000020 */
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,
reed3601f282016-02-05 11:18:39 -080076 kRGBA_F16_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000077
reed3601f282016-02-05 11:18:39 -080078 kLastEnum_SkColorType = kRGBA_F16_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000079
80#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000081 kN32_SkColorType = kBGRA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000082#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000083 kN32_SkColorType = kRGBA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000084#else
reed3b40ee62014-12-21 14:29:04 -080085 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000086#endif
reed@google.com3443fd82013-11-13 19:09:13 +000087};
88
reed6d7cd1f2016-04-15 10:03:03 -070089extern const uint8_t gPrivate_SkColorTypeBytesPerPixel[];
reed@google.com3443fd82013-11-13 19:09:13 +000090
reed6d7cd1f2016-04-15 10:03:03 -070091static int SkColorTypeBytesPerPixel(SkColorType ct) {
92 SkASSERT((unsigned)ct <= (unsigned)kLastEnum_SkColorType);
93 return gPrivate_SkColorTypeBytesPerPixel[ct];
reed@google.com3443fd82013-11-13 19:09:13 +000094}
95
reed6d7cd1f2016-04-15 10:03:03 -070096extern const uint8_t gPrivate_SkColorTypeShiftPerPixel[];
97
reed8c3fd4f2016-04-15 06:59:38 -070098static int SkColorTypeShiftPerPixel(SkColorType ct) {
reed6d7cd1f2016-04-15 10:03:03 -070099 SkASSERT((unsigned)ct <= (unsigned)kLastEnum_SkColorType);
100 return gPrivate_SkColorTypeShiftPerPixel[ct];
reed8c3fd4f2016-04-15 06:59:38 -0700101}
102
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000103static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
104 return width * SkColorTypeBytesPerPixel(ct);
105}
106
107static inline bool SkColorTypeIsValid(unsigned value) {
108 return value <= kLastEnum_SkColorType;
109}
110
reed92fc2ae2015-05-22 08:06:21 -0700111static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
reed8c3fd4f2016-04-15 06:59:38 -0700112 if (kUnknown_SkColorType == ct) {
113 return 0;
reed92fc2ae2015-05-22 08:06:21 -0700114 }
reed8c3fd4f2016-04-15 06:59:38 -0700115 return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
reed92fc2ae2015-05-22 08:06:21 -0700116}
117
reed@google.com3443fd82013-11-13 19:09:13 +0000118///////////////////////////////////////////////////////////////////////////////
119
120/**
scroggo2fd0d142014-07-01 07:08:19 -0700121 * Return true if alphaType is supported by colorType. If there is a canonical
122 * alphaType for this colorType, return it in canonical.
123 */
124bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
125 SkAlphaType* canonical = NULL);
126
127///////////////////////////////////////////////////////////////////////////////
128
129/**
rileyaabaef862014-09-12 17:45:58 -0700130 * Describes the color space a YUV pixel.
131 */
132enum SkYUVColorSpace {
133 /** Standard JPEG color space. */
134 kJPEG_SkYUVColorSpace,
135 /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color
136 range. See http://en.wikipedia.org/wiki/Rec._601 for details. */
137 kRec601_SkYUVColorSpace,
rileya13400392015-07-20 15:00:03 -0700138 /** HDTV standard Rec. 709 color space. Uses "studio swing" [16, 235] color
139 range. See http://en.wikipedia.org/wiki/Rec._709 for details. */
140 kRec709_SkYUVColorSpace,
rileyaabaef862014-09-12 17:45:58 -0700141
rileya13400392015-07-20 15:00:03 -0700142 kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace
rileyaabaef862014-09-12 17:45:58 -0700143};
144
145///////////////////////////////////////////////////////////////////////////////
146
reed7c748852014-11-10 08:57:21 -0800147enum SkColorProfileType {
148 kLinear_SkColorProfileType,
149 kSRGB_SkColorProfileType,
150
151 kLastEnum_SkColorProfileType = kSRGB_SkColorProfileType
152};
153
rileyaabaef862014-09-12 17:45:58 -0700154/**
reed@google.com3443fd82013-11-13 19:09:13 +0000155 * Describe an image's dimensions and pixel type.
reed7c748852014-11-10 08:57:21 -0800156 * Used for both src images and render-targets (surfaces).
reed@google.com3443fd82013-11-13 19:09:13 +0000157 */
reed41814922015-01-30 14:54:38 -0800158struct SK_API SkImageInfo {
reede5ea5002014-09-03 11:54:58 -0700159public:
160 SkImageInfo()
161 : fWidth(0)
162 , fHeight(0)
163 , fColorType(kUnknown_SkColorType)
reed44977482015-02-27 10:23:00 -0800164 , fAlphaType(kUnknown_SkAlphaType)
reed7c748852014-11-10 08:57:21 -0800165 , fProfileType(kLinear_SkColorProfileType)
reede5ea5002014-09-03 11:54:58 -0700166 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000167
reed7c748852014-11-10 08:57:21 -0800168 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
169 SkColorProfileType pt = kLinear_SkColorProfileType) {
170 return SkImageInfo(width, height, ct, at, pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000171 }
172
173 /**
174 * Sets colortype to the native ARGB32 type.
175 */
reed7c748852014-11-10 08:57:21 -0800176 static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
177 SkColorProfileType pt = kLinear_SkColorProfileType) {
178 return SkImageInfo(width, height, kN32_SkColorType, at, pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000179 }
180
181 /**
182 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
183 */
reed7c748852014-11-10 08:57:21 -0800184 static SkImageInfo MakeN32Premul(int width, int height,
185 SkColorProfileType pt = kLinear_SkColorProfileType) {
186 return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType, pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000187 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000188
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000189 /**
190 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
191 */
reed7c748852014-11-10 08:57:21 -0800192 static SkImageInfo MakeN32Premul(const SkISize& size,
193 SkColorProfileType pt = kLinear_SkColorProfileType) {
194 return MakeN32Premul(size.width(), size.height(), pt);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000195 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000196
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000197 static SkImageInfo MakeA8(int width, int height) {
reed7c748852014-11-10 08:57:21 -0800198 return SkImageInfo(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType,
199 kLinear_SkColorProfileType);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000200 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000201
reed@google.com900ecf22014-02-20 20:55:37 +0000202 static SkImageInfo MakeUnknown(int width, int height) {
reed44977482015-02-27 10:23:00 -0800203 return SkImageInfo(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType,
reed7c748852014-11-10 08:57:21 -0800204 kLinear_SkColorProfileType);
reed@google.com900ecf22014-02-20 20:55:37 +0000205 }
206
reedf252f642014-06-14 04:24:56 -0700207 static SkImageInfo MakeUnknown() {
reede5ea5002014-09-03 11:54:58 -0700208 return SkImageInfo();
reedf252f642014-06-14 04:24:56 -0700209 }
210
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000211 int width() const { return fWidth; }
212 int height() const { return fHeight; }
213 SkColorType colorType() const { return fColorType; }
214 SkAlphaType alphaType() const { return fAlphaType; }
reed7c748852014-11-10 08:57:21 -0800215 SkColorProfileType profileType() const { return fProfileType; }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000216
217 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
218
reed@google.com3443fd82013-11-13 19:09:13 +0000219 bool isOpaque() const {
220 return SkAlphaTypeIsOpaque(fAlphaType);
221 }
222
reed7c748852014-11-10 08:57:21 -0800223 bool isLinear() const { return kLinear_SkColorProfileType == fProfileType; }
224 bool isSRGB() const { return kSRGB_SkColorProfileType == fProfileType; }
225
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000226 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
reed7c748852014-11-10 08:57:21 -0800227 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000228
reedc77392e2014-06-02 13:07:26 -0700229 /**
230 * Return a new ImageInfo with the same colortype and alphatype as this info,
231 * but with the specified width and height.
232 */
233 SkImageInfo makeWH(int newWidth, int newHeight) const {
reed7c748852014-11-10 08:57:21 -0800234 return SkImageInfo::Make(newWidth, newHeight, fColorType, fAlphaType, fProfileType);
reedc77392e2014-06-02 13:07:26 -0700235 }
236
reede5ea5002014-09-03 11:54:58 -0700237 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
reed7c748852014-11-10 08:57:21 -0800238 return SkImageInfo::Make(fWidth, fHeight, fColorType, newAlphaType, fProfileType);
reede5ea5002014-09-03 11:54:58 -0700239 }
240
241 SkImageInfo makeColorType(SkColorType newColorType) const {
reed7c748852014-11-10 08:57:21 -0800242 return SkImageInfo::Make(fWidth, fHeight, newColorType, fAlphaType, fProfileType);
reede5ea5002014-09-03 11:54:58 -0700243 }
reed7c748852014-11-10 08:57:21 -0800244
reed8c3fd4f2016-04-15 06:59:38 -0700245 int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
246
247 int shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
reed@google.com3443fd82013-11-13 19:09:13 +0000248
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000249 uint64_t minRowBytes64() const {
250 return sk_64_mul(fWidth, this->bytesPerPixel());
251 }
252
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000253 size_t minRowBytes() const {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000254 return (size_t)this->minRowBytes64();
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000255 }
skia.committer@gmail.com8ed64432014-02-11 03:02:13 +0000256
reed92fc2ae2015-05-22 08:06:21 -0700257 size_t computeOffset(int x, int y, size_t rowBytes) const {
258 SkASSERT((unsigned)x < (unsigned)fWidth);
259 SkASSERT((unsigned)y < (unsigned)fHeight);
260 return SkColorTypeComputeOffset(fColorType, x, y, rowBytes);
261 }
262
reed@google.com3443fd82013-11-13 19:09:13 +0000263 bool operator==(const SkImageInfo& other) const {
264 return 0 == memcmp(this, &other, sizeof(other));
265 }
266 bool operator!=(const SkImageInfo& other) const {
267 return 0 != memcmp(this, &other, sizeof(other));
268 }
reed@google.com9230ea22013-12-09 22:01:03 +0000269
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000270 void unflatten(SkReadBuffer&);
271 void flatten(SkWriteBuffer&) const;
reed@google.com9230ea22013-12-09 22:01:03 +0000272
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000273 int64_t getSafeSize64(size_t rowBytes) const {
reed@google.com9230ea22013-12-09 22:01:03 +0000274 if (0 == fHeight) {
275 return 0;
276 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000277 return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel();
reed@google.com9230ea22013-12-09 22:01:03 +0000278 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000279
280 size_t getSafeSize(size_t rowBytes) const {
reedde499882015-06-18 13:41:40 -0700281 int64_t size = this->getSafeSize64(rowBytes);
282 if (!sk_64_isS32(size)) {
283 return 0;
284 }
285 return sk_64_asS32(size);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000286 }
287
288 bool validRowBytes(size_t rowBytes) const {
289 uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
290 return rowBytes >= rb;
291 }
292
293 SkDEBUGCODE(void validate() const;)
reede5ea5002014-09-03 11:54:58 -0700294
reede5ea5002014-09-03 11:54:58 -0700295private:
reed7c748852014-11-10 08:57:21 -0800296 int fWidth;
297 int fHeight;
298 SkColorType fColorType;
299 SkAlphaType fAlphaType;
reedc2e68272015-08-01 07:03:20 -0700300 SkColorProfileType fProfileType;
reede5ea5002014-09-03 11:54:58 -0700301
reed7c748852014-11-10 08:57:21 -0800302 SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, SkColorProfileType pt)
reede5ea5002014-09-03 11:54:58 -0700303 : fWidth(width)
304 , fHeight(height)
305 , fColorType(ct)
306 , fAlphaType(at)
reed7c748852014-11-10 08:57:21 -0800307 , fProfileType(pt)
reede5ea5002014-09-03 11:54:58 -0700308 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000309};
310
brianosman898235c2016-04-06 07:38:23 -0700311///////////////////////////////////////////////////////////////////////////////
312
313static inline bool SkColorAndProfileAreGammaCorrect(SkColorType ct, SkColorProfileType pt) {
314 return kSRGB_SkColorProfileType == pt || kRGBA_F16_SkColorType == ct;
315}
316
317static inline bool SkImageInfoIsGammaCorrect(const SkImageInfo& info) {
318 return SkColorAndProfileAreGammaCorrect(info.colorType(), info.profileType());
319}
320
reed@google.com3443fd82013-11-13 19:09:13 +0000321#endif