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