summaryrefslogtreecommitdiffstats
path: root/skia/ext/image_operations.cc
diff options
context:
space:
mode:
authorbrettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-10 15:01:10 +0000
committerbrettw@google.com <brettw@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-10 15:01:10 +0000
commit53c8fdf0f2abd695a66237d901a8d74eb45bb6d0 (patch)
treec3d96f89afce15dc055b0c2b3b9f32aadc2d9e8a /skia/ext/image_operations.cc
parent6addd9e9debd24130c31cdfab55c84bca9bfdc72 (diff)
downloadchromium_src-53c8fdf0f2abd695a66237d901a8d74eb45bb6d0.zip
chromium_src-53c8fdf0f2abd695a66237d901a8d74eb45bb6d0.tar.gz
chromium_src-53c8fdf0f2abd695a66237d901a8d74eb45bb6d0.tar.bz2
Remove base dependency from image_operations and associated unittests. I also renamed the namespace from "gfx" to "skia".
This also adds back the image_operations_unittest to a project (this had gotten lost in my move). Review URL: http://codereview.chromium.org/13143 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6706 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/image_operations.cc')
-rw-r--r--skia/ext/image_operations.cc132
1 files changed, 62 insertions, 70 deletions
diff --git a/skia/ext/image_operations.cc b/skia/ext/image_operations.cc
index 5e6c3ab..fb0bd40 100644
--- a/skia/ext/image_operations.cc
+++ b/skia/ext/image_operations.cc
@@ -9,14 +9,11 @@
#include "skia/ext/image_operations.h"
-#include "base/gfx/rect.h"
-#include "base/gfx/size.h"
-#include "base/logging.h"
-#include "base/stack_container.h"
#include "SkBitmap.h"
+#include "SkRect.h"
#include "skia/ext/convolver.h"
-namespace gfx {
+namespace skia {
namespace {
@@ -62,13 +59,13 @@ float EvalLanczos(int filter_size, float x) {
class ResizeFilter {
public:
ResizeFilter(ImageOperations::ResizeMethod method,
- const Size& src_full_size,
- const Size& dest_size,
- const Rect& dest_subset);
+ int src_full_width, int src_full_height,
+ int dest_width, int dest_height,
+ const SkIRect& dest_subset);
// Returns the bounds in the input bitmap of data that is used in the output.
// The filter offsets are within this rectangle.
- const Rect& src_depend() { return src_depend_; }
+ const SkIRect& src_depend() { return src_depend_; }
// Returns the filled filter values.
const ConvolusionFilter1D& x_filter() { return x_filter_; }
@@ -87,7 +84,7 @@ class ResizeFilter {
// each direction as the size of the window = 3 for Lanczos3.
return 3.0f;
default:
- NOTREACHED();
+ SkASSERT(false);
return 1.0f;
}
}
@@ -115,7 +112,7 @@ class ResizeFilter {
case ImageOperations::RESIZE_LANCZOS3:
return EvalLanczos(3, pos);
default:
- NOTREACHED();
+ SkASSERT(false);
return 0;
}
}
@@ -123,7 +120,7 @@ class ResizeFilter {
ImageOperations::ResizeMethod method_;
// Subset of source the filters will touch.
- Rect src_depend_;
+ SkIRect src_depend_;
// Size of the filter support on one side only in the destination space.
// See GetFilterSupport.
@@ -131,40 +128,38 @@ class ResizeFilter {
float y_filter_support_;
// Subset of scaled destination bitmap to compute.
- Rect out_bounds_;
+ SkIRect out_bounds_;
ConvolusionFilter1D x_filter_;
ConvolusionFilter1D y_filter_;
-
- DISALLOW_EVIL_CONSTRUCTORS(ResizeFilter);
};
ResizeFilter::ResizeFilter(ImageOperations::ResizeMethod method,
- const Size& src_full_size,
- const Size& dest_size,
- const Rect& dest_subset)
+ int src_full_width, int src_full_height,
+ int dest_width, int dest_height,
+ const SkIRect& dest_subset)
: method_(method),
out_bounds_(dest_subset) {
- float scale_x = static_cast<float>(dest_size.width()) /
- static_cast<float>(src_full_size.width());
- float scale_y = static_cast<float>(dest_size.height()) /
- static_cast<float>(src_full_size.height());
+ float scale_x = static_cast<float>(dest_width) /
+ static_cast<float>(src_full_width);
+ float scale_y = static_cast<float>(dest_height) /
+ static_cast<float>(src_full_height);
x_filter_support_ = GetFilterSupport(scale_x);
y_filter_support_ = GetFilterSupport(scale_y);
- gfx::Rect src_full(0, 0, src_full_size.width(), src_full_size.height());
- gfx::Rect dest_full(0, 0,
- static_cast<int>(src_full_size.width() * scale_x + 0.5),
- static_cast<int>(src_full_size.height() * scale_y + 0.5));
+ SkIRect src_full = {0, 0, src_full_width, src_full_height};
+ SkIRect dest_full = {0, 0,
+ static_cast<int>(src_full_width * scale_x + 0.5),
+ static_cast<int>(src_full_height * scale_y + 0.5)};
// Support of the filter in source space.
float src_x_support = x_filter_support_ / scale_x;
float src_y_support = y_filter_support_ / scale_y;
- ComputeFilters(src_full_size.width(), dest_subset.x(), dest_subset.width(),
+ ComputeFilters(src_full_width, dest_subset.fLeft, dest_subset.width(),
scale_x, src_x_support, &x_filter_);
- ComputeFilters(src_full_size.height(), dest_subset.y(), dest_subset.height(),
+ ComputeFilters(src_full_height, dest_subset.fTop, dest_subset.height(),
scale_y, src_y_support, &y_filter_);
}
@@ -184,8 +179,10 @@ void ResizeFilter::ComputeFilters(int src_size,
// Speed up the divisions below by turning them into multiplies.
float inv_scale = 1.0f / scale;
- StackVector<float, 64> filter_values;
- StackVector<int16, 64> fixed_filter_values;
+ std::vector<float> filter_values;
+ filter_values.reserve(64);
+ std::vector<short> fixed_filter_values;
+ fixed_filter_values.reserve(64);
// Loop over all pixels in the output range. We will generate one set of
// filter values for each one. Those values will tell us how to blend the
@@ -194,8 +191,8 @@ void ResizeFilter::ComputeFilters(int src_size,
dest_subset_i++) {
// Reset the arrays. We don't declare them inside so they can re-use the
// same malloc-ed buffer.
- filter_values->clear();
- fixed_filter_values->clear();
+ filter_values.clear();
+ fixed_filter_values.clear();
// This is the pixel in the source directly under the pixel in the dest.
float src_pixel = dest_subset_i * inv_scale;
@@ -218,19 +215,19 @@ void ResizeFilter::ComputeFilters(int src_size,
// Compute the filter value at that location.
float filter_value = ComputeFilter(dest_filter_pos);
- filter_values->push_back(filter_value);
+ filter_values.push_back(filter_value);
filter_sum += filter_value;
}
- DCHECK(!filter_values->empty()) << "We should always get a filter!";
+ SkASSERT(!filter_values->empty());
// The filter must be normalized so that we don't affect the brightness of
// the image. Convert to normalized fixed point.
- int16 fixed_sum = 0;
- for (size_t i = 0; i < filter_values->size(); i++) {
- int16 cur_fixed = output->FloatToFixed(filter_values[i] / filter_sum);
+ short fixed_sum = 0;
+ for (size_t i = 0; i < filter_values.size(); i++) {
+ short cur_fixed = output->FloatToFixed(filter_values[i] / filter_sum);
fixed_sum += cur_fixed;
- fixed_filter_values->push_back(cur_fixed);
+ fixed_filter_values.push_back(cur_fixed);
}
// The conversion to fixed point will leave some rounding errors, which
@@ -238,12 +235,12 @@ void ResizeFilter::ComputeFilters(int src_size,
// arbitrarily add this to the center of the filter array (this won't always
// be the center of the filter function since it could get clipped on the
// edges, but it doesn't matter enough to worry about that case).
- int16 leftovers = output->FloatToFixed(1.0f) - fixed_sum;
- fixed_filter_values[fixed_filter_values->size() / 2] += leftovers;
+ short leftovers = output->FloatToFixed(1.0f) - fixed_sum;
+ fixed_filter_values[fixed_filter_values.size() / 2] += leftovers;
// Now it's ready to go.
output->AddFilter(src_begin, &fixed_filter_values[0],
- static_cast<int>(fixed_filter_values->size()));
+ static_cast<int>(fixed_filter_values.size()));
}
}
@@ -254,27 +251,24 @@ void ResizeFilter::ComputeFilters(int src_size,
// static
SkBitmap ImageOperations::Resize(const SkBitmap& source,
ResizeMethod method,
- const Size& dest_size,
- const Rect& dest_subset) {
- DCHECK(Rect(dest_size.width(), dest_size.height()).Contains(dest_subset)) <<
- "The supplied subset does not fall within the destination image.";
-
+ int dest_width, int dest_height,
+ const SkIRect& dest_subset) {
// If the size of source or destination is 0, i.e. 0x0, 0xN or Nx0, just
// return empty
if (source.width() < 1 || source.height() < 1 ||
- dest_size.width() < 1 || dest_size.height() < 1)
- return SkBitmap();
+ dest_width < 1 || dest_height < 1)
+ return SkBitmap();
SkAutoLockPixels locker(source);
- ResizeFilter filter(method, Size(source.width(), source.height()),
- dest_size, dest_subset);
+ ResizeFilter filter(method, source.width(), source.height(),
+ dest_width, dest_height, dest_subset);
// Get a source bitmap encompassing this touched area. We construct the
// offsets and row strides such that it looks like a new bitmap, while
// referring to the old data.
- const uint8* source_subset =
- reinterpret_cast<const uint8*>(source.getPixels());
+ const unsigned char* source_subset =
+ reinterpret_cast<const unsigned char*>(source.getPixels());
// Convolve into the result.
SkBitmap result;
@@ -294,29 +288,28 @@ SkBitmap ImageOperations::Resize(const SkBitmap& source,
// static
SkBitmap ImageOperations::Resize(const SkBitmap& source,
ResizeMethod method,
- const Size& dest_size) {
- Rect dest_subset(0, 0, dest_size.width(), dest_size.height());
- return Resize(source, method, dest_size, dest_subset);
+ int dest_width, int dest_height) {
+ SkIRect dest_subset = {0, 0, dest_width, dest_height};
+ return Resize(source, method, dest_width, dest_height, dest_subset);
}
// static
SkBitmap ImageOperations::CreateBlendedBitmap(const SkBitmap& first,
const SkBitmap& second,
double alpha) {
- DCHECK(alpha <= 1 && alpha >= 0);
- DCHECK(first.width() == second.width());
- DCHECK(first.height() == second.height());
- DCHECK(first.bytesPerPixel() == second.bytesPerPixel());
- DCHECK(first.config() == SkBitmap::kARGB_8888_Config);
+ SkASSERT(alpha <= 1 && alpha >= 0);
+ SkASSERT(first.width() == second.width());
+ SkASSERT(first.height() == second.height());
+ SkASSERT(first.bytesPerPixel() == second.bytesPerPixel());
+ SkASSERT(first.config() == SkBitmap::kARGB_8888_Config);
// Optimize for case where we won't need to blend anything.
static const double alpha_min = 1.0 / 255;
static const double alpha_max = 254.0 / 255;
- if (alpha < alpha_min) {
+ if (alpha < alpha_min)
return first;
- } else if (alpha > alpha_max) {
+ else if (alpha > alpha_max)
return second;
- }
SkAutoLockPixels lock_first(first);
SkAutoLockPixels lock_second(second);
@@ -330,13 +323,13 @@ SkBitmap ImageOperations::CreateBlendedBitmap(const SkBitmap& first,
double first_alpha = 1 - alpha;
for (int y = 0; y < first.height(); y++) {
- uint32* first_row = first.getAddr32(0, y);
- uint32* second_row = second.getAddr32(0, y);
- uint32* dst_row = blended.getAddr32(0, y);
+ uint32_t* first_row = first.getAddr32(0, y);
+ uint32_t* second_row = second.getAddr32(0, y);
+ uint32_t* dst_row = blended.getAddr32(0, y);
for (int x = 0; x < first.width(); x++) {
- uint32 first_pixel = first_row[x];
- uint32 second_pixel = second_row[x];
+ uint32_t first_pixel = first_row[x];
+ uint32_t second_pixel = second_row[x];
int a = static_cast<int>(
SkColorGetA(first_pixel) * first_alpha +
@@ -358,5 +351,4 @@ SkBitmap ImageOperations::CreateBlendedBitmap(const SkBitmap& first,
return blended;
}
-} // namespace gfx
-
+} // namespace skia