blob: 402cd031b654f9c0d0772a9a0b4daacc41359e19 [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//
19// Hextile decoding function.
20//
Pierre Ossmanbcc295e2014-02-12 13:16:43 +010021// This file is #included after having set the following macro:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000022// BPP - 8, 16 or 32
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000023
24#include <rdr/InStream.h>
Josef Gajdusek2a4734c2016-11-04 12:24:08 +010025#include <rfb/Exception.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000026#include <rfb/hextileConstants.h>
27
28namespace rfb {
29
30// CONCAT2E concatenates its arguments, expanding them if they are macros
31
32#ifndef CONCAT2E
33#define CONCAT2(a,b) a##b
34#define CONCAT2E(a,b) CONCAT2(a,b)
35#endif
36
37#define PIXEL_T rdr::CONCAT2E(U,BPP)
38#define READ_PIXEL CONCAT2E(readOpaque,BPP)
39#define HEXTILE_DECODE CONCAT2E(hextileDecode,BPP)
40
Pierre Ossman80b42092015-11-10 17:17:34 +010041static void HEXTILE_DECODE (const Rect& r, rdr::InStream* is,
42 const PixelFormat& pf,
43 ModifiablePixelBuffer* pb)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044{
45 Rect t;
46 PIXEL_T bg = 0;
47 PIXEL_T fg = 0;
Pierre Ossman1349e422016-10-05 11:00:37 +020048 PIXEL_T buf[16 * 16];
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000049
50 for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) {
51
52 t.br.y = __rfbmin(r.br.y, t.tl.y + 16);
53
54 for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) {
55
56 t.br.x = __rfbmin(r.br.x, t.tl.x + 16);
57
58 int tileType = is->readU8();
59
60 if (tileType & hextileRaw) {
Pierre Ossman80b42092015-11-10 17:17:34 +010061 is->readBytes(buf, t.area() * (BPP/8));
62 pb->imageRect(pf, t, buf);
63 continue;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000064 }
65
66 if (tileType & hextileBgSpecified)
Pierre Ossman80b42092015-11-10 17:17:34 +010067 bg = is->READ_PIXEL();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000068
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000069 int len = t.area();
Pierre Ossman80b42092015-11-10 17:17:34 +010070 PIXEL_T* ptr = buf;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000071 while (len-- > 0) *ptr++ = bg;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000072
73 if (tileType & hextileFgSpecified)
Pierre Ossman80b42092015-11-10 17:17:34 +010074 fg = is->READ_PIXEL();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075
76 if (tileType & hextileAnySubrects) {
77 int nSubrects = is->readU8();
78
79 for (int i = 0; i < nSubrects; i++) {
80
81 if (tileType & hextileSubrectsColoured)
82 fg = is->READ_PIXEL();
83
84 int xy = is->readU8();
85 int wh = is->readU8();
86
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000087 int x = ((xy >> 4) & 15);
88 int y = (xy & 15);
89 int w = ((wh >> 4) & 15) + 1;
90 int h = (wh & 15) + 1;
Josef Gajdusek2a4734c2016-11-04 12:24:08 +010091 if (x + w > 16 || y + h > 16) {
92 throw rfb::Exception("HEXTILE_DECODE: Hextile out of bounds");
93 }
Pierre Ossman80b42092015-11-10 17:17:34 +010094 PIXEL_T* ptr = buf + y * t.width() + x;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095 int rowAdd = t.width() - w;
96 while (h-- > 0) {
97 int len = w;
98 while (len-- > 0) *ptr++ = fg;
99 ptr += rowAdd;
100 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000101 }
102 }
Pierre Ossman0c9bd4b2014-07-09 16:44:11 +0200103 pb->imageRect(pf, t, buf);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 }
105 }
106}
107
108#undef PIXEL_T
109#undef READ_PIXEL
110#undef HEXTILE_DECODE
111}