2017-08-18 23:52:53 +00:00
|
|
|
#include <qpdf/Pl_DCT.hh>
|
|
|
|
#include <qpdf/Pl_StdioFile.hh>
|
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
|
|
|
|
#include <iostream>
|
2023-05-20 11:22:32 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2017-08-18 23:52:53 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
static void
|
|
|
|
usage()
|
2017-08-18 23:52:53 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Usage: dct_compress infile outfile width height"
|
|
|
|
<< " {rgb|cmyk|gray}" << std::endl;
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Callback: public Pl_DCT::CompressConfig
|
|
|
|
{
|
|
|
|
public:
|
2017-08-22 11:20:55 +00:00
|
|
|
Callback() :
|
|
|
|
called(false)
|
|
|
|
{
|
|
|
|
}
|
2023-05-20 13:25:46 +00:00
|
|
|
~Callback() override = default;
|
|
|
|
void apply(jpeg_compress_struct*) override;
|
2017-08-22 11:20:55 +00:00
|
|
|
bool called;
|
2017-08-18 23:52:53 +00:00
|
|
|
};
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
void
|
|
|
|
Callback::apply(jpeg_compress_struct*)
|
2017-08-18 23:52:53 +00:00
|
|
|
{
|
|
|
|
this->called = true;
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
2017-08-18 23:52:53 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (argc != 6) {
|
2017-08-18 23:52:53 +00:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
char* infilename = argv[1];
|
|
|
|
char* outfilename = argv[2];
|
2019-06-21 03:35:23 +00:00
|
|
|
JDIMENSION width = QUtil::string_to_uint(argv[3]);
|
|
|
|
JDIMENSION height = QUtil::string_to_uint(argv[4]);
|
2017-08-18 23:52:53 +00:00
|
|
|
char* colorspace = argv[5];
|
|
|
|
J_COLOR_SPACE cs =
|
2022-04-02 21:14:10 +00:00
|
|
|
((strcmp(colorspace, "rgb") == 0) ? JCS_RGB
|
|
|
|
: (strcmp(colorspace, "cmyk") == 0) ? JCS_CMYK
|
|
|
|
: (strcmp(colorspace, "gray") == 0) ? JCS_GRAYSCALE
|
|
|
|
: JCS_UNKNOWN);
|
2017-08-18 23:52:53 +00:00
|
|
|
int components = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (cs) {
|
|
|
|
case JCS_RGB:
|
2017-08-18 23:52:53 +00:00
|
|
|
components = 3;
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case JCS_CMYK:
|
2017-08-18 23:52:53 +00:00
|
|
|
components = 4;
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
case JCS_GRAYSCALE:
|
2017-08-18 23:52:53 +00:00
|
|
|
components = 1;
|
|
|
|
break;
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2017-08-18 23:52:53 +00:00
|
|
|
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);
|
2022-04-02 21:14:10 +00:00
|
|
|
while (!done) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t len = fread(buf, 1, sizeof(buf), infile);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (len <= 0) {
|
2022-02-08 14:18:08 +00:00
|
|
|
done = true;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
dct.write(buf, len);
|
|
|
|
}
|
2017-08-18 23:52:53 +00:00
|
|
|
}
|
|
|
|
dct.finish();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!callback.called) {
|
2017-08-18 23:52:53 +00:00
|
|
|
std::cout << "Callback was not called" << std::endl;
|
|
|
|
}
|
|
|
|
fclose(infile);
|
|
|
|
fclose(outfile);
|
|
|
|
return 0;
|
|
|
|
}
|