blob: bf13d3dc09ac47eb6fc741d259ba85a52204bda5 [file] [log] [blame]
msarett36c38cb2016-08-15 18:52:17 -07001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
msarett36c38cb2016-08-15 18:52:17 -07009#include "SkCanvas.h"
10#include "SkData.h"
Florin Malitaab244f02017-05-03 19:16:58 +000011#include "SkImage.h"
msarett36c38cb2016-08-15 18:52:17 -070012#include "SkImageEncoder.h"
13#include "Resources.h"
14
15namespace skiagm {
16
17class EncodeGM : public GM {
18public:
19 EncodeGM() {}
20
21protected:
22 SkString onShortName() override {
23 return SkString("encode");
24 }
25
26 SkISize onISize() override {
27 return SkISize::Make(1024, 600);
28 }
29
30 void onDraw(SkCanvas* canvas) override {
31 SkBitmap orig;
32 GetResourceAsBitmap("mandrill_512_q075.jpg", &orig);
Hal Canarydb683012016-11-23 08:55:18 -070033 sk_sp<SkData> pngData(sk_tool_utils::EncodeImageToData(orig, SkEncodedImageFormat::kPNG, 100));
34 sk_sp<SkData> jpegData(sk_tool_utils::EncodeImageToData(orig, SkEncodedImageFormat::kJPEG, 100));
msarett36c38cb2016-08-15 18:52:17 -070035
36 sk_sp<SkImage> pngImage = SkImage::MakeFromEncoded(pngData);
37 sk_sp<SkImage> jpegImage = SkImage::MakeFromEncoded(jpegData);
38 canvas->drawImage(pngImage.get(), 0.0f, 0.0f);
39 canvas->drawImage(jpegImage.get(), 512.0f, 0.0f);
40
41 const char text[] = "Images should look identical.";
42 canvas->drawText(text, sizeof(text) - 1, 450.0f, 550.0f, SkPaint());
43 }
44
45private:
46 typedef GM INHERITED;
47};
48
49DEF_GM( return new EncodeGM; )
50}