blob: db129883210c137505576e762ddc999228237342 [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
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000078 void print(char* str, int len) const;
79 bool parse(const char* str);
80
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000081 protected:
Pierre Ossman19dbca22009-04-21 17:30:45 +000082 void updateState(void);
Pierre Ossman6655d962014-01-20 14:50:19 +010083 bool isSane(void);
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000084
85 public:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000086 int bpp;
87 int depth;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010088
89 // This only tracks if the client thinks it is in colour map mode.
90 // In practice we are always in true colour mode.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000091 bool trueColour;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +000092
93 // FIXME: These should be protected, but we need to fix TransImageGetter first.
94 public:
95 bool bigEndian;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000096 int redMax;
97 int greenMax;
98 int blueMax;
99 int redShift;
100 int greenShift;
101 int blueShift;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000102
103 protected:
Pierre Ossman6e5cd5d2014-02-28 11:54:34 +0100104 /* Pre-computed values to keep algorithms simple */
105 int redBits, greenBits, blueBits;
106 int maxBits, minBits;
Pierre Ossman19dbca22009-04-21 17:30:45 +0000107 bool endianMismatch;
Pierre Ossman2fe68da2014-07-08 15:06:25 +0200108
109 static rdr::U8 upconvTable[256*8];
110
111 class Init;
112 friend class Init;
113 static Init _init;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000114 };
115}
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000116
117#include <rfb/PixelFormat.inl>
118
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119#endif