blob: e70706b57ba22230a46a941902ceb90cbc3c54e4 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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 Ossman456b2c22014-01-15 13:22:03 +010020#include <rfb/TransImageGetter.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000021#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
27using namespace rfb;
28
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000029IntParameter 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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049ZRLEEncoder::ZRLEEncoder(SMsgWriter* writer_)
Pierre Ossman4bca9112014-03-13 15:08:36 +010050 : writer(writer_), zos(0,0,zlibLevel), mos(129*1024)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000051{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052}
53
54ZRLEEncoder::~ZRLEEncoder()
55{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056}
57
DRCffe09d62011-08-17 02:27:59 +000058bool ZRLEEncoder::writeRect(const Rect& r, TransImageGetter* ig, Rect* actual)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000059{
60 rdr::U8* imageBuf = writer->getImageBuf(64 * 64 * 4 + 4);
Pierre Ossman4bca9112014-03-13 15:08:36 +010061 mos.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000062 bool wroteAll = true;
63 *actual = r;
64
65 switch (writer->bpp()) {
66 case 8:
Pierre Ossman4bca9112014-03-13 15:08:36 +010067 wroteAll = zrleEncode8(r, &mos, &zos, imageBuf, actual, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068 break;
69 case 16:
Pierre Ossman4bca9112014-03-13 15:08:36 +010070 wroteAll = zrleEncode16(r, &mos, &zos, imageBuf, actual, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071 break;
72 case 32:
73 {
74 const PixelFormat& pf = writer->getConnParams()->pf();
75
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000076 Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
77 bool fitsInLS3Bytes = maxPixel < (1<<24);
78 bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000079
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000080 if ((fitsInLS3Bytes && pf.isLittleEndian()) ||
81 (fitsInMS3Bytes && pf.isBigEndian()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000082 {
Pierre Ossman4bca9112014-03-13 15:08:36 +010083 wroteAll = zrleEncode24A(r, &mos, &zos, imageBuf, actual, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084 }
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000085 else if ((fitsInLS3Bytes && pf.isBigEndian()) ||
86 (fitsInMS3Bytes && pf.isLittleEndian()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 {
Pierre Ossman4bca9112014-03-13 15:08:36 +010088 wroteAll = zrleEncode24B(r, &mos, &zos, imageBuf, actual, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000089 }
90 else
91 {
Pierre Ossman4bca9112014-03-13 15:08:36 +010092 wroteAll = zrleEncode32(r, &mos, &zos, imageBuf, actual, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093 }
94 break;
95 }
96 }
97
98 writer->startRect(*actual, encodingZRLE);
99 rdr::OutStream* os = writer->getOutStream();
Pierre Ossman4bca9112014-03-13 15:08:36 +0100100 os->writeU32(mos.length());
101 os->writeBytes(mos.data(), mos.length());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 writer->endRect();
103 return wroteAll;
104}