blob: 840af6be9ab73af09edf248de1a431020a82516b [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
Pierre Ossman7b5c0692014-03-17 14:35:51 +010031static inline void writeOpaque24A(rdr::OutStream* os, rdr::U32 u)
32{
33 os->check(3);
34 os->writeU8(((rdr::U8*)&u)[0]);
35 os->writeU8(((rdr::U8*)&u)[1]);
36 os->writeU8(((rdr::U8*)&u)[2]);
37}
38static inline void writeOpaque24B(rdr::OutStream* os, rdr::U32 u)
39{
40 os->check(3);
41 os->writeU8(((rdr::U8*)&u)[1]);
42 os->writeU8(((rdr::U8*)&u)[2]);
43 os->writeU8(((rdr::U8*)&u)[3]);
44}
45
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000046#define EXTRA_ARGS ImageGetter* ig
47#define GET_IMAGE_INTO_BUF(r,buf) ig->getImage(buf, r);
48#define BPP 8
49#include <rfb/zrleEncode.h>
50#undef BPP
51#define BPP 16
52#include <rfb/zrleEncode.h>
53#undef BPP
54#define BPP 32
55#include <rfb/zrleEncode.h>
56#define CPIXEL 24A
57#include <rfb/zrleEncode.h>
58#undef CPIXEL
59#define CPIXEL 24B
60#include <rfb/zrleEncode.h>
61#undef CPIXEL
62#undef BPP
63
Pierre Ossman4aba19e2014-01-29 16:42:48 +010064ZRLEEncoder::ZRLEEncoder(SMsgWriter* writer)
65 : Encoder(writer), zos(0,0,zlibLevel), mos(129*1024)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000066{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067}
68
69ZRLEEncoder::~ZRLEEncoder()
70{
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071}
72
Pierre Ossman717c07b2014-01-21 14:45:10 +010073void ZRLEEncoder::writeRect(const Rect& r, TransImageGetter* ig)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000074{
75 rdr::U8* imageBuf = writer->getImageBuf(64 * 64 * 4 + 4);
Pierre Ossman4bca9112014-03-13 15:08:36 +010076 mos.clear();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000077
78 switch (writer->bpp()) {
79 case 8:
Pierre Ossman717c07b2014-01-21 14:45:10 +010080 zrleEncode8(r, &mos, &zos, imageBuf, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000081 break;
82 case 16:
Pierre Ossman717c07b2014-01-21 14:45:10 +010083 zrleEncode16(r, &mos, &zos, imageBuf, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084 break;
85 case 32:
86 {
87 const PixelFormat& pf = writer->getConnParams()->pf();
88
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000089 Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
90 bool fitsInLS3Bytes = maxPixel < (1<<24);
91 bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000092
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000093 if ((fitsInLS3Bytes && pf.isLittleEndian()) ||
94 (fitsInMS3Bytes && pf.isBigEndian()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 {
Pierre Ossman717c07b2014-01-21 14:45:10 +010096 zrleEncode24A(r, &mos, &zos, imageBuf, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000097 }
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000098 else if ((fitsInLS3Bytes && pf.isBigEndian()) ||
99 (fitsInMS3Bytes && pf.isLittleEndian()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000100 {
Pierre Ossman717c07b2014-01-21 14:45:10 +0100101 zrleEncode24B(r, &mos, &zos, imageBuf, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 }
103 else
104 {
Pierre Ossman717c07b2014-01-21 14:45:10 +0100105 zrleEncode32(r, &mos, &zos, imageBuf, ig);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000106 }
107 break;
108 }
109 }
110
Pierre Ossman717c07b2014-01-21 14:45:10 +0100111 writer->startRect(r, encodingZRLE);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000112 rdr::OutStream* os = writer->getOutStream();
Pierre Ossman4bca9112014-03-13 15:08:36 +0100113 os->writeU32(mos.length());
114 os->writeBytes(mos.data(), mos.length());
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000115 writer->endRect();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000116}