1 /* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
2  * Copyright 2016-2019 Brian P. Hinz
3  *
4  * This is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This software is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this software; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17  * USA.
18  */
19 
20 // -=- Generic pixel buffer class
21 
22 package com.tigervnc.rfb;
23 
24 import java.awt.*;
25 import java.awt.image.*;
26 import java.awt.Color;
27 import java.nio.*;
28 import java.util.concurrent.atomic.*;
29 
30 public abstract class PixelBuffer {
31 
32   // We do a lot of byte offset calculations that assume the result fits
33   // inside a signed 32 bit integer. Limit the maximum size of pixel
34   // buffers so that these calculations never overflow.
35 
36   static final int maxPixelBufferWidth = 16384;
37   static final int maxPixelBufferHeight = 16384;
38 
PixelBuffer(PixelFormat pf, int w, int h)39   public PixelBuffer(PixelFormat pf, int w, int h) {
40     format = pf;
41     width_ = w;
42     height_= h;
43     setSize(w, h);
44   }
45 
PixelBuffer()46   protected PixelBuffer() { width_ = 0; height_ = 0; }
47 
48   // Get pixel format
getPF()49   public final PixelFormat getPF() { return format; }
50 
51   // Get width, height and number of pixels
width()52   public final int width() { return width_; }
height()53   public final int height() { return height_; }
area()54   public final int area() { return width_ * height_; }
55 
56   // Get rectangle encompassing this buffer
57   //   Top-left of rectangle is either at (0,0), or the specified point.
getRect()58   public final Rect getRect() { return new Rect(0, 0, width_, height_); }
getRect(Point pos)59   public final Rect getRect(Point pos) {
60     return new Rect(pos, pos.translate(new Point(width_, height_)));
61   }
62 
63   ///////////////////////////////////////////////
64   // Access to pixel data
65   //
66 
67   // Get a pointer into the buffer
68   //   The pointer is to the top-left pixel of the specified Rect.
getBuffer(Rect r)69   public abstract Raster getBuffer(Rect r);
70 
71   // Get pixel data for a given part of the buffer
72   //   Data is copied into the supplied buffer, with the specified
73   //   stride. Try to avoid using this though as getBuffer() will in
74   //   most cases avoid the extra memory copy.
75   //void getImage(void* imageBuf, const Rect& r, int stride=0) const;
76   // Get pixel data in a given format
77   //   Works just the same as getImage(), but guaranteed to be in a
78   //   specific format.
79   //void getImage(const PixelFormat& pf, void* imageBuf,
80   //                const Rect& r, int stride=0) const;
getImage()81   public Image getImage() { return image; }
82 
setSize(int width, int height)83   public void setSize(int width, int height)
84   {
85     if ((width < 0) || (width > maxPixelBufferWidth))
86       throw new Exception("Invalid PixelBuffer width of "+width+" pixels requested");
87     if ((height < 0) || (height > maxPixelBufferHeight))
88       throw new Exception("Invalid PixelBuffer height of "+height+" pixels requested");
89 
90     width_ = width;
91     height_ = height;
92   }
93 
94   ///////////////////////////////////////////////
95   // Framebuffer update methods
96   //
97 
98   // Ensure that the specified rectangle of buffer is up to date.
99   //   Overridden by derived classes implementing framebuffer access
100   //   to copy the required display data into place.
101   //public abstract void grabRegion(Region& region) {}
102 
103   protected PixelFormat format;
104   protected int width_, height_;
105   protected Image image;
106 }
107