Issue 1197 attachment: pdfium_test.cc (2.5 KB)

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145


#include "public/cpp/fpdf_scopers.h"
#include "public/fpdf_ext.h"
#include "samples/pdfium_test_write_helper.h"
#include "testing/test_support.h"




bool RenderPage(const std::string& name,
FPDF_DOCUMENT doc,
const int page_index)
{

FPDF_PAGE page = FPDF_LoadPage ( doc, page_index );

if ( ! page )
{
printf ( "FPDF_LoadPage failed\n" );
return false;
}

double scale = .5;

auto width = static_cast<int>(FPDF_GetPageWidth ( page ) * scale);
auto height = static_cast<int>(FPDF_GetPageHeight ( page ) * scale);
int alpha = FPDFPage_HasTransparency ( page ) ? 1 : 0;


ScopedFPDFBitmap bitmap ( FPDFBitmap_Create ( width, height, alpha ));

if (bitmap)
{
FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);

FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, FPDF_ANNOT);


int stride = FPDFBitmap_GetStride(bitmap.get());
const char* buffer =
reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get()));

std::string image_file_name;
image_file_name = WritePng(name.c_str(), page_index, buffer, stride, width, height);
}

FPDF_ClosePage ( page );
page = NULL;


return !!bitmap;
}



















void RenderPdf ( const char *name )
{

#if 0
size_t file_length = 0;
std::unique_ptr<char, pdfium::FreeDeleter> file_contents = GetFileContents ( name, &file_length );

TestLoader loader ( file_contents.get(), file_length );

FPDF_FILEACCESS file_access = {};
file_access.m_FileLen = static_cast<unsigned long>( file_length );
file_access.m_GetBlock = TestLoader::GetBlock;
file_access.m_Param = &loader;

FPDF_DOCUMENT doc = FPDF_LoadCustomDocument ( &file_access, nullptr );
#else

FPDF_DOCUMENT doc = FPDF_LoadDocument ( name, NULL );
#endif


if ( ! doc )
return;


int page_count = FPDF_GetPageCount ( doc );

int rendered_pages = 0;
int bad_pages = 0;

for (int i = 0; i < page_count; ++i)
{
if ( RenderPage ( name, doc, i ))
{
++rendered_pages;
}
else
{
++bad_pages;
}
}

fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
if (bad_pages)
fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);


FPDF_CloseDocument(doc);

}







int main ( int argc, const char* argv[] )
{
FPDF_InitLibrary ( );

char fname[] = "bad_pg_10.pdf";
fprintf(stderr, "Rendering PDF file %s.\n", fname );

RenderPdf ( fname );

FPDF_DestroyLibrary();

return 0;
}