blob: c0150627caaedbc4ec8a8916fa1bac6c7f19d2e8 [file] [log] [blame]
humper@google.comb0889472013-07-09 21:37:14 +00001/*
2 * Copyright 2013 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
10#include "SkImageDecoder.h"
11#include "SkStream.h"
12
13class ScaleBitmapGM : public skiagm::GM {
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000014
humper@google.comb0889472013-07-09 21:37:14 +000015public:
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000016
humper@google.comb0889472013-07-09 21:37:14 +000017 ScaleBitmapGM(const char filename[], float scale)
18 : fFilename(filename), fScale(scale)
19 {
20 this->setBGColor(0xFFDDDDDD);
21 fName.printf("scalebitmap_%s_%f", filename, scale);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000022
humper@google.comb0889472013-07-09 21:37:14 +000023 SkString path(skiagm::GM::gResourcePath);
24 path.append("/");
25 path.append(fFilename);
26
27 SkImageDecoder *codec = NULL;
28 SkFILEStream stream(path.c_str());
29 if (stream.isValid()) {
30 codec = SkImageDecoder::Factory(&stream);
31 }
32 if (codec) {
33 stream.rewind();
34 codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config,
35 SkImageDecoder::kDecodePixels_Mode);
36 SkDELETE(codec);
37 } else {
38 fBM.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
39 fBM.allocPixels();
40 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
41 }
42 fSize = fBM.height();
43 }
44
45protected:
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000046
47
humper@google.comb0889472013-07-09 21:37:14 +000048 SkBitmap fBM;
49 SkString fName;
50 SkString fFilename;
51 int fSize;
52 float fScale;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000053
54
humper@google.comb0889472013-07-09 21:37:14 +000055 virtual SkString onShortName() SK_OVERRIDE {
56 return fName;
57 }
58
59 virtual SkISize onISize() SK_OVERRIDE {
60 return SkISize::Make(fBM.width() * fScale, fBM.height() * fScale);
61 }
62
63 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
64 SkBitmap dst;
65 dst.setConfig(SkBitmap::kARGB_8888_Config, fBM.width() * fScale, fBM.height() * fScale);
66 dst.allocPixels();
67 fBM.scale(&dst);
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000068
humper@google.comb0889472013-07-09 21:37:14 +000069 canvas->drawBitmap(dst, 0, 0);
70 }
71
72private:
73 typedef skiagm::GM INHERITED;
74};
75
76class ScaleBitmapMipmapGM: public ScaleBitmapGM {
77 SkMatrix fMatrix;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000078
humper@google.comb0889472013-07-09 21:37:14 +000079public:
80 ScaleBitmapMipmapGM(const char filename[], float scale)
81 : INHERITED(filename, scale)
82 {
83 fName.printf("scalebitmap_mipmap_%s_%f", filename, scale);
84 fBM.buildMipMap();
85 fMatrix.setScale(scale, scale);
86 }
87protected:
88 virtual void onDraw(SkCanvas *canvas) SK_OVERRIDE {
89 SkPaint paint;
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000090
humper@google.comb0889472013-07-09 21:37:14 +000091 paint.setFilterBitmap(true);
92 canvas->drawBitmapMatrix(fBM, fMatrix, &paint);
93 }
94private:
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000095 typedef ScaleBitmapGM INHERITED;
humper@google.comb0889472013-07-09 21:37:14 +000096};
97
98//////////////////////////////////////////////////////////////////////////////
99
100DEF_GM( return new ScaleBitmapGM("mandrill_128.png", 2); )
101DEF_GM( return new ScaleBitmapGM("mandrill_64.png", 4); )
102DEF_GM( return new ScaleBitmapGM("mandrill_32.png", 8); )
103DEF_GM( return new ScaleBitmapGM("mandrill_16.png", 16); )
104
105DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.5f); )
106DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.25f); )
107DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.125f); )
108DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.0625f); )
109
110DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.5f); )
111DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.25f); )
112DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.125f); )
113DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.0625f); )