blob: 7479d7e28c3b083668d2ee9b80d2300b79eb0043 [file] [log] [blame]
Peter Åstrand9b0809c2005-02-10 15:13:38 +00001/* Copyright (C) 2000-2003 Constantin Kaplinsky. 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//
20// tightEncode.h - Tight encoding function.
21//
22// This file is #included after having set the following macros:
23// BPP - 8, 16 or 32
24// EXTRA_ARGS - optional extra arguments
25// GET_IMAGE_INTO_BUF - gets a rectangle of pixel data into a buffer
26//
27
28#include <rdr/OutStream.h>
29#include <rdr/ZlibOutStream.h>
30#include <assert.h>
31
32namespace rfb {
33
34// CONCAT2E concatenates its arguments, expanding them if they are macros
35
36#ifndef CONCAT2E
37#define CONCAT2(a,b) a##b
38#define CONCAT2E(a,b) CONCAT2(a,b)
39#endif
40
41#define PIXEL_T rdr::CONCAT2E(U,BPP)
42#define WRITE_PIXEL CONCAT2E(writeOpaque,BPP)
43#define TIGHT_ENCODE CONCAT2E(tightEncode,BPP)
44#define SWAP_PIXEL CONCAT2E(SWAP,BPP)
45#define HASH_FUNCTION CONCAT2E(HASH_FUNC,BPP)
46#define PACK_PIXELS CONCAT2E(packPixels,BPP)
47#define DETECT_SMOOTH_IMAGE CONCAT2E(detectSmoothImage,BPP)
48#define ENCODE_SOLID_RECT CONCAT2E(encodeSolidRect,BPP)
49#define ENCODE_FULLCOLOR_RECT CONCAT2E(encodeFullColorRect,BPP)
50#define ENCODE_MONO_RECT CONCAT2E(encodeMonoRect,BPP)
51#define ENCODE_INDEXED_RECT CONCAT2E(encodeIndexedRect,BPP)
52#define PREPARE_JPEG_ROW CONCAT2E(prepareJpegRow,BPP)
53#define ENCODE_JPEG_RECT CONCAT2E(encodeJpegRect,BPP)
54#define FILL_PALETTE CONCAT2E(fillPalette,BPP)
55
56#ifndef TIGHT_ONCE
57#define TIGHT_ONCE
58
59//
60// C-style structures to store palette entries and compression paramentes.
61// Such code probably should be converted into C++ classes.
62//
63
64struct TIGHT_COLOR_LIST {
65 TIGHT_COLOR_LIST *next;
66 int idx;
67 rdr::U32 rgb;
68};
69
70struct TIGHT_PALETTE_ENTRY {
71 TIGHT_COLOR_LIST *listNode;
72 int numPixels;
73};
74
75struct TIGHT_PALETTE {
76 TIGHT_PALETTE_ENTRY entry[256];
77 TIGHT_COLOR_LIST *hash[256];
78 TIGHT_COLOR_LIST list[256];
79};
80
81// FIXME: Is it really a good idea to use static variables for this?
82static int s_endianMismatch; // local/remote formats differ in byte order
83static bool s_pack24; // use 24-bit packing for 32-bit pixels
84static int s_rs, s_gs, s_bs; // shifts for 24-bit pixel conversion
85
86// FIXME: Make a separate class for palette operations.
87static int s_palMaxColors, s_palNumColors;
88static rdr::U32 s_monoBackground, s_monoForeground;
89static TIGHT_PALETTE s_palette;
90
91//
92// Swapping bytes in pixels.
93// FIXME: Use a sort of ImageGetter that does not convert pixel format?
94//
95
96#ifndef SWAP16
97#define SWAP16(n) ((((n) & 0xff) << 8) | (((n) >> 8) & 0xff))
98#endif
99#ifndef SWAP32
100#define SWAP32(n) (((n) >> 24) | (((n) & 0x00ff0000) >> 8) | \
101 (((n) & 0x0000ff00) << 8) | ((n) << 24))
102#endif
103
104//
105// Functions to operate on palette structures.
106//
107
108#define HASH_FUNC16(rgb) ((int)(((rgb >> 8) + rgb) & 0xFF))
109#define HASH_FUNC32(rgb) ((int)(((rgb >> 16) + (rgb >> 8)) & 0xFF))
110
111static void paletteReset(void)
112{
113 s_palNumColors = 0;
114 memset(s_palette.hash, 0, 256 * sizeof(TIGHT_COLOR_LIST *));
115}
116
117static int paletteInsert(rdr::U32 rgb, int numPixels, int bpp)
118{
119 TIGHT_COLOR_LIST *pnode;
120 TIGHT_COLOR_LIST *prev_pnode = NULL;
121 int hash_key, idx, new_idx, count;
122
123 hash_key = (bpp == 16) ? HASH_FUNC16(rgb) : HASH_FUNC32(rgb);
124
125 pnode = s_palette.hash[hash_key];
126
127 while (pnode != NULL) {
128 if (pnode->rgb == rgb) {
129 // Such palette entry already exists.
130 new_idx = idx = pnode->idx;
131 count = s_palette.entry[idx].numPixels + numPixels;
132 if (new_idx && s_palette.entry[new_idx-1].numPixels < count) {
133 do {
134 s_palette.entry[new_idx] = s_palette.entry[new_idx-1];
135 s_palette.entry[new_idx].listNode->idx = new_idx;
136 new_idx--;
137 }
138 while (new_idx &&
139 s_palette.entry[new_idx-1].numPixels < count);
140 s_palette.entry[new_idx].listNode = pnode;
141 pnode->idx = new_idx;
142 }
143 s_palette.entry[new_idx].numPixels = count;
144 return s_palNumColors;
145 }
146 prev_pnode = pnode;
147 pnode = pnode->next;
148 }
149
150 // Check if palette is full.
151 if ( s_palNumColors == 256 || s_palNumColors == s_palMaxColors ) {
152 s_palNumColors = 0;
153 return 0;
154 }
155
156 // Move palette entries with lesser pixel counts.
157 for ( idx = s_palNumColors;
158 idx > 0 && s_palette.entry[idx-1].numPixels < numPixels;
159 idx-- ) {
160 s_palette.entry[idx] = s_palette.entry[idx-1];
161 s_palette.entry[idx].listNode->idx = idx;
162 }
163
164 // Add new palette entry into the freed slot.
165 pnode = &s_palette.list[s_palNumColors];
166 if (prev_pnode != NULL) {
167 prev_pnode->next = pnode;
168 } else {
169 s_palette.hash[hash_key] = pnode;
170 }
171 pnode->next = NULL;
172 pnode->idx = idx;
173 pnode->rgb = rgb;
174 s_palette.entry[idx].listNode = pnode;
175 s_palette.entry[idx].numPixels = numPixels;
176
177 return (++s_palNumColors);
178}
179
180//
181// Compress the data (but do not perform actual compression if the data
182// size is less than TIGHT_MIN_TO_COMPRESS bytes.
183//
184
185static void compressData(rdr::OutStream *os, rdr::ZlibOutStream *zos,
186 const void *buf, unsigned int length, int zlibLevel)
187{
188 if (length < TIGHT_MIN_TO_COMPRESS) {
189 os->writeBytes(buf, length);
190 } else {
191 // FIXME: Using a temporary MemOutStream may be not efficient.
192 // Maybe use the same static object used in the JPEG coder?
193 rdr::MemOutStream mem_os;
194 zos->setUnderlying(&mem_os);
195 zos->writeBytes(buf, length);
196 zos->flush();
197 os->writeCompactLength(mem_os.length());
198 os->writeBytes(mem_os.data(), mem_os.length());
199 }
200}
201
202//
203// Destination manager implementation for the JPEG library.
204// FIXME: Implement JPEG compression in new rdr::JpegOutStream class.
205//
206
207// FIXME: Keeping a MemOutStream instance may consume too much space.
208rdr::MemOutStream s_jpeg_os;
209
210static struct jpeg_destination_mgr s_jpegDstManager;
211static JOCTET *s_jpegDstBuffer;
212static size_t s_jpegDstBufferLen;
213
214static void
215JpegInitDestination(j_compress_ptr cinfo)
216{
217 s_jpeg_os.clear();
218 s_jpegDstManager.next_output_byte = s_jpegDstBuffer;
219 s_jpegDstManager.free_in_buffer = s_jpegDstBufferLen;
220}
221
222static boolean
223JpegEmptyOutputBuffer(j_compress_ptr cinfo)
224{
225 s_jpeg_os.writeBytes(s_jpegDstBuffer, s_jpegDstBufferLen);
226 s_jpegDstManager.next_output_byte = s_jpegDstBuffer;
227 s_jpegDstManager.free_in_buffer = s_jpegDstBufferLen;
228
229 return TRUE;
230}
231
232static void
233JpegTermDestination(j_compress_ptr cinfo)
234{
235 int dataLen = s_jpegDstBufferLen - s_jpegDstManager.free_in_buffer;
236 s_jpeg_os.writeBytes(s_jpegDstBuffer, dataLen);
237}
238
239static void
240JpegSetDstManager(j_compress_ptr cinfo, JOCTET *buf, size_t buflen)
241{
242 s_jpegDstBuffer = buf;
243 s_jpegDstBufferLen = buflen;
244 s_jpegDstManager.init_destination = JpegInitDestination;
245 s_jpegDstManager.empty_output_buffer = JpegEmptyOutputBuffer;
246 s_jpegDstManager.term_destination = JpegTermDestination;
247 cinfo->dest = &s_jpegDstManager;
248}
249
250#endif // #ifndef TIGHT_ONCE
251
252static void ENCODE_SOLID_RECT (rdr::OutStream *os,
253 PIXEL_T *buf);
254static void ENCODE_FULLCOLOR_RECT (rdr::OutStream *os, rdr::ZlibOutStream zos[4],
255 PIXEL_T *buf, const Rect& r);
256static void ENCODE_MONO_RECT (rdr::OutStream *os, rdr::ZlibOutStream zos[4],
257 PIXEL_T *buf, const Rect& r);
258#if (BPP != 8)
259static void ENCODE_INDEXED_RECT (rdr::OutStream *os, rdr::ZlibOutStream zos[4],
260 PIXEL_T *buf, const Rect& r);
261static void ENCODE_JPEG_RECT (rdr::OutStream *os,
262 PIXEL_T *buf, const PixelFormat& pf, const Rect& r);
263#endif
264
265static void FILL_PALETTE (PIXEL_T *data, int count);
266
267//
268// Convert 32-bit color samples into 24-bit colors, in place.
269// Performs packing only when redMax, greenMax and blueMax are all 255.
270// Color components are assumed to be byte-aligned.
271//
272
273static inline unsigned int PACK_PIXELS (PIXEL_T *buf, unsigned int count)
274{
275#if (BPP != 32)
276 return count * sizeof(PIXEL_T);
277#else
278 if (!s_pack24)
279 return count * sizeof(PIXEL_T);
280
281 rdr::U32 pix;
282 rdr::U8 *dst = (rdr::U8 *)buf;
283 for (unsigned int i = 0; i < count; i++) {
284 pix = *buf++;
285 *dst++ = (rdr::U8)(pix >> s_rs);
286 *dst++ = (rdr::U8)(pix >> s_gs);
287 *dst++ = (rdr::U8)(pix >> s_bs);
288 }
289 return count * 3;
290#endif
291}
292
293//
294// Function to guess if a given rectangle is suitable for JPEG compression.
295// Returns true is it looks like a good data for JPEG, false otherwise.
296//
297// FIXME: Scan the image and determine is it really good for JPEG.
298//
299
300#if (BPP != 8)
301static bool DETECT_SMOOTH_IMAGE (PIXEL_T *buf, const Rect& r)
302{
303 if (r.width() < TIGHT_DETECT_MIN_WIDTH ||
304 r.height() < TIGHT_DETECT_MIN_HEIGHT ||
305 r.area() < TIGHT_JPEG_MIN_RECT_SIZE ||
306 s_pjconf == NULL)
307 return 0;
308
309 return 1;
310}
311#endif
312
313// FIXME: Split rectangles into smaller ones!
314// FIXME: Compare encoder code with 1.3 before the final version.
315
316//
317// Main function of the Tight encoder
318//
319
320void TIGHT_ENCODE (const Rect& r, rdr::OutStream *os,
321 rdr::ZlibOutStream zos[4], void* buf, ConnParams* cp
322#ifdef EXTRA_ARGS
323 , EXTRA_ARGS
324#endif
325 )
326{
327 const PixelFormat& pf = cp->pf();
328 GET_IMAGE_INTO_BUF(r, buf);
329 PIXEL_T* pixels = (PIXEL_T*)buf;
330
331#if (BPP != 8)
332 union {
333 rdr::U32 value32;
334 rdr::U8 test;
335 } littleEndian;
336 littleEndian.value32 = 1;
337 s_endianMismatch = (littleEndian.test != !pf.bigEndian);
338#endif
339
340#if (BPP == 32)
341 // Check if it's necessary to pack 24-bit pixels, and
342 // compute appropriate shift values if necessary.
343 s_pack24 = (pf.depth == 24 && pf.redMax == 0xFF &&
344 pf.greenMax == 0xFF && pf.blueMax == 0xFF);
345 if (s_pack24) {
346 if (!s_endianMismatch) {
347 s_rs = pf.redShift;
348 s_gs = pf.greenShift;
349 s_bs = pf.blueShift;
350 } else {
351 s_rs = 24 - pf.redShift;
352 s_gs = 24 - pf.greenShift;
353 s_bs = 24 - pf.blueShift;
354 }
355 }
356#endif
357
358 s_palMaxColors = r.area() / s_pconf->idxMaxColorsDivisor;
359 if (s_palMaxColors < 2 && r.area() >= s_pconf->monoMinRectSize) {
360 s_palMaxColors = 2;
361 }
362 FILL_PALETTE(pixels, r.area());
363
364 switch (s_palNumColors) {
365 case 0:
366 // Truecolor image
367#if (BPP != 8)
368 if (s_pjconf != NULL && DETECT_SMOOTH_IMAGE(pixels, r)) {
369 ENCODE_JPEG_RECT(os, pixels, pf, r);
370 break;
371 }
372#endif
373 ENCODE_FULLCOLOR_RECT(os, zos, pixels, r);
374 break;
375 case 1:
376 // Solid rectangle
377 ENCODE_SOLID_RECT(os, pixels);
378 break;
379 case 2:
380 // Two-color rectangle
381 ENCODE_MONO_RECT(os, zos, pixels, r);
382 break;
383#if (BPP != 8)
384 default:
385 // Up to 256 different colors
386 if (s_palNumColors > 96 && s_pjconf != NULL &&
387 DETECT_SMOOTH_IMAGE(pixels, r)) {
388 ENCODE_JPEG_RECT(os, pixels, pf, r);
389 } else {
390 ENCODE_INDEXED_RECT(os, zos, pixels, r);
391 }
392#endif
393 }
394}
395
396//
397// Subencoding implementations.
398//
399
400static void ENCODE_SOLID_RECT (rdr::OutStream *os, PIXEL_T *buf)
401{
402 os->writeU8(0x08 << 4);
403
404 int length = PACK_PIXELS(buf, 1);
405 os->writeBytes(buf, length);
406}
407
408static void ENCODE_FULLCOLOR_RECT (rdr::OutStream *os, rdr::ZlibOutStream zos[4],
409 PIXEL_T *buf, const Rect& r)
410{
411 const int streamId = 0;
412 os->writeU8(streamId << 4);
413
414 int length = PACK_PIXELS(buf, r.area());
415 compressData(os, &zos[streamId], buf, length, s_pconf->rawZlibLevel);
416}
417
418static void ENCODE_MONO_RECT (rdr::OutStream *os, rdr::ZlibOutStream zos[4],
419 PIXEL_T *buf, const Rect& r)
420{
421 const int streamId = 1;
422 os->writeU8((streamId | 0x04) << 4);
423 os->writeU8(0x01);
424
425 // Write the palette
426 PIXEL_T pal[2] = { (PIXEL_T)s_monoBackground, (PIXEL_T)s_monoForeground };
427 os->writeU8(1);
428 os->writeBytes(pal, PACK_PIXELS(pal, 2));
429
430 // Encode the data in-place
431 PIXEL_T *src = buf;
432 rdr::U8 *dst = (rdr::U8 *)buf;
433 int w = r.width();
434 int h = r.height();
435 PIXEL_T bg;
436 unsigned int value, mask;
437 int aligned_width;
438 int x, y, bg_bits;
439
440 bg = (PIXEL_T) s_monoBackground;
441 aligned_width = w - w % 8;
442
443 for (y = 0; y < h; y++) {
444 for (x = 0; x < aligned_width; x += 8) {
445 for (bg_bits = 0; bg_bits < 8; bg_bits++) {
446 if (*src++ != bg)
447 break;
448 }
449 if (bg_bits == 8) {
450 *dst++ = 0;
451 continue;
452 }
453 mask = 0x80 >> bg_bits;
454 value = mask;
455 for (bg_bits++; bg_bits < 8; bg_bits++) {
456 mask >>= 1;
457 if (*src++ != bg) {
458 value |= mask;
459 }
460 }
461 *dst++ = (rdr::U8)value;
462 }
463
464 mask = 0x80;
465 value = 0;
466 if (x >= w)
467 continue;
468
469 for (; x < w; x++) {
470 if (*src++ != bg) {
471 value |= mask;
472 }
473 mask >>= 1;
474 }
475 *dst++ = (rdr::U8)value;
476 }
477
478 // Write the data
479 int length = (w + 7) / 8;
480 length *= h;
481 compressData(os, &zos[streamId], buf, length, s_pconf->monoZlibLevel);
482}
483
484#if (BPP != 8)
485static void ENCODE_INDEXED_RECT (rdr::OutStream *os, rdr::ZlibOutStream zos[4],
486 PIXEL_T *buf, const Rect& r)
487{
488 const int streamId = 2;
489 os->writeU8((streamId | 0x04) << 4);
490 os->writeU8(0x01);
491
492 // Write the palette
493 {
494 PIXEL_T pal[256];
495 for (int i = 0; i < s_palNumColors; i++)
496 pal[i] = (PIXEL_T)s_palette.entry[i].listNode->rgb;
497 os->writeU8((rdr::U8)(s_palNumColors - 1));
498 os->writeBytes(pal, PACK_PIXELS(pal, s_palNumColors));
499 }
500
501 // Encode data in-place
502 PIXEL_T *src = buf;
503 rdr::U8 *dst = (rdr::U8 *)buf;
504 int count = r.area();
505 PIXEL_T rgb;
506 TIGHT_COLOR_LIST *pnode;
507 int rep = 0;
508
509 while (count--) {
510 rgb = *src++;
511 while (count && *src == rgb) {
512 rep++, src++, count--;
513 }
514 pnode = s_palette.hash[HASH_FUNCTION(rgb)];
515 while (pnode != NULL) {
516 if ((PIXEL_T)pnode->rgb == rgb) {
517 *dst++ = (rdr::U8)pnode->idx;
518 while (rep) {
519 *dst++ = (rdr::U8)pnode->idx;
520 rep--;
521 }
522 break;
523 }
524 pnode = pnode->next;
525 }
526 }
527
528 // Write the data
529 compressData(os, &zos[streamId], buf, r.area(), s_pconf->idxZlibLevel);
530}
531#endif // #if (BPP != 8)
532
533//
534// JPEG compression.
535//
536
537#if (BPP != 8)
538static void PREPARE_JPEG_ROW (PIXEL_T *src, const PixelFormat& pf,
539 rdr::U8 *dst, int count)
540{
541 // FIXME: Add a version of this function optimized for 24-bit colors?
542 PIXEL_T pix;
543 while (count--) {
544 pix = *src++;
545 if (s_endianMismatch)
546 pix = SWAP_PIXEL(pix);
547 *dst++ = (rdr::U8)((pix >> pf.redShift & pf.redMax) * 255 / pf.redMax);
548 *dst++ = (rdr::U8)((pix >> pf.greenShift & pf.greenMax) * 255 / pf.greenMax);
549 *dst++ = (rdr::U8)((pix >> pf.blueShift & pf.blueMax) * 255 / pf.blueMax);
550 }
551}
552#endif // #if (BPP != 8)
553
554#if (BPP != 8)
555static void ENCODE_JPEG_RECT (rdr::OutStream *os, PIXEL_T *buf,
556 const PixelFormat& pf, const Rect& r)
557{
558 int w = r.width();
559 int h = r.height();
560
561 struct jpeg_compress_struct cinfo;
562 struct jpeg_error_mgr jerr;
563
564 // FIXME: Make srcBuf[] and/or dstBuf[] static?
565 rdr::U8 *srcBuf = new rdr::U8[w * 3];
566 JSAMPROW rowPointer[1];
567 rowPointer[0] = (JSAMPROW)srcBuf;
568
569 cinfo.err = jpeg_std_error(&jerr);
570 jpeg_create_compress(&cinfo);
571
572 cinfo.image_width = w;
573 cinfo.image_height = h;
574 cinfo.input_components = 3;
575 cinfo.in_color_space = JCS_RGB;
576
577 jpeg_set_defaults(&cinfo);
578 jpeg_set_quality(&cinfo, s_pjconf->jpegQuality, TRUE);
579
580 rdr::U8 *dstBuf = new rdr::U8[2048];
581 JpegSetDstManager(&cinfo, dstBuf, 2048);
582
583 jpeg_start_compress(&cinfo, TRUE);
584 for (int dy = 0; dy < h; dy++) {
585 PREPARE_JPEG_ROW(&buf[dy * w], pf, srcBuf, w);
586 jpeg_write_scanlines(&cinfo, rowPointer, 1);
587 }
588 jpeg_finish_compress(&cinfo);
589 jpeg_destroy_compress(&cinfo);
590
591 delete[] srcBuf;
592 delete[] dstBuf;
593
594 os->writeU8(0x09 << 4);
595 os->writeCompactLength(s_jpeg_os.length());
596 os->writeBytes(s_jpeg_os.data(), s_jpeg_os.length());
597}
598#endif // #if (BPP != 8)
599
600//
601// Determine the number of colors in the rectangle, and fill in the palette.
602//
603
604#if (BPP == 8)
605static void FILL_PALETTE (PIXEL_T *data, int count)
606{
607 PIXEL_T c0, c1;
608 int i, n0, n1;
609
610 s_palNumColors = 0;
611
612 c0 = data[0];
613 for (i = 1; i < count && data[i] == c0; i++);
614 if (i == count) {
615 s_palNumColors = 1;
616 return; // Solid rectangle
617 }
618
619 if (s_palMaxColors < 2)
620 return;
621
622 n0 = i;
623 c1 = data[i];
624 n1 = 0;
625 for (i++; i < count; i++) {
626 if (data[i] == c0) {
627 n0++;
628 } else if (data[i] == c1) {
629 n1++;
630 } else
631 break;
632 }
633 if (i == count) {
634 if (n0 > n1) {
635 s_monoBackground = (rdr::U32)c0;
636 s_monoForeground = (rdr::U32)c1;
637 } else {
638 s_monoBackground = (rdr::U32)c1;
639 s_monoForeground = (rdr::U32)c0;
640 }
641 s_palNumColors = 2; // Two colors
642 }
643}
644#else // (BPP != 8)
645static void FILL_PALETTE (PIXEL_T *data, int count)
646{
647 PIXEL_T c0, c1, ci = 0;
648 int i, n0, n1, ni;
649
650 c0 = data[0];
651 for (i = 1; i < count && data[i] == c0; i++);
652 if (i >= count) {
653 s_palNumColors = 1; // Solid rectangle
654 return;
655 }
656
657 if (s_palMaxColors < 2) {
658 s_palNumColors = 0; // Full-color format preferred
659 return;
660 }
661
662 n0 = i;
663 c1 = data[i];
664 n1 = 0;
665 for (i++; i < count; i++) {
666 ci = data[i];
667 if (ci == c0) {
668 n0++;
669 } else if (ci == c1) {
670 n1++;
671 } else
672 break;
673 }
674 if (i >= count) {
675 if (n0 > n1) {
676 s_monoBackground = (rdr::U32)c0;
677 s_monoForeground = (rdr::U32)c1;
678 } else {
679 s_monoBackground = (rdr::U32)c1;
680 s_monoForeground = (rdr::U32)c0;
681 }
682 s_palNumColors = 2; // Two colors
683 return;
684 }
685
686 paletteReset();
687 paletteInsert (c0, (rdr::U32)n0, BPP);
688 paletteInsert (c1, (rdr::U32)n1, BPP);
689
690 ni = 1;
691 for (i++; i < count; i++) {
692 if (data[i] == ci) {
693 ni++;
694 } else {
695 if (!paletteInsert (ci, (rdr::U32)ni, BPP))
696 return;
697 ci = data[i];
698 ni = 1;
699 }
700 }
701 paletteInsert (ci, (rdr::U32)ni, BPP);
702}
703#endif // #if (BPP == 8)
704
705#undef PIXEL_T
706#undef WRITE_PIXEL
707#undef TIGHT_ENCODE
708#undef SWAP_PIXEL
709#undef HASH_FUNCTION
710#undef PACK_PIXELS
711#undef DETECT_SMOOTH_IMAGE
712#undef ENCODE_SOLID_RECT
713#undef ENCODE_FULLCOLOR_RECT
714#undef ENCODE_MONO_RECT
715#undef ENCODE_INDEXED_RECT
716#undef PREPARE_JPEG_ROW
717#undef ENCODE_JPEG_RECT
718#undef FILL_PALETTE
719}