blob: 4a8c2860fc061b77a89445e4f43c5e30a3cd6220 [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
msarett23c51102016-05-27 07:39:02 -070011#include "SkColorSpace.h"
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000012#include "SkMath.h"
halcanaryf622a6c2014-10-24 12:54:53 -070013#include "SkRect.h"
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000014#include "SkSize.h"
reed@google.com3443fd82013-11-13 19:09:13 +000015
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000016class SkReadBuffer;
halcanaryf622a6c2014-10-24 12:54:53 -070017class SkWriteBuffer;
reed@google.com9230ea22013-12-09 22:01:03 +000018
reed@google.com3443fd82013-11-13 19:09:13 +000019/**
bsalomonafa95e22015-10-12 10:39:46 -070020 * Describes how to interpret the alpha component of a pixel.
reed@google.com3443fd82013-11-13 19:09:13 +000021 */
22enum SkAlphaType {
reed44977482015-02-27 10:23:00 -080023 kUnknown_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000024
reed@google.com3443fd82013-11-13 19:09:13 +000025 /**
26 * All pixels are stored as opaque. This differs slightly from kIgnore in
27 * that kOpaque has correct "opaque" values stored in the pixels, while
28 * kIgnore may not, but in both cases the caller should treat the pixels
29 * as opaque.
30 */
31 kOpaque_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000032
reed@google.com3443fd82013-11-13 19:09:13 +000033 /**
34 * All pixels have their alpha premultiplied in their color components.
35 * This is the natural format for the rendering target pixels.
36 */
37 kPremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000038
reed@google.com3443fd82013-11-13 19:09:13 +000039 /**
40 * All pixels have their color components stored without any regard to the
41 * alpha. e.g. this is the default configuration for PNG images.
42 *
43 * This alpha-type is ONLY supported for input images. Rendering cannot
44 * generate this on output.
45 */
46 kUnpremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000047
reed@google.com3443fd82013-11-13 19:09:13 +000048 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
49};
50
51static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
reed44977482015-02-27 10:23:00 -080052 return kOpaque_SkAlphaType == at;
reed@google.com3443fd82013-11-13 19:09:13 +000053}
54
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000055static inline bool SkAlphaTypeIsValid(unsigned value) {
56 return value <= kLastEnum_SkAlphaType;
57}
58
reed@google.com3443fd82013-11-13 19:09:13 +000059///////////////////////////////////////////////////////////////////////////////
60
61/**
62 * Describes how to interpret the components of a pixel.
reed6c6ddb82014-06-30 12:44:03 -070063 *
64 * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native"
65 * form for skia's blitters. Use this if you don't have a swizzle preference
66 * for 32bit pixels.
reed@google.com3443fd82013-11-13 19:09:13 +000067 */
68enum SkColorType {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000069 kUnknown_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000070 kAlpha_8_SkColorType,
71 kRGB_565_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000072 kARGB_4444_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000073 kRGBA_8888_SkColorType,
74 kBGRA_8888_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000075 kIndex_8_SkColorType,
reed0c9b1a82015-03-17 17:44:06 -070076 kGray_8_SkColorType,
reed3601f282016-02-05 11:18:39 -080077 kRGBA_F16_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000078
reed3601f282016-02-05 11:18:39 -080079 kLastEnum_SkColorType = kRGBA_F16_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000080
81#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000082 kN32_SkColorType = kBGRA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000083#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000084 kN32_SkColorType = kRGBA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000085#else
reed3b40ee62014-12-21 14:29:04 -080086 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000087#endif
reed@google.com3443fd82013-11-13 19:09:13 +000088};
89
reed6d7cd1f2016-04-15 10:03:03 -070090static int SkColorTypeBytesPerPixel(SkColorType ct) {
reed9d453b02016-04-16 12:24:09 -070091 static const uint8_t gSize[] = {
92 0, // Unknown
93 1, // Alpha_8
94 2, // RGB_565
95 2, // ARGB_4444
96 4, // RGBA_8888
97 4, // BGRA_8888
98 1, // kIndex_8
99 1, // kGray_8
100 8, // kRGBA_F16
101 };
102 static_assert(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
103 "size_mismatch_with_SkColorType_enum");
104
105 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
106 return gSize[ct];
reed@google.com3443fd82013-11-13 19:09:13 +0000107}
108
reed8c3fd4f2016-04-15 06:59:38 -0700109static int SkColorTypeShiftPerPixel(SkColorType ct) {
reed9d453b02016-04-16 12:24:09 -0700110 static const uint8_t gShift[] = {
111 0, // Unknown
112 0, // Alpha_8
113 1, // RGB_565
114 1, // ARGB_4444
115 2, // RGBA_8888
116 2, // BGRA_8888
117 0, // kIndex_8
118 0, // kGray_8
119 3, // kRGBA_F16
120 };
121 static_assert(SK_ARRAY_COUNT(gShift) == (size_t)(kLastEnum_SkColorType + 1),
122 "size_mismatch_with_SkColorType_enum");
123
124 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gShift));
125 return gShift[ct];
reed8c3fd4f2016-04-15 06:59:38 -0700126}
127
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000128static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
129 return width * SkColorTypeBytesPerPixel(ct);
130}
131
132static inline bool SkColorTypeIsValid(unsigned value) {
133 return value <= kLastEnum_SkColorType;
134}
135
reed92fc2ae2015-05-22 08:06:21 -0700136static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
reed8c3fd4f2016-04-15 06:59:38 -0700137 if (kUnknown_SkColorType == ct) {
138 return 0;
reed92fc2ae2015-05-22 08:06:21 -0700139 }
reed8c3fd4f2016-04-15 06:59:38 -0700140 return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
reed92fc2ae2015-05-22 08:06:21 -0700141}
142
reed@google.com3443fd82013-11-13 19:09:13 +0000143///////////////////////////////////////////////////////////////////////////////
144
145/**
scroggo2fd0d142014-07-01 07:08:19 -0700146 * Return true if alphaType is supported by colorType. If there is a canonical
147 * alphaType for this colorType, return it in canonical.
148 */
149bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
150 SkAlphaType* canonical = NULL);
151
152///////////////////////////////////////////////////////////////////////////////
153
154/**
rileyaabaef862014-09-12 17:45:58 -0700155 * Describes the color space a YUV pixel.
156 */
157enum SkYUVColorSpace {
158 /** Standard JPEG color space. */
159 kJPEG_SkYUVColorSpace,
160 /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color
161 range. See http://en.wikipedia.org/wiki/Rec._601 for details. */
162 kRec601_SkYUVColorSpace,
rileya13400392015-07-20 15:00:03 -0700163 /** HDTV standard Rec. 709 color space. Uses "studio swing" [16, 235] color
164 range. See http://en.wikipedia.org/wiki/Rec._709 for details. */
165 kRec709_SkYUVColorSpace,
rileyaabaef862014-09-12 17:45:58 -0700166
rileya13400392015-07-20 15:00:03 -0700167 kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace
rileyaabaef862014-09-12 17:45:58 -0700168};
169
170///////////////////////////////////////////////////////////////////////////////
171
Brian Osman7b8400d2016-11-08 17:08:54 -0500172enum class SkDestinationSurfaceColorMode {
173 kLegacy,
174 kGammaAndColorSpaceAware,
brianosman982eb7f2016-06-06 13:10:58 -0700175};
176
rileyaabaef862014-09-12 17:45:58 -0700177/**
reed@google.com3443fd82013-11-13 19:09:13 +0000178 * Describe an image's dimensions and pixel type.
reed7c748852014-11-10 08:57:21 -0800179 * Used for both src images and render-targets (surfaces).
reed@google.com3443fd82013-11-13 19:09:13 +0000180 */
reed41814922015-01-30 14:54:38 -0800181struct SK_API SkImageInfo {
reede5ea5002014-09-03 11:54:58 -0700182public:
183 SkImageInfo()
msarett23c51102016-05-27 07:39:02 -0700184 : fColorSpace(nullptr)
185 , fWidth(0)
reede5ea5002014-09-03 11:54:58 -0700186 , fHeight(0)
187 , fColorType(kUnknown_SkColorType)
reed44977482015-02-27 10:23:00 -0800188 , fAlphaType(kUnknown_SkAlphaType)
reede5ea5002014-09-03 11:54:58 -0700189 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000190
reed7c748852014-11-10 08:57:21 -0800191 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
reed960b2d62016-06-17 09:26:41 -0700192 sk_sp<SkColorSpace> cs = nullptr) {
193 return SkImageInfo(width, height, ct, at, std::move(cs));
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000194 }
195
196 /**
197 * Sets colortype to the native ARGB32 type.
198 */
reed7c748852014-11-10 08:57:21 -0800199 static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
reed960b2d62016-06-17 09:26:41 -0700200 sk_sp<SkColorSpace> cs = nullptr) {
201 return Make(width, height, kN32_SkColorType, at, cs);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000202 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000203
reedfbce71f2016-06-02 12:40:22 -0700204 /**
205 * Create an ImageInfo marked as SRGB with N32 swizzle.
206 */
207 static SkImageInfo MakeS32(int width, int height, SkAlphaType at);
208
reed960b2d62016-06-17 09:26:41 -0700209 /**
210 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
211 */
reeddabe5d32016-06-21 10:28:14 -0700212 static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) {
213 return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, cs);
reed960b2d62016-06-17 09:26:41 -0700214 }
215
216 static SkImageInfo MakeN32Premul(const SkISize& size) {
217 return MakeN32Premul(size.width(), size.height());
218 }
219
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000220 static SkImageInfo MakeA8(int width, int height) {
reed960b2d62016-06-17 09:26:41 -0700221 return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000222 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000223
reed@google.com900ecf22014-02-20 20:55:37 +0000224 static SkImageInfo MakeUnknown(int width, int height) {
reed960b2d62016-06-17 09:26:41 -0700225 return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
reed@google.com900ecf22014-02-20 20:55:37 +0000226 }
227
reedf252f642014-06-14 04:24:56 -0700228 static SkImageInfo MakeUnknown() {
reed960b2d62016-06-17 09:26:41 -0700229 return MakeUnknown(0, 0);
reedf252f642014-06-14 04:24:56 -0700230 }
reed960b2d62016-06-17 09:26:41 -0700231
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000232 int width() const { return fWidth; }
233 int height() const { return fHeight; }
234 SkColorType colorType() const { return fColorType; }
235 SkAlphaType alphaType() const { return fAlphaType; }
msarett23c51102016-05-27 07:39:02 -0700236 SkColorSpace* colorSpace() const { return fColorSpace.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500237 sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
msarett23c51102016-05-27 07:39:02 -0700238
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000239 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
240
reed@google.com3443fd82013-11-13 19:09:13 +0000241 bool isOpaque() const {
242 return SkAlphaTypeIsOpaque(fAlphaType);
243 }
244
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000245 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
reed7c748852014-11-10 08:57:21 -0800246 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000247
reed960b2d62016-06-17 09:26:41 -0700248 bool gammaCloseToSRGB() const {
249 return fColorSpace && fColorSpace->gammaCloseToSRGB();
250 }
251
reedc77392e2014-06-02 13:07:26 -0700252 /**
253 * Return a new ImageInfo with the same colortype and alphatype as this info,
254 * but with the specified width and height.
255 */
256 SkImageInfo makeWH(int newWidth, int newHeight) const {
reed960b2d62016-06-17 09:26:41 -0700257 return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace);
reedc77392e2014-06-02 13:07:26 -0700258 }
259
reede5ea5002014-09-03 11:54:58 -0700260 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
reed960b2d62016-06-17 09:26:41 -0700261 return Make(fWidth, fHeight, fColorType, newAlphaType, fColorSpace);
reede5ea5002014-09-03 11:54:58 -0700262 }
263
264 SkImageInfo makeColorType(SkColorType newColorType) const {
reed960b2d62016-06-17 09:26:41 -0700265 return Make(fWidth, fHeight, newColorType, fAlphaType, fColorSpace);
reede5ea5002014-09-03 11:54:58 -0700266 }
reed7c748852014-11-10 08:57:21 -0800267
msarett804b4612016-06-09 11:03:45 -0700268 SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const {
reed960b2d62016-06-17 09:26:41 -0700269 return Make(fWidth, fHeight, fColorType, fAlphaType, std::move(cs));
msarett804b4612016-06-09 11:03:45 -0700270 }
271
reed8c3fd4f2016-04-15 06:59:38 -0700272 int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
273
274 int shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
reed@google.com3443fd82013-11-13 19:09:13 +0000275
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000276 uint64_t minRowBytes64() const {
277 return sk_64_mul(fWidth, this->bytesPerPixel());
278 }
279
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000280 size_t minRowBytes() const {
Matt Sarett29121eb2016-10-17 14:32:46 -0400281 uint64_t minRowBytes = this->minRowBytes64();
282 if (!sk_64_isS32(minRowBytes)) {
283 return 0;
284 }
285 return sk_64_asS32(minRowBytes);
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000286 }
skia.committer@gmail.com8ed64432014-02-11 03:02:13 +0000287
reed92fc2ae2015-05-22 08:06:21 -0700288 size_t computeOffset(int x, int y, size_t rowBytes) const {
289 SkASSERT((unsigned)x < (unsigned)fWidth);
290 SkASSERT((unsigned)y < (unsigned)fHeight);
291 return SkColorTypeComputeOffset(fColorType, x, y, rowBytes);
292 }
293
reed@google.com3443fd82013-11-13 19:09:13 +0000294 bool operator==(const SkImageInfo& other) const {
msarett23c51102016-05-27 07:39:02 -0700295 return fWidth == other.fWidth && fHeight == other.fHeight &&
296 fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
msarettabbd6d52016-08-01 09:43:08 -0700297 SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
reed@google.com3443fd82013-11-13 19:09:13 +0000298 }
299 bool operator!=(const SkImageInfo& other) const {
msarettabbd6d52016-08-01 09:43:08 -0700300 return !(*this == other);
reed@google.com3443fd82013-11-13 19:09:13 +0000301 }
reed@google.com9230ea22013-12-09 22:01:03 +0000302
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000303 void unflatten(SkReadBuffer&);
304 void flatten(SkWriteBuffer&) const;
reed@google.com9230ea22013-12-09 22:01:03 +0000305
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000306 int64_t getSafeSize64(size_t rowBytes) const {
reed@google.com9230ea22013-12-09 22:01:03 +0000307 if (0 == fHeight) {
308 return 0;
309 }
Matt Sarett29121eb2016-10-17 14:32:46 -0400310 return sk_64_mul(fHeight - 1, rowBytes) + sk_64_mul(fWidth, this->bytesPerPixel());
reed@google.com9230ea22013-12-09 22:01:03 +0000311 }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000312
313 size_t getSafeSize(size_t rowBytes) const {
reedde499882015-06-18 13:41:40 -0700314 int64_t size = this->getSafeSize64(rowBytes);
315 if (!sk_64_isS32(size)) {
316 return 0;
317 }
318 return sk_64_asS32(size);
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000319 }
320
321 bool validRowBytes(size_t rowBytes) const {
322 uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
323 return rowBytes >= rb;
324 }
325
msarett23c51102016-05-27 07:39:02 -0700326 void reset() {
reed960b2d62016-06-17 09:26:41 -0700327 fColorSpace = nullptr;
msarett23c51102016-05-27 07:39:02 -0700328 fWidth = 0;
329 fHeight = 0;
330 fColorType = kUnknown_SkColorType;
331 fAlphaType = kUnknown_SkAlphaType;
msarett23c51102016-05-27 07:39:02 -0700332 }
333
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000334 SkDEBUGCODE(void validate() const;)
reede5ea5002014-09-03 11:54:58 -0700335
reede5ea5002014-09-03 11:54:58 -0700336private:
msarett23c51102016-05-27 07:39:02 -0700337 sk_sp<SkColorSpace> fColorSpace;
reed7c748852014-11-10 08:57:21 -0800338 int fWidth;
339 int fHeight;
340 SkColorType fColorType;
341 SkAlphaType fAlphaType;
reede5ea5002014-09-03 11:54:58 -0700342
reed960b2d62016-06-17 09:26:41 -0700343 SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
msarett23c51102016-05-27 07:39:02 -0700344 : fColorSpace(std::move(cs))
345 , fWidth(width)
reede5ea5002014-09-03 11:54:58 -0700346 , fHeight(height)
347 , fColorType(ct)
348 , fAlphaType(at)
349 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000350};
351
reed@google.com3443fd82013-11-13 19:09:13 +0000352#endif