Debian Codegraph @

podofo/test/common/PdfTest.cpp

raw symbols SCIP
1 /**
2 * Copyright (C) 2010 by Dominik Seichter <[email protected]>
3 * Copyright (C) 2021 by Francesco Pretto <[email protected]>
4 *
5 * Licensed under GNU Library General Public 2.0 or later.
6 * Some rights reserved. See COPYING, AUTHORS.
7 */
8
9 #include "PdfTest.h"
10
11 #include <utf8cpp/utf8.h>
12
13 using namespace std;
14 using namespace PoDoFo;
15
16 static void readTestInputFile(const string_view& filepath, string& str);
17 static void writeTestOutputFile(const string_view& filepath, const string_view& view);
18
19 static struct TestPaths
20 {
21 TestPaths() :
22 Input(PDF_TEST_RESOURCE_PATH), Output(PDF_TEST_OUTPUT_PATH) { }
23 fs::path Input;
24 fs::path Output;
25 } s_paths;
26
27
28 void utls::CombinePaths(std::filesystem::path& path, std::initializer_list<std::string_view> paths)
29 {
30 for (auto& entry : paths)
31 path /= std::filesystem::u8path(entry);
32 }
33
34 string TestUtils::GetTestOutputFilePath(const string_view& filename)
35 {
36 return (s_paths.Output / fs::u8path(filename)).u8string();
37 }
38
39 string TestUtils::GetTestInputFilePath(const string_view& filename)
40 {
41 return (s_paths.Input / fs::u8path(filename)).u8string();
42 }
43
44 const fs::path& TestUtils::GetTestInputPath()
45 {
46 return s_paths.Input;
47 }
48
49 const fs::path& TestUtils::GetTestOutputPath()
50 {
51 return s_paths.Output;
52 }
53
54 void TestUtils::ReadTestInputFile(const string_view& filename, string& str)
55 {
56 readTestInputFile(GetTestInputFilePath(filename), str);
57 }
58
59 void TestUtils::WriteTestOutputFile(const string_view& filename, const string_view& view)
60 {
61 writeTestOutputFile(GetTestOutputFilePath(filename), view);
62 }
63
64 void TestUtils::AssertEqual(double expected, double actual, double threshold)
65 {
66 if (std::abs(actual - expected) > threshold)
67 FAIL("Expected different than actual");
68 }
69
70 void TestUtils::SaveFramePPM(charbuff& buffer, const void* data,
71 PdfPixelFormat srcPixelFormat, unsigned width, unsigned height)
72 {
73 BufferStreamDevice stream(buffer);
74 SaveFramePPM(stream, data, srcPixelFormat, width, height);
75 }
76
77 void TestUtils::SaveFramePPM(OutputStream& stream, const void* data,
78 PdfPixelFormat srcPixelFormat, unsigned width, unsigned height)
79 {
80 // Write header
81 stringstream headerStream;
82 headerStream << "P7\nWIDTH " << width << "\nHEIGHT " << height << "\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n";
83
84 stream.Write(headerStream.str());
85
86 unsigned lineSize = width * 4;
87 charbuff scanline(lineSize);
88
89 // Write pixel data
90 switch (srcPixelFormat)
91 {
92 case PdfPixelFormat::RGBA:
93 {
94 stream.Write((const char*)data, (size_t)width * height * 4ULL);
95 break;
96 }
97 case PdfPixelFormat::BGRA:
98 {
99 for (unsigned i1 = 0; i1 < height; i1++)
100 {
101 for (unsigned i2 = 0; i2 < width; i2++)
102 {
103 scanline[i2 * 4 + 0] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 2];
104 scanline[i2 * 4 + 1] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 1];
105 scanline[i2 * 4 + 2] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 0];
106 scanline[i2 * 4 + 3] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 3];
107 }
108
109 stream.Write(scanline.data(), scanline.size());
110 }
111 break;
112 }
113 case PdfPixelFormat::Unknown:
114 default:
115 PODOFO_RAISE_ERROR(PdfErrorCode::InvalidEnumValue);
116 }
117
118 stream.Flush();
119 }
120
121 void readTestInputFile(const string_view& filepath, string& str)
122 {
123 #ifdef _WIN32
124 auto filepath16 = utf8::utf8to16((string)filepath);
125 ifstream stream((wchar_t*)filepath16.c_str(), ios_base::binary);
126 #else
127 ifstream stream((string)filepath, ios_base::binary);
128 #endif
129
130 stream.seekg(0, ios::end);
131 if (stream.tellg() == -1)
132 throw runtime_error("Error reading from stream");
133
134 str.reserve((size_t)stream.tellg());
135 stream.seekg(0, ios::beg);
136
137 str.assign((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
138 }
139
140 void writeTestOutputFile(const string_view& filepath, const string_view& view)
141 {
142 #ifdef _WIN32
143 auto filepath16 = utf8::utf8to16((string)filepath);
144 ofstream stream((wchar_t*)filepath16.c_str(), ios_base::binary);
145 #else
146 ofstream stream((string)filepath, ios_base::binary);
147 #endif
148
149 stream.write(view.data(), view.size());
150 stream.close();
151 if (stream.fail())
152 throw runtime_error("Error writing to stream");
153 }