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 
19 //
20 // ZRLE decoding function.
21 //
22 // This file is #included after having set the following macro:
23 // BPP                - 8, 16 or 32
24 
25 namespace rfb {
26 
27 // CONCAT2E concatenates its arguments, expanding them if they are macros
28 
29 #ifndef CONCAT2E
30 #define CONCAT2(a,b) a##b
31 #define CONCAT2E(a,b) CONCAT2(a,b)
32 #endif
33 
34 #ifdef CPIXEL
35 #define PIXEL_T rdr::CONCAT2E(U,BPP)
36 #define READ_PIXEL(is) CONCAT2E(readOpaque,CPIXEL)(is)
37 #define ZRLE_DECODE CONCAT2E(zrleDecode,CPIXEL)
38 #else
39 #define PIXEL_T rdr::CONCAT2E(U,BPP)
40 #define READ_PIXEL(is) is->CONCAT2E(readOpaque,BPP)()
41 #define ZRLE_DECODE CONCAT2E(zrleDecode,BPP)
42 #endif
43 
ZRLE_DECODE(const Rect & r,rdr::InStream * is,rdr::ZlibInStream * zis,const PixelFormat & pf,ModifiablePixelBuffer * pb)44 void ZRLE_DECODE (const Rect& r, rdr::InStream* is,
45                   rdr::ZlibInStream* zis,
46                   const PixelFormat& pf, ModifiablePixelBuffer* pb)
47 {
48   int length = is->readU32();
49   zis->setUnderlying(is, length);
50   Rect t;
51   PIXEL_T buf[64 * 64];
52 
53   for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 64) {
54 
55     t.br.y = __rfbmin(r.br.y, t.tl.y + 64);
56 
57     for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 64) {
58 
59       t.br.x = __rfbmin(r.br.x, t.tl.x + 64);
60 
61       zlibHasData(zis, 1);
62       int mode = zis->readU8();
63       bool rle = mode & 128;
64       int palSize = mode & 127;
65       PIXEL_T palette[128];
66 
67 #ifdef CPIXEL
68       zlibHasData(zis, 3 * palSize);
69 #else
70       zlibHasData(zis, BPP/8 * palSize);
71 #endif
72       for (int i = 0; i < palSize; i++) {
73         palette[i] = READ_PIXEL(zis);
74       }
75 
76       if (palSize == 1) {
77         PIXEL_T pix = palette[0];
78         pb->fillRect(pf, t, &pix);
79         continue;
80       }
81 
82       if (!rle) {
83         if (palSize == 0) {
84 
85           // raw
86 
87 #ifdef CPIXEL
88           zlibHasData(zis, 3 * t.area());
89           for (PIXEL_T* ptr = buf; ptr < buf+t.area(); ptr++) {
90             *ptr = READ_PIXEL(zis);
91           }
92 #else
93           zlibHasData(zis, BPP/8 * t.area());
94           zis->readBytes(buf, t.area() * (BPP / 8));
95 #endif
96 
97         } else {
98 
99           // packed pixels
100           int bppp = ((palSize > 16) ? 8 :
101                       ((palSize > 4) ? 4 : ((palSize > 2) ? 2 : 1)));
102 
103           PIXEL_T* ptr = buf;
104 
105           for (int i = 0; i < t.height(); i++) {
106             PIXEL_T* eol = ptr + t.width();
107             rdr::U8 byte = 0;
108             rdr::U8 nbits = 0;
109 
110             while (ptr < eol) {
111               if (nbits == 0) {
112                 zlibHasData(zis, 1);
113                 byte = zis->readU8();
114                 nbits = 8;
115               }
116               nbits -= bppp;
117               rdr::U8 index = (byte >> nbits) & ((1 << bppp) - 1) & 127;
118               *ptr++ = palette[index];
119             }
120           }
121         }
122 
123       } else {
124 
125         if (palSize == 0) {
126 
127           // plain RLE
128 
129           PIXEL_T* ptr = buf;
130           PIXEL_T* end = ptr + t.area();
131           while (ptr < end) {
132 #ifdef CPIXEL
133             zlibHasData(zis, 3);
134 #else
135             zlibHasData(zis, BPP/8);
136 #endif
137             PIXEL_T pix = READ_PIXEL(zis);
138             int len = 1;
139             int b;
140             do {
141               zlibHasData(zis, 1);
142               b = zis->readU8();
143               len += b;
144             } while (b == 255);
145 
146             if (end - ptr < len) {
147               throw Exception ("ZRLE decode error");
148             }
149 
150             while (len-- > 0) *ptr++ = pix;
151 
152           }
153         } else {
154 
155           // palette RLE
156 
157           PIXEL_T* ptr = buf;
158           PIXEL_T* end = ptr + t.area();
159           while (ptr < end) {
160             zlibHasData(zis, 1);
161             int index = zis->readU8();
162             int len = 1;
163             if (index & 128) {
164               int b;
165               do {
166                 zlibHasData(zis, 1);
167                 b = zis->readU8();
168                 len += b;
169               } while (b == 255);
170 
171               if (end - ptr < len) {
172                 throw Exception ("ZRLE decode error");
173               }
174             }
175 
176             index &= 127;
177 
178             PIXEL_T pix = palette[index];
179 
180             while (len-- > 0) *ptr++ = pix;
181           }
182         }
183       }
184 
185       pb->imageRect(pf, t, buf);
186     }
187   }
188 
189   zis->flushUnderlying();
190   zis->setUnderlying(NULL, 0);
191 }
192 
193 #undef ZRLE_DECODE
194 #undef READ_PIXEL
195 #undef PIXEL_T
196 }
197