| 1 |
/* |
| 2 |
* Copyright 2016 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 SkEncodedInfo_DEFINED |
| 9 |
#define SkEncodedInfo_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkAlphaType.h" |
| 12 |
#include "include/core/SkColorSpace.h" |
| 13 |
#include "include/core/SkColorType.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkTypes.h" |
| 18 |
#include "include/private/SkHdrMetadata.h" |
| 19 |
#include "include/private/base/SkAPI.h" |
| 20 |
#include "include/private/base/SkTo.h" |
| 21 |
#include "modules/skcms/skcms.h" |
| 22 |
|
| 23 |
#include <cstdint> |
| 24 |
#include <memory> |
| 25 |
#include <tuple> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
namespace SkCodecs { |
| 29 |
class ColorProfile; |
| 30 |
} |
| 31 |
|
| 32 |
struct SK_API SkEncodedInfo { |
| 33 |
public: |
| 34 |
enum Alpha { |
| 35 |
kOpaque_Alpha, |
| 36 |
kUnpremul_Alpha, |
| 37 |
|
| 38 |
// Each pixel is either fully opaque or fully transparent. |
| 39 |
// There is no difference between requesting kPremul or kUnpremul. |
| 40 |
kBinary_Alpha, |
| 41 |
}; |
| 42 |
|
| 43 |
/* |
| 44 |
* We strive to make the number of components per pixel obvious through |
| 45 |
* our naming conventions. |
| 46 |
* Ex: kRGB has 3 components. kRGBA has 4 components. |
| 47 |
* |
| 48 |
* This sometimes results in redundant Alpha and Color information. |
| 49 |
* Ex: kRGB images must also be kOpaque. |
| 50 |
*/ |
| 51 |
enum Color { |
| 52 |
// PNG, WBMP |
| 53 |
kGray_Color, |
| 54 |
|
| 55 |
// PNG |
| 56 |
kGrayAlpha_Color, |
| 57 |
|
| 58 |
// PNG with Skia-specific sBIT |
| 59 |
// Like kGrayAlpha, except this expects to be treated as |
| 60 |
// kAlpha_8_SkColorType, which ignores the gray component. If |
| 61 |
// decoded to full color (e.g. kN32), the gray component is respected |
| 62 |
// (so it can share code with kGrayAlpha). |
| 63 |
kXAlpha_Color, |
| 64 |
|
| 65 |
// PNG |
| 66 |
// 565 images may be encoded to PNG by specifying the number of |
| 67 |
// significant bits for each channel. This is a strange 565 |
| 68 |
// representation because the image is still encoded with 8 bits per |
| 69 |
// component. |
| 70 |
k565_Color, |
| 71 |
|
| 72 |
// PNG, GIF, BMP |
| 73 |
kPalette_Color, |
| 74 |
|
| 75 |
// PNG, RAW |
| 76 |
kRGB_Color, |
| 77 |
kRGBA_Color, |
| 78 |
|
| 79 |
// BMP |
| 80 |
kBGR_Color, |
| 81 |
kBGRX_Color, |
| 82 |
kBGRA_Color, |
| 83 |
|
| 84 |
// JPEG, WEBP |
| 85 |
kYUV_Color, |
| 86 |
|
| 87 |
// WEBP |
| 88 |
kYUVA_Color, |
| 89 |
|
| 90 |
// JPEG |
| 91 |
// Photoshop actually writes inverted CMYK data into JPEGs, where zero |
| 92 |
// represents 100% ink coverage. For this reason, we treat CMYK JPEGs |
| 93 |
// as having inverted CMYK. libjpeg-turbo warns that this may break |
| 94 |
// other applications, but the CMYK JPEGs we see on the web expect to |
| 95 |
// be treated as inverted CMYK. |
| 96 |
kInvertedCMYK_Color, |
| 97 |
kYCCK_Color, |
| 98 |
}; |
| 99 |
|
| 100 |
static SkEncodedInfo Make( |
| 101 |
int width, int height, Color color, Alpha alpha, int bitsPerComponent); |
| 102 |
|
| 103 |
static SkEncodedInfo Make( |
| 104 |
int width, int height, Color color, Alpha alpha, int bitsPerComponent, |
| 105 |
std::unique_ptr<SkCodecs::ColorProfile> profile); |
| 106 |
|
| 107 |
static SkEncodedInfo Make( |
| 108 |
int width, int height, Color color, Alpha alpha, int bitsPerComponent, |
| 109 |
std::unique_ptr<SkCodecs::ColorProfile> profile, int colorDepth); |
| 110 |
|
| 111 |
static SkEncodedInfo Make( |
| 112 |
int width, int height, Color color, Alpha alpha, int bitsPerComponent, |
| 113 |
int colorDepth, |
| 114 |
std::unique_ptr<SkCodecs::ColorProfile> profile, const skhdr::Metadata& hdrMetadata); |
| 115 |
|
| 116 |
/* |
| 117 |
* Returns a recommended SkImageInfo. |
| 118 |
* |
| 119 |
* TODO: Leave this up to the client. |
| 120 |
*/ |
| 121 |
SkImageInfo makeImageInfo() const; |
| 122 |
|
| 123 |
int width() const { return fWidth; } |
| 124 |
int height() const { return fHeight; } |
| 125 |
Color color() const { return fColor; } |
| 126 |
Alpha alpha() const { return fAlpha; } |
| 127 |
bool opaque() const { return fAlpha == kOpaque_Alpha; } |
| 128 |
|
| 129 |
// TODO(https://issues.skia.org/issues/464217864): Remove direct access to the |
| 130 |
// skcms_ICCProfile and change profileData() to serialize a new profile. |
| 131 |
const skcms_ICCProfile* profile() const; |
| 132 |
sk_sp<const SkData> profileData() const; |
| 133 |
const SkCodecs::ColorProfile* colorProfile() const { |
| 134 |
return fColorProfile.get(); |
| 135 |
} |
| 136 |
|
| 137 |
uint8_t bitsPerComponent() const { return fBitsPerComponent; } |
| 138 |
|
| 139 |
uint8_t bitsPerPixel() const { |
| 140 |
switch (fColor) { |
| 141 |
case kGray_Color: |
| 142 |
return fBitsPerComponent; |
| 143 |
case kXAlpha_Color: |
| 144 |
case kGrayAlpha_Color: |
| 145 |
return 2 * fBitsPerComponent; |
| 146 |
case kPalette_Color: |
| 147 |
return fBitsPerComponent; |
| 148 |
case kRGB_Color: |
| 149 |
case kBGR_Color: |
| 150 |
case kYUV_Color: |
| 151 |
case k565_Color: |
| 152 |
return 3 * fBitsPerComponent; |
| 153 |
case kRGBA_Color: |
| 154 |
case kBGRA_Color: |
| 155 |
case kBGRX_Color: |
| 156 |
case kYUVA_Color: |
| 157 |
case kInvertedCMYK_Color: |
| 158 |
case kYCCK_Color: |
| 159 |
return 4 * fBitsPerComponent; |
| 160 |
} |
| 161 |
SkASSERT(false); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
SkEncodedInfo(const SkEncodedInfo& orig) = delete; |
| 166 |
SkEncodedInfo& operator=(const SkEncodedInfo&) = delete; |
| 167 |
|
| 168 |
SkEncodedInfo(SkEncodedInfo&& orig); |
| 169 |
SkEncodedInfo& operator=(SkEncodedInfo&&); |
| 170 |
|
| 171 |
// Explicit copy method, to avoid accidental copying. |
| 172 |
SkEncodedInfo copy() const; |
| 173 |
|
| 174 |
// Return number of bits of R/G/B channel |
| 175 |
uint8_t getColorDepth() const { |
| 176 |
return fColorDepth; |
| 177 |
} |
| 178 |
|
| 179 |
// Return the HDR metadata associated with this image. Note that even SDR images can include |
| 180 |
// HDR metadata (e.g, indicating how to inverse tone map when displayed on an HDR display). |
| 181 |
const skhdr::Metadata& getHdrMetadata() const { |
| 182 |
return fHdrMetadata; |
| 183 |
} |
| 184 |
|
| 185 |
~SkEncodedInfo(); |
| 186 |
|
| 187 |
private: |
| 188 |
SkEncodedInfo( |
| 189 |
int width, int height, Color color, Alpha alpha, uint8_t bitsPerComponent, |
| 190 |
uint8_t colorDepth, |
| 191 |
std::unique_ptr<SkCodecs::ColorProfile> profile, const skhdr::Metadata& hdrMetadata); |
| 192 |
|
| 193 |
static void VerifyColor(Color color, Alpha alpha, int bitsPerComponent) { |
| 194 |
// Avoid `-Wunused-parameter` warnings on non-debug builds. |
| 195 |
std::ignore = alpha; |
| 196 |
std::ignore = bitsPerComponent; |
| 197 |
|
| 198 |
switch (color) { |
| 199 |
case kGray_Color: |
| 200 |
SkASSERT(kOpaque_Alpha == alpha); |
| 201 |
return; |
| 202 |
case kGrayAlpha_Color: |
| 203 |
SkASSERT(kOpaque_Alpha != alpha); |
| 204 |
return; |
| 205 |
case kPalette_Color: |
| 206 |
SkASSERT(16 != bitsPerComponent); |
| 207 |
return; |
| 208 |
case kRGB_Color: |
| 209 |
case kBGR_Color: |
| 210 |
case kBGRX_Color: |
| 211 |
SkASSERT(kOpaque_Alpha == alpha); |
| 212 |
SkASSERT(bitsPerComponent >= 8); |
| 213 |
return; |
| 214 |
case kYUV_Color: |
| 215 |
case kInvertedCMYK_Color: |
| 216 |
case kYCCK_Color: |
| 217 |
SkASSERT(kOpaque_Alpha == alpha); |
| 218 |
SkASSERT(8 == bitsPerComponent); |
| 219 |
return; |
| 220 |
case kRGBA_Color: |
| 221 |
SkASSERT(bitsPerComponent >= 8); |
| 222 |
return; |
| 223 |
case kBGRA_Color: |
| 224 |
case kYUVA_Color: |
| 225 |
SkASSERT(8 == bitsPerComponent); |
| 226 |
return; |
| 227 |
case kXAlpha_Color: |
| 228 |
SkASSERT(kUnpremul_Alpha == alpha); |
| 229 |
SkASSERT(8 == bitsPerComponent); |
| 230 |
return; |
| 231 |
case k565_Color: |
| 232 |
SkASSERT(kOpaque_Alpha == alpha); |
| 233 |
SkASSERT(8 == bitsPerComponent); |
| 234 |
return; |
| 235 |
} |
| 236 |
SkASSERT(false); // Unrecognized `color` enum value. |
| 237 |
} |
| 238 |
|
| 239 |
int fWidth; |
| 240 |
int fHeight; |
| 241 |
Color fColor; |
| 242 |
Alpha fAlpha; |
| 243 |
uint8_t fBitsPerComponent; |
| 244 |
uint8_t fColorDepth; |
| 245 |
std::unique_ptr<const SkCodecs::ColorProfile> fColorProfile; |
| 246 |
skhdr::Metadata fHdrMetadata; |
| 247 |
}; |
| 248 |
|
| 249 |
#endif |