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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
  3. * Copyright 2009-2022 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 uint8_t* buffer) const;
  53. inline void bufferFromPixel(uint8_t* buffer, Pixel pixel) const;
  54. inline Pixel pixelFromRGB(uint16_t red, uint16_t green, uint16_t blue) const;
  55. inline Pixel pixelFromRGB(uint8_t red, uint8_t green, uint8_t blue) const;
  56. void bufferFromRGB(uint8_t *dst, const uint8_t* src, int pixels) const;
  57. void bufferFromRGB(uint8_t *dst, const uint8_t* src,
  58. int w, int stride, int h) const;
  59. inline void rgbFromPixel(Pixel pix, uint16_t *r, uint16_t *g, uint16_t *b) const;
  60. inline void rgbFromPixel(Pixel pix, uint8_t *r, uint8_t *g, uint8_t *b) const;
  61. void rgbFromBuffer(uint8_t* dst, const uint8_t* src, int pixels) const;
  62. void rgbFromBuffer(uint8_t* dst, const uint8_t* src,
  63. int w, int stride, int h) const;
  64. Pixel pixelFromPixel(const PixelFormat &srcPF, Pixel src) const;
  65. void bufferFromBuffer(uint8_t* dst, const PixelFormat &srcPF,
  66. const uint8_t* src, int pixels) const;
  67. void bufferFromBuffer(uint8_t* dst, const PixelFormat &srcPF,
  68. const uint8_t* 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. // Templated, optimised methods
  77. template<class T>
  78. void directBufferFromBufferFrom888(T* dst, const PixelFormat &srcPF,
  79. const uint8_t* src, int w, int h,
  80. int dstStride, int srcStride) const;
  81. template<class T>
  82. void directBufferFromBufferTo888(uint8_t* dst, const PixelFormat &srcPF,
  83. const T* src, int w, int h,
  84. int dstStride, int srcStride) const;
  85. public:
  86. int bpp;
  87. int depth;
  88. // This only tracks if the client thinks it is in colour map mode.
  89. // In practice we are always in true colour mode.
  90. bool trueColour;
  91. protected:
  92. bool bigEndian;
  93. int redMax;
  94. int greenMax;
  95. int blueMax;
  96. int redShift;
  97. int greenShift;
  98. int blueShift;
  99. protected:
  100. /* Pre-computed values to keep algorithms simple */
  101. int redBits, greenBits, blueBits;
  102. int maxBits, minBits;
  103. bool endianMismatch;
  104. static uint8_t upconvTable[256*8];
  105. static uint8_t downconvTable[256*8];
  106. class Init;
  107. friend class Init;
  108. static Init _init;
  109. /* Only for testing this class */
  110. friend void makePixel(const rfb::PixelFormat &, uint8_t *);
  111. friend bool verifyPixel(const rfb::PixelFormat &,
  112. const rfb::PixelFormat &,
  113. const uint8_t *);
  114. };
  115. }
  116. #include <rfb/PixelFormat.inl>
  117. #endif