blob: ef0dd9f55ddf33c5cf16446ce86a8f3fc39521a8 [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>
20#include <rfb/ImageGetter.h>
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
27using namespace rfb;
28
29rdr::MemOutStream* ZRLEEncoder::sharedMos = 0;
30int ZRLEEncoder::maxLen = 4097 * 1024; // enough for width 16384 32-bit pixels
31
32IntParameter zlibLevel("ZlibLevel","Zlib compression level",-1);
33
34#define EXTRA_ARGS ImageGetter* ig
35#define GET_IMAGE_INTO_BUF(r,buf) ig->getImage(buf, r);
36#define BPP 8
37#include <rfb/zrleEncode.h>
38#undef BPP
39#define BPP 16
40#include <rfb/zrleEncode.h>
41#undef BPP
42#define BPP 32
43#include <rfb/zrleEncode.h>
44#define CPIXEL 24A
45#include <rfb/zrleEncode.h>
46#undef CPIXEL
47#define CPIXEL 24B
48#include <rfb/zrleEncode.h>
49#undef CPIXEL
50#undef BPP
51
52Encoder* ZRLEEncoder::create(SMsgWriter* writer)
53{
54 return new ZRLEEncoder(writer);
55}
56
57ZRLEEncoder::ZRLEEncoder(SMsgWriter* writer_)
58 : writer(writer_), zos(0,0,zlibLevel)
59{
60 if (sharedMos)
61 mos = sharedMos;
62 else
63 mos = new rdr::MemOutStream(129*1024);
64}
65
66ZRLEEncoder::~ZRLEEncoder()
67{
68 if (!sharedMos)
69 delete mos;
70}
71
72bool ZRLEEncoder::writeRect(const Rect& r, ImageGetter* ig, Rect* actual)
73{
74 rdr::U8* imageBuf = writer->getImageBuf(64 * 64 * 4 + 4);
75 mos->clear();
76 bool wroteAll = true;
77 *actual = r;
78
79 switch (writer->bpp()) {
80 case 8:
81 wroteAll = zrleEncode8(r, mos, &zos, imageBuf, maxLen, actual, ig);
82 break;
83 case 16:
84 wroteAll = zrleEncode16(r, mos, &zos, imageBuf, maxLen, actual, ig);
85 break;
86 case 32:
87 {
88 const PixelFormat& pf = writer->getConnParams()->pf();
89
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000090 Pixel maxPixel = pf.pixelFromRGB((rdr::U16)-1, (rdr::U16)-1, (rdr::U16)-1);
91 bool fitsInLS3Bytes = maxPixel < (1<<24);
92 bool fitsInMS3Bytes = (maxPixel & 0xff) == 0;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000093
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000094 if ((fitsInLS3Bytes && pf.isLittleEndian()) ||
95 (fitsInMS3Bytes && pf.isBigEndian()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 {
97 wroteAll = zrleEncode24A(r, mos, &zos, imageBuf, maxLen, actual, ig);
98 }
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000099 else if ((fitsInLS3Bytes && pf.isBigEndian()) ||
100 (fitsInMS3Bytes && pf.isLittleEndian()))
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 {
102 wroteAll = zrleEncode24B(r, mos, &zos, imageBuf, maxLen, actual, ig);
103 }
104 else
105 {
106 wroteAll = zrleEncode32(r, mos, &zos, imageBuf, maxLen, actual, ig);
107 }
108 break;
109 }
110 }
111
112 writer->startRect(*actual, encodingZRLE);
113 rdr::OutStream* os = writer->getOutStream();
114 os->writeU32(mos->length());
115 os->writeBytes(mos->data(), mos->length());
116 writer->endRect();
117 return wroteAll;
118}