blob: d389d5e00abb7ffa1036277edad3eb9448d176ac [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
11#include "SkTypes.h"
commit-bot@chromium.org32678d92014-01-15 02:38:22 +000012#include "SkSize.h"
reed@google.com3443fd82013-11-13 19:09:13 +000013
reed@google.com9230ea22013-12-09 22:01:03 +000014class SkFlattenableWriteBuffer;
15class SkFlattenableReadBuffer;
16
reed@google.com3443fd82013-11-13 19:09:13 +000017/**
18 * Describes how to interpret the alpha compoent of a pixel.
19 */
20enum SkAlphaType {
21 /**
22 * All pixels should be treated as opaque, regardless of the value stored
23 * in their alpha field. Used for legacy images that wrote 0 or garbarge
24 * in their alpha field, but intended the RGB to be treated as opaque.
25 */
26 kIgnore_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000027
reed@google.com3443fd82013-11-13 19:09:13 +000028 /**
29 * All pixels are stored as opaque. This differs slightly from kIgnore in
30 * that kOpaque has correct "opaque" values stored in the pixels, while
31 * kIgnore may not, but in both cases the caller should treat the pixels
32 * as opaque.
33 */
34 kOpaque_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000035
reed@google.com3443fd82013-11-13 19:09:13 +000036 /**
37 * All pixels have their alpha premultiplied in their color components.
38 * This is the natural format for the rendering target pixels.
39 */
40 kPremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000041
reed@google.com3443fd82013-11-13 19:09:13 +000042 /**
43 * All pixels have their color components stored without any regard to the
44 * alpha. e.g. this is the default configuration for PNG images.
45 *
46 * This alpha-type is ONLY supported for input images. Rendering cannot
47 * generate this on output.
48 */
49 kUnpremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000050
reed@google.com3443fd82013-11-13 19:09:13 +000051 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
52};
53
54static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
55 SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_order);
56 SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order);
57 SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order);
58
59 return (unsigned)at <= kOpaque_SkAlphaType;
60}
61
62///////////////////////////////////////////////////////////////////////////////
63
64/**
65 * Describes how to interpret the components of a pixel.
66 */
67enum SkColorType {
68 kAlpha_8_SkColorType,
69 kRGB_565_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000070 kARGB_4444_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000071 kRGBA_8888_SkColorType,
72 kBGRA_8888_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000073 kIndex_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000074
reed@google.com9230ea22013-12-09 22:01:03 +000075 kLastEnum_SkColorType = kIndex_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000076
77#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
78 kPMColor_SkColorType = kBGRA_8888_SkColorType
79#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
80 kPMColor_SkColorType = kRGBA_8888_SkColorType
81#else
82#error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
83#endif
84};
85
86static int SkColorTypeBytesPerPixel(SkColorType ct) {
87 static const uint8_t gSize[] = {
88 1, // Alpha_8
89 2, // RGB_565
reed@google.com9230ea22013-12-09 22:01:03 +000090 2, // ARGB_4444
reed@google.com3443fd82013-11-13 19:09:13 +000091 4, // RGBA_8888
92 4, // BGRA_8888
93 1, // kIndex_8
94 };
95 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
96 size_mismatch_with_SkColorType_enum);
97
98 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
99 return gSize[ct];
100}
101
102///////////////////////////////////////////////////////////////////////////////
103
104/**
105 * Describe an image's dimensions and pixel type.
106 */
107struct SkImageInfo {
108 int fWidth;
109 int fHeight;
110 SkColorType fColorType;
111 SkAlphaType fAlphaType;
112
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000113 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at) {
114 SkASSERT(width >= 0);
115 SkASSERT(height >= 0);
116 SkImageInfo info = {
117 width, height, ct, at
118 };
119 return info;
120 }
121
122 /**
123 * Sets colortype to the native ARGB32 type.
124 */
125 static SkImageInfo MakeN32(int width, int height, SkAlphaType at) {
126 SkASSERT(width >= 0);
127 SkASSERT(height >= 0);
128 SkImageInfo info = {
129 width, height, kPMColor_SkColorType, at
130 };
131 return info;
132 }
133
134 /**
135 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
136 */
137 static SkImageInfo MakeN32Premul(int width, int height) {
138 SkASSERT(width >= 0);
139 SkASSERT(height >= 0);
140 SkImageInfo info = {
141 width, height, kPMColor_SkColorType, kPremul_SkAlphaType
142 };
143 return info;
144 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000145
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000146 /**
147 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
148 */
149 static SkImageInfo MakeN32Premul(const SkISize& size) {
150 return MakeN32Premul(size.width(), size.height());
151 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000152
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000153 static SkImageInfo MakeA8(int width, int height) {
154 SkASSERT(width >= 0);
155 SkASSERT(height >= 0);
156 SkImageInfo info = {
157 width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType
158 };
159 return info;
160 }
skia.committer@gmail.comd77b3ec2014-01-15 07:01:43 +0000161
reed@google.com3443fd82013-11-13 19:09:13 +0000162 bool isOpaque() const {
163 return SkAlphaTypeIsOpaque(fAlphaType);
164 }
165
166 int bytesPerPixel() const {
167 return SkColorTypeBytesPerPixel(fColorType);
168 }
169
reed@google.com9230ea22013-12-09 22:01:03 +0000170 size_t minRowBytes() const {
171 return fWidth * this->bytesPerPixel();
172 }
173
reed@google.com3443fd82013-11-13 19:09:13 +0000174 bool operator==(const SkImageInfo& other) const {
175 return 0 == memcmp(this, &other, sizeof(other));
176 }
177 bool operator!=(const SkImageInfo& other) const {
178 return 0 != memcmp(this, &other, sizeof(other));
179 }
reed@google.com9230ea22013-12-09 22:01:03 +0000180
181 void unflatten(SkFlattenableReadBuffer&);
182 void flatten(SkFlattenableWriteBuffer&) const;
183
184 size_t getSafeSize(size_t rowBytes) const {
185 if (0 == fHeight) {
186 return 0;
187 }
188 return (fHeight - 1) * rowBytes + fWidth * this->bytesPerPixel();
189 }
reed@google.com3443fd82013-11-13 19:09:13 +0000190};
191
192#endif