Changeset 2071


Ignore:
Timestamp:
01/11/07 09:00:46 (13 years ago)
Author:
nor
Message:

Changes to API and "Object-orientation" due to BaseCompressor.java, also added x, y to arguments for compress method after discussion with Curtis.

Location:
trunk/loci/formats
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/Base64Encoder.java

    r2063 r2071  
    2525package loci.formats; 
    2626 
    27 import java.util.Random; 
    28  
    2927/** 
    3028 * Implements encoding (compress) and decoding (decompress) methods for 
    3129 * Base64. 
    3230 */ 
    33 public class Base64Encoder implements Compressor { 
     31public class Base64Encoder extends BaseCompressor implements Compressor { 
    3432 
    3533  // Base64 alphabet and codes 
     
    7573 
    7674  /** 
    77    * Encodes a block of data into Base64 format. 
    78    * 
    79    * @param input the data to be encoded 
    80    * @param dims ignored 
    81    * @param options ignored 
    82    * @return The encoded data 
    83    */ 
    84   public byte[] compress(byte[] input, int[] dims, Object options) { 
     75   * Encodes a block of data into Base64. 
     76   * 
     77   * @param input the data to be encoded. 
     78   * @param x ignored. 
     79   * @param y ignored. 
     80   * @param dims ignored. 
     81   * @param options ignored. 
     82   * @return The encoded data. 
     83   */ 
     84  public byte[] compress(byte[] input, int x, int y, int[] dims, 
     85    Object options) throws FormatException 
     86  { 
    8587    int dataBits = input.length * 8; 
    8688    int fewerThan24 = dataBits % 24; 
     
    155157 
    156158  /** 
    157    * Encodes a block of data into Base64 format. 
    158    * This method simply concatenates data[0] + data[1] + ... + data[i] into 
    159    * a 1D block of data, then calls the 1D version of compress. 
    160    * 
    161    * @param data the data to be encoded 
    162    * @param dims ignored 
    163    * @param options ignored 
    164    * @return The compressed data 
    165    */ 
    166   public byte[] compress(byte[][] data, int[] dims, Object options) { 
    167     int len = 0; 
    168     for(int i = 0; i < data.length; i++) { 
    169       len += data[i].length; 
    170     } 
    171     byte[] toCompress = new byte[len]; 
    172     int curPos = 0; 
    173     for(int i = 0; i < data.length; i++) { 
    174       System.arraycopy(data[i], 0, toCompress, curPos, data[i].length); 
    175       curPos += data[i].length; 
    176     } 
    177     return compress(toCompress, dims, options); 
    178   } 
    179  
    180   /** 
    181159   * Decompresses a block of data. 
    182160   * 
     
    237215 
    238216  /** 
    239    * Decodes a Base64 data block. 
    240    * This method simply concatenates data[0] + data[1] + ... + data[i] into 
    241    * a 1D block of data, then calls the 1D version of decompress. 
    242    * 
    243    * @param data the data to be decoded 
    244    * @return The decoded data 
    245    * @throws FormatException If input is not a valid Base64 data block. 
    246    */ 
    247   public byte[] decompress(byte[][] data) throws FormatException { 
    248     int len = 0; 
    249     for(int i = 0; i < data.length; i++) { 
    250       len += data[i].length; 
    251     } 
    252     byte[] toDecompress = new byte[len]; 
    253     int curPos = 0; 
    254     for(int i = 0; i < data.length; i++) { 
    255       System.arraycopy(data[i], 0, toDecompress, curPos, data[i].length); 
    256       curPos += data[i].length; 
    257     } 
    258     return decompress(toDecompress); 
    259   } 
    260  
    261   /** 
    262217   * Decodes a Base64 String by converting to bytes and passing to the 
    263218   * decompress method. 
     
    272227  } 
    273228 
    274     /** 
    275    * Main testing method. 
     229  /** 
     230   * Main testing method. test is inherited from parent class. 
    276231   * 
    277232   * @param args ignored 
     
    279234   *                         compress method. 
    280235   */ 
     236 
    281237  public static void main(String[] args) throws FormatException { 
    282     byte[] testdata = new byte[50000]; 
    283     Random r = new Random(); 
    284     System.out.println("Generating random data"); 
    285     r.nextBytes(testdata); 
    286     Base64Encoder c = new Base64Encoder(); 
    287     System.out.println("Compressing data"); 
    288     byte[] compressed = c.compress(testdata, null, null); 
    289     System.out.println("Decompressing data"); 
    290     byte[] decompressed = c.decompress(compressed); 
    291     System.out.print("Comparing data... "); 
    292     if(testdata.length != decompressed.length) { 
    293       System.out.println("Test data differs in length from uncompressed data"); 
    294       System.out.println("Exiting..."); 
    295       System.exit(-1); 
    296     } 
    297     else { 
    298       boolean equalsFlag = true; 
    299       for(int i = 0; i < testdata.length; i++) { 
    300         if(testdata[i] != decompressed[i]) { 
    301           System.out.println("Test data and uncompressed data differs at byte" + 
    302                              i); 
    303           equalsFlag = false; 
    304         } 
    305       } 
    306       if(!equalsFlag) { 
    307         System.out.println("Comparison failed. \nExiting..."); 
    308         System.exit(-1); 
    309       } 
    310     } 
    311     System.out.println("Success."); 
    312     System.out.println("Generating 2D byte array test"); 
    313     byte[][] twoDtest = new byte[100][500]; 
    314     for(int i = 0; i < 100; i++) { 
    315       System.arraycopy(testdata, 500*i, twoDtest[i], 0, 500); 
    316     } 
    317     byte[] twoDcompressed = c.compress(twoDtest, null, null); 
    318     System.out.print("Comparing compressed data... "); 
    319     if(twoDcompressed.length != compressed.length) { 
    320       System.out.println("1D and 2D compressed data not same length"); 
    321       System.out.println("Exiting..."); 
    322       System.exit(-1); 
    323     } 
    324     boolean equalsFlag = true; 
    325     for(int i = 0; i < twoDcompressed.length; i++) { 
    326       if(twoDcompressed[i] != compressed[i]) { 
    327         System.out.println("1D data and 2D compressed data differs at byte" + 
    328                            i); 
    329         equalsFlag = false; 
    330       } 
    331       if(!equalsFlag) { 
    332         System.out.println("Comparison failed. \nExiting..."); 
    333         System.exit(-1); 
    334       } 
    335     } 
    336     System.out.println("Success."); 
    337     System.out.println("Test complete"); 
     238    LZWCompressor c = new LZWCompressor(); 
     239    c.test(); 
    338240  } 
    339241 
  • trunk/loci/formats/Compressor.java

    r2057 r2071  
    4646   * 
    4747   * @param data the data to be compressed 
    48    * @param dims the dimensions (if appropriate) of the contained pixel data 
    49    * @param options an Object representing an options required by the compressor 
     48   * @param x length of the x dimension of the image data, if appropriate 
     49   * @param y length of the y dimension of the image data, if appropriate 
     50   * @param dims the dimensions of the image data, if appropriate 
     51   * @param options options to be used during compression, if appropriate 
    5052   * @return The compressed data 
     53   * @throws FormatException If input is not an LZW-compressed data block. 
    5154   */ 
    52   byte[] compress(byte[] data, int[] dims, Object options); 
     55  byte[] compress(byte[] data, int x, int y, 
     56      int[] dims, Object options) throws FormatException; 
    5357 
    5458  /** 
     
    5660   * 
    5761   * @param data the data to be compressed 
    58    * @param dims the dimensions (if appropriate) of the contained pixel data 
    59    * @param options an Object representing an options required by the compressor 
     62   * @param x length of the x dimension of the image data, if appropriate 
     63   * @param y length of the y dimension of the image data, if appropriate 
     64   * @param dims the dimensions of the image data, if appropriate 
     65   * @param options options to be used during compression, if appropriate 
    6066   * @return The compressed data 
     67   * @throws FormatException If input is not an LZW-compressed data block. 
    6168   */ 
    62   byte[] compress(byte[][] data, int[] dims, Object options); 
     69  byte[] compress(byte[][] data, int x, int y, 
     70      int[] dims, Object options) throws FormatException; 
    6371 
    6472  /** 
  • trunk/loci/formats/LZWCompressor.java

    r2058 r2071  
    2525package loci.formats; 
    2626 
    27 import java.util.Random; 
    28  
    2927/** 
    3028 * Implements basic LZW compression and decompression, as outlined in the 
     
    3331 * 
    3432 */ 
    35 public class LZWCompressor implements Compressor { 
     33public class LZWCompressor extends BaseCompressor implements Compressor { 
    3634 
    3735  // LZW compression codes 
     
    4442   * 
    4543   * @param input the data to be compressed 
     44   * @param x ignored for LZW. 
     45   * @param y ignored for LZW. 
    4646   * @param dims ignored for LZW. 
    4747   * @param options ignored for LZW. 
    4848   * @return The compressed data 
    4949   */ 
    50   public byte[] compress(byte[] input, int[] dims, Object options) { 
     50  public byte[] compress(byte[] input, int x, int y, int[] dims, 
     51    Object options) throws FormatException 
     52  { 
    5153 
    5254    if (input == null || input.length == 0) return input; 
     
    9092 
    9193    return out.toByteArray(); 
    92   } 
    93  
    94   /** 
    95    * Compresses a block of data using LZW compression. 
    96    * This method simply concatenates data[0] + data[1] + ... + data[i] into 
    97    * a 1D block of data, then calls the 1D version of compress. 
    98    * 
    99    * @param data the data to be compressed 
    100    * @param dims ignored for LZW. 
    101    * @param options ignored for LZW. 
    102    * @return The compressed data 
    103    */ 
    104   public byte[] compress(byte[][] data, int[] dims, Object options) { 
    105     int len = 0; 
    106     for(int i = 0; i < data.length; i++) { 
    107       len += data[i].length; 
    108     } 
    109     byte[] toCompress = new byte[len]; 
    110     int curPos = 0; 
    111     for(int i = 0; i < data.length; i++) { 
    112       System.arraycopy(data[i], 0, toCompress, curPos, data[i].length); 
    113       curPos += data[i].length; 
    114     } 
    115     return compress(toCompress, dims, options); 
    116   } 
    117  
    118   /** 
    119    * Decodes an LZW-compressed data block. 
    120    * This method simply concatenates data[0] + data[1] + ... + data[i] into 
    121    * a 1D block of data, then calls the 1D version of decompress. 
    122    * 
    123    * @param data the data to be decompressed 
    124    * @return The decompressed data 
    125    * @throws FormatException If input is not an LZW-compressed data block. 
    126    */ 
    127   public byte[] decompress(byte[][] data) throws FormatException { 
    128     int len = 0; 
    129     for(int i = 0; i < data.length; i++) { 
    130       len += data[i].length; 
    131     } 
    132     byte[] toDecompress = new byte[len]; 
    133     int curPos = 0; 
    134     for(int i = 0; i < data.length; i++) { 
    135       System.arraycopy(data[i], 0, toDecompress, curPos, data[i].length); 
    136       curPos += data[i].length; 
    137     } 
    138     return decompress(toDecompress); 
    13994  } 
    14095 
     
    217172   *                         compress method. 
    218173   */ 
     174 
    219175  public static void main(String[] args) throws FormatException { 
    220     byte[] testdata = new byte[50000]; 
    221     Random r = new Random(); 
    222     System.out.println("Generating random data"); 
    223     r.nextBytes(testdata); 
    224176    LZWCompressor c = new LZWCompressor(); 
    225     System.out.println("Compressing data"); 
    226     byte[] compressed = c.compress(testdata, null, null); 
    227     System.out.println("Decompressing data"); 
    228     byte[] decompressed = c.decompress(compressed); 
    229     System.out.print("Comparing data... "); 
    230     if(testdata.length != decompressed.length) { 
    231       System.out.println("Test data differs in length from uncompressed data"); 
    232       System.out.println("Exiting..."); 
    233       System.exit(-1); 
    234     } 
    235     else { 
    236       boolean equalsFlag = true; 
    237       for(int i = 0; i < testdata.length; i++) { 
    238         if(testdata[i] != decompressed[i]) { 
    239           System.out.println("Test data and uncompressed data differs at byte" + 
    240                              i); 
    241           equalsFlag = false; 
    242         } 
    243       } 
    244       if(!equalsFlag) { 
    245         System.out.println("Comparison failed. \nExiting..."); 
    246         System.exit(-1); 
    247       } 
    248     } 
    249     System.out.println("Success."); 
    250     System.out.println("Generating 2D byte array test"); 
    251     byte[][] twoDtest = new byte[100][500]; 
    252     for(int i = 0; i < 100; i++) { 
    253       System.arraycopy(testdata, 500*i, twoDtest[i], 0, 500); 
    254     } 
    255     byte[] twoDcompressed = c.compress(twoDtest, null, null); 
    256     System.out.print("Comparing compressed data... "); 
    257     if(twoDcompressed.length != compressed.length) { 
    258       System.out.println("1D and 2D compressed data not same length"); 
    259       System.out.println("Exiting..."); 
    260       System.exit(-1); 
    261     } 
    262     boolean equalsFlag = true; 
    263     for(int i = 0; i < twoDcompressed.length; i++) { 
    264       if(twoDcompressed[i] != compressed[i]) { 
    265         System.out.println("1D data and 2D compressed data differs at byte" + 
    266                            i); 
    267         equalsFlag = false; 
    268       } 
    269       if(!equalsFlag) { 
    270         System.out.println("Comparison failed. \nExiting..."); 
    271         System.exit(-1); 
    272       } 
    273     } 
    274     System.out.println("Success."); 
    275     System.out.println("Test complete"); 
     177    c.test(); 
    276178  } 
     179 
    277180} 
  • trunk/loci/formats/TiffTools.java

    r2060 r2071  
    24122412    else if (compression == LZW) { 
    24132413      LZWCompressor c = new LZWCompressor(); 
    2414       return c.compress(input, null, null); 
     2414      return c.compress(input, 0, 0, null, null); 
    24152415      // return Compression.lzwCompress(input); 
    24162416    } 
Note: See TracChangeset for help on using the changeset viewer.