corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /// <summary>**************************************************************************
2 ///
3 /// $Id: ColorSpace.java,v 1.2 2002/07/25 16:31:11 grosbois Exp $
4 ///
5 /// Copyright Eastman Kodak Company, 343 State Street, Rochester, NY 14650
6 /// $Date $
7 /// ***************************************************************************
8 /// </summary>
9 using System;
10 using FileFormatBoxes = CSJ2K.j2k.fileformat.FileFormatBoxes;
11 using ParameterList = CSJ2K.j2k.util.ParameterList;
12 using HeaderDecoder = CSJ2K.j2k.codestream.reader.HeaderDecoder;
13 using RandomAccessIO = CSJ2K.j2k.io.RandomAccessIO;
14 using ICCProfile = CSJ2K.Icc.ICCProfile;
15 using PaletteBox = CSJ2K.Color.Boxes.PaletteBox;
16 using ComponentMappingBox = CSJ2K.Color.Boxes.ComponentMappingBox;
17 using ColorSpecificationBox = CSJ2K.Color.Boxes.ColorSpecificationBox;
18 using ChannelDefinitionBox = CSJ2K.Color.Boxes.ChannelDefinitionBox;
19 using ImageHeaderBox = CSJ2K.Color.Boxes.ImageHeaderBox;
20 using JP2Box = CSJ2K.Color.Boxes.JP2Box;
21 namespace CSJ2K.Color
22 {
23  
24 /// <summary> This class analyzes the image to provide colorspace
25 /// information for the decoding chain. It does this by
26 /// examining the box structure of the JP2 image.
27 /// It also provides access to the parameter list information,
28 /// which is stored as a public final field.
29 ///
30 /// </summary>
31 /// <seealso cref="jj2000.j2k.icc.ICCProfile">
32 /// </seealso>
33 /// <version> 1.0
34 /// </version>
35 /// <author> Bruce A. Kern
36 /// </author>
37 public class ColorSpace
38 {
39 /// <summary> Retrieve the ICC profile from the images as
40 /// a byte array.
41 /// </summary>
42 /// <returns> the ICC Profile as a byte [].
43 /// </returns>
44 virtual public byte[] ICCProfile
45 {
46 get
47 {
48 return csbox.ICCProfile;
49 }
50  
51 }
52 /// <summary>Return the colorspace method (Profiled, enumerated, or palettized). </summary>
53 virtual public MethodEnum Method
54 {
55 get
56 {
57 return csbox.Method;
58 }
59  
60 }
61 /// <summary>Return number of channels in the palette. </summary>
62 virtual public PaletteBox PaletteBox
63 {
64 get
65 {
66 return pbox;
67 }
68  
69 }
70 /// <summary>Return number of channels in the palette. </summary>
71 virtual public int PaletteChannels
72 {
73 get
74 {
75 return pbox == null?0:pbox.NumColumns;
76 }
77  
78 }
79 /// <summary>Is palettized predicate. </summary>
80 virtual public bool Palettized
81 {
82 get
83 {
84 return pbox != null;
85 }
86  
87 }
88 //UPGRADE_NOTE: Final was removed from the declaration of 'eol '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
89 public static readonly System.String eol = System.Environment.NewLine;
90  
91 // Renamed for convenience:
92 internal const int GRAY = 0;
93 internal const int RED = 1;
94 internal const int GREEN = 2;
95 internal const int BLUE = 3;
96  
97 /// <summary>Parameter Specs </summary>
98 public ParameterList pl;
99  
100 /// <summary>Parameter Specs </summary>
101 public HeaderDecoder hd;
102  
103 /* Image box structure as pertains to colorspacees. */
104 private PaletteBox pbox = null;
105 private ComponentMappingBox cmbox = null;
106 private ColorSpecificationBox csbox = null;
107 private ChannelDefinitionBox cdbox = null;
108 private ImageHeaderBox ihbox = null;
109  
110 /// <summary>Input image </summary>
111 private RandomAccessIO in_Renamed = null;
112  
113 /// <summary>Indent a String that contains newlines. </summary>
114 public static System.String indent(System.String ident, System.Text.StringBuilder instr)
115 {
116 return indent(ident, instr.ToString());
117 }
118  
119 /// <summary>Indent a String that contains newlines. </summary>
120 public static System.String indent(System.String ident, System.String instr)
121 {
122 System.Text.StringBuilder tgt = new System.Text.StringBuilder(instr);
123 char eolChar = eol[0];
124 int i = tgt.Length;
125 while (--i > 0)
126 {
127 if (tgt[i] == eolChar)
128 tgt.Insert(i + 1, ident);
129 }
130 return ident + tgt.ToString();
131 }
132  
133 /// <summary> public constructor which takes in the image, parameterlist and the
134 /// image header decoder as args.
135 /// </summary>
136 /// <param name="in">input RandomAccess image file.
137 /// </param>
138 /// <param name="hd">provides information about the image header.
139 /// </param>
140 /// <param name="pl">provides parameters from the default and commandline lists.
141 /// </param>
142 /// <exception cref="IOException,">ColorSpaceException
143 /// </exception>
144 public ColorSpace(RandomAccessIO in_Renamed, HeaderDecoder hd, ParameterList pl)
145 {
146 this.pl = pl;
147 this.in_Renamed = in_Renamed;
148 this.hd = hd;
149 getBoxes();
150 }
151  
152 /// <summary> Retrieve the various boxes from the JP2 file.</summary>
153 /// <exception cref="ColorSpaceException,">IOException
154 /// </exception>
155 protected internal void getBoxes()
156 {
157 //byte[] data;
158 int type;
159 long len = 0;
160 int boxStart = 0;
161 byte[] boxHeader = new byte[16];
162 int i = 0;
163  
164 // Search the toplevel boxes for the header box
165 while (true)
166 {
167 in_Renamed.seek(boxStart);
168 in_Renamed.readFully(boxHeader, 0, 16);
169 // CONVERSION PROBLEM?
170  
171 len = (long)CSJ2K.Icc.ICCProfile.getInt(boxHeader, 0);
172 if (len == 1)
173 len = CSJ2K.Icc.ICCProfile.getLong(boxHeader, 8); // Extended
174 // length
175 type = CSJ2K.Icc.ICCProfile.getInt(boxHeader, 4);
176  
177 // Verify the contents of the file so far.
178 if (i == 0 && type != CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_SIGNATURE_BOX)
179 {
180 throw new ColorSpaceException("first box in image not " + "signature");
181 }
182 else if (i == 1 && type != CSJ2K.j2k.fileformat.FileFormatBoxes.FILE_TYPE_BOX)
183 {
184 throw new ColorSpaceException("second box in image not file");
185 }
186 else if (type == CSJ2K.j2k.fileformat.FileFormatBoxes.CONTIGUOUS_CODESTREAM_BOX)
187 {
188 throw new ColorSpaceException("header box not found in image");
189 }
190 else if (type == CSJ2K.j2k.fileformat.FileFormatBoxes.JP2_HEADER_BOX)
191 {
192 break;
193 }
194  
195 // Progress to the next box.
196 ++i;
197 boxStart = (int) (boxStart + len);
198 }
199  
200 // boxStart indexes the start of the JP2_HEADER_BOX,
201 // make headerBoxEnd index the end of the box.
202 long headerBoxEnd = boxStart + len;
203  
204 if (len == 1)
205 boxStart += 8; // Extended length header
206  
207 for (boxStart += 8; boxStart < headerBoxEnd; boxStart = (int) (boxStart + len))
208 {
209 in_Renamed.seek(boxStart);
210 in_Renamed.readFully(boxHeader, 0, 16);
211 len = (long)CSJ2K.Icc.ICCProfile.getInt(boxHeader, 0);
212 if (len == 1)
213 throw new ColorSpaceException("Extended length boxes " + "not supported");
214 type = (int)CSJ2K.Icc.ICCProfile.getInt(boxHeader, 4);
215  
216 switch (type)
217 {
218  
219 case CSJ2K.j2k.fileformat.FileFormatBoxes.IMAGE_HEADER_BOX:
220 ihbox = new ImageHeaderBox(in_Renamed, boxStart);
221 break;
222  
223 case CSJ2K.j2k.fileformat.FileFormatBoxes.COLOUR_SPECIFICATION_BOX:
224 csbox = new ColorSpecificationBox(in_Renamed, boxStart);
225 break;
226  
227 case CSJ2K.j2k.fileformat.FileFormatBoxes.CHANNEL_DEFINITION_BOX:
228 cdbox = new ChannelDefinitionBox(in_Renamed, boxStart);
229 break;
230  
231 case CSJ2K.j2k.fileformat.FileFormatBoxes.COMPONENT_MAPPING_BOX:
232 cmbox = new ComponentMappingBox(in_Renamed, boxStart);
233 break;
234  
235 case CSJ2K.j2k.fileformat.FileFormatBoxes.PALETTE_BOX:
236 pbox = new PaletteBox(in_Renamed, boxStart);
237 break;
238  
239 default:
240 break;
241  
242 }
243 }
244  
245 if (ihbox == null)
246 throw new ColorSpaceException("image header box not found");
247  
248 if ((pbox == null && cmbox != null) || (pbox != null && cmbox == null))
249 throw new ColorSpaceException("palette box and component " + "mapping box inconsistency");
250 }
251  
252  
253 /// <summary>Return the channel definition of the input component. </summary>
254 public virtual int getChannelDefinition(int c)
255 {
256 if (cdbox == null)
257 return c;
258 else
259 return cdbox.getCn(c + 1);
260 }
261  
262 /// <summary>Return the colorspace (sYCC, sRGB, sGreyScale). </summary>
263 public virtual CSEnum getColorSpace()
264 {
265 return csbox.ColorSpace;
266 }
267  
268 /// <summary>Return bitdepth of the palette entries. </summary>
269 public virtual int getPaletteChannelBits(int c)
270 {
271 return pbox == null ? 0 : (int)pbox.getBitDepth(c);
272 }
273  
274 /// <summary> Return a palettized sample</summary>
275 /// <param name="channel">requested
276 /// </param>
277 /// <param name="index">of entry
278 /// </param>
279 /// <returns> palettized sample
280 /// </returns>
281 public virtual int getPalettizedSample(int channel, int index)
282 {
283 return pbox == null?0:pbox.getEntry(channel, index);
284 }
285  
286 /// <summary>Signed output predicate. </summary>
287 public virtual bool isOutputSigned(int channel)
288 {
289 return (pbox != null)?pbox.isSigned(channel):hd.isOriginalSigned(channel);
290 }
291  
292 /// <summary>Return a suitable String representation of the class instance. </summary>
293 public override System.String ToString()
294 {
295 System.Text.StringBuilder rep = new System.Text.StringBuilder("[ColorSpace is ").Append(csbox.MethodString).Append(Palettized?" and palettized ":" ").Append(Method == MethodEnum.ENUMERATED?csbox.ColorSpaceString:"");
296 if (ihbox != null)
297 {
298 rep.Append(eol).Append(indent(" ", ihbox.ToString()));
299 }
300 if (cdbox != null)
301 {
302 rep.Append(eol).Append(indent(" ", cdbox.ToString()));
303 }
304 if (csbox != null)
305 {
306 rep.Append(eol).Append(indent(" ", csbox.ToString()));
307 }
308 if (pbox != null)
309 {
310 rep.Append(eol).Append(indent(" ", pbox.ToString()));
311 }
312 if (cmbox != null)
313 {
314 rep.Append(eol).Append(indent(" ", cmbox.ToString()));
315 }
316 return rep.Append("]").ToString();
317 }
318  
319 /// <summary> Are profiling diagnostics turned on</summary>
320 /// <returns> yes or no
321 /// </returns>
322 public virtual bool debugging()
323 {
324 return pl.Get("colorspace_debug") != null && pl.Get("colorspace_debug").ToUpper().Equals("on".ToUpper());
325 }
326  
327  
328 public enum MethodEnum
329 {
330 ICC_PROFILED,
331 ENUMERATED
332 }
333 public enum CSEnum
334 {
335 sRGB,
336 GreyScale,
337 sYCC,
338 esRGB,
339 Illegal,
340 Unknown
341 }
342 /* Enumeration Class */
343 /*
344 /// <summary>method enumeration </summary>
345 //UPGRADE_NOTE: Final was removed from the declaration of 'ICC_PROFILED '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
346 public const MethodEnum ICC_PROFILED = new MethodEnum("profiled");
347 /// <summary>method enumeration </summary>
348 //UPGRADE_NOTE: Final was removed from the declaration of 'ENUMERATED '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
349 public const MethodEnum ENUMERATED = new MethodEnum("enumerated");
350  
351 /// <summary>colorspace enumeration </summary>
352 //UPGRADE_NOTE: Final was removed from the declaration of 'sRGB '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
353 public const CSEnum sRGB = new CSEnum("sRGB");
354 /// <summary>colorspace enumeration </summary>
355 //UPGRADE_NOTE: Final was removed from the declaration of 'GreyScale '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
356 public const CSEnum GreyScale = new CSEnum("GreyScale");
357 /// <summary>colorspace enumeration </summary>
358 //UPGRADE_NOTE: Final was removed from the declaration of 'sYCC '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
359 public const CSEnum sYCC = new CSEnum("sYCC");
360 /// <summary>colorspace enumeration </summary>
361 //UPGRADE_NOTE: Final was removed from the declaration of 'Illegal '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
362 public const CSEnum Illegal = new CSEnum("Illegal");
363 /// <summary>colorspace enumeration </summary>
364 //UPGRADE_NOTE: Final was removed from the declaration of 'Unknown '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
365 public const CSEnum Unknown = new CSEnum("Unknown");
366  
367 /// <summary> Typesafe enumeration class</summary>
368 /// <version> 1.0
369 /// </version>
370 /// <author> Bruce A Kern
371 /// </author>
372 public class Enumeration
373 {
374 //UPGRADE_NOTE: Final was removed from the declaration of 'value '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
375 public System.String value_Renamed;
376 public Enumeration(System.String value_Renamed)
377 {
378 this.value_Renamed = value_Renamed;
379 }
380 public override System.String ToString()
381 {
382 return value_Renamed;
383 }
384 }
385  
386  
387 /// <summary> Method enumeration class</summary>
388 /// <version> 1.0
389 /// </version>
390 /// <author> Bruce A Kern
391 /// </author>
392 public class MethodEnum:Enumeration
393 {
394 public MethodEnum(System.String value_Renamed):base(value_Renamed)
395 {
396 }
397 }
398  
399 /// <summary> Colorspace enumeration class</summary>
400 /// <version> 1.0
401 /// </version>
402 /// <author> Bruce A Kern
403 /// </author>
404 public class CSEnum:Enumeration
405 {
406 public CSEnum(System.String value_Renamed):base(value_Renamed)
407 {
408 }
409 }
410 */
411 /* end class ColorSpace */
412 }
413 }