| OLD | NEW |
| 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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkColorPriv.h" |
| 10 #include "SkImagePriv.h" |
| 9 #include "SkMagnifierImageFilter.h" | 11 #include "SkMagnifierImageFilter.h" |
| 10 #include "SkColorPriv.h" | |
| 11 #include "SkReadBuffer.h" | 12 #include "SkReadBuffer.h" |
| 13 #include "SkValidationUtils.h" |
| 12 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
| 13 #include "SkValidationUtils.h" | |
| 14 | 15 |
| 15 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
| 16 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 17 #include "GrInvariantOutput.h" | 18 #include "GrInvariantOutput.h" |
| 18 #include "effects/GrSingleTextureEffect.h" | 19 #include "effects/GrSingleTextureEffect.h" |
| 19 #include "gl/GrGLProcessor.h" | 20 #include "gl/GrGLProcessor.h" |
| 20 #include "gl/GrGLSL.h" | 21 #include "gl/GrGLSL.h" |
| 21 #include "gl/GrGLTexture.h" | 22 #include "gl/GrGLTexture.h" |
| 22 #include "gl/builders/GrGLProgramBuilder.h" | 23 #include "gl/builders/GrGLProgramBuilder.h" |
| 23 | 24 |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 buffer.readRect(&src); | 282 buffer.readRect(&src); |
| 282 return Create(src, buffer.readScalar(), common.getInput(0)); | 283 return Create(src, buffer.readScalar(), common.getInput(0)); |
| 283 } | 284 } |
| 284 | 285 |
| 285 void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const { | 286 void SkMagnifierImageFilter::flatten(SkWriteBuffer& buffer) const { |
| 286 this->INHERITED::flatten(buffer); | 287 this->INHERITED::flatten(buffer); |
| 287 buffer.writeRect(fSrcRect); | 288 buffer.writeRect(fSrcRect); |
| 288 buffer.writeScalar(fInset); | 289 buffer.writeScalar(fInset); |
| 289 } | 290 } |
| 290 | 291 |
| 291 bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkBitmap& src, | 292 bool SkMagnifierImageFilter::onFilterImage(Proxy*, const SkImage* src, |
| 292 const Context&, SkBitmap* dst, | 293 const Context&, SkAutoTUnref<const Sk
Image>& dst, |
| 293 SkIPoint* offset) const { | 294 SkIPoint* offset) const { |
| 294 if ((src.colorType() != kN32_SkColorType) || | 295 if ((fSrcRect.width() >= src->width()) || |
| 295 (fSrcRect.width() >= src.width()) || | 296 (fSrcRect.height() >= src->height())) { |
| 296 (fSrcRect.height() >= src.height())) { | 297 return false; |
| 298 } |
| 299 SkBitmap srcBitmap; |
| 300 SkAutoImageAsN32Bitmap aai(src, &srcBitmap); |
| 301 if (!srcBitmap.getPixels() || srcBitmap.width() <= 0 || srcBitmap.height() <
= 0) { |
| 297 return false; | 302 return false; |
| 298 } | 303 } |
| 299 | 304 |
| 300 SkAutoLockPixels alp(src); | 305 SkBitmap dstBitmap; |
| 301 SkASSERT(src.getPixels()); | 306 if (!dstBitmap.tryAllocPixels(srcBitmap.info())) { |
| 302 if (!src.getPixels() || src.width() <= 0 || src.height() <= 0) { | |
| 303 return false; | |
| 304 } | |
| 305 | |
| 306 if (!dst->tryAllocPixels(src.info())) { | |
| 307 return false; | 307 return false; |
| 308 } | 308 } |
| 309 | 309 |
| 310 SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; | 310 SkScalar inv_inset = fInset > 0 ? SkScalarInvert(fInset) : SK_Scalar1; |
| 311 | 311 |
| 312 SkScalar inv_x_zoom = fSrcRect.width() / src.width(); | 312 SkScalar inv_x_zoom = fSrcRect.width() / srcBitmap.width(); |
| 313 SkScalar inv_y_zoom = fSrcRect.height() / src.height(); | 313 SkScalar inv_y_zoom = fSrcRect.height() / srcBitmap.height(); |
| 314 | 314 |
| 315 SkColor* sptr = src.getAddr32(0, 0); | 315 SkColor* sptr = srcBitmap.getAddr32(0, 0); |
| 316 SkColor* dptr = dst->getAddr32(0, 0); | 316 SkColor* dptr = dstBitmap.getAddr32(0, 0); |
| 317 int width = src.width(), height = src.height(); | 317 int width = srcBitmap.width(), height = srcBitmap.height(); |
| 318 for (int y = 0; y < height; ++y) { | 318 for (int y = 0; y < height; ++y) { |
| 319 for (int x = 0; x < width; ++x) { | 319 for (int x = 0; x < width; ++x) { |
| 320 SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset; | 320 SkScalar x_dist = SkMin32(x, width - x - 1) * inv_inset; |
| 321 SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset; | 321 SkScalar y_dist = SkMin32(y, height - y - 1) * inv_inset; |
| 322 SkScalar weight = 0; | 322 SkScalar weight = 0; |
| 323 | 323 |
| 324 static const SkScalar kScalar2 = SkScalar(2); | 324 static const SkScalar kScalar2 = SkScalar(2); |
| 325 | 325 |
| 326 // To create a smooth curve at the corners, we need to work on | 326 // To create a smooth curve at the corners, we need to work on |
| 327 // a square twice the size of the inset. | 327 // a square twice the size of the inset. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 344 SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zo
om)) + | 344 SkScalar y_interp = SkScalarMul(weight, (fSrcRect.y() + y * inv_y_zo
om)) + |
| 345 (SK_Scalar1 - weight) * y; | 345 (SK_Scalar1 - weight) * y; |
| 346 | 346 |
| 347 int x_val = SkPin32(SkScalarFloorToInt(x_interp), 0, width - 1); | 347 int x_val = SkPin32(SkScalarFloorToInt(x_interp), 0, width - 1); |
| 348 int y_val = SkPin32(SkScalarFloorToInt(y_interp), 0, height - 1); | 348 int y_val = SkPin32(SkScalarFloorToInt(y_interp), 0, height - 1); |
| 349 | 349 |
| 350 *dptr = sptr[y_val * width + x_val]; | 350 *dptr = sptr[y_val * width + x_val]; |
| 351 dptr++; | 351 dptr++; |
| 352 } | 352 } |
| 353 } | 353 } |
| 354 srcBitmap = SkBitmap(); |
| 355 SkImage* image = SkNewImageFromBitmap(dstBitmap, NULL); |
| 356 if (NULL == image) { |
| 357 return false; |
| 358 } |
| 359 dst.reset(image); |
| 360 offset->fX = 0; |
| 361 offset->fY = 0; |
| 354 return true; | 362 return true; |
| 355 } | 363 } |
| 356 | 364 |
| 357 #ifndef SK_IGNORE_TO_STRING | 365 #ifndef SK_IGNORE_TO_STRING |
| 358 void SkMagnifierImageFilter::toString(SkString* str) const { | 366 void SkMagnifierImageFilter::toString(SkString* str) const { |
| 359 str->appendf("SkMagnifierImageFilter: ("); | 367 str->appendf("SkMagnifierImageFilter: ("); |
| 360 str->appendf("src: (%f,%f,%f,%f) ", | 368 str->appendf("src: (%f,%f,%f,%f) ", |
| 361 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m); | 369 fSrcRect.fLeft, fSrcRect.fTop, fSrcRect.fRight, fSrcRect.fBotto
m); |
| 362 str->appendf("inset: %f", fInset); | 370 str->appendf("inset: %f", fInset); |
| 363 str->append(")"); | 371 str->append(")"); |
| 364 } | 372 } |
| 365 #endif | 373 #endif |
| OLD | NEW |