summaryrefslogtreecommitdiffstats
path: root/common/rfb/PixelBuffer.h
blob: 0d1526357af000efffcd41a464a6037b33559abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// -=- PixelBuffer.h
//
// The PixelBuffer class encapsulates the PixelFormat and dimensions
// of a block of pixel data.

#ifndef __RFB_PIXEL_BUFFER_H__
#define __RFB_PIXEL_BUFFER_H__

#include <rfb/ImageGetter.h>
#include <rfb/PixelFormat.h>
#include <rfb/ColourMap.h>
#include <rfb/Rect.h>
#include <rfb/Pixel.h>

namespace rfb {

  class Region;

  class PixelBuffer : public ImageGetter {
  public:
    PixelBuffer(const PixelFormat& pf, int width, int height, ColourMap* cm);
    virtual ~PixelBuffer();

    ///////////////////////////////////////////////
    // Format / Layout
    //

    // Set/get pixel format & colourmap
  protected:
    // Only for subclasses that support changing parameters
    virtual void setPF(const PixelFormat &pf);
  public:
    virtual const PixelFormat &getPF() const;
    virtual ColourMap* getColourMap() const;

    // Get width, height and number of pixels
    int width()  const { return width_; }
    int height() const { return height_; }
    int area() const { return width_ * height_; }

    // Get rectangle encompassing this buffer
    //   Top-left of rectangle is either at (0,0), or the specified point.
    Rect getRect() const { return Rect(0, 0, width_, height_); }
    Rect getRect(const Point& pos) const {
      return Rect(pos, pos.translate(Point(width_, height_)));
    }

    ///////////////////////////////////////////////
    // Access to pixel data
    //

    // Get a pointer into the buffer
    //   The pointer is to the top-left pixel of the specified Rect.
    //   The buffer stride (in pixels) is returned.
    virtual const rdr::U8* getPixelsR(const Rect& r, int* stride) = 0;
    virtual rdr::U8* getPixelsRW(const Rect& r, int* stride) = 0;

    // Get pixel data for a given part of the buffer
    //   Data is copied into the supplied buffer, with the specified
    //   stride.
    virtual void getImage(void* imageBuf, const Rect& r, int stride=0);

    ///////////////////////////////////////////////
    // Framebuffer update methods
    //

    // Ensure that the specified rectangle of buffer is up to date.
    //   Overridden by derived classes implementing framebuffer access
    //   to copy the required display data into place.
    virtual void grabRegion(const Region& region) {}

  protected:
    PixelBuffer();
    PixelFormat format;
    int width_, height_;
    ColourMap* colourmap;
  };

  // FullFramePixelBuffer

  class FullFramePixelBuffer : public PixelBuffer {
  public:
    FullFramePixelBuffer(const PixelFormat& pf, int width, int height,
                         rdr::U8* data_, ColourMap* cm);
    virtual ~FullFramePixelBuffer();

  protected:
    virtual void setPF(const PixelFormat &pf);

  public:
    // - Get the number of pixels per row in the actual pixel buffer data area
    //   This may in some cases NOT be the same as width().
    virtual int getStride() const;

    // Get a pointer to specified pixel data
    rdr::U8* getPixelsRW(const Rect& r, int* stride);
    virtual const rdr::U8* getPixelsR(const Rect& r, int* stride) {
      return getPixelsRW(r, stride);
    }

    ///////////////////////////////////////////////
    // Basic rendering operations
    // These operations DO NOT clip to the pixelbuffer area, or trap overruns.

    // Fill a rectangle
    virtual void fillRect(const Rect &dest, Pixel pix);

    // Copy pixel data to the buffer
    virtual void imageRect(const Rect &dest, const void* pixels, int stride=0);

    // Copy pixel data from one PixelBuffer location to another
    virtual void copyRect(const Rect &dest, const Point &move_by_delta);

    // Copy pixel data to the buffer through a mask
    //   pixels is a pointer to the pixel to be copied to r.tl.
    //   maskPos specifies the pixel offset in the mask to start from.
    //   mask_ is a pointer to the mask bits at (0,0).
    //   pStride and mStride are the strides of the pixel and mask buffers.
    virtual void maskRect(const Rect& r, const void* pixels, const void* mask_);

    //   pixel is the Pixel value to be used where mask_ is set
    virtual void maskRect(const Rect& r, Pixel pixel, const void* mask_);

    // *** Should this be visible?
    rdr::U8* data;

  protected:
    FullFramePixelBuffer();
    void (*fillRectFn)(rdr::U8 *, int, const Rect&, Pixel);
  };

  // -=- Managed pixel buffer class
  // Automatically allocates enough space for the specified format & area

  class ManagedPixelBuffer : public FullFramePixelBuffer {
  public:
    ManagedPixelBuffer();
    ManagedPixelBuffer(const PixelFormat& pf, int width, int height);
    virtual ~ManagedPixelBuffer();

    // Manage the pixel buffer layout
    virtual void setPF(const PixelFormat &pf);
    virtual void setSize(int w, int h);

    // Assign a colour map to the buffer
    virtual void setColourMap(ColourMap* cm, bool own_cm);

    // Return the total number of bytes of pixel data in the buffer
    int dataLen() const { return width_ * height_ * (format.bpp/8); }

  protected:
    unsigned long datasize;
    bool own_colourmap;
    void checkDataSize();
  };

};

#endif // __RFB_PIXEL_BUFFER_H__