Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved. |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 2 | * Copyright (C) 2011 D. R. Commander |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 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 Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 25 | #include <rfb/JpegCompressor.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | |
| 27 | // FIXME: Check if specifying extern "C" is really necessary. |
| 28 | #include <stdio.h> |
| 29 | extern "C" { |
Constantin Kaplinsky | e19ac6d | 2006-05-30 06:15:20 +0000 | [diff] [blame] | 30 | #include <jpeglib.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | namespace rfb { |
| 34 | |
Pierre Ossman | 456b2c2 | 2014-01-15 13:22:03 +0100 | [diff] [blame] | 35 | class TransImageGetter; |
| 36 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 37 | struct TIGHT_CONF { |
| 38 | unsigned int maxRectSize, maxRectWidth; |
| 39 | unsigned int monoMinRectSize; |
| 40 | int idxZlibLevel, monoZlibLevel, rawZlibLevel; |
| 41 | int idxMaxColorsDivisor; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 42 | int palMaxColorsWithJPEG; |
DRC | 773cf3c | 2009-03-12 19:26:44 +0000 | [diff] [blame] | 43 | int jpegQuality; |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 44 | int jpegSubsampling; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | // |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 48 | // C-style structures to store palette entries and compression paramentes. |
| 49 | // Such code probably should be converted into C++ classes. |
| 50 | // |
| 51 | |
| 52 | struct TIGHT_COLOR_LIST { |
| 53 | TIGHT_COLOR_LIST *next; |
| 54 | int idx; |
| 55 | rdr::U32 rgb; |
| 56 | }; |
| 57 | |
| 58 | struct TIGHT_PALETTE_ENTRY { |
| 59 | TIGHT_COLOR_LIST *listNode; |
| 60 | int numPixels; |
| 61 | }; |
| 62 | |
| 63 | struct TIGHT_PALETTE { |
| 64 | TIGHT_PALETTE_ENTRY entry[256]; |
| 65 | TIGHT_COLOR_LIST *hash[256]; |
| 66 | TIGHT_COLOR_LIST list[256]; |
| 67 | }; |
| 68 | |
| 69 | // |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | // Compression level stuff. The following array contains various |
| 71 | // encoder parameters for each of 10 compression levels (0..9). |
| 72 | // Last three parameters correspond to JPEG quality levels (0..9). |
| 73 | // |
| 74 | // NOTE: s_conf[9].maxRectSize should be >= s_conf[i].maxRectSize, |
| 75 | // where i in [0..8]. RequiredBuffSize() method depends on this. |
| 76 | // FIXME: Is this comment obsolete? |
| 77 | // |
| 78 | |
| 79 | |
| 80 | class TightEncoder : public Encoder { |
| 81 | public: |
Pierre Ossman | 316a324 | 2014-01-15 12:40:20 +0100 | [diff] [blame] | 82 | TightEncoder(SMsgWriter* writer); |
| 83 | virtual ~TightEncoder(); |
| 84 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 85 | virtual void setCompressLevel(int level); |
| 86 | virtual void setQualityLevel(int level); |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 87 | virtual void setFineQualityLevel(int quality, int subsampling); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 88 | virtual int getNumRects(const Rect &r); |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame^] | 89 | virtual void writeRect(const Rect& r, TransImageGetter* ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | |
| 91 | private: |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 92 | bool checkSolidTile(Rect& r, rdr::U32* colorPtr, bool needSameColor); |
| 93 | void extendSolidArea(const Rect& r, rdr::U32 colorValue, Rect& er); |
| 94 | void findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr); |
| 95 | void sendRectSimple(const Rect& r); |
| 96 | void writeSubrect(const Rect& r, bool forceSolid = false); |
| 97 | |
DRC | 3004cb6 | 2011-11-03 18:45:39 +0000 | [diff] [blame] | 98 | void compressData(const void *buf, unsigned int length, |
| 99 | rdr::ZlibOutStream *zos, int zlibLevel, |
| 100 | rdr::OutStream *os); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 101 | |
| 102 | int paletteInsert(rdr::U32 rgb, int numPixels, int bpp); |
| 103 | void paletteReset(void); |
| 104 | |
DRC | 56dee1a | 2012-02-13 03:55:10 +0000 | [diff] [blame] | 105 | void fastFillPalette8(const rdr::U8 *data, int stride, const Rect &r); |
| 106 | void fastFillPalette16(const rdr::U16 *data, int stride, const Rect &r); |
| 107 | void fastFillPalette32(const rdr::U32 *data, int stride, const Rect &r); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 108 | |
| 109 | void fillPalette8(rdr::U8 *data, int count); |
| 110 | void fillPalette16(rdr::U16 *data, int count); |
| 111 | void fillPalette32(rdr::U32 *data, int count); |
| 112 | |
| 113 | unsigned int packPixels8(rdr::U8 *buf, unsigned int count); |
| 114 | unsigned int packPixels16(rdr::U16 *buf, unsigned int count); |
| 115 | unsigned int packPixels32(rdr::U32 *buf, unsigned int count); |
| 116 | |
| 117 | void tightEncode8(const Rect& r, rdr::OutStream *os, bool forceSolid); |
| 118 | void tightEncode16(const Rect& r, rdr::OutStream *os, bool forceSolid); |
| 119 | void tightEncode32(const Rect& r, rdr::OutStream *os, bool forceSolid); |
| 120 | |
| 121 | bool checkSolidTile8(Rect& r, rdr::U32 *colorPtr, bool needSameColor); |
| 122 | bool checkSolidTile16(Rect& r, rdr::U32 *colorPtr, bool needSameColor); |
| 123 | bool checkSolidTile32(Rect& r, rdr::U32 *colorPtr, bool needSameColor); |
| 124 | |
DRC | 3004cb6 | 2011-11-03 18:45:39 +0000 | [diff] [blame] | 125 | void encodeSolidRect8(rdr::U8 *buf, rdr::OutStream *os); |
| 126 | void encodeSolidRect16(rdr::U16 *buf, rdr::OutStream *os); |
| 127 | void encodeSolidRect32(rdr::U32 *buf, rdr::OutStream *os); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 128 | |
DRC | 3004cb6 | 2011-11-03 18:45:39 +0000 | [diff] [blame] | 129 | void encodeFullColorRect8(rdr::U8 *buf, const Rect& r, rdr::OutStream *os); |
| 130 | void encodeFullColorRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os); |
| 131 | void encodeFullColorRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 132 | |
DRC | 3004cb6 | 2011-11-03 18:45:39 +0000 | [diff] [blame] | 133 | void encodeMonoRect8(rdr::U8 *buf, const Rect& r, rdr::OutStream *os); |
| 134 | void encodeMonoRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os); |
| 135 | void encodeMonoRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 136 | |
DRC | 3004cb6 | 2011-11-03 18:45:39 +0000 | [diff] [blame] | 137 | void encodeIndexedRect16(rdr::U16 *buf, const Rect& r, rdr::OutStream *os); |
| 138 | void encodeIndexedRect32(rdr::U32 *buf, const Rect& r, rdr::OutStream *os); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 139 | |
DRC | 56dee1a | 2012-02-13 03:55:10 +0000 | [diff] [blame] | 140 | void encodeJpegRect16(rdr::U16 *buf, int stride, const Rect& r, |
| 141 | rdr::OutStream *os); |
| 142 | void encodeJpegRect32(rdr::U32 *buf, int stride, const Rect& r, |
| 143 | rdr::OutStream *os); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 144 | |
| 145 | SMsgWriter* writer; |
| 146 | rdr::MemOutStream mos; |
| 147 | rdr::ZlibOutStream zos[4]; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 148 | JpegCompressor jc; |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame] | 149 | TransImageGetter *ig; |
| 150 | PixelFormat serverpf, clientpf; |
| 151 | |
| 152 | bool pack24; |
| 153 | int palMaxColors, palNumColors; |
| 154 | rdr::U32 monoBackground, monoForeground; |
| 155 | TIGHT_PALETTE palette; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 156 | |
| 157 | static const int defaultCompressLevel; |
| 158 | static const TIGHT_CONF conf[]; |
| 159 | |
| 160 | const TIGHT_CONF* pconf; |
DRC | b4a8323 | 2011-08-19 04:57:18 +0000 | [diff] [blame] | 161 | int jpegQuality; |
Pierre Ossman | b948a91 | 2014-01-15 13:23:43 +0100 | [diff] [blame] | 162 | int jpegSubsampling; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | } |
| 166 | |
| 167 | #endif |