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> |
| 24 | #include <rfb/ZRLEEncoder.h> |
| 25 | #include <rfb/Configuration.h> |
| 26 | |
| 27 | using namespace rfb; |
| 28 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 29 | IntParameter zlibLevel("ZlibLevel","Zlib compression level",-1); |
| 30 | |
| 31 | #define EXTRA_ARGS ImageGetter* ig |
| 32 | #define GET_IMAGE_INTO_BUF(r,buf) ig->getImage(buf, r); |
| 33 | #define BPP 8 |
| 34 | #include <rfb/zrleEncode.h> |
| 35 | #undef BPP |
| 36 | #define BPP 16 |
| 37 | #include <rfb/zrleEncode.h> |
| 38 | #undef BPP |
| 39 | #define BPP 32 |
| 40 | #include <rfb/zrleEncode.h> |
| 41 | #define CPIXEL 24A |
| 42 | #include <rfb/zrleEncode.h> |
| 43 | #undef CPIXEL |
| 44 | #define CPIXEL 24B |
| 45 | #include <rfb/zrleEncode.h> |
| 46 | #undef CPIXEL |
| 47 | #undef BPP |
| 48 | |
Pierre Ossman | 4aba19e | 2014-01-29 16:42:48 +0100 | [diff] [blame] | 49 | ZRLEEncoder::ZRLEEncoder(SMsgWriter* writer) |
| 50 | : Encoder(writer), zos(0,0,zlibLevel), mos(129*1024) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 51 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ZRLEEncoder::~ZRLEEncoder() |
| 55 | { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 58 | void ZRLEEncoder::writeRect(const Rect& r, TransImageGetter* ig) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 59 | { |
| 60 | rdr::U8* imageBuf = writer->getImageBuf(64 * 64 * 4 + 4); |
Pierre Ossman | 4bca911 | 2014-03-13 15:08:36 +0100 | [diff] [blame] | 61 | mos.clear(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 62 | |
| 63 | switch (writer->bpp()) { |
| 64 | case 8: |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 65 | zrleEncode8(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 66 | break; |
| 67 | case 16: |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 68 | zrleEncode16(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | break; |
| 70 | case 32: |
| 71 | { |
| 72 | const PixelFormat& pf = writer->getConnParams()->pf(); |
| 73 | |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 74 | Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1); |
| 75 | bool fitsInLS3Bytes = maxPixel < (1<<24); |
| 76 | bool fitsInMS3Bytes = (maxPixel & 0xff) == 0; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 77 | |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 78 | if ((fitsInLS3Bytes && pf.isLittleEndian()) || |
| 79 | (fitsInMS3Bytes && pf.isBigEndian())) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 80 | { |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 81 | zrleEncode24A(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 82 | } |
Pierre Ossman | 67b2b2f | 2009-03-06 10:12:55 +0000 | [diff] [blame] | 83 | else if ((fitsInLS3Bytes && pf.isBigEndian()) || |
| 84 | (fitsInMS3Bytes && pf.isLittleEndian())) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 85 | { |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 86 | zrleEncode24B(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 90 | zrleEncode32(r, &mos, &zos, imageBuf, ig); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 91 | } |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
Pierre Ossman | 717c07b | 2014-01-21 14:45:10 +0100 | [diff] [blame] | 96 | writer->startRect(r, encodingZRLE); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 97 | rdr::OutStream* os = writer->getOutStream(); |
Pierre Ossman | 4bca911 | 2014-03-13 15:08:36 +0100 | [diff] [blame] | 98 | os->writeU32(mos.length()); |
| 99 | os->writeBytes(mos.data(), mos.length()); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 100 | writer->endRect(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | } |