filterindiabox.cpp revision 44f41293ac94530d08fca2ce8484a7248e7a5e97
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 8#include "gm.h" 9#include "SkGradientShader.h" 10 11#include "SkTypeface.h" 12#include "SkImageDecoder.h" 13#include "SkStream.h" 14 15#include "SkImageEncoder.h" 16#include "SkBitmapScaler.h" 17#include "SkBitmapProcState.h" 18 19static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { 20 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), 21 SkIntToScalar(bm.height())); 22 mat.mapRect(&bounds); 23 return SkSize::Make(bounds.width(), bounds.height()); 24} 25 26static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) { 27 SkPaint paint; 28 29 SkAutoCanvasRestore acr(canvas, true); 30 31 canvas->drawBitmapMatrix(bm, mat, &paint); 32 33 paint.setFilterLevel(SkPaint::kLow_FilterLevel); 34 canvas->translate(dx, 0); 35 canvas->drawBitmapMatrix(bm, mat, &paint); 36 37 paint.setFilterLevel(SkPaint::kMedium_FilterLevel); 38 canvas->translate(dx, 0); 39 canvas->drawBitmapMatrix(bm, mat, &paint); 40 41 paint.setFilterLevel(SkPaint::kHigh_FilterLevel); 42 canvas->translate(dx, 0); 43 canvas->drawBitmapMatrix(bm, mat, &paint); 44} 45 46class FilterIndiaBoxGM : public skiagm::GM { 47 void onOnceBeforeDraw() { 48 49 this->makeBitmap(); 50 51 SkScalar cx = SkScalarHalf(fBM.width()); 52 SkScalar cy = SkScalarHalf(fBM.height()); 53 54 float vertScale = 30.0/55.0; 55 float horizScale = 150.0/200.0; 56 57 fMatrix[0].setScale(horizScale, vertScale); 58 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale); 59 } 60 61public: 62 SkBitmap fBM; 63 SkMatrix fMatrix[2]; 64 SkString fName; 65 66 FilterIndiaBoxGM() 67 { 68 this->setBGColor(0xFFDDDDDD); 69 } 70 71 FilterIndiaBoxGM(const char filename[]) 72 : fFilename(filename) 73 { 74 fName.printf("filterindiabox"); 75 } 76 77protected: 78 virtual uint32_t onGetFlags() const SK_OVERRIDE { 79 return kSkipTiled_Flag; 80 } 81 82 virtual SkString onShortName() SK_OVERRIDE { 83 return fName; 84 } 85 86 virtual SkISize onISize() SK_OVERRIDE { 87 return SkISize::Make(1024, 768); 88 } 89 90 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 91 92 canvas->translate(10, 10); 93 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { 94 SkSize size = computeSize(fBM, fMatrix[i]); 95 size.fWidth += 20; 96 size.fHeight += 20; 97 98 draw_row(canvas, fBM, fMatrix[i], size.fWidth); 99 canvas->translate(0, size.fHeight); 100 } 101 } 102 103 protected: 104 SkString fFilename; 105 int fSize; 106 107 SkScalar getScale() { 108 return 192.f/fSize; 109 } 110 111 void makeBitmap() { 112 SkString path(skiagm::GM::gResourcePath); 113 path.append("/"); 114 path.append(fFilename); 115 116 SkImageDecoder *codec = NULL; 117 SkFILEStream stream(path.c_str()); 118 if (stream.isValid()) { 119 codec = SkImageDecoder::Factory(&stream); 120 } 121 if (codec) { 122 stream.rewind(); 123 codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config, 124 SkImageDecoder::kDecodePixels_Mode); 125 SkDELETE(codec); 126 } else { 127 fBM.allocN32Pixels(1, 1); 128 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 129 } 130 fSize = fBM.height(); 131 } 132 private: 133 typedef skiagm::GM INHERITED; 134}; 135 136////////////////////////////////////////////////////////////////////////////// 137 138 139DEF_GM( return new FilterIndiaBoxGM("box.gif"); ) 140