001/* 002 * $RCSfile: ImgDataConverter.java,v $ 003 * $Revision: 1.1 $ 004 * $Date: 2005/02/11 05:02:13 $ 005 * $State: Exp $ 006 * 007 * Interface: ImgDataConverter 008 * 009 * Description: The abstract class for classes that provide 010 * Image Data Convertres (int -> float, float->int). 011 * 012 * 013 * 014 * COPYRIGHT: 015 * 016 * This software module was originally developed by Raphaël Grosbois and 017 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel 018 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David 019 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research 020 * Centre France S.A) in the course of development of the JPEG2000 021 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This 022 * software module is an implementation of a part of the JPEG 2000 023 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio 024 * Systems AB and Canon Research Centre France S.A (collectively JJ2000 025 * Partners) agree not to assert against ISO/IEC and users of the JPEG 026 * 2000 Standard (Users) any of their rights under the copyright, not 027 * including other intellectual property rights, for this software module 028 * with respect to the usage by ISO/IEC and Users of this software module 029 * or modifications thereof for use in hardware or software products 030 * claiming conformance to the JPEG 2000 Standard. Those intending to use 031 * this software module in hardware or software products are advised that 032 * their use may infringe existing patents. The original developers of 033 * this software module, JJ2000 Partners and ISO/IEC assume no liability 034 * for use of this software module or modifications thereof. No license 035 * or right to this software module is granted for non JPEG 2000 Standard 036 * conforming products. JJ2000 Partners have full right to use this 037 * software module for his/her own purpose, assign or donate this 038 * software module to any third party and to inhibit third parties from 039 * using this software module for non JPEG 2000 Standard conforming 040 * products. This copyright notice must be included in all copies or 041 * derivative works of this software module. 042 * 043 * Copyright (c) 1999/2000 JJ2000 Partners. 044 * */ 045package jj2000.j2k.image; 046 047 048/** 049 * This class is responsible of all data type conversions. It should be used, 050 * at encoder side, between Tiler and ForwardWT modules and, at decoder side, 051 * between InverseWT/CompDemixer and ImgWriter modules. The conversion is 052 * realized when a block of data is requested: if source and destination data 053 * type are the same one, it does nothing, else appropriate cast is done. All 054 * the methods of the 'ImgData' interface are implemented by the 055 * 'ImgDataAdapter' class that is the superclass of this one, so they don't 056 * need to be reimplemented by subclasses. 057 * */ 058public class ImgDataConverter extends ImgDataAdapter implements BlkImgDataSrc { 059 060 /** The block used to request data from the source in the case that a 061 * conversion seems necessary. It can be either int or float at 062 * initialization time. It will be checked (and corrected if necessary) by 063 * the source whenever necessary */ 064 private DataBlk srcBlk = new DataBlkInt(); 065 066 /** The source of image data */ 067 private BlkImgDataSrc src; 068 069 /** The number of fraction bits in the casted ints */ 070 private int fp; 071 072 /** 073 * Constructs a new ImgDataConverter object that operates on the specified 074 * source of image data. 075 * 076 * @param imgSrc The source from where to get the data to be transformed 077 * 078 * @param fp The number of fraction bits in the casted ints 079 * 080 * @see BlkImgDataSrc 081 * */ 082 public ImgDataConverter(BlkImgDataSrc imgSrc, int fp) { 083 super(imgSrc); 084 src = imgSrc; 085 this.fp=fp; 086 } 087 088 /** 089 * Constructs a new ImgDataConverter object that operates on the specified 090 * source of image data. 091 * 092 * @param imgSrc The source from where to get the data to be transformed 093 * 094 * @see BlkImgDataSrc 095 * */ 096 public ImgDataConverter(BlkImgDataSrc imgSrc) { 097 super(imgSrc); 098 src = imgSrc; 099 fp = 0; 100 } 101 102 /** 103 * Returns the position of the fixed point in the specified 104 * component. This is the position of the least significant integral 105 * (i.e. non-fractional) bit, which is equivalent to the number of 106 * fractional bits. For instance, for fixed-point values with 2 fractional 107 * bits, 2 is returned. For floating-point data this value does not apply 108 * and 0 should be returned. Position 0 is the position of the least 109 * significant bit in the data. 110 * 111 * @param c The index of the component. 112 * 113 * @return The position of the fixed-point, which is the same as the 114 * number of fractional bits. 115 * */ 116 public int getFixedPoint(int c){ 117 return fp; 118 } 119 120 /** 121 * Returns, in the blk argument, a block of image data containing the 122 * specifed rectangular area, in the specified component, using the 123 * 'transfer type' specified in the block given as argument. The data is 124 * returned, as a copy of the internal data, therefore the returned data 125 * can be modified "in place". 126 * 127 * <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w' 128 * and 'h' members of the 'blk' argument, relative to the current 129 * tile. These members are not modified by this method. The 'offset' of 130 * the returned data is 0, and the 'scanw' is the same as the block's 131 * width. See the 'DataBlk' class. 132 * 133 * <P>This method, in general, is less efficient than the 134 * 'getInternCompData()' method since, in general, it copies the 135 * data. However if the array of returned data is to be modified by the 136 * caller then this method is preferable. 137 * 138 * <P>If the data array in 'blk' is 'null', then a new one is created. If 139 * the data array is not 'null' then it is reused, and it must be large 140 * enough to contain the block's data. Otherwise an 'ArrayStoreException' 141 * or an 'IndexOutOfBoundsException' is thrown by the Java system. 142 * 143 * <P>The returned data may have its 'progressive' attribute set. In this 144 * case the returned data is only an approximation of the "final" data. 145 * 146 * @param blk Its coordinates and dimensions specify the area to return, 147 * relative to the current tile. If it contains a non-null data array, 148 * then it must be large enough. If it contains a null data array a new 149 * one is created. Some fields in this object are modified to return the 150 * data. 151 * 152 * @param c The index of the component from which to get the data. 153 * 154 * @see #getInternCompData 155 * */ 156 public DataBlk getCompData(DataBlk blk, int c){ 157 return getData(blk,c,false); 158 } 159 160 /** 161 * Returns, in the blk argument, a block of image data containing the 162 * specifed rectangular area, in the specified component, using the 163 * 'transfer type' defined in the block given as argument. The data is 164 * returned, as a reference to the internal data, if any, instead of as a 165 * copy, therefore the returned data should not be modified. 166 * 167 * <P>The rectangular area to return is specified by the 'ulx', 'uly', 'w' 168 * and 'h' members of the 'blk' argument, relative to the current 169 * tile. These members are not modified by this method. The 'offset' and 170 * 'scanw' of the returned data can be arbitrary. See the 'DataBlk' class. 171 * 172 * <P> If source data and expected data (blk) are using the same type, 173 * block returned without any modification. If not appropriate cast is 174 * used. 175 * 176 * <P>This method, in general, is more efficient than the 'getCompData()' 177 * method since it may not copy the data. However if the array of returned 178 * data is to be modified by the caller then the other method is probably 179 * preferable. 180 * 181 * <P>If the data array in <tt>blk</tt> is <tt>null</tt>, then a new one 182 * is created if necessary. The implementation of this interface may 183 * choose to return the same array or a new one, depending on what is more 184 * efficient. Therefore, the data array in <tt>blk</tt> prior to the 185 * method call should not be considered to contain the returned data, a 186 * new array may have been created. Instead, get the array from 187 * <tt>blk</tt> after the method has returned. 188 * 189 * <P>The returned data may have its 'progressive' attribute set. In this 190 * case the returned data is only an approximation of the "final" data. 191 * 192 * @param blk Its coordinates and dimensions specify the area to return, 193 * relative to the current tile. Some fields in this object are modified 194 * to return the data. 195 * 196 * @param c The index of the component from which to get the data. 197 * 198 * @return The requested DataBlk 199 * 200 * @see #getCompData 201 * */ 202 public final DataBlk getInternCompData(DataBlk blk, int c){ 203 return getData(blk,c,true); 204 } 205 206 /** 207 * Implements the 'getInternCompData()' and the 'getCompData()' 208 * methods. The 'intern' flag signals which of the two methods should run 209 * as. 210 * 211 * @param blk The data block to get. 212 * 213 * @param c The index of the component from which to get the data. 214 * 215 * @param intern If true behave as 'getInternCompData(). Otherwise behave 216 * as 'getCompData()' 217 * 218 * @return The requested data block 219 * 220 * @see #getInternCompData 221 * 222 * @see #getCompData 223 * */ 224 private DataBlk getData(DataBlk blk, int c, boolean intern) { 225 DataBlk reqBlk; // Reference to block used in request to source 226 227 // Keep request data type 228 int otype = blk.getDataType(); 229 230 if (otype == srcBlk.getDataType()) { 231 // Probably requested type is same as source type 232 reqBlk = blk; 233 } 234 else { 235 // Probably requested type is not the same as source type 236 reqBlk = srcBlk; 237 // We need to copy requested coordinates and size 238 reqBlk.ulx = blk.ulx; 239 reqBlk.uly = blk.uly; 240 reqBlk.w = blk.w; 241 reqBlk.h = blk.h; 242 } 243 244 // Get source data block 245 if (intern) { 246 // We can use the intern variant 247 srcBlk = src.getInternCompData(reqBlk,c); 248 } 249 else { 250 // Do not use the intern variant. Note that this is not optimal 251 // since if we are going to convert below then we could have used 252 // the intern variant. But there is currently no way to know if we 253 // will need to do conversion or not before getting the data. 254 srcBlk = src.getCompData(reqBlk,c); 255 } 256 257 // Check if casting is needed 258 if(srcBlk.getDataType()==otype){ 259 return srcBlk; 260 } 261 262 int i; 263 int k,kSrc,kmin; 264 float mult; 265 int w=srcBlk.w; 266 int h=srcBlk.h; 267 268 switch(otype){ 269 case DataBlk.TYPE_FLOAT: // Cast INT -> FLOAT 270 271 float farr[]; 272 int srcIArr[]; 273 274 // Get data array from resulting blk 275 farr = (float[]) blk.getData(); 276 if (farr == null || farr.length < w*h) { 277 farr = new float[w*h]; 278 blk.setData(farr); 279 } 280 281 blk.scanw = srcBlk.w; 282 blk.offset = 0; 283 blk.progressive = srcBlk.progressive; 284 srcIArr=(int[]) srcBlk.getData(); 285 286 // Cast data from source to blk 287 fp=src.getFixedPoint(c); 288 if(fp!=0){ 289 mult=1.0f/(1<<fp); 290 for (i=h-1, k=w*h-1, kSrc = 291 srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) { 292 for (kmin = k-w; k>kmin; k--, kSrc--) { 293 farr[k]=((srcIArr[kSrc]*mult)); 294 } 295 // Jump to geggining of next line in source 296 kSrc -= srcBlk.scanw - w; 297 } 298 } 299 else{ 300 for (i=h-1, k=w*h-1, kSrc = 301 srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) { 302 for (kmin = k-w; k>kmin; k--, kSrc--) { 303 farr[k]=((float)(srcIArr[kSrc])); 304 } 305 // Jump to geggining of next line in source 306 kSrc -= srcBlk.scanw - w; 307 } 308 } 309 break; // End of cast INT-> FLOAT 310 311 case DataBlk.TYPE_INT: // cast FLOAT -> INT 312 int iarr[]; 313 float srcFArr[]; 314 315 // Get data array from resulting blk 316 iarr = (int[]) blk.getData(); 317 if (iarr == null || iarr.length < w*h) { 318 iarr = new int[w*h]; 319 blk.setData(iarr); 320 } 321 blk.scanw = srcBlk.w; 322 blk.offset = 0; 323 blk.progressive = srcBlk.progressive; 324 srcFArr=(float[]) srcBlk.getData(); 325 326 // Cast data from source to blk 327 if(fp!=0){ 328 mult=(float)(1<<fp); 329 for (i=h-1, k=w*h-1, kSrc = 330 srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) { 331 for (kmin = k-w; k>kmin; k--, kSrc--) { 332 if (srcFArr[kSrc] > 0.0f) { 333 iarr[k] = (int) (srcFArr[kSrc]*mult+0.5f); 334 } 335 else { 336 iarr[k] = (int) (srcFArr[kSrc]*mult-0.5f); 337 } 338 } 339 // Jump to geggining of next line in source 340 kSrc -= srcBlk.scanw - w; 341 } 342 } 343 else{ 344 for (i=h-1, k=w*h-1, kSrc = 345 srcBlk.offset+(h-1)*srcBlk.scanw+w-1; i>=0; i--) { 346 for (kmin = k-w; k>kmin; k--, kSrc--) { 347 if (srcFArr[kSrc] > 0.0f) { 348 iarr[k] = (int) (srcFArr[kSrc]+0.5f); 349 } 350 else { 351 iarr[k] = (int) (srcFArr[kSrc]-0.5f); 352 } 353 } 354 // Jump to geggining of next line in source 355 kSrc -= srcBlk.scanw - w; 356 } 357 } 358 break; // End cast FLOAT -> INT 359 default: 360 throw 361 new IllegalArgumentException("Only integer and float data "+ 362 "are "+ 363 "supported by JJ2000"); 364 } 365 return blk; 366 } 367} 368 369