blob: a58f93c572aa5cdb0723ba3280601a2e7cc670c0 [file] [log] [blame]
Leon Scroggins III20a7ecc2016-12-19 15:04:06 -05001/*
2 * Copyright 2016 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
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkCodec.h"
11#include "SkCodecImageGenerator.h"
12#include "SkColor.h"
13#include "SkCommandLineFlags.h"
14#include "SkImageGenerator.h"
15#include "SkString.h"
16#include "Resources.h"
17
18DEFINE_string(codec_scaled, "brickwork-texture.jpg", "Image in resources/ to draw scaled.");
19
20class CodecScaledGM : public skiagm::GM {
21private:
22 // FIXME: Once generateScaledPixels is plumbed to SkImage, store an SkImage
23 // and call SkImage::scalePixels.
24 std::unique_ptr<SkImageGenerator> fGenerator;
25
26public:
27 CodecScaledGM()
28 {}
29
30private:
31 SkString onShortName() override {
32 return SkString("codec_scaled");
33 }
34
35 SkISize onISize() override {
36 if (this->initCodec()) {
37 SkISize dim = fGenerator->getInfo().dimensions();
38 // Wide enough to show 8 versions, corresponding to the options JPEG supports.
39 dim.fWidth *= 8;
40 // Tall enough to display 2 versions - one using computed dimensions, and one
41 // with scaling.
42 dim.fHeight *= 2;
43 return dim;
44 }
45 return SkISize::Make(640, 480);
46 }
47
48 void onDrawBackground(SkCanvas* canvas) override {
49 canvas->clear(SK_ColorWHITE);
50 }
51
52 bool initCodec() {
53 if (fGenerator) {
54 return true;
55 }
56
57 if (FLAGS_codec_scaled.isEmpty()) {
58 SkDebugf("Nothing specified for --codec_scaled!");
59 return false;
60 }
61
62 SkString path = GetResourcePath(FLAGS_codec_scaled[0]);
63 sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
64 if (!data) {
65 return false;
66 }
67
68 fGenerator.reset(SkCodecImageGenerator::NewFromEncodedCodec(data));
69 if (!fGenerator) {
70 SkDebugf("Could create codec from %s", FLAGS_codec_scaled[0]);
71 return false;
72 }
73
74 return true;
75 }
76
77 void onDraw(SkCanvas* canvas) override {
78 if (!this->initCodec()) {
79 return;
80 }
81
Leon Scroggins III20a7ecc2016-12-19 15:04:06 -050082 SkAutoCanvasRestore acr(canvas, true);
83 for (float scale : { 1.0f, .875f, .750f, .625f, .5f, .375f, .25f, .125f }) {
Leon Scroggins III9be9a422016-12-22 09:53:21 -050084 // generateScaledPixels does not support index8
85 const auto info = fGenerator->getInfo().makeColorType(kN32_SkColorType);
Leon Scroggins III20a7ecc2016-12-19 15:04:06 -050086 auto scaledInfo = info;
87 SkImageGenerator::SupportedSizes sizes;
88 if (fGenerator->computeScaledDimensions(scale, &sizes)) {
89 scaledInfo = info.makeWH(sizes.fSizes[0].fWidth, sizes.fSizes[0].fHeight);
90 }
91
92 SkBitmap bm;
Leon Scroggins III9be9a422016-12-22 09:53:21 -050093 bm.allocPixels(scaledInfo);
94 SkPixmap pixmap(scaledInfo, bm.getPixels(), bm.rowBytes());
Leon Scroggins III20a7ecc2016-12-19 15:04:06 -050095 if (fGenerator->generateScaledPixels(pixmap)) {
Leon Scroggins III9be9a422016-12-22 09:53:21 -050096 canvas->drawBitmap(bm, 0, 0);
Leon Scroggins III20a7ecc2016-12-19 15:04:06 -050097 }
98
Leon Scroggins III9be9a422016-12-22 09:53:21 -050099 bm.allocPixels(info);
100 if (fGenerator->getPixels(info, bm.getPixels(), bm.rowBytes())) {
Leon Scroggins III20a7ecc2016-12-19 15:04:06 -0500101 SkAutoCanvasRestore acr2(canvas, true);
102 canvas->translate(0, SkIntToScalar(info.height()));
103 canvas->scale(SkFloatToScalar(scale), SkFloatToScalar(scale));
104 canvas->drawBitmap(bm, 0, 0);
105 }
106
107 canvas->translate(SkIntToScalar(info.width()), 0);
108 }
109 }
110};
111
112DEF_GM(return new CodecScaledGM);