summaryrefslogtreecommitdiffstats
path: root/printing/image.cc
blob: 667adae8c4596412776a753bd8420aabf242c9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/image.h"

#include "base/file_util.h"
#include "base/gfx/png_decoder.h"
#include "base/gfx/png_encoder.h"
#include "base/gfx/rect.h"
#include "base/md5.h"
#include "base/string_util.h"
#include "skia/ext/platform_device.h"

#if defined(OS_WIN)
#include "base/gfx/gdi_util.h"  // EMF support
#endif

namespace {

// A simple class which temporarily overrides system settings.
// The bitmap image rendered via the PlayEnhMetaFile() function depends on
// some system settings.
// As a workaround for such dependency, this class saves the system settings
// and changes them. This class also restore the saved settings in its
// destructor.
class DisableFontSmoothing {
 public:
  explicit DisableFontSmoothing(bool disable) : enable_again_(false) {
    if (disable) {
#if defined(OS_WIN)
      BOOL enabled;
      if (SystemParametersInfo(SPI_GETFONTSMOOTHING, 0, &enabled, 0) &&
          enabled) {
        if (SystemParametersInfo(SPI_SETFONTSMOOTHING, FALSE, NULL, 0))
          enable_again_ = true;
      }
#endif
    }
  }

  ~DisableFontSmoothing() {
    if (enable_again_) {
#if defined(OS_WIN)
      BOOL result = SystemParametersInfo(SPI_SETFONTSMOOTHING, TRUE, NULL, 0);
      DCHECK(result);
#endif
    }
  }

 private:
  bool enable_again_;

  DISALLOW_EVIL_CONSTRUCTORS(DisableFontSmoothing);
};

}  // namespace

namespace printing {

Image::Image(const std::wstring& filename)
    : row_length_(0),
      ignore_alpha_(true) {
  std::string data;
  file_util::ReadFileToString(filename, &data);
  std::wstring ext = file_util::GetFileExtensionFromPath(filename);
  bool success = false;
  if (LowerCaseEqualsASCII(ext, "png")) {
    success = LoadPng(data);
  } else if (LowerCaseEqualsASCII(ext, "emf")) {
    success = LoadMetafile(data);
  } else {
    DCHECK(false);
  }
  if (!success) {
    size_.SetSize(0, 0);
    row_length_ = 0;
    data_.clear();
  }
}

Image::Image(const NativeMetafile& metafile)
    : row_length_(0),
      ignore_alpha_(true) {
  LoadMetafile(metafile);
}

Image::Image(const Image& image)
    : size_(image.size_),
      row_length_(image.row_length_),
      data_(image.data_),
      ignore_alpha_(image.ignore_alpha_) {
}

std::string Image::checksum() const {
  MD5Digest digest;
  MD5Sum(&data_[0], data_.size(), &digest);
  return HexEncode(&digest, sizeof(digest));
}

bool Image::SaveToPng(const std::wstring& filename) const {
  DCHECK(!data_.empty());
  std::vector<unsigned char> compressed;
  bool success = PNGEncoder::Encode(&*data_.begin(),
                                    PNGEncoder::FORMAT_BGRA,
                                    size_.width(),
                                    size_.height(),
                                    row_length_,
                                    true,
                                    &compressed);
  DCHECK(success && compressed.size());
  if (success) {
    int write_bytes = file_util::WriteFile(
        filename,
        reinterpret_cast<char*>(&*compressed.begin()), compressed.size());
    success = (write_bytes == static_cast<int>(compressed.size()));
    DCHECK(success);
  }
  return success;
}

double Image::PercentageDifferent(const Image& rhs) const {
  if (size_.width() == 0 || size_.height() == 0 ||
    rhs.size_.width() == 0 || rhs.size_.height() == 0)
    return 100.;

  int width = std::min(size_.width(), rhs.size_.width());
  int height = std::min(size_.height(), rhs.size_.height());
  // Compute pixels different in the overlap
  int pixels_different = 0;
  for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
      uint32 lhs_pixel = pixel_at(x, y);
      uint32 rhs_pixel = rhs.pixel_at(x, y);
      if (lhs_pixel != rhs_pixel)
        ++pixels_different;
    }

    // Look for extra right lhs pixels. They should be white.
    for (int x = width; x < size_.width(); ++x) {
      uint32 lhs_pixel = pixel_at(x, y);
      if (lhs_pixel != Color(SK_ColorWHITE))
        ++pixels_different;
    }

    // Look for extra right rhs pixels. They should be white.
    for (int x = width; x < rhs.size_.width(); ++x) {
      uint32 rhs_pixel = rhs.pixel_at(x, y);
      if (rhs_pixel != Color(SK_ColorWHITE))
        ++pixels_different;
    }
  }

  // Look for extra bottom lhs pixels. They should be white.
  for (int y = height; y < size_.height(); ++y) {
    for (int x = 0; x < size_.width(); ++x) {
      uint32 lhs_pixel = pixel_at(x, y);
      if (lhs_pixel != Color(SK_ColorWHITE))
        ++pixels_different;
    }
  }

  // Look for extra bottom rhs pixels. They should be white.
  for (int y = height; y < rhs.size_.height(); ++y) {
    for (int x = 0; x < rhs.size_.width(); ++x) {
      uint32 rhs_pixel = rhs.pixel_at(x, y);
      if (rhs_pixel != Color(SK_ColorWHITE))
        ++pixels_different;
    }
  }

  // Like the WebKit ImageDiff tool, we define percentage different in terms
  // of the size of the 'actual' bitmap.
  double total_pixels = static_cast<double>(size_.width()) *
      static_cast<double>(height);
  return static_cast<double>(pixels_different) / total_pixels * 100.;
}

bool Image::LoadPng(const std::string& compressed) {
  int w;
  int h;
  bool success = PNGDecoder::Decode(
      reinterpret_cast<const unsigned char*>(compressed.c_str()),
      compressed.size(), PNGDecoder::FORMAT_BGRA, &data_, &w, &h);
  size_.SetSize(w, h);
  row_length_ = size_.width() * sizeof(uint32);
  return success;
}

bool Image::LoadMetafile(const std::string& data) {
  DCHECK(!data.empty());
#if defined(OS_WIN)
  NativeMetafile metafile;
  metafile.CreateFromData(data.data(), data.size());
  return LoadMetafile(metafile);
#else
  NOTIMPLEMENTED();
  return false;
#endif
}

bool Image::LoadMetafile(const NativeMetafile& metafile) {
#if defined(OS_WIN)
  gfx::Rect rect(metafile.GetBounds());
  DisableFontSmoothing disable_in_this_scope(true);
  // Create a temporary HDC and bitmap to retrieve the rendered data.
  HDC hdc = CreateCompatibleDC(NULL);
  BITMAPV4HEADER hdr;
  DCHECK_EQ(rect.x(), 0);
  DCHECK_EQ(rect.y(), 0);
  DCHECK_GE(rect.width(), 0);  // Metafile could be empty.
  DCHECK_GE(rect.height(), 0);
  if (rect.width() > 0 && rect.height() > 0) {
    size_ = rect.size();
    gfx::CreateBitmapV4Header(rect.width(), rect.height(), &hdr);
    void* bits;
    HBITMAP bitmap = CreateDIBSection(hdc,
                                      reinterpret_cast<BITMAPINFO*>(&hdr), 0,
                                      &bits, NULL, 0);
    DCHECK(bitmap);
    DCHECK(SelectObject(hdc, bitmap));
    skia::PlatformDevice::InitializeDC(hdc);
    bool success = metafile.Playback(hdc, NULL);
    row_length_ = size_.width() * sizeof(uint32);
    size_t bytes = row_length_ * size_.height();
    DCHECK(bytes);
    data_.resize(bytes);
    memcpy(&*data_.begin(), bits, bytes);
    DeleteDC(hdc);
    DeleteObject(bitmap);
    return success;
  }
#else
  NOTIMPLEMENTED();
#endif

  return false;
}

}  // namespace printing