| 1 |
/* |
| 2 |
* Copyright 2015 The Android Open Source Project |
| 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 SkCodecPriv_DEFINED |
| 9 |
#define SkCodecPriv_DEFINED |
| 10 |
|
| 11 |
#include "include/codec/SkCodec.h" |
| 12 |
#include "include/codec/SkEncodedOrigin.h" |
| 13 |
#include "include/core/SkImageInfo.h" |
| 14 |
#include "include/core/SkTypes.h" |
| 15 |
#include "include/private/SkEncodedInfo.h" |
| 16 |
#include "modules/skcms/skcms.h" |
| 17 |
#include "src/codec/SkColorPalette.h" |
| 18 |
#include "src/core/SkColorData.h" |
| 19 |
|
| 20 |
#include <string_view> |
| 21 |
|
| 22 |
#ifdef SK_PRINT_CODEC_MESSAGES |
| 23 |
#define SkCodecPrintf SkDebugf |
| 24 |
#else |
| 25 |
#define SkCodecPrintf(...) |
| 26 |
#endif |
| 27 |
|
| 28 |
namespace SkCodecs { |
| 29 |
|
| 30 |
bool HasDecoder(std::string_view id); |
| 31 |
|
| 32 |
// All color profile information from an SkCodec. |
| 33 |
// TODO(https://issues.skia.org/issues/464217864): This should include skhdr::Metadata. |
| 34 |
class ColorProfile { |
| 35 |
public: |
| 36 |
// Create a ColorProfile from an SkData for an ICC profile. |
| 37 |
// Returns nullptr if the data fails to parse. This will use |
| 38 |
// either MakeICCProfileWithSkCMS or MakeICCProfileWithRust |
| 39 |
// depending on the build configuration. |
| 40 |
static std::unique_ptr<ColorProfile> MakeICCProfile(sk_sp<const SkData>); |
| 41 |
|
| 42 |
// Implementation of MakeICCProfile that uses skcms to parse. |
| 43 |
// This should only be called by tests. |
| 44 |
static std::unique_ptr<ColorProfile> MakeICCProfileWithSkCMS(sk_sp<const SkData>); |
| 45 |
|
| 46 |
// Create a ColorProfile from an SkColorSpace. Returns nullptr if the color space is |
| 47 |
// nullptr. |
| 48 |
static std::unique_ptr<ColorProfile> Make(sk_sp<SkColorSpace>); |
| 49 |
|
| 50 |
// Create a ColorProfile from the specified matrix and transfer curves. This will always |
| 51 |
// succeed. |
| 52 |
static std::unique_ptr<ColorProfile> Make(const skcms_TransferFunction& trfn, |
| 53 |
const skcms_Matrix3x3& toXYZD50); |
| 54 |
|
| 55 |
// Create a ColorProfile with the specified CICP values. This will always succeed, even if |
| 56 |
// the values are unrecognized. |
| 57 |
static std::unique_ptr<ColorProfile> MakeCICP(uint8_t color_primaries, |
| 58 |
uint8_t transfer_characteristics, |
| 59 |
uint8_t matrix_coefficients, |
| 60 |
uint8_t full_range_flag); |
| 61 |
|
| 62 |
// Create a copy of this. |
| 63 |
std::unique_ptr<ColorProfile> clone() const; |
| 64 |
|
| 65 |
// Return the color data space for the profile. |
| 66 |
enum class DataSpace { |
| 67 |
kRGB, // skcms_Signature_RGB |
| 68 |
kCMYK, // skcms_Signature_CMYK |
| 69 |
kGray, // skcms_Signature_Gray |
| 70 |
kOther, // all other values |
| 71 |
}; |
| 72 |
DataSpace dataSpace() const; |
| 73 |
|
| 74 |
// Return the color space that this profile represents exactly. Return nullptr if this profile |
| 75 |
// cannot be represented as an SkColorSpace. |
| 76 |
sk_sp<SkColorSpace> getExactColorSpace() const; |
| 77 |
|
| 78 |
// Return the color space that Android uses for this profile (see implementation for details). |
| 79 |
// Will not return nullptr. |
| 80 |
sk_sp<SkColorSpace> getAndroidOutputColorSpace() const; |
| 81 |
|
| 82 |
// TODO(https://issues.skia.org/issues/464217864): Remove direct access to the |
| 83 |
// skcms_ICCProfile and change data() to be a serialize() function. |
| 84 |
const skcms_ICCProfile* profile() const { return &fProfile; } |
| 85 |
sk_sp<const SkData> data() const { return fData; } |
| 86 |
|
| 87 |
private: |
| 88 |
ColorProfile(const skcms_ICCProfile&, sk_sp<const SkData> = nullptr); |
| 89 |
|
| 90 |
#if defined(SK_CODEC_COLOR_PROFILE_PARSE_WITH_RUST) |
| 91 |
friend std::unique_ptr<ColorProfile> MakeICCProfileWithRust(sk_sp<const SkData>); |
| 92 |
#endif |
| 93 |
|
| 94 |
skcms_ICCProfile fProfile; |
| 95 |
sk_sp<const SkData> fData; |
| 96 |
}; |
| 97 |
|
| 98 |
} // namespace SkCodecs |
| 99 |
|
| 100 |
class SkCodecPriv final { |
| 101 |
public: |
| 102 |
static const SkEncodedInfo& GetEncodedInfo(const SkCodec* codec) { |
| 103 |
SkASSERT(codec); |
| 104 |
return codec->getEncodedInfo(); |
| 105 |
} |
| 106 |
|
| 107 |
static bool SelectXformFormat(SkColorType colorType, |
| 108 |
bool forColorTable, |
| 109 |
skcms_PixelFormat* outFormat); |
| 110 |
|
| 111 |
// FIXME: Consider sharing with dm, nanbench, and tools. |
| 112 |
static float GetScaleFromSampleSize(int sampleSize) { return 1.0f / ((float)sampleSize); } |
| 113 |
|
| 114 |
static bool IsValidSubset(const SkIRect& subset, const SkISize& imageDims) { |
| 115 |
return SkIRect::MakeSize(imageDims).contains(subset); |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* returns a scaled dimension based on the original dimension and the sampleSize |
| 120 |
* NOTE: we round down here for scaled dimension to match the behavior of SkImageDecoder |
| 121 |
*/ |
| 122 |
static int GetSampledDimension(int srcDimension, int sampleSize) { |
| 123 |
if (sampleSize > srcDimension) { |
| 124 |
return 1; |
| 125 |
} |
| 126 |
if (sampleSize == 0) { |
| 127 |
return 0; |
| 128 |
} |
| 129 |
return srcDimension / sampleSize; |
| 130 |
} |
| 131 |
|
| 132 |
/* |
| 133 |
* Returns the first coordinate that we will keep during a scaled decode. |
| 134 |
* The output can be interpreted as an x-coordinate or a y-coordinate. |
| 135 |
* |
| 136 |
* This does not need to be called and is not called when sampleFactor == 1. |
| 137 |
*/ |
| 138 |
static int GetStartCoord(int sampleFactor) { return sampleFactor / 2; } |
| 139 |
|
| 140 |
/* |
| 141 |
* Given a coordinate in the original image, this returns the corresponding |
| 142 |
* coordinate in the scaled image. This function is meaningless if |
| 143 |
* IsCoordNecessary returns false. |
| 144 |
* The output can be interpreted as an x-coordinate or a y-coordinate. |
| 145 |
* |
| 146 |
* This does not need to be called and is not called when sampleFactor == 1. |
| 147 |
*/ |
| 148 |
static int GetDstCoord(int srcCoord, int sampleFactor) { return srcCoord / sampleFactor; } |
| 149 |
|
| 150 |
/* |
| 151 |
* When scaling, we will discard certain y-coordinates (rows) and |
| 152 |
* x-coordinates (columns). This function returns true if we should keep the |
| 153 |
* coordinate and false otherwise. |
| 154 |
* The inputs may be x-coordinates or y-coordinates. |
| 155 |
* |
| 156 |
* This does not need to be called and is not called when sampleFactor == 1. |
| 157 |
*/ |
| 158 |
static bool IsCoordNecessary(int srcCoord, int sampleFactor, int scaledDim) { |
| 159 |
// Get the first coordinate that we want to keep |
| 160 |
int startCoord = GetStartCoord(sampleFactor); |
| 161 |
|
| 162 |
// Return false on edge cases |
| 163 |
if (srcCoord < startCoord || GetDstCoord(srcCoord, sampleFactor) >= scaledDim) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
// Every sampleFactor rows are necessary |
| 168 |
return ((srcCoord - startCoord) % sampleFactor) == 0; |
| 169 |
} |
| 170 |
|
| 171 |
static bool ValidAlpha(SkAlphaType dstAlpha, bool srcIsOpaque) { |
| 172 |
if (kUnknown_SkAlphaType == dstAlpha) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
if (srcIsOpaque) { |
| 177 |
if (kOpaque_SkAlphaType != dstAlpha) { |
| 178 |
SkCodecPrintf( |
| 179 |
"Warning: an opaque image should be decoded as opaque " |
| 180 |
"- it is being decoded as non-opaque, which will draw slower\n"); |
| 181 |
} |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
return dstAlpha != kOpaque_SkAlphaType; |
| 186 |
} |
| 187 |
|
| 188 |
/* |
| 189 |
* If there is a color table, get a pointer to the colors, otherwise return nullptr |
| 190 |
*/ |
| 191 |
static const SkPMColor* GetColorPtr(SkColorPalette* colorTable) { |
| 192 |
return nullptr != colorTable ? colorTable->readColors() : nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
/* |
| 196 |
* Compute row bytes for an image using pixels per byte |
| 197 |
*/ |
| 198 |
static size_t ComputeRowBytesPixelsPerByte(int width, uint32_t pixelsPerByte) { |
| 199 |
return (width + pixelsPerByte - 1) / pixelsPerByte; |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
* Compute row bytes for an image using bytes per pixel |
| 204 |
*/ |
| 205 |
static size_t ComputeRowBytesBytesPerPixel(int width, uint32_t bytesPerPixel) { |
| 206 |
return width * bytesPerPixel; |
| 207 |
} |
| 208 |
|
| 209 |
/* |
| 210 |
* Compute row bytes for an image |
| 211 |
*/ |
| 212 |
static size_t ComputeRowBytes(int width, uint32_t bitsPerPixel) { |
| 213 |
if (bitsPerPixel < 16) { |
| 214 |
SkASSERT(0 == 8 % bitsPerPixel); |
| 215 |
const uint32_t pixelsPerByte = 8 / bitsPerPixel; |
| 216 |
return ComputeRowBytesPixelsPerByte(width, pixelsPerByte); |
| 217 |
} else { |
| 218 |
SkASSERT(0 == bitsPerPixel % 8); |
| 219 |
const uint32_t bytesPerPixel = bitsPerPixel / 8; |
| 220 |
return ComputeRowBytesBytesPerPixel(width, bytesPerPixel); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* Get a byte from a buffer |
| 226 |
* This method is unsafe, the caller is responsible for performing a check |
| 227 |
*/ |
| 228 |
static uint8_t UnsafeGetByte(const uint8_t* buffer, uint32_t i) { return buffer[i]; } |
| 229 |
|
| 230 |
/* |
| 231 |
* Get a short from a buffer |
| 232 |
* This method is unsafe, the caller is responsible for performing a check |
| 233 |
*/ |
| 234 |
static uint16_t UnsafeGetShort(const uint8_t* buffer, uint32_t i) { |
| 235 |
uint16_t result; |
| 236 |
memcpy(&result, &(buffer[i]), 2); |
| 237 |
#ifdef SK_CPU_BENDIAN |
| 238 |
return SkEndianSwap16(result); |
| 239 |
#else |
| 240 |
return result; |
| 241 |
#endif |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* Get an int from a buffer |
| 246 |
* This method is unsafe, the caller is responsible for performing a check |
| 247 |
*/ |
| 248 |
static uint32_t UnsafeGetInt(const uint8_t* buffer, uint32_t i) { |
| 249 |
uint32_t result; |
| 250 |
memcpy(&result, &(buffer[i]), 4); |
| 251 |
#ifdef SK_CPU_BENDIAN |
| 252 |
return SkEndianSwap32(result); |
| 253 |
#else |
| 254 |
return result; |
| 255 |
#endif |
| 256 |
} |
| 257 |
|
| 258 |
/* |
| 259 |
* @param data Buffer to read bytes from |
| 260 |
* @param isLittleEndian Output parameter |
| 261 |
* Indicates if the data is little endian |
| 262 |
* Is unaffected on false returns |
| 263 |
*/ |
| 264 |
static bool IsValidEndianMarker(const uint8_t* data, bool* isLittleEndian) { |
| 265 |
// II indicates Intel (little endian) and MM indicates motorola (big endian). |
| 266 |
if (('I' != data[0] || 'I' != data[1]) && ('M' != data[0] || 'M' != data[1])) { |
| 267 |
return false; |
| 268 |
} |
| 269 |
|
| 270 |
*isLittleEndian = ('I' == data[0]); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
static uint16_t GetEndianShort(const uint8_t* data, bool littleEndian) { |
| 275 |
if (littleEndian) { |
| 276 |
return (data[1] << 8) | (data[0]); |
| 277 |
} |
| 278 |
|
| 279 |
return (data[0] << 8) | (data[1]); |
| 280 |
} |
| 281 |
|
| 282 |
static uint32_t GetEndianInt(const uint8_t* data, bool littleEndian) { |
| 283 |
if (littleEndian) { |
| 284 |
return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | (data[0]); |
| 285 |
} |
| 286 |
|
| 287 |
return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]); |
| 288 |
} |
| 289 |
|
| 290 |
static SkPMColor PremultiplyARGBasRGBA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
| 291 |
if (a != 255) { |
| 292 |
r = SkMulDiv255Round(r, a); |
| 293 |
g = SkMulDiv255Round(g, a); |
| 294 |
b = SkMulDiv255Round(b, a); |
| 295 |
} |
| 296 |
|
| 297 |
return SkPackARGB_as_RGBA(a, r, g, b); |
| 298 |
} |
| 299 |
|
| 300 |
static SkPMColor PremultiplyARGBasBGRA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) { |
| 301 |
if (a != 255) { |
| 302 |
r = SkMulDiv255Round(r, a); |
| 303 |
g = SkMulDiv255Round(g, a); |
| 304 |
b = SkMulDiv255Round(b, a); |
| 305 |
} |
| 306 |
|
| 307 |
return SkPackARGB_as_BGRA(a, r, g, b); |
| 308 |
} |
| 309 |
|
| 310 |
static bool IsRGBA(SkColorType colorType) { |
| 311 |
#ifdef SK_PMCOLOR_IS_RGBA |
| 312 |
return (kBGRA_8888_SkColorType != colorType); |
| 313 |
#else |
| 314 |
return (kRGBA_8888_SkColorType == colorType); |
| 315 |
#endif |
| 316 |
} |
| 317 |
|
| 318 |
// Method for coverting to a 32 bit pixel. |
| 319 |
using PackColorProc = uint32_t (*)(U8CPU a, U8CPU r, U8CPU g, U8CPU b); |
| 320 |
|
| 321 |
static PackColorProc ChoosePackColorProc(bool isPremul, SkColorType colorType) { |
| 322 |
bool isRGBA = IsRGBA(colorType); |
| 323 |
if (isPremul) { |
| 324 |
if (isRGBA) { |
| 325 |
return &PremultiplyARGBasRGBA; |
| 326 |
} else { |
| 327 |
return &PremultiplyARGBasBGRA; |
| 328 |
} |
| 329 |
} else { |
| 330 |
if (isRGBA) { |
| 331 |
return &SkPackARGB_as_RGBA; |
| 332 |
} else { |
| 333 |
return &SkPackARGB_as_BGRA; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
#endif // SkCodecPriv_DEFINED |