OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if !defined(OS_WIN) | 7 #if !defined(OS_WIN) |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 EXPECT_TRUE(compressed.size()); | 89 EXPECT_TRUE(compressed.size()); |
90 | 90 |
91 SkBitmap bitmap; | 91 SkBitmap bitmap; |
92 EXPECT_TRUE(gfx::PNGCodec::Decode( | 92 EXPECT_TRUE(gfx::PNGCodec::Decode( |
93 reinterpret_cast<const unsigned char*>(compressed.data()), | 93 reinterpret_cast<const unsigned char*>(compressed.data()), |
94 compressed.size(), &bitmap)); | 94 compressed.size(), &bitmap)); |
95 SetSkBitmap(bitmap); | 95 SetSkBitmap(bitmap); |
96 } | 96 } |
97 | 97 |
98 // Loads the image from a canvas. | 98 // Loads the image from a canvas. |
99 Image(const skia::PlatformCanvas& canvas) : ignore_alpha_(true) { | 99 Image(skia::PlatformCanvas& canvas) : ignore_alpha_(true) { |
100 // Use a different way to access the bitmap. The normal way would be to | 100 // Use a different way to access the bitmap. The normal way would be to |
101 // query the SkBitmap. | 101 // query the SkBitmap. |
102 HDC context = canvas.beginPlatformPaint(); | 102 skia::ScopedPlatformPaint scoped_platform_paint(&canvas); |
| 103 HDC context = scoped_platform_paint.GetPlatformSurface(); |
103 HGDIOBJ bitmap = GetCurrentObject(context, OBJ_BITMAP); | 104 HGDIOBJ bitmap = GetCurrentObject(context, OBJ_BITMAP); |
104 EXPECT_TRUE(bitmap != NULL); | 105 EXPECT_TRUE(bitmap != NULL); |
105 // Initialize the clip region to the entire bitmap. | 106 // Initialize the clip region to the entire bitmap. |
106 BITMAP bitmap_data; | 107 BITMAP bitmap_data; |
107 EXPECT_EQ(GetObject(bitmap, sizeof(BITMAP), &bitmap_data), sizeof(BITMAP)); | 108 EXPECT_EQ(GetObject(bitmap, sizeof(BITMAP), &bitmap_data), sizeof(BITMAP)); |
108 width_ = bitmap_data.bmWidth; | 109 width_ = bitmap_data.bmWidth; |
109 height_ = bitmap_data.bmHeight; | 110 height_ = bitmap_data.bmHeight; |
110 row_length_ = bitmap_data.bmWidthBytes; | 111 row_length_ = bitmap_data.bmWidthBytes; |
111 size_t size = row_length_ * height_; | 112 size_t size = row_length_ * height_; |
112 data_.resize(size); | 113 data_.resize(size); |
113 memcpy(&*data_.begin(), bitmap_data.bmBits, size); | 114 memcpy(&*data_.begin(), bitmap_data.bmBits, size); |
114 canvas.endPlatformPaint(); | |
115 } | 115 } |
116 | 116 |
117 // Loads the image from a canvas. | 117 // Loads the image from a canvas. |
118 Image(const SkBitmap& bitmap) : ignore_alpha_(true) { | 118 Image(const SkBitmap& bitmap) : ignore_alpha_(true) { |
119 SetSkBitmap(bitmap); | 119 SetSkBitmap(bitmap); |
120 } | 120 } |
121 | 121 |
122 int width() const { return width_; } | 122 int width() const { return width_; } |
123 int height() const { return height_; } | 123 int height() const { return height_; } |
124 int row_length() const { return row_length_; } | 124 int row_length() const { return row_length_; } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 for (size_t i = 0; i < tmp.size(); ++i) | 260 for (size_t i = 0; i < tmp.size(); ++i) |
261 tmp[i] = base::ToLowerASCII(tmp[i]); | 261 tmp[i] = base::ToLowerASCII(tmp[i]); |
262 | 262 |
263 return test_dir_.AppendASCII(tmp); | 263 return test_dir_.AppendASCII(tmp); |
264 } | 264 } |
265 | 265 |
266 // Compares or saves the bitmap currently loaded in the context, depending on | 266 // Compares or saves the bitmap currently loaded in the context, depending on |
267 // kGenerating value. Returns 0 on success or any positive value between ]0, | 267 // kGenerating value. Returns 0 on success or any positive value between ]0, |
268 // 100] on failure. The return value is the percentage of difference between | 268 // 100] on failure. The return value is the percentage of difference between |
269 // the image in the file and the image in the canvas. | 269 // the image in the file and the image in the canvas. |
270 double ProcessCanvas(const skia::PlatformCanvas& canvas, | 270 double ProcessCanvas(skia::PlatformCanvas& canvas, |
271 FilePath::StringType filename) const { | 271 FilePath::StringType filename) const { |
272 filename = filename + FILE_PATH_LITERAL(".png"); | 272 filename = filename + FILE_PATH_LITERAL(".png"); |
273 switch (action_) { | 273 switch (action_) { |
274 case GENERATE: | 274 case GENERATE: |
275 SaveImage(canvas, filename); | 275 SaveImage(canvas, filename); |
276 return 0.; | 276 return 0.; |
277 case COMPARE: | 277 case COMPARE: |
278 return CompareImage(canvas, filename); | 278 return CompareImage(canvas, filename); |
279 case NOOP: | 279 case NOOP: |
280 return 0; | 280 return 0; |
281 default: | 281 default: |
282 // Invalid state, returns that the image is 100 different. | 282 // Invalid state, returns that the image is 100 different. |
283 return 100.; | 283 return 100.; |
284 } | 284 } |
285 } | 285 } |
286 | 286 |
287 // Compares the bitmap currently loaded in the context with the file. Returns | 287 // Compares the bitmap currently loaded in the context with the file. Returns |
288 // the percentage of pixel difference between both images, between 0 and 100. | 288 // the percentage of pixel difference between both images, between 0 and 100. |
289 double CompareImage(const skia::PlatformCanvas& canvas, | 289 double CompareImage(skia::PlatformCanvas& canvas, |
290 const FilePath::StringType& filename) const { | 290 const FilePath::StringType& filename) const { |
291 Image image1(canvas); | 291 Image image1(canvas); |
292 Image image2(test_file(filename)); | 292 Image image2(test_file(filename)); |
293 double diff = image1.PercentageDifferent(image2); | 293 double diff = image1.PercentageDifferent(image2); |
294 return diff; | 294 return diff; |
295 } | 295 } |
296 | 296 |
297 // Saves the bitmap currently loaded in the context into the file. | 297 // Saves the bitmap currently loaded in the context into the file. |
298 void SaveImage(const skia::PlatformCanvas& canvas, | 298 void SaveImage(skia::PlatformCanvas& canvas, |
299 const FilePath::StringType& filename) const { | 299 const FilePath::StringType& filename) const { |
300 Image(canvas).SaveToFile(test_file(filename)); | 300 Image(canvas).SaveToFile(test_file(filename)); |
301 } | 301 } |
302 | 302 |
303 ProcessAction action_; | 303 ProcessAction action_; |
304 | 304 |
305 // Path to directory used to contain the test data. | 305 // Path to directory used to contain the test data. |
306 FilePath test_dir_; | 306 FilePath test_dir_; |
307 | 307 |
308 DISALLOW_COPY_AND_ASSIGN(ImageTest); | 308 DISALLOW_COPY_AND_ASSIGN(ImageTest); |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
970 { | 970 { |
971 vcanvas_->rotate(67); | 971 vcanvas_->rotate(67); |
972 pcanvas_->rotate(67); | 972 pcanvas_->rotate(67); |
973 vcanvas_->drawBitmap(bitmap, 20, -50, NULL); | 973 vcanvas_->drawBitmap(bitmap, 20, -50, NULL); |
974 pcanvas_->drawBitmap(bitmap, 20, -50, NULL); | 974 pcanvas_->drawBitmap(bitmap, 20, -50, NULL); |
975 EXPECT_EQ(0., ProcessImage(FILE_PATH_LITERAL("rotate"))); | 975 EXPECT_EQ(0., ProcessImage(FILE_PATH_LITERAL("rotate"))); |
976 } | 976 } |
977 } | 977 } |
978 | 978 |
979 } // namespace skia | 979 } // namespace skia |
OLD | NEW |