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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkData.h" |
Florin Malita | ab244f0 | 2017-05-03 19:16:58 +0000 | [diff] [blame] | 11 | #include "SkImage.h" |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
| 13 | #include "Resources.h" |
| 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class EncodeGM : public GM { |
| 18 | public: |
| 19 | EncodeGM() {} |
| 20 | |
| 21 | protected: |
| 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; |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 32 | GetResourceAsBitmap("images/mandrill_512_q075.jpg", &orig); |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 33 | 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)); |
msarett | 36c38cb | 2016-08-15 18:52:17 -0700 | [diff] [blame] | 35 | |
| 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 | |
| 45 | private: |
| 46 | typedef GM INHERITED; |
| 47 | }; |
| 48 | |
| 49 | DEF_GM( return new EncodeGM; ) |
| 50 | } |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 51 | |
| 52 | /////////// |
| 53 | |
| 54 | #if 0 |
| 55 | DEF_SIMPLE_GM(jpeg_orientation, canvas, 1000, 1000) { |
| 56 | static sk_sp<SkImage> imgs[8]; |
| 57 | if (!imgs[0]) { |
| 58 | for (int i = 0; i < 8; ++i) { |
| 59 | SkString path; |
| 60 | path.printf("/skia/orientation/Landscape_%d.jpg", i + 1); |
| 61 | auto stream = SkStream::MakeFromFile(path.c_str()); |
| 62 | auto data = SkData::MakeFromStream(stream.get(), stream->getLength()); |
| 63 | imgs[i] = SkImage::MakeFromEncoded(data, nullptr); |
| 64 | } |
| 65 | } |
| 66 | canvas->scale(0.25, 0.25); |
| 67 | for (int i = 0; i < 8; ++i) { |
| 68 | SkImage* img = imgs[i].get(); |
| 69 | canvas->drawImage(img, 0, 0, nullptr); |
| 70 | canvas->translate(0, img->height()); |
| 71 | if (i == 3) { |
| 72 | canvas->translate(img->width(), -4*img->height()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | #endif |