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

Side by Side Diff: src/image/SkImage.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 | « src/core/SkPaint.cpp ('k') | src/image/SkImage_Base.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 2012 Google Inc. 2 * Copyright 2012 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 "SkBitmap.h" 8 #include "SkBitmap.h"
9 #include "SkCanvas.h" 9 #include "SkCanvas.h"
10 #include "SkImageGenerator.h" 10 #include "SkImageGenerator.h"
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 } 87 }
88 return as_IB(this)->onNewSurface(info, *props); 88 return as_IB(this)->onNewSurface(info, *props);
89 } 89 }
90 90
91 const char* SkImage::toString(SkString* str) const { 91 const char* SkImage::toString(SkString* str) const {
92 str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(), 92 str->appendf("image: (id:%d (%d, %d) %s)", this->uniqueID(), this->width(), this->height(),
93 this->isOpaque() ? "opaque" : ""); 93 this->isOpaque() ? "opaque" : "");
94 return str->c_str(); 94 return str->c_str();
95 } 95 }
96 96
97 SkImage* SkImage::newImage(int newWidth, int newHeight, const SkIRect* subset,
98 SkFilterQuality quality) const {
99 if (newWidth <= 0 || newHeight <= 0) {
100 return NULL;
101 }
102
103 const SkIRect bounds = SkIRect::MakeWH(this->width(), this->height());
104
105 if (subset) {
106 if (!bounds.contains(*subset)) {
107 return NULL;
108 }
109 if (bounds == *subset) {
110 subset = NULL; // and fall through to check below
111 }
112 }
113
114 if (NULL == subset && this->width() == newWidth && this->height() == newHeig ht) {
115 return SkRef(const_cast<SkImage*>(this));
116 }
117
118 return as_IB(this)->onNewImage(newWidth, newHeight, subset, quality);
119 }
120
97 /////////////////////////////////////////////////////////////////////////////// 121 ///////////////////////////////////////////////////////////////////////////////
98 122
99 static bool raster_canvas_supports(const SkImageInfo& info) { 123 static bool raster_canvas_supports(const SkImageInfo& info) {
100 switch (info.colorType()) { 124 switch (info.colorType()) {
101 case kN32_SkColorType: 125 case kN32_SkColorType:
102 return kUnpremul_SkAlphaType != info.alphaType(); 126 return kUnpremul_SkAlphaType != info.alphaType();
103 case kRGB_565_SkColorType: 127 case kRGB_565_SkColorType:
104 return true; 128 return true;
105 case kAlpha_8_SkColorType: 129 case kAlpha_8_SkColorType:
106 return true; 130 return true;
(...skipping 12 matching lines...) Expand all
119 SkBitmap bm; 143 SkBitmap bm;
120 bm.installPixels(dstInfo, dstPixels, dstRowBytes); 144 bm.installPixels(dstInfo, dstPixels, dstRowBytes);
121 SkCanvas canvas(bm); 145 SkCanvas canvas(bm);
122 146
123 SkPaint paint; 147 SkPaint paint;
124 paint.setXfermodeMode(SkXfermode::kSrc_Mode); 148 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
125 canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint); 149 canvas.drawImage(this, -SkIntToScalar(srcX), -SkIntToScalar(srcY), &paint);
126 150
127 return true; 151 return true;
128 } 152 }
153
154 SkImage* SkImage_Base::onNewImage(int newWidth, int newHeight, const SkIRect* su bset,
155 SkFilterQuality quality) const {
156 const bool opaque = this->isOpaque();
157 const SkImageInfo info = SkImageInfo::Make(newWidth, newHeight, kN32_SkColor Type,
158 opaque ? kOpaque_SkAlphaType : kP remul_SkAlphaType);
159 SkAutoTUnref<SkSurface> surface(this->newSurface(info, NULL));
160 if (!surface.get()) {
161 return NULL;
162 }
163
164 SkRect src;
165 if (subset) {
166 src.set(*subset);
167 } else {
168 src = SkRect::MakeIWH(this->width(), this->height());
169 }
170
171 surface->getCanvas()->scale(newWidth / src.width(), newHeight / src.height() );
172 surface->getCanvas()->translate(-src.x(), -src.y());
173
174 SkPaint paint;
175 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
176 paint.setFilterQuality(quality);
177 surface->getCanvas()->drawImage(this, 0, 0, &paint);
178 return surface->newImageSnapshot();
179 }
180
181
OLDNEW
« no previous file with comments | « src/core/SkPaint.cpp ('k') | src/image/SkImage_Base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698