Roll src/third_party/WebKit dbf9be3:8d6c3d5 (svn 202308:202312)
[chromium-blink-merge.git] / printing / image.h
blob1406a8554e40f1fd5f5006b68672ab4376dcfd71
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_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "printing/printing_export.h"
14 #include "ui/gfx/geometry/size.h"
16 namespace base {
17 class FilePath;
20 namespace printing {
22 class Metafile;
24 // Lightweight raw-bitmap management. The image, once initialized, is immutable.
25 // The main purpose is testing image contents.
26 class PRINTING_EXPORT Image {
27 public:
28 // Creates the image from the given file on disk. Uses extension to
29 // defer file type. PNG and EMF (on Windows) currently supported.
30 // If image loading fails size().IsEmpty() will be true.
31 explicit Image(const base::FilePath& path);
33 // Creates the image from the metafile. Deduces bounds based on bounds in
34 // metafile. If loading fails size().IsEmpty() will be true.
35 explicit Image(const Metafile& metafile);
37 // Copy constructor.
38 explicit Image(const Image& image);
40 ~Image();
42 const gfx::Size& size() const {
43 return size_;
46 // Return a checksum of the image (MD5 over the internal data structure).
47 std::string checksum() const;
49 // Save image as PNG.
50 bool SaveToPng(const base::FilePath& filepath) const;
52 // Returns % of pixels different
53 double PercentageDifferent(const Image& rhs) const;
55 // Returns the 0x0RGB or 0xARGB value of the pixel at the given location.
56 uint32_t Color(uint32_t color) const {
57 if (ignore_alpha_)
58 return color & 0xFFFFFF; // Strip out A.
59 else
60 return color;
63 uint32_t pixel_at(int x, int y) const {
64 DCHECK(x >= 0 && x < size_.width());
65 DCHECK(y >= 0 && y < size_.height());
66 const uint32_t* data = reinterpret_cast<const uint32_t*>(&*data_.begin());
67 const uint32_t* data_row = data + y * row_length_ / sizeof(uint32_t);
68 return Color(data_row[x]);
71 private:
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);
76 bool LoadPng(const std::string& compressed);
78 bool LoadMetafile(const std::string& data);
80 bool LoadMetafile(const Metafile& metafile);
82 // Pixel dimensions of the image.
83 gfx::Size size_;
85 // Length of a line in bytes.
86 int row_length_;
88 // Actual bitmap data in arrays of RGBAs (so when loaded as uint32_t, it's
89 // 0xABGR).
90 std::vector<unsigned char> data_;
92 // Flag to signal if the comparison functions should ignore the alpha channel.
93 const bool ignore_alpha_; // Currently always true.
95 // Prevent operator= (this function has no implementation)
96 Image& operator=(const Image& image);
99 } // namespace printing
101 #endif // PRINTING_IMAGE_H_