corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /// <summary>**************************************************************************
2 ///
3 /// $Id: ICCProfiler.java,v 1.2 2002/08/08 14:08:27 grosbois Exp $
4 ///
5 /// Copyright Eastman Kodak Company, 343 State Street, Rochester, NY 14650
6 /// $Date $
7 /// ***************************************************************************
8 /// </summary>
9 using System;
10 using CSJ2K.j2k.decoder;
11 using CSJ2K.j2k.image;
12 using CSJ2K.j2k.util;
13 using CSJ2K.j2k.io;
14 using CSJ2K.Color;
15 using CSJ2K.Icc.Lut;
16 namespace CSJ2K.Icc
17 {
18  
19 /// <summary> This class provides ICC Profiling API for the jj2000.j2k imaging chain
20 /// by implementing the BlkImgDataSrc interface, in particular the getCompData
21 /// and getInternCompData methods.
22 ///
23 /// </summary>
24 /// <seealso cref="jj2000.j2k.icc.ICCProfile">
25 /// </seealso>
26 /// <version> 1.0
27 /// </version>
28 /// <author> Bruce A. Kern
29 /// </author>
30 public class ICCProfiler:ColorSpaceMapper
31 {
32  
33 /// <summary>The prefix for ICC Profiler options </summary>
34 new public const char OPT_PREFIX = 'I';
35  
36 /// <summary>Platform dependant end of line String. </summary>
37 //UPGRADE_NOTE: Final was removed from the declaration of 'eol '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
38 new protected internal static readonly System.String eol = System.Environment.NewLine;
39  
40 // Renamed for convenience:
41 //UPGRADE_NOTE: Final was removed from the declaration of 'GRAY '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
42 //UPGRADE_NOTE: The initialization of 'GRAY' was moved to static method 'icc.ICCProfiler'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
43 private static readonly int GRAY;
44 //UPGRADE_NOTE: Final was removed from the declaration of 'RED '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
45 //UPGRADE_NOTE: The initialization of 'RED' was moved to static method 'icc.ICCProfiler'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
46 private static readonly int RED;
47 //UPGRADE_NOTE: Final was removed from the declaration of 'GREEN '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
48 //UPGRADE_NOTE: The initialization of 'GREEN' was moved to static method 'icc.ICCProfiler'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
49 private static readonly int GREEN;
50 //UPGRADE_NOTE: Final was removed from the declaration of 'BLUE '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
51 //UPGRADE_NOTE: The initialization of 'BLUE' was moved to static method 'icc.ICCProfiler'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1005'"
52 private static readonly int BLUE;
53  
54 // ICCProfiles.
55 internal RestrictedICCProfile ricc = null;
56 internal ICCProfile icc = null;
57  
58 // Temporary variables needed during profiling.
59 private DataBlkInt[] tempInt; // Holds the results of the transform.
60 private DataBlkFloat[] tempFloat; // Holds the results of the transform.
61  
62 private System.Object xform = null;
63  
64 /// <summary>The image's ICC profile. </summary>
65 private RestrictedICCProfile iccp = null;
66  
67 /// <summary> Factory method for creating instances of this class.</summary>
68 /// <param name="src">-- source of image data
69 /// </param>
70 /// <param name="csMap">-- provides colorspace info
71 /// </param>
72 /// <returns> ICCProfiler instance
73 /// </returns>
74 /// <exception cref="IOException">profile access exception
75 /// </exception>
76 /// <exception cref="ICCProfileException">profile content exception
77 /// </exception>
78 public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, CSJ2K.Color.ColorSpace csMap)
79 {
80 return new ICCProfiler(src, csMap);
81 }
82  
83 /// <summary> Ctor which creates an ICCProfile for the image and initializes
84 /// all data objects (input, working, output).
85 ///
86 /// </summary>
87 /// <param name="src">-- Source of image data
88 /// </param>
89 /// <param name="csm">-- provides colorspace info
90 ///
91 /// </param>
92 /// <exception cref="IOException">
93 /// </exception>
94 /// <exception cref="ICCProfileException">
95 /// </exception>
96 /// <exception cref="IllegalArgumentException">
97 /// </exception>
98 protected internal ICCProfiler(BlkImgDataSrc src, CSJ2K.Color.ColorSpace csMap):base(src, csMap)
99 {
100 initialize();
101  
102 iccp = getICCProfile(csMap);
103 if (ncomps == 1)
104 {
105 xform = new MonochromeTransformTosRGB(iccp, maxValueArray[0], shiftValueArray[0]);
106 }
107 else
108 {
109 xform = new MatrixBasedTransformTosRGB(iccp, maxValueArray, shiftValueArray);
110 }
111  
112 /* end ICCProfiler ctor */
113 }
114  
115 /// <summary>General utility used by ctors </summary>
116 private void initialize()
117 {
118  
119 tempInt = new DataBlkInt[ncomps];
120 tempFloat = new DataBlkFloat[ncomps];
121  
122 /* For each component, get the maximum data value, a reference
123 * to the pixel data and set up working and temporary DataBlks
124 * for both integer and float output.
125 */
126 for (int i = 0; i < ncomps; ++i)
127 {
128 tempInt[i] = new DataBlkInt();
129 tempFloat[i] = new DataBlkFloat();
130 }
131 }
132  
133 /// <summary> Get the ICCProfile information JP2 ColorSpace</summary>
134 /// <param name="csm">provides all necessary info about the colorspace
135 /// </param>
136 /// <returns> ICCMatrixBasedInputProfile for 3 component input and
137 /// ICCMonochromeInputProfile for a 1 component source. Returns
138 /// null if exceptions were encountered.
139 /// </returns>
140 /// <exception cref="ColorSpaceException">
141 /// </exception>
142 /// <exception cref="ICCProfileException">
143 /// </exception>
144 /// <exception cref="IllegalArgumentException">
145 /// </exception>
146 private RestrictedICCProfile getICCProfile(CSJ2K.Color.ColorSpace csm)
147 {
148  
149 switch (ncomps)
150 {
151  
152 case 1:
153 icc = ICCMonochromeInputProfile.createInstance(csm);
154 ricc = icc.parse();
155 if (ricc.Type != RestrictedICCProfile.kMonochromeInput)
156 throw new System.ArgumentException("wrong ICCProfile type" + " for image");
157 break;
158  
159 case 3:
160 icc = ICCMatrixBasedInputProfile.createInstance(csm);
161 ricc = icc.parse();
162 if (ricc.Type != RestrictedICCProfile.kThreeCompInput)
163 throw new System.ArgumentException("wrong ICCProfile type" + " for image");
164 break;
165  
166 default:
167 throw new System.ArgumentException("illegal number of " + "components (" + ncomps + ") in image");
168  
169 }
170 return ricc;
171 }
172  
173 /// <summary> Returns, in the blk argument, a block of image data containing the
174 /// specifed rectangular area, in the specified component. The data is
175 /// returned, as a copy of the internal data, therefore the returned data
176 /// can be modified "in place".
177 ///
178 /// <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w'
179 /// and 'h' members of the 'blk' argument, relative to the current
180 /// tile. These members are not modified by this method. The 'offset' of
181 /// the returned data is 0, and the 'scanw' is the same as the block's
182 /// width. See the 'DataBlk' class.
183 ///
184 /// <P>If the data array in 'blk' is 'null', then a new one is created. If
185 /// the data array is not 'null' then it is reused, and it must be large
186 /// enough to contain the block's data. Otherwise an 'ArrayStoreException'
187 /// or an 'IndexOutOfBoundsException' is thrown by the Java system.
188 ///
189 /// <P>The returned data has its 'progressive' attribute set to that of the
190 /// input data.
191 ///
192 /// </summary>
193 /// <param name="out">Its coordinates and dimensions specify the area to
194 /// return. If it contains a non-null data array, then it must have the
195 /// correct dimensions. If it contains a null data array a new one is
196 /// created. The fields in this object are modified to return the data.
197 ///
198 /// </param>
199 /// <param name="c">The index of the component from which to get the data. Only 0
200 /// and 3 are valid.
201 ///
202 /// </param>
203 /// <returns> The requested DataBlk
204 ///
205 /// </returns>
206 /// <seealso cref="getInternCompData">
207 ///
208 /// </seealso>
209 public override DataBlk getCompData(DataBlk outblk, int c)
210 {
211  
212 try
213 {
214 if (ncomps != 1 && ncomps != 3)
215 {
216 System.String msg = "ICCProfiler: icc profile _not_ applied to " + ncomps + " component image";
217 FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, msg);
218 return src.getCompData(outblk, c);
219 }
220  
221 int type = outblk.DataType;
222  
223 int leftedgeOut = - 1; // offset to the start of the output scanline
224 int rightedgeOut = - 1; // offset to the end of the output
225 // scanline + 1
226 int leftedgeIn = - 1; // offset to the start of the input scanline
227 int rightedgeIn = - 1; // offset to the end of the input
228 // scanline + 1
229  
230 // Calculate all components:
231 for (int i = 0; i < ncomps; ++i)
232 {
233  
234 int fixedPtBits = src.getFixedPoint(i);
235 int shiftVal = shiftValueArray[i];
236 int maxVal = maxValueArray[i];
237  
238 // Initialize general input and output indexes
239 int kOut = - 1;
240 int kIn = - 1;
241  
242 switch (type)
243 {
244  
245 // Int and Float data only
246 case DataBlk.TYPE_INT:
247  
248 // Set up the DataBlk geometry
249 copyGeometry(workInt[i], outblk);
250 copyGeometry(tempInt[i], outblk);
251 copyGeometry(inInt[i], outblk);
252 InternalBuffer = outblk;
253  
254 // Reference the output array
255 workDataInt[i] = (int[]) workInt[i].Data;
256  
257 // Request data from the source.
258 inInt[i] = (DataBlkInt) src.getInternCompData(inInt[i], i);
259 dataInt[i] = inInt[i].DataInt;
260  
261 // The nitty-gritty.
262  
263 for (int row = 0; row < outblk.h; ++row)
264 {
265 leftedgeIn = inInt[i].offset + row * inInt[i].scanw;
266 rightedgeIn = leftedgeIn + inInt[i].w;
267 leftedgeOut = outblk.offset + row * outblk.scanw;
268 rightedgeOut = leftedgeOut + outblk.w;
269  
270 for (kOut = leftedgeOut, kIn = leftedgeIn; kIn < rightedgeIn; ++kIn, ++kOut)
271 {
272 int tmpInt = (dataInt[i][kIn] >> fixedPtBits) + shiftVal;
273 workDataInt[i][kOut] = ((tmpInt < 0)?0:((tmpInt > maxVal)?maxVal:tmpInt));
274 }
275 }
276 break;
277  
278  
279 case DataBlk.TYPE_FLOAT:
280  
281 // Set up the DataBlk geometry
282 copyGeometry(workFloat[i], outblk);
283 copyGeometry(tempFloat[i], outblk);
284 copyGeometry(inFloat[i], outblk);
285 InternalBuffer = outblk;
286  
287 // Reference the output array
288 workDataFloat[i] = (float[]) workFloat[i].Data;
289  
290 // Request data from the source.
291 inFloat[i] = (DataBlkFloat) src.getInternCompData(inFloat[i], i);
292 dataFloat[i] = inFloat[i].DataFloat;
293  
294 // The nitty-gritty.
295  
296 for (int row = 0; row < outblk.h; ++row)
297 {
298 leftedgeIn = inFloat[i].offset + row * inFloat[i].scanw;
299 rightedgeIn = leftedgeIn + inFloat[i].w;
300 leftedgeOut = outblk.offset + row * outblk.scanw;
301 rightedgeOut = leftedgeOut + outblk.w;
302  
303 for (kOut = leftedgeOut, kIn = leftedgeIn; kIn < rightedgeIn; ++kIn, ++kOut)
304 {
305 float tmpFloat = dataFloat[i][kIn] / (1 << fixedPtBits) + shiftVal;
306 workDataFloat[i][kOut] = ((tmpFloat < 0)?0:((tmpFloat > maxVal)?maxVal:tmpFloat));
307 }
308 }
309 break;
310  
311  
312 case DataBlk.TYPE_SHORT:
313 case DataBlk.TYPE_BYTE:
314 default:
315 // Unsupported output type.
316 throw new System.ArgumentException("Invalid source " + "datablock type");
317 }
318 }
319  
320 switch (type)
321 {
322  
323 // Int and Float data only
324 case DataBlk.TYPE_INT:
325  
326 if (ncomps == 1)
327 {
328 ((MonochromeTransformTosRGB) xform).apply(workInt[c], tempInt[c]);
329 }
330 else
331 {
332 // ncomps == 3
333 ((MatrixBasedTransformTosRGB) xform).apply(workInt, tempInt);
334 }
335  
336 outblk.progressive = inInt[c].progressive;
337 outblk.Data = tempInt[c].Data;
338 break;
339  
340  
341 case DataBlk.TYPE_FLOAT:
342  
343 if (ncomps == 1)
344 {
345 ((MonochromeTransformTosRGB) xform).apply(workFloat[c], tempFloat[c]);
346 }
347 else
348 {
349 // ncomps == 3
350 ((MatrixBasedTransformTosRGB) xform).apply(workFloat, tempFloat);
351 }
352  
353 outblk.progressive = inFloat[c].progressive;
354 outblk.Data = tempFloat[c].Data;
355 break;
356  
357  
358 case DataBlk.TYPE_SHORT:
359 case DataBlk.TYPE_BYTE:
360 default:
361 // Unsupported output type.
362 throw new System.ArgumentException("invalid source datablock" + " type");
363 }
364  
365 // Initialize the output block geometry and set the profiled
366 // data into the output block.
367 outblk.offset = 0;
368 outblk.scanw = outblk.w;
369 }
370 catch (MatrixBasedTransformException e)
371 {
372 //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
373 FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.ERROR, "matrix transform problem:\n" + e.Message);
374 if (pl.getParameter("debug").Equals("on"))
375 {
376 SupportClass.WriteStackTrace(e, Console.Error);
377 }
378 else
379 {
380 FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.ERROR, "Use '-debug' option for more details");
381 }
382 return null;
383 }
384 catch (MonochromeTransformException e)
385 {
386 //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
387 FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.ERROR, "monochrome transform problem:\n" + e.Message);
388 if (pl.getParameter("debug").Equals("on"))
389 {
390 SupportClass.WriteStackTrace(e, Console.Error);
391 }
392 else
393 {
394 FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.ERROR, "Use '-debug' option for more details");
395 }
396 return null;
397 }
398  
399 return outblk;
400 }
401  
402 /// <summary> Returns, in the blk argument, a block of image data containing the
403 /// specifed rectangular area, in the specified component. The data is
404 /// returned, as a reference to the internal data, if any, instead of as a
405 /// copy, therefore the returned data should not be modified.
406 ///
407 /// <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w'
408 /// and 'h' members of the 'blk' argument, relative to the current
409 /// tile. These members are not modified by this method. The 'offset' and
410 /// 'scanw' of the returned data can be arbitrary. See the 'DataBlk' class.
411 ///
412 /// <P>This method, in general, is more efficient than the 'getCompData()'
413 /// method since it may not copy the data. However if the array of returned
414 /// data is to be modified by the caller then the other method is probably
415 /// preferable.
416 ///
417 /// <P>If possible, the data in the returned 'DataBlk' should be the
418 /// internal data itself, instead of a copy, in order to increase the data
419 /// transfer efficiency. However, this depends on the particular
420 /// implementation (it may be more convenient to just return a copy of the
421 /// data). This is the reason why the returned data should not be modified.
422 ///
423 /// <P>If the data array in <tt>blk</tt> is <tt>null</tt>, then a new one
424 /// is created if necessary. The implementation of this interface may
425 /// choose to return the same array or a new one, depending on what is more
426 /// efficient. Therefore, the data array in <tt>blk</tt> prior to the
427 /// method call should not be considered to contain the returned data, a
428 /// new array may have been created. Instead, get the array from
429 /// <tt>blk</tt> after the method has returned.
430 ///
431 /// <P>The returned data may have its 'progressive' attribute set. In this
432 /// case the returned data is only an approximation of the "final" data.
433 ///
434 /// </summary>
435 /// <param name="blk">Its coordinates and dimensions specify the area to return,
436 /// relative to the current tile. Some fields in this object are modified
437 /// to return the data.
438 ///
439 /// </param>
440 /// <param name="c">The index of the component from which to get the data.
441 ///
442 /// </param>
443 /// <returns> The requested DataBlk
444 ///
445 /// </returns>
446 /// <seealso cref="getCompData">
447 ///
448 /// </seealso>
449 public override DataBlk getInternCompData(DataBlk out_Renamed, int c)
450 {
451 return getCompData(out_Renamed, c);
452 }
453  
454 /// <summary>Return a suitable String representation of the class instance. </summary>
455 public override System.String ToString()
456 {
457 System.Text.StringBuilder rep = new System.Text.StringBuilder("[ICCProfiler:");
458 System.Text.StringBuilder body = new System.Text.StringBuilder();
459 if (icc != null)
460 {
461 body.Append(eol).Append(CSJ2K.Color.ColorSpace.indent(" ", icc.ToString()));
462 }
463 if (xform != null)
464 {
465 //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'"
466 body.Append(eol).Append(CSJ2K.Color.ColorSpace.indent(" ", xform.ToString()));
467 }
468 rep.Append(CSJ2K.Color.ColorSpace.indent(" ", body));
469 return rep.Append("]").ToString();
470 }
471  
472 /* end class ICCProfiler */
473 static ICCProfiler()
474 {
475 GRAY = RestrictedICCProfile.GRAY;
476 RED = RestrictedICCProfile.RED;
477 GREEN = RestrictedICCProfile.GREEN;
478 BLUE = RestrictedICCProfile.BLUE;
479 }
480 }
481 }