enikey | 00e0dbf | 2008-12-08 10:59:59 +0000 | [diff] [blame] | 1 | package com.tightvnc.decoder; |
| 2 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 3 | import com.tightvnc.vncviewer.RecordInterface; |
| 4 | import com.tightvnc.vncviewer.RfbInputStream; |
| 5 | import java.awt.Graphics; |
| 6 | import java.awt.Image; |
| 7 | import java.awt.image.ColorModel; |
| 8 | import java.awt.image.DirectColorModel; |
| 9 | import java.awt.image.MemoryImageSource; |
| 10 | import java.awt.Color; |
enikey | 663025d | 2008-12-19 05:14:15 +0000 | [diff] [blame^] | 11 | import java.awt.Toolkit; |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 12 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 13 | // |
| 14 | // This is base decoder class. |
| 15 | // Other classes will be childs of RawDecoder. |
| 16 | // |
enikey | 00e0dbf | 2008-12-08 10:59:59 +0000 | [diff] [blame] | 17 | public class RawDecoder { |
| 18 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 19 | public RawDecoder(Graphics g, RfbInputStream is) { |
| 20 | setGraphics(g); |
| 21 | setRfbInputStream(is); |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 22 | // FIXME: cm24 created in getColorModel24. |
| 23 | // Remove if no bugs |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 24 | 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); |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 32 | // FIXME: cm24 created in getColorModel24. |
| 33 | // Remove if no bugs |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 34 | 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 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 49 | // |
| 50 | // FIXME: This method may be useless in future, remove if so |
| 51 | // |
| 52 | |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 53 | public int getBPP() { |
| 54 | return bytesPerPixel; |
| 55 | } |
| 56 | |
| 57 | public void setFrameBufferSize(int w, int h) { |
| 58 | framebufferWidth = w; |
| 59 | framebufferHeight = h; |
| 60 | } |
| 61 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 62 | // |
enikey | 663025d | 2008-12-19 05:14:15 +0000 | [diff] [blame^] | 63 | // 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 | // |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 90 | // 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 | // |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 119 | 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 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 126 | // |
| 127 | // This data must be shared between decoders |
| 128 | // |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 129 | protected static byte []pixels8 = null; |
| 130 | protected static int []pixels24 = null; |
| 131 | protected static MemoryImageSource pixelsSource = null; |
| 132 | protected static Image rawPixelsImage = null; |
| 133 | |
enikey | b3e19f7 | 2008-12-19 04:54:49 +0000 | [diff] [blame] | 134 | // |
| 135 | // Access to this static members only though protected methods |
| 136 | // |
enikey | 2f811b8 | 2008-12-19 04:45:15 +0000 | [diff] [blame] | 137 | private static ColorModel cm8 = null; |
| 138 | private static ColorModel cm24 = null; |
| 139 | private static Color []color256 = null; |
enikey | 00e0dbf | 2008-12-08 10:59:59 +0000 | [diff] [blame] | 140 | } |