blob: 1dae1bf04236c48b533836226943716d2257a298 [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"
11#include "SkImageEncoder.h"
12#include "Resources.h"
13
14namespace skiagm {
15
16class EncodeGM : public GM {
17public:
18 EncodeGM() {}
19
20protected:
21 SkString onShortName() override {
22 return SkString("encode");
23 }
24
25 SkISize onISize() override {
26 return SkISize::Make(1024, 600);
27 }
28
29 void onDraw(SkCanvas* canvas) override {
30 SkBitmap orig;
31 GetResourceAsBitmap("mandrill_512_q075.jpg", &orig);
Hal Canarydb683012016-11-23 08:55:18 -070032 sk_sp<SkData> pngData(sk_tool_utils::EncodeImageToData(orig, SkEncodedImageFormat::kPNG, 100));
33 sk_sp<SkData> jpegData(sk_tool_utils::EncodeImageToData(orig, SkEncodedImageFormat::kJPEG, 100));
msarett36c38cb2016-08-15 18:52:17 -070034
35 sk_sp<SkImage> pngImage = SkImage::MakeFromEncoded(pngData);
36 sk_sp<SkImage> jpegImage = SkImage::MakeFromEncoded(jpegData);
37 canvas->drawImage(pngImage.get(), 0.0f, 0.0f);
38 canvas->drawImage(jpegImage.get(), 512.0f, 0.0f);
39
40 const char text[] = "Images should look identical.";
41 canvas->drawText(text, sizeof(text) - 1, 450.0f, 550.0f, SkPaint());
42 }
43
44private:
45 typedef GM INHERITED;
46};
47
48DEF_GM( return new EncodeGM; )
49}