aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/tigervnc/vncviewer/JavaPixelBuffer.java
blob: 016b8cf87860ea8e42feaf23aaed8ecb87bc714f (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
/* Copyright (C) 2012-2016 Brian P. Hinz
 * Copyright (C) 2012 D. R. Commander.  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA.
 */

package com.tigervnc.vncviewer;

import java.awt.*;
import java.awt.image.*;
import java.nio.*;

import com.tigervnc.rfb.*;
import com.tigervnc.rfb.Point;

public class JavaPixelBuffer extends PlatformPixelBuffer 
{

  public JavaPixelBuffer(int w, int h) {
    super(getPreferredPF(), w, h,
          getPreferredPF().getColorModel().createCompatibleWritableRaster(w,h));
    ColorModel cm = format.getColorModel();
    image = new BufferedImage(cm, data, cm.isAlphaPremultiplied(), null);
    image.setAccelerationPriority(1);
  }

  public WritableRaster getBufferRW(Rect r)
  {
    synchronized(image) {
      return ((BufferedImage)image)
        .getSubimage(r.tl.x, r.tl.y, r.width(), r.height()).getRaster();
    }
  }

  public Raster getBuffer(Rect r)
  {
    Rectangle rect =
      new Rectangle(r.tl.x, r.tl.y, r.width(), r.height());
    synchronized(image) {
      return ((BufferedImage)image).getData(rect);
    }
  }

  public void fillRect(Rect r, byte[] pix)
  {
    ColorModel cm = format.getColorModel();
    int pixel =
      ByteBuffer.wrap(pix).order(format.getByteOrder()).asIntBuffer().get(0);
    Color c = new Color(cm.getRGB(pixel));
    synchronized(image) {
      Graphics2D g2 = (Graphics2D)image.getGraphics();
      g2.setColor(c);
      g2.fillRect(r.tl.x, r.tl.y, r.width(), r.height());
      g2.dispose();
    }

    commitBufferRW(r);
  }

  public void copyRect(Rect rect, Point move_by_delta)
  {
    synchronized(image) {
      Graphics2D g2 = (Graphics2D)image.getGraphics();
      g2.copyArea(rect.tl.x - move_by_delta.x,
                  rect.tl.y - move_by_delta.y,
                  rect.width(), rect.height(),
                  move_by_delta.x, move_by_delta.y);
      g2.dispose();
    }

    commitBufferRW(rect);
  }

  private static PixelFormat getPreferredPF()
  {
    GraphicsEnvironment ge =
      GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();
    ColorModel cm = gc.getColorModel();
    int depth = ((cm.getPixelSize() > 24) ? 24 : cm.getPixelSize());
    int bpp = (depth > 16 ? 32 : (depth > 8 ? 16 : 8));
    ByteOrder byteOrder = ByteOrder.nativeOrder();
    boolean bigEndian = (byteOrder == ByteOrder.BIG_ENDIAN ? true : false);
    boolean trueColour = true;
    int redShift    = cm.getComponentSize()[0] + cm.getComponentSize()[1];
    int greenShift  = cm.getComponentSize()[0];
    int blueShift   = 0;
    int redMask   = ((int)Math.pow(2, cm.getComponentSize()[2]) - 1);
    int greenMask = ((int)Math.pow(2, cm.getComponentSize()[1]) - 1);
    int blueMmask = ((int)Math.pow(2, cm.getComponentSize()[0]) - 1);
    return new PixelFormat(bpp, depth, bigEndian, trueColour,
                           redMask, greenMask, blueMmask,
                           redShift, greenShift, blueShift);
  }

}