Mercurial > hg > Applications > TightVNC_orginal
comparison src/main/java/com/glavsoft/drawing/ColorDecoder.java @ 0:4689cc86d6cb
create TreeViewer2 Repository
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Jul 2012 13:20:49 +0900 |
parents | |
children | 472a9bcacb21 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4689cc86d6cb |
---|---|
1 // Copyright (C) 2010, 2011 GlavSoft LLC. | |
2 // All rights reserved. | |
3 // | |
4 //------------------------------------------------------------------------- | |
5 // This file is part of the TightVNC software. Please visit our Web site: | |
6 // | |
7 // http://www.tightvnc.com/ | |
8 // | |
9 // This program is free software; you can redistribute it and/or modify | |
10 // it under the terms of the GNU General Public License as published by | |
11 // the Free Software Foundation; either version 2 of the License, or | |
12 // (at your option) any later version. | |
13 // | |
14 // This program is distributed in the hope that it will be useful, | |
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 // GNU General Public License for more details. | |
18 // | |
19 // You should have received a copy of the GNU General Public License along | |
20 // with this program; if not, write to the Free Software Foundation, Inc., | |
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 //------------------------------------------------------------------------- | |
23 // | |
24 | |
25 package com.glavsoft.drawing; | |
26 | |
27 import com.glavsoft.exceptions.TransportException; | |
28 import com.glavsoft.rfb.encoding.PixelFormat; | |
29 import com.glavsoft.transport.Reader; | |
30 | |
31 public class ColorDecoder { | |
32 protected byte redShift; | |
33 protected byte greenShift; | |
34 protected byte blueShift; | |
35 public short redMax; | |
36 public short greenMax; | |
37 public short blueMax; | |
38 private final int bytesPerPixel; | |
39 private final int bytesPerPixelSignificant; | |
40 private final byte[] buff; | |
41 | |
42 private int startShift; | |
43 private int startShiftCompact; | |
44 private int addShiftItem; | |
45 private final boolean isTightSpecific; | |
46 | |
47 public ColorDecoder(PixelFormat pf) { | |
48 redShift = pf.redShift; | |
49 greenShift = pf.greenShift; | |
50 blueShift = pf.blueShift; | |
51 redMax = pf.redMax; | |
52 greenMax = pf.greenMax; | |
53 blueMax = pf.blueMax; | |
54 bytesPerPixel = pf.bitsPerPixel / 8; | |
55 bytesPerPixelSignificant = 24 == pf.depth && 32 == pf.bitsPerPixel ? 3 : bytesPerPixel; | |
56 buff = new byte[bytesPerPixel]; | |
57 if (0 == pf.bigEndianFlag) { | |
58 startShift = 0; | |
59 startShiftCompact = 0; | |
60 addShiftItem = 8; | |
61 } else { | |
62 startShift = pf.bitsPerPixel - 8; | |
63 startShiftCompact = Math.max(0, pf.depth - 8); | |
64 addShiftItem = -8; | |
65 } | |
66 isTightSpecific = 4==bytesPerPixel && 3==bytesPerPixelSignificant && | |
67 255 == redMax && 255 == greenMax && 255 == blueMax; | |
68 } | |
69 | |
70 protected int readColor(Reader reader) throws TransportException { | |
71 return getColor(reader.readBytes(buff, 0, bytesPerPixel), 0); | |
72 } | |
73 | |
74 protected int readCompactColor(Reader reader) throws TransportException { | |
75 return getCompactColor(reader.readBytes(buff, 0, bytesPerPixelSignificant), 0); | |
76 } | |
77 | |
78 protected int readTightColor(Reader reader) throws TransportException { | |
79 return getTightColor(reader.readBytes(buff, 0, bytesPerPixelSignificant), 0); | |
80 } | |
81 | |
82 protected int convertColor(int rawColor) { | |
83 return 255 * (rawColor >> redShift & redMax) / redMax << 16 | | |
84 255 * (rawColor >> greenShift & greenMax) / greenMax << 8 | | |
85 255 * (rawColor >> blueShift & blueMax) / blueMax; | |
86 } | |
87 | |
88 public void fillRawComponents(byte[] comp, byte[] bytes, int offset) { | |
89 int rawColor = getRawTightColor(bytes, offset); | |
90 comp[0] = (byte) (rawColor >> redShift & redMax); | |
91 comp[1] = (byte) (rawColor >> greenShift & greenMax); | |
92 comp[2] = (byte) (rawColor >> blueShift & blueMax); | |
93 } | |
94 | |
95 protected int getTightColor(byte[] bytes, int offset) { | |
96 return convertColor(getRawTightColor(bytes, offset)); | |
97 } | |
98 | |
99 private int getRawTightColor(byte[] bytes, int offset) { | |
100 if (isTightSpecific) | |
101 return (bytes[offset++] & 0xff)<<16 | | |
102 (bytes[offset++] & 0xff)<<8 | | |
103 bytes[offset++] & 0xff; | |
104 else | |
105 return getRawColor(bytes, offset); | |
106 } | |
107 | |
108 protected int getColor(byte[] bytes, int offset) { | |
109 return convertColor(getRawColor(bytes, offset)); | |
110 } | |
111 | |
112 private int getRawColor(byte[] bytes, int offset) { | |
113 int shift = startShift; | |
114 int item = addShiftItem; | |
115 int rawColor = (bytes[offset++] & 0xff)<<shift; | |
116 for (int i=1; i<bytesPerPixel; ++i) { | |
117 rawColor |= (bytes[offset++] & 0xff)<<(shift+=item); | |
118 } | |
119 return rawColor; | |
120 } | |
121 | |
122 protected int getCompactColor(byte[] bytes, int offset) { | |
123 int shift = startShiftCompact; | |
124 int item = addShiftItem; | |
125 int rawColor = (bytes[offset++] & 0xff)<<shift; | |
126 for (int i=1; i<bytesPerPixelSignificant; ++i) { | |
127 rawColor |= (bytes[offset++] & 0xff)<<(shift+=item); | |
128 } | |
129 return convertColor(rawColor); | |
130 } | |
131 | |
132 } |