2022-05-03 12:21:01 +00:00
|
|
|
#include <qpdf/assert_test.h>
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Pl_PNGFilter.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pl_TIFFPredictor.hh>
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-05-20 18:13:09 +00:00
|
|
|
#include <iostream>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
run(char const* filename,
|
|
|
|
char const* filter,
|
|
|
|
bool encode,
|
|
|
|
unsigned int columns,
|
|
|
|
unsigned int bits_per_sample,
|
|
|
|
unsigned int samples_per_pixel)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2013-02-28 21:45:11 +00:00
|
|
|
FILE* in = QUtil::safe_fopen(filename, "rb");
|
|
|
|
FILE* o1 = QUtil::safe_fopen("out", "wb");
|
2008-04-29 12:55:25 +00:00
|
|
|
Pipeline* out = new Pl_StdioFile("out", o1);
|
2023-05-20 11:46:50 +00:00
|
|
|
Pipeline* pl = nullptr;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (strcmp(filter, "png") == 0) {
|
2018-01-13 19:32:39 +00:00
|
|
|
pl = new Pl_PNGFilter(
|
2022-04-02 21:14:10 +00:00
|
|
|
"png",
|
|
|
|
out,
|
2018-01-13 19:32:39 +00:00
|
|
|
encode ? Pl_PNGFilter::a_encode : Pl_PNGFilter::a_decode,
|
2022-04-02 21:14:10 +00:00
|
|
|
columns,
|
|
|
|
samples_per_pixel,
|
|
|
|
bits_per_sample);
|
|
|
|
} else if (strcmp(filter, "tiff") == 0) {
|
2018-01-13 19:15:20 +00:00
|
|
|
pl = new Pl_TIFFPredictor(
|
2022-04-02 21:14:10 +00:00
|
|
|
"png",
|
|
|
|
out,
|
2018-01-13 19:15:20 +00:00
|
|
|
encode ? Pl_TIFFPredictor::a_encode : Pl_TIFFPredictor::a_decode,
|
2022-04-02 21:14:10 +00:00
|
|
|
columns,
|
|
|
|
samples_per_pixel,
|
|
|
|
bits_per_sample);
|
|
|
|
} else {
|
2018-01-13 19:32:39 +00:00
|
|
|
std::cerr << "unknown filter " << filter << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
assert((2 * (columns + 1)) < 1024);
|
|
|
|
unsigned char buf[1024];
|
|
|
|
size_t len;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (true) {
|
2022-02-08 14:18:08 +00:00
|
|
|
len = fread(buf, 1, (2 * columns) + 1, in);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (len == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pl->write(buf, len);
|
|
|
|
len = fread(buf, 1, 1, in);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (len == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pl->write(buf, len);
|
|
|
|
len = fread(buf, 1, 1, in);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (len == 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
pl->write(buf, len);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pl->finish();
|
|
|
|
delete pl;
|
|
|
|
delete out;
|
|
|
|
fclose(o1);
|
|
|
|
fclose(in);
|
|
|
|
|
|
|
|
std::cout << "done" << std::endl;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (argc != 7) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cerr << "Usage: predictor {png|tiff} {en,de}code filename"
|
2022-04-02 21:14:10 +00:00
|
|
|
<< " columns samples-per-pixel bits-per-sample" << std::endl;
|
2022-02-08 14:18:08 +00:00
|
|
|
exit(2);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
2018-01-13 19:32:39 +00:00
|
|
|
char* filter = argv[1];
|
|
|
|
bool encode = (strcmp(argv[2], "encode") == 0);
|
|
|
|
char* filename = argv[3];
|
|
|
|
int columns = QUtil::string_to_int(argv[4]);
|
|
|
|
int samples_per_pixel = QUtil::string_to_int(argv[5]);
|
|
|
|
int bits_per_sample = QUtil::string_to_int(argv[6]);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
try {
|
|
|
|
run(filename,
|
|
|
|
filter,
|
|
|
|
encode,
|
2019-06-21 03:35:23 +00:00
|
|
|
QIntC::to_uint(columns),
|
|
|
|
QIntC::to_uint(bits_per_sample),
|
|
|
|
QIntC::to_uint(samples_per_pixel));
|
2022-04-02 21:14:10 +00:00
|
|
|
} catch (std::exception& e) {
|
2022-02-08 14:18:08 +00:00
|
|
|
std::cout << e.what() << std::endl;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|