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

Side by Side Diff: tools/imagediff/image_diff.cc

Issue 21202003: Move simple png encoding for image_diff into tools/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix android_webview third_party_whitelist.txt Created 7 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « tools/imagediff/DEPS ('k') | tools/imagediff/image_diff.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // This file input format is based loosely on 5 // This file input format is based loosely on
6 // Tools/DumpRenderTree/ImageDiff.m 6 // Tools/DumpRenderTree/ImageDiff.m
7 7
8 // The exact format of this tool's output to stdout is important, to match 8 // The exact format of this tool's output to stdout is important, to match
9 // what the run-webkit-tests script expects. 9 // what the run-webkit-tests script expects.
10 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <iostream> 12 #include <iostream>
13 #include <string> 13 #include <string>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/basictypes.h" 16 #include "base/basictypes.h"
17 #include "base/command_line.h" 17 #include "base/command_line.h"
18 #include "base/file_util.h" 18 #include "base/file_util.h"
19 #include "base/files/file_path.h" 19 #include "base/files/file_path.h"
20 #include "base/logging.h" 20 #include "base/logging.h"
21 #include "base/memory/scoped_ptr.h" 21 #include "base/memory/scoped_ptr.h"
22 #include "base/process/memory.h" 22 #include "base/process/memory.h"
23 #include "base/safe_numerics.h" 23 #include "base/safe_numerics.h"
24 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
25 #include "base/strings/utf_string_conversions.h" 25 #include "base/strings/utf_string_conversions.h"
26 #include "webkit/support/webkit_support_gfx.h" 26 #include "tools/imagediff/image_diff_png.h"
27 27
28 #if defined(OS_WIN) 28 #if defined(OS_WIN)
29 #include "windows.h" 29 #include "windows.h"
30 #endif 30 #endif
31 31
32 // Causes the app to remain open, waiting for pairs of filenames on stdin. 32 // Causes the app to remain open, waiting for pairs of filenames on stdin.
33 // The caller is then responsible for terminating this app. 33 // The caller is then responsible for terminating this app.
34 static const char kOptionPollStdin[] = "use-stdin"; 34 static const char kOptionPollStdin[] = "use-stdin";
35 static const char kOptionGenerateDiff[] = "diff"; 35 static const char kOptionGenerateDiff[] = "diff";
36 36
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // Creates the image from stdin with the given data length. On success, it 73 // Creates the image from stdin with the given data length. On success, it
74 // will return true. On failure, no other methods should be accessed. 74 // will return true. On failure, no other methods should be accessed.
75 bool CreateFromStdin(size_t byte_length) { 75 bool CreateFromStdin(size_t byte_length) {
76 if (byte_length == 0) 76 if (byte_length == 0)
77 return false; 77 return false;
78 78
79 scoped_ptr<unsigned char[]> source(new unsigned char[byte_length]); 79 scoped_ptr<unsigned char[]> source(new unsigned char[byte_length]);
80 if (fread(source.get(), 1, byte_length, stdin) != byte_length) 80 if (fread(source.get(), 1, byte_length, stdin) != byte_length)
81 return false; 81 return false;
82 82
83 if (!webkit_support::DecodePNG(source.get(), byte_length, 83 if (!image_diff_png::DecodePNG(source.get(), byte_length,
84 &data_, &w_, &h_)) { 84 &data_, &w_, &h_)) {
85 Clear(); 85 Clear();
86 return false; 86 return false;
87 } 87 }
88 return true; 88 return true;
89 } 89 }
90 90
91 // Creates the image from the given filename on disk, and returns true on 91 // Creates the image from the given filename on disk, and returns true on
92 // success. 92 // success.
93 bool CreateFromFilename(const base::FilePath& path) { 93 bool CreateFromFilename(const base::FilePath& path) {
94 FILE* f = file_util::OpenFile(path, "rb"); 94 FILE* f = file_util::OpenFile(path, "rb");
95 if (!f) 95 if (!f)
96 return false; 96 return false;
97 97
98 std::vector<unsigned char> compressed; 98 std::vector<unsigned char> compressed;
99 const int buf_size = 1024; 99 const int buf_size = 1024;
100 unsigned char buf[buf_size]; 100 unsigned char buf[buf_size];
101 size_t num_read = 0; 101 size_t num_read = 0;
102 while ((num_read = fread(buf, 1, buf_size, f)) > 0) { 102 while ((num_read = fread(buf, 1, buf_size, f)) > 0) {
103 compressed.insert(compressed.end(), buf, buf + num_read); 103 compressed.insert(compressed.end(), buf, buf + num_read);
104 } 104 }
105 105
106 file_util::CloseFile(f); 106 file_util::CloseFile(f);
107 107
108 if (!webkit_support::DecodePNG(&compressed[0], compressed.size(), 108 if (!image_diff_png::DecodePNG(&compressed[0], compressed.size(),
109 &data_, &w_, &h_)) { 109 &data_, &w_, &h_)) {
110 Clear(); 110 Clear();
111 return false; 111 return false;
112 } 112 }
113 return true; 113 return true;
114 } 114 }
115 115
116 void Clear() { 116 void Clear() {
117 w_ = h_ = 0; 117 w_ = h_ = 0;
118 data_.clear(); 118 data_.clear();
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 file2.value().c_str()); 308 file2.value().c_str());
309 return kStatusError; 309 return kStatusError;
310 } 310 }
311 311
312 Image diff_image; 312 Image diff_image;
313 bool same = CreateImageDiff(baseline_image, actual_image, &diff_image); 313 bool same = CreateImageDiff(baseline_image, actual_image, &diff_image);
314 if (same) 314 if (same)
315 return kStatusSame; 315 return kStatusSame;
316 316
317 std::vector<unsigned char> png_encoding; 317 std::vector<unsigned char> png_encoding;
318 webkit_support::EncodeRGBAPNG( 318 image_diff_png::EncodeRGBAPNG(
319 diff_image.data(), diff_image.w(), diff_image.h(), 319 diff_image.data(), diff_image.w(), diff_image.h(),
320 diff_image.w() * 4, &png_encoding); 320 diff_image.w() * 4, &png_encoding);
321 if (file_util::WriteFile(out_file, 321 if (file_util::WriteFile(out_file,
322 reinterpret_cast<char*>(&png_encoding.front()), 322 reinterpret_cast<char*>(&png_encoding.front()),
323 base::checked_numeric_cast<int>(png_encoding.size())) < 0) 323 base::checked_numeric_cast<int>(png_encoding.size())) < 0)
324 return kStatusError; 324 return kStatusError;
325 325
326 return kStatusDifferent; 326 return kStatusDifferent;
327 } 327 }
328 328
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 base::FilePath(args[1]), 372 base::FilePath(args[1]),
373 base::FilePath(args[2])); 373 base::FilePath(args[2]));
374 } 374 }
375 } else if (args.size() == 2) { 375 } else if (args.size() == 2) {
376 return CompareImages(base::FilePath(args[0]), base::FilePath(args[1])); 376 return CompareImages(base::FilePath(args[0]), base::FilePath(args[1]));
377 } 377 }
378 378
379 PrintHelp(); 379 PrintHelp();
380 return kStatusError; 380 return kStatusError;
381 } 381 }
OLDNEW
« no previous file with comments | « tools/imagediff/DEPS ('k') | tools/imagediff/image_diff.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698