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

Side by Side Diff: gm/downsamplebitmap.cpp

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

Powered by Google App Engine
This is Rietveld 408576698