blob: 1165479f7484267e47dc5dfa2bba90e5a79dce56 [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"
12
reed@google.com9230ea22013-12-09 22:01:03 +000013class SkFlattenableWriteBuffer;
14class SkFlattenableReadBuffer;
15
reed@google.com3443fd82013-11-13 19:09:13 +000016/**
17 * Describes how to interpret the alpha compoent of a pixel.
18 */
19enum SkAlphaType {
20 /**
21 * All pixels should be treated as opaque, regardless of the value stored
22 * in their alpha field. Used for legacy images that wrote 0 or garbarge
23 * in their alpha field, but intended the RGB to be treated as opaque.
24 */
25 kIgnore_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000026
reed@google.com3443fd82013-11-13 19:09:13 +000027 /**
28 * All pixels are stored as opaque. This differs slightly from kIgnore in
29 * that kOpaque has correct "opaque" values stored in the pixels, while
30 * kIgnore may not, but in both cases the caller should treat the pixels
31 * as opaque.
32 */
33 kOpaque_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000034
reed@google.com3443fd82013-11-13 19:09:13 +000035 /**
36 * All pixels have their alpha premultiplied in their color components.
37 * This is the natural format for the rendering target pixels.
38 */
39 kPremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000040
reed@google.com3443fd82013-11-13 19:09:13 +000041 /**
42 * All pixels have their color components stored without any regard to the
43 * alpha. e.g. this is the default configuration for PNG images.
44 *
45 * This alpha-type is ONLY supported for input images. Rendering cannot
46 * generate this on output.
47 */
48 kUnpremul_SkAlphaType,
skia.committer@gmail.com73a5d532013-11-14 07:02:31 +000049
reed@google.com3443fd82013-11-13 19:09:13 +000050 kLastEnum_SkAlphaType = kUnpremul_SkAlphaType
51};
52
53static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) {
54 SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_order);
55 SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order);
56 SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order);
57
58 return (unsigned)at <= kOpaque_SkAlphaType;
59}
60
61///////////////////////////////////////////////////////////////////////////////
62
63/**
64 * Describes how to interpret the components of a pixel.
65 */
66enum SkColorType {
67 kAlpha_8_SkColorType,
68 kRGB_565_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000069 kARGB_4444_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000070 kRGBA_8888_SkColorType,
71 kBGRA_8888_SkColorType,
reed@google.com9230ea22013-12-09 22:01:03 +000072 kIndex_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000073
reed@google.com9230ea22013-12-09 22:01:03 +000074 kLastEnum_SkColorType = kIndex_8_SkColorType,
reed@google.com3443fd82013-11-13 19:09:13 +000075
76#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
77 kPMColor_SkColorType = kBGRA_8888_SkColorType
78#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
79 kPMColor_SkColorType = kRGBA_8888_SkColorType
80#else
81#error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
82#endif
83};
84
85static int SkColorTypeBytesPerPixel(SkColorType ct) {
86 static const uint8_t gSize[] = {
87 1, // Alpha_8
88 2, // RGB_565
reed@google.com9230ea22013-12-09 22:01:03 +000089 2, // ARGB_4444
reed@google.com3443fd82013-11-13 19:09:13 +000090 4, // RGBA_8888
91 4, // BGRA_8888
92 1, // kIndex_8
93 };
94 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1),
95 size_mismatch_with_SkColorType_enum);
96
97 SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize));
98 return gSize[ct];
99}
100
101///////////////////////////////////////////////////////////////////////////////
102
103/**
104 * Describe an image's dimensions and pixel type.
105 */
106struct SkImageInfo {
107 int fWidth;
108 int fHeight;
109 SkColorType fColorType;
110 SkAlphaType fAlphaType;
111
112 bool isOpaque() const {
113 return SkAlphaTypeIsOpaque(fAlphaType);
114 }
115
116 int bytesPerPixel() const {
117 return SkColorTypeBytesPerPixel(fColorType);
118 }
119
reed@google.com9230ea22013-12-09 22:01:03 +0000120 size_t minRowBytes() const {
121 return fWidth * this->bytesPerPixel();
122 }
123
reed@google.com3443fd82013-11-13 19:09:13 +0000124 bool operator==(const SkImageInfo& other) const {
125 return 0 == memcmp(this, &other, sizeof(other));
126 }
127 bool operator!=(const SkImageInfo& other) const {
128 return 0 != memcmp(this, &other, sizeof(other));
129 }
reed@google.com9230ea22013-12-09 22:01:03 +0000130
131 void unflatten(SkFlattenableReadBuffer&);
132 void flatten(SkFlattenableWriteBuffer&) const;
133
134 size_t getSafeSize(size_t rowBytes) const {
135 if (0 == fHeight) {
136 return 0;
137 }
138 return (fHeight - 1) * rowBytes + fWidth * this->bytesPerPixel();
139 }
reed@google.com3443fd82013-11-13 19:09:13 +0000140};
141
142#endif