corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /// <summary>**************************************************************************
2 ///
3 /// $Id: PaletteBox.java,v 1.1 2002/07/25 14:50:47 grosbois Exp $
4 ///
5 /// Copyright Eastman Kodak Company, 343 State Street, Rochester, NY 14650
6 /// $Date $
7 /// ***************************************************************************
8 /// </summary>
9 using System;
10 using ColorSpaceException = CSJ2K.Color.ColorSpaceException;
11 using ICCProfile = CSJ2K.Icc.ICCProfile;
12 using ParameterList = CSJ2K.j2k.util.ParameterList;
13 using RandomAccessIO = CSJ2K.j2k.io.RandomAccessIO;
14 namespace CSJ2K.Color.Boxes
15 {
16  
17 /// <summary> This class models the palette box contained in a JP2
18 /// image.
19 ///
20 /// </summary>
21 /// <version> 1.0
22 /// </version>
23 /// <author> Bruce A. Kern
24 /// </author>
25 public sealed class PaletteBox:JP2Box
26 {
27 /// <summary>Return the number of palette entries. </summary>
28 public int NumEntries
29 {
30 get
31 {
32 return nentries;
33 }
34  
35 }
36 /// <summary>Return the number of palette columns. </summary>
37 public int NumColumns
38 {
39 get
40 {
41 return ncolumns;
42 }
43  
44 }
45  
46 private int nentries;
47 private int ncolumns;
48 private short[] bitdepth;
49 private int[][] entries;
50  
51 /// <summary> Construct a PaletteBox from an input image.</summary>
52 /// <param name="in">RandomAccessIO jp2 image
53 /// </param>
54 /// <param name="boxStart">offset to the start of the box in the image
55 /// </param>
56 /// <exception cref="IOException,">ColorSpaceException
57 /// </exception>
58 public PaletteBox(RandomAccessIO in_Renamed, int boxStart):base(in_Renamed, boxStart)
59 {
60 readBox();
61 }
62  
63 /// <summary>Analyze the box content. </summary>
64 internal void readBox()
65 {
66 byte[] bfr = new byte[4];
67 int i, j, b, m;
68 //int entry;
69  
70 // Read the number of palette entries and columns per entry.
71 in_Renamed.seek((int) dataStart);
72 in_Renamed.readFully(bfr, 0, 3);
73 nentries = ICCProfile.getShort(bfr, 0) & 0x0000ffff;
74 ncolumns = bfr[2] & 0x0000ffff;
75  
76 // Read the bitdepths for each column
77 bitdepth = new short[ncolumns];
78 bfr = new byte[ncolumns];
79 in_Renamed.readFully(bfr, 0, ncolumns);
80 for (i = 0; i < ncolumns; ++i)
81 {
82 bitdepth[i] = (short) (bfr[i] & 0x00fff);
83 }
84  
85 entries = new int[nentries * ncolumns][];
86  
87 bfr = new byte[2];
88 for (i = 0; i < nentries; ++i)
89 {
90 entries[i] = new int[ncolumns];
91  
92 for (j = 0; j < ncolumns; ++j)
93 {
94  
95 int bd = getBitDepth(j);
96 bool signed = isSigned(j);
97  
98 switch (getEntrySize(j))
99 {
100  
101 case 1: // 8 bit entries
102 in_Renamed.readFully(bfr, 0, 1);
103 b = bfr[0];
104 break;
105  
106  
107 case 2: // 16 bits
108 in_Renamed.readFully(bfr, 0, 2);
109 b = ICCProfile.getShort(bfr, 0);
110 break;
111  
112  
113 default:
114 throw new ColorSpaceException("palettes greater than 16 bits deep not supported");
115  
116 }
117  
118 if (signed)
119 {
120 // Do sign extension if high bit is set.
121 if ((b & (1 << (bd - 1))) == 0)
122 {
123 // high bit not set.
124 m = (1 << bd) - 1;
125 entries[i][j] = m & b;
126 }
127 else
128 {
129 // high bit set.
130 // CONVERSION PROBLEM?
131 m = unchecked((int)(0xffffffff << bd));
132 entries[i][j] = m | b;
133 }
134 }
135 else
136 {
137 // Clear all high bits.
138 m = (1 << bd) - 1;
139 entries[i][j] = m & b;
140 }
141 }
142 }
143 }
144  
145 /// <summary>Are entries signed predicate. </summary>
146 public bool isSigned(int column)
147 {
148 return (bitdepth[column] & 0x80) == 1;
149 }
150  
151 /// <summary>Are entries unsigned predicate. </summary>
152 public bool isUnSigned(int column)
153 {
154 return !isSigned(column);
155 }
156  
157 /// <summary>Return the bitdepth of palette entries. </summary>
158 public short getBitDepth(int column)
159 {
160 return (short) ((bitdepth[column] & 0x7f) + 1);
161 }
162  
163 /// <summary>Return an entry for a given index and column. </summary>
164 public int getEntry(int column, int entry)
165 {
166 return entries[entry][column];
167 }
168  
169 /// <summary>Return a suitable String representation of the class instance. </summary>
170 public override System.String ToString()
171 {
172 System.Text.StringBuilder rep = new System.Text.StringBuilder("[PaletteBox ").Append("nentries= ").Append(System.Convert.ToString(nentries)).Append(", ncolumns= ").Append(System.Convert.ToString(ncolumns)).Append(", bitdepth per column= (");
173 for (int i = 0; i < ncolumns; ++i)
174 rep.Append(getBitDepth(i)).Append(isSigned(i)?"S":"U").Append(i < ncolumns - 1?", ":"");
175 return rep.Append(")]").ToString();
176 }
177  
178 private int getEntrySize(int column)
179 {
180 int bd = getBitDepth(column);
181 return bd / 8 + (bd % 8) == 0?0:1;
182 }
183  
184 /* end class PaletteBox */
185 static PaletteBox()
186 {
187 {
188 type = 0x70636c72;
189 }
190 }
191 }
192 }