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