blob: 6876c8bb044ffa577b51539290f597e5225b730b [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 *
Joe Gregorioc4859072017-01-20 14:21:27 +000043 * This alpha-type is ONLY supported for input images. Rendering cannot
44 * generate this on output.
reed@google.com3443fd82013-11-13 19:09:13 +000045 */
46 kUnpremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000047
Cary Clark745c3982018-01-24 15:37:13 -050048 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType,
reed@google.com3443fd82013-11-13 19:09:13 +000049};
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
Brian Salomone41e1762018-01-25 14:07:47 -050061/** Temporary macro that allows us to add new color types without breaking Chrome compile. */
62#define SK_EXTENDED_COLOR_TYPES
63
reed@google.com3443fd82013-11-13 19:09:13 +000064/**
65 * Describes how to interpret the components of a pixel.
reed6c6ddb82014-06-30 12:44:03 -070066 *
67 * kN32_SkColorType is an alias for whichever 32bit ARGB format is the "native"
68 * form for skia's blitters. Use this if you don't have a swizzle preference
69 * for 32bit pixels.
reed@google.com3443fd82013-11-13 19:09:13 +000070 */
71enum SkColorType {
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +000072 kUnknown_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000073 kAlpha_8_SkColorType,
74 kRGB_565_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000075 kARGB_4444_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000076 kRGBA_8888_SkColorType,
Brian Salomone41e1762018-01-25 14:07:47 -050077 kRGB_888x_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000078 kBGRA_8888_SkColorType,
Brian Salomone41e1762018-01-25 14:07:47 -050079 kRGBA_1010102_SkColorType,
80 kRGB_101010x_SkColorType,
reed0c9b1a82015-03-17 17:44:06 -070081 kGray_8_SkColorType,
reed3601f282016-02-05 11:18:39 -080082 kRGBA_F16_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000083
reed3601f282016-02-05 11:18:39 -080084 kLastEnum_SkColorType = kRGBA_F16_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000085
86#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000087 kN32_SkColorType = kBGRA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000088#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000089 kN32_SkColorType = kRGBA_8888_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000090#else
Cary Clark745c3982018-01-24 15:37:13 -050091 #error "SK_*32_SHIFT values must correspond to BGRA or RGBA byte order"
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +000092#endif
reed@google.com3443fd82013-11-13 19:09:13 +000093};
94
reed6d7cd1f2016-04-15 10:03:03 -070095static int SkColorTypeBytesPerPixel(SkColorType ct) {
Brian Salomone41e1762018-01-25 14:07:47 -050096 switch (ct) {
97 case kUnknown_SkColorType: return 0;
98 case kAlpha_8_SkColorType: return 1;
99 case kRGB_565_SkColorType: return 2;
100 case kARGB_4444_SkColorType: return 2;
101 case kRGBA_8888_SkColorType: return 4;
102 case kBGRA_8888_SkColorType: return 4;
103 case kRGB_888x_SkColorType: return 4;
104 case kRGBA_1010102_SkColorType: return 4;
105 case kRGB_101010x_SkColorType: return 4;
106 case kGray_8_SkColorType: return 1;
107 case kRGBA_F16_SkColorType: return 8;
108 }
109 return 0;
reed@google.com3443fd82013-11-13 19:09:13 +0000110}
111
reed8c3fd4f2016-04-15 06:59:38 -0700112static int SkColorTypeShiftPerPixel(SkColorType ct) {
Brian Salomone41e1762018-01-25 14:07:47 -0500113 switch (ct) {
114 case kUnknown_SkColorType: return 0;
115 case kAlpha_8_SkColorType: return 0;
116 case kRGB_565_SkColorType: return 1;
117 case kARGB_4444_SkColorType: return 1;
118 case kRGBA_8888_SkColorType: return 2;
119 case kRGB_888x_SkColorType: return 2;
120 case kBGRA_8888_SkColorType: return 2;
121 case kRGBA_1010102_SkColorType: return 2;
122 case kRGB_101010x_SkColorType: return 2;
123 case kGray_8_SkColorType: return 0;
124 case kRGBA_F16_SkColorType: return 3;
125 }
126 return 0;
reed8c3fd4f2016-04-15 06:59:38 -0700127}
128
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000129static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
130 return width * SkColorTypeBytesPerPixel(ct);
131}
132
133static inline bool SkColorTypeIsValid(unsigned value) {
134 return value <= kLastEnum_SkColorType;
135}
136
reed92fc2ae2015-05-22 08:06:21 -0700137static inline size_t SkColorTypeComputeOffset(SkColorType ct, int x, int y, size_t rowBytes) {
reed8c3fd4f2016-04-15 06:59:38 -0700138 if (kUnknown_SkColorType == ct) {
139 return 0;
reed92fc2ae2015-05-22 08:06:21 -0700140 }
reed8c3fd4f2016-04-15 06:59:38 -0700141 return y * rowBytes + (x << SkColorTypeShiftPerPixel(ct));
reed92fc2ae2015-05-22 08:06:21 -0700142}
143
reed@google.com3443fd82013-11-13 19:09:13 +0000144///////////////////////////////////////////////////////////////////////////////
145
146/**
scroggo2fd0d142014-07-01 07:08:19 -0700147 * Return true if alphaType is supported by colorType. If there is a canonical
148 * alphaType for this colorType, return it in canonical.
149 */
150bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
Ben Wagnera93a14a2017-08-28 10:34:05 -0400151 SkAlphaType* canonical = nullptr);
scroggo2fd0d142014-07-01 07:08:19 -0700152
153///////////////////////////////////////////////////////////////////////////////
154
155/**
rileyaabaef862014-09-12 17:45:58 -0700156 * Describes the color space a YUV pixel.
157 */
158enum SkYUVColorSpace {
159 /** Standard JPEG color space. */
160 kJPEG_SkYUVColorSpace,
161 /** SDTV standard Rec. 601 color space. Uses "studio swing" [16, 235] color
162 range. See http://en.wikipedia.org/wiki/Rec._601 for details. */
163 kRec601_SkYUVColorSpace,
rileya13400392015-07-20 15:00:03 -0700164 /** HDTV standard Rec. 709 color space. Uses "studio swing" [16, 235] color
165 range. See http://en.wikipedia.org/wiki/Rec._709 for details. */
166 kRec709_SkYUVColorSpace,
rileyaabaef862014-09-12 17:45:58 -0700167
Cary Clark745c3982018-01-24 15:37:13 -0500168 kLastEnum_SkYUVColorSpace = kRec709_SkYUVColorSpace,
rileyaabaef862014-09-12 17:45:58 -0700169};
170
171///////////////////////////////////////////////////////////////////////////////
172
Brian Osman7b8400d2016-11-08 17:08:54 -0500173enum class SkDestinationSurfaceColorMode {
174 kLegacy,
175 kGammaAndColorSpaceAware,
brianosman982eb7f2016-06-06 13:10:58 -0700176};
177
rileyaabaef862014-09-12 17:45:58 -0700178/**
reed@google.com3443fd82013-11-13 19:09:13 +0000179 * Describe an image's dimensions and pixel type.
reed7c748852014-11-10 08:57:21 -0800180 * Used for both src images and render-targets (surfaces).
reed@google.com3443fd82013-11-13 19:09:13 +0000181 */
reed41814922015-01-30 14:54:38 -0800182struct SK_API SkImageInfo {
reede5ea5002014-09-03 11:54:58 -0700183public:
184 SkImageInfo()
msarett23c51102016-05-27 07:39:02 -0700185 : fColorSpace(nullptr)
186 , fWidth(0)
reede5ea5002014-09-03 11:54:58 -0700187 , fHeight(0)
188 , fColorType(kUnknown_SkColorType)
reed44977482015-02-27 10:23:00 -0800189 , fAlphaType(kUnknown_SkAlphaType)
reede5ea5002014-09-03 11:54:58 -0700190 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000191
reed7c748852014-11-10 08:57:21 -0800192 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at,
reed960b2d62016-06-17 09:26:41 -0700193 sk_sp<SkColorSpace> cs = nullptr) {
194 return SkImageInfo(width, height, ct, at, std::move(cs));
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000195 }
196
197 /**
198 * Sets colortype to the native ARGB32 type.
199 */
reed7c748852014-11-10 08:57:21 -0800200 static SkImageInfo MakeN32(int width, int height, SkAlphaType at,
reed960b2d62016-06-17 09:26:41 -0700201 sk_sp<SkColorSpace> cs = nullptr) {
202 return Make(width, height, kN32_SkColorType, at, cs);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000203 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000204
reedfbce71f2016-06-02 12:40:22 -0700205 /**
206 * Create an ImageInfo marked as SRGB with N32 swizzle.
207 */
208 static SkImageInfo MakeS32(int width, int height, SkAlphaType at);
209
reed960b2d62016-06-17 09:26:41 -0700210 /**
211 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
212 */
reeddabe5d32016-06-21 10:28:14 -0700213 static SkImageInfo MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs = nullptr) {
214 return Make(width, height, kN32_SkColorType, kPremul_SkAlphaType, cs);
reed960b2d62016-06-17 09:26:41 -0700215 }
216
217 static SkImageInfo MakeN32Premul(const SkISize& size) {
218 return MakeN32Premul(size.width(), size.height());
219 }
220
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000221 static SkImageInfo MakeA8(int width, int height) {
reed960b2d62016-06-17 09:26:41 -0700222 return Make(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000223 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000224
reed@google.com900ecf22014-02-20 20:55:37 +0000225 static SkImageInfo MakeUnknown(int width, int height) {
reed960b2d62016-06-17 09:26:41 -0700226 return Make(width, height, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
reed@google.com900ecf22014-02-20 20:55:37 +0000227 }
228
reedf252f642014-06-14 04:24:56 -0700229 static SkImageInfo MakeUnknown() {
reed960b2d62016-06-17 09:26:41 -0700230 return MakeUnknown(0, 0);
reedf252f642014-06-14 04:24:56 -0700231 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400232
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000233 int width() const { return fWidth; }
234 int height() const { return fHeight; }
235 SkColorType colorType() const { return fColorType; }
236 SkAlphaType alphaType() const { return fAlphaType; }
msarett23c51102016-05-27 07:39:02 -0700237 SkColorSpace* colorSpace() const { return fColorSpace.get(); }
Mike Reed693fdbd2017-01-12 10:13:40 -0500238 sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
msarett23c51102016-05-27 07:39:02 -0700239
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000240 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
241
reed@google.com3443fd82013-11-13 19:09:13 +0000242 bool isOpaque() const {
243 return SkAlphaTypeIsOpaque(fAlphaType);
244 }
245
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000246 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
reed7c748852014-11-10 08:57:21 -0800247 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000248
reed960b2d62016-06-17 09:26:41 -0700249 bool gammaCloseToSRGB() const {
250 return fColorSpace && fColorSpace->gammaCloseToSRGB();
251 }
252
reedc77392e2014-06-02 13:07:26 -0700253 /**
254 * Return a new ImageInfo with the same colortype and alphatype as this info,
255 * but with the specified width and height.
256 */
257 SkImageInfo makeWH(int newWidth, int newHeight) const {
reed960b2d62016-06-17 09:26:41 -0700258 return Make(newWidth, newHeight, fColorType, fAlphaType, fColorSpace);
reedc77392e2014-06-02 13:07:26 -0700259 }
260
reede5ea5002014-09-03 11:54:58 -0700261 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
reed960b2d62016-06-17 09:26:41 -0700262 return Make(fWidth, fHeight, fColorType, newAlphaType, fColorSpace);
reede5ea5002014-09-03 11:54:58 -0700263 }
Ben Wagner63fd7602017-10-09 15:45:33 -0400264
reede5ea5002014-09-03 11:54:58 -0700265 SkImageInfo makeColorType(SkColorType newColorType) const {
reed960b2d62016-06-17 09:26:41 -0700266 return Make(fWidth, fHeight, newColorType, fAlphaType, fColorSpace);
reede5ea5002014-09-03 11:54:58 -0700267 }
reed7c748852014-11-10 08:57:21 -0800268
msarett804b4612016-06-09 11:03:45 -0700269 SkImageInfo makeColorSpace(sk_sp<SkColorSpace> cs) const {
reed960b2d62016-06-17 09:26:41 -0700270 return Make(fWidth, fHeight, fColorType, fAlphaType, std::move(cs));
msarett804b4612016-06-09 11:03:45 -0700271 }
272
reed8c3fd4f2016-04-15 06:59:38 -0700273 int bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
274
275 int shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
reed@google.com3443fd82013-11-13 19:09:13 +0000276
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000277 uint64_t minRowBytes64() const {
278 return sk_64_mul(fWidth, this->bytesPerPixel());
279 }
280
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000281 size_t minRowBytes() const {
Matt Sarett29121eb2016-10-17 14:32:46 -0400282 uint64_t minRowBytes = this->minRowBytes64();
283 if (!sk_64_isS32(minRowBytes)) {
284 return 0;
285 }
286 return sk_64_asS32(minRowBytes);
mike@reedtribe.org169a0ed2014-02-11 02:20:17 +0000287 }
skia.committer@gmail.com8ed64432014-02-11 03:02:13 +0000288
reed92fc2ae2015-05-22 08:06:21 -0700289 size_t computeOffset(int x, int y, size_t rowBytes) const {
290 SkASSERT((unsigned)x < (unsigned)fWidth);
291 SkASSERT((unsigned)y < (unsigned)fHeight);
292 return SkColorTypeComputeOffset(fColorType, x, y, rowBytes);
293 }
294
reed@google.com3443fd82013-11-13 19:09:13 +0000295 bool operator==(const SkImageInfo& other) const {
msarett23c51102016-05-27 07:39:02 -0700296 return fWidth == other.fWidth && fHeight == other.fHeight &&
297 fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
msarettabbd6d52016-08-01 09:43:08 -0700298 SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
reed@google.com3443fd82013-11-13 19:09:13 +0000299 }
300 bool operator!=(const SkImageInfo& other) const {
msarettabbd6d52016-08-01 09:43:08 -0700301 return !(*this == other);
reed@google.com3443fd82013-11-13 19:09:13 +0000302 }
reed@google.com9230ea22013-12-09 22:01:03 +0000303
Cary Clark745c3982018-01-24 15:37:13 -0500304 void unflatten(SkReadBuffer& buffer);
305 void flatten(SkWriteBuffer& buffer) const;
reed@google.com9230ea22013-12-09 22:01:03 +0000306
Mike Reede74dafc2017-09-29 13:41:58 -0400307 /**
308 * Returns the size (in bytes) of the image buffer that this info needs, given the specified
309 * rowBytes. The rowBytes must be >= this->minRowBytes().
Mike Reed3758c752017-10-11 11:30:31 -0400310 *
311 * if (height == 0) {
312 * return 0;
313 * } else {
314 * return (height - 1) * rowBytes + width * bytes_per_pixel;
315 * }
316 *
Mike Reed72818012017-10-09 11:37:44 -0400317 * If the calculation overflows this returns SK_MaxSizeT
318 */
Mike Reede74dafc2017-09-29 13:41:58 -0400319 size_t computeByteSize(size_t rowBytes) const;
320
321 /**
322 * Returns the minimum size (in bytes) of the image buffer that this info needs.
323 * If the calculation overflows, or if the height is 0, this returns 0.
324 */
325 size_t computeMinByteSize() const {
326 return this->computeByteSize(this->minRowBytes());
327 }
328
Mike Reedc5eb97d2017-10-09 10:42:51 -0400329 // Returns true if the result of computeByteSize (or computeMinByteSize) overflowed
330 static bool ByteSizeOverflowed(size_t byteSize) {
Mike Reed72818012017-10-09 11:37:44 -0400331 return SK_MaxSizeT == byteSize;
Mike Reedc5eb97d2017-10-09 10:42:51 -0400332 }
333
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000334 bool validRowBytes(size_t rowBytes) const {
Mike Reedf0ffb892017-10-03 14:47:21 -0400335 uint64_t minRB = sk_64_mul(fWidth, this->bytesPerPixel());
336 return rowBytes >= minRB;
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000337 }
338
msarett23c51102016-05-27 07:39:02 -0700339 void reset() {
reed960b2d62016-06-17 09:26:41 -0700340 fColorSpace = nullptr;
msarett23c51102016-05-27 07:39:02 -0700341 fWidth = 0;
342 fHeight = 0;
343 fColorType = kUnknown_SkColorType;
344 fAlphaType = kUnknown_SkAlphaType;
msarett23c51102016-05-27 07:39:02 -0700345 }
346
commit-bot@chromium.org61e96cd2014-02-11 18:21:45 +0000347 SkDEBUGCODE(void validate() const;)
reede5ea5002014-09-03 11:54:58 -0700348
reede5ea5002014-09-03 11:54:58 -0700349private:
msarett23c51102016-05-27 07:39:02 -0700350 sk_sp<SkColorSpace> fColorSpace;
reed7c748852014-11-10 08:57:21 -0800351 int fWidth;
352 int fHeight;
353 SkColorType fColorType;
354 SkAlphaType fAlphaType;
reede5ea5002014-09-03 11:54:58 -0700355
reed960b2d62016-06-17 09:26:41 -0700356 SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
msarett23c51102016-05-27 07:39:02 -0700357 : fColorSpace(std::move(cs))
358 , fWidth(width)
reede5ea5002014-09-03 11:54:58 -0700359 , fHeight(height)
360 , fColorType(ct)
361 , fAlphaType(at)
362 {}
reed@google.com3443fd82013-11-13 19:09:13 +0000363};
364
reed@google.com3443fd82013-11-13 19:09:13 +0000365#endif