image.h 2.5 KB

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