2016-06-13 18:25:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/codec/SkCodec.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkImage.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkImageInfo.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/Resources.h"
|
2016-06-13 18:25:03 +00:00
|
|
|
|
2019-05-01 14:59:30 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-06-13 18:25:03 +00:00
|
|
|
namespace skiagm {
|
|
|
|
|
|
|
|
class BitmapImageGM : public GM {
|
|
|
|
public:
|
|
|
|
BitmapImageGM() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
SkString onShortName() override {
|
2016-12-08 13:32:44 +00:00
|
|
|
return SkString("bitmap-image-srgb-legacy");
|
2016-06-13 18:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(2*kSize, 2*kSize);
|
|
|
|
}
|
|
|
|
|
2019-02-07 23:20:09 +00:00
|
|
|
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
|
2016-06-13 18:25:03 +00:00
|
|
|
// Create image.
|
2017-12-08 15:21:31 +00:00
|
|
|
const char* path = "images/mandrill_512_q075.jpg";
|
2016-12-08 13:32:44 +00:00
|
|
|
sk_sp<SkImage> image = GetResourceAsImage(path);
|
2016-06-13 18:25:03 +00:00
|
|
|
if (!image) {
|
2019-02-07 23:20:09 +00:00
|
|
|
*errorMsg = "Couldn't load images/mandrill_512_q075.jpg. "
|
|
|
|
"Did you forget to set the resource path?";
|
|
|
|
return DrawResult::kFail;
|
2016-06-13 18:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create matching bitmap.
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path)));
|
2021-01-25 20:36:09 +00:00
|
|
|
auto [codecImage, _] = codec->getImage();
|
2016-06-13 18:25:03 +00:00
|
|
|
|
|
|
|
// The GM will be displayed in a 2x2 grid.
|
2016-12-08 13:32:44 +00:00
|
|
|
// The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas.
|
2016-06-13 18:25:03 +00:00
|
|
|
SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType);
|
2016-12-08 13:32:44 +00:00
|
|
|
SkBitmap legacyBMCanvas;
|
|
|
|
legacyBMCanvas.allocPixels(linearInfo);
|
|
|
|
SkCanvas legacyCanvas(legacyBMCanvas);
|
2021-01-23 17:23:23 +00:00
|
|
|
legacyCanvas.drawImage(image, 0.0f, 0.0f);
|
2016-12-08 13:32:44 +00:00
|
|
|
legacyCanvas.translate(SkScalar(kSize), 0.0f);
|
2021-01-25 20:36:09 +00:00
|
|
|
legacyCanvas.drawImage(codecImage, 0.0f, 0.0f);
|
2021-01-24 13:57:23 +00:00
|
|
|
canvas->drawImage(legacyBMCanvas.asImage(), 0.0f, 0.0f);
|
2016-06-13 18:25:03 +00:00
|
|
|
canvas->translate(0.0f, SkScalar(kSize));
|
|
|
|
|
|
|
|
// The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas.
|
|
|
|
SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType);
|
|
|
|
SkBitmap srgbBMCanvas;
|
|
|
|
srgbBMCanvas.allocPixels(srgbInfo);
|
|
|
|
SkCanvas srgbCanvas(srgbBMCanvas);
|
2021-01-23 17:23:23 +00:00
|
|
|
srgbCanvas.drawImage(image, 0.0f, 0.0f);
|
2016-06-13 18:25:03 +00:00
|
|
|
srgbCanvas.translate(SkScalar(kSize), 0.0f);
|
2021-01-25 20:36:09 +00:00
|
|
|
srgbCanvas.drawImage(codecImage, 0.0f, 0.0f);
|
2021-01-24 13:57:23 +00:00
|
|
|
canvas->drawImage(srgbBMCanvas.asImage(), 0.0f, 0.0f);
|
2019-02-07 23:20:09 +00:00
|
|
|
return DrawResult::kOk;
|
2016-06-13 18:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr int kSize = 512;
|
2016-06-13 18:25:03 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2016-06-13 18:25:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-01-23 15:22:01 +00:00
|
|
|
DEF_GM( return new BitmapImageGM; )
|
2016-06-13 18:25:03 +00:00
|
|
|
|
2020-08-06 18:11:56 +00:00
|
|
|
} // namespace skiagm
|