SkImageInfo.h revision 3e89524e747b513986abfeeea00b6fac79593f26
1/*
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
13class SkFlattenableWriteBuffer;
14class SkFlattenableReadBuffer;
15
16/**
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,
26
27    /**
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,
34
35    /**
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,
40
41    /**
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,
49
50    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,
69    kARGB_4444_SkColorType,
70    kRGBA_8888_SkColorType,
71    kBGRA_8888_SkColorType,
72    kIndex8_SkColorType,
73
74    kLastEnum_SkColorType = kIndex8_SkColorType,
75
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
89        2,  // ARGB_4444
90        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
120    size_t minRowBytes() const {
121        return fWidth * this->bytesPerPixel();
122    }
123
124    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    }
130
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    }
140};
141
142#endif
143