aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/dct_compress.cc
blob: ad8710b1fe14fe31a059ae1b7f8acb9b98160755 (plain) (blame)
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
#include <qpdf/Pl_DCT.hh>
#include <qpdf/Pl_StdioFile.hh>
#include <qpdf/QUtil.hh>

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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:
    Callback() :
        called(false)
    {
    }
    virtual ~Callback() = default;
    virtual void apply(jpeg_compress_struct*);
    bool called;
};

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];
    JDIMENSION width = QUtil::string_to_uint(argv[3]);
    JDIMENSION height = QUtil::string_to_uint(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;
}