pwg_encoder_unittest.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2013 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 "components/pwg_encoder/pwg_encoder.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include "base/hash/sha1.h"
  8. #include "base/strings/string_number_conversions.h"
  9. #include "components/pwg_encoder/bitmap_image.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace pwg_encoder {
  12. namespace {
  13. const int kRasterWidth = 612;
  14. const int kRasterHeight = 792;
  15. const int kRasterDPI = 72;
  16. std::unique_ptr<BitmapImage> MakeSampleBitmap() {
  17. auto bitmap_image = std::make_unique<BitmapImage>(
  18. gfx::Size(kRasterWidth, kRasterHeight), BitmapImage::RGBA);
  19. uint32_t* bitmap_data =
  20. reinterpret_cast<uint32_t*>(bitmap_image->pixel_data());
  21. for (int i = 0; i < kRasterWidth * kRasterHeight; i++)
  22. bitmap_data[i] = 0xFFFFFF;
  23. for (int i = 0; i < kRasterWidth; i++) {
  24. for (int j = 200; j < 300; j++) {
  25. int row_start = j * kRasterWidth;
  26. uint32_t red = (i * 255) / kRasterWidth;
  27. bitmap_data[row_start + i] = red;
  28. }
  29. }
  30. // To test run length encoding
  31. for (int i = 0; i < kRasterWidth; i++) {
  32. for (int j = 400; j < 500; j++) {
  33. int row_start = j * kRasterWidth;
  34. if ((i / 40) % 2 == 0) {
  35. bitmap_data[row_start + i] = 255 << 8;
  36. } else {
  37. bitmap_data[row_start + i] = 255 << 16;
  38. }
  39. }
  40. }
  41. return bitmap_image;
  42. }
  43. } // namespace
  44. TEST(PwgRasterTest, Encode) {
  45. // Encode in color by default.
  46. std::unique_ptr<BitmapImage> image = MakeSampleBitmap();
  47. PwgHeaderInfo header_info;
  48. header_info.dpi = gfx::Size(kRasterDPI, kRasterDPI);
  49. std::string output = PwgEncoder::GetDocumentHeader();
  50. output += PwgEncoder::EncodePage(*image, header_info);
  51. EXPECT_EQ(2970U, output.size());
  52. std::string sha1 = base::SHA1HashString(output);
  53. EXPECT_EQ("4AD7442998C8FEAE94BC9C8B177A7C94766CC9FB",
  54. base::HexEncode(sha1.data(), sha1.size()));
  55. // Encode again in monochrome.
  56. header_info.color_space = PwgHeaderInfo::SGRAY;
  57. output = PwgEncoder::GetDocumentHeader();
  58. output += PwgEncoder::EncodePage(*image, header_info);
  59. EXPECT_EQ(2388U, output.size());
  60. sha1 = base::SHA1HashString(output);
  61. EXPECT_EQ("4E718B0A69AC26A366A2E23AE1ECA6055079A1FF",
  62. base::HexEncode(sha1.data(), sha1.size()));
  63. }
  64. } // namespace pwg_encoder