aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/codec_scaled.cpp
blob: a58f93c572aa5cdb0723ba3280601a2e7cc670c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkCanvas.h"
#include "SkCodec.h"
#include "SkCodecImageGenerator.h"
#include "SkColor.h"
#include "SkCommandLineFlags.h"
#include "SkImageGenerator.h"
#include "SkString.h"
#include "Resources.h"

DEFINE_string(codec_scaled, "brickwork-texture.jpg", "Image in resources/ to draw scaled.");

class CodecScaledGM : public skiagm::GM {
private:
    // FIXME: Once generateScaledPixels is plumbed to SkImage, store an SkImage
    // and call SkImage::scalePixels.
    std::unique_ptr<SkImageGenerator>   fGenerator;

public:
    CodecScaledGM()
    {}

private:
    SkString onShortName() override {
        return SkString("codec_scaled");
    }

    SkISize onISize() override {
        if (this->initCodec()) {
            SkISize dim = fGenerator->getInfo().dimensions();
            // Wide enough to show 8 versions, corresponding to the options JPEG supports.
            dim.fWidth *= 8;
            // Tall enough to display 2 versions - one using computed dimensions, and one
            // with scaling.
            dim.fHeight *= 2;
            return dim;
        }
        return SkISize::Make(640, 480);
    }

    void onDrawBackground(SkCanvas* canvas) override {
        canvas->clear(SK_ColorWHITE);
    }

    bool initCodec() {
        if (fGenerator) {
            return true;
        }

        if (FLAGS_codec_scaled.isEmpty()) {
            SkDebugf("Nothing specified for --codec_scaled!");
            return false;
        }

        SkString path = GetResourcePath(FLAGS_codec_scaled[0]);
        sk_sp<SkData> data(SkData::MakeFromFileName(path.c_str()));
        if (!data) {
            return false;
        }

        fGenerator.reset(SkCodecImageGenerator::NewFromEncodedCodec(data));
        if (!fGenerator) {
            SkDebugf("Could create codec from %s", FLAGS_codec_scaled[0]);
            return false;
        }

        return true;
    }

    void onDraw(SkCanvas* canvas) override {
        if (!this->initCodec()) {
            return;
        }

        SkAutoCanvasRestore acr(canvas, true);
        for (float scale : { 1.0f, .875f, .750f, .625f, .5f, .375f, .25f, .125f }) {
            // generateScaledPixels does not support index8
            const auto info = fGenerator->getInfo().makeColorType(kN32_SkColorType);
            auto scaledInfo = info;
            SkImageGenerator::SupportedSizes sizes;
            if (fGenerator->computeScaledDimensions(scale, &sizes)) {
                scaledInfo = info.makeWH(sizes.fSizes[0].fWidth, sizes.fSizes[0].fHeight);
            }

            SkBitmap bm;
            bm.allocPixels(scaledInfo);
            SkPixmap pixmap(scaledInfo, bm.getPixels(), bm.rowBytes());
            if (fGenerator->generateScaledPixels(pixmap)) {
                canvas->drawBitmap(bm, 0, 0);
            }

            bm.allocPixels(info);
            if (fGenerator->getPixels(info, bm.getPixels(), bm.rowBytes())) {
                SkAutoCanvasRestore acr2(canvas, true);
                canvas->translate(0, SkIntToScalar(info.height()));
                canvas->scale(SkFloatToScalar(scale), SkFloatToScalar(scale));
                canvas->drawBitmap(bm, 0, 0);
            }

            canvas->translate(SkIntToScalar(info.width()), 0);
        }
    }
};

DEF_GM(return new CodecScaledGM);