Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | #include <rdr/OutStream.h> |
| 19 | #include <rfb/Exception.h> |
Pierre Ossman | 456b2c2 | 2014-01-15 13:22:03 +0100 | [diff] [blame] | 20 | #include <rfb/TransImageGetter.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 21 | #include <rfb/encodings.h> |
| 22 | #include <rfb/ConnParams.h> |
| 23 | #include <rfb/SMsgWriter.h> |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 24 | #include <rfb/SConnection.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 25 | #include <rfb/ZRLEEncoder.h> |
| 26 | #include <rfb/Configuration.h> |
| 27 | |
| 28 | using namespace rfb; |
| 29 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 30 | IntParameter zlibLevel("ZlibLevel","Zlib compression level",-1); |
| 31 | |
Pierre Ossman | 7b5c069 | 2014-03-17 14:35:51 +0100 | [diff] [blame] | 32 | static inline void writeOpaque24A(rdr::OutStream* os, rdr::U32 u) |
| 33 | { |
| 34 | os->check(3); |
| 35 | os->writeU8(((rdr::U8*)&u)[0]); |
| 36 | os->writeU8(((rdr::U8*)&u)[1]); |
| 37 | os->writeU8(((rdr::U8*)&u)[2]); |
| 38 | } |
| 39 | static inline void writeOpaque24B(rdr::OutStream* os, rdr::U32 u) |
| 40 | { |
| 41 | os->check(3); |
| 42 | os->writeU8(((rdr::U8*)&u)[1]); |
| 43 | os->writeU8(((rdr::U8*)&u)[2]); |
| 44 | os->writeU8(((rdr::U8*)&u)[3]); |
| 45 | } |
| 46 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 47 | #define BPP 8 |
| 48 | #include <rfb/zrleEncode.h> |
| 49 | #undef BPP |
| 50 | #define BPP 16 |
| 51 | #include <rfb/zrleEncode.h> |
| 52 | #undef BPP |
| 53 | #define BPP 32 |
| 54 | #include <rfb/zrleEncode.h> |
| 55 | #define CPIXEL 24A |
| 56 | #include <rfb/zrleEncode.h> |
| 57 | #undef CPIXEL |
| 58 | #define CPIXEL 24B |
| 59 | #include <rfb/zrleEncode.h> |
| 60 | #undef CPIXEL |
| 61 | #undef BPP |
| 62 | |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 63 | ZRLEEncoder::ZRLEEncoder(SConnection* conn) |
| 64 | : Encoder(conn), zos(0,0,zlibLevel), mos(129*1024) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | ZRLEEncoder::~ZRLEEncoder() |
| 69 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 72 | void ZRLEEncoder::writeRect(const Rect& r, TransImageGetter* ig) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 73 | { |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 74 | rdr::U8* imageBuf = conn->writer()->getImageBuf(64 * 64 * 4 + 4); |
Pierre Ossman | 4bca911 | 2014-03-13 15:08:36 +0100 | [diff] [blame] | 75 | mos.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 76 | |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 77 | switch (conn->cp.pf().bpp) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 78 | case 8: |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 79 | zrleEncode8(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | break; |
| 81 | case 16: |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 82 | zrleEncode16(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 83 | break; |
| 84 | case 32: |
| 85 | { |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 86 | const PixelFormat& pf = conn->cp.pf(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 88 | Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1); |
| 89 | bool fitsInLS3Bytes = maxPixel < (1<<24); |
| 90 | bool fitsInMS3Bytes = (maxPixel & 0xff) == 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 91 | |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 92 | if ((fitsInLS3Bytes && pf.isLittleEndian()) || |
| 93 | (fitsInMS3Bytes && pf.isBigEndian())) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | { |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 95 | zrleEncode24A(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 96 | } |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 97 | else if ((fitsInLS3Bytes && pf.isBigEndian()) || |
| 98 | (fitsInMS3Bytes && pf.isLittleEndian())) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 99 | { |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 100 | zrleEncode24B(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 104 | zrleEncode32(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 105 | } |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 110 | conn->writer()->startRect(r, encodingZRLE); |
| 111 | rdr::OutStream* os = conn->getOutStream(); |
Pierre Ossman | 4bca911 | 2014-03-13 15:08:36 +0100 | [diff] [blame] | 112 | os->writeU32(mos.length()); |
| 113 | os->writeBytes(mos.data(), mos.length()); |
Pierre Ossman | 668468b | 2014-01-31 12:37:32 +0100 | [diff] [blame] | 114 | conn->writer()->endRect(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 115 | } |