blob: c1b2ca1ebc65c6bba475c3b5637d16071ad8eb9e [file] [log] [blame]
enikey00e0dbf2008-12-08 10:59:59 +00001package com.tightvnc.decoder;
2
enikey2f811b82008-12-19 04:45:15 +00003import com.tightvnc.vncviewer.RecordInterface;
4import com.tightvnc.vncviewer.RfbInputStream;
5import java.awt.Graphics;
6import java.awt.Image;
7import java.awt.image.ColorModel;
8import java.awt.image.DirectColorModel;
9import java.awt.image.MemoryImageSource;
10import java.awt.Color;
enikey663025d2008-12-19 05:14:15 +000011import java.awt.Toolkit;
enikey2f811b82008-12-19 04:45:15 +000012
enikeyb3e19f72008-12-19 04:54:49 +000013//
14// This is base decoder class.
15// Other classes will be childs of RawDecoder.
16//
enikey00e0dbf2008-12-08 10:59:59 +000017public class RawDecoder {
18
enikey2f811b82008-12-19 04:45:15 +000019 public RawDecoder(Graphics g, RfbInputStream is) {
20 setGraphics(g);
21 setRfbInputStream(is);
enikeyb3e19f72008-12-19 04:54:49 +000022 // FIXME: cm24 created in getColorModel24.
23 // Remove if no bugs
enikey2f811b82008-12-19 04:45:15 +000024 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
25 }
26
27 public RawDecoder(Graphics g, RfbInputStream is, int frameBufferW,
28 int frameBufferH) {
29 setGraphics(g);
30 setRfbInputStream(is);
31 setFrameBufferSize(frameBufferW, frameBufferH);
enikeyb3e19f72008-12-19 04:54:49 +000032 // FIXME: cm24 created in getColorModel24.
33 // Remove if no bugs
enikey2f811b82008-12-19 04:45:15 +000034 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
35 }
36
37 public void setRfbInputStream(RfbInputStream is) {
38 rfbis = is;
39 }
40
41 public void setGraphics(Graphics g) {
42 graphics = g;
43 }
44
45 public void setBPP(int bpp) {
46 bytesPerPixel = bpp;
47 }
48
enikeyb3e19f72008-12-19 04:54:49 +000049 //
50 // FIXME: This method may be useless in future, remove if so
51 //
52
enikey2f811b82008-12-19 04:45:15 +000053 public int getBPP() {
54 return bytesPerPixel;
55 }
56
57 public void setFrameBufferSize(int w, int h) {
58 framebufferWidth = w;
59 framebufferHeight = h;
60 }
61
enikeyb3e19f72008-12-19 04:54:49 +000062 //
enikey663025d2008-12-19 05:14:15 +000063 // Updates pixels data.
64 // This methods must be called when framebuffer is resized
65 // or BPP is changed.
66 //
67
68 public void update() {
69 // Images with raw pixels should be re-allocated on every change
70 // of geometry or pixel format.
71 int fbWidth = framebufferWidth;
72 int fbHeight = framebufferHeight;
73
74 if (bytesPerPixel == 1) {
75 pixels24 = null;
76 pixels8 = new byte[fbWidth * fbHeight];
77 pixelsSource = new MemoryImageSource(fbWidth, fbHeight, getColorModel8(),
78 pixels8, 0, fbWidth);
79 } else {
80 pixels8 = null;
81 pixels24 = new int[fbWidth * fbHeight];
82 pixelsSource =
83 new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth);
84 }
85 pixelsSource.setAnimated(true);
86 rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource);
87 }
88
89 //
enikeyb3e19f72008-12-19 04:54:49 +000090 // Private static members access methdos
91 //
92
93 protected ColorModel getColorModel8() {
94 if (cm8 == null) {
95 cm8 = cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6));
96 }
97 return cm8;
98 }
99
100 protected ColorModel getColorModel24() {
101 if (cm24 == null) {
102 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
103 }
104 return cm24;
105 }
106
107 protected Color[]getColor256() {
108 if (color256 == null) {
109 color256 = new Color[256];
110 for (int i = 0; i < 256; i++)
111 color256[i] = new Color(cm8.getRGB(i));
112 }
113 return color256;
114 }
115
116 //
117 // Unique data for every decoder (? maybe not ?)
118 //
enikey2f811b82008-12-19 04:45:15 +0000119 protected int bytesPerPixel = 4;
120 protected int framebufferWidth = 0;
121 protected int framebufferHeight = 0;
122 protected RfbInputStream rfbis = null;
123 protected Graphics graphics = null;
124 protected RecordInterface rec = null;
125
enikeyb3e19f72008-12-19 04:54:49 +0000126 //
127 // This data must be shared between decoders
128 //
enikey2f811b82008-12-19 04:45:15 +0000129 protected static byte []pixels8 = null;
130 protected static int []pixels24 = null;
131 protected static MemoryImageSource pixelsSource = null;
132 protected static Image rawPixelsImage = null;
133
enikeyb3e19f72008-12-19 04:54:49 +0000134 //
135 // Access to this static members only though protected methods
136 //
enikey2f811b82008-12-19 04:45:15 +0000137 private static ColorModel cm8 = null;
138 private static ColorModel cm24 = null;
139 private static Color []color256 = null;
enikey00e0dbf2008-12-08 10:59:59 +0000140}