Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JavaPixelBuffer.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2012-2016 Brian P. Hinz
  2. * Copyright (C) 2012 D. R. Commander. All Rights Reserved.
  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. package com.tigervnc.vncviewer;
  20. import java.awt.*;
  21. import java.awt.image.*;
  22. import java.nio.*;
  23. import com.tigervnc.rfb.*;
  24. import com.tigervnc.rfb.Point;
  25. public class JavaPixelBuffer extends PlatformPixelBuffer
  26. {
  27. public JavaPixelBuffer(int w, int h) {
  28. super(getPreferredPF(), w, h,
  29. getPreferredPF().getColorModel().createCompatibleWritableRaster(w,h));
  30. ColorModel cm = format.getColorModel();
  31. image = new BufferedImage(cm, data, cm.isAlphaPremultiplied(), null);
  32. image.setAccelerationPriority(1);
  33. }
  34. public WritableRaster getBufferRW(Rect r)
  35. {
  36. synchronized(image) {
  37. return ((BufferedImage)image)
  38. .getSubimage(r.tl.x, r.tl.y, r.width(), r.height()).getRaster();
  39. }
  40. }
  41. public Raster getBuffer(Rect r)
  42. {
  43. Rectangle rect =
  44. new Rectangle(r.tl.x, r.tl.y, r.width(), r.height());
  45. synchronized(image) {
  46. return ((BufferedImage)image).getData(rect);
  47. }
  48. }
  49. public void fillRect(Rect r, byte[] pix)
  50. {
  51. ColorModel cm = format.getColorModel();
  52. int pixel =
  53. ByteBuffer.wrap(pix).order(format.getByteOrder()).asIntBuffer().get(0);
  54. Color c = new Color(cm.getRGB(pixel));
  55. synchronized(image) {
  56. Graphics2D g2 = (Graphics2D)image.getGraphics();
  57. g2.setColor(c);
  58. g2.fillRect(r.tl.x, r.tl.y, r.width(), r.height());
  59. g2.dispose();
  60. }
  61. commitBufferRW(r);
  62. }
  63. public void copyRect(Rect rect, Point move_by_delta)
  64. {
  65. synchronized(image) {
  66. Graphics2D g2 = (Graphics2D)image.getGraphics();
  67. g2.copyArea(rect.tl.x - move_by_delta.x,
  68. rect.tl.y - move_by_delta.y,
  69. rect.width(), rect.height(),
  70. move_by_delta.x, move_by_delta.y);
  71. g2.dispose();
  72. }
  73. commitBufferRW(rect);
  74. }
  75. private static PixelFormat getPreferredPF()
  76. {
  77. GraphicsEnvironment ge =
  78. GraphicsEnvironment.getLocalGraphicsEnvironment();
  79. GraphicsDevice gd = ge.getDefaultScreenDevice();
  80. GraphicsConfiguration gc = gd.getDefaultConfiguration();
  81. ColorModel cm = gc.getColorModel();
  82. int depth = ((cm.getPixelSize() > 24) ? 24 : cm.getPixelSize());
  83. int bpp = (depth > 16 ? 32 : (depth > 8 ? 16 : 8));
  84. ByteOrder byteOrder = ByteOrder.nativeOrder();
  85. boolean bigEndian = (byteOrder == ByteOrder.BIG_ENDIAN ? true : false);
  86. boolean trueColour = true;
  87. int redShift = cm.getComponentSize()[0] + cm.getComponentSize()[1];
  88. int greenShift = cm.getComponentSize()[0];
  89. int blueShift = 0;
  90. int redMask = ((int)Math.pow(2, cm.getComponentSize()[2]) - 1);
  91. int greenMask = ((int)Math.pow(2, cm.getComponentSize()[1]) - 1);
  92. int blueMmask = ((int)Math.pow(2, cm.getComponentSize()[0]) - 1);
  93. return new PixelFormat(bpp, depth, bigEndian, trueColour,
  94. redMask, greenMask, blueMmask,
  95. redShift, greenShift, blueShift);
  96. }
  97. }