| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <stdlib.h> | |
| 6 | |
| 7 #include "base/gfx/image_operations.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 #include "SkBitmap.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // Computes the average pixel value for the given range, inclusive. | |
| 14 uint32_t AveragePixel(const SkBitmap& bmp, | |
| 15 int x_min, int x_max, | |
| 16 int y_min, int y_max) { | |
| 17 float accum[4] = {0, 0, 0, 0}; | |
| 18 int count = 0; | |
| 19 for (int y = y_min; y <= y_max; y++) { | |
| 20 for (int x = x_min; x <= x_max; x++) { | |
| 21 uint32_t cur = *bmp.getAddr32(x, y); | |
| 22 accum[0] += SkColorGetB(cur); | |
| 23 accum[1] += SkColorGetG(cur); | |
| 24 accum[2] += SkColorGetR(cur); | |
| 25 accum[3] += SkColorGetA(cur); | |
| 26 count++; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 return SkColorSetARGB(static_cast<unsigned char>(accum[3] / count), | |
| 31 static_cast<unsigned char>(accum[2] / count), | |
| 32 static_cast<unsigned char>(accum[1] / count), | |
| 33 static_cast<unsigned char>(accum[0] / count)); | |
| 34 } | |
| 35 | |
| 36 // Returns true if each channel of the given two colors are "close." This is | |
| 37 // used for comparing colors where rounding errors may cause off-by-one. | |
| 38 bool ColorsClose(uint32_t a, uint32_t b) { | |
| 39 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && | |
| 40 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && | |
| 41 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 && | |
| 42 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; | |
| 43 } | |
| 44 | |
| 45 void FillDataToBitmap(int w, int h, SkBitmap* bmp) { | |
| 46 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
| 47 bmp->allocPixels(); | |
| 48 | |
| 49 unsigned char* src_data = | |
| 50 reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0)); | |
| 51 for (int i = 0; i < w * h; i++) { | |
| 52 src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255); | |
| 53 src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255); | |
| 54 src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255); | |
| 55 src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 // Makes the bitmap 50% the size as the original using a box filter. This is | |
| 62 // an easy operation that we can check the results for manually. | |
| 63 TEST(ImageOperations, Halve) { | |
| 64 // Make our source bitmap. | |
| 65 int src_w = 30, src_h = 38; | |
| 66 SkBitmap src; | |
| 67 FillDataToBitmap(src_w, src_h, &src); | |
| 68 | |
| 69 // Do a halving of the full bitmap. | |
| 70 SkBitmap actual_results = gfx::ImageOperations::Resize( | |
| 71 src, gfx::ImageOperations::RESIZE_BOX, gfx::Size(src_w / 2, src_h / 2)); | |
| 72 ASSERT_EQ(src_w / 2, actual_results.width()); | |
| 73 ASSERT_EQ(src_h / 2, actual_results.height()); | |
| 74 | |
| 75 // Compute the expected values & compare. | |
| 76 SkAutoLockPixels lock(actual_results); | |
| 77 for (int y = 0; y < actual_results.height(); y++) { | |
| 78 for (int x = 0; x < actual_results.width(); x++) { | |
| 79 int first_x = std::max(0, x * 2 - 1); | |
| 80 int last_x = std::min(src_w - 1, x * 2); | |
| 81 | |
| 82 int first_y = std::max(0, y * 2 - 1); | |
| 83 int last_y = std::min(src_h - 1, y * 2); | |
| 84 | |
| 85 uint32_t expected_color = AveragePixel(src, | |
| 86 first_x, last_x, first_y, last_y); | |
| 87 EXPECT_TRUE(ColorsClose(expected_color, *actual_results.getAddr32(x, y))); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 TEST(ImageOperations, HalveSubset) { | |
| 93 // Make our source bitmap. | |
| 94 int src_w = 16, src_h = 34; | |
| 95 SkBitmap src; | |
| 96 FillDataToBitmap(src_w, src_h, &src); | |
| 97 | |
| 98 // Do a halving of the full bitmap. | |
| 99 SkBitmap full_results = gfx::ImageOperations::Resize( | |
| 100 src, gfx::ImageOperations::RESIZE_BOX, gfx::Size(src_w / 2, src_h / 2)); | |
| 101 ASSERT_EQ(src_w / 2, full_results.width()); | |
| 102 ASSERT_EQ(src_h / 2, full_results.height()); | |
| 103 | |
| 104 // Now do a halving of a a subset, recall the destination subset is in the | |
| 105 // destination coordinate system (max = half of the original image size). | |
| 106 gfx::Rect subset_rect(2, 3, 3, 6); | |
| 107 SkBitmap subset_results = gfx::ImageOperations::Resize( | |
| 108 src, gfx::ImageOperations::RESIZE_BOX, | |
| 109 gfx::Size(src_w / 2, src_h / 2), subset_rect); | |
| 110 ASSERT_EQ(subset_rect.width(), subset_results.width()); | |
| 111 ASSERT_EQ(subset_rect.height(), subset_results.height()); | |
| 112 | |
| 113 // The computed subset and the corresponding subset of the original image | |
| 114 // should be the same. | |
| 115 SkAutoLockPixels full_lock(full_results); | |
| 116 SkAutoLockPixels subset_lock(subset_results); | |
| 117 for (int y = 0; y < subset_rect.height(); y++) { | |
| 118 for (int x = 0; x < subset_rect.width(); x++) { | |
| 119 ASSERT_EQ( | |
| 120 *full_results.getAddr32(x + subset_rect.x(), y + subset_rect.y()), | |
| 121 *subset_results.getAddr32(x, y)); | |
| 122 } | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 // Resamples an iamge to the same image, it should give almost the same result. | |
| 127 TEST(ImageOperations, ResampleToSame) { | |
| 128 // Make our source bitmap. | |
| 129 int src_w = 16, src_h = 34; | |
| 130 SkBitmap src; | |
| 131 FillDataToBitmap(src_w, src_h, &src); | |
| 132 | |
| 133 // Do a resize of the full bitmap to the same size. The lanczos filter is good | |
| 134 // enough that we should get exactly the same image for output. | |
| 135 SkBitmap results = gfx::ImageOperations::Resize( | |
| 136 src, gfx::ImageOperations::RESIZE_LANCZOS3, gfx::Size(src_w, src_h)); | |
| 137 ASSERT_EQ(src_w, results.width()); | |
| 138 ASSERT_EQ(src_h, results.height()); | |
| 139 | |
| 140 SkAutoLockPixels src_lock(src); | |
| 141 SkAutoLockPixels results_lock(results); | |
| 142 for (int y = 0; y < src_h; y++) { | |
| 143 for (int x = 0; x < src_w; x++) { | |
| 144 EXPECT_EQ(*src.getAddr32(x, y), *results.getAddr32(x, y)); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 | |
| OLD | NEW |