aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/JpegCompressor.h
blob: 93fdc7bf96cb22cc7fc82a7c8551f99cd42806a1 (plain)
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
#ifndef __JPEGCOMPRESSOR_H__
#define __JPEGCOMPRESSOR_H__

#include <stdio.h>
#include <sys/types.h>
extern "C" {
#include <jpeg/jpeglib.h>
}

#include <rdr/types.h>
#include <rfb/PixelFormat.h>

namespace rfb {

  //
  // An abstract interface for performing JPEG compression.
  //

  class JpegCompressor
  {
  public:
    virtual ~JpegCompressor() {}

    // Set JPEG quality level (0..100)
    virtual void setQuality(int level) = 0;

    // Actually compress an image.
    virtual void compress(const rdr::U32 *buf, const PixelFormat *fmt,
                          int w, int h, int stride) = 0;

    // Access results of the compression.
    virtual size_t getDataLength() = 0;
    virtual const char *getDataPtr() = 0;
  };

  //
  // A C++ class for performing JPEG compression via the
  // Independent JPEG Group's software (free JPEG library).
  //

  class StandardJpegCompressor : public JpegCompressor
  {
  public:
    StandardJpegCompressor();
    virtual ~StandardJpegCompressor();

    // Set JPEG quality level (0..100)
    virtual void setQuality(int level);

    // Actually compress the image.
    virtual void compress(const rdr::U32 *buf, const PixelFormat *fmt,
                          int w, int h, int stride);

    // Access results of the compression.
    virtual size_t getDataLength() { return m_cdata_ready; }
    virtual const char *getDataPtr() { return (const char *)m_cdata; }

  public:
    // Our implementation of JPEG destination manager. These three
    // functions should never be called directly. They are made public
    // because they should be accessible from C-compatible functions
    // called by the JPEG library.
    void initDestination();
    bool emptyOutputBuffer();
    void termDestination();

  protected:
    static const int ALLOC_CHUNK_SIZE;
    static const int DEFAULT_QUALITY;

    struct jpeg_compress_struct m_cinfo;
    struct jpeg_error_mgr m_jerr;

    unsigned char *m_cdata;
    size_t m_cdata_allocated;
    size_t m_cdata_ready;
  };

}

#endif // __JPEGCOMPRESSOR_H__