comparison src/main/java/com/glavsoft/drawing/ColorDecoder.java @ 52:472a9bcacb21 draft default tip

TightVNC 2.7.1.0
author you@cr.ie.u-ryukyu.ac.jp
date Wed, 07 Aug 2013 19:01:17 +0900
parents 4689cc86d6cb
children
comparison
equal deleted inserted replaced
0:4689cc86d6cb 52:472a9bcacb21
1 // Copyright (C) 2010, 2011 GlavSoft LLC. 1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
2 // All rights reserved. 2 // All rights reserved.
3 // 3 //
4 //------------------------------------------------------------------------- 4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site: 5 // This file is part of the TightVNC software. Please visit our Web site:
6 // 6 //
27 import com.glavsoft.exceptions.TransportException; 27 import com.glavsoft.exceptions.TransportException;
28 import com.glavsoft.rfb.encoding.PixelFormat; 28 import com.glavsoft.rfb.encoding.PixelFormat;
29 import com.glavsoft.transport.Reader; 29 import com.glavsoft.transport.Reader;
30 30
31 public class ColorDecoder { 31 public class ColorDecoder {
32 protected byte redShift; 32 protected byte redShift;
33 protected byte greenShift; 33 protected byte greenShift;
34 protected byte blueShift; 34 protected byte blueShift;
35 public short redMax; 35 public short redMax;
36 public short greenMax; 36 public short greenMax;
37 public short blueMax; 37 public short blueMax;
38 private final int bytesPerPixel; 38 public final int bytesPerPixel;
39 private final int bytesPerPixelSignificant; 39 public final int bytesPerCPixel;
40 public final int bytesPerPixelTight;
40 private final byte[] buff; 41 private final byte[] buff;
41 42
42 private int startShift; 43 private int startShift;
43 private int startShiftCompact; 44 private int startShiftCompact;
44 private int addShiftItem; 45 private int addShiftItem;
45 private final boolean isTightSpecific; 46 private final boolean isTightSpecific;
46 47
47 public ColorDecoder(PixelFormat pf) { 48 public ColorDecoder(PixelFormat pf) {
48 redShift = pf.redShift; 49 redShift = pf.redShift;
49 greenShift = pf.greenShift; 50 greenShift = pf.greenShift;
50 blueShift = pf.blueShift; 51 blueShift = pf.blueShift;
51 redMax = pf.redMax; 52 redMax = pf.redMax;
52 greenMax = pf.greenMax; 53 greenMax = pf.greenMax;
53 blueMax = pf.blueMax; 54 blueMax = pf.blueMax;
54 bytesPerPixel = pf.bitsPerPixel / 8; 55 bytesPerPixel = pf.bitsPerPixel / 8;
55 bytesPerPixelSignificant = 24 == pf.depth && 32 == pf.bitsPerPixel ? 3 : bytesPerPixel; 56 final long significant = redMax << redShift | greenMax << greenShift | blueMax << blueShift;
57 bytesPerCPixel = pf.depth <= 24 // as in RFB
58 // || 32 == pf.depth) // UltraVNC use this... :(
59 && 32 == pf.bitsPerPixel
60 && ((significant & 0x00ff000000L) == 0 || (significant & 0x000000ffL) == 0)
61 ? 3
62 : bytesPerPixel;
63 bytesPerPixelTight = 24 == pf.depth && 32 == pf.bitsPerPixel ? 3 : bytesPerPixel;
56 buff = new byte[bytesPerPixel]; 64 buff = new byte[bytesPerPixel];
57 if (0 == pf.bigEndianFlag) { 65 if (0 == pf.bigEndianFlag) {
58 startShift = 0; 66 startShift = 0;
59 startShiftCompact = 0; 67 startShiftCompact = 0;
60 addShiftItem = 8; 68 addShiftItem = 8;
61 } else { 69 } else {
62 startShift = pf.bitsPerPixel - 8; 70 startShift = pf.bitsPerPixel - 8;
63 startShiftCompact = Math.max(0, pf.depth - 8); 71 startShiftCompact = Math.max(0, pf.depth - 8);
64 addShiftItem = -8; 72 addShiftItem = -8;
65 } 73 }
66 isTightSpecific = 4==bytesPerPixel && 3==bytesPerPixelSignificant && 74 isTightSpecific = 4==bytesPerPixel && 3==bytesPerPixelTight &&
67 255 == redMax && 255 == greenMax && 255 == blueMax; 75 255 == redMax && 255 == greenMax && 255 == blueMax;
68 } 76 }
69 77
70 protected int readColor(Reader reader) throws TransportException { 78 protected int readColor(Reader reader) throws TransportException {
71 return getColor(reader.readBytes(buff, 0, bytesPerPixel), 0); 79 return getColor(reader.readBytes(buff, 0, bytesPerPixel), 0);
72 } 80 }
73 81
74 protected int readCompactColor(Reader reader) throws TransportException { 82 protected int readCompactColor(Reader reader) throws TransportException {
75 return getCompactColor(reader.readBytes(buff, 0, bytesPerPixelSignificant), 0); 83 return getCompactColor(reader.readBytes(buff, 0, bytesPerCPixel), 0);
76 } 84 }
77 85
78 protected int readTightColor(Reader reader) throws TransportException { 86 protected int readTightColor(Reader reader) throws TransportException {
79 return getTightColor(reader.readBytes(buff, 0, bytesPerPixelSignificant), 0); 87 return getTightColor(reader.readBytes(buff, 0, bytesPerPixelTight), 0);
80 } 88 }
81 89
82 protected int convertColor(int rawColor) { 90 protected int convertColor(int rawColor) {
83 return 255 * (rawColor >> redShift & redMax) / redMax << 16 | 91 return 255 * (rawColor >> redShift & redMax) / redMax << 16 |
84 255 * (rawColor >> greenShift & greenMax) / greenMax << 8 | 92 255 * (rawColor >> greenShift & greenMax) / greenMax << 8 |
90 comp[0] = (byte) (rawColor >> redShift & redMax); 98 comp[0] = (byte) (rawColor >> redShift & redMax);
91 comp[1] = (byte) (rawColor >> greenShift & greenMax); 99 comp[1] = (byte) (rawColor >> greenShift & greenMax);
92 comp[2] = (byte) (rawColor >> blueShift & blueMax); 100 comp[2] = (byte) (rawColor >> blueShift & blueMax);
93 } 101 }
94 102
95 protected int getTightColor(byte[] bytes, int offset) { 103 public int getTightColor(byte[] bytes, int offset) {
96 return convertColor(getRawTightColor(bytes, offset)); 104 return convertColor(getRawTightColor(bytes, offset));
97 } 105 }
98 106
99 private int getRawTightColor(byte[] bytes, int offset) { 107 private int getRawTightColor(byte[] bytes, int offset) {
100 if (isTightSpecific) 108 if (isTightSpecific)
101 return (bytes[offset++] & 0xff)<<16 | 109 return (bytes[offset++] & 0xff)<<16 |
102 (bytes[offset++] & 0xff)<<8 | 110 (bytes[offset++] & 0xff)<<8 |
103 bytes[offset++] & 0xff; 111 bytes[offset] & 0xff;
104 else 112 else
105 return getRawColor(bytes, offset); 113 return getRawColor(bytes, offset);
106 } 114 }
107 115
108 protected int getColor(byte[] bytes, int offset) { 116 protected int getColor(byte[] bytes, int offset) {
121 129
122 protected int getCompactColor(byte[] bytes, int offset) { 130 protected int getCompactColor(byte[] bytes, int offset) {
123 int shift = startShiftCompact; 131 int shift = startShiftCompact;
124 int item = addShiftItem; 132 int item = addShiftItem;
125 int rawColor = (bytes[offset++] & 0xff)<<shift; 133 int rawColor = (bytes[offset++] & 0xff)<<shift;
126 for (int i=1; i<bytesPerPixelSignificant; ++i) { 134 for (int i=1; i< bytesPerCPixel; ++i) {
127 rawColor |= (bytes[offset++] & 0xff)<<(shift+=item); 135 rawColor |= (bytes[offset++] & 0xff)<<(shift+=item);
128 } 136 }
129 return convertColor(rawColor); 137 return convertColor(rawColor);
130 } 138 }
131 139