blob: 507ed8164937103abf590927882f125176180a81 [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;
enikey2f811b82008-12-19 04:45:15 +00006import java.awt.Graphics;
7import java.awt.Image;
8import java.awt.image.ColorModel;
9import java.awt.image.DirectColorModel;
10import java.awt.image.MemoryImageSource;
11import java.awt.Color;
enikey663025d2008-12-19 05:14:15 +000012import java.awt.Toolkit;
enikey2f811b82008-12-19 04:45:15 +000013
enikeyb3e19f72008-12-19 04:54:49 +000014//
15// This is base decoder class.
16// Other classes will be childs of RawDecoder.
17//
enikey00e0dbf2008-12-08 10:59:59 +000018public class RawDecoder {
19
enikey2f811b82008-12-19 04:45:15 +000020 public RawDecoder(Graphics g, RfbInputStream is) {
21 setGraphics(g);
22 setRfbInputStream(is);
enikeyb3e19f72008-12-19 04:54:49 +000023 // FIXME: cm24 created in getColorModel24.
24 // Remove if no bugs
enikey2f811b82008-12-19 04:45:15 +000025 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
26 }
27
28 public RawDecoder(Graphics g, RfbInputStream is, int frameBufferW,
29 int frameBufferH) {
30 setGraphics(g);
31 setRfbInputStream(is);
32 setFrameBufferSize(frameBufferW, frameBufferH);
enikeyb3e19f72008-12-19 04:54:49 +000033 // FIXME: cm24 created in getColorModel24.
34 // Remove if no bugs
enikey2f811b82008-12-19 04:45:15 +000035 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
36 }
37
38 public void setRfbInputStream(RfbInputStream is) {
39 rfbis = is;
40 }
41
42 public void setGraphics(Graphics g) {
43 graphics = g;
44 }
45
46 public void setBPP(int bpp) {
47 bytesPerPixel = bpp;
48 }
49
enikeyb3e19f72008-12-19 04:54:49 +000050 //
51 // FIXME: This method may be useless in future, remove if so
52 //
53
enikey2f811b82008-12-19 04:45:15 +000054 public int getBPP() {
55 return bytesPerPixel;
56 }
57
58 public void setFrameBufferSize(int w, int h) {
59 framebufferWidth = w;
60 framebufferHeight = h;
61 }
62
enikeyb3e19f72008-12-19 04:54:49 +000063 //
enikey4f92da42008-12-19 05:28:12 +000064 // Decodes Raw Pixels data and draw it into graphics
65 //
66
67 public void handleRect(int x, int y, int w, int h) throws IOException, Exception {
68 if (bytesPerPixel == 1) {
69 for (int dy = y; dy < y + h; dy++) {
70 if (pixels8 != null) {
71 rfbis.readFully(pixels8, dy * framebufferWidth + x, w);
72 }
73 if (rec.canWrite()) {
74 rec.write(pixels8, dy * framebufferWidth + x, w);
75 }
76 }
77 } else {
78 byte[] buf = new byte[w * 4];
79 int i, offset;
80 for (int dy = y; dy < y + h; dy++) {
81 rfbis.readFully(buf);
82 //
83 // Save decoded data to RecordInterface
84 //
85 if (rec.canWrite()) {
86 rec.write(buf);
87 }
88 offset = dy * framebufferWidth + x;
89 if (pixels24 != null) {
90 for (i = 0; i < w; i++) {
91 pixels24[offset + i] =
92 (buf[i * 4 + 2] & 0xFF) << 16 |
93 (buf[i * 4 + 1] & 0xFF) << 8 |
94 (buf[i * 4] & 0xFF);
95 } //for
96 } // if
97 } // for
98 } // else
99 handleUpdatedPixels(x, y, w, h);
100 } // void
101
102 //
103 // Display newly updated area of pixels.
104 //
105 protected void handleUpdatedPixels(int x, int y, int w, int h) {
106 // Draw updated pixels of the off-screen image.
107 pixelsSource.newPixels(x, y, w, h);
108 graphics.setClip(x, y, w, h);
109 graphics.drawImage(rawPixelsImage, 0, 0, null);
110 graphics.setClip(0, 0, framebufferWidth, framebufferHeight);
111 }
112
113 //
enikey663025d2008-12-19 05:14:15 +0000114 // Updates pixels data.
enikey4f92da42008-12-19 05:28:12 +0000115 // This method must be called when framebuffer is resized
enikey663025d2008-12-19 05:14:15 +0000116 // or BPP is changed.
117 //
118
119 public void update() {
120 // Images with raw pixels should be re-allocated on every change
121 // of geometry or pixel format.
122 int fbWidth = framebufferWidth;
123 int fbHeight = framebufferHeight;
124
125 if (bytesPerPixel == 1) {
126 pixels24 = null;
127 pixels8 = new byte[fbWidth * fbHeight];
128 pixelsSource = new MemoryImageSource(fbWidth, fbHeight, getColorModel8(),
129 pixels8, 0, fbWidth);
130 } else {
131 pixels8 = null;
132 pixels24 = new int[fbWidth * fbHeight];
133 pixelsSource =
134 new MemoryImageSource(fbWidth, fbHeight, cm24, pixels24, 0, fbWidth);
135 }
136 pixelsSource.setAnimated(true);
137 rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource);
138 }
139
140 //
enikeyb3e19f72008-12-19 04:54:49 +0000141 // Private static members access methdos
142 //
143
144 protected ColorModel getColorModel8() {
145 if (cm8 == null) {
146 cm8 = cm8 = new DirectColorModel(8, 7, (7 << 3), (3 << 6));
147 }
148 return cm8;
149 }
150
151 protected ColorModel getColorModel24() {
152 if (cm24 == null) {
153 cm24 = new DirectColorModel(24, 0xFF0000, 0x00FF00, 0x0000FF);
154 }
155 return cm24;
156 }
157
158 protected Color[]getColor256() {
159 if (color256 == null) {
160 color256 = new Color[256];
161 for (int i = 0; i < 256; i++)
162 color256[i] = new Color(cm8.getRGB(i));
163 }
164 return color256;
165 }
166
167 //
168 // Unique data for every decoder (? maybe not ?)
169 //
enikey2f811b82008-12-19 04:45:15 +0000170 protected int bytesPerPixel = 4;
171 protected int framebufferWidth = 0;
172 protected int framebufferHeight = 0;
173 protected RfbInputStream rfbis = null;
174 protected Graphics graphics = null;
175 protected RecordInterface rec = null;
176
enikeyb3e19f72008-12-19 04:54:49 +0000177 //
178 // This data must be shared between decoders
179 //
enikey2f811b82008-12-19 04:45:15 +0000180 protected static byte []pixels8 = null;
181 protected static int []pixels24 = null;
182 protected static MemoryImageSource pixelsSource = null;
183 protected static Image rawPixelsImage = null;
184
enikeyb3e19f72008-12-19 04:54:49 +0000185 //
186 // Access to this static members only though protected methods
187 //
enikey2f811b82008-12-19 04:45:15 +0000188 private static ColorModel cm8 = null;
189 private static ColorModel cm24 = null;
190 private static Color []color256 = null;
enikey00e0dbf2008-12-08 10:59:59 +0000191}