corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /// <summary>**************************************************************************
2 ///
3 /// $Id: PalettizedColorSpaceMapper.java,v 1.2 2002/08/08 14:07:16 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.image;
11 using CSJ2K.j2k.util;
12 using CSJ2K.Color.Boxes;
13 namespace CSJ2K.Color
14 {
15  
16 /// <summary> This class provides decoding of images with palettized colorspaces.
17 /// Here each sample in the input is treated as an index into a color
18 /// palette of triplet sRGB output values.
19 ///
20 /// </summary>
21 /// <seealso cref="jj2000.j2k.colorspace.ColorSpace">
22 /// </seealso>
23 /// <version> 1.0
24 /// </version>
25 /// <author> Bruce A. Kern
26 /// </author>
27 public class PalettizedColorSpaceMapper:ColorSpaceMapper
28 {
29 /// <summary> Returns the number of components in the image.
30 ///
31 /// </summary>
32 /// <returns> The number of components in the image.
33 /// </returns>
34 override public int NumComps
35 {
36 get
37 {
38 return pbox == null?src.NumComps:pbox.NumColumns;
39 }
40  
41 }
42 internal int[] outShiftValueArray;
43 internal int srcChannel = 0;
44  
45 /// <summary>Access to the palette box information. </summary>
46 private PaletteBox pbox;
47  
48 /// <summary> Factory method for creating instances of this class.</summary>
49 /// <param name="src">-- source of image data
50 /// </param>
51 /// <param name="csMap">-- provides colorspace info
52 /// </param>
53 /// <returns> PalettizedColorSpaceMapper instance
54 /// </returns>
55 public static new BlkImgDataSrc createInstance(BlkImgDataSrc src, ColorSpace csMap)
56 {
57 return new PalettizedColorSpaceMapper(src, csMap);
58 }
59  
60 /// <summary> Ctor which creates an ICCProfile for the image and initializes
61 /// all data objects (input, working, and output).
62 ///
63 /// </summary>
64 /// <param name="src">-- Source of image data
65 /// </param>
66 /// <param name="csm">-- provides colorspace info
67 /// </param>
68 protected internal PalettizedColorSpaceMapper(BlkImgDataSrc src, ColorSpace csMap):base(src, csMap)
69 {
70 pbox = csMap.PaletteBox;
71 initialize();
72 }
73  
74 /// <summary>General utility used by ctors </summary>
75 private void initialize()
76 {
77 if (ncomps != 1 && ncomps != 3)
78 throw new ColorSpaceException("wrong number of components (" + ncomps + ") for palettized image");
79  
80 int outComps = NumComps;
81 outShiftValueArray = new int[outComps];
82  
83 for (int i = 0; i < outComps; i++)
84 {
85 outShiftValueArray[i] = 1 << (getNomRangeBits(i) - 1);
86 }
87 }
88  
89  
90 /// <summary> Returns, in the blk argument, a block of image data containing the
91 /// specifed rectangular area, in the specified component. The data is
92 /// returned, as a copy of the internal data, therefore the returned data
93 /// can be modified "in place".
94 ///
95 /// <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w'
96 /// and 'h' members of the 'blk' argument, relative to the current
97 /// tile. These members are not modified by this method. The 'offset' of
98 /// the returned data is 0, and the 'scanw' is the same as the block's
99 /// width. See the 'DataBlk' class.
100 ///
101 /// <P>If the data array in 'blk' is 'null', then a new one is created. If
102 /// the data array is not 'null' then it is reused, and it must be large
103 /// enough to contain the block's data. Otherwise an 'ArrayStoreException'
104 /// or an 'IndexOutOfBoundsException' is thrown by the Java system.
105 ///
106 /// <P>The returned data has its 'progressive' attribute set to that of the
107 /// input data.
108 ///
109 /// </summary>
110 /// <param name="blk">Its coordinates and dimensions specify the area to
111 /// return. If it contains a non-null data array, then it must have the
112 /// correct dimensions. If it contains a null data array a new one is
113 /// created. The fields in this object are modified to return the data.
114 ///
115 /// </param>
116 /// <param name="c">The index of the component from which to get the data. Only 0
117 /// and 3 are valid.
118 ///
119 /// </param>
120 /// <returns> The requested DataBlk
121 ///
122 /// </returns>
123 /// <seealso cref="getInternCompData">
124 ///
125 /// </seealso>
126 public override DataBlk getCompData(DataBlk out_Renamed, int c)
127 {
128  
129 if (pbox == null)
130 return src.getCompData(out_Renamed, c);
131  
132 if (ncomps != 1)
133 {
134 System.String msg = "PalettizedColorSpaceMapper: color palette " + "_not_ applied, incorrect number (" + System.Convert.ToString(ncomps) + ") of components";
135 FacilityManager.getMsgLogger().printmsg(CSJ2K.j2k.util.MsgLogger_Fields.WARNING, msg);
136 return src.getCompData(out_Renamed, c);
137 }
138  
139 // Initialize general input and output indexes
140 int leftedgeOut = - 1; // offset to the start of the output scanline
141 int rightedgeOut = - 1; // offset to the end of the output
142 // scanline + 1
143 int leftedgeIn = - 1; // offset to the start of the input scanline
144 int rightedgeIn = - 1; // offset to the end of the input
145 // scanline + 1
146 int kOut = - 1;
147 int kIn = - 1;
148  
149 // Assure a properly sized data buffer for output.
150 InternalBuffer = out_Renamed;
151  
152 switch (out_Renamed.DataType)
153 {
154  
155 // Int and Float data only
156 case DataBlk.TYPE_INT:
157  
158 copyGeometry(inInt[0], out_Renamed);
159  
160 // Request data from the source.
161 inInt[0] = (DataBlkInt) src.getInternCompData(inInt[0], 0);
162 dataInt[0] = (int[]) inInt[0].Data;
163 int[] outdataInt = ((DataBlkInt) out_Renamed).DataInt;
164  
165 // The nitty-gritty.
166  
167 for (int row = 0; row < out_Renamed.h; ++row)
168 {
169 leftedgeIn = inInt[0].offset + row * inInt[0].scanw;
170 rightedgeIn = leftedgeIn + inInt[0].w;
171 leftedgeOut = out_Renamed.offset + row * out_Renamed.scanw;
172 rightedgeOut = leftedgeOut + out_Renamed.w;
173  
174 for (kOut = leftedgeOut, kIn = leftedgeIn; kIn < rightedgeIn; ++kIn, ++kOut)
175 {
176 outdataInt[kOut] = pbox.getEntry(c, dataInt[0][kIn] + shiftValueArray[0]) - outShiftValueArray[c];
177 }
178 }
179 out_Renamed.progressive = inInt[0].progressive;
180 break;
181  
182  
183 case DataBlk.TYPE_FLOAT:
184  
185 copyGeometry(inFloat[0], out_Renamed);
186  
187 // Request data from the source.
188 inFloat[0] = (DataBlkFloat) src.getInternCompData(inFloat[0], 0);
189 dataFloat[0] = (float[]) inFloat[0].Data;
190 float[] outdataFloat = ((DataBlkFloat) out_Renamed).DataFloat;
191  
192 // The nitty-gritty.
193  
194 for (int row = 0; row < out_Renamed.h; ++row)
195 {
196 leftedgeIn = inFloat[0].offset + row * inFloat[0].scanw;
197 rightedgeIn = leftedgeIn + inFloat[0].w;
198 leftedgeOut = out_Renamed.offset + row * out_Renamed.scanw;
199 rightedgeOut = leftedgeOut + out_Renamed.w;
200  
201 for (kOut = leftedgeOut, kIn = leftedgeIn; kIn < rightedgeIn; ++kIn, ++kOut)
202 {
203 //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
204 outdataFloat[kOut] = pbox.getEntry(c, (int) dataFloat[0][kIn] + shiftValueArray[0]) - outShiftValueArray[c];
205 }
206 }
207 out_Renamed.progressive = inFloat[0].progressive;
208 break;
209  
210  
211 case DataBlk.TYPE_SHORT:
212 case DataBlk.TYPE_BYTE:
213 default:
214 // Unsupported output type.
215 throw new System.ArgumentException("invalid source datablock" + " type");
216 }
217  
218 // Initialize the output block geometry and set the profiled
219 // data into the output block.
220 out_Renamed.offset = 0;
221 out_Renamed.scanw = out_Renamed.w;
222 return out_Renamed;
223 }
224  
225  
226 /// <summary>Return a suitable String representation of the class instance, e.g.
227 /// <p>
228 /// [PalettizedColorSpaceMapper
229 /// ncomps= 3, scomp= 1, nentries= 1024
230 /// column=0, 7 bit signed entry
231 /// column=1, 7 bit unsigned entry
232 /// column=2, 7 bit signed entry]
233 /// <p>
234 ///
235 /// </summary>
236 public override System.String ToString()
237 {
238  
239 int c;
240 System.Text.StringBuilder rep = new System.Text.StringBuilder("[PalettizedColorSpaceMapper ");
241 System.Text.StringBuilder body = new System.Text.StringBuilder(" " + eol);
242  
243 if (pbox != null)
244 {
245 body.Append("ncomps= ").Append(NumComps).Append(", scomp= ").Append(srcChannel);
246 for (c = 0; c < NumComps; ++c)
247 {
248 body.Append(eol).Append("column= ").Append(c).Append(", ").Append(pbox.getBitDepth(c)).Append(" bit ").Append(pbox.isSigned(c)?"signed entry":"unsigned entry");
249 }
250 }
251 else
252 {
253 body.Append("image does not contain a palette box");
254 }
255  
256 rep.Append(ColorSpace.indent(" ", body));
257 return rep.Append("]").ToString();
258 }
259  
260  
261 /// <summary> Returns, in the blk argument, a block of image data containing the
262 /// specifed rectangular area, in the specified component. The data is
263 /// returned, as a reference to the internal data, if any, instead of as a
264 /// copy, therefore the returned data should not be modified.
265 ///
266 /// <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w'
267 /// and 'h' members of the 'blk' argument, relative to the current
268 /// tile. These members are not modified by this method. The 'offset' and
269 /// 'scanw' of the returned data can be arbitrary. See the 'DataBlk' class.
270 ///
271 /// <P>This method, in general, is more efficient than the 'getCompData()'
272 /// method since it may not copy the data. However if the array of returned
273 /// data is to be modified by the caller then the other method is probably
274 /// preferable.
275 ///
276 /// <P>If possible, the data in the returned 'DataBlk' should be the
277 /// internal data itself, instead of a copy, in order to increase the data
278 /// transfer efficiency. However, this depends on the particular
279 /// implementation (it may be more convenient to just return a copy of the
280 /// data). This is the reason why the returned data should not be modified.
281 ///
282 /// <P>If the data array in <tt>blk</tt> is <tt>null</tt>, then a new one
283 /// is created if necessary. The implementation of this interface may
284 /// choose to return the same array or a new one, depending on what is more
285 /// efficient. Therefore, the data array in <tt>blk</tt> prior to the
286 /// method call should not be considered to contain the returned data, a
287 /// new array may have been created. Instead, get the array from
288 /// <tt>blk</tt> after the method has returned.
289 ///
290 /// <P>The returned data may have its 'progressive' attribute set. In this
291 /// case the returned data is only an approximation of the "final" data.
292 ///
293 /// </summary>
294 /// <param name="blk">Its coordinates and dimensions specify the area to return,
295 /// relative to the current tile. Some fields in this object are modified
296 /// to return the data.
297 ///
298 /// </param>
299 /// <param name="c">The index of the component from which to get the data.
300 ///
301 /// </param>
302 /// <returns> The requested DataBlk
303 ///
304 /// </returns>
305 /// <seealso cref="getCompData">
306 /// </seealso>
307 public override DataBlk getInternCompData(DataBlk out_Renamed, int c)
308 {
309 return getCompData(out_Renamed, c);
310 }
311  
312 /// <summary> Returns the number of bits, referred to as the "range bits",
313 /// corresponding to the nominal range of the image data in the specified
314 /// component. If this number is <i>n</b> then for unsigned data the
315 /// nominal range is between 0 and 2^b-1, and for signed data it is between
316 /// -2^(b-1) and 2^(b-1)-1. In the case of transformed data which is not in
317 /// the image domain (e.g., wavelet coefficients), this method returns the
318 /// "range bits" of the image data that generated the coefficients.
319 ///
320 /// </summary>
321 /// <param name="c">The index of the component.
322 ///
323 /// </param>
324 /// <returns> The number of bits corresponding to the nominal range of the
325 /// image data (in the image domain).
326 /// </returns>
327 public override int getNomRangeBits(int c)
328 {
329 return pbox == null?src.getNomRangeBits(c):pbox.getBitDepth(c);
330 }
331  
332  
333 /// <summary> Returns the component subsampling factor in the horizontal direction,
334 /// for the specified component. This is, approximately, the ratio of
335 /// dimensions between the reference grid and the component itself, see the
336 /// 'ImgData' interface desription for details.
337 ///
338 /// </summary>
339 /// <param name="c">The index of the component (between 0 and N-1)
340 ///
341 /// </param>
342 /// <returns> The horizontal subsampling factor of component 'c'
343 ///
344 /// </returns>
345 /// <seealso cref="jj2000.j2k.image.ImgData">
346 ///
347 /// </seealso>
348 public override int getCompSubsX(int c)
349 {
350 return imgdatasrc.getCompSubsX(srcChannel);
351 }
352  
353 /// <summary> Returns the component subsampling factor in the vertical direction, for
354 /// the specified component. This is, approximately, the ratio of
355 /// dimensions between the reference grid and the component itself, see the
356 /// 'ImgData' interface desription for details.
357 ///
358 /// </summary>
359 /// <param name="c">The index of the component (between 0 and N-1)
360 ///
361 /// </param>
362 /// <returns> The vertical subsampling factor of component 'c'
363 ///
364 /// </returns>
365 /// <seealso cref="jj2000.j2k.image.ImgData">
366 ///
367 /// </seealso>
368 public override int getCompSubsY(int c)
369 {
370 return imgdatasrc.getCompSubsY(srcChannel);
371 }
372  
373 /// <summary> Returns the width in pixels of the specified tile-component
374 ///
375 /// </summary>
376 /// <param name="t">Tile index
377 ///
378 /// </param>
379 /// <param name="c">The index of the component, from 0 to N-1.
380 ///
381 /// </param>
382 /// <returns> The width in pixels of component <tt>c</tt> in tile<tt>t</tt>.
383 ///
384 /// </returns>
385 public override int getTileCompWidth(int t, int c)
386 {
387 return imgdatasrc.getTileCompWidth(t, srcChannel);
388 }
389  
390 /// <summary> Returns the height in pixels of the specified tile-component.
391 ///
392 /// </summary>
393 /// <param name="t">The tile index.
394 ///
395 /// </param>
396 /// <param name="c">The index of the component, from 0 to N-1.
397 ///
398 /// </param>
399 /// <returns> The height in pixels of component <tt>c</tt> in tile
400 /// <tt>t</tt>.
401 ///
402 /// </returns>
403 public override int getTileCompHeight(int t, int c)
404 {
405 return imgdatasrc.getTileCompHeight(t, srcChannel);
406 }
407  
408 /// <summary> Returns the width in pixels of the specified component in the overall
409 /// image.
410 ///
411 /// </summary>
412 /// <param name="c">The index of the component, from 0 to N-1.
413 ///
414 /// </param>
415 /// <returns> The width in pixels of component <tt>c</tt> in the overall
416 /// image.
417 ///
418 /// </returns>
419 public override int getCompImgWidth(int c)
420 {
421 return imgdatasrc.getCompImgWidth(srcChannel);
422 }
423  
424  
425 /// <summary> Returns the number of bits, referred to as the "range bits",
426 /// corresponding to the nominal range of the image data in the specified
427 /// component. If this number is <i>n</b> then for unsigned data the
428 /// nominal range is between 0 and 2^b-1, and for signed data it is between
429 /// -2^(b-1) and 2^(b-1)-1. In the case of transformed data which is not in
430 /// the image domain (e.g., wavelet coefficients), this method returns the
431 /// "range bits" of the image data that generated the coefficients.
432 ///
433 /// </summary>
434 /// <param name="c">The index of the component.
435 ///
436 /// </param>
437 /// <returns> The number of bits corresponding to the nominal range of the
438 /// image data (in the image domain).
439 ///
440 /// </returns>
441 public override int getCompImgHeight(int c)
442 {
443 return imgdatasrc.getCompImgHeight(srcChannel);
444 }
445  
446 /// <summary> Returns the horizontal coordinate of the upper-left corner of the
447 /// specified component in the current tile.
448 ///
449 /// </summary>
450 /// <param name="c">The index of the component.
451 ///
452 /// </param>
453 public override int getCompULX(int c)
454 {
455 return imgdatasrc.getCompULX(srcChannel);
456 }
457  
458 /// <summary> Returns the vertical coordinate of the upper-left corner of the
459 /// specified component in the current tile.
460 ///
461 /// </summary>
462 /// <param name="c">The index of the component.
463 ///
464 /// </param>
465 public override int getCompULY(int c)
466 {
467 return imgdatasrc.getCompULY(srcChannel);
468 }
469  
470 /* end class PalettizedColorSpaceMapper */
471 }
472 }