get image from skcodec
Part of the larger work to remove drawImage from canvas (and in general simplify our apis w.r.t. older patterns like skbitmap). Change-Id: If208927e1d46256519036c42e68aec3d3c809a82 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/358836 Reviewed-by: Leon Scroggins <scroggo@google.com> Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
parent
f9652245b4
commit
844beb5eef
@ -1041,23 +1041,18 @@ Result ColorCodecSrc::draw(GrDirectContext*, SkCanvas* canvas) const {
|
||||
info = canvasInfo.makeDimensions(info.dimensions());
|
||||
}
|
||||
|
||||
SkBitmap bitmap;
|
||||
if (!bitmap.tryAllocPixels(info)) {
|
||||
return Result::Fatal("Image(%s) is too large (%d x %d)",
|
||||
fPath.c_str(), info.width(), info.height());
|
||||
}
|
||||
|
||||
switch (auto r = codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes())) {
|
||||
auto [image, result] = codec->getImage(info);
|
||||
switch (result) {
|
||||
case SkCodec::kSuccess:
|
||||
case SkCodec::kErrorInInput:
|
||||
case SkCodec::kIncompleteInput:
|
||||
canvas->drawBitmap(bitmap, 0,0);
|
||||
canvas->drawImage(image, 0,0);
|
||||
return Result::Ok();
|
||||
case SkCodec::kInvalidConversion:
|
||||
// TODO(mtklein): why are there formats we can't decode to?
|
||||
return Result::Skip("SkCodec can't decode to this format.");
|
||||
default:
|
||||
return Result::Fatal("Couldn't getPixels %s. Error code %d", fPath.c_str(), r);
|
||||
return Result::Fatal("Couldn't getPixels %s. Error code %d", fPath.c_str(), result);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,9 +48,7 @@ protected:
|
||||
|
||||
// Create matching bitmap.
|
||||
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path)));
|
||||
SkBitmap bitmap;
|
||||
bitmap.allocPixels(codec->getInfo());
|
||||
codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
|
||||
auto [codecImage, _] = codec->getImage();
|
||||
|
||||
// The GM will be displayed in a 2x2 grid.
|
||||
// The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas.
|
||||
@ -60,7 +58,7 @@ protected:
|
||||
SkCanvas legacyCanvas(legacyBMCanvas);
|
||||
legacyCanvas.drawImage(image, 0.0f, 0.0f);
|
||||
legacyCanvas.translate(SkScalar(kSize), 0.0f);
|
||||
legacyCanvas.drawImage(bitmap.asImage(), 0.0f, 0.0f);
|
||||
legacyCanvas.drawImage(codecImage, 0.0f, 0.0f);
|
||||
canvas->drawImage(legacyBMCanvas.asImage(), 0.0f, 0.0f);
|
||||
canvas->translate(0.0f, SkScalar(kSize));
|
||||
|
||||
@ -71,7 +69,7 @@ protected:
|
||||
SkCanvas srgbCanvas(srgbBMCanvas);
|
||||
srgbCanvas.drawImage(image, 0.0f, 0.0f);
|
||||
srgbCanvas.translate(SkScalar(kSize), 0.0f);
|
||||
srgbCanvas.drawImage(bitmap.asImage(), 0.0f, 0.0f);
|
||||
srgbCanvas.drawImage(codecImage, 0.0f, 0.0f);
|
||||
canvas->drawImage(srgbBMCanvas.asImage(), 0.0f, 0.0f);
|
||||
return DrawResult::kOk;
|
||||
}
|
||||
|
@ -28,11 +28,7 @@ sk_sp<SkImage> make_raster_image(const char* path) {
|
||||
sk_sp<SkData> resourceData = GetResourceAsData(path);
|
||||
std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(resourceData);
|
||||
|
||||
SkBitmap bitmap;
|
||||
bitmap.allocPixels(codec->getInfo());
|
||||
|
||||
codec->getPixels(codec->getInfo(), bitmap.getPixels(), bitmap.rowBytes());
|
||||
return bitmap.asImage();
|
||||
return std::get<0>(codec->getImage());
|
||||
}
|
||||
|
||||
sk_sp<SkImage> make_color_space(
|
||||
|
@ -40,14 +40,10 @@ static sk_sp<SkImage> make_raster_image(SkColorType colorType) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SkBitmap bitmap;
|
||||
SkImageInfo info = codec->getInfo().makeWH(kWidth, kHeight)
|
||||
.makeColorType(colorType)
|
||||
.makeAlphaType(kPremul_SkAlphaType);
|
||||
bitmap.allocPixels(info);
|
||||
codec->getPixels(info, bitmap.getPixels(), bitmap.rowBytes());
|
||||
bitmap.setImmutable();
|
||||
return bitmap.asImage();
|
||||
return std::get<0>(codec->getImage(info));
|
||||
}
|
||||
|
||||
static sk_sp<SkImage> make_codec_image() {
|
||||
|
@ -28,6 +28,7 @@ class SkAndroidCodec;
|
||||
class SkColorSpace;
|
||||
class SkData;
|
||||
class SkFrameHolder;
|
||||
class SkImage;
|
||||
class SkPngChunkReader;
|
||||
class SkSampler;
|
||||
|
||||
@ -381,6 +382,13 @@ public:
|
||||
return this->getPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), opts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an image containing the pixels.
|
||||
*/
|
||||
std::tuple<sk_sp<SkImage>, SkCodec::Result> getImage(const SkImageInfo& info,
|
||||
const Options* opts = nullptr);
|
||||
std::tuple<sk_sp<SkImage>, SkCodec::Result> getImage();
|
||||
|
||||
/**
|
||||
* If decoding to YUV is supported, this returns true. Otherwise, this
|
||||
* returns false and the caller will ignore output parameter yuvaPixmapInfo.
|
||||
|
@ -7,8 +7,10 @@
|
||||
|
||||
#include "include/codec/SkAndroidCodec.h"
|
||||
#include "include/codec/SkCodec.h"
|
||||
#include "include/core/SkBitmap.h"
|
||||
#include "include/core/SkColorSpace.h"
|
||||
#include "include/core/SkData.h"
|
||||
#include "include/core/SkImage.h"
|
||||
#include "include/private/SkHalf.h"
|
||||
#include "src/codec/SkBmpCodec.h"
|
||||
#include "src/codec/SkCodecPriv.h"
|
||||
@ -436,6 +438,30 @@ SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t
|
||||
return result;
|
||||
}
|
||||
|
||||
std::tuple<sk_sp<SkImage>, SkCodec::Result> SkCodec::getImage(const SkImageInfo& info,
|
||||
const Options* options) {
|
||||
SkBitmap bm;
|
||||
if (!bm.tryAllocPixels(info)) {
|
||||
return {nullptr, kInternalError};
|
||||
}
|
||||
|
||||
Result result = this->getPixels(info, bm.getPixels(), bm.rowBytes(), options);
|
||||
switch (result) {
|
||||
case kSuccess:
|
||||
case kIncompleteInput:
|
||||
case kErrorInInput:
|
||||
bm.setImmutable();
|
||||
return {bm.asImage(), result};
|
||||
|
||||
default: break;
|
||||
}
|
||||
return {nullptr, result};
|
||||
}
|
||||
|
||||
std::tuple<sk_sp<SkImage>, SkCodec::Result> SkCodec::getImage() {
|
||||
return this->getImage(this->getInfo(), nullptr);
|
||||
}
|
||||
|
||||
SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
|
||||
size_t rowBytes, const SkCodec::Options* options) {
|
||||
fStartedIncrementalDecode = false;
|
||||
|
@ -169,16 +169,12 @@ static void init(Source* source, std::shared_ptr<SkCodec> codec) {
|
||||
info = canvas->imageInfo().makeDimensions(info.dimensions());
|
||||
}
|
||||
|
||||
SkBitmap bm;
|
||||
bm.allocPixels(info);
|
||||
switch (SkCodec::Result result = codec->getPixels(info, bm.getPixels(), bm.rowBytes())) {
|
||||
case SkCodec::kSuccess:
|
||||
case SkCodec::kErrorInInput:
|
||||
case SkCodec::kIncompleteInput: canvas->drawBitmap(bm, 0,0);
|
||||
break;
|
||||
default: return fail("codec->getPixels() failed: %d\n", result);
|
||||
auto [image, result] = codec->getImage(info);
|
||||
if (image) {
|
||||
canvas->drawImage(image, 0,0);
|
||||
return ok;
|
||||
}
|
||||
return ok;
|
||||
return fail("codec->getPixels() failed: %d\n", result);
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user