blob: b18045f7f2fc5566e772d6f9ae482b84b098ff9b [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
93 public:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000094 int bpp;
95 int depth;
Pierre Ossmanb6b4dc62014-01-20 15:05:21 +010096
97 // This only tracks if the client thinks it is in colour map mode.
98 // In practice we are always in true colour mode.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000099 bool trueColour;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000100
101 // FIXME: These should be protected, but we need to fix TransImageGetter first.
102 public:
103 bool bigEndian;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 int redMax;
105 int greenMax;
106 int blueMax;
107 int redShift;
108 int greenShift;
109 int blueShift;
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000110
111 protected:
Pierre Ossman6e5cd5d2014-02-28 11:54:34 +0100112 /* Pre-computed values to keep algorithms simple */
113 int redBits, greenBits, blueBits;
114 int maxBits, minBits;
Pierre Ossman19dbca22009-04-21 17:30:45 +0000115 bool endianMismatch;
Pierre Ossman2fe68da2014-07-08 15:06:25 +0200116
117 static rdr::U8 upconvTable[256*8];
118
119 class Init;
120 friend class Init;
121 static Init _init;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122 };
123}
Pierre Ossman67b2b2f2009-03-06 10:12:55 +0000124
125#include <rfb/PixelFormat.inl>
126
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000127#endif