Make ServerBoundCertStore interface async, move SQLiteServerBoundCertStore load onto...
[chromium-blink-merge.git] / printing / image.h
blobe34c2c24c501a9bf7d4a3db6af37940b3e9312ef
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/size.h"
16 class FilePath;
18 namespace printing {
20 class Metafile;
22 // Lightweight raw-bitmap management. The image, once initialized, is immutable.
23 // The main purpose is testing image contents.
24 class PRINTING_EXPORT Image {
25 public:
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);
35 // Copy constructor.
36 explicit Image(const Image& image);
38 ~Image();
40 const gfx::Size& size() const {
41 return size_;
44 // Return a checksum of the image (MD5 over the internal data structure).
45 std::string checksum() const;
47 // Save image as PNG.
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 {
55 if (ignore_alpha_)
56 return color & 0xFFFFFF; // Strip out A.
57 else
58 return color;
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]);
69 private:
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.
81 gfx::Size size_;
83 // Length of a line in bytes.
84 int row_length_;
86 // Actual bitmap data in arrays of RGBAs (so when loaded as uint32, it's
87 // 0xABGR).
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_