blob: 8874662cc6b4b4bd1991ca3ff16265f7b16a1eb0 [file] [log] [blame]
Pierre Ossmanc0397262014-03-14 15:59:46 +01001/* Copyright (C) 2000-2003 Constantin Kaplinsky. All Rights Reserved.
2 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
3 * Copyright 2014 Pierre Ossman for Cendio AB
4 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
21#define CONCAT2(a,b) a##b
22#define CONCAT2E(a,b) CONCAT2(a,b)
23
24#define UBPP CONCAT2E(U,BPP)
25
26void TightEncoder::writeMonoRect(int width, int height,
27 const rdr::UBPP* buffer, int stride,
28 const PixelFormat& pf,
29 const Palette& palette)
30{
31 rdr::OutStream* os;
32
33 const int streamId = 1;
34 rdr::UBPP pal[2];
35
36 int length;
37 rdr::OutStream* zos;
38
39 assert(palette.size() == 2);
40
41 os = conn->getOutStream();
42
43 os->writeU8((streamId | tightExplicitFilter) << 4);
44 os->writeU8(tightFilterPalette);
45
46 // Write the palette
47 pal[0] = (rdr::UBPP)palette.getColour(0);
48 pal[1] = (rdr::UBPP)palette.getColour(1);
49
50 os->writeU8(1);
51 writePixels((rdr::U8*)pal, pf, 2, os);
52
53 // Set up compression
54 length = (width + 7)/8 * height;
55 zos = getZlibOutStream(streamId, monoZlibLevel, length);
56
57 // Encode the data
58 rdr::UBPP bg;
59 unsigned int value, mask;
60 int pad, aligned_width;
61 int x, y, bg_bits;
62
63 bg = pal[0];
64 aligned_width = width - width % 8;
65 pad = stride - width;
66
67 for (y = 0; y < height; y++) {
68 for (x = 0; x < aligned_width; x += 8) {
69 for (bg_bits = 0; bg_bits < 8; bg_bits++) {
70 if (*buffer++ != bg)
71 break;
72 }
73 if (bg_bits == 8) {
74 zos->writeU8(0);
75 continue;
76 }
77 mask = 0x80 >> bg_bits;
78 value = mask;
79 for (bg_bits++; bg_bits < 8; bg_bits++) {
80 mask >>= 1;
81 if (*buffer++ != bg) {
82 value |= mask;
83 }
84 }
85 zos->writeU8(value);
86 }
87
88 if (x < width) {
89 mask = 0x80;
90 value = 0;
91
92 for (; x < width; x++) {
93 if (*buffer++ != bg) {
94 value |= mask;
95 }
96 mask >>= 1;
97 }
98 zos->writeU8(value);
99 }
100
101 buffer += pad;
102 }
103
104 // Finish the zlib stream
105 flushZlibOutStream(zos);
106}
107
108#if (BPP != 8)
109void TightEncoder::writeIndexedRect(int width, int height,
110 const rdr::UBPP* buffer, int stride,
111 const PixelFormat& pf,
112 const Palette& palette)
113{
114 rdr::OutStream* os;
115
116 const int streamId = 2;
117 rdr::UBPP pal[256];
118
119 rdr::OutStream* zos;
120
121 int pad;
122 rdr::UBPP prevColour;
123 unsigned char idx;
124
125 assert(palette.size() > 0);
126 assert(palette.size() <= 256);
127
128 os = conn->getOutStream();
129
130 os->writeU8((streamId | tightExplicitFilter) << 4);
131 os->writeU8(tightFilterPalette);
132
133 // Write the palette
134 for (int i = 0; i < palette.size(); i++)
135 pal[i] = (rdr::UBPP)palette.getColour(i);
136
137 os->writeU8(palette.size() - 1);
138 writePixels((rdr::U8*)pal, pf, palette.size(), os);
139
140 // Set up compression
141 zos = getZlibOutStream(streamId, idxZlibLevel, width * height);
142
143 // Encode the data
144 pad = stride - width;
145
146 prevColour = *buffer;
147 idx = palette.lookup(*buffer);
148
149 while (height--) {
150 int w = width;
151 while (w--) {
152 if (*buffer != prevColour) {
153 prevColour = *buffer;
154 idx = palette.lookup(*buffer);
155 }
156 zos->writeU8(idx);
157 buffer++;
158 }
159 buffer += pad;
160 }
161
162 // Finish the zlib stream
163 flushZlibOutStream(zos);
164}
165#endif // #if (BPP != 8)