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 #ifndef PRINTING_IMAGE_H_ | 5 #ifndef PRINTING_IMAGE_H_ |
6 #define PRINTING_IMAGE_H_ | 6 #define PRINTING_IMAGE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "printing/printing_export.h" | 15 #include "printing/printing_export.h" |
16 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 class FilePath; | 19 class FilePath; |
20 } | 20 } |
21 | 21 |
22 namespace printing { | 22 namespace printing { |
23 | 23 |
24 class Metafile; | |
25 | |
26 // Lightweight raw-bitmap management. The image, once initialized, is immutable. | 24 // Lightweight raw-bitmap management. The image, once initialized, is immutable. |
27 // The main purpose is testing image contents. | 25 // The main purpose is testing image contents. |
28 class PRINTING_EXPORT Image { | 26 class PRINTING_EXPORT Image { |
29 public: | 27 public: |
30 // Creates the image from the metafile. Deduces bounds based on bounds in | 28 // Creates the image from the metafile. Deduces bounds based on bounds in |
31 // metafile. If loading fails size().IsEmpty() will be true. | 29 // metafile. If loading fails size().IsEmpty() will be true. |
32 explicit Image(const Metafile& metafile); | 30 Image(const void* metafile_src_buffer, size_t metafile_src_buffer_size); |
33 | 31 |
34 // Copy constructor. | 32 Image(const Image& image); |
35 explicit Image(const Image& image); | 33 Image(Image&& image); |
36 | 34 |
37 ~Image(); | 35 ~Image(); |
38 | 36 |
39 const gfx::Size& size() const { | 37 const gfx::Size& size() const { |
40 return size_; | 38 return size_; |
41 } | 39 } |
42 | 40 |
43 // Return a checksum of the image (MD5 over the internal data structure). | 41 // Return a checksum of the image (MD5 over the internal data structure). |
44 std::string checksum() const; | 42 std::string checksum() const; |
45 | 43 |
46 // Save image as PNG. | 44 // Save image as PNG. |
47 bool SaveToPng(const base::FilePath& filepath) const; | 45 bool SaveToPng(const base::FilePath& filepath) const; |
48 | 46 |
49 // Returns % of pixels different | |
50 double PercentageDifferent(const Image& rhs) const; | |
51 | |
52 // Returns the 0x0RGB or 0xARGB value of the pixel at the given location. | |
53 uint32_t Color(uint32_t color) const { | |
54 if (ignore_alpha_) | |
55 return color & 0xFFFFFF; // Strip out A. | |
56 else | |
57 return color; | |
58 } | |
59 | |
60 uint32_t pixel_at(int x, int y) const { | |
61 DCHECK(x >= 0 && x < size_.width()); | |
62 DCHECK(y >= 0 && y < size_.height()); | |
63 const uint32_t* data = reinterpret_cast<const uint32_t*>(&*data_.begin()); | |
64 const uint32_t* data_row = data + y * row_length_ / sizeof(uint32_t); | |
65 return Color(data_row[x]); | |
66 } | |
67 | |
68 private: | 47 private: |
69 // Construct from metafile. This is kept internal since it's ambiguous what | 48 bool LoadMetafile(const void* metafile_src_buffer, |
70 // kind of data is used (png, bmp, metafile etc). | 49 size_t metafile_src_buffer_size); |
71 Image(const void* data, size_t size); | |
72 | |
73 bool LoadPng(const std::string& compressed); | |
74 | |
75 bool LoadMetafile(const Metafile& metafile); | |
76 | 50 |
77 // Pixel dimensions of the image. | 51 // Pixel dimensions of the image. |
78 gfx::Size size_; | 52 gfx::Size size_; |
79 | 53 |
80 // Length of a line in bytes. | 54 // Length of a line in bytes. |
81 int row_length_; | 55 int row_length_; |
82 | 56 |
83 // Actual bitmap data in arrays of RGBAs (so when loaded as uint32_t, it's | 57 // Actual bitmap data in arrays of RGBAs (so when loaded as uint32_t, it's |
84 // 0xABGR). | 58 // 0xABGR). |
85 std::vector<unsigned char> data_; | 59 std::vector<unsigned char> data_; |
86 | 60 |
87 // Flag to signal if the comparison functions should ignore the alpha channel. | 61 // Flag to signal if the comparison functions should ignore the alpha channel. |
88 const bool ignore_alpha_; // Currently always true. | 62 const bool ignore_alpha_; // Currently always true. |
89 | 63 |
90 // Prevent operator= (this function has no implementation) | 64 // Prevent operator= (this function has no implementation) |
91 Image& operator=(const Image& image); | 65 Image& operator=(const Image& image); |
92 }; | 66 }; |
93 | 67 |
94 } // namespace printing | 68 } // namespace printing |
95 | 69 |
96 #endif // PRINTING_IMAGE_H_ | 70 #endif // PRINTING_IMAGE_H_ |
OLD | NEW |