You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PixelFormat.h 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2009-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. // PixelFormat - structure to represent a pixel format. Also has useful
  22. // methods for reading & writing to streams, etc. Conversion to and from
  23. // other formats are also handled by this class. We have three different
  24. // representations that we refer to:
  25. //
  26. // a) Pixels - Unsigned native integers in the format specified by this
  27. // PixelFormat object.
  28. // b) Buffer - Same thing as pixels, but in the appropriate byte stream
  29. // format. This involves endian conversion and padding.
  30. // c) RGB - A byte stream of 8 bit red, green and blue elements, in that
  31. // order.
  32. //
  33. #ifndef __RFB_PIXELFORMAT_H__
  34. #define __RFB_PIXELFORMAT_H__
  35. #include <rfb/Pixel.h>
  36. namespace rdr { class InStream; class OutStream; }
  37. namespace rfb {
  38. class PixelFormat {
  39. public:
  40. PixelFormat(int b, int d, bool e, bool t,
  41. int rm, int gm, int bm, int rs, int gs, int bs);
  42. PixelFormat();
  43. // Checks if the formats have identical buffer representation.
  44. // They might still have different pixel representation, endianness
  45. // or true colour state.
  46. bool equal(const PixelFormat& other) const;
  47. void read(rdr::InStream* is);
  48. void write(rdr::OutStream* os) const;
  49. bool is888(void) const;
  50. bool isBigEndian(void) const;
  51. bool isLittleEndian(void) const;
  52. inline Pixel pixelFromBuffer(const rdr::U8* buffer) const;
  53. inline void bufferFromPixel(rdr::U8* buffer, Pixel pixel) const;
  54. inline Pixel pixelFromRGB(rdr::U16 red, rdr::U16 green, rdr::U16 blue) const;
  55. inline Pixel pixelFromRGB(rdr::U8 red, rdr::U8 green, rdr::U8 blue) const;
  56. void bufferFromRGB(rdr::U8 *dst, const rdr::U8* src, int pixels) const;
  57. void bufferFromRGB(rdr::U8 *dst, const rdr::U8* src,
  58. int w, int stride, int h) const;
  59. inline void rgbFromPixel(Pixel pix, rdr::U16 *r, rdr::U16 *g, rdr::U16 *b) const;
  60. inline void rgbFromPixel(Pixel pix, rdr::U8 *r, rdr::U8 *g, rdr::U8 *b) const;
  61. void rgbFromBuffer(rdr::U8* dst, const rdr::U8* src, int pixels) const;
  62. void rgbFromBuffer(rdr::U8* dst, const rdr::U8* src,
  63. int w, int stride, int h) const;
  64. Pixel pixelFromPixel(const PixelFormat &srcPF, Pixel src) const;
  65. void bufferFromBuffer(rdr::U8* dst, const PixelFormat &srcPF,
  66. const rdr::U8* src, int pixels) const;
  67. void bufferFromBuffer(rdr::U8* dst, const PixelFormat &srcPF,
  68. const rdr::U8* src, int w, int h,
  69. int dstStride, int srcStride) const;
  70. void print(char* str, int len) const;
  71. bool parse(const char* str);
  72. protected:
  73. void updateState(void);
  74. bool isSane(void);
  75. private:
  76. // Preprocessor generated, optimised methods
  77. void directBufferFromBufferFrom888(rdr::U8* dst, const PixelFormat &srcPF,
  78. const rdr::U8* src, int w, int h,
  79. int dstStride, int srcStride) const;
  80. void directBufferFromBufferFrom888(rdr::U16* dst, const PixelFormat &srcPF,
  81. const rdr::U8* src, int w, int h,
  82. int dstStride, int srcStride) const;
  83. void directBufferFromBufferFrom888(rdr::U32* dst, const PixelFormat &srcPF,
  84. const rdr::U8* src, int w, int h,
  85. int dstStride, int srcStride) const;
  86. void directBufferFromBufferTo888(rdr::U8* dst, const PixelFormat &srcPF,
  87. const rdr::U8* src, int w, int h,
  88. int dstStride, int srcStride) const;
  89. void directBufferFromBufferTo888(rdr::U8* dst, const PixelFormat &srcPF,
  90. const rdr::U16* src, int w, int h,
  91. int dstStride, int srcStride) const;
  92. void directBufferFromBufferTo888(rdr::U8* dst, const PixelFormat &srcPF,
  93. const rdr::U32* src, int w, int h,
  94. int dstStride, int srcStride) const;
  95. public:
  96. int bpp;
  97. int depth;
  98. // This only tracks if the client thinks it is in colour map mode.
  99. // In practice we are always in true colour mode.
  100. bool trueColour;
  101. protected:
  102. bool bigEndian;
  103. int redMax;
  104. int greenMax;
  105. int blueMax;
  106. int redShift;
  107. int greenShift;
  108. int blueShift;
  109. protected:
  110. /* Pre-computed values to keep algorithms simple */
  111. int redBits, greenBits, blueBits;
  112. int maxBits, minBits;
  113. bool endianMismatch;
  114. static rdr::U8 upconvTable[256*8];
  115. static rdr::U8 downconvTable[256*8];
  116. class Init;
  117. friend class Init;
  118. static Init _init;
  119. /* Only for testing this class */
  120. friend void makePixel(const rfb::PixelFormat &, rdr::U8 *);
  121. friend bool verifyPixel(const rfb::PixelFormat &,
  122. const rfb::PixelFormat &,
  123. const rdr::U8 *);
  124. };
  125. }
  126. #include <rfb/PixelFormat.inl>
  127. #endif