Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / image.h
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.
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/logging.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 {
39     return size_;
40   }
41
42   // Return a checksum of the image (MD5 over the internal data structure).
43   std::string checksum() const;
44
45   // Save image as PNG.
46   bool SaveToPng(const base::FilePath& filepath) const;
47
48   // Returns % of pixels different
49   double PercentageDifferent(const Image& rhs) const;
50
51   // Returns the 0x0RGB or 0xARGB value of the pixel at the given location.
52   uint32_t Color(uint32_t color) const {
53     if (ignore_alpha_)
54       return color & 0xFFFFFF;  // Strip out A.
55     else
56       return color;
57   }
58
59   uint32_t pixel_at(int x, int y) const {
60     DCHECK(x >= 0 && x < size_.width());
61     DCHECK(y >= 0 && y < size_.height());
62     const uint32_t* data = reinterpret_cast<const uint32_t*>(&*data_.begin());
63     const uint32_t* data_row = data + y * row_length_ / sizeof(uint32_t);
64     return Color(data_row[x]);
65   }
66
67  private:
68   // Construct from metafile.  This is kept internal since it's ambiguous what
69   // kind of data is used (png, bmp, metafile etc).
70   Image(const void* data, size_t size);
71
72   bool LoadPng(const std::string& compressed);
73
74   // Loads the first page from |metafile|.
75   bool LoadMetafile(const Metafile& metafile);
76
77   // Pixel dimensions of the image.
78   gfx::Size size_;
79
80   // Length of a line in bytes.
81   int row_length_;
82
83   // Actual bitmap data in arrays of RGBAs (so when loaded as uint32_t, it's
84   // 0xABGR).
85   std::vector<unsigned char> data_;
86
87   // Flag to signal if the comparison functions should ignore the alpha channel.
88   const bool ignore_alpha_;  // Currently always true.
89
90   // Prevent operator= (this function has no implementation)
91   Image& operator=(const Image& image);
92 };
93
94 }  // namespace printing
95
96 #endif  // PRINTING_IMAGE_H_