filterbitmap.cpp revision bfefc7c95fc0e8ebd5000c68f6d16e1a3ea0e71e
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 15static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { 16 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); 17} 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 FilterBitmapGM : public skiagm::GM { 47 void onOnceBeforeDraw() { 48 49 this->makeBitmap(); 50 51 SkScalar cx = SkScalarHalf(fBM.width()); 52 SkScalar cy = SkScalarHalf(fBM.height()); 53 SkScalar scale = this->getScale(); 54 55 // these two matrices use a scale factor configured by the subclass 56 fMatrix[0].setScale(scale, scale); 57 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); 58 59 // up/down scaling mix 60 fMatrix[2].setScale(0.7f, 1.05f); 61 } 62 63public: 64 SkBitmap fBM; 65 SkMatrix fMatrix[3]; 66 SkString fName; 67 68 FilterBitmapGM() 69 { 70 this->setBGColor(0xFFDDDDDD); 71 } 72 73protected: 74 virtual uint32_t onGetFlags() const SK_OVERRIDE { 75 return kSkipTiled_Flag; 76 } 77 78 virtual SkString onShortName() SK_OVERRIDE { 79 return fName; 80 } 81 82 virtual SkISize onISize() SK_OVERRIDE { 83 return SkISize::Make(1024, 768); 84 } 85 86 virtual void makeBitmap() = 0; 87 virtual SkScalar getScale() = 0; 88 89 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 90 91 canvas->translate(10, 10); 92 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { 93 SkSize size = computeSize(fBM, fMatrix[i]); 94 size.fWidth += 20; 95 size.fHeight += 20; 96 97 draw_row(canvas, fBM, fMatrix[i], size.fWidth); 98 canvas->translate(0, size.fHeight); 99 } 100 } 101 102private: 103 typedef skiagm::GM INHERITED; 104}; 105 106class FilterBitmapTextGM: public FilterBitmapGM { 107 public: 108 FilterBitmapTextGM(float textSize) 109 : fTextSize(textSize) 110 { 111 fName.printf("filterbitmap_text_%.2fpt", fTextSize); 112 } 113 114 protected: 115 float fTextSize; 116 117 SkScalar getScale() SK_OVERRIDE { 118 return 32.f/fTextSize; 119 } 120 121 void makeBitmap() SK_OVERRIDE { 122 fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); 123 SkCanvas canvas(fBM); 124 canvas.drawColor(SK_ColorWHITE); 125 126 SkPaint paint; 127 paint.setAntiAlias(true); 128 paint.setSubpixelText(true); 129 paint.setTextSize(fTextSize); 130 131 setTypeface(&paint, "Times", SkTypeface::kNormal); 132 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); 133 setTypeface(&paint, "Times", SkTypeface::kBold); 134 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); 135 setTypeface(&paint, "Times", SkTypeface::kItalic); 136 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); 137 setTypeface(&paint, "Times", SkTypeface::kBoldItalic); 138 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); 139 } 140 private: 141 typedef FilterBitmapGM INHERITED; 142}; 143 144class FilterBitmapCheckerboardGM: public FilterBitmapGM { 145 public: 146 FilterBitmapCheckerboardGM(int size, int num_checks) 147 : fSize(size), fNumChecks(num_checks) 148 { 149 fName.printf("filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); 150 } 151 152 protected: 153 int fSize; 154 int fNumChecks; 155 156 SkScalar getScale() SK_OVERRIDE { 157 return 192.f/fSize; 158 } 159 160 void makeBitmap() SK_OVERRIDE { 161 fBM.allocN32Pixels(fSize, fSize); 162 for (int y = 0; y < fSize; y ++) { 163 for (int x = 0; x < fSize; x ++) { 164 SkPMColor* s = fBM.getAddr32(x, y); 165 int cx = (x * fNumChecks) / fSize; 166 int cy = (y * fNumChecks) / fSize; 167 if ((cx+cy)%2) { 168 *s = 0xFFFFFFFF; 169 } else { 170 *s = 0xFF000000; 171 } 172 } 173 } 174 } 175 private: 176 typedef FilterBitmapGM INHERITED; 177}; 178 179class FilterBitmapImageGM: public FilterBitmapGM { 180 public: 181 FilterBitmapImageGM(const char filename[]) 182 : fFilename(filename) 183 { 184 fName.printf("filterbitmap_image_%s", filename); 185 } 186 187 protected: 188 SkString fFilename; 189 int fSize; 190 191 SkScalar getScale() SK_OVERRIDE { 192 return 192.f/fSize; 193 } 194 195 void makeBitmap() SK_OVERRIDE { 196 SkString path(skiagm::GM::gResourcePath); 197 path.append("/"); 198 path.append(fFilename); 199 200 SkImageDecoder *codec = NULL; 201 SkFILEStream stream(path.c_str()); 202 if (stream.isValid()) { 203 codec = SkImageDecoder::Factory(&stream); 204 } 205 if (codec) { 206 stream.rewind(); 207 codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); 208 SkDELETE(codec); 209 } else { 210 fBM.allocN32Pixels(1, 1); 211 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 212 } 213 fSize = fBM.height(); 214 } 215 private: 216 typedef FilterBitmapGM INHERITED; 217}; 218 219////////////////////////////////////////////////////////////////////////////// 220 221DEF_GM( return new FilterBitmapTextGM(3); ) 222DEF_GM( return new FilterBitmapTextGM(7); ) 223DEF_GM( return new FilterBitmapTextGM(10); ) 224DEF_GM( return new FilterBitmapCheckerboardGM(4,4); ) 225DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) 226DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) 227DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) 228DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) 229DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) 230DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) 231DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) 232DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) 233DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) 234DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) 235