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> |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 24 | #include <rfb/JpegCompressor.h> |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame^] | 25 | #include <rfb/TransImageGetter.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/Encoder.h> |
| 27 | |
| 28 | // FIXME: Check if specifying extern "C" is really necessary. |
| 29 | #include <stdio.h> |
| 30 | extern "C" { |
Constantin Kaplinsky | e19ac6d | 2006-05-30 06:15:20 +0000 | [diff] [blame] | 31 | #include <jpeglib.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | namespace rfb { |
| 35 | |
| 36 | struct TIGHT_CONF { |
| 37 | unsigned int maxRectSize, maxRectWidth; |
| 38 | unsigned int monoMinRectSize; |
| 39 | int idxZlibLevel, monoZlibLevel, rawZlibLevel; |
| 40 | int idxMaxColorsDivisor; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 41 | int palMaxColorsWithJPEG; |
DRC | 773cf3c | 2009-03-12 19:26:44 +0000 | [diff] [blame] | 42 | int jpegQuality; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 43 | JPEG_SUBSAMP jpegSubSample; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | // |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame^] | 47 | // 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | // 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); |
| 84 | virtual int getNumRects(const Rect &r); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame^] | 85 | virtual bool writeRect(const Rect& r, TransImageGetter* ig, Rect* actual); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 86 | virtual ~TightEncoder(); |
| 87 | |
| 88 | private: |
| 89 | TightEncoder(SMsgWriter* writer); |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame^] | 90 | bool checkSolidTile(Rect& r, rdr::U32* colorPtr, bool needSameColor); |
| 91 | void extendSolidArea(const Rect& r, rdr::U32 colorValue, Rect& er); |
| 92 | void findBestSolidArea(Rect& r, rdr::U32 colorValue, Rect& bestr); |
| 93 | void sendRectSimple(const Rect& r); |
| 94 | void writeSubrect(const Rect& r, bool forceSolid = false); |
| 95 | |
| 96 | void compressData(rdr::OutStream *os, rdr::ZlibOutStream *zos, |
| 97 | const void *buf, unsigned int length, int zlibLevel); |
| 98 | |
| 99 | int paletteInsert(rdr::U32 rgb, int numPixels, int bpp); |
| 100 | void paletteReset(void); |
| 101 | |
| 102 | void fastFillPalette8(const Rect &r, rdr::U8 *data, int stride); |
| 103 | void fastFillPalette16(const Rect &r, rdr::U16 *data, int stride); |
| 104 | void fastFillPalette32(const Rect &r, rdr::U32 *data, int stride); |
| 105 | |
| 106 | void fillPalette8(rdr::U8 *data, int count); |
| 107 | void fillPalette16(rdr::U16 *data, int count); |
| 108 | void fillPalette32(rdr::U32 *data, int count); |
| 109 | |
| 110 | unsigned int packPixels8(rdr::U8 *buf, unsigned int count); |
| 111 | unsigned int packPixels16(rdr::U16 *buf, unsigned int count); |
| 112 | unsigned int packPixels32(rdr::U32 *buf, unsigned int count); |
| 113 | |
| 114 | void tightEncode8(const Rect& r, rdr::OutStream *os, bool forceSolid); |
| 115 | void tightEncode16(const Rect& r, rdr::OutStream *os, bool forceSolid); |
| 116 | void tightEncode32(const Rect& r, rdr::OutStream *os, bool forceSolid); |
| 117 | |
| 118 | bool checkSolidTile8(Rect& r, rdr::U32 *colorPtr, bool needSameColor); |
| 119 | bool checkSolidTile16(Rect& r, rdr::U32 *colorPtr, bool needSameColor); |
| 120 | bool checkSolidTile32(Rect& r, rdr::U32 *colorPtr, bool needSameColor); |
| 121 | |
| 122 | void encodeSolidRect8(rdr::OutStream *os, rdr::U8 *buf); |
| 123 | void encodeSolidRect16(rdr::OutStream *os, rdr::U16 *buf); |
| 124 | void encodeSolidRect32(rdr::OutStream *os, rdr::U32 *buf); |
| 125 | |
| 126 | void encodeFullColorRect8(rdr::OutStream *os, rdr::U8 *buf, const Rect& r); |
| 127 | void encodeFullColorRect16(rdr::OutStream *os, rdr::U16 *buf, const Rect& r); |
| 128 | void encodeFullColorRect32(rdr::OutStream *os, rdr::U32 *buf, const Rect& r); |
| 129 | |
| 130 | void encodeMonoRect8(rdr::OutStream *os, rdr::U8 *buf, const Rect& r); |
| 131 | void encodeMonoRect16(rdr::OutStream *os, rdr::U16 *buf, const Rect& r); |
| 132 | void encodeMonoRect32(rdr::OutStream *os, rdr::U32 *buf, const Rect& r); |
| 133 | |
| 134 | void encodeIndexedRect16(rdr::OutStream *os, rdr::U16 *buf, const Rect& r); |
| 135 | void encodeIndexedRect32(rdr::OutStream *os, rdr::U32 *buf, const Rect& r); |
| 136 | |
| 137 | void encodeJpegRect16(rdr::OutStream *os, rdr::U16 *buf, int, const Rect& r); |
| 138 | void encodeJpegRect32(rdr::OutStream *os, rdr::U32 *buf, int, const Rect& r); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 139 | |
| 140 | SMsgWriter* writer; |
| 141 | rdr::MemOutStream mos; |
| 142 | rdr::ZlibOutStream zos[4]; |
DRC | cd2c5d4 | 2011-08-11 11:18:34 +0000 | [diff] [blame] | 143 | JpegCompressor jc; |
DRC | ffe09d6 | 2011-08-17 02:27:59 +0000 | [diff] [blame^] | 144 | TransImageGetter *ig; |
| 145 | PixelFormat serverpf, clientpf; |
| 146 | |
| 147 | bool pack24; |
| 148 | int palMaxColors, palNumColors; |
| 149 | rdr::U32 monoBackground, monoForeground; |
| 150 | TIGHT_PALETTE palette; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 151 | |
| 152 | static const int defaultCompressLevel; |
| 153 | static const TIGHT_CONF conf[]; |
| 154 | |
| 155 | const TIGHT_CONF* pconf; |
| 156 | const TIGHT_CONF* pjconf; |
| 157 | }; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | #endif |