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

Side by Side Diff: gm/downsamplebitmap.cpp

Issue 18721006: fix trivial matrix computation (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 5 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 | gyp/gmslides.gypi » ('j') | no next file with comments »
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 2013 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 #include "SkStream.h" 13 #include "SkStream.h"
14 14
15 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style sty le) { 15 static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style sty le) {
16 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); 16 SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style)));
17 } 17 }
18 18
19 static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { 19 class DownsampleBitmapGM : public skiagm::GM {
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
26 static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat,
27 SkScalar dx) {
28 SkPaint paint;
29
30 SkAutoCanvasRestore acr(canvas, true);
31
32 canvas->drawBitmapMatrix(bm, mat, &paint);
33
34 paint.setFilterBitmap(true);
35 canvas->translate(dx, 0);
36 canvas->drawBitmapMatrix(bm, mat, &paint);
37
38 paint.setFlags(paint.getFlags() | SkPaint::kHighQualityFilterBitmap_Flag);
39 canvas->translate(dx, 0);
40 canvas->drawBitmapMatrix(bm, mat, &paint);
41 }
42
43 class FilterBitmapGM : public skiagm::GM {
44 void onOnceBeforeDraw() {
45
46 make_bitmap();
47
48 SkScalar cx = SkScalarHalf(fBM.width());
49 SkScalar cy = SkScalarHalf(fBM.height());
50 SkScalar scale = get_scale();
51
52
53 fMatrix[0].setScale(scale, scale);
54 fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale);
55 }
56 20
57 public: 21 public:
58 SkBitmap fBM; 22 SkBitmap fBM;
59 SkMatrix fMatrix[2];
60 SkString fName; 23 SkString fName;
24 bool fBitmapMade;
61 25
62 FilterBitmapGM() 26 DownsampleBitmapGM()
63 { 27 {
64 this->setBGColor(0xFFDDDDDD); 28 this->setBGColor(0xFFDDDDDD);
29 fBitmapMade = false;
65 } 30 }
66 31
67 void setName(const char name[]) { 32 void setName(const char name[]) {
68 fName.set(name); 33 fName.set(name);
69 } 34 }
70 35
71 protected: 36 protected:
72 virtual SkString onShortName() SK_OVERRIDE { 37 virtual SkString onShortName() SK_OVERRIDE {
73 return fName; 38 return fName;
74 } 39 }
75 40
76 virtual SkISize onISize() SK_OVERRIDE { 41 virtual SkISize onISize() SK_OVERRIDE {
77 return SkISize::Make(920, 480); 42 make_bitmap_wrapper();
43 return SkISize::Make(4 * fBM.width(), fBM.height());
44 }
45
46 void make_bitmap_wrapper() {
47 if (!fBitmapMade) {
48 fBitmapMade = true;
49 make_bitmap();
50 }
78 } 51 }
79 52
80 virtual void make_bitmap() = 0; 53 virtual void make_bitmap() = 0;
81 virtual SkScalar get_scale() = 0;
82 54
83 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { 55 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
56 make_bitmap_wrapper();
84 57
85 canvas->translate(10, 10); 58 int curX = 0;
86 for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { 59 int curWidth;
87 SkSize size = computeSize(fBM, fMatrix[i]); 60 float curScale = 1;
88 size.fWidth += 20; 61 do {
89 size.fHeight += 20;
90 62
91 draw_col(canvas, fBM, fMatrix[i], size.fWidth); 63 SkMatrix matrix;
92 canvas->translate(0, size.fHeight); 64 matrix.setScale( curScale, curScale );
93 } 65
66 SkPaint paint;
67 paint.setFilterBitmap(true);
68 paint.setFlags( paint.getFlags() | SkPaint::kHighQualityFilterBitmap _Flag );
69
70 canvas->save();
71 canvas->translate( (SkScalar) curX, 0.f );
72 canvas->drawBitmapMatrix( fBM, matrix, &paint );
73 canvas->restore();
74
75 curWidth = (int) (fBM.width() * curScale + 2);
76 curX += curWidth;
77 curScale *= 0.75f;
78 } while (curX < 4*fBM.width());
94 } 79 }
95 80
96 private: 81 private:
97 typedef skiagm::GM INHERITED; 82 typedef skiagm::GM INHERITED;
98 }; 83 };
99 84
100 class FilterBitmapTextGM: public FilterBitmapGM { 85 class DownsampleBitmapTextGM: public DownsampleBitmapGM {
101 public: 86 public:
102 FilterBitmapTextGM(float textSize) 87 DownsampleBitmapTextGM(float textSize)
103 : fTextSize(textSize) 88 : fTextSize(textSize)
104 { 89 {
105 char name[1024]; 90 char name[1024];
106 sprintf(name, "filterbitmap_text_%.2fpt", fTextSize); 91 sprintf(name, "downsamplebitmap_text_%.2fpt", fTextSize);
107 setName(name); 92 setName(name);
108 } 93 }
109 94
110 protected: 95 protected:
111 float fTextSize; 96 float fTextSize;
112 97
113 SkScalar get_scale() SK_OVERRIDE { 98 virtual void make_bitmap() SK_OVERRIDE {
114 return 32.f/fTextSize;
115 }
116
117 void make_bitmap() SK_OVERRIDE {
118 fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTe xtSize * 6)); 99 fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTe xtSize * 6));
119 fBM.allocPixels(); 100 fBM.allocPixels();
120 SkCanvas canvas(fBM); 101 SkCanvas canvas(fBM);
121 canvas.drawColor(SK_ColorWHITE); 102 canvas.drawColor(SK_ColorWHITE);
122 103
123 SkPaint paint; 104 SkPaint paint;
124 paint.setAntiAlias(true); 105 paint.setAntiAlias(true);
125 paint.setSubpixelText(true); 106 paint.setSubpixelText(true);
126 paint.setTextSize(fTextSize); 107 paint.setTextSize(fTextSize);
127 108
128 setTypeface(&paint, "Times", SkTypeface::kNormal); 109 setTypeface(&paint, "Times", SkTypeface::kNormal);
129 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint ); 110 canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint );
130 setTypeface(&paint, "Times", SkTypeface::kBold); 111 setTypeface(&paint, "Times", SkTypeface::kBold);
131 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint ); 112 canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint );
132 setTypeface(&paint, "Times", SkTypeface::kItalic); 113 setTypeface(&paint, "Times", SkTypeface::kItalic);
133 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint ); 114 canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint );
134 setTypeface(&paint, "Times", SkTypeface::kBoldItalic); 115 setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
135 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint ); 116 canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint );
136 } 117 }
137 private: 118 private:
138 typedef FilterBitmapGM INHERITED; 119 typedef DownsampleBitmapGM INHERITED;
139 }; 120 };
140 121
141 class FilterBitmapCheckerboardGM: public FilterBitmapGM { 122 class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM {
142 public: 123 public:
143 FilterBitmapCheckerboardGM(int size, int num_checks) 124 DownsampleBitmapCheckerboardGM(int size, int numChecks)
144 : fSize(size), fNumChecks(num_checks) 125 : fSize(size), fNumChecks(numChecks)
145 { 126 {
146 char name[1024]; 127 char name[1024];
147 sprintf(name, "filterbitmap_checkerboard_%d_%d", fSize, fNumChecks); 128 sprintf(name, "downsamplebitmap_checkerboard_%d_%d", fSize, fNumChec ks);
148 setName(name); 129 setName(name);
149 } 130 }
150 131
151 protected: 132 protected:
152 int fSize; 133 int fSize;
153 int fNumChecks; 134 int fNumChecks;
154 135
155 SkScalar get_scale() SK_OVERRIDE { 136 virtual void make_bitmap() SK_OVERRIDE {
156 return 192.f/fSize;
157 }
158
159 void make_bitmap() SK_OVERRIDE {
160 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize); 137 fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize);
161 fBM.allocPixels(); 138 fBM.allocPixels();
162 SkAutoLockPixels lock(fBM); 139 SkAutoLockPixels lock(fBM);
163 for (int y = 0; y < fSize; y ++) { 140 for (int y = 0; y < fSize; ++y) {
164 for (int x = 0; x < fSize; x ++) { 141 for (int x = 0; x < fSize; ++x) {
165 SkPMColor* s = fBM.getAddr32(x, y); 142 SkPMColor* s = fBM.getAddr32(x, y);
166 int cx = (x * fNumChecks) / fSize; 143 int cx = (x * fNumChecks) / fSize;
167 int cy = (y * fNumChecks) / fSize; 144 int cy = (y * fNumChecks) / fSize;
168 if ((cx+cy)%2) { 145 if ((cx+cy)%2) {
169 *s = 0xFFFFFFFF; 146 *s = 0xFFFFFFFF;
170 } else { 147 } else {
171 *s = 0xFF000000; 148 *s = 0xFF000000;
172 } 149 }
173 } 150 }
174 } 151 }
175 } 152 }
176 private: 153 private:
177 typedef FilterBitmapGM INHERITED; 154 typedef DownsampleBitmapGM INHERITED;
178 }; 155 };
179 156
180 class FilterBitmapImageGM: public FilterBitmapGM { 157 class DownsampleBitmapImageGM: public DownsampleBitmapGM {
181 public: 158 public:
182 FilterBitmapImageGM(const char filename[]) 159 DownsampleBitmapImageGM(const char filename[])
183 : fFilename(filename) 160 : fFilename(filename)
184 { 161 {
185 char name[1024]; 162 char name[1024];
186 sprintf(name, "filterbitmap_image_%s", filename); 163 sprintf(name, "downsamplebitmap_image_%s", filename);
187 setName(name); 164 setName(name);
188 } 165 }
189 166
190 protected: 167 protected:
191 SkString fFilename; 168 SkString fFilename;
192 int fSize; 169 int fSize;
193 170
194 SkScalar get_scale() SK_OVERRIDE { 171 virtual void make_bitmap() SK_OVERRIDE {
195 return 192.f/fSize;
196 }
197
198 void make_bitmap() SK_OVERRIDE {
199 SkString path(skiagm::GM::gResourcePath); 172 SkString path(skiagm::GM::gResourcePath);
200 path.append("/"); 173 path.append("/");
201 path.append(fFilename); 174 path.append(fFilename);
202 175
203 SkImageDecoder *codec = NULL; 176 SkImageDecoder *codec = NULL;
204 SkFILEStream stream(path.c_str()); 177 SkFILEStream stream(path.c_str());
205 if (stream.isValid()) { 178 if (stream.isValid()) {
206 codec = SkImageDecoder::Factory(&stream); 179 codec = SkImageDecoder::Factory(&stream);
207 } 180 }
208 if (codec) { 181 if (codec) {
209 stream.rewind(); 182 stream.rewind();
210 codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config, 183 codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config,
211 SkImageDecoder::kDecodePixels_Mode); 184 SkImageDecoder::kDecodePixels_Mode);
212 SkDELETE(codec); 185 SkDELETE(codec);
213 } else { 186 } else {
214 fBM.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); 187 fBM.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
215 fBM.allocPixels(); 188 fBM.allocPixels();
216 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad 189 *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
217 } 190 }
218 fSize = fBM.height(); 191 fSize = fBM.height();
219 } 192 }
220 private: 193 private:
221 typedef FilterBitmapGM INHERITED; 194 typedef DownsampleBitmapGM INHERITED;
222 }; 195 };
223 196
224 ////////////////////////////////////////////////////////////////////////////// 197 //////////////////////////////////////////////////////////////////////////////
225 198
226 DEF_GM( return new FilterBitmapTextGM(3); ) 199 DEF_GM( return new DownsampleBitmapTextGM(72); )
227 DEF_GM( return new FilterBitmapTextGM(7); ) 200 DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256); )
228 DEF_GM( return new FilterBitmapTextGM(10); ) 201 DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png"); )
229 DEF_GM( return new FilterBitmapCheckerboardGM(4,4); )
230 DEF_GM( return new FilterBitmapCheckerboardGM(32,32); )
231 DEF_GM( return new FilterBitmapCheckerboardGM(32,8); )
232 DEF_GM( return new FilterBitmapCheckerboardGM(32,2); )
233 DEF_GM( return new FilterBitmapCheckerboardGM(192,192); )
234 DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); )
235 DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); )
236 DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); )
237 DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); )
238 DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); )
239 DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); )
OLDNEW
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698