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 #define CONCAT2(a,b) a##b
20 #define CONCAT2E(a,b) CONCAT2(a,b)
21
22 #define UBPP CONCAT2E(U,BPP)
23
writePaletteTile(int width,int height,const rdr::UBPP * buffer,int stride,const PixelFormat & pf,const Palette & palette)24 void ZRLEEncoder::writePaletteTile(int width, int height,
25 const rdr::UBPP* buffer, int stride,
26 const PixelFormat& pf,
27 const Palette& palette)
28 {
29 const int bitsPerPackedPixel[] = {
30 0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
31 };
32
33 int bppp;
34 int pad;
35
36 assert(palette.size() > 1);
37 assert(palette.size() <= 16);
38
39 zos.writeU8(palette.size());
40 writePalette(pf, palette);
41
42 bppp = bitsPerPackedPixel[palette.size()-1];
43 pad = stride - width;
44
45 for (int i = 0; i < height; i++) {
46 int w;
47
48 rdr::U8 nbits = 0;
49 rdr::U8 byte = 0;
50
51 w = width;
52 while (w--) {
53 rdr::UBPP pix = *buffer++;
54 rdr::U8 index = palette.lookup(pix);
55 byte = (byte << bppp) | index;
56 nbits += bppp;
57 if (nbits >= 8) {
58 zos.writeU8(byte);
59 nbits = 0;
60 }
61 }
62 if (nbits > 0) {
63 byte <<= 8 - nbits;
64 zos.writeU8(byte);
65 }
66
67 buffer += pad;
68 }
69 }
70
writePaletteRLETile(int width,int height,const rdr::UBPP * buffer,int stride,const PixelFormat & pf,const Palette & palette)71 void ZRLEEncoder::writePaletteRLETile(int width, int height,
72 const rdr::UBPP* buffer, int stride,
73 const PixelFormat& pf,
74 const Palette& palette)
75 {
76 int pad;
77
78 rdr::UBPP prevColour;
79 int runLength;
80
81 assert(palette.size() > 1);
82 assert(palette.size() <= 127);
83
84 zos.writeU8(palette.size() | 0x80);
85 writePalette(pf, palette);
86
87 pad = stride - width;
88
89 prevColour = *buffer;
90 runLength = 0;
91
92 while (height--) {
93 int w = width;
94 while (w--) {
95 if (prevColour != *buffer) {
96 if (runLength == 1)
97 zos.writeU8(palette.lookup(prevColour));
98 else {
99 zos.writeU8(palette.lookup(prevColour) | 0x80);
100
101 while (runLength > 255) {
102 zos.writeU8(255);
103 runLength -= 255;
104 }
105 zos.writeU8(runLength - 1);
106 }
107
108 prevColour = *buffer;
109 runLength = 0;
110 }
111
112 runLength++;
113 buffer++;
114 }
115 buffer += pad;
116 }
117 if (runLength == 1)
118 zos.writeU8(palette.lookup(prevColour));
119 else {
120 zos.writeU8(palette.lookup(prevColour) | 0x80);
121
122 while (runLength > 255) {
123 zos.writeU8(255);
124 runLength -= 255;
125 }
126 zos.writeU8(runLength - 1);
127 }
128 }
129