2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkImageInfo_DEFINED
9 #define SkImageInfo_DEFINED
19 * Describes how to interpret the alpha compoent of a pixel.
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.
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
38 * All pixels have their alpha premultiplied in their color components.
39 * This is the natural format for the rendering target pixels.
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.
47 * This alpha-type is ONLY supported for input images. Rendering cannot
48 * generate this on output.
50 kUnpremul_SkAlphaType,
52 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
55 static 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);
60 return (unsigned)at <= kOpaque_SkAlphaType;
63 static inline bool SkAlphaTypeIsValid(unsigned value) {
64 return value <= kLastEnum_SkAlphaType;
67 ///////////////////////////////////////////////////////////////////////////////
70 * Describes how to interpret the components of a pixel.
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
80 kARGB_4444_SkColorType,
81 kRGBA_8888_SkColorType,
82 kBGRA_8888_SkColorType,
85 kLastEnum_SkColorType = kIndex_8_SkColorType,
87 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
88 kN32_SkColorType = kBGRA_8888_SkColorType,
89 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
90 kN32_SkColorType = kRGBA_8888_SkColorType,
92 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
95 #ifdef SK_SUPPORT_LEGACY_N32_NAME
96 kPMColor_SkColorType = kN32_SkColorType
100 static int SkColorTypeBytesPerPixel(SkColorType ct) {
101 static const uint8_t gSize[] = {
110 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
111 size_mismatch_with_SkColorType_enum);
113 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
117 static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) {
118 return width * SkColorTypeBytesPerPixel(ct);
121 static inline bool SkColorTypeIsValid(unsigned value) {
122 return value <= kLastEnum_SkColorType;
125 ///////////////////////////////////////////////////////////////////////////////
128 * Return true if alphaType is supported by colorType. If there is a canonical
129 * alphaType for this colorType, return it in canonical.
131 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
132 SkAlphaType* canonical = NULL);
134 ///////////////////////////////////////////////////////////////////////////////
137 * Describes the color space a YUV pixel.
139 enum 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,
146 kLastEnum_SkYUVColorSpace = kRec601_SkYUVColorSpace
149 ///////////////////////////////////////////////////////////////////////////////
152 * Describe an image's dimensions and pixel type.
159 , fColorType(kUnknown_SkColorType)
160 , fAlphaType(kIgnore_SkAlphaType)
163 static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at) {
164 return SkImageInfo(width, height, ct, at);
168 * Sets colortype to the native ARGB32 type.
170 static SkImageInfo MakeN32(int width, int height, SkAlphaType at) {
171 return SkImageInfo(width, height, kN32_SkColorType, at);
175 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
177 static SkImageInfo MakeN32Premul(int width, int height) {
178 return SkImageInfo(width, height, kN32_SkColorType, kPremul_SkAlphaType);
182 * Sets colortype to the native ARGB32 type, and the alphatype to premul.
184 static SkImageInfo MakeN32Premul(const SkISize& size) {
185 return MakeN32Premul(size.width(), size.height());
188 static SkImageInfo MakeA8(int width, int height) {
189 return SkImageInfo(width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType);
192 static SkImageInfo MakeUnknown(int width, int height) {
193 return SkImageInfo(width, height, kUnknown_SkColorType, kIgnore_SkAlphaType);
196 static SkImageInfo MakeUnknown() {
197 return SkImageInfo();
200 int width() const { return fWidth; }
201 int height() const { return fHeight; }
202 SkColorType colorType() const { return fColorType; }
203 SkAlphaType alphaType() const { return fAlphaType; }
205 bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; }
207 bool isOpaque() const {
208 return SkAlphaTypeIsOpaque(fAlphaType);
211 SkIRect bounds() const { return SkIRect::MakeWH(fWidth, fHeight); }
212 SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); }
215 * Return a new ImageInfo with the same colortype and alphatype as this info,
216 * but with the specified width and height.
218 SkImageInfo makeWH(int newWidth, int newHeight) const {
219 return SkImageInfo::Make(newWidth, newHeight, fColorType, fAlphaType);
222 SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const {
223 return SkImageInfo::Make(fWidth, fHeight, fColorType, newAlphaType);
226 SkImageInfo makeColorType(SkColorType newColorType) const {
227 return SkImageInfo::Make(fWidth, fHeight, newColorType, fAlphaType);
230 int bytesPerPixel() const {
231 return SkColorTypeBytesPerPixel(fColorType);
234 uint64_t minRowBytes64() const {
235 return sk_64_mul(fWidth, this->bytesPerPixel());
238 size_t minRowBytes() const {
239 return (size_t)this->minRowBytes64();
242 bool operator==(const SkImageInfo& other) const {
243 return 0 == memcmp(this, &other, sizeof(other));
245 bool operator!=(const SkImageInfo& other) const {
246 return 0 != memcmp(this, &other, sizeof(other));
249 void unflatten(SkReadBuffer&);
250 void flatten(SkWriteBuffer&) const;
252 int64_t getSafeSize64(size_t rowBytes) const {
256 return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel();
259 size_t getSafeSize(size_t rowBytes) const {
260 return (size_t)this->getSafeSize64(rowBytes);
263 bool validRowBytes(size_t rowBytes) const {
264 uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel());
265 return rowBytes >= rb;
268 SkDEBUGCODE(void validate() const;)
270 #ifdef SK_SUPPORT_LEGACY_PUBLIC_IMAGEINFO_FIELDS
277 SkColorType fColorType;
278 SkAlphaType fAlphaType;
281 SkImageInfo(int width, int height, SkColorType ct, SkAlphaType at)