blob: 9025e9ef007a133de2e7fa9ea513cce5806b2aa3 [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;
enikey4f92da42008-12-19 05:28:12 +00005import java.io.IOException;
enikeyc6f6bab2008-12-24 03:41:00 +00006import java.io.DataOutput;
enikey2f811b82008-12-19 04:45:15 +00007import java.awt.Graphics;
8import java.awt.Image;
9import java.awt.image.ColorModel;
10import java.awt.image.DirectColorModel;
11import java.awt.image.MemoryImageSource;
12import java.awt.Color;
enikey663025d2008-12-19 05:14:15 +000013import java.awt.Toolkit;
enikey2f811b82008-12-19 04:45:15 +000014
enikeyb3e19f72008-12-19 04:54:49 +000015//
16// This is base decoder class.
17// Other classes will be childs of RawDecoder.
18//
enikeyccf6e7c2008-12-19 06:00:18 +000019
enikey00e0dbf2008-12-08 10:59:59 +000020public class RawDecoder {
enikey6558a302008-12-24 05:14:30 +000021 final static int EncodingRaw = 0;
enikey00e0dbf2008-12-08 10:59:59 +000022
enikey2f811b82008-12-19 04:45:15 +000023 public RawDecoder(Graphics g, RfbInputStream is) {
24 setGraphics(g);
25 setRfbInputStream(is);
enikeyb3e19f72008-12-19 04:54:49 +000026 // FIXME: cm24 created in getColorModel24.
27 // Remove if no bugs
enikey2f811b82008-12-19 04:45:15 +000028 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
29 }
30
31 public RawDecoder(Graphics g, RfbInputStream is, int frameBufferW,
32 int frameBufferH) {
33 setGraphics(g);
34 setRfbInputStream(is);
35 setFrameBufferSize(frameBufferW, frameBufferH);
enikeyb3e19f72008-12-19 04:54:49 +000036 // FIXME: cm24 created in getColorModel24.
37 // Remove if no bugs
enikey2f811b82008-12-19 04:45:15 +000038 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
39 }
40
enikey2c23ba12008-12-19 05:34:17 +000041 //
42 // Set methods to set value of non-static protected members of class
43 //
44
enikey2f811b82008-12-19 04:45:15 +000045 public void setRfbInputStream(RfbInputStream is) {
46 rfbis = is;
47 }
48
49 public void setGraphics(Graphics g) {
50 graphics = g;
51 }
52
53 public void setBPP(int bpp) {
54 bytesPerPixel = bpp;
55 }
56
enikey2c23ba12008-12-19 05:34:17 +000057 public void setFrameBufferSize(int w, int h) {
58 framebufferWidth = w;
59 framebufferHeight = h;
60 }
61
enikey292cf9d2008-12-19 05:30:11 +000062 public void setSessionRecorder(RecordInterface ri) {
63 rec = ri;
64 }
65
enikeyb3e19f72008-12-19 04:54:49 +000066 //
enikey3e9ff9f2008-12-24 03:30:29 +000067 // FIXME: Rename this method after we don't need RecordInterface
68 // in RawDecoder class to record session
69 //
70
enikeyc6f6bab2008-12-24 03:41:00 +000071 public void setDataOutputStream(DataOutput os) {
enikey3e9ff9f2008-12-24 03:30:29 +000072 dos = os;
73 }
74
75 //
enikeyb3e19f72008-12-19 04:54:49 +000076 // FIXME: This method may be useless in future, remove if so
77 //
78
enikey2f811b82008-12-19 04:45:15 +000079 public int getBPP() {
80 return bytesPerPixel;
81 }
82
enikeyb3e19f72008-12-19 04:54:49 +000083 //
enikey4f92da42008-12-19 05:28:12 +000084 // Decodes Raw Pixels data and draw it into graphics
85 //
86
87 public void handleRect(int x, int y, int w, int h) throws IOException, Exception {
88 if (bytesPerPixel == 1) {
89 for (int dy = y; dy < y + h; dy++) {
90 if (pixels8 != null) {
91 rfbis.readFully(pixels8, dy * framebufferWidth + x, w);
92 }
enikey2c23ba12008-12-19 05:34:17 +000093 //
enikey5276d792008-12-24 04:12:34 +000094 // Save decoded data to record output stream
enikey2c23ba12008-12-19 05:34:17 +000095 //
enikey5276d792008-12-24 04:12:34 +000096 if (dos != null) {
97 dos.write(pixels8, dy * framebufferWidth + x, w);
enikey4f92da42008-12-19 05:28:12 +000098 }
99 }
100 } else {
101 byte[] buf = new byte[w * 4];
102 int i, offset;
103 for (int dy = y; dy < y + h; dy++) {
104 rfbis.readFully(buf);
105 //
enikey5276d792008-12-24 04:12:34 +0000106 // Save decoded data to record output stream
enikey4f92da42008-12-19 05:28:12 +0000107 //
enikey5276d792008-12-24 04:12:34 +0000108 if (dos != null) {
109 dos.write(buf);
enikey4f92da42008-12-19 05:28:12 +0000110 }
111 offset = dy * framebufferWidth + x;
112 if (pixels24 != null) {
113 for (i = 0; i < w; i++) {
114 pixels24[offset + i] =
115 (buf[i * 4 + 2] & 0xFF) << 16 |
116 (buf[i * 4 + 1] & 0xFF) << 8 |
117 (buf[i * 4] & 0xFF);
118 } //for
119 } // if
120 } // for
121 } // else
122 handleUpdatedPixels(x, y, w, h);
123 } // void
enikey3e9ff9f2008-12-24 03:30:29 +0000124
enikey4f92da42008-12-19 05:28:12 +0000125 //
126 // Display newly updated area of pixels.
127 //
enikey2c23ba12008-12-19 05:34:17 +0000128
enikey4f92da42008-12-19 05:28:12 +0000129 protected void handleUpdatedPixels(int x, int y, int w, int h) {
130 // Draw updated pixels of the off-screen image.
131 pixelsSource.newPixels(x, y, w, h);
132 graphics.setClip(x, y, w, h);
133 graphics.drawImage(rawPixelsImage, 0, 0, null);
134 graphics.setClip(0, 0, framebufferWidth, framebufferHeight);
135 }
136
137 //
enikey663025d2008-12-19 05:14:15 +0000138 // Updates pixels data.
enikey4f92da42008-12-19 05:28:12 +0000139 // This method must be called when framebuffer is resized
enikey663025d2008-12-19 05:14:15 +0000140 // or BPP is changed.
141 //
142
143 public void update() {
144 // Images with raw pixels should be re-allocated on every change
145 // of geometry or pixel format.
146 int fbWidth = framebufferWidth;
147 int fbHeight = framebufferHeight;
148
149 if (bytesPerPixel == 1) {
150 pixels24 = null;
151 pixels8 = new byte[fbWidth * fbHeight];
152 pixelsSource = new MemoryImageSource(fbWidth, fbHeight, getColorModel8(),
153 pixels8, 0, fbWidth);
154 } else {
155 pixels8 = null;
156 pixels24 = new int[fbWidth * fbHeight];
157 pixelsSource =
158 new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth);
159 }
160 pixelsSource.setAnimated(true);
161 rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource);
162 }
163
164 //
enikey1924c442008-12-19 05:36:36 +0000165 // Private static members access methods
enikeyb3e19f72008-12-19 04:54:49 +0000166 //
167
168 protected ColorModel getColorModel8() {
169 if (cm8 == null) {
170 cm8 = cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6));
171 }
172 return cm8;
173 }
174
175 protected ColorModel getColorModel24() {
176 if (cm24 == null) {
177 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
178 }
179 return cm24;
180 }
181
182 protected Color[]getColor256() {
183 if (color256 == null) {
184 color256 = new Color[256];
185 for (int i = 0; i < 256; i++)
186 color256[i] = new Color(cm8.getRGB(i));
187 }
188 return color256;
189 }
190
191 //
192 // Unique data for every decoder (? maybe not ?)
193 //
enikey2c23ba12008-12-19 05:34:17 +0000194
enikey2f811b82008-12-19 04:45:15 +0000195 protected int bytesPerPixel = 4;
196 protected int framebufferWidth = 0;
197 protected int framebufferHeight = 0;
198 protected RfbInputStream rfbis = null;
199 protected Graphics graphics = null;
200 protected RecordInterface rec = null;
enikeyc6f6bab2008-12-24 03:41:00 +0000201 protected DataOutput dos = null;
enikey2f811b82008-12-19 04:45:15 +0000202
enikeyb3e19f72008-12-19 04:54:49 +0000203 //
204 // This data must be shared between decoders
205 //
enikey2c23ba12008-12-19 05:34:17 +0000206
enikey2f811b82008-12-19 04:45:15 +0000207 protected static byte []pixels8 = null;
208 protected static int []pixels24 = null;
209 protected static MemoryImageSource pixelsSource = null;
210 protected static Image rawPixelsImage = null;
211
enikeyb3e19f72008-12-19 04:54:49 +0000212 //
213 // Access to this static members only though protected methods
214 //
enikey2c23ba12008-12-19 05:34:17 +0000215
enikey2f811b82008-12-19 04:45:15 +0000216 private static ColorModel cm8 = null;
217 private static ColorModel cm24 = null;
218 private static Color []color256 = null;
enikey00e0dbf2008-12-08 10:59:59 +0000219}