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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <stdint.h>
  36. namespace rdr { class InStream; class OutStream; }
  37. namespace rfb {
  38. typedef uint32_t Pixel; // must be big enough to hold any pixel value
  39. class PixelFormat {
  40. public:
  41. PixelFormat(int b, int d, bool e, bool t,
  42. int rm, int gm, int bm, int rs, int gs, int bs);
  43. PixelFormat();
  44. // Checks if the formats have identical buffer representation.
  45. // They might still have different pixel representation, endianness
  46. // or true colour state.
  47. bool operator==(const PixelFormat& other) const;
  48. bool operator!=(const PixelFormat& other) const;
  49. void read(rdr::InStream* is);
  50. void write(rdr::OutStream* os) const;
  51. bool is888(void) const;
  52. bool isBigEndian(void) const;
  53. bool isLittleEndian(void) const;
  54. inline Pixel pixelFromBuffer(const uint8_t* buffer) const;
  55. inline void bufferFromPixel(uint8_t* buffer, Pixel pixel) const;
  56. inline Pixel pixelFromRGB(uint16_t red, uint16_t green, uint16_t blue) const;
  57. inline Pixel pixelFromRGB(uint8_t red, uint8_t green, uint8_t blue) const;
  58. void bufferFromRGB(uint8_t *dst, const uint8_t* src, int pixels) const;
  59. void bufferFromRGB(uint8_t *dst, const uint8_t* src,
  60. int w, int stride, int h) const;
  61. inline void rgbFromPixel(Pixel pix, uint16_t *r, uint16_t *g, uint16_t *b) const;
  62. inline void rgbFromPixel(Pixel pix, uint8_t *r, uint8_t *g, uint8_t *b) const;
  63. void rgbFromBuffer(uint8_t* dst, const uint8_t* src, int pixels) const;
  64. void rgbFromBuffer(uint8_t* dst, const uint8_t* src,
  65. int w, int stride, int h) const;
  66. Pixel pixelFromPixel(const PixelFormat &srcPF, Pixel src) const;
  67. void bufferFromBuffer(uint8_t* dst, const PixelFormat &srcPF,
  68. const uint8_t* src, int pixels) const;
  69. void bufferFromBuffer(uint8_t* dst, const PixelFormat &srcPF,
  70. const uint8_t* src, int w, int h,
  71. int dstStride, int srcStride) const;
  72. void print(char* str, int len) const;
  73. bool parse(const char* str);
  74. protected:
  75. void updateState(void);
  76. bool isSane(void);
  77. private:
  78. // Templated, optimised methods
  79. template<class T>
  80. void directBufferFromBufferFrom888(T* dst, const PixelFormat &srcPF,
  81. const uint8_t* src, int w, int h,
  82. int dstStride, int srcStride) const;
  83. template<class T>
  84. void directBufferFromBufferTo888(uint8_t* dst, const PixelFormat &srcPF,
  85. const T* src, int w, int h,
  86. int dstStride, int srcStride) const;
  87. public:
  88. int bpp;
  89. int depth;
  90. // This only tracks if the client thinks it is in colour map mode.
  91. // In practice we are always in true colour mode.
  92. bool trueColour;
  93. protected:
  94. bool bigEndian;
  95. int redMax;
  96. int greenMax;
  97. int blueMax;
  98. int redShift;
  99. int greenShift;
  100. int blueShift;
  101. protected:
  102. /* Pre-computed values to keep algorithms simple */
  103. int redBits, greenBits, blueBits;
  104. int maxBits, minBits;
  105. bool endianMismatch;
  106. static uint8_t upconvTable[256*8];
  107. static uint8_t downconvTable[256*8];
  108. class Init;
  109. friend class Init;
  110. static Init _init;
  111. /* Only for testing this class */
  112. friend void makePixel(const rfb::PixelFormat &, uint8_t *);
  113. friend bool verifyPixel(const rfb::PixelFormat &,
  114. const rfb::PixelFormat &,
  115. const uint8_t *);
  116. };
  117. }
  118. #include <rfb/PixelFormat.inl>
  119. #endif