image.cc 3.6 KB

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