Debian Codegraph @

podofo/test/common/PdfTest.cpp

raw
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 writeTestOutputFile(const string_view& filepath, const string_view& view);
17
18 static struct TestPaths
19 {
20 TestPaths() :
21 Input(PDF_TEST_RESOURCE_PATH), Output(PDF_TEST_OUTPUT_PATH) { }
22 fs::path Input;
23 fs::path Output;
24 } s_paths;
25
26
27 void utls::CombinePaths(std::filesystem::path& path, std::initializer_list<std::string_view> paths)
28 {
29 for (auto& entry : paths)
30 path /= std::filesystem::u8path(entry);
31 }
32
33 string TestUtils::GetTestOutputFilePath(const string_view& filename)
34 {
35 return (s_paths.Output / fs::u8path(filename)).u8string();
36 }
37
38 string TestUtils::GetTestInputFilePath(const string_view& filename)
39 {
40 return (s_paths.Input / fs::u8path(filename)).u8string();
41 }
42
43 const fs::path& TestUtils::GetTestInputPath()
44 {
45 return s_paths.Input;
46 }
47
48 const fs::path& TestUtils::GetTestOutputPath()
49 {
50 return s_paths.Output;
51 }
52
53 void TestUtils::ReadTestInputFileTo(std::string& str, const string_view& filename)
54 {
55 readTestInputFileTo(str, GetTestInputFilePath(filename));
56 }
57
58 void TestUtils::WriteTestOutputFileTo(const string_view& filename, const string_view& view)
59 {
60 writeTestOutputFile(GetTestOutputFilePath(filename), view);
61 }
62
63 void TestUtils::AssertEqual(double expected, double actual, double threshold)
64 {
65 if (std::abs(actual - expected) > threshold)
66 FAIL("Expected different than actual");
67 }
68
69 void TestUtils::SaveFramePPM(charbuff& buffer, const void* data,
70 PdfPixelFormat srcPixelFormat, unsigned width, unsigned height)
71 {
72 BufferStreamDevice stream(buffer);
73 SaveFramePPM(stream, data, srcPixelFormat, width, height);
74 }
75
76 void TestUtils::SaveFramePPM(OutputStream& stream, const void* data,
77 PdfPixelFormat srcPixelFormat, unsigned width, unsigned height)
78 {
79 // Write header
80 stringstream headerStream;
81 headerStream << "P7\nWIDTH " << width << "\nHEIGHT " << height << "\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n";
82
83 stream.Write(headerStream.str());
84
85 unsigned lineSize = width * 4;
86 charbuff scanline(lineSize);
87
88 // Write pixel data
89 switch (srcPixelFormat)
90 {
91 case PdfPixelFormat::RGBA:
92 {
93 stream.Write((const char*)data, (size_t)width * height * 4ULL);
94 break;
95 }
96 case PdfPixelFormat::BGRA:
97 {
98 for (unsigned i1 = 0; i1 < height; i1++)
99 {
100 for (unsigned i2 = 0; i2 < width; i2++)
101 {
102 scanline[i2 * 4 + 0] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 2];
103 scanline[i2 * 4 + 1] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 1];
104 scanline[i2 * 4 + 2] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 0];
105 scanline[i2 * 4 + 3] = ((const unsigned char*)data)[i1 * lineSize + i2 * 4 + 3];
106 }
107
108 stream.Write(scanline.data(), scanline.size());
109 }
110 break;
111 }
112 case PdfPixelFormat::Unknown:
113 default:
114 PODOFO_RAISE_ERROR(PdfErrorCode::InvalidEnumValue);
115 }
116
117 stream.Flush();
118 }
119
120 bool TestUtils::IsBufferEqual(const bufferview& buffer, const string_view& filename)
121 {
122 charbuff tmp;
123 utls::ReadTo(tmp, filename);
124 return buffer == tmp;
125 }
126
127 bool TestUtils::AreFilesEqual(const string_view& filename1, const string_view& filename2)
128 {
129 charbuff tmp1;
130 utls::ReadTo(tmp1, filename1);
131
132 charbuff tmp2;
133 utls::ReadTo(tmp2, filename2);
134 return tmp1 == tmp2;
135 }
136
137 void TestUtils::readTestInputFileTo(string& str, const string_view& filepath)
138 {
139 #ifdef _WIN32
140 auto filepath16 = utf8::utf8to16((string)filepath);
141 ifstream stream((wchar_t*)filepath16.c_str(), ios_base::binary);
142 #else
143 ifstream stream((string)filepath, ios_base::binary);
144 #endif
145
146 stream.seekg(0, ios::end);
147 if (stream.tellg() == -1)
148 throw runtime_error("Error reading from stream");
149
150 str.reserve((size_t)stream.tellg());
151 stream.seekg(0, ios::beg);
152
153 str.assign((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
154 }
155
156 void writeTestOutputFile(const string_view& filepath, const string_view& view)
157 {
158 #ifdef _WIN32
159 auto filepath16 = utf8::utf8to16((string)filepath);
160 ofstream stream((wchar_t*)filepath16.c_str(), ios_base::binary);
161 #else
162 ofstream stream((string)filepath, ios_base::binary);
163 #endif
164
165 stream.write(view.data(), view.size());
166 stream.close();
167 if (stream.fail())
168 throw runtime_error("Error writing to stream");
169 }