From ae90d2c485318beb8b4b938d09ffaf5c6f0a5e21 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 18 Aug 2017 19:52:53 -0400 Subject: Implement Pl_DCT pipeline Additional testing is added in later commits to be supported by additional changes in the library. --- libtests/build.mk | 2 + libtests/dct_compress.cc | 93 +++++++++++++++++++++++++++++++++++++++++++++ libtests/dct_uncompress.cc | 44 +++++++++++++++++++++ libtests/qtest/dct.test | 66 ++++++++++++++++++++++++++++++++ libtests/qtest/dct/rawdata | Bin 0 -> 102400 bytes 5 files changed, 205 insertions(+) create mode 100644 libtests/dct_compress.cc create mode 100644 libtests/dct_uncompress.cc create mode 100644 libtests/qtest/dct.test create mode 100644 libtests/qtest/dct/rawdata (limited to 'libtests') diff --git a/libtests/build.mk b/libtests/build.mk index 7977c8c5..baa34125 100644 --- a/libtests/build.mk +++ b/libtests/build.mk @@ -4,6 +4,8 @@ BINS_libtests = \ bits \ buffer \ concatenate \ + dct_compress \ + dct_uncompress \ flate \ hex \ input_source \ diff --git a/libtests/dct_compress.cc b/libtests/dct_compress.cc new file mode 100644 index 00000000..ea5d9c33 --- /dev/null +++ b/libtests/dct_compress.cc @@ -0,0 +1,93 @@ +#include +#include +#include + +#include +#include +#include +#include + +static void usage() +{ + std::cerr << "Usage: dct_compress infile outfile width height" + << " {rgb|cmyk|gray}" << std::endl; + exit(2); +} + +class Callback: public Pl_DCT::CompressConfig +{ + public: + virtual ~Callback() + { + } + virtual void apply(jpeg_compress_struct*); + bool called = false; +}; + +void Callback::apply(jpeg_compress_struct*) +{ + this->called = true; +} + +int main(int argc, char* argv[]) +{ + if (argc != 6) + { + usage(); + } + + char* infilename = argv[1]; + char* outfilename = argv[2]; + unsigned int width = atoi(argv[3]); + unsigned int height = atoi(argv[4]); + char* colorspace = argv[5]; + J_COLOR_SPACE cs = + ((strcmp(colorspace, "rgb") == 0) ? JCS_RGB : + (strcmp(colorspace, "cmyk") == 0) ? JCS_CMYK : + (strcmp(colorspace, "gray") == 0) ? JCS_GRAYSCALE : + JCS_UNKNOWN); + int components = 0; + switch (cs) + { + case JCS_RGB: + components = 3; + break; + case JCS_CMYK: + components = 4; + break; + case JCS_GRAYSCALE: + components = 1; + break; + default: + usage(); + break; + } + + FILE* infile = QUtil::safe_fopen(infilename, "rb"); + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); + Pl_StdioFile out("stdout", outfile); + unsigned char buf[100]; + bool done = false; + Callback callback; + Pl_DCT dct("dct", &out, width, height, components, cs, &callback); + while (! done) + { + size_t len = fread(buf, 1, sizeof(buf), infile); + if (len <= 0) + { + done = true; + } + else + { + dct.write(buf, len); + } + } + dct.finish(); + if (! callback.called) + { + std::cout << "Callback was not called" << std::endl; + } + fclose(infile); + fclose(outfile); + return 0; +} diff --git a/libtests/dct_uncompress.cc b/libtests/dct_uncompress.cc new file mode 100644 index 00000000..1ab1ae21 --- /dev/null +++ b/libtests/dct_uncompress.cc @@ -0,0 +1,44 @@ +#include +#include +#include + +#include +#include +#include +#include + +int main(int argc, char* argv[]) +{ + if (argc != 3) + { + std::cerr << "Usage: dct_uncompress infile outfile" + << std::endl; + exit(2); + } + + char* infilename = argv[1]; + char* outfilename = argv[2]; + + FILE* infile = QUtil::safe_fopen(infilename, "rb"); + FILE* outfile = QUtil::safe_fopen(outfilename, "wb"); + Pl_StdioFile out("stdout", outfile); + unsigned char buf[100]; + bool done = false; + Pl_DCT dct("dct", &out); + while (! done) + { + size_t len = fread(buf, 1, sizeof(buf), infile); + if (len <= 0) + { + done = true; + } + else + { + dct.write(buf, len); + } + } + dct.finish(); + fclose(infile); + fclose(outfile); + return 0; +} diff --git a/libtests/qtest/dct.test b/libtests/qtest/dct.test new file mode 100644 index 00000000..f3b28581 --- /dev/null +++ b/libtests/qtest/dct.test @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +require 5.008; +use warnings; +use strict; + +chdir("dct") or die "chdir testdir failed: $!\n"; + +require TestDriver; + +# This test suite does light verification of DCT by running some data +# through a round trip with one encoding system. The +# examples/pdf-create program also exercises DCT but does so more +# fully. + +my $td = new TestDriver('dct'); + +cleanup(); + +$td->runtest("compress", + {$td->COMMAND => "dct_compress rawdata a.jpg 400 256 gray"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); +$td->runtest("decompress", + {$td->COMMAND => "dct_uncompress a.jpg out"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); +# Compare +my @raw = get_data('rawdata'); +my @processed = get_data('out'); +my $checked_data = 0; +if ($td->runtest("bytes in data", + {$td->STRING => scalar(@processed)}, + {$td->STRING => scalar(@raw)})) +{ + my $mismatch = 0; + for (my $i = 0; $i < scalar(@raw); ++$i) + { + $checked_data = 1; + my $delta = abs(ord($raw[$i]) - ord($processed[$i])); + if ($delta > 10) + { + ++$mismatch; + } + } + $td->runtest("data is close enough", + {$td->STRING => $mismatch}, + {$td->STRING => '0'}); +} + +cleanup(); + +$td->report(3 + $checked_data); + +sub cleanup +{ + system("rm -f a.jpg out"); +} + +sub get_data +{ + my $file = shift; + local $/ = undef; + open(F, "<$file") || die; + binmode(F); + my $data = ; + close(F); + split('', $data); +} diff --git a/libtests/qtest/dct/rawdata b/libtests/qtest/dct/rawdata new file mode 100644 index 00000000..a3594098 Binary files /dev/null and b/libtests/qtest/dct/rawdata differ -- cgit v1.2.3-54-g00ecf