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
3 // found in the LICENSE file.
5 #ifndef PRINTING_IMAGE_H_
6 #define PRINTING_IMAGE_H_
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "printing/printing_export.h"
14 #include "ui/gfx/size.h"
22 // Lightweight raw-bitmap management. The image, once initialized, is immutable.
23 // The main purpose is testing image contents.
24 class PRINTING_EXPORT Image
{
26 // Creates the image from the given file on disk. Uses extension to
27 // defer file type. PNG and EMF (on Windows) currently supported.
28 // If image loading fails size().IsEmpty() will be true.
29 explicit Image(const FilePath
& path
);
31 // Creates the image from the metafile. Deduces bounds based on bounds in
32 // metafile. If loading fails size().IsEmpty() will be true.
33 explicit Image(const Metafile
& metafile
);
36 explicit Image(const Image
& image
);
40 const gfx::Size
& size() const {
44 // Return a checksum of the image (MD5 over the internal data structure).
45 std::string
checksum() const;
48 bool SaveToPng(const FilePath
& filepath
) const;
50 // Returns % of pixels different
51 double PercentageDifferent(const Image
& rhs
) const;
53 // Returns the 0x0RGB or 0xARGB value of the pixel at the given location.
54 uint32
Color(uint32 color
) const {
56 return color
& 0xFFFFFF; // Strip out A.
61 uint32
pixel_at(int x
, int y
) const {
62 DCHECK(x
>= 0 && x
< size_
.width());
63 DCHECK(y
>= 0 && y
< size_
.height());
64 const uint32
* data
= reinterpret_cast<const uint32
*>(&*data_
.begin());
65 const uint32
* data_row
= data
+ y
* row_length_
/ sizeof(uint32
);
66 return Color(data_row
[x
]);
70 // Construct from metafile. This is kept internal since it's ambiguous what
71 // kind of data is used (png, bmp, metafile etc).
72 Image(const void* data
, size_t size
);
74 bool LoadPng(const std::string
& compressed
);
76 bool LoadMetafile(const std::string
& data
);
78 bool LoadMetafile(const Metafile
& metafile
);
80 // Pixel dimensions of the image.
83 // Length of a line in bytes.
86 // Actual bitmap data in arrays of RGBAs (so when loaded as uint32, it's
88 std::vector
<unsigned char> data_
;
90 // Flag to signal if the comparison functions should ignore the alpha channel.
91 const bool ignore_alpha_
; // Currently always true.
93 // Prevent operator= (this function has no implementation)
94 Image
& operator=(const Image
& image
);
97 } // namespace printing
99 #endif // PRINTING_IMAGE_H_