Commit 4edfe1f41d386c0fa6043926d761219bdb29e7c0

Authored by Jay Berkenbilt
1 parent 38bdbc07

Add tests for new PNG filters

libqpdf/Pl_PNGFilter.cc
1 #include <qpdf/Pl_PNGFilter.hh> 1 #include <qpdf/Pl_PNGFilter.hh>
  2 +#include <qpdf/QTC.hh>
2 #include <stdexcept> 3 #include <stdexcept>
3 #include <string.h> 4 #include <string.h>
4 #include <limits.h> 5 #include <limits.h>
@@ -134,6 +135,7 @@ Pl_PNGFilter::decodeRow() @@ -134,6 +135,7 @@ Pl_PNGFilter::decodeRow()
134 void 135 void
135 Pl_PNGFilter::decodeSub() 136 Pl_PNGFilter::decodeSub()
136 { 137 {
  138 + QTC::TC("libtests", "Pl_PNGFilter decodeSub");
137 unsigned char* buffer = this->cur_row + 1; 139 unsigned char* buffer = this->cur_row + 1;
138 unsigned int bpp = this->bytes_per_pixel; 140 unsigned int bpp = this->bytes_per_pixel;
139 141
@@ -153,6 +155,7 @@ Pl_PNGFilter::decodeSub() @@ -153,6 +155,7 @@ Pl_PNGFilter::decodeSub()
153 void 155 void
154 Pl_PNGFilter::decodeUp() 156 Pl_PNGFilter::decodeUp()
155 { 157 {
  158 + QTC::TC("libtests", "Pl_PNGFilter decodeUp");
156 unsigned char* buffer = this->cur_row + 1; 159 unsigned char* buffer = this->cur_row + 1;
157 unsigned char* above_buffer = this->prev_row + 1; 160 unsigned char* above_buffer = this->prev_row + 1;
158 161
@@ -166,6 +169,7 @@ Pl_PNGFilter::decodeUp() @@ -166,6 +169,7 @@ Pl_PNGFilter::decodeUp()
166 void 169 void
167 Pl_PNGFilter::decodeAverage() 170 Pl_PNGFilter::decodeAverage()
168 { 171 {
  172 + QTC::TC("libtests", "Pl_PNGFilter decodeAverage");
169 unsigned char* buffer = this->cur_row + 1; 173 unsigned char* buffer = this->cur_row + 1;
170 unsigned char* above_buffer = this->prev_row + 1; 174 unsigned char* above_buffer = this->prev_row + 1;
171 unsigned int bpp = this->bytes_per_pixel; 175 unsigned int bpp = this->bytes_per_pixel;
@@ -188,6 +192,7 @@ Pl_PNGFilter::decodeAverage() @@ -188,6 +192,7 @@ Pl_PNGFilter::decodeAverage()
188 void 192 void
189 Pl_PNGFilter::decodePaeth() 193 Pl_PNGFilter::decodePaeth()
190 { 194 {
  195 + QTC::TC("libtests", "Pl_PNGFilter decodePaeth");
191 unsigned char* buffer = this->cur_row + 1; 196 unsigned char* buffer = this->cur_row + 1;
192 unsigned char* above_buffer = this->prev_row + 1; 197 unsigned char* above_buffer = this->prev_row + 1;
193 unsigned int bpp = this->bytes_per_pixel; 198 unsigned int bpp = this->bytes_per_pixel;
libtests/libtests.testcov
@@ -29,3 +29,7 @@ Pl_RunLength flush full buffer 1 @@ -29,3 +29,7 @@ Pl_RunLength flush full buffer 1
29 Pl_RunLength flush empty buffer 0 29 Pl_RunLength flush empty buffer 0
30 Pl_DCT empty_pipeline_output_buffer 0 30 Pl_DCT empty_pipeline_output_buffer 0
31 Pl_DCT term_pipeline_destination 0 31 Pl_DCT term_pipeline_destination 0
  32 +Pl_PNGFilter decodeSub 0
  33 +Pl_PNGFilter decodeUp 0
  34 +Pl_PNGFilter decodeAverage 0
  35 +Pl_PNGFilter decodePaeth 0
libtests/png_filter.cc
@@ -8,7 +8,8 @@ @@ -8,7 +8,8 @@
8 #include <string.h> 8 #include <string.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 10
11 -void run(char const* filename, bool encode, unsigned int columns) 11 +void run(char const* filename, bool encode, unsigned int columns,
  12 + int bits_per_sample, int samples_per_pixel)
12 { 13 {
13 // Decode the file 14 // Decode the file
14 FILE* in = QUtil::safe_fopen(filename, "rb"); 15 FILE* in = QUtil::safe_fopen(filename, "rb");
@@ -17,7 +18,7 @@ void run(char const* filename, bool encode, unsigned int columns) @@ -17,7 +18,7 @@ void run(char const* filename, bool encode, unsigned int columns)
17 Pipeline* pl = new Pl_PNGFilter( 18 Pipeline* pl = new Pl_PNGFilter(
18 "png", out, 19 "png", out,
19 encode ? Pl_PNGFilter::a_encode : Pl_PNGFilter::a_decode, 20 encode ? Pl_PNGFilter::a_encode : Pl_PNGFilter::a_decode,
20 - columns); 21 + columns, samples_per_pixel, bits_per_sample);
21 assert((2 * (columns + 1)) < 1024); 22 assert((2 * (columns + 1)) < 1024);
22 unsigned char buf[1024]; 23 unsigned char buf[1024];
23 size_t len; 24 size_t len;
@@ -54,18 +55,22 @@ void run(char const* filename, bool encode, unsigned int columns) @@ -54,18 +55,22 @@ void run(char const* filename, bool encode, unsigned int columns)
54 55
55 int main(int argc, char* argv[]) 56 int main(int argc, char* argv[])
56 { 57 {
57 - if (argc != 4) 58 + if (argc != 6)
58 { 59 {
59 - std::cerr << "Usage: pipeline {en,de}code filename columns" << std::endl; 60 + std::cerr << "Usage: png_filter {en,de}code filename"
  61 + << " columns samples-per-pixel bits-per-sample"
  62 + << std::endl;
60 exit(2); 63 exit(2);
61 } 64 }
62 bool encode = (strcmp(argv[1], "encode") == 0); 65 bool encode = (strcmp(argv[1], "encode") == 0);
63 char* filename = argv[2]; 66 char* filename = argv[2];
64 int columns = QUtil::string_to_int(argv[3]); 67 int columns = QUtil::string_to_int(argv[3]);
  68 + int samples_per_pixel = QUtil::string_to_int(argv[4]);
  69 + int bits_per_sample = QUtil::string_to_int(argv[5]);
65 70
66 try 71 try
67 { 72 {
68 - run(filename, encode, columns); 73 + run(filename, encode, columns, bits_per_sample, samples_per_pixel);
69 } 74 }
70 catch (std::exception& e) 75 catch (std::exception& e)
71 { 76 {
libtests/qtest/png_filter.test
@@ -14,7 +14,7 @@ my $td = new TestDriver(&#39;png_filter&#39;); @@ -14,7 +14,7 @@ my $td = new TestDriver(&#39;png_filter&#39;);
14 cleanup(); 14 cleanup();
15 15
16 $td->runtest("decode columns = 4", 16 $td->runtest("decode columns = 4",
17 - {$td->COMMAND => "png_filter decode in1 4"}, 17 + {$td->COMMAND => "png_filter decode in1 4 1 8"},
18 {$td->STRING => "done\n", 18 {$td->STRING => "done\n",
19 $td->EXIT_STATUS => 0}, 19 $td->EXIT_STATUS => 0},
20 $td->NORMALIZE_NEWLINES); 20 $td->NORMALIZE_NEWLINES);
@@ -24,7 +24,7 @@ $td-&gt;runtest(&quot;check output&quot;, @@ -24,7 +24,7 @@ $td-&gt;runtest(&quot;check output&quot;,
24 {$td->FILE => "out1"}); 24 {$td->FILE => "out1"});
25 25
26 $td->runtest("decode columns = 5", 26 $td->runtest("decode columns = 5",
27 - {$td->COMMAND => "png_filter decode in2 5"}, 27 + {$td->COMMAND => "png_filter decode in2 5 1 8"},
28 {$td->STRING => "done\n", 28 {$td->STRING => "done\n",
29 $td->EXIT_STATUS => 0}, 29 $td->EXIT_STATUS => 0},
30 $td->NORMALIZE_NEWLINES); 30 $td->NORMALIZE_NEWLINES);
@@ -34,7 +34,7 @@ $td-&gt;runtest(&quot;check output&quot;, @@ -34,7 +34,7 @@ $td-&gt;runtest(&quot;check output&quot;,
34 {$td->FILE => "out2"}); 34 {$td->FILE => "out2"});
35 35
36 $td->runtest("encode columns = 4", 36 $td->runtest("encode columns = 4",
37 - {$td->COMMAND => "png_filter encode out1 4"}, 37 + {$td->COMMAND => "png_filter encode out1 4 1 8"},
38 {$td->STRING => "done\n", 38 {$td->STRING => "done\n",
39 $td->EXIT_STATUS => 0}, 39 $td->EXIT_STATUS => 0},
40 $td->NORMALIZE_NEWLINES); 40 $td->NORMALIZE_NEWLINES);
@@ -44,7 +44,7 @@ $td-&gt;runtest(&quot;check output&quot;, @@ -44,7 +44,7 @@ $td-&gt;runtest(&quot;check output&quot;,
44 {$td->FILE => "in1"}); 44 {$td->FILE => "in1"});
45 45
46 $td->runtest("encode columns = 5", 46 $td->runtest("encode columns = 5",
47 - {$td->COMMAND => "png_filter encode out2 5"}, 47 + {$td->COMMAND => "png_filter encode out2 5 1 8"},
48 {$td->STRING => "done\n", 48 {$td->STRING => "done\n",
49 $td->EXIT_STATUS => 0}, 49 $td->EXIT_STATUS => 0},
50 $td->NORMALIZE_NEWLINES); 50 $td->NORMALIZE_NEWLINES);
@@ -53,9 +53,41 @@ $td-&gt;runtest(&quot;check output&quot;, @@ -53,9 +53,41 @@ $td-&gt;runtest(&quot;check output&quot;,
53 {$td->FILE => "out"}, 53 {$td->FILE => "out"},
54 {$td->FILE => "in2"}); 54 {$td->FILE => "in2"});
55 55
  56 +my @other = (
  57 + '01--32-3-16',
  58 + '02--32-1-8',
  59 + '03--32-3-8',
  60 + '04--32-1-8',
  61 + '05--32-3-8',
  62 + '06--32-1-8',
  63 + '07--32-3-8',
  64 + '08--32-1-8',
  65 + '09--32-3-8',
  66 + '10--32-1-8',
  67 + '11--32-3-8',
  68 + '12--32-1-4',
  69 + );
  70 +
  71 +foreach my $i (@other)
  72 +{
  73 + $i =~ m/^.*?--(\d+)-(\d+)-(\d+)$/ or die;
  74 + my $columns = $1;
  75 + my $samples_per_pixel = $2;
  76 + my $bits_per_sample = $3;
  77 + $td->runtest("decode $i",
  78 + {$td->COMMAND => "png_filter decode $i.data" .
  79 + " $columns $samples_per_pixel $bits_per_sample"},
  80 + {$td->STRING => "done\n",
  81 + $td->EXIT_STATUS => 0},
  82 + $td->NORMALIZE_NEWLINES);
  83 + $td->runtest("check output for $i",
  84 + {$td->FILE => "out"},
  85 + {$td->FILE => "$i.decoded"});
  86 +}
  87 +
56 cleanup(); 88 cleanup();
57 89
58 -$td->report(8); 90 +$td->report(8 + (2 * scalar(@other)));
59 91
60 sub cleanup 92 sub cleanup
61 { 93 {
qpdf/qtest/qpdf.test
@@ -2336,6 +2336,29 @@ $td-&gt;runtest(&quot;convert inline-images to qdf&quot;, @@ -2336,6 +2336,29 @@ $td-&gt;runtest(&quot;convert inline-images to qdf&quot;,
2336 compare_pdfs("inline-images.pdf", "a.pdf"); 2336 compare_pdfs("inline-images.pdf", "a.pdf");
2337 2337
2338 show_ntests(); 2338 show_ntests();
  2339 +
  2340 +
  2341 +# ----------
  2342 +$td->notify("--- PNG filtering Tests ---");
  2343 +$n_tests += 2;
  2344 +$n_compare_pdfs += 1;
  2345 +
  2346 +# The PDF file was submitted on bug #83 on github. All the PNG filters
  2347 +# are exercised. The test suite does not exercise PNG predictors with
  2348 +# LZW because I don't have a way to create such a file, but it's very
  2349 +# likely that it will work since the handling of the PNG filters is
  2350 +# separate from the regular decompression.
  2351 +$td->runtest("decode png-filtering",
  2352 + {$td->COMMAND => "qpdf --static-id" .
  2353 + " --compress-streams=n --decode-level=generalized" .
  2354 + " png-filters.pdf a.pdf"},
  2355 + {$td->STRING => "", $td->EXIT_STATUS => 0});
  2356 +$td->runtest("check output",
  2357 + {$td->FILE => "a.pdf"},
  2358 + {$td->FILE => "png-filters-decoded.pdf"});
  2359 +compare_pdfs("png-filters.pdf", "a.pdf");
  2360 +
  2361 +show_ntests();
2339 # ---------- 2362 # ----------
2340 $td->notify("--- fix-qdf Tests ---"); 2363 $td->notify("--- fix-qdf Tests ---");
2341 $n_tests += 5; 2364 $n_tests += 5;
qpdf/qtest/qpdf/png-filters-decoded.pdf 0 → 100644
No preview for this file type
qpdf/qtest/qpdf/png-filters.pdf 0 → 100644
No preview for this file type