You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PixelBuffer.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // -=- Generic pixel buffer class
  20. package com.tigervnc.rfb;
  21. import java.awt.*;
  22. import java.awt.image.*;
  23. import java.awt.Color;
  24. import java.nio.*;
  25. import java.util.concurrent.atomic.*;
  26. public abstract class PixelBuffer {
  27. // We do a lot of byte offset calculations that assume the result fits
  28. // inside a signed 32 bit integer. Limit the maximum size of pixel
  29. // buffers so that these calculations never overflow.
  30. static final int maxPixelBufferWidth = 16384;
  31. static final int maxPixelBufferHeight = 16384;
  32. public PixelBuffer(PixelFormat pf, int w, int h) {
  33. format = pf;
  34. width_ = w;
  35. height_= h;
  36. setSize(w, h);
  37. }
  38. protected PixelBuffer() { width_ = 0; height_ = 0; }
  39. // Get pixel format
  40. public final PixelFormat getPF() { return format; }
  41. // Get width, height and number of pixels
  42. public final int width() { return width_; }
  43. public final int height() { return height_; }
  44. public final int area() { return width_ * height_; }
  45. // Get rectangle encompassing this buffer
  46. // Top-left of rectangle is either at (0,0), or the specified point.
  47. public final Rect getRect() { return new Rect(0, 0, width_, height_); }
  48. public final Rect getRect(Point pos) {
  49. return new Rect(pos, pos.translate(new Point(width_, height_)));
  50. }
  51. ///////////////////////////////////////////////
  52. // Access to pixel data
  53. //
  54. // Get a pointer into the buffer
  55. // The pointer is to the top-left pixel of the specified Rect.
  56. public abstract Raster getBuffer(Rect r);
  57. // Get pixel data for a given part of the buffer
  58. // Data is copied into the supplied buffer, with the specified
  59. // stride. Try to avoid using this though as getBuffer() will in
  60. // most cases avoid the extra memory copy.
  61. //void getImage(void* imageBuf, const Rect& r, int stride=0) const;
  62. // Get pixel data in a given format
  63. // Works just the same as getImage(), but guaranteed to be in a
  64. // specific format.
  65. //void getImage(const PixelFormat& pf, void* imageBuf,
  66. // const Rect& r, int stride=0) const;
  67. public Image getImage() { return image; }
  68. public void setSize(int width, int height)
  69. {
  70. if ((width < 0) || (width > maxPixelBufferWidth))
  71. throw new Exception("Invalid PixelBuffer width of "+width+" pixels requested");
  72. if ((height < 0) || (height > maxPixelBufferHeight))
  73. throw new Exception("Invalid PixelBuffer height of "+height+" pixels requested");
  74. width_ = width;
  75. height_ = height;
  76. }
  77. ///////////////////////////////////////////////
  78. // Framebuffer update methods
  79. //
  80. // Ensure that the specified rectangle of buffer is up to date.
  81. // Overridden by derived classes implementing framebuffer access
  82. // to copy the required display data into place.
  83. //public abstract void grabRegion(Region& region) {}
  84. protected PixelFormat format;
  85. protected int width_, height_;
  86. protected Image image;
  87. }