Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: gm/filterbitmap.cpp

Issue 15755019: more general GM setup for testing image filtering -- now with checkerboards, images, and text. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | gm/lena_bytes.h » ('j') | gm/lena_bytes.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "gm.h" 8 #include "gm.h"
9 #include "SkGradientShader.h" 9 #include "SkGradientShader.h"
10 10
11 #include "SkTypeface.h" 11 #include "SkTypeface.h"
12 #include "SkImageDecoder.h" 12 #include "SkImageDecoder.h"
13 13
14 #include "lena_bytes.h"
15
14 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style sty le) { 16 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style sty le) {
15 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); 17 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style)));
16 } 18 }
17 19
18 static void load_bm(SkBitmap* bm) {
19 // SkImageDecoder::DecodeFile("/skia/trunk/books.jpg", bm);
20
21 bm->setConfig(SkBitmap::kARGB_8888_Config, 160, 120);
22 bm->allocPixels();
23 SkCanvas canvas(*bm);
24 canvas.drawColor(SK_ColorWHITE);
25
26 SkPaint paint;
27 paint.setAntiAlias(true);
28 paint.setSubpixelText(true);
29 paint.setTextSize(17);
30
31 setTypeface(&paint, "Times", SkTypeface::kNormal);
32 canvas.drawText("Hamburgefons", 12, 10, 25, paint);
33 setTypeface(&paint, "Times", SkTypeface::kBold);
34 canvas.drawText("Hamburgefons", 12, 10, 50, paint);
35 setTypeface(&paint, "Times", SkTypeface::kItalic);
36 canvas.drawText("Hamburgefons", 12, 10, 75, paint);
37 setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
38 canvas.drawText("Hamburgefons", 12, 10, 100, paint);
39 }
40
41 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { 20 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
42 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), 21 SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
43 SkIntToScalar(bm.height())); 22 SkIntToScalar(bm.height()));
44 mat.mapRect(&bounds); 23 mat.mapRect(&bounds);
45 return SkSize::Make(bounds.width(), bounds.height()); 24 return SkSize::Make(bounds.width(), bounds.height());
46 } 25 }
47 26
48 static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, 27 static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat,
49 SkScalar dx) { 28 SkScalar dx) {
50 SkPaint paint; 29 SkPaint paint;
(...skipping 11 matching lines...) Expand all
62 canvas->drawBitmapMatrix(bm, mat, &paint); 41 canvas->drawBitmapMatrix(bm, mat, &paint);
63 } 42 }
64 43
65 class FilterBitmapGM : public skiagm::GM { 44 class FilterBitmapGM : public skiagm::GM {
66 bool fOnce; 45 bool fOnce;
67 void init() { 46 void init() {
68 if (fOnce) { 47 if (fOnce) {
69 return; 48 return;
70 } 49 }
71 fOnce = true; 50 fOnce = true;
72 load_bm(&fBM); 51
73 52 make_bitmap();
53
74 SkScalar cx = SkScalarHalf(fBM.width()); 54 SkScalar cx = SkScalarHalf(fBM.width());
75 SkScalar cy = SkScalarHalf(fBM.height()); 55 SkScalar cy = SkScalarHalf(fBM.height());
76 SkScalar scale = 1.6f; 56 SkScalar scale = get_scale();
57
77 58
78 fMatrix[0].setScale(scale, scale); 59 fMatrix[0].setScale(scale, scale);
79 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); 60 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale);
80 } 61 }
81 62
82 public: 63 public:
83 SkBitmap fBM; 64 SkBitmap fBM;
84 SkMatrix fMatrix[2]; 65 SkMatrix fMatrix[2];
66 SkString fName;
67
68 FilterBitmapGM():
69 fOnce(false) {
70 this->setBGColor(0xFFDDDDDD);
71 }
85 72
86 FilterBitmapGM() : fOnce(false) { 73 void setName( const char name[] ) {
87 this->setBGColor(0xFFDDDDDD); 74 fName = SkString(name);
reed1 2013/05/29 19:26:04 alternative (slightly more efficient) : fName.set(
88 } 75 }
89 76
90 protected: 77 protected:
91 virtual SkString onShortName() SK_OVERRIDE { 78 virtual SkString onShortName() SK_OVERRIDE {
92 return SkString("filterbitmap"); 79 return fName;
93 } 80 }
94 81
95 virtual SkISize onISize() SK_OVERRIDE { 82 virtual SkISize onISize() SK_OVERRIDE {
96 return SkISize::Make(920, 480); 83 return SkISize::Make(920, 480);
97 } 84 }
98 85
86 virtual void make_bitmap() = 0;
87 virtual SkScalar get_scale() = 0;
88
99 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 89 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
100 this->init(); 90 this->init();
101 91
102 canvas->translate(10, 10); 92 canvas->translate(10, 10);
103 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { 93 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
104 SkSize size = computeSize(fBM, fMatrix[i]); 94 SkSize size = computeSize(fBM, fMatrix[i]);
105 size.fWidth += 20; 95 size.fWidth += 20;
106 size.fHeight += 20; 96 size.fHeight += 20;
107 97
108 draw_col(canvas, fBM, fMatrix[i], size.fWidth); 98 draw_col(canvas, fBM, fMatrix[i], size.fWidth);
109 canvas->translate(0, size.fHeight); 99 canvas->translate(0, size.fHeight);
110 } 100 }
111 } 101 }
112 102
113 private: 103 private:
114 typedef skiagm::GM INHERITED; 104 typedef skiagm::GM INHERITED;
115 }; 105 };
116 106
107 class FilterBitmapTextGM: public FilterBitmapGM {
108 public:
109 FilterBitmapTextGM( float textSize )
110 : fTextSize( textSize )
111 {
112 char name[1024];
113 sprintf( name, "filterbitmap_text_%.2fpt", fTextSize );
114 setName( name );
115 }
116
117 protected:
118 float fTextSize;
119
120 SkScalar get_scale() SK_OVERRIDE {
121 return 32.f/fTextSize;
122 }
123
124 void make_bitmap() SK_OVERRIDE {
125 fBM.setConfig(SkBitmap::kARGB_8888_Config, fTextSize * 8, fTextSize * 6);
126 fBM.allocPixels();
127 SkCanvas canvas(fBM);
128 canvas.drawColor(SK_ColorWHITE);
129
130 SkPaint paint;
131 paint.setAntiAlias(true);
132 paint.setSubpixelText(true);
133 paint.setTextSize(fTextSize);
134
135 setTypeface(&paint, "Times", SkTypeface::kNormal);
136 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2*fTextSize, paint) ;
137 setTypeface(&paint, "Times", SkTypeface::kBold);
138 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4*fTextSize, paint) ;
139 setTypeface(&paint, "Times", SkTypeface::kItalic);
140 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6*fTextSize, paint) ;
141 setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
142 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8*fTextSize, paint) ;
143 }
144 private:
145 typedef FilterBitmapGM INHERITED;
146 };
147
148 class FilterBitmapCheckerboardGM: public FilterBitmapGM {
149 public:
150 FilterBitmapCheckerboardGM( int size, int num_checks )
151 : fSize( size ), fNumChecks( num_checks )
152 {
153 char name[1024];
154 sprintf( name, "filterbitmap_checkerboard_%d_%d", fSize, fNumChecks );
155 setName( name );
156 }
157
158 protected:
159 int fSize;
160 int fNumChecks;
161
162 SkScalar get_scale() SK_OVERRIDE {
163 return 192.f/fSize;
164 }
165
166 void make_bitmap() SK_OVERRIDE {
167 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize);
168 fBM.allocPixels();
169 SkAutoLockPixels lock(fBM);
170 for (int y = 0; y < fSize; y ++) {
171 for (int x = 0; x < fSize; x ++) {
172 SkPMColor* s = fBM.getAddr32(x, y);
173 int cx = (x * fNumChecks) / fSize;
174 int cy = (y * fNumChecks) / fSize;
175 if ((cx+cy)%2) {
176 *s = 0xFFFFFFFF;
177 } else {
178 *s = 0xFF000000;
179 }
180 }
181 }
182 }
183 private:
184 typedef FilterBitmapGM INHERITED;
185 };
186
187 class FilterBitmapImageGM: public FilterBitmapGM {
188 public:
189 FilterBitmapImageGM( int size )
190 : fSize( size )
191 {
192 char name[1024];
193 sprintf( name, "filterbitmap_image_%d", fSize );
194 setName( name );
195 }
196
197 protected:
198 int fSize;
199 int fNumChecks;
200
201 SkScalar get_scale() SK_OVERRIDE {
202 return 192.f/fSize;
203 }
204
205 void make_bitmap() SK_OVERRIDE {
206 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize);
207 fBM.allocPixels();
208
209 unsigned char *lena;
210 switch( fSize ) {
211 case 16:
212 lena = lena_16;
213 break;
214 case 32:
215 lena = lena_32;
216 break;
217 case 64:
218 lena = lena_64;
219 break;
220 case 128:
221 lena = lena_128;
222 break;
223 default:
224 lena = NULL;
225 break;
226 }
227 SkAutoLockPixels lock(fBM);
228 for (int y = 0; y < fSize; y ++) {
229 for (int x = 0; x < fSize; x ++) {
230 SkPMColor* s = fBM.getAddr32(x, y);
231 unsigned char *src = lena + 4*(y*fSize + x);
232 *s = SkPreMultiplyARGB(src[3], src[0], src[1], src[2]);
233 }
234 }
235 }
236 private:
237 typedef FilterBitmapGM INHERITED;
238 };
239
117 ////////////////////////////////////////////////////////////////////////////// 240 //////////////////////////////////////////////////////////////////////////////
118 241
119 DEF_GM( return new FilterBitmapGM; ) 242 DEF_GM( return new FilterBitmapTextGM(3); )
243 DEF_GM( return new FilterBitmapTextGM(7); )
244 DEF_GM( return new FilterBitmapTextGM(10); )
245 DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
246 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
247 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
248 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
249 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
250 DEF_GM( return new FilterBitmapImageGM(16); )
251 DEF_GM( return new FilterBitmapImageGM(32); )
252 DEF_GM( return new FilterBitmapImageGM(64); )
253 DEF_GM( return new FilterBitmapImageGM(128); )
OLDNEW
« no previous file with comments | « no previous file | gm/lena_bytes.h » ('j') | gm/lena_bytes.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698