本文整理汇总了C++中SkImageInfo::getSafeSize方法的典型用法代码示例。如果您正苦于以下问题:C++ SkImageInfo::getSafeSize方法的具体用法?C++ SkImageInfo::getSafeSize怎么用?C++ SkImageInfo::getSafeSize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SkImageInfo
的用法示例。
在下文中一共展示了SkImageInfo::getSafeSize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_dimensions
static void test_dimensions(skiatest::Reporter* r, const char path[]) {
// Create the codec from the resource file
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 create codec '%s'", path);
return;
}
// Check that the decode is successful for a variety of scales
for (float scale = -0.05f; scale < 2.0f; scale += 0.05f) {
// Scale the output dimensions
SkISize scaledDims = codec->getScaledDimensions(scale);
SkImageInfo scaledInfo = codec->getInfo().makeWH(scaledDims.width(), scaledDims.height());
// Set up for the decode
size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
SkAutoTMalloc<SkPMColor> pixels(totalBytes);
SkImageGenerator::Result result =
codec->getPixels(scaledInfo, pixels.get(), rowBytes, NULL, NULL, NULL);
REPORTER_ASSERT(r, SkImageGenerator::kSuccess == result);
}
}
示例2: tryGenerateBitmap
bool SkImageGenerator::tryGenerateBitmap(SkBitmap* bitmap, const SkImageInfo* infoPtr) {
const SkImageInfo info = infoPtr ? *infoPtr : this->getInfo();
const size_t rowBytes = info.minRowBytes();
const size_t pixelSize = info.getSafeSize(rowBytes);
if (0 == pixelSize) {
return false;
}
SkAutoFree pixelStorage(sk_malloc_flags(pixelSize, 0));
void* pixels = pixelStorage.get();
if (!pixels) {
return false;
}
SkPMColor ctStorage[256];
int ctCount = 0;
if (!this->getPixels(info, pixels, rowBytes, ctStorage, &ctCount)) {
return false;
}
SkAutoTUnref<SkColorTable> ctable;
if (ctCount > 0) {
SkASSERT(kIndex_8_SkColorType == info.colorType());
ctable.reset(new SkColorTable(ctStorage, ctCount));
} else {
SkASSERT(kIndex_8_SkColorType != info.colorType());
}
return bitmap->installPixels(info, pixelStorage.detach(), rowBytes, ctable,
release_malloc_proc, nullptr);
}
示例3: test_dimensions
static void test_dimensions(skiatest::Reporter* r, const char path[]) {
// Create the codec from the resource file
SkAutoTDelete<SkStream> stream(resource(path));
if (!stream) {
SkDebugf("Missing resource '%s'\n", path);
return;
}
SkAutoTDelete<SkAndroidCodec> codec(SkAndroidCodec::NewFromStream(stream.detach()));
if (!codec) {
ERRORF(r, "Unable to create codec '%s'", path);
return;
}
// Check that the decode is successful for a variety of scales
for (int sampleSize = 1; sampleSize < 32; sampleSize++) {
// Scale the output dimensions
SkISize scaledDims = codec->getSampledDimensions(sampleSize);
SkImageInfo scaledInfo = codec->getInfo()
.makeWH(scaledDims.width(), scaledDims.height())
.makeColorType(kN32_SkColorType);
// Set up for the decode
size_t rowBytes = scaledDims.width() * sizeof(SkPMColor);
size_t totalBytes = scaledInfo.getSafeSize(rowBytes);
SkAutoTMalloc<SkPMColor> pixels(totalBytes);
SkAndroidCodec::AndroidOptions options;
options.fSampleSize = sampleSize;
SkCodec::Result result =
codec->getAndroidPixels(scaledInfo, pixels.get(), rowBytes, &options);
REPORTER_ASSERT(r, SkCodec::kSuccess == result);
}
}
示例4: storage
TEST_F(DeferredImageDecoderTest, frameOpacity)
{
std::unique_ptr<ImageDecoder> actualDecoder = ImageDecoder::create(*m_data,
ImageDecoder::AlphaPremultiplied, ImageDecoder::GammaAndColorProfileApplied);
std::unique_ptr<DeferredImageDecoder> decoder = DeferredImageDecoder::createForTesting(std::move(actualDecoder));
decoder->setData(*m_data, true);
SkImageInfo pixInfo = SkImageInfo::MakeN32Premul(1, 1);
size_t rowBytes = pixInfo.minRowBytes();
size_t size = pixInfo.getSafeSize(rowBytes);
SkAutoMalloc storage(size);
SkPixmap pixmap(pixInfo, storage.get(), rowBytes);
// Before decoding, the frame is not known to be opaque.
RefPtr<SkImage> frame = decoder->createFrameAtIndex(0);
ASSERT_TRUE(frame);
EXPECT_FALSE(frame->isOpaque());
// Force a lazy decode by reading pixels.
EXPECT_TRUE(frame->readPixels(pixmap, 0, 0));
// After decoding, the frame is known to be opaque.
frame = decoder->createFrameAtIndex(0);
ASSERT_TRUE(frame);
EXPECT_TRUE(frame->isOpaque());
// Re-generating the opaque-marked frame should not fail.
EXPECT_TRUE(frame->readPixels(pixmap, 0, 0));
}
示例5: AllocSize
size_t SkAutoPixmapStorage::AllocSize(const SkImageInfo& info, size_t* rowBytes) {
size_t rb = info.minRowBytes();
if (rowBytes) {
*rowBytes = rb;
}
return info.getSafeSize(rb);
}
示例6: check_fill
static void check_fill(skiatest::Reporter* r,
const SkImageInfo& imageInfo,
uint32_t startRow,
uint32_t endRow,
size_t rowBytes,
uint32_t offset,
uint32_t colorOrIndex) {
// Calculate the total size of the image in bytes. Use the smallest possible size.
// The offset value tells us to adjust the pointer from the memory we allocate in order
// to test on different memory alignments. If offset is nonzero, we need to increase the
// size of the memory we allocate in order to make sure that we have enough. We are
// still allocating the smallest possible size.
const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
// Create fake image data where every byte has a value of 0
SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]);
memset(storage.get(), 0, totalBytes);
// Adjust the pointer in order to test on different memory alignments
uint8_t* imageData = storage.get() + offset;
uint8_t* imageStart = imageData + rowBytes * startRow;
const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1);
SkSampler::Fill(fillInfo, imageStart, rowBytes, colorOrIndex, SkCodec::kNo_ZeroInitialized);
// Ensure that the pixels are filled properly
// The bots should catch any memory corruption
uint8_t* indexPtr = imageData + startRow * rowBytes;
uint8_t* grayPtr = indexPtr;
uint32_t* colorPtr = (uint32_t*) indexPtr;
uint16_t* color565Ptr = (uint16_t*) indexPtr;
for (uint32_t y = startRow; y <= endRow; y++) {
for (int32_t x = 0; x < imageInfo.width(); x++) {
switch (imageInfo.colorType()) {
case kIndex_8_SkColorType:
REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
break;
case kN32_SkColorType:
REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
break;
case kGray_8_SkColorType:
REPORTER_ASSERT(r, kFillGray == grayPtr[x]);
break;
case kRGB_565_SkColorType:
REPORTER_ASSERT(r, kFill565 == color565Ptr[x]);
break;
default:
REPORTER_ASSERT(r, false);
break;
}
}
indexPtr += rowBytes;
colorPtr = (uint32_t*) indexPtr;
}
}
示例7: Pattern
ImagePattern::ImagePattern(PassRefPtr<Image> image, RepeatMode repeatMode)
: Pattern(repeatMode)
, m_tileImage(image->imageForCurrentFrame())
{
if (m_tileImage) {
// TODO(fmalita): mechanism to extract the actual SkImageInfo from an SkImage?
const SkImageInfo info =
SkImageInfo::MakeN32Premul(m_tileImage->width(), m_tileImage->height());
adjustExternalMemoryAllocated(info.getSafeSize(info.minRowBytes()));
}
}
示例8:
DEF_TEST(ImageDataRef, reporter) {
SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
size_t rowBytes = info.minRowBytes();
size_t size = info.getSafeSize(rowBytes);
SkData* data = SkData::NewUninitialized(size);
REPORTER_ASSERT(reporter, data->unique());
SkImage* image = SkImage::NewRasterData(info, data, rowBytes);
REPORTER_ASSERT(reporter, !data->unique());
image->unref();
REPORTER_ASSERT(reporter, data->unique());
data->unref();
}
示例9: sk_ref_sp
sk_sp<SkImage> SkImage::makeNonTextureImage() const {
if (!this->isTextureBacked()) {
return sk_ref_sp(const_cast<SkImage*>(this));
}
SkImageInfo info = as_IB(this)->onImageInfo();
size_t rowBytes = info.minRowBytes();
size_t size = info.getSafeSize(rowBytes);
auto data = SkData::MakeUninitialized(size);
if (!data) {
return nullptr;
}
SkPixmap pm(info, data->writable_data(), rowBytes);
if (!this->readPixels(pm, 0, 0, kDisallow_CachingHint)) {
return nullptr;
}
return MakeRasterData(info, data, rowBytes);
}
示例10: SkASSERT
sk_sp<SkPixelRef> SkMallocPixelRef::MakeWithData(const SkImageInfo& info,
size_t rowBytes,
sk_sp<SkColorTable> ctable,
sk_sp<SkData> data) {
SkASSERT(data != nullptr);
if (!is_valid(info, ctable.get())) {
return nullptr;
}
if ((rowBytes < info.minRowBytes()) || (data->size() < info.getSafeSize(rowBytes))) {
return nullptr;
}
// must get this address before we call release
void* pixels = const_cast<void*>(data->data());
SkPixelRef* pr = new SkMallocPixelRef(info, pixels, rowBytes, std::move(ctable),
sk_data_releaseproc, data.release());
pr->setImmutable(); // since we were created with (immutable) data
return sk_sp<SkPixelRef>(pr);
}
示例11: test_newraster
static void test_newraster(skiatest::Reporter* reporter) {
SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
const size_t minRowBytes = info.minRowBytes();
const size_t size = info.getSafeSize(minRowBytes);
SkAutoMalloc storage(size);
SkPMColor* baseAddr = static_cast<SkPMColor*>(storage.get());
sk_bzero(baseAddr, size);
SkCanvas* canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
REPORTER_ASSERT(reporter, canvas);
SkImageInfo info2;
size_t rowBytes;
const SkPMColor* addr = (const SkPMColor*)canvas->peekPixels(&info2, &rowBytes);
REPORTER_ASSERT(reporter, addr);
REPORTER_ASSERT(reporter, info == info2);
REPORTER_ASSERT(reporter, minRowBytes == rowBytes);
for (int y = 0; y < info.height(); ++y) {
for (int x = 0; x < info.width(); ++x) {
REPORTER_ASSERT(reporter, 0 == addr[x]);
}
addr = (const SkPMColor*)((const char*)addr + rowBytes);
}
SkDELETE(canvas);
// now try a deliberately bad info
info = info.makeWH(-1, info.height());
REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
// too big
info = info.makeWH(1 << 30, 1 << 30);
REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
// not a valid pixel type
info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
REPORTER_ASSERT(reporter, NULL == SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes));
// We should succeed with a zero-sized valid info
info = SkImageInfo::MakeN32Premul(0, 0);
canvas = SkCanvas::NewRasterDirect(info, baseAddr, minRowBytes);
REPORTER_ASSERT(reporter, canvas);
SkDELETE(canvas);
}
示例12: check_fill
static void check_fill(skiatest::Reporter* r,
const SkImageInfo& imageInfo,
uint32_t startRow,
uint32_t endRow,
size_t rowBytes,
uint32_t offset,
uint32_t colorOrIndex,
SkPMColor* colorTable) {
// Calculate the total size of the image in bytes. Use the smallest possible size.
// The offset value tells us to adjust the pointer from the memory we allocate in order
// to test on different memory alignments. If offset is nonzero, we need to increase the
// size of the memory we allocate in order to make sure that we have enough. We are
// still allocating the smallest possible size.
const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset;
// Create fake image data where every byte has a value of 0
SkAutoTDeleteArray<uint8_t> storage(SkNEW_ARRAY(uint8_t, totalBytes));
memset(storage.get(), 0, totalBytes);
// Adjust the pointer in order to test on different memory alignments
uint8_t* imageData = storage.get() + offset;
uint8_t* imageStart = imageData + rowBytes * startRow;
// Fill image with the fill value starting at the indicated row
SkSwizzler::Fill(imageStart, imageInfo, rowBytes, endRow - startRow + 1, colorOrIndex,
colorTable);
// Ensure that the pixels are filled properly
// The bots should catch any memory corruption
uint8_t* indexPtr = imageData + startRow * rowBytes;
uint32_t* colorPtr = (uint32_t*) indexPtr;
for (uint32_t y = startRow; y <= endRow; y++) {
for (int32_t x = 0; x < imageInfo.width(); x++) {
if (kIndex_8_SkColorType == imageInfo.colorType()) {
REPORTER_ASSERT(r, kFillIndex == indexPtr[x]);
} else {
REPORTER_ASSERT(r, kFillColor == colorPtr[x]);
}
}
indexPtr += rowBytes;
colorPtr = (uint32_t*) indexPtr;
}
}
示例13: test_newraster
static void test_newraster(skiatest::Reporter* reporter) {
SkImageInfo info = SkImageInfo::MakeN32Premul(10, 10);
const size_t minRowBytes = info.minRowBytes();
const size_t size = info.getSafeSize(minRowBytes);
SkAutoTMalloc<SkPMColor> storage(size);
SkPMColor* baseAddr = storage.get();
sk_bzero(baseAddr, size);
std::unique_ptr<SkCanvas> canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
REPORTER_ASSERT(reporter, canvas);
SkPixmap pmap;
const SkPMColor* addr = canvas->peekPixels(&pmap) ? pmap.addr32() : nullptr;
REPORTER_ASSERT(reporter, addr);
REPORTER_ASSERT(reporter, info == pmap.info());
REPORTER_ASSERT(reporter, minRowBytes == pmap.rowBytes());
for (int y = 0; y < info.height(); ++y) {
for (int x = 0; x < info.width(); ++x) {
REPORTER_ASSERT(reporter, 0 == addr[x]);
}
addr = (const SkPMColor*)((const char*)addr + pmap.rowBytes());
}
// now try a deliberately bad info
info = info.makeWH(-1, info.height());
REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
// too big
info = info.makeWH(1 << 30, 1 << 30);
REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
// not a valid pixel type
info = SkImageInfo::Make(10, 10, kUnknown_SkColorType, info.alphaType());
REPORTER_ASSERT(reporter, nullptr == SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes));
// We should succeed with a zero-sized valid info
info = SkImageInfo::MakeN32Premul(0, 0);
canvas = SkCanvas::MakeRasterDirect(info, baseAddr, minRowBytes);
REPORTER_ASSERT(reporter, canvas);
}
示例14: tryGenerateBitmap
bool SkImageGenerator::tryGenerateBitmap(SkBitmap* bitmap, const SkImageInfo* infoPtr,
SkBitmap::Allocator* allocator) {
SkImageInfo info = infoPtr ? *infoPtr : this->getInfo();
if (0 == info.getSafeSize(info.minRowBytes())) {
return false;
}
if (!bitmap->setInfo(info)) {
return reset_and_return_false(bitmap);
}
SkPMColor ctStorage[256];
memset(ctStorage, 0xFF, sizeof(ctStorage)); // init with opaque-white for the moment
SkAutoTUnref<SkColorTable> ctable(new SkColorTable(ctStorage, 256));
if (!bitmap->tryAllocPixels(allocator, ctable)) {
// SkResourceCache's custom allcator can'thandle ctables, so it may fail on
// kIndex_8_SkColorTable.
// skbug.com/4355
#if 1
// ignroe the allocator, and see if we can succeed without it
if (!bitmap->tryAllocPixels(nullptr, ctable)) {
return reset_and_return_false(bitmap);
}
#else
// this is the up-scale technique, not fully debugged, but we keep it here at the moment
// to remind ourselves that this might be better than ignoring the allocator.
info = SkImageInfo::MakeN32(info.width(), info.height(), info.alphaType());
if (!bitmap->setInfo(info)) {
return reset_and_return_false(bitmap);
}
// we pass nullptr for the ctable arg, since we are now explicitly N32
if (!bitmap->tryAllocPixels(allocator, nullptr)) {
return reset_and_return_false(bitmap);
}
#endif
}
bitmap->lockPixels();
if (!bitmap->getPixels()) {
return reset_and_return_false(bitmap);
}
int ctCount = 0;
if (!this->getPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(),
ctStorage, &ctCount)) {
return reset_and_return_false(bitmap);
}
if (ctCount > 0) {
SkASSERT(kIndex_8_SkColorType == bitmap->colorType());
// we and bitmap should be owners
SkASSERT(!ctable->unique());
// Now we need to overwrite the ctable we built earlier, with the correct colors.
// This does mean that we may have made the table too big, but that cannot be avoided
// until we can change SkImageGenerator's API to return us the ctable *before* we have to
// allocate space for all the pixels.
ctable->dangerous_overwriteColors(ctStorage, ctCount);
} else {
SkASSERT(kIndex_8_SkColorType != bitmap->colorType());
// we should be the only owner
SkASSERT(ctable->unique());
}
return true;
}
示例15: NewFromStream
//.........这里部分代码省略.........
directoryEntries[i].size = size;
}
// Default Result, if no valid embedded codecs are found.
*result = kInvalidInput;
// 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, &directoryEntries[numImages - 1], lessThan);
// Now will construct a candidate codec for each of the embedded images
uint32_t bytesRead = kIcoDirectoryBytes + numImages * kIcoDirEntryBytes;
std::unique_ptr<SkTArray<std::unique_ptr<SkCodec>, true>> codecs(
new (SkTArray<std::unique_ptr<SkCodec>, true>)(numImages));
for (uint32_t i = 0; i < numImages; i++) {
uint32_t offset = directoryEntries[i].offset;
uint32_t size = directoryEntries[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
SkAutoFree buffer(sk_malloc_flags(size, 0));
if (!buffer) {
SkCodecPrintf("Warning: OOM trying to create embedded stream.\n");
break;
}
if (inputStream->read(buffer.get(), size) != size) {
SkCodecPrintf("Warning: could not create embedded stream.\n");
*result = kIncompleteInput;
break;
}
sk_sp<SkData> data(SkData::MakeFromMalloc(buffer.release(), size));
std::unique_ptr<SkMemoryStream> embeddedStream(new SkMemoryStream(data));
bytesRead += size;
// Check if the embedded codec is bmp or png and create the codec
SkCodec* codec = nullptr;
Result dummyResult;
if (SkPngCodec::IsPng((const char*) data->bytes(), data->size())) {
codec = SkPngCodec::NewFromStream(embeddedStream.release(), &dummyResult);
} else {
codec = SkBmpCodec::NewFromIco(embeddedStream.release(), &dummyResult);
}
// 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
size_t maxSize = 0;
int maxIndex = 0;
for (int i = 0; i < codecs->count(); i++) {
SkImageInfo info = codecs->operator[](i)->getInfo();
size_t size = info.getSafeSize(info.minRowBytes());
if (size > maxSize) {
maxSize = size;
maxIndex = i;
}
}
int width = codecs->operator[](maxIndex)->getInfo().width();
int height = codecs->operator[](maxIndex)->getInfo().height();
SkEncodedInfo info = codecs->operator[](maxIndex)->getEncodedInfo();
SkColorSpace* colorSpace = codecs->operator[](maxIndex)->getInfo().colorSpace();
*result = kSuccess;
// The original stream is no longer needed, because the embedded codecs own their
// own streams.
return new SkIcoCodec(width, height, info, codecs.release(), sk_ref_sp(colorSpace));
}