• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
8 
9 #include "Resources.h"
10 #include "SkCanvas.h"
11 #include "SkData.h"
12 #include "SkFont.h"
13 #include "SkImage.h"
14 #include "SkImageEncoder.h"
15 
16 namespace skiagm {
17 
18 class EncodeGM : public GM {
19 public:
EncodeGM()20     EncodeGM() {}
21 
22 protected:
onShortName()23     SkString onShortName() override {
24         return SkString("encode");
25     }
26 
onISize()27     SkISize onISize() override {
28         return SkISize::Make(1024, 600);
29     }
30 
onDraw(SkCanvas * canvas)31     void onDraw(SkCanvas* canvas) override {
32         SkBitmap orig;
33         GetResourceAsBitmap("images/mandrill_512_q075.jpg", &orig);
34         auto pngData = SkEncodeBitmap(orig, SkEncodedImageFormat::kPNG, 100);
35         auto jpgData = SkEncodeBitmap(orig, SkEncodedImageFormat::kJPEG, 100);
36 
37         sk_sp<SkImage> pngImage = SkImage::MakeFromEncoded(pngData);
38         sk_sp<SkImage> jpgImage = SkImage::MakeFromEncoded(jpgData);
39         canvas->drawImage(pngImage.get(), 0.0f, 0.0f);
40         canvas->drawImage(jpgImage.get(), 512.0f, 0.0f);
41 
42         SkFont font;
43         font.setEdging(SkFont::Edging::kAlias);
44         canvas->drawString("Images should look identical.", 450.0f, 550.0f, font, SkPaint());
45     }
46 
47 private:
48     typedef GM INHERITED;
49 };
50 
51 DEF_GM( return new EncodeGM; )
52 }
53 
54 ///////////
55 
56 #if 0
57 DEF_SIMPLE_GM(jpeg_orientation, canvas, 1000, 1000) {
58     static sk_sp<SkImage> imgs[8];
59     if (!imgs[0]) {
60         for (int i = 0; i < 8; ++i) {
61             SkString path;
62             path.printf("/skia/orientation/Landscape_%d.jpg", i + 1);
63             auto stream = SkStream::MakeFromFile(path.c_str());
64             auto data = SkData::MakeFromStream(stream.get(), stream->getLength());
65             imgs[i] = SkImage::MakeFromEncoded(data, nullptr);
66         }
67     }
68     canvas->scale(0.25, 0.25);
69     for (int i = 0; i < 8; ++i) {
70         SkImage* img = imgs[i].get();
71         canvas->drawImage(img, 0, 0, nullptr);
72         canvas->translate(0, img->height());
73         if (i == 3) {
74             canvas->translate(img->width(), -4*img->height());
75         }
76     }
77 }
78 #endif
79