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

Side by Side Diff: gm/image.cpp

Issue 821083002: add newImage API (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: check when subset == bounds Created 5 years, 11 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
« no previous file with comments | « gm/drawbitmaprect.cpp ('k') | include/core/SkFilterQuality.h » ('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 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 "SkData.h"
10 #include "SkCanvas.h"
11 #include "SkRandom.h"
12 #include "SkStream.h"
9 #include "SkSurface.h" 13 #include "SkSurface.h"
10 #include "SkCanvas.h"
11 #include "SkStream.h"
12 #include "SkData.h"
13 14
14 #if SK_SUPPORT_GPU 15 #if SK_SUPPORT_GPU
15 #include "GrContext.h" 16 #include "GrContext.h"
16 #endif 17 #endif
17 18
18 static void drawJpeg(SkCanvas* canvas, const SkISize& size) { 19 static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
19 // TODO: Make this draw a file that is checked in, so it can 20 // TODO: Make this draw a file that is checked in, so it can
20 // be exercised on machines other than mike's. Will require a 21 // be exercised on machines other than mike's. Will require a
21 // rebaseline. 22 // rebaseline.
22 SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.goo gle.jpeg")); 23 SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.goo gle.jpeg"));
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 190
190 uint32_t onGetFlags() const SK_OVERRIDE { 191 uint32_t onGetFlags() const SK_OVERRIDE {
191 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag; 192 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
192 } 193 }
193 194
194 private: 195 private:
195 typedef skiagm::GM INHERITED; 196 typedef skiagm::GM INHERITED;
196 }; 197 };
197 DEF_GM( return new ImageGM; ) 198 DEF_GM( return new ImageGM; )
198 199
200 class ImageResizeGM : public skiagm::GM {
201 enum {
202 W = 100,
203 H = 100,
204 };
205 public:
206 ImageResizeGM() {}
207
208 protected:
209 SkString onShortName() SK_OVERRIDE { return SkString("image-resize"); }
210
211 SkISize onISize() SK_OVERRIDE { return SkISize::Make(510, 480); }
212
213 void drawIntoImage(SkCanvas* canvas) {
214 SkPaint paint;
215 paint.setAntiAlias(true);
216 paint.setStyle(SkPaint::kStroke_Style);
217 paint.setStrokeWidth(3);
218 SkRandom rand;
219 for (int i = 0; i < 60; ++i) {
220 paint.setColor(rand.nextU());
221 SkScalar x = rand.nextUScalar1() * W;
222 SkScalar y = rand.nextUScalar1() * H;
223 SkScalar r = rand.nextUScalar1() * W / 2;
224 canvas->drawCircle(x, y, r, paint);
225 }
226 }
227
228 SkImage* makeImage(SkCanvas* canvas) {
229 const SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
230 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
231 if (!surface) {
232 surface.reset(SkSurface::NewRaster(info));
233 }
234 this->drawIntoImage(surface->getCanvas());
235 return surface->newImageSnapshot();
236 }
237
238 void drawResized(SkCanvas* canvas, SkImage* image, int newW, int newH, const SkIRect* subset,
239 SkFilterQuality fq) {
240 // canvas method
241 SkPaint paint;
242 paint.setFilterQuality(fq);
243 SkRect dstR = SkRect::MakeWH(SkIntToScalar(newW), SkIntToScalar(newH));
244 SkRect srcR;
245 if (subset) {
246 srcR.set(*subset);
247 }
248 canvas->drawImageRect(image, subset ? &srcR : NULL, dstR, &paint);
249 canvas->translate(newW + 20.0f, 0);
250
251 // image method
252 SkAutoTUnref<SkImage> image2(image->newImage(newW, newH, subset, fq));
253 canvas->drawImage(image2, 0, 0, NULL);
254 canvas->translate(image2->width() + 20.0f, 0);
255 }
256
257 void drawImage(SkCanvas* canvas, SkImage* image, SkFilterQuality fq) {
258
259 canvas->drawImage(image, 0, 0, NULL);
260 canvas->translate(image->width() + 20.0f, 0);
261 this->drawResized(canvas, image, image->width()*4/10, image->height()*4/ 10, NULL, fq);
262
263 SkIRect subset = SkIRect::MakeLTRB(W/4, H/4, W/2, H/2);
264 this->drawResized(canvas, image, W, H, &subset, fq);
265 }
266
267 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
268 canvas->translate(10, 10);
269
270 SkAutoTUnref<SkImage> image(this->makeImage(canvas));
271
272 const SkFilterQuality fq[] = {
273 kNone_SkFilterQuality,
274 kLow_SkFilterQuality,
275 kMedium_SkFilterQuality,
276 kHigh_SkFilterQuality,
277 };
278 for (size_t i = 0; i < SK_ARRAY_COUNT(fq); ++i) {
279 {
280 SkAutoCanvasRestore acr(canvas, true);
281 this->drawImage(canvas, image, fq[i]);
282 }
283 canvas->translate(0, image->height() + 20.0f);
284 }
285 }
286
287 private:
288 typedef skiagm::GM INHERITED;
289 };
290 DEF_GM( return new ImageResizeGM; )
291
OLDNEW
« no previous file with comments | « gm/drawbitmaprect.cpp ('k') | include/core/SkFilterQuality.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698