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 | // |
| 21 | // This file is #included after having set the following macros: |
| 22 | // BPP - 8, 16 or 32 |
| 23 | // EXTRA_ARGS - optional extra arguments |
| 24 | // FILL_RECT - fill a rectangle with a single colour |
| 25 | // IMAGE_RECT - draw a rectangle of pixel data from a buffer |
| 26 | |
| 27 | #include <rdr/InStream.h> |
| 28 | #include <rfb/hextileConstants.h> |
| 29 | |
| 30 | namespace rfb { |
| 31 | |
| 32 | // CONCAT2E concatenates its arguments, expanding them if they are macros |
| 33 | |
| 34 | #ifndef CONCAT2E |
| 35 | #define CONCAT2(a,b) a##b |
| 36 | #define CONCAT2E(a,b) CONCAT2(a,b) |
| 37 | #endif |
| 38 | |
| 39 | #define PIXEL_T rdr::CONCAT2E(U,BPP) |
| 40 | #define READ_PIXEL CONCAT2E(readOpaque,BPP) |
| 41 | #define HEXTILE_DECODE CONCAT2E(hextileDecode,BPP) |
| 42 | |
| 43 | void HEXTILE_DECODE (const Rect& r, rdr::InStream* is, PIXEL_T* buf |
| 44 | #ifdef EXTRA_ARGS |
| 45 | , EXTRA_ARGS |
| 46 | #endif |
| 47 | ) |
| 48 | { |
| 49 | Rect t; |
| 50 | PIXEL_T bg = 0; |
| 51 | PIXEL_T fg = 0; |
| 52 | |
| 53 | for (t.tl.y = r.tl.y; t.tl.y < r.br.y; t.tl.y += 16) { |
| 54 | |
| 55 | t.br.y = __rfbmin(r.br.y, t.tl.y + 16); |
| 56 | |
| 57 | for (t.tl.x = r.tl.x; t.tl.x < r.br.x; t.tl.x += 16) { |
| 58 | |
| 59 | t.br.x = __rfbmin(r.br.x, t.tl.x + 16); |
| 60 | |
| 61 | int tileType = is->readU8(); |
| 62 | |
| 63 | if (tileType & hextileRaw) { |
| 64 | is->readBytes(buf, t.area() * (BPP/8)); |
| 65 | IMAGE_RECT(t, buf); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | if (tileType & hextileBgSpecified) |
| 70 | bg = is->READ_PIXEL(); |
| 71 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 72 | int len = t.area(); |
| 73 | PIXEL_T* ptr = (PIXEL_T*)buf; |
| 74 | while (len-- > 0) *ptr++ = bg; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | |
| 76 | if (tileType & hextileFgSpecified) |
| 77 | fg = is->READ_PIXEL(); |
| 78 | |
| 79 | if (tileType & hextileAnySubrects) { |
| 80 | int nSubrects = is->readU8(); |
| 81 | |
| 82 | for (int i = 0; i < nSubrects; i++) { |
| 83 | |
| 84 | if (tileType & hextileSubrectsColoured) |
| 85 | fg = is->READ_PIXEL(); |
| 86 | |
| 87 | int xy = is->readU8(); |
| 88 | int wh = is->readU8(); |
| 89 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 90 | int x = ((xy >> 4) & 15); |
| 91 | int y = (xy & 15); |
| 92 | int w = ((wh >> 4) & 15) + 1; |
| 93 | int h = (wh & 15) + 1; |
| 94 | PIXEL_T* ptr = (PIXEL_T*)buf + y * t.width() + x; |
| 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 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 103 | IMAGE_RECT(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 | } |