File: pwg_encoder_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download | duplicates (4)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "components/pwg_encoder/pwg_encoder.h"

#include <stdint.h>

#include <memory>

#include "base/hash/sha1.h"
#include "base/strings/string_number_conversions.h"
#include "components/pwg_encoder/bitmap_image.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace pwg_encoder {

namespace {

const int kRasterWidth = 612;
const int kRasterHeight = 792;
const int kRasterDPI = 72;

std::unique_ptr<BitmapImage> MakeSampleBitmap() {
  auto bitmap_image = std::make_unique<BitmapImage>(
      gfx::Size(kRasterWidth, kRasterHeight), BitmapImage::RGBA);
  uint32_t* bitmap_data =
      reinterpret_cast<uint32_t*>(bitmap_image->pixel_data());
  for (int i = 0; i < kRasterWidth * kRasterHeight; i++)
    bitmap_data[i] = 0xFFFFFF;

  for (int i = 0; i < kRasterWidth; i++) {
    for (int j = 200; j < 300; j++) {
      int row_start = j * kRasterWidth;
      uint32_t red = (i * 255) / kRasterWidth;
      bitmap_data[row_start + i] = red;
    }
  }

  // To test run length encoding
  for (int i = 0; i < kRasterWidth; i++) {
    for (int j = 400; j < 500; j++) {
      int row_start = j * kRasterWidth;
      if ((i / 40) % 2 == 0) {
        bitmap_data[row_start + i] = 255 << 8;
      } else {
        bitmap_data[row_start + i] = 255 << 16;
      }
    }
  }

  return bitmap_image;
}

}  // namespace

TEST(PwgRasterTest, Encode) {
  // Encode in color by default.
  std::unique_ptr<BitmapImage> image = MakeSampleBitmap();
  PwgHeaderInfo header_info;
  header_info.dpi = gfx::Size(kRasterDPI, kRasterDPI);

  std::string output = PwgEncoder::GetDocumentHeader();
  output += PwgEncoder::EncodePage(*image, header_info);
  EXPECT_EQ(2970U, output.size());

  std::string sha1 = base::SHA1HashString(output);
  EXPECT_EQ("4AD7442998C8FEAE94BC9C8B177A7C94766CC9FB", base::HexEncode(sha1));

  // Encode again in monochrome.
  header_info.color_space = PwgHeaderInfo::SGRAY;

  output = PwgEncoder::GetDocumentHeader();
  output += PwgEncoder::EncodePage(*image, header_info);
  EXPECT_EQ(2388U, output.size());

  sha1 = base::SHA1HashString(output);
  EXPECT_EQ("4E718B0A69AC26A366A2E23AE1ECA6055079A1FF", base::HexEncode(sha1));
}

}  // namespace pwg_encoder