helper to convert CGImage -> SkImage

BUG=skia:

Change-Id: I07e0b8fe510d34ab541de7572cb6775478527624
Reviewed-on: https://skia-review.googlesource.com/6386
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Mike Reed 2016-12-21 12:01:12 -05:00 committed by Skia Commit-Bot
parent bde428513f
commit 463c848f3b
5 changed files with 36 additions and 10 deletions

View File

@ -199,9 +199,10 @@ static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
SkBitmap bm2;
SkCreateBitmapFromCGImage(&bm2, image);
CGImageRelease(image);
canvas->drawBitmap(bm2, 10, 120);
canvas->drawImage(SkMakeImageFromCGImage(image), 10, 120 + bm2.height() + 10);
CGImageRelease(image);
}
class CGImageGM : public skiagm::GM {
@ -243,10 +244,7 @@ protected:
private:
typedef skiagm::GM INHERITED;
};
#if 0 // Disabled pending fix from reed@
DEF_GM( return new CGImageGM; )
#endif
//DEF_GM( return new CGImageGM; )
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -10,6 +10,7 @@
#include "SkSize.h"
#include "SkImageInfo.h"
#include "SkImage.h"
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
@ -32,7 +33,9 @@ class SkStreamRewindable;
*
* On failure, return false, and leave bitmap unchanged.
*/
SK_API bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef src, SkISize* scaleToFit = NULL);
SK_API bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef src);
SK_API sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef);
/**
* Copy the pixels from src into the memory specified by info/rowBytes/dstPixels. On failure,

View File

@ -212,9 +212,9 @@ SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, vo
return true;
}
bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleToFit) {
const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth(image));
const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHeight(image));
bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image) {
const int width = SkToInt(CGImageGetWidth(image));
const int height = SkToInt(CGImageGetHeight(image));
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
SkBitmap tmp;
@ -245,4 +245,14 @@ bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleTo
return true;
}
sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef src) {
SkBitmap bm;
if (!SkCreateBitmapFromCGImage(&bm, src)) {
return nullptr;
}
bm.setImmutable();
return SkImage::MakeFromBitmap(bm);
}
#endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)

View File

@ -57,6 +57,19 @@ SkStreamAsset* GetResourceAsStream(const char* resource) {
return stream.release();
}
sk_sp<SkData> GetResourceAsData(const char* resource) {
SkString resourcePath = GetResourcePath(resource);
std::unique_ptr<SkFILEStream> stream(new SkFILEStream(resourcePath.c_str()));
if (!stream->isValid()) {
SkDebugf("Resource %s not found.\n", resource);
return nullptr;
}
size_t bytes = stream->getLength();
sk_sp<SkData> data = SkData::MakeUninitialized(bytes);
stream->read(data->writable_data(), bytes);
return data;
}
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource) {
std::unique_ptr<SkStreamAsset> stream(GetResourceAsStream(resource));
if (!stream) {

View File

@ -12,6 +12,7 @@
#include "SkString.h"
class SkBitmap;
class SkData;
class SkImage;
class SkStreamAsset;
class SkTypeface;
@ -22,6 +23,7 @@ void SetResourcePath(const char* );
bool GetResourceAsBitmap(const char* resource, SkBitmap* dst);
sk_sp<SkImage> GetResourceAsImage(const char* resource);
SkStreamAsset* GetResourceAsStream(const char* resource);
sk_sp<SkData> GetResourceAsData(const char* resource);
sk_sp<SkTypeface> MakeResourceAsTypeface(const char* resource);
#endif // Resources_DEFINED