blob: 5b4b6332390a97c9ccd2467b17efdb1e476cbced [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
DRCffe09d62011-08-17 02:27:59 +00002 * Copyright (C) 2011 D. R. Commander. All Rights Reserved.
Pierre Ossman6655d962014-01-20 14:50:19 +01003 * Copyright 2009-2014 Pierre Ossman for Cendio AB
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00004 *
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
Pierre Ossman823665c2014-01-20 16:55:19 +010022// 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.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000032//
33
34#ifndef __RFB_PIXELFORMAT_H__
35#define __RFB_PIXELFORMAT_H__
36
37#include <rfb/Pixel.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000038
39namespace rdr { class InStream; class OutStream; }
40
41namespace rfb {
42
43 class PixelFormat {
44 public:
45 PixelFormat(int b, int d, bool e, bool t,
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010046 int rm, int gm, int bm, int rs, int gs, int bs);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000047 PixelFormat();
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000048
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010049 // Checks if the formats have identical buffer representation.
50 // They might still have different pixel representation, endianness
51 // or true colour state.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000052 bool equal(const PixelFormat& other) const;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000053
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 void read(rdr::InStream* is);
55 void write(rdr::OutStream* os) const;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000056
57 bool is888(void) const;
58 bool isBigEndian(void) const;
59 bool isLittleEndian(void) const;
60
61 inline Pixel pixelFromBuffer(const rdr::U8* buffer) const;
Pierre Ossman19501b82009-03-31 14:06:53 +000062 inline void bufferFromPixel(rdr::U8* buffer, Pixel pixel) const;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000063
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010064 inline Pixel pixelFromRGB(rdr::U16 red, rdr::U16 green, rdr::U16 blue) const;
65 inline Pixel pixelFromRGB(rdr::U8 red, rdr::U8 green, rdr::U8 blue) const;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000066
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010067 void bufferFromRGB(rdr::U8 *dst, const rdr::U8* src, int pixels) const;
68 void bufferFromRGB(rdr::U8 *dst, const rdr::U8* src,
69 int w, int stride, int h) const;
Pierre Ossman19501b82009-03-31 14:06:53 +000070
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010071 inline void rgbFromPixel(Pixel pix, rdr::U16 *r, rdr::U16 *g, rdr::U16 *b) const;
72 inline void rgbFromPixel(Pixel pix, rdr::U8 *r, rdr::U8 *g, rdr::U8 *b) const;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000073
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010074 void rgbFromBuffer(rdr::U8* dst, const rdr::U8* src, int pixels) const;
75 void rgbFromBuffer(rdr::U8* dst, const rdr::U8* src,
76 int w, int stride, int h) const;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000077
Pierre Ossman761fe242014-01-29 17:00:36 +010078 Pixel pixelFromPixel(const PixelFormat &srcPF, Pixel src) const;
79
80 void bufferFromBuffer(rdr::U8* dst, const PixelFormat &srcPF,
81 const rdr::U8* src, int pixels) const;
82 void bufferFromBuffer(rdr::U8* dst, const PixelFormat &srcPF,
83 const rdr::U8* src, int w, int h,
84 int dstStride, int srcStride) const;
85
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 void print(char* str, int len) const;
87 bool parse(const char* str);
88
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000089 protected:
Pierre Ossman19dbca22009-04-21 17:30:45 +000090 void updateState(void);
Pierre Ossman6655d962014-01-20 14:50:19 +010091 bool isSane(void);
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000092
Pierre Ossmanc02c05d2014-01-30 10:47:07 +010093 private:
94 // Preprocessor generated, optimised methods
95
96 void directBufferFromBufferFrom888(rdr::U8* dst, const PixelFormat &srcPF,
97 const rdr::U8* src, int w, int h,
98 int dstStride, int srcStride) const;
99 void directBufferFromBufferFrom888(rdr::U16* dst, const PixelFormat &srcPF,
100 const rdr::U8* src, int w, int h,
101 int dstStride, int srcStride) const;
102 void directBufferFromBufferFrom888(rdr::U32* dst, const PixelFormat &srcPF,
103 const rdr::U8* src, int w, int h,
104 int dstStride, int srcStride) const;
105
106 void directBufferFromBufferTo888(rdr::U8* dst, const PixelFormat &srcPF,
107 const rdr::U8* src, int w, int h,
108 int dstStride, int srcStride) const;
109 void directBufferFromBufferTo888(rdr::U8* dst, const PixelFormat &srcPF,
110 const rdr::U16* src, int w, int h,
111 int dstStride, int srcStride) const;
112 void directBufferFromBufferTo888(rdr::U8* dst, const PixelFormat &srcPF,
113 const rdr::U32* src, int w, int h,
114 int dstStride, int srcStride) const;
115
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000116 public:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117 int bpp;
118 int depth;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +0100119
120 // This only tracks if the client thinks it is in colour map mode.
121 // In practice we are always in true colour mode.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122 bool trueColour;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000123
Pierre Ossmana4148f82014-09-25 10:49:16 +0200124 protected:
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000125 bool bigEndian;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000126 int redMax;
127 int greenMax;
128 int blueMax;
129 int redShift;
130 int greenShift;
131 int blueShift;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000132
133 protected:
Pierre Ossman6e5cd5d2014-02-28 11:54:34 +0100134 /* Pre-computed values to keep algorithms simple */
135 int redBits, greenBits, blueBits;
136 int maxBits, minBits;
Pierre Ossman19dbca22009-04-21 17:30:45 +0000137 bool endianMismatch;
Pierre Ossman2fe68da2014-07-08 15:06:25 +0200138
139 static rdr::U8 upconvTable[256*8];
Pierre Ossman816baa32018-03-01 14:11:39 +0100140 static rdr::U8 downconvTable[256*8];
Pierre Ossman2fe68da2014-07-08 15:06:25 +0200141
142 class Init;
143 friend class Init;
144 static Init _init;
Pierre Ossman41deb882014-09-25 09:58:55 +0200145
146 /* Only for testing this class */
147 friend void makePixel(const rfb::PixelFormat &, rdr::U8 *);
148 friend bool verifyPixel(const rfb::PixelFormat &,
149 const rfb::PixelFormat &,
150 const rdr::U8 *);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151 };
152}
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000153
154#include <rfb/PixelFormat.inl>
155
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000156#endif