blob: b633512fe2776db82d30f9a9db48f5e8780ccd15 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
DRCcd2c5d42011-08-11 11:18:34 +00002 * Copyright (C) 2011 D. R. Commander
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00003 *
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This software is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this software; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
18 */
19#ifndef __RFB_TIGHTENCODER_H__
20#define __RFB_TIGHTENCODER_H__
21
22#include <rdr/MemOutStream.h>
23#include <rdr/ZlibOutStream.h>
24#include <rfb/Encoder.h>
Pierre Ossmanb948a912014-01-15 13:23:43 +010025#include <rfb/JpegCompressor.h>
Pierre Ossman65ad3222014-03-07 13:48:29 +010026#include <rfb/Palette.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000027
28// FIXME: Check if specifying extern "C" is really necessary.
29#include <stdio.h>
30extern "C" {
Constantin Kaplinskye19ac6d2006-05-30 06:15:20 +000031#include <jpeglib.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032}
33
34namespace rfb {
35
Pierre Ossman456b2c22014-01-15 13:22:03 +010036 class TransImageGetter;
37
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038 struct TIGHT_CONF {
39 unsigned int maxRectSize, maxRectWidth;
40 unsigned int monoMinRectSize;
41 int idxZlibLevel, monoZlibLevel, rawZlibLevel;
42 int idxMaxColorsDivisor;
DRCcd2c5d42011-08-11 11:18:34 +000043 int palMaxColorsWithJPEG;
DRC773cf3c2009-03-12 19:26:44 +000044 int jpegQuality;
Pierre Ossmanb948a912014-01-15 13:23:43 +010045 int jpegSubsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046 };
47
48 //
49 // Compression level stuff. The following array contains various
50 // encoder parameters for each of 10 compression levels (0..9).
51 // Last three parameters correspond to JPEG quality levels (0..9).
52 //
53 // NOTE: s_conf[9].maxRectSize should be >= s_conf[i].maxRectSize,
54 // where i in [0..8]. RequiredBuffSize() method depends on this.
55 // FIXME: Is this comment obsolete?
56 //
57
58
59 class TightEncoder : public Encoder {
60 public:
Pierre Ossman316a3242014-01-15 12:40:20 +010061 TightEncoder(SMsgWriter* writer);
62 virtual ~TightEncoder();
63
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000064 virtual void setCompressLevel(int level);
65 virtual void setQualityLevel(int level);
Pierre Ossmanb948a912014-01-15 13:23:43 +010066 virtual void setFineQualityLevel(int quality, int subsampling);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067 virtual int getNumRects(const Rect &r);
Pierre Ossman717c07b2014-01-21 14:45:10 +010068 virtual void writeRect(const Rect& r, TransImageGetter* ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069
70 private:
DRCffe09d62011-08-17 02:27:59 +000071 bool checkSolidTile(Rect& r, rdr::U32* colorPtr, bool needSameColor);
72 void extendSolidArea(const Rect& r, rdr::U32 colorValue, Rect& er);
73 void findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr);
74 void sendRectSimple(const Rect& r);
75 void writeSubrect(const Rect& r, bool forceSolid = false);
76
Pierre Ossman7b5c0692014-03-17 14:35:51 +010077 void writeCompact(rdr::OutStream* os, rdr::U32 value);
78
DRC3004cb62011-11-03 18:45:39 +000079 void compressData(const void *buf, unsigned int length,
80 rdr::ZlibOutStream *zos, int zlibLevel,
81 rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +000082
DRC56dee1a2012-02-13 03:55:10 +000083 void fastFillPalette8(const rdr::U8 *data, int stride, const Rect &r);
84 void fastFillPalette16(const rdr::U16 *data, int stride, const Rect &r);
85 void fastFillPalette32(const rdr::U32 *data, int stride, const Rect &r);
DRCffe09d62011-08-17 02:27:59 +000086
87 void fillPalette8(rdr::U8 *data, int count);
88 void fillPalette16(rdr::U16 *data, int count);
89 void fillPalette32(rdr::U32 *data, int count);
90
91 unsigned int packPixels8(rdr::U8 *buf, unsigned int count);
92 unsigned int packPixels16(rdr::U16 *buf, unsigned int count);
93 unsigned int packPixels32(rdr::U32 *buf, unsigned int count);
94
95 void tightEncode8(const Rect& r, rdr::OutStream *os, bool forceSolid);
96 void tightEncode16(const Rect& r, rdr::OutStream *os, bool forceSolid);
97 void tightEncode32(const Rect& r, rdr::OutStream *os, bool forceSolid);
98
99 bool checkSolidTile8(Rect& r, rdr::U32 *colorPtr, bool needSameColor);
100 bool checkSolidTile16(Rect& r, rdr::U32 *colorPtr, bool needSameColor);
101 bool checkSolidTile32(Rect& r, rdr::U32 *colorPtr, bool needSameColor);
102
DRC3004cb62011-11-03 18:45:39 +0000103 void encodeSolidRect8(rdr::U8 *buf, rdr::OutStream *os);
104 void encodeSolidRect16(rdr::U16 *buf, rdr::OutStream *os);
105 void encodeSolidRect32(rdr::U32 *buf, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000106
DRC3004cb62011-11-03 18:45:39 +0000107 void encodeFullColorRect8(rdr::U8 *buf, const Rect& r, rdr::OutStream *os);
108 void encodeFullColorRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os);
109 void encodeFullColorRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000110
DRC3004cb62011-11-03 18:45:39 +0000111 void encodeMonoRect8(rdr::U8 *buf, const Rect& r, rdr::OutStream *os);
112 void encodeMonoRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os);
113 void encodeMonoRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000114
DRC3004cb62011-11-03 18:45:39 +0000115 void encodeIndexedRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os);
116 void encodeIndexedRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000117
DRC56dee1a2012-02-13 03:55:10 +0000118 void encodeJpegRect16(rdr::U16 *buf, int stride, const Rect& r,
119 rdr::OutStream *os);
120 void encodeJpegRect32(rdr::U32 *buf, int stride, const Rect& r,
121 rdr::OutStream *os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000123 rdr::MemOutStream mos;
124 rdr::ZlibOutStream zos[4];
DRCcd2c5d42011-08-11 11:18:34 +0000125 JpegCompressor jc;
DRCffe09d62011-08-17 02:27:59 +0000126 TransImageGetter *ig;
127 PixelFormat serverpf, clientpf;
128
129 bool pack24;
Pierre Ossman65ad3222014-03-07 13:48:29 +0100130 int palMaxColors;
131 Palette palette;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000132
133 static const int defaultCompressLevel;
134 static const TIGHT_CONF conf[];
135
136 const TIGHT_CONF* pconf;
DRCb4a83232011-08-19 04:57:18 +0000137 int jpegQuality;
Pierre Ossmanb948a912014-01-15 13:23:43 +0100138 int jpegSubsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000139 };
140
141}
142
143#endif