Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 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 | // Hextile decoding function. |
| 20 | // |
Pierre Ossman | bcc295e | 2014-02-12 13:16:43 +0100 | [diff] [blame] | 21 | // This file is #included after having set the following macro: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 22 | // BPP - 8, 16 or 32 |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 23 | |
| 24 | #include <rdr/InStream.h> |
Josef Gajdusek | 2a4734c | 2016-11-04 12:24:08 +0100 | [diff] [blame] | 25 | #include <rfb/Exception.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <rfb/hextileConstants.h> |
| 27 | |
| 28 | namespace 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 Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 41 | static void HEXTILE_DECODE (const Rect& r, rdr::InStream* is, |
| 42 | const PixelFormat& pf, |
| 43 | ModifiablePixelBuffer* pb) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | { |
| 45 | Rect t; |
| 46 | PIXEL_T bg = 0; |
| 47 | PIXEL_T fg = 0; |
Pierre Ossman | 1349e42 | 2016-10-05 11:00:37 +0200 | [diff] [blame] | 48 | PIXEL_T buf[16 * 16]; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 49 | |
| 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 Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 61 | is->readBytes(buf, t.area() * (BPP/8)); |
| 62 | pb->imageRect(pf, t, buf); |
| 63 | continue; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (tileType & hextileBgSpecified) |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 67 | bg = is->READ_PIXEL(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 68 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 69 | int len = t.area(); |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 70 | PIXEL_T* ptr = buf; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 71 | while (len-- > 0) *ptr++ = bg; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | |
| 73 | if (tileType & hextileFgSpecified) |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 74 | fg = is->READ_PIXEL(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 87 | int x = ((xy >> 4) & 15); |
| 88 | int y = (xy & 15); |
| 89 | int w = ((wh >> 4) & 15) + 1; |
| 90 | int h = (wh & 15) + 1; |
Josef Gajdusek | 2a4734c | 2016-11-04 12:24:08 +0100 | [diff] [blame] | 91 | if (x + w > 16 || y + h > 16) { |
| 92 | throw rfb::Exception("HEXTILE_DECODE: Hextile out of bounds"); |
| 93 | } |
Pierre Ossman | 80b4209 | 2015-11-10 17:17:34 +0100 | [diff] [blame] | 94 | PIXEL_T* ptr = buf + y * t.width() + x; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 95 | int rowAdd = t.width() - w; |
| 96 | while (h-- > 0) { |
| 97 | int len = w; |
| 98 | while (len-- > 0) *ptr++ = fg; |
| 99 | ptr += rowAdd; |
| 100 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
Pierre Ossman | 0c9bd4b | 2014-07-09 16:44:11 +0200 | [diff] [blame] | 103 | pb->imageRect(pf, t, buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | #undef PIXEL_T |
| 109 | #undef READ_PIXEL |
| 110 | #undef HEXTILE_DECODE |
| 111 | } |