[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef PRINTING_IMAGE_H_ | ||||
6 | #define PRINTING_IMAGE_H_ | ||||
7 | |||||
8 | #include <string> | ||||
9 | #include <vector> | ||||
10 | |||||
11 | #include "base/basictypes.h" | ||||
[email protected] | 542bdfe | 2010-11-30 03:55:47 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 13 | #include "printing/printing_export.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 14 | #include "ui/gfx/size.h" |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 15 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 16 | namespace base { |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 17 | class FilePath; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 18 | } |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 19 | |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 20 | namespace printing { |
21 | |||||
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 22 | class Metafile; |
23 | |||||
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 24 | // Lightweight raw-bitmap management. The image, once initialized, is immutable. |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 25 | // The main purpose is testing image contents. |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 26 | class PRINTING_EXPORT Image { |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 27 | public: |
[email protected] | 63597e4e | 2010-07-08 17:49:05 | [diff] [blame] | 28 | // Creates the image from the given file on disk. Uses extension to |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 29 | // defer file type. PNG and EMF (on Windows) currently supported. |
30 | // If image loading fails size().IsEmpty() will be true. | ||||
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 31 | explicit Image(const base::FilePath& path); |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 32 | |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 33 | // Creates the image from the metafile. Deduces bounds based on bounds in |
34 | // metafile. If loading fails size().IsEmpty() will be true. | ||||
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 35 | explicit Image(const Metafile& metafile); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 36 | |
37 | // Copy constructor. | ||||
38 | explicit Image(const Image& image); | ||||
39 | |||||
[email protected] | 9b2331d9 | 2010-10-04 23:11:19 | [diff] [blame] | 40 | ~Image(); |
41 | |||||
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 42 | const gfx::Size& size() const { |
43 | return size_; | ||||
44 | } | ||||
45 | |||||
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 46 | // Return a checksum of the image (MD5 over the internal data structure). |
47 | std::string checksum() const; | ||||
48 | |||||
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 49 | // Save image as PNG. |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 50 | bool SaveToPng(const base::FilePath& filepath) const; |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 51 | |
52 | // Returns % of pixels different | ||||
53 | double PercentageDifferent(const Image& rhs) const; | ||||
54 | |||||
55 | // Returns the 0x0RGB or 0xARGB value of the pixel at the given location. | ||||
56 | uint32 Color(uint32 color) const { | ||||
57 | if (ignore_alpha_) | ||||
58 | return color & 0xFFFFFF; // Strip out A. | ||||
59 | else | ||||
60 | return color; | ||||
61 | } | ||||
62 | |||||
[email protected] | 542bdfe | 2010-11-30 03:55:47 | [diff] [blame] | 63 | uint32 pixel_at(int x, int y) const { |
64 | DCHECK(x >= 0 && x < size_.width()); | ||||
65 | DCHECK(y >= 0 && y < size_.height()); | ||||
66 | const uint32* data = reinterpret_cast<const uint32*>(&*data_.begin()); | ||||
67 | const uint32* data_row = data + y * row_length_ / sizeof(uint32); | ||||
68 | return Color(data_row[x]); | ||||
69 | } | ||||
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 70 | |
71 | private: | ||||
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 72 | // Construct from metafile. This is kept internal since it's ambiguous what |
73 | // kind of data is used (png, bmp, metafile etc). | ||||
74 | Image(const void* data, size_t size); | ||||
75 | |||||
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 76 | bool LoadPng(const std::string& compressed); |
77 | |||||
78 | bool LoadMetafile(const std::string& data); | ||||
79 | |||||
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 80 | bool LoadMetafile(const Metafile& metafile); |
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 81 | |
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 82 | // Pixel dimensions of the image. |
83 | gfx::Size size_; | ||||
84 | |||||
85 | // Length of a line in bytes. | ||||
86 | int row_length_; | ||||
87 | |||||
88 | // Actual bitmap data in arrays of RGBAs (so when loaded as uint32, it's | ||||
89 | // 0xABGR). | ||||
90 | std::vector<unsigned char> data_; | ||||
91 | |||||
92 | // Flag to signal if the comparison functions should ignore the alpha channel. | ||||
93 | const bool ignore_alpha_; // Currently always true. | ||||
94 | |||||
[email protected] | aa82249f | 2009-07-16 17:23:58 | [diff] [blame] | 95 | // Prevent operator= (this function has no implementation) |
96 | Image& operator=(const Image& image); | ||||
[email protected] | 1947868 | 2009-07-13 16:11:08 | [diff] [blame] | 97 | }; |
98 | |||||
99 | } // namespace printing | ||||
100 | |||||
101 | #endif // PRINTING_IMAGE_H_ |