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

Side by Side Diff: src/effects/SkBitmapSource.cpp

Issue 106933002: Implement srcRect and dstRect functionality in SkBitmapSource. This is required for the "preserveAs… (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Fix nits Created 7 years 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 | « include/effects/SkBitmapSource.h ('k') | src/effects/SkTileImageFilter.cpp » ('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 2012 The Android Open Source Project 2 * Copyright 2012 The Android Open Source Project
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 "SkBitmapSource.h" 8 #include "SkBitmapSource.h"
9 #include "SkDevice.h"
10 #include "SkCanvas.h"
11 #include "SkFlattenableBuffers.h"
12 #include "SkValidationUtils.h"
9 13
10 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap) 14 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap)
11 : INHERITED(0, 0), 15 : INHERITED(0, 0),
12 fBitmap(bitmap) { 16 fBitmap(bitmap),
17 fSrcRect(SkRect::MakeWH(SkIntToScalar(bitmap.width()),
18 SkIntToScalar(bitmap.height()))),
19 fDstRect(fSrcRect) {
20 }
21
22 SkBitmapSource::SkBitmapSource(const SkBitmap& bitmap, const SkRect& srcRect, co nst SkRect& dstRect)
23 : INHERITED(0, 0),
24 fBitmap(bitmap),
25 fSrcRect(srcRect),
26 fDstRect(dstRect) {
13 } 27 }
14 28
15 SkBitmapSource::SkBitmapSource(SkFlattenableReadBuffer& buffer) 29 SkBitmapSource::SkBitmapSource(SkFlattenableReadBuffer& buffer)
16 : INHERITED(0, buffer) { 30 : INHERITED(0, buffer) {
17 fBitmap.unflatten(buffer); 31 fBitmap.unflatten(buffer);
32 buffer.readRect(&fSrcRect);
33 buffer.readRect(&fDstRect);
34 buffer.validate(SkIsValidRect(fSrcRect) && SkIsValidRect(fDstRect));
18 } 35 }
19 36
20 void SkBitmapSource::flatten(SkFlattenableWriteBuffer& buffer) const { 37 void SkBitmapSource::flatten(SkFlattenableWriteBuffer& buffer) const {
21 this->INHERITED::flatten(buffer); 38 this->INHERITED::flatten(buffer);
22 fBitmap.flatten(buffer); 39 fBitmap.flatten(buffer);
40 buffer.writeRect(fSrcRect);
41 buffer.writeRect(fDstRect);
23 } 42 }
24 43
25 bool SkBitmapSource::onFilterImage(Proxy*, const SkBitmap&, const SkMatrix&, 44 bool SkBitmapSource::onFilterImage(Proxy* proxy, const SkBitmap&, const SkMatrix & matrix,
26 SkBitmap* result, SkIPoint* offset) { 45 SkBitmap* result, SkIPoint* offset) {
27 *result = fBitmap; 46 SkRect bounds, dstRect;
47 fBitmap.getBounds(&bounds);
48 matrix.mapRect(&dstRect, fDstRect);
49 if (fSrcRect == bounds && dstRect == bounds) {
50 // No regions cropped out or resized; return entire bitmap.
51 *result = fBitmap;
52 return true;
53 }
54 SkIRect dstIRect;
55 dstRect.roundOut(&dstIRect);
56
57 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstIRect.width(), dstI Rect.height()));
58 if (NULL == device.get()) {
59 return false;
60 }
61
62 SkCanvas canvas(device.get());
63 SkPaint paint;
64
65 // Subtract off the integer component of the translation (will be applied in loc, below).
66 dstRect.offset(-SkIntToScalar(dstIRect.fLeft), -SkIntToScalar(dstIRect.fTop) );
67 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
68 // FIXME: this probably shouldn't be necessary, but drawBitmapRectToRect ass erts
69 // None filtering when it's translate-only
70 paint.setFilterLevel(
71 fSrcRect.width() == dstRect.width() && fSrcRect.height() == dstRect.heig ht() ?
72 SkPaint::kNone_FilterLevel : SkPaint::kMedium_FilterLevel);
73 canvas.drawBitmapRectToRect(fBitmap, &fSrcRect, dstRect, &paint);
74
75 *result = device.get()->accessBitmap(false);
76 offset->fX += dstIRect.fLeft;
77 offset->fY += dstIRect.fTop;
28 return true; 78 return true;
29 } 79 }
OLDNEW
« no previous file with comments | « include/effects/SkBitmapSource.h ('k') | src/effects/SkTileImageFilter.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698