Flutter Engine
The Flutter Engine
Classes | Functions
png_codec.cpp File Reference
#include "gm/gm.h"
#include "include/codec/SkCodec.h"
#include "include/codec/SkEncodedImageFormat.h"
#include "include/codec/SkPngDecoder.h"
#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkImage.h"
#include "include/core/SkRect.h"
#include "include/core/SkSize.h"
#include "include/core/SkStream.h"
#include "include/core/SkString.h"
#include "include/private/base/SkTArray.h"
#include "include/private/base/SkTemplates.h"
#include "src/base/SkAutoMalloc.h"
#include "src/core/SkSwizzlePriv.h"
#include "src/utils/SkOSPath.h"
#include "tools/flags/CommandLineFlags.h"
#include "tools/flags/CommonFlags.h"
#include <map>
#include <memory>
#include <string>
#include <vector>

Go to the source code of this file.

Classes

struct  DecodeResult
 
class  PNGCodecGM
 

Functions

 DEFINE_string (pngCodecGMImages, "", "Zero or more images or directories where to find PNG images to test with " "PNGCodecGM. Directories are scanned non-recursively. All files are assumed to be " "PNG images.")
 
 DEFINE_string (pngCodecDecodeMode, "", "One of \"get-all-pixels\", \"incremental\" or \"zero-init\".")
 
 DEFINE_string (pngCodecDstColorType, "", "One of \"force-grayscale\", " "\"force-nonnative-premul-color\" or \"get-from-canvas\".")
 
 DEFINE_string (pngCodecDstAlphaType, "", "One of \"premul\" or \"unpremul\".")
 
static constexpr const char * sk_color_type_to_str (SkColorType colorType)
 
static constexpr const char * sk_alpha_type_to_str (SkAlphaType alphaType)
 
static DecodeResult decode (std::string path)
 
static std::string registerGMsForImage (std::string path, PNGCodecGM::DecodeMode decodeMode, PNGCodecGM::DstColorType dstColorType, SkAlphaType dstAlphaType)
 
static std::string parse_and_validate_flags (PNGCodecGM::DecodeMode *decodeMode, PNGCodecGM::DstColorType *dstColorType, SkAlphaType *dstAlphaType)
 
 DEF_GM_REGISTERER_FN ([]() -> std::string { PNGCodecGM::DecodeMode decodeMode;PNGCodecGM::DstColorType dstColorType;SkAlphaType dstAlphaType;if(std::string errorMsg=parse_and_validate_flags(&decodeMode, &dstColorType, &dstAlphaType);errorMsg !="") { return errorMsg;} skia_private::TArray< SkString > images;if(!CommonFlags::CollectImages(FLAGS_pngCodecGMImages, &images)) { return "Failed to collect images.";} for(const SkString &image :images) { if(std::string errorMsg=registerGMsForImage(image.c_str(), decodeMode, dstColorType, dstAlphaType);errorMsg !="") { return errorMsg;} } return "";})
 

Function Documentation

◆ decode()

static DecodeResult decode ( std::string  path)
static

Definition at line 124 of file png_codec.cpp.

124 {
126 if (!encoded) {
127 return {.errorMsg = SkStringPrintf("Could not read \"%s\".", path.c_str()).c_str()};
128 }
130 std::unique_ptr<SkCodec> codec = SkPngDecoder::Decode(SkMemoryStream::Make(encoded), &result);
132 return {.errorMsg = SkStringPrintf("Could not create codec for \"%s\": %s.",
133 path.c_str(),
135 .c_str()};
136 }
137 return {.codec = std::move(codec)};
138}
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
static const char * ResultToString(Result)
Definition: SkCodec.cpp:880
Result
Definition: SkCodec.h:76
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition: SkData.cpp:148
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Definition: SkStream.cpp:314
const char * c_str() const
Definition: SkString.h:133
@ kSuccess
Definition: embedder.h:73
GAsyncResult * result
SK_API std::unique_ptr< SkCodec > Decode(std::unique_ptr< SkStream >, SkCodec::Result *, SkCodecs::DecodeContext=nullptr)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57

◆ DEF_GM_REGISTERER_FN()

DEF_GM_REGISTERER_FN ( [] () -> std::string { PNGCodecGM::DecodeMode decodeMode;PNGCodecGM::DstColorType dstColorType;SkAlphaType dstAlphaType;if(std::string errorMsg=parse_and_validate_flags(&decodeMode, &dstColorType, &dstAlphaType);errorMsg !="") { return errorMsg;} skia_private::TArray< SkString > images;if(!CommonFlags::CollectImages(FLAGS_pngCodecGMImages, &images)) { return "Failed to collect images.";} for(const SkString &image :images) { if(std::string errorMsg=registerGMsForImage(image.c_str(), decodeMode, dstColorType, dstAlphaType);errorMsg !="") { return errorMsg;} } return "";}  )

◆ DEFINE_string() [1/4]

DEFINE_string ( pngCodecDecodeMode  ,
""  ,
"One of \"get-all-pixels\"  ,
\"incremental\" or \"zero-init\"."   
)

◆ DEFINE_string() [2/4]

DEFINE_string ( pngCodecDstAlphaType  ,
""  ,
"One of \"premul\" or \"unpremul\"."   
)

◆ DEFINE_string() [3/4]

DEFINE_string ( pngCodecDstColorType  ,
""  ,
"One of \"force-grayscale\"  ,
" "\"force-nonnative-premul-color\" or \"get-from-canvas\"."   
)

◆ DEFINE_string() [4/4]

DEFINE_string ( pngCodecGMImages  ,
""  ,
"Zero or more images or directories where to find PNG images to test with " "PNGCodecGM. Directories are scanned non-recursively. All files are assumed to be " "PNG images."   
)

◆ parse_and_validate_flags()

static std::string parse_and_validate_flags ( PNGCodecGM::DecodeMode decodeMode,
PNGCodecGM::DstColorType dstColorType,
SkAlphaType dstAlphaType 
)
static

Definition at line 466 of file png_codec.cpp.

468 {
473 };
474 if (SkString errorMsg = FLAGS_pngCodecDecodeMode.parseAndValidate(
475 "--pngCodecDecodeMode", decodeModeValues, decodeMode);
476 errorMsg != SkString()) {
477 return errorMsg.c_str();
478 }
479
483 {SkString("force-nonnative-premul-color"),
485 };
486 if (SkString errorMsg = FLAGS_pngCodecDstColorType.parseAndValidate(
487 "--pngCodecDstColorType", dstColorTypeValues, dstColorType);
488 errorMsg != SkString()) {
489 return errorMsg.c_str();
490 }
491
493 {SkString("premul"), kPremul_SkAlphaType},
494 {SkString("unpremul"), kUnpremul_SkAlphaType},
495 };
496 if (SkString errorMsg = FLAGS_pngCodecDstAlphaType.parseAndValidate(
497 "--pngCodecDstAlphaType", dstAlphaTypeValues, dstAlphaType);
498 errorMsg != SkString()) {
499 return errorMsg.c_str();
500 }
501
502 return "";
503}
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29

◆ registerGMsForImage()

static std::string registerGMsForImage ( std::string  path,
PNGCodecGM::DecodeMode  decodeMode,
PNGCodecGM::DstColorType  dstColorType,
SkAlphaType  dstAlphaType 
)
static

Definition at line 429 of file png_codec.cpp.

432 {
433 DecodeResult decodeResult = decode(path);
434 if (decodeResult.errorMsg != "") {
435 return decodeResult.errorMsg;
436 }
437
438 if (dstColorType == PNGCodecGM::DstColorType::kForceGrayscale &&
439 decodeResult.codec->getInfo().colorType() != kGray_8_SkColorType) {
440 return SkStringPrintf(
441 "image \"%s\" has color type %s; this is incompatible with the given "
442 "dstColorType argument: %s (expected image color type: %s)",
443 path.c_str(),
444 sk_color_type_to_str(decodeResult.codec->getInfo().colorType()),
447 .c_str();
448 }
449
450 if (dstAlphaType == kUnpremul_SkAlphaType &&
451 decodeResult.codec->getInfo().alphaType() == kOpaque_SkAlphaType) {
452 return SkStringPrintf(
453 "image \"%s\" has alpha type %s; this is incompatible with the given "
454 "dstAlphaType argument: %s",
455 path.c_str(),
458 .c_str();
459 }
460
461 skiagm::Register(new PNGCodecGM(path, decodeMode, dstColorType, dstAlphaType));
462 return "";
463}
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
static constexpr const char * DstColorTypeToString(DstColorType dstColorType)
Definition: png_codec.cpp:175
void Register(skiagm::GM *gm)
Definition: gm.cpp:256
static DecodeResult decode(std::string path)
Definition: png_codec.cpp:124
static constexpr const char * sk_alpha_type_to_str(SkAlphaType alphaType)
Definition: png_codec.cpp:105
static constexpr const char * sk_color_type_to_str(SkColorType colorType)
Definition: png_codec.cpp:47
std::unique_ptr< SkCodec > codec
Definition: png_codec.cpp:120
std::string errorMsg
Definition: png_codec.cpp:121

◆ sk_alpha_type_to_str()

static constexpr const char * sk_alpha_type_to_str ( SkAlphaType  alphaType)
staticconstexpr

Definition at line 105 of file png_codec.cpp.

105 {
106 switch (alphaType) {
108 return "kUnknown_SkAlphaType";
110 return "kOpaque_SkAlphaType";
112 return "kPremul_SkAlphaType";
114 return "kUnpremul_SkAlphaType";
115 }
117}
@ kUnknown_SkAlphaType
uninitialized
Definition: SkAlphaType.h:27
#define SkUNREACHABLE
Definition: SkAssert.h:135

◆ sk_color_type_to_str()

static constexpr const char * sk_color_type_to_str ( SkColorType  colorType)
staticconstexpr

Definition at line 47 of file png_codec.cpp.

47 {
48 switch (colorType) {
50 return "kUnknown_SkColorType";
52 return "kAlpha_8_SkColorType";
54 return "kRGB_565_SkColorType";
56 return "kARGB_4444_SkColorType";
58 return "kRGBA_8888_SkColorType";
60 return "kRGB_888x_SkColorType";
62 return "kBGRA_8888_SkColorType";
64 return "kRGBA_1010102_SkColorType";
66 return "kBGRA_1010102_SkColorType";
68 return "kRGB_101010x_SkColorType";
70 return "kBGR_101010x_SkColorType";
72 return "kBGR_101010x_XR_SkColorType";
74 return "kGray_8_SkColorType";
76 return "kRGBA_F16Norm_SkColorType";
78 return "kRGBA_F16_SkColorType";
80 return "kRGBA_F32_SkColorType";
82 return "kR8G8_unorm_SkColorType";
84 return "kA16_float_SkColorType";
86 return "kR16G16_float_SkColorType";
88 return "kA16_unorm_SkColorType";
90 return "kR16G16_unorm_SkColorType";
92 return "kR16G16B16A16_unorm_SkColorType";
94 return "kSRGBA_8888_SkColorType";
96 return "kR8_unorm_SkColorType";
98 return "kRGBA_10x6_SkColorType";
100 return "kBGRA_10101010_XR_SkColorType";
101 }
103}
@ kR16G16B16A16_unorm_SkColorType
pixel with a little endian uint16_t for red, green, blue
Definition: SkColorType.h:50
@ kRGBA_10x6_SkColorType
pixel with 10 used bits (most significant) followed by 6 unused
Definition: SkColorType.h:33
@ kR8_unorm_SkColorType
Definition: SkColorType.h:54
@ kBGR_101010x_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word
Definition: SkColorType.h:30
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kR8G8_unorm_SkColorType
pixel with a uint8_t for red and green
Definition: SkColorType.h:43
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kA16_unorm_SkColorType
pixel with a little endian uint16_t for alpha
Definition: SkColorType.h:48
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:29
@ kSRGBA_8888_SkColorType
Definition: SkColorType.h:53
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition: SkColorType.h:32
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition: SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:25
@ kBGRA_1010102_SkColorType
10 bits for blue, green, red; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:28
@ kA16_float_SkColorType
pixel with a half float for alpha
Definition: SkColorType.h:45
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition: SkColorType.h:31
@ kR16G16_unorm_SkColorType
pixel with a little endian uint16_t for red and green
Definition: SkColorType.h:49
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkColorType.h:36
@ kUnknown_SkColorType
uninitialized
Definition: SkColorType.h:20
@ kR16G16_float_SkColorType
pixel with a half float for red and green
Definition: SkColorType.h:46
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)