1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkImageInfo.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,229 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkImageInfo_DEFINED 1.12 +#define SkImageInfo_DEFINED 1.13 + 1.14 +#include "SkMath.h" 1.15 +#include "SkSize.h" 1.16 + 1.17 +class SkWriteBuffer; 1.18 +class SkReadBuffer; 1.19 + 1.20 +/** 1.21 + * Describes how to interpret the alpha compoent of a pixel. 1.22 + */ 1.23 +enum SkAlphaType { 1.24 + /** 1.25 + * All pixels should be treated as opaque, regardless of the value stored 1.26 + * in their alpha field. Used for legacy images that wrote 0 or garbarge 1.27 + * in their alpha field, but intended the RGB to be treated as opaque. 1.28 + */ 1.29 + kIgnore_SkAlphaType, 1.30 + 1.31 + /** 1.32 + * All pixels are stored as opaque. This differs slightly from kIgnore in 1.33 + * that kOpaque has correct "opaque" values stored in the pixels, while 1.34 + * kIgnore may not, but in both cases the caller should treat the pixels 1.35 + * as opaque. 1.36 + */ 1.37 + kOpaque_SkAlphaType, 1.38 + 1.39 + /** 1.40 + * All pixels have their alpha premultiplied in their color components. 1.41 + * This is the natural format for the rendering target pixels. 1.42 + */ 1.43 + kPremul_SkAlphaType, 1.44 + 1.45 + /** 1.46 + * All pixels have their color components stored without any regard to the 1.47 + * alpha. e.g. this is the default configuration for PNG images. 1.48 + * 1.49 + * This alpha-type is ONLY supported for input images. Rendering cannot 1.50 + * generate this on output. 1.51 + */ 1.52 + kUnpremul_SkAlphaType, 1.53 + 1.54 + kLastEnum_SkAlphaType = kUnpremul_SkAlphaType 1.55 +}; 1.56 + 1.57 +static inline bool SkAlphaTypeIsOpaque(SkAlphaType at) { 1.58 + SK_COMPILE_ASSERT(kIgnore_SkAlphaType < kOpaque_SkAlphaType, bad_alphatype_order); 1.59 + SK_COMPILE_ASSERT(kPremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order); 1.60 + SK_COMPILE_ASSERT(kUnpremul_SkAlphaType > kOpaque_SkAlphaType, bad_alphatype_order); 1.61 + 1.62 + return (unsigned)at <= kOpaque_SkAlphaType; 1.63 +} 1.64 + 1.65 +static inline bool SkAlphaTypeIsValid(unsigned value) { 1.66 + return value <= kLastEnum_SkAlphaType; 1.67 +} 1.68 + 1.69 +/////////////////////////////////////////////////////////////////////////////// 1.70 + 1.71 +/** 1.72 + * Describes how to interpret the components of a pixel. 1.73 + */ 1.74 +enum SkColorType { 1.75 + kUnknown_SkColorType, 1.76 + kAlpha_8_SkColorType, 1.77 + kRGB_565_SkColorType, 1.78 + kARGB_4444_SkColorType, 1.79 + kRGBA_8888_SkColorType, 1.80 + kBGRA_8888_SkColorType, 1.81 + kIndex_8_SkColorType, 1.82 + 1.83 + kLastEnum_SkColorType = kIndex_8_SkColorType, 1.84 + 1.85 +#if SK_PMCOLOR_BYTE_ORDER(B,G,R,A) 1.86 + kPMColor_SkColorType = kBGRA_8888_SkColorType 1.87 +#elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A) 1.88 + kPMColor_SkColorType = kRGBA_8888_SkColorType 1.89 +#else 1.90 +#error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order" 1.91 +#endif 1.92 +}; 1.93 + 1.94 +static int SkColorTypeBytesPerPixel(SkColorType ct) { 1.95 + static const uint8_t gSize[] = { 1.96 + 0, // Unknown 1.97 + 1, // Alpha_8 1.98 + 2, // RGB_565 1.99 + 2, // ARGB_4444 1.100 + 4, // RGBA_8888 1.101 + 4, // BGRA_8888 1.102 + 1, // kIndex_8 1.103 + }; 1.104 + SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gSize) == (size_t)(kLastEnum_SkColorType + 1), 1.105 + size_mismatch_with_SkColorType_enum); 1.106 + 1.107 + SkASSERT((size_t)ct < SK_ARRAY_COUNT(gSize)); 1.108 + return gSize[ct]; 1.109 +} 1.110 + 1.111 +static inline size_t SkColorTypeMinRowBytes(SkColorType ct, int width) { 1.112 + return width * SkColorTypeBytesPerPixel(ct); 1.113 +} 1.114 + 1.115 +static inline bool SkColorTypeIsValid(unsigned value) { 1.116 + return value <= kLastEnum_SkColorType; 1.117 +} 1.118 + 1.119 +/////////////////////////////////////////////////////////////////////////////// 1.120 + 1.121 +/** 1.122 + * Describe an image's dimensions and pixel type. 1.123 + */ 1.124 +struct SkImageInfo { 1.125 + int fWidth; 1.126 + int fHeight; 1.127 + SkColorType fColorType; 1.128 + SkAlphaType fAlphaType; 1.129 + 1.130 + static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at) { 1.131 + SkImageInfo info = { 1.132 + width, height, ct, at 1.133 + }; 1.134 + return info; 1.135 + } 1.136 + 1.137 + /** 1.138 + * Sets colortype to the native ARGB32 type. 1.139 + */ 1.140 + static SkImageInfo MakeN32(int width, int height, SkAlphaType at) { 1.141 + SkImageInfo info = { 1.142 + width, height, kPMColor_SkColorType, at 1.143 + }; 1.144 + return info; 1.145 + } 1.146 + 1.147 + /** 1.148 + * Sets colortype to the native ARGB32 type, and the alphatype to premul. 1.149 + */ 1.150 + static SkImageInfo MakeN32Premul(int width, int height) { 1.151 + SkImageInfo info = { 1.152 + width, height, kPMColor_SkColorType, kPremul_SkAlphaType 1.153 + }; 1.154 + return info; 1.155 + } 1.156 + 1.157 + /** 1.158 + * Sets colortype to the native ARGB32 type, and the alphatype to premul. 1.159 + */ 1.160 + static SkImageInfo MakeN32Premul(const SkISize& size) { 1.161 + return MakeN32Premul(size.width(), size.height()); 1.162 + } 1.163 + 1.164 + static SkImageInfo MakeA8(int width, int height) { 1.165 + SkImageInfo info = { 1.166 + width, height, kAlpha_8_SkColorType, kPremul_SkAlphaType 1.167 + }; 1.168 + return info; 1.169 + } 1.170 + 1.171 + static SkImageInfo MakeUnknown(int width, int height) { 1.172 + SkImageInfo info = { 1.173 + width, height, kUnknown_SkColorType, kIgnore_SkAlphaType 1.174 + }; 1.175 + return info; 1.176 + } 1.177 + 1.178 + int width() const { return fWidth; } 1.179 + int height() const { return fHeight; } 1.180 + SkColorType colorType() const { return fColorType; } 1.181 + SkAlphaType alphaType() const { return fAlphaType; } 1.182 + 1.183 + bool isEmpty() const { return fWidth <= 0 || fHeight <= 0; } 1.184 + 1.185 + bool isOpaque() const { 1.186 + return SkAlphaTypeIsOpaque(fAlphaType); 1.187 + } 1.188 + 1.189 + SkISize dimensions() const { return SkISize::Make(fWidth, fHeight); } 1.190 + 1.191 + int bytesPerPixel() const { 1.192 + return SkColorTypeBytesPerPixel(fColorType); 1.193 + } 1.194 + 1.195 + uint64_t minRowBytes64() const { 1.196 + return sk_64_mul(fWidth, this->bytesPerPixel()); 1.197 + } 1.198 + 1.199 + size_t minRowBytes() const { 1.200 + return (size_t)this->minRowBytes64(); 1.201 + } 1.202 + 1.203 + bool operator==(const SkImageInfo& other) const { 1.204 + return 0 == memcmp(this, &other, sizeof(other)); 1.205 + } 1.206 + bool operator!=(const SkImageInfo& other) const { 1.207 + return 0 != memcmp(this, &other, sizeof(other)); 1.208 + } 1.209 + 1.210 + void unflatten(SkReadBuffer&); 1.211 + void flatten(SkWriteBuffer&) const; 1.212 + 1.213 + int64_t getSafeSize64(size_t rowBytes) const { 1.214 + if (0 == fHeight) { 1.215 + return 0; 1.216 + } 1.217 + return sk_64_mul(fHeight - 1, rowBytes) + fWidth * this->bytesPerPixel(); 1.218 + } 1.219 + 1.220 + size_t getSafeSize(size_t rowBytes) const { 1.221 + return (size_t)this->getSafeSize64(rowBytes); 1.222 + } 1.223 + 1.224 + bool validRowBytes(size_t rowBytes) const { 1.225 + uint64_t rb = sk_64_mul(fWidth, this->bytesPerPixel()); 1.226 + return rowBytes >= rb; 1.227 + } 1.228 + 1.229 + SkDEBUGCODE(void validate() const;) 1.230 +}; 1.231 + 1.232 +#endif