humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | class ScaleBitmapGM : public skiagm::GM { |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 14 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 15 | public: |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 16 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 17 | 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.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 22 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 23 | 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 | |
| 45 | protected: |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 46 | |
| 47 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 48 | SkBitmap fBM; |
| 49 | SkString fName; |
| 50 | SkString fFilename; |
| 51 | int fSize; |
| 52 | float fScale; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 53 | |
| 54 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 55 | 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.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 68 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 69 | canvas->drawBitmap(dst, 0, 0); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | typedef skiagm::GM INHERITED; |
| 74 | }; |
| 75 | |
| 76 | class ScaleBitmapMipmapGM: public ScaleBitmapGM { |
| 77 | SkMatrix fMatrix; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 78 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 79 | public: |
| 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 | } |
| 87 | protected: |
| 88 | virtual void onDraw(SkCanvas *canvas) SK_OVERRIDE { |
| 89 | SkPaint paint; |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 90 | |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 91 | paint.setFilterBitmap(true); |
| 92 | canvas->drawBitmapMatrix(fBM, fMatrix, &paint); |
| 93 | } |
| 94 | private: |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 95 | typedef ScaleBitmapGM INHERITED; |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | ////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | DEF_GM( return new ScaleBitmapGM("mandrill_128.png", 2); ) |
| 101 | DEF_GM( return new ScaleBitmapGM("mandrill_64.png", 4); ) |
| 102 | DEF_GM( return new ScaleBitmapGM("mandrill_32.png", 8); ) |
| 103 | DEF_GM( return new ScaleBitmapGM("mandrill_16.png", 16); ) |
| 104 | |
| 105 | DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.5f); ) |
| 106 | DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.25f); ) |
| 107 | DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.125f); ) |
| 108 | DEF_GM( return new ScaleBitmapGM("nature.jpg", 0.0625f); ) |
| 109 | |
| 110 | DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.5f); ) |
| 111 | DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.25f); ) |
| 112 | DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.125f); ) |
| 113 | DEF_GM( return new ScaleBitmapMipmapGM("nature.jpg", 0.0625f); ) |