Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / image.cc
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 #include "printing/image.h"
6
7 #include <stdint.h>
8
9 #include <algorithm>
10
11 #include "base/files/file_util.h"
12 #include "base/md5.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "printing/metafile.h"
16 #include "third_party/skia/include/core/SkColor.h"
17 #include "ui/gfx/codec/png_codec.h"
18
19 namespace printing {
20
21 Image::Image(const Metafile& metafile)
22     : row_length_(0),
23       ignore_alpha_(true) {
24   LoadMetafile(metafile);
25 }
26
27 Image::Image(const Image& image) = default;
28
29 Image::~Image() = default;
30
31 std::string Image::checksum() const {
32   base::MD5Digest digest;
33   base::MD5Sum(&data_[0], data_.size(), &digest);
34   return base::MD5DigestToBase16(digest);
35 }
36
37 bool Image::SaveToPng(const base::FilePath& filepath) const {
38   DCHECK(!data_.empty());
39   std::vector<unsigned char> compressed;
40   bool success = gfx::PNGCodec::Encode(&*data_.begin(),
41                                        gfx::PNGCodec::FORMAT_BGRA,
42                                        size_,
43                                        row_length_,
44                                        true,
45                                        std::vector<gfx::PNGCodec::Comment>(),
46                                        &compressed);
47   DCHECK(success && compressed.size());
48   if (success) {
49     int write_bytes = base::WriteFile(
50         filepath,
51         reinterpret_cast<char*>(&*compressed.begin()),
52         base::checked_cast<int>(compressed.size()));
53     success = (write_bytes == static_cast<int>(compressed.size()));
54     DCHECK(success);
55   }
56   return success;
57 }
58
59 double Image::PercentageDifferent(const Image& rhs) const {
60   if (size_.width() == 0 || size_.height() == 0 ||
61     rhs.size_.width() == 0 || rhs.size_.height() == 0)
62     return 100.;
63
64   int width = std::min(size_.width(), rhs.size_.width());
65   int height = std::min(size_.height(), rhs.size_.height());
66   // Compute pixels different in the overlap
67   int pixels_different = 0;
68   for (int y = 0; y < height; ++y) {
69     for (int x = 0; x < width; ++x) {
70       uint32_t lhs_pixel = pixel_at(x, y);
71       uint32_t rhs_pixel = rhs.pixel_at(x, y);
72       if (lhs_pixel != rhs_pixel)
73         ++pixels_different;
74     }
75
76     // Look for extra right lhs pixels. They should be white.
77     for (int x = width; x < size_.width(); ++x) {
78       uint32_t lhs_pixel = pixel_at(x, y);
79       if (lhs_pixel != Color(SK_ColorWHITE))
80         ++pixels_different;
81     }
82
83     // Look for extra right rhs pixels. They should be white.
84     for (int x = width; x < rhs.size_.width(); ++x) {
85       uint32_t rhs_pixel = rhs.pixel_at(x, y);
86       if (rhs_pixel != Color(SK_ColorWHITE))
87         ++pixels_different;
88     }
89   }
90
91   // Look for extra bottom lhs pixels. They should be white.
92   for (int y = height; y < size_.height(); ++y) {
93     for (int x = 0; x < size_.width(); ++x) {
94       uint32_t lhs_pixel = pixel_at(x, y);
95       if (lhs_pixel != Color(SK_ColorWHITE))
96         ++pixels_different;
97     }
98   }
99
100   // Look for extra bottom rhs pixels. They should be white.
101   for (int y = height; y < rhs.size_.height(); ++y) {
102     for (int x = 0; x < rhs.size_.width(); ++x) {
103       uint32_t rhs_pixel = rhs.pixel_at(x, y);
104       if (rhs_pixel != Color(SK_ColorWHITE))
105         ++pixels_different;
106     }
107   }
108
109   // Like the WebKit ImageDiff tool, we define percentage different in terms
110   // of the size of the 'actual' bitmap.
111   double total_pixels = static_cast<double>(size_.width()) *
112       static_cast<double>(height);
113   return static_cast<double>(pixels_different) / total_pixels * 100.;
114 }
115
116 bool Image::LoadPng(const std::string& compressed) {
117   int w;
118   int h;
119   bool success = gfx::PNGCodec::Decode(
120       reinterpret_cast<const unsigned char*>(compressed.c_str()),
121       compressed.size(), gfx::PNGCodec::FORMAT_BGRA, &data_, &w, &h);
122   size_.SetSize(w, h);
123   row_length_ = size_.width() * sizeof(uint32_t);
124   return success;
125 }
126
127 }  // namespace printing