blob: 93f6decf50b3a58a6c5d239dd6485bcc90cd00b6 [file] [log] [blame]
Constantin Kaplinsky71a32f02007-07-24 12:10:16 +00001#ifndef __JPEGCOMPRESSOR_H__
2#define __JPEGCOMPRESSOR_H__
3
4#include <stdio.h>
5#include <sys/types.h>
6extern "C" {
Adam Tkaca0d5fe32008-10-22 14:47:09 +00007#include <jpeglib.h>
Constantin Kaplinsky71a32f02007-07-24 12:10:16 +00008}
9
10#include <rdr/types.h>
11#include <rfb/PixelFormat.h>
12
13namespace rfb {
14
15 //
16 // An abstract interface for performing JPEG compression.
17 //
18
19 class JpegCompressor
20 {
21 public:
22 virtual ~JpegCompressor() {}
23
24 // Set JPEG quality level (0..100)
25 virtual void setQuality(int level) = 0;
26
27 // Actually compress an image.
28 virtual void compress(const rdr::U32 *buf, const PixelFormat *fmt,
29 int w, int h, int stride) = 0;
30
31 // Access results of the compression.
32 virtual size_t getDataLength() = 0;
33 virtual const char *getDataPtr() = 0;
34 };
35
36 //
37 // A C++ class for performing JPEG compression via the
38 // Independent JPEG Group's software (free JPEG library).
39 //
40
41 class StandardJpegCompressor : public JpegCompressor
42 {
43 public:
44 StandardJpegCompressor();
45 virtual ~StandardJpegCompressor();
46
47 // Set JPEG quality level (0..100)
48 virtual void setQuality(int level);
49
50 // Actually compress the image.
51 virtual void compress(const rdr::U32 *buf, const PixelFormat *fmt,
52 int w, int h, int stride);
53
54 // Access results of the compression.
55 virtual size_t getDataLength() { return m_cdata_ready; }
56 virtual const char *getDataPtr() { return (const char *)m_cdata; }
57
58 public:
59 // Our implementation of JPEG destination manager. These three
60 // functions should never be called directly. They are made public
61 // because they should be accessible from C-compatible functions
62 // called by the JPEG library.
63 void initDestination();
64 bool emptyOutputBuffer();
65 void termDestination();
66
67 protected:
68 static const int ALLOC_CHUNK_SIZE;
69 static const int DEFAULT_QUALITY;
70
71 struct jpeg_compress_struct m_cinfo;
72 struct jpeg_error_mgr m_jerr;
73
74 unsigned char *m_cdata;
75 size_t m_cdata_allocated;
76 size_t m_cdata_ready;
77 };
78
79}
80
81#endif // __JPEGCOMPRESSOR_H__
82