/apps/desktop/libvncserver/zrleencodetemplate.c

http://ftk.googlecode.com/ · C · 272 lines · 180 code · 50 blank · 42 comment · 39 complexity · ebaf4f2ca4623a128df61816512f6da1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2002 RealVNC Ltd. All Rights Reserved.
  3. * Copyright (C) 2003 Sun Microsystems, Inc.
  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. * Before including this file, you must define a number of CPP macros.
  22. *
  23. * BPP should be 8, 16 or 32 depending on the bits per pixel.
  24. * GET_IMAGE_INTO_BUF should be some code which gets a rectangle of pixel data
  25. * into the given buffer. EXTRA_ARGS can be defined to pass any other
  26. * arguments needed by GET_IMAGE_INTO_BUF.
  27. *
  28. * Note that the buf argument to ZRLE_ENCODE needs to be at least one pixel
  29. * bigger than the largest tile of pixel data, since the ZRLE encoding
  30. * algorithm writes to the position one past the end of the pixel data.
  31. */
  32. #include "zrleoutstream.h"
  33. #include "zrlepalettehelper.h"
  34. #include <assert.h>
  35. /* __RFB_CONCAT2 concatenates its two arguments. __RFB_CONCAT2E does the same
  36. but also expands its arguments if they are macros */
  37. #ifndef __RFB_CONCAT2E
  38. #define __RFB_CONCAT2(a,b) a##b
  39. #define __RFB_CONCAT2E(a,b) __RFB_CONCAT2(a,b)
  40. #endif
  41. #ifdef CPIXEL
  42. #define PIXEL_T __RFB_CONCAT2E(zrle_U,BPP)
  43. #define zrleOutStreamWRITE_PIXEL __RFB_CONCAT2E(zrleOutStreamWriteOpaque,CPIXEL)
  44. #define ZRLE_ENCODE __RFB_CONCAT2E(zrleEncode,CPIXEL)
  45. #define ZRLE_ENCODE_TILE __RFB_CONCAT2E(zrleEncodeTile,CPIXEL)
  46. #define BPPOUT 24
  47. #else
  48. #define PIXEL_T __RFB_CONCAT2E(zrle_U,BPP)
  49. #define zrleOutStreamWRITE_PIXEL __RFB_CONCAT2E(zrleOutStreamWriteOpaque,BPP)
  50. #define ZRLE_ENCODE __RFB_CONCAT2E(zrleEncode,BPP)
  51. #define ZRLE_ENCODE_TILE __RFB_CONCAT2E(zrleEncodeTile,BPP)
  52. #define BPPOUT BPP
  53. #endif
  54. #ifndef ZRLE_ONCE
  55. #define ZRLE_ONCE
  56. static const int bitsPerPackedPixel[] = {
  57. 0, 1, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  58. };
  59. static zrlePaletteHelper paletteHelper;
  60. #endif /* ZRLE_ONCE */
  61. void ZRLE_ENCODE_TILE (PIXEL_T* data, int w, int h, zrleOutStream* os);
  62. static void ZRLE_ENCODE (int x, int y, int w, int h,
  63. zrleOutStream* os, void* buf
  64. EXTRA_ARGS
  65. )
  66. {
  67. int ty;
  68. for (ty = y; ty < y+h; ty += rfbZRLETileHeight) {
  69. int tx, th = rfbZRLETileHeight;
  70. if (th > y+h-ty) th = y+h-ty;
  71. for (tx = x; tx < x+w; tx += rfbZRLETileWidth) {
  72. int tw = rfbZRLETileWidth;
  73. if (tw > x+w-tx) tw = x+w-tx;
  74. GET_IMAGE_INTO_BUF(tx,ty,tw,th,buf);
  75. ZRLE_ENCODE_TILE((PIXEL_T*)buf, tw, th, os);
  76. }
  77. }
  78. zrleOutStreamFlush(os);
  79. }
  80. void ZRLE_ENCODE_TILE (PIXEL_T* data, int w, int h, zrleOutStream* os)
  81. {
  82. /* First find the palette and the number of runs */
  83. zrlePaletteHelper *ph;
  84. int runs = 0;
  85. int singlePixels = 0;
  86. rfbBool useRle;
  87. rfbBool usePalette;
  88. int estimatedBytes;
  89. int plainRleBytes;
  90. int i;
  91. PIXEL_T* ptr = data;
  92. PIXEL_T* end = ptr + h * w;
  93. *end = ~*(end-1); /* one past the end is different so the while loop ends */
  94. ph = &paletteHelper;
  95. zrlePaletteHelperInit(ph);
  96. while (ptr < end) {
  97. PIXEL_T pix = *ptr;
  98. if (*++ptr != pix) {
  99. singlePixels++;
  100. } else {
  101. while (*++ptr == pix) ;
  102. runs++;
  103. }
  104. zrlePaletteHelperInsert(ph, pix);
  105. }
  106. /* Solid tile is a special case */
  107. if (ph->size == 1) {
  108. zrleOutStreamWriteU8(os, 1);
  109. zrleOutStreamWRITE_PIXEL(os, ph->palette[0]);
  110. return;
  111. }
  112. /* Try to work out whether to use RLE and/or a palette. We do this by
  113. estimating the number of bytes which will be generated and picking the
  114. method which results in the fewest bytes. Of course this may not result
  115. in the fewest bytes after compression... */
  116. useRle = FALSE;
  117. usePalette = FALSE;
  118. estimatedBytes = w * h * (BPPOUT/8); /* start assuming raw */
  119. plainRleBytes = ((BPPOUT/8)+1) * (runs + singlePixels);
  120. if (plainRleBytes < estimatedBytes) {
  121. useRle = TRUE;
  122. estimatedBytes = plainRleBytes;
  123. }
  124. if (ph->size < 128) {
  125. int paletteRleBytes = (BPPOUT/8) * ph->size + 2 * runs + singlePixels;
  126. if (paletteRleBytes < estimatedBytes) {
  127. useRle = TRUE;
  128. usePalette = TRUE;
  129. estimatedBytes = paletteRleBytes;
  130. }
  131. if (ph->size < 17) {
  132. int packedBytes = ((BPPOUT/8) * ph->size +
  133. w * h * bitsPerPackedPixel[ph->size-1] / 8);
  134. if (packedBytes < estimatedBytes) {
  135. useRle = FALSE;
  136. usePalette = TRUE;
  137. estimatedBytes = packedBytes;
  138. }
  139. }
  140. }
  141. if (!usePalette) ph->size = 0;
  142. zrleOutStreamWriteU8(os, (useRle ? 128 : 0) | ph->size);
  143. for (i = 0; i < ph->size; i++) {
  144. zrleOutStreamWRITE_PIXEL(os, ph->palette[i]);
  145. }
  146. if (useRle) {
  147. PIXEL_T* ptr = data;
  148. PIXEL_T* end = ptr + w * h;
  149. PIXEL_T* runStart;
  150. PIXEL_T pix;
  151. while (ptr < end) {
  152. int len;
  153. runStart = ptr;
  154. pix = *ptr++;
  155. while (*ptr == pix && ptr < end)
  156. ptr++;
  157. len = ptr - runStart;
  158. if (len <= 2 && usePalette) {
  159. int index = zrlePaletteHelperLookup(ph, pix);
  160. if (len == 2)
  161. zrleOutStreamWriteU8(os, index);
  162. zrleOutStreamWriteU8(os, index);
  163. continue;
  164. }
  165. if (usePalette) {
  166. int index = zrlePaletteHelperLookup(ph, pix);
  167. zrleOutStreamWriteU8(os, index | 128);
  168. } else {
  169. zrleOutStreamWRITE_PIXEL(os, pix);
  170. }
  171. len -= 1;
  172. while (len >= 255) {
  173. zrleOutStreamWriteU8(os, 255);
  174. len -= 255;
  175. }
  176. zrleOutStreamWriteU8(os, len);
  177. }
  178. } else {
  179. /* no RLE */
  180. if (usePalette) {
  181. int bppp;
  182. PIXEL_T* ptr = data;
  183. /* packed pixels */
  184. assert (ph->size < 17);
  185. bppp = bitsPerPackedPixel[ph->size-1];
  186. for (i = 0; i < h; i++) {
  187. zrle_U8 nbits = 0;
  188. zrle_U8 byte = 0;
  189. PIXEL_T* eol = ptr + w;
  190. while (ptr < eol) {
  191. PIXEL_T pix = *ptr++;
  192. zrle_U8 index = zrlePaletteHelperLookup(ph, pix);
  193. byte = (byte << bppp) | index;
  194. nbits += bppp;
  195. if (nbits >= 8) {
  196. zrleOutStreamWriteU8(os, byte);
  197. nbits = 0;
  198. }
  199. }
  200. if (nbits > 0) {
  201. byte <<= 8 - nbits;
  202. zrleOutStreamWriteU8(os, byte);
  203. }
  204. }
  205. } else {
  206. /* raw */
  207. #ifdef CPIXEL
  208. PIXEL_T *ptr;
  209. for (ptr = data; ptr < data+w*h; ptr++) {
  210. zrleOutStreamWRITE_PIXEL(os, *ptr);
  211. }
  212. #else
  213. zrleOutStreamWriteBytes(os, (zrle_U8 *)data, w*h*(BPP/8));
  214. #endif
  215. }
  216. }
  217. }
  218. #undef PIXEL_T
  219. #undef zrleOutStreamWRITE_PIXEL
  220. #undef ZRLE_ENCODE
  221. #undef ZRLE_ENCODE_TILE
  222. #undef BPPOUT