blob: 465d8ed680d406aea5daf745af39aaaa79be7c96 [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>
25
26// FIXME: Check if specifying extern "C" is really necessary.
27#include <stdio.h>
28extern "C" {
Constantin Kaplinskye19ac6d2006-05-30 06:15:20 +000029#include <jpeglib.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000030}
31
32namespace rfb {
33
Pierre Ossman456b2c22014-01-15 13:22:03 +010034 class TransImageGetter;
35
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000036 struct TIGHT_CONF {
37 unsigned int maxRectSize, maxRectWidth;
38 unsigned int monoMinRectSize;
39 int idxZlibLevel, monoZlibLevel, rawZlibLevel;
40 int idxMaxColorsDivisor;
DRCcd2c5d42011-08-11 11:18:34 +000041 int palMaxColorsWithJPEG;
DRC773cf3c2009-03-12 19:26:44 +000042 int jpegQuality;
DRCb4a83232011-08-19 04:57:18 +000043 JPEG_SUBSAMP jpegSubsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044 };
45
46 //
DRCffe09d62011-08-17 02:27:59 +000047 // C-style structures to store palette entries and compression paramentes.
48 // Such code probably should be converted into C++ classes.
49 //
50
51 struct TIGHT_COLOR_LIST {
52 TIGHT_COLOR_LIST *next;
53 int idx;
54 rdr::U32 rgb;
55 };
56
57 struct TIGHT_PALETTE_ENTRY {
58 TIGHT_COLOR_LIST *listNode;
59 int numPixels;
60 };
61
62 struct TIGHT_PALETTE {
63 TIGHT_PALETTE_ENTRY entry[256];
64 TIGHT_COLOR_LIST *hash[256];
65 TIGHT_COLOR_LIST list[256];
66 };
67
68 //
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069 // Compression level stuff. The following array contains various
70 // encoder parameters for each of 10 compression levels (0..9).
71 // Last three parameters correspond to JPEG quality levels (0..9).
72 //
73 // NOTE: s_conf[9].maxRectSize should be >= s_conf[i].maxRectSize,
74 // where i in [0..8]. RequiredBuffSize() method depends on this.
75 // FIXME: Is this comment obsolete?
76 //
77
78
79 class TightEncoder : public Encoder {
80 public:
81 static Encoder* create(SMsgWriter* writer);
82 virtual void setCompressLevel(int level);
83 virtual void setQualityLevel(int level);
DRCb4a83232011-08-19 04:57:18 +000084 virtual void setFineQualityLevel(int quality, JPEG_SUBSAMP subsampling);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000085 virtual int getNumRects(const Rect &r);
DRCffe09d62011-08-17 02:27:59 +000086 virtual bool writeRect(const Rect& r, TransImageGetter* ig, Rect* actual);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 virtual ~TightEncoder();
88
89 private:
90 TightEncoder(SMsgWriter* writer);
DRCffe09d62011-08-17 02:27:59 +000091 bool checkSolidTile(Rect& r, rdr::U32* colorPtr, bool needSameColor);
92 void extendSolidArea(const Rect& r, rdr::U32 colorValue, Rect& er);
93 void findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr);
94 void sendRectSimple(const Rect& r);
95 void writeSubrect(const Rect& r, bool forceSolid = false);
96
DRC3004cb62011-11-03 18:45:39 +000097 void compressData(const void *buf, unsigned int length,
98 rdr::ZlibOutStream *zos, int zlibLevel,
99 rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000100
101 int paletteInsert(rdr::U32 rgb, int numPixels, int bpp);
102 void paletteReset(void);
103
DRC56dee1a2012-02-13 03:55:10 +0000104 void fastFillPalette8(const rdr::U8 *data, int stride, const Rect &r);
105 void fastFillPalette16(const rdr::U16 *data, int stride, const Rect &r);
106 void fastFillPalette32(const rdr::U32 *data, int stride, const Rect &r);
DRCffe09d62011-08-17 02:27:59 +0000107
108 void fillPalette8(rdr::U8 *data, int count);
109 void fillPalette16(rdr::U16 *data, int count);
110 void fillPalette32(rdr::U32 *data, int count);
111
112 unsigned int packPixels8(rdr::U8 *buf, unsigned int count);
113 unsigned int packPixels16(rdr::U16 *buf, unsigned int count);
114 unsigned int packPixels32(rdr::U32 *buf, unsigned int count);
115
116 void tightEncode8(const Rect& r, rdr::OutStream *os, bool forceSolid);
117 void tightEncode16(const Rect& r, rdr::OutStream *os, bool forceSolid);
118 void tightEncode32(const Rect& r, rdr::OutStream *os, bool forceSolid);
119
120 bool checkSolidTile8(Rect& r, rdr::U32 *colorPtr, bool needSameColor);
121 bool checkSolidTile16(Rect& r, rdr::U32 *colorPtr, bool needSameColor);
122 bool checkSolidTile32(Rect& r, rdr::U32 *colorPtr, bool needSameColor);
123
DRC3004cb62011-11-03 18:45:39 +0000124 void encodeSolidRect8(rdr::U8 *buf, rdr::OutStream *os);
125 void encodeSolidRect16(rdr::U16 *buf, rdr::OutStream *os);
126 void encodeSolidRect32(rdr::U32 *buf, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000127
DRC3004cb62011-11-03 18:45:39 +0000128 void encodeFullColorRect8(rdr::U8 *buf, const Rect& r, rdr::OutStream *os);
129 void encodeFullColorRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os);
130 void encodeFullColorRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000131
DRC3004cb62011-11-03 18:45:39 +0000132 void encodeMonoRect8(rdr::U8 *buf, const Rect& r, rdr::OutStream *os);
133 void encodeMonoRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os);
134 void encodeMonoRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000135
DRC3004cb62011-11-03 18:45:39 +0000136 void encodeIndexedRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os);
137 void encodeIndexedRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os);
DRCffe09d62011-08-17 02:27:59 +0000138
DRC56dee1a2012-02-13 03:55:10 +0000139 void encodeJpegRect16(rdr::U16 *buf, int stride, const Rect& r,
140 rdr::OutStream *os);
141 void encodeJpegRect32(rdr::U32 *buf, int stride, const Rect& r,
142 rdr::OutStream *os);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000143
144 SMsgWriter* writer;
145 rdr::MemOutStream mos;
146 rdr::ZlibOutStream zos[4];
DRCcd2c5d42011-08-11 11:18:34 +0000147 JpegCompressor jc;
DRCffe09d62011-08-17 02:27:59 +0000148 TransImageGetter *ig;
149 PixelFormat serverpf, clientpf;
150
151 bool pack24;
152 int palMaxColors, palNumColors;
153 rdr::U32 monoBackground, monoForeground;
154 TIGHT_PALETTE palette;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000155
156 static const int defaultCompressLevel;
157 static const TIGHT_CONF conf[];
158
159 const TIGHT_CONF* pconf;
DRCb4a83232011-08-19 04:57:18 +0000160 int jpegQuality;
161 JPEG_SUBSAMP jpegSubsampling;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 };
163
164}
165
166#endif