/apps/desktop/libvncserver/tableinittctemplate.c

http://ftk.googlecode.com/ · C · 142 lines · 81 code · 19 blank · 42 comment · 12 complexity · 7943c24153fc6b77714d8f0f348b4505 MD5 · raw file

  1. /*
  2. * tableinittctemplate.c - template for initialising lookup tables for
  3. * truecolour to truecolour translation.
  4. *
  5. * This file shouldn't be compiled. It is included multiple times by
  6. * translate.c, each time with a different definition of the macro OUT.
  7. * For each value of OUT, this file defines two functions for initialising
  8. * lookup tables. One is for truecolour translation using a single lookup
  9. * table, the other is for truecolour translation using three separate
  10. * lookup tables for the red, green and blue values.
  11. *
  12. * I know this code isn't nice to read because of all the macros, but
  13. * efficiency is important here.
  14. */
  15. /*
  16. * OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
  17. * Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.
  18. * All Rights Reserved.
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This software is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this software; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  33. * USA.
  34. */
  35. #if !defined(OUT)
  36. #error "This file shouldn't be compiled."
  37. #error "It is included as part of translate.c"
  38. #endif
  39. #define OUT_T CONCAT3E(uint,OUT,_t)
  40. #define SwapOUT(x) CONCAT2E(Swap,OUT(x))
  41. #define rfbInitTrueColourSingleTableOUT \
  42. CONCAT2E(rfbInitTrueColourSingleTable,OUT)
  43. #define rfbInitTrueColourRGBTablesOUT CONCAT2E(rfbInitTrueColourRGBTables,OUT)
  44. #define rfbInitOneRGBTableOUT CONCAT2E(rfbInitOneRGBTable,OUT)
  45. static void
  46. rfbInitOneRGBTableOUT (OUT_T *table, int inMax, int outMax, int outShift,
  47. int swap);
  48. /*
  49. * rfbInitTrueColourSingleTable sets up a single lookup table for truecolour
  50. * translation.
  51. */
  52. static void
  53. rfbInitTrueColourSingleTableOUT (char **table, rfbPixelFormat *in,
  54. rfbPixelFormat *out)
  55. {
  56. int i;
  57. int inRed, inGreen, inBlue, outRed, outGreen, outBlue;
  58. OUT_T *t;
  59. int nEntries = 1 << in->bitsPerPixel;
  60. if (*table) free(*table);
  61. *table = (char *)malloc(nEntries * sizeof(OUT_T));
  62. t = (OUT_T *)*table;
  63. for (i = 0; i < nEntries; i++) {
  64. inRed = (i >> in->redShift) & in->redMax;
  65. inGreen = (i >> in->greenShift) & in->greenMax;
  66. inBlue = (i >> in->blueShift) & in->blueMax;
  67. outRed = (inRed * out->redMax + in->redMax / 2) / in->redMax;
  68. outGreen = (inGreen * out->greenMax + in->greenMax / 2) / in->greenMax;
  69. outBlue = (inBlue * out->blueMax + in->blueMax / 2) / in->blueMax;
  70. t[i] = ((outRed << out->redShift) |
  71. (outGreen << out->greenShift) |
  72. (outBlue << out->blueShift));
  73. #if (OUT != 8)
  74. if (out->bigEndian != in->bigEndian) {
  75. t[i] = SwapOUT(t[i]);
  76. }
  77. #endif
  78. }
  79. }
  80. /*
  81. * rfbInitTrueColourRGBTables sets up three separate lookup tables for the
  82. * red, green and blue values.
  83. */
  84. static void
  85. rfbInitTrueColourRGBTablesOUT (char **table, rfbPixelFormat *in,
  86. rfbPixelFormat *out)
  87. {
  88. OUT_T *redTable;
  89. OUT_T *greenTable;
  90. OUT_T *blueTable;
  91. if (*table) free(*table);
  92. *table = (char *)malloc((in->redMax + in->greenMax + in->blueMax + 3)
  93. * sizeof(OUT_T));
  94. redTable = (OUT_T *)*table;
  95. greenTable = redTable + in->redMax + 1;
  96. blueTable = greenTable + in->greenMax + 1;
  97. rfbInitOneRGBTableOUT (redTable, in->redMax, out->redMax,
  98. out->redShift, (out->bigEndian != in->bigEndian));
  99. rfbInitOneRGBTableOUT (greenTable, in->greenMax, out->greenMax,
  100. out->greenShift, (out->bigEndian != in->bigEndian));
  101. rfbInitOneRGBTableOUT (blueTable, in->blueMax, out->blueMax,
  102. out->blueShift, (out->bigEndian != in->bigEndian));
  103. }
  104. static void
  105. rfbInitOneRGBTableOUT (OUT_T *table, int inMax, int outMax, int outShift,
  106. int swap)
  107. {
  108. int i;
  109. int nEntries = inMax + 1;
  110. for (i = 0; i < nEntries; i++) {
  111. table[i] = ((i * outMax + inMax / 2) / inMax) << outShift;
  112. #if (OUT != 8)
  113. if (swap) {
  114. table[i] = SwapOUT(table[i]);
  115. }
  116. #endif
  117. }
  118. }
  119. #undef OUT_T
  120. #undef SwapOUT
  121. #undef rfbInitTrueColourSingleTableOUT
  122. #undef rfbInitTrueColourRGBTablesOUT
  123. #undef rfbInitOneRGBTableOUT