aboutsummaryrefslogtreecommitdiffstats
path: root/java/com/tigervnc/vncviewer/JavaPixelBuffer.java
blob: 16242d050bac033069e0b30e73c1f41f4ab37f3b (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
/* 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.*;

public class JavaPixelBuffer extends PlatformPixelBuffer 
{

  public JavaPixelBuffer(int w, int h) {
    super(getPreferredPF(), w, h,
          getPreferredPF().getColorModel().createCompatibleWritableRaster(w,h));
    image = new BufferedImage(getPreferredPF().getColorModel(),
                              getBufferRW(new Rect(0, 0, w, h)), true, null);
    image.setAccelerationPriority(1);
  }

  public synchronized 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));
    Graphics2D g2 = ((BufferedImage)image).createGraphics();
    g2.setColor(c);
    g2.fillRect(r.tl.x, r.tl.y, r.width(), r.height());
    g2.dispose();

    commitBufferRW(r);
  }

  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);
  }

}