本文整理汇总了C++中SkImageInfo::makeAlphaType方法的典型用法代码示例。如果您正苦于以下问题:C++ SkImageInfo::makeAlphaType方法的具体用法?C++ SkImageInfo::makeAlphaType怎么用?C++ SkImageInfo::makeAlphaType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SkImageInfo
的用法示例。
在下文中一共展示了SkImageInfo::makeAlphaType方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_codec
static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
const SkMD5::Digest* goodDigest) {
REPORTER_ASSERT(r, info.dimensions() == size);
bm.allocPixels(info);
SkAutoLockPixels autoLockPixels(bm);
SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
REPORTER_ASSERT(r, result == expectedResult);
md5(bm, digest);
if (goodDigest) {
REPORTER_ASSERT(r, *digest == *goodDigest);
}
{
// Test decoding to 565
SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
SkCodec::Result expected565 = info.alphaType() == kOpaque_SkAlphaType ?
expectedResult : SkCodec::kInvalidConversion;
test_info(r, codec, info565, expected565, nullptr);
}
// Verify that re-decoding gives the same result. It is interesting to check this after
// a decode to 565, since choosing to decode to 565 may result in some of the decode
// options being modified. These options should return to their defaults on another
// decode to kN32, so the new digest should match the old digest.
test_info(r, codec, info, expectedResult, digest);
{
// Check alpha type conversions
if (info.alphaType() == kOpaque_SkAlphaType) {
test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
expectedResult, digest);
test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
expectedResult, digest);
} else {
// Decoding to opaque should fail
test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
SkCodec::kInvalidConversion, nullptr);
SkAlphaType otherAt = info.alphaType();
if (kPremul_SkAlphaType == otherAt) {
otherAt = kUnpremul_SkAlphaType;
} else {
otherAt = kPremul_SkAlphaType;
}
// The other non-opaque alpha type should always succeed, but not match.
test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
}
}
}
示例2: draw_image
static void draw_image(SkCanvas* canvas, SkImage* image, SkColorType dstColorType,
SkAlphaType dstAlphaType, sk_sp<SkColorSpace> dstColorSpace,
SkImage::CachingHint hint) {
size_t rowBytes = image->width() * SkColorTypeBytesPerPixel(dstColorType);
sk_sp<SkData> data = SkData::MakeUninitialized(rowBytes * image->height());
dstColorSpace = fix_for_colortype(dstColorSpace.get(), dstColorType);
SkImageInfo dstInfo = SkImageInfo::Make(image->width(), image->height(), dstColorType,
dstAlphaType, dstColorSpace);
if (!image->readPixels(dstInfo, data->writable_data(), rowBytes, 0, 0, hint)) {
memset(data->writable_data(), 0, rowBytes * image->height());
}
// SkImage must be premul, so manually premul the data if we unpremul'd during readPixels
if (kUnpremul_SkAlphaType == dstAlphaType) {
auto xform = SkColorSpaceXform::New(dstColorSpace.get(), dstColorSpace.get());
if (!xform->apply(select_xform_format(dstColorType), data->writable_data(),
select_xform_format(dstColorType), data->data(),
image->width() * image->height(), kPremul_SkAlphaType)) {
memset(data->writable_data(), 0, rowBytes * image->height());
}
dstInfo = dstInfo.makeAlphaType(kPremul_SkAlphaType);
}
// readPixels() does not always clamp F16. The drawing code expects pixels in the 0-1 range.
clamp_if_necessary(dstInfo, data->writable_data());
// Now that we have called readPixels(), dump the raw pixels into an srgb image.
sk_sp<SkColorSpace> srgb = fix_for_colortype(
SkColorSpace::MakeSRGB().get(), dstColorType);
sk_sp<SkImage> raw = SkImage::MakeRasterData(dstInfo.makeColorSpace(srgb), data, rowBytes);
canvas->drawImage(raw.get(), 0.0f, 0.0f, nullptr);
}
示例3: make_premul
static SkImageInfo make_premul(const SkImageInfo& info) {
if (kUnpremul_SkAlphaType == info.alphaType()) {
return info.makeAlphaType(kPremul_SkAlphaType);
}
return info;
}
示例4: initializeXforms
bool SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options,
SkPMColor ctable[], int* ctableCount) {
if (setjmp(png_jmpbuf(fPng_ptr))) {
SkCodecPrintf("Failed on png_read_update_info.\n");
return false;
}
png_read_update_info(fPng_ptr, fInfo_ptr);
// It's important to reset fColorXform to nullptr. We don't do this on rewinding
// because the interlaced scanline decoder may need to rewind.
fColorXform = nullptr;
SkImageInfo swizzlerInfo = dstInfo;
bool needsColorXform = needs_color_xform(dstInfo, this->getInfo());
if (needsColorXform) {
switch (dstInfo.colorType()) {
case kRGBA_8888_SkColorType:
case kBGRA_8888_SkColorType:
case kRGBA_F16_SkColorType:
swizzlerInfo = swizzlerInfo.makeColorType(kRGBA_8888_SkColorType);
if (kPremul_SkAlphaType == dstInfo.alphaType()) {
swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
}
break;
case kIndex_8_SkColorType:
break;
default:
return false;
}
fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace()),
sk_ref_sp(dstInfo.colorSpace()));
if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) {
return false;
}
}
if (SkEncodedInfo::kPalette_Color == this->getEncodedInfo().color()) {
if (!this->createColorTable(dstInfo, ctableCount)) {
return false;
}
}
// Copy the color table to the client if they request kIndex8 mode
copy_color_table(swizzlerInfo, fColorTable, ctable, ctableCount);
// Create the swizzler. SkPngCodec retains ownership of the color table.
const SkPMColor* colors = get_color_ptr(fColorTable.get());
fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colors, swizzlerInfo,
options));
SkASSERT(fSwizzler);
return true;
}
示例5: compatibleInfo
static bool compatibleInfo(const SkImageInfo& src, const SkImageInfo& dst)
{
if (src == dst)
return true;
// It is legal to write kOpaque_SkAlphaType pixels into a kPremul_SkAlphaType buffer.
// This can happen when DeferredImageDecoder allocates an kOpaque_SkAlphaType image
// generator based on cached frame info, while the ImageFrame-allocated dest bitmap
// stays kPremul_SkAlphaType.
if (src.alphaType() == kOpaque_SkAlphaType && dst.alphaType() == kPremul_SkAlphaType) {
const SkImageInfo& tmp = src.makeAlphaType(kPremul_SkAlphaType);
return tmp == dst;
}
return false;
}
示例6: fix_embedded_alpha
static SkImageInfo fix_embedded_alpha(const SkImageInfo& dstInfo, SkAlphaType embeddedAlpha) {
// FIXME (msarett): ICO is considered non-opaque, even if the embedded BMP
// incorrectly claims it has no alpha.
switch (embeddedAlpha) {
case kPremul_SkAlphaType:
case kUnpremul_SkAlphaType:
// Use the requested alpha type if the embedded codec supports alpha.
embeddedAlpha = dstInfo.alphaType();
break;
case kOpaque_SkAlphaType:
// If the embedded codec claims it is opaque, decode as if it is opaque.
break;
default:
SkASSERT(false);
break;
}
return dstInfo.makeAlphaType(embeddedAlpha);
}
示例7: onPrepareToDecode
SkCodec::Result SkBmpMaskCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
const SkCodec::Options& options, SkPMColor inputColorPtr[], int* inputColorCount) {
if (this->colorXform()) {
this->resetXformBuffer(dstInfo.width());
}
SkImageInfo swizzlerInfo = dstInfo;
if (this->colorXform()) {
swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
if (kPremul_SkAlphaType == dstInfo.alphaType()) {
swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
}
}
// Initialize the mask swizzler
fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(swizzlerInfo, this->getInfo(),
fMasks.get(), this->bitsPerPixel(), options));
SkASSERT(fMaskSwizzler);
return SkCodec::kSuccess;
}
示例8: onPrepareToDecode
SkCodec::Result SkBmpMaskCodec::onPrepareToDecode(const SkImageInfo& dstInfo,
const SkCodec::Options& options) {
if (this->colorXform()) {
this->resetXformBuffer(dstInfo.width());
}
SkImageInfo swizzlerInfo = dstInfo;
if (this->colorXform()) {
swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
if (kPremul_SkAlphaType == dstInfo.alphaType()) {
swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
}
}
bool srcIsOpaque = this->getEncodedInfo().opaque();
fMaskSwizzler.reset(SkMaskSwizzler::CreateMaskSwizzler(swizzlerInfo, srcIsOpaque,
fMasks.get(), this->bitsPerPixel(), options));
SkASSERT(fMaskSwizzler);
return SkCodec::kSuccess;
}
示例9: test_codec
static void test_codec(skiatest::Reporter* r, Codec* codec, SkBitmap& bm, const SkImageInfo& info,
const SkISize& size, SkCodec::Result expectedResult, SkMD5::Digest* digest,
const SkMD5::Digest* goodDigest) {
REPORTER_ASSERT(r, info.dimensions() == size);
bm.allocPixels(info);
SkAutoLockPixels autoLockPixels(bm);
SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
REPORTER_ASSERT(r, result == expectedResult);
md5(bm, digest);
if (goodDigest) {
REPORTER_ASSERT(r, *digest == *goodDigest);
}
{
// Test decoding to 565
SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
if (info.alphaType() == kOpaque_SkAlphaType) {
// Decoding to 565 should succeed.
SkBitmap bm565;
bm565.allocPixels(info565);
SkAutoLockPixels alp(bm565);
// This will allow comparison even if the image is incomplete.
bm565.eraseColor(SK_ColorBLACK);
REPORTER_ASSERT(r, expectedResult == codec->getPixels(info565,
bm565.getPixels(), bm565.rowBytes()));
SkMD5::Digest digest565;
md5(bm565, &digest565);
// A dumb client's request for non-opaque should also succeed.
for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
info565 = info565.makeAlphaType(alpha);
test_info(r, codec, info565, expectedResult, &digest565);
}
} else {
test_info(r, codec, info565, SkCodec::kInvalidConversion, nullptr);
}
}
if (codec->getInfo().colorType() == kGray_8_SkColorType) {
SkImageInfo grayInfo = codec->getInfo();
SkBitmap grayBm;
grayBm.allocPixels(grayInfo);
SkAutoLockPixels alp(grayBm);
grayBm.eraseColor(SK_ColorBLACK);
REPORTER_ASSERT(r, expectedResult == codec->getPixels(grayInfo,
grayBm.getPixels(), grayBm.rowBytes()));
SkMD5::Digest grayDigest;
md5(grayBm, &grayDigest);
for (auto alpha : { kPremul_SkAlphaType, kUnpremul_SkAlphaType }) {
grayInfo = grayInfo.makeAlphaType(alpha);
test_info(r, codec, grayInfo, expectedResult, &grayDigest);
}
}
// Verify that re-decoding gives the same result. It is interesting to check this after
// a decode to 565, since choosing to decode to 565 may result in some of the decode
// options being modified. These options should return to their defaults on another
// decode to kN32, so the new digest should match the old digest.
test_info(r, codec, info, expectedResult, digest);
{
// Check alpha type conversions
if (info.alphaType() == kOpaque_SkAlphaType) {
test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
expectedResult, digest);
test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
expectedResult, digest);
} else {
// Decoding to opaque should fail
test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
SkCodec::kInvalidConversion, nullptr);
SkAlphaType otherAt = info.alphaType();
if (kPremul_SkAlphaType == otherAt) {
otherAt = kUnpremul_SkAlphaType;
} else {
otherAt = kPremul_SkAlphaType;
}
// The other non-opaque alpha type should always succeed, but not match.
test_info(r, codec, info.makeAlphaType(otherAt), expectedResult, nullptr);
}
}
}
示例10: validate_info
static SkImageInfo validate_info(const SkImageInfo& info) {
SkAlphaType newAlphaType = info.alphaType();
SkAssertResult(SkColorTypeValidateAlphaType(info.colorType(), info.alphaType(), &newAlphaType));
return info.makeAlphaType(newAlphaType);
}
示例11: check
static void check(skiatest::Reporter* r,
const char path[],
SkISize size,
bool supportsScanlineDecoding,
bool supportsSubsetDecoding,
bool supports565 = true) {
SkAutoTDelete<SkStream> stream(resource(path));
if (!stream) {
SkDebugf("Missing resource '%s'\n", path);
return;
}
SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach()));
if (!codec) {
ERRORF(r, "Unable to decode '%s'", path);
return;
}
// This test is used primarily to verify rewinding works properly. Using kN32 allows
// us to test this without the added overhead of creating different bitmaps depending
// on the color type (ex: building a color table for kIndex8). DM is where we test
// decodes to all possible destination color types.
SkImageInfo info = codec->getInfo().makeColorType(kN32_SkColorType);
REPORTER_ASSERT(r, info.dimensions() == size);
{
// Test decoding to 565
SkImageInfo info565 = info.makeColorType(kRGB_565_SkColorType);
SkCodec::Result expected = (supports565 && info.alphaType() == kOpaque_SkAlphaType) ?
SkCodec::kSuccess : SkCodec::kInvalidConversion;
test_info(r, codec, info565, expected, NULL);
}
SkBitmap bm;
bm.allocPixels(info);
SkAutoLockPixels autoLockPixels(bm);
SkCodec::Result result =
codec->getPixels(info, bm.getPixels(), bm.rowBytes(), NULL, NULL, NULL);
REPORTER_ASSERT(r, result == SkCodec::kSuccess);
SkMD5::Digest digest;
md5(bm, &digest);
// verify that re-decoding gives the same result.
test_info(r, codec, info, SkCodec::kSuccess, &digest);
{
// Check alpha type conversions
if (info.alphaType() == kOpaque_SkAlphaType) {
test_info(r, codec, info.makeAlphaType(kUnpremul_SkAlphaType),
SkCodec::kInvalidConversion, NULL);
test_info(r, codec, info.makeAlphaType(kPremul_SkAlphaType),
SkCodec::kInvalidConversion, NULL);
} else {
// Decoding to opaque should fail
test_info(r, codec, info.makeAlphaType(kOpaque_SkAlphaType),
SkCodec::kInvalidConversion, NULL);
SkAlphaType otherAt = info.alphaType();
if (kPremul_SkAlphaType == otherAt) {
otherAt = kUnpremul_SkAlphaType;
} else {
otherAt = kPremul_SkAlphaType;
}
// The other non-opaque alpha type should always succeed, but not match.
test_info(r, codec, info.makeAlphaType(otherAt), SkCodec::kSuccess, NULL);
}
}
// Scanline decoding follows.
stream.reset(resource(path));
SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(
SkScanlineDecoder::NewFromStream(stream.detach()));
if (supportsScanlineDecoding) {
bm.eraseColor(SK_ColorYELLOW);
REPORTER_ASSERT(r, scanlineDecoder);
REPORTER_ASSERT(r, scanlineDecoder->start(info) == SkCodec::kSuccess);
for (int y = 0; y < info.height(); y++) {
result = scanlineDecoder->getScanlines(bm.getAddr(0, y), 1, 0);
REPORTER_ASSERT(r, result == SkCodec::kSuccess);
}
// verify that scanline decoding gives the same result.
compare_to_good_digest(r, digest, bm);
} else {
REPORTER_ASSERT(r, !scanlineDecoder);
}
// The rest of this function tests decoding subsets, and will decode an arbitrary number of
// random subsets.
// Do not attempt to decode subsets of an image of only once pixel, since there is no
// meaningful subset.
if (size.width() * size.height() == 1) {
return;
}
SkRandom rand;
SkIRect subset;
SkCodec::Options opts;
opts.fSubset = ⊂
//.........这里部分代码省略.........
示例12: NewFromStream
//.........这里部分代码省略.........
uint32_t offset = get_int(entryBuffer.get(), 12 + i*kIcoDirEntryBytes);
// Save the vital fields
directoryEntries.get()[i].offset = offset;
directoryEntries.get()[i].size = size;
}
// It is "customary" that the embedded images will be stored in order of
// increasing offset. However, the specification does not indicate that
// they must be stored in this order, so we will not trust that this is the
// case. Here we sort the embedded images by increasing offset.
struct EntryLessThan {
bool operator() (Entry a, Entry b) const {
return a.offset < b.offset;
}
};
EntryLessThan lessThan;
SkTQSort(directoryEntries.get(), directoryEntries.get() + numImages - 1,
lessThan);
// Now will construct a candidate codec for each of the embedded images
uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes;
SkAutoTDelete<SkTArray<SkAutoTDelete<SkCodec>, true>> codecs(
new (SkTArray<SkAutoTDelete<SkCodec>, true>)(numImages));
for (uint32_t i = 0; i < numImages; i++) {
uint32_t offset = directoryEntries.get()[i].offset;
uint32_t size = directoryEntries.get()[i].size;
// Ensure that the offset is valid
if (offset < bytesRead) {
SkCodecPrintf("Warning: invalid ico offset.\n");
continue;
}
// If we cannot skip, assume we have reached the end of the stream and
// stop trying to make codecs
if (inputStream.get()->skip(offset - bytesRead) != offset - bytesRead) {
SkCodecPrintf("Warning: could not skip to ico offset.\n");
break;
}
bytesRead = offset;
// Create a new stream for the embedded codec
SkAutoTUnref<SkData> data(
SkData::NewFromStream(inputStream.get(), size));
if (nullptr == data.get()) {
SkCodecPrintf("Warning: could not create embedded stream.\n");
break;
}
SkAutoTDelete<SkMemoryStream> embeddedStream(new SkMemoryStream(data.get()));
bytesRead += size;
// Check if the embedded codec is bmp or png and create the codec
SkCodec* codec = nullptr;
if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
codec = SkPngCodec::NewFromStream(embeddedStream.detach());
} else {
codec = SkBmpCodec::NewFromIco(embeddedStream.detach());
}
// Save a valid codec
if (nullptr != codec) {
codecs->push_back().reset(codec);
}
}
// Recognize if there are no valid codecs
if (0 == codecs->count()) {
SkCodecPrintf("Error: could not find any valid embedded ico codecs.\n");
return nullptr;
}
// Use the largest codec as a "suggestion" for image info
uint32_t maxSize = 0;
uint32_t maxIndex = 0;
for (int32_t i = 0; i < codecs->count(); i++) {
SkImageInfo info = codecs->operator[](i)->getInfo();
uint32_t size = info.width() * info.height();
if (size > maxSize) {
maxSize = size;
maxIndex = i;
}
}
SkImageInfo info = codecs->operator[](maxIndex)->getInfo();
// ICOs contain an alpha mask after the image which means we cannot
// guarantee that an image is opaque, even if the sub-codec thinks it
// is.
// FIXME (msarett): The BMP decoder depends on the alpha type in order
// to decode correctly, otherwise it could report kUnpremul and we would
// not have to correct it here. Is there a better way?
// FIXME (msarett): This is only true for BMP in ICO - could a PNG in ICO
// be opaque? Is it okay that we missed out on the opportunity to mark
// such an image as opaque?
info = info.makeAlphaType(kUnpremul_SkAlphaType);
// Note that stream is owned by the embedded codec, the ico does not need
// direct access to the stream.
return new SkIcoCodec(info, codecs.detach());
}