Changeset 5962


Ignore:
Timestamp:
02/26/10 06:02:39 (10 years ago)
Author:
callan
Message:

#466 Made TiffCompression enum instances a little bit more elegant and object oriented

Location:
branches/cleanup/components/bio-formats
Files:
4 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/cleanup/components/bio-formats/src/loci/formats/codec/LuraWaveCodec.java

    r5943 r5962  
    6363 
    6464  /** 
    65    * Default constructor. 
     65   * Initializes the LuraWave dependency service. This is called at the 
     66   * beginning of the {@link #decompress()} method to avoid having the 
     67   * constructor's method definition contain a checked exception. 
    6668   * @throws FormatException If there is an error initializing LuraWave 
    6769   * services. 
    6870   */ 
    69   public LuraWaveCodec() throws FormatException { 
     71  private void initialize() throws FormatException { 
     72    if (service != null) { 
     73      return; 
     74    } 
    7075    try { 
    7176      ServiceFactory factory = new ServiceFactory(); 
     
    102107    throws FormatException 
    103108  { 
     109    initialize(); 
    104110    BufferedInputStream stream =  
    105111      new BufferedInputStream(new ByteArrayInputStream(buf), 4096); 
  • branches/cleanup/components/bio-formats/src/loci/formats/gui/AWTTiffTools.java

    r5957 r5962  
    3535import loci.formats.FormatException; 
    3636import loci.formats.FormatTools; 
     37import loci.formats.codec.CodecOptions; 
    3738import loci.formats.tiff.IFD; 
    3839import loci.formats.tiff.PhotoInterp; 
     
    196197      TiffCompression.difference(strips[i], 
    197198        bps, width, planarConfig, predictor); 
    198       strips[i] = TiffCompression.compress(strips[i], ifd); 
     199      CodecOptions options = compression.getCompressionCodecOptions(ifd); 
     200      strips[i] = compression.compress(strips[i], options); 
    199201    } 
    200202 
  • branches/cleanup/components/bio-formats/src/loci/formats/tiff/TiffCompression.java

    r5957 r5962  
    3232import loci.common.DataTools; 
    3333import loci.formats.FormatException; 
     34import loci.formats.codec.BaseCodec; 
    3435import loci.formats.codec.Codec; 
    3536import loci.formats.codec.CodecOptions; 
     
    4142import loci.formats.codec.NikonCodec; 
    4243import loci.formats.codec.PackbitsCodec; 
     44import loci.formats.codec.PassthroughCodec; 
    4345import loci.formats.codec.ZlibCodec; 
    4446 
     
    6062public enum TiffCompression implements CodedEnum { 
    6163 
    62   UNCOMPRESSED(1, "Uncompressed"), 
    63   CCITT_1D(2, "CCITT Group 3 1-Dimensional Modified Huffman"), 
    64   GROUP_3_FAX(3, "CCITT T.4 bi-level encoding (Group 3 Fax)"), 
    65   GROUP_4_FAX(4, "CCITT T.6 bi-level encoding (Group 4 Fax)"), 
    66   LZW(5, "LZW"), 
     64  // (TIFF code, codec, codec name) 
     65  UNCOMPRESSED(1, new PassthroughCodec(), "Uncompressed"), 
     66  CCITT_1D(2, null, "CCITT Group 3 1-Dimensional Modified Huffman"), 
     67  GROUP_3_FAX(3, null, "CCITT T.4 bi-level encoding (Group 3 Fax)"), 
     68  GROUP_4_FAX(4, null, "CCITT T.6 bi-level encoding (Group 4 Fax)"), 
     69  LZW(5, new LZWCodec(), "LZW"), 
    6770  //JPEG(6), 
    68   JPEG(7, "JPEG"), 
    69   PACK_BITS(32773, "PackBits"), 
    70   PROPRIETARY_DEFLATE(32946, "Deflate (Zlib)"), 
    71   DEFLATE(8, "Deflate (Zlib)"), 
    72   THUNDERSCAN(32809, "Thunderscan"), 
    73   JPEG_2000(33003, "JPEG-2000"), 
    74   JPEG_2000_LOSSY(33004, "JPEG-2000 Lossy"), 
    75   ALT_JPEG(33007, "JPEG"), 
    76   NIKON(34713, "Nikon"), 
    77   LURAWAVE(65535, "LuraWave"); 
     71  JPEG(7, new JPEGCodec(), "JPEG"), 
     72  PACK_BITS(32773, new PackbitsCodec(), "PackBits"), 
     73  PROPRIETARY_DEFLATE(32946, new ZlibCodec(), "Deflate (Zlib)"), 
     74  DEFLATE(8, new ZlibCodec(), "Deflate (Zlib)"), 
     75  THUNDERSCAN(32809, null, "Thunderscan"), 
     76  JPEG_2000(33003, new JPEG2000Codec(), "JPEG-2000") { 
     77    @Override 
     78    public CodecOptions getCompressionCodecOptions(IFD ifd) 
     79        throws FormatException { 
     80      CodecOptions options = super.getCompressionCodecOptions(ifd); 
     81      options.lossless = true; 
     82      return JPEG2000CodecOptions.getDefaultOptions(options); 
     83    } 
     84  }, 
     85  JPEG_2000_LOSSY(33004, new JPEG2000Codec(), "JPEG-2000 Lossy") { 
     86    @Override 
     87    public CodecOptions getCompressionCodecOptions(IFD ifd) 
     88        throws FormatException { 
     89      CodecOptions options = super.getCompressionCodecOptions(ifd); 
     90      options.lossless = false; 
     91      return JPEG2000CodecOptions.getDefaultOptions(options); 
     92    } 
     93  }, 
     94  ALT_JPEG(33007, new JPEGCodec(), "JPEG"), 
     95  NIKON(34713, new NikonCodec(), "Nikon"), 
     96  LURAWAVE(65535, new LuraWaveCodec(), "LuraWave"); 
    7897 
    7998  // -- Constants -- 
     
    84103  /** Code for the TIFF compression in the actual TIFF file. */ 
    85104  private int code; 
     105 
     106  /** TIFF compression codec. */ 
     107  private BaseCodec codec; 
    86108 
    87109  /** Name of the TIFF compression codec. */ 
     
    103125   * Default constructor. 
    104126   * @param code Integer "code" for the TIFF compression type. 
     127   * @param codec TIFF compression codec. 
    105128   * @param codecName String name of the compression type. 
    106129   */ 
    107   private TiffCompression(int code, String codecName) { 
     130  private TiffCompression(int code, BaseCodec codec, String codecName) { 
    108131    this.code = code; 
     132    this.codec = codec; 
    109133    this.codecName = codecName; 
    110134  } 
     
    126150  } 
    127151 
    128    
    129152  /** 
    130153   * Retrieves the name of the TIFF compression codec. 
     
    137160  // -- TiffCompression methods - decompression -- 
    138161 
    139   /** Returns true if the given decompression scheme is supported. */ 
    140   public boolean isSupportedDecompression() { 
    141     return 
    142       this == UNCOMPRESSED || 
    143       this == LZW || 
    144       this == JPEG || 
    145       this == ALT_JPEG || 
    146       this == JPEG_2000 || 
    147       this == JPEG_2000_LOSSY || 
    148       this == PACK_BITS || 
    149       this == PROPRIETARY_DEFLATE || 
    150       this == DEFLATE || 
    151       this == NIKON || 
    152       this == LURAWAVE; 
    153   } 
    154  
    155   /** Decodes a strip of data compressed with the given compression scheme. */ 
    156   public byte[] uncompress(byte[] input, CodecOptions options) 
     162  /** Decodes a strip of data. */ 
     163  public byte[] decompress(byte[] input, CodecOptions options) 
    157164    throws FormatException, IOException 
    158165  { 
    159     if (!isSupportedDecompression()) { 
    160       String compressionName = getCodecName(); 
    161       String message = null; 
    162       if (compressionName != null) { 
    163         message = 
    164           "Sorry, " + compressionName + " compression mode is not supported"; 
    165       } 
    166       else message = "Unknown Compression type (" + this + ")"; 
    167       throw new FormatException(message); 
    168     } 
    169  
    170     Codec codec = null; 
    171  
    172     if (this == UNCOMPRESSED) return input; 
    173     else if (this == LZW) codec = new LZWCodec(); 
    174     else if (this == JPEG || this == ALT_JPEG) { 
    175       codec = new JPEGCodec(); 
    176     } 
    177     else if (this == JPEG_2000 || this == JPEG_2000_LOSSY) { 
    178       codec = new JPEG2000Codec(); 
    179     } 
    180     else if (this == PACK_BITS) codec = new PackbitsCodec(); 
    181     else if (this == PROPRIETARY_DEFLATE || this == DEFLATE) { 
    182       codec = new ZlibCodec(); 
    183     } 
    184     else if (this == NIKON) codec = new NikonCodec(); 
    185     else if (this == LURAWAVE) codec = new LuraWaveCodec(); 
    186     if (codec != null) return codec.decompress(input, options); 
    187     throw new FormatException("Unhandled compression (" + this + ")"); 
     166    if (codec == null) { 
     167      throw new FormatException( 
     168          "Sorry, " + getCodecName() + " compression mode is not supported"); 
     169    } 
     170    return codec.decompress(input, options); 
    188171  } 
    189172 
     
    225208 
    226209  // -- TiffCompression methods - compression -- 
    227  
    228   /** Returns true if the given compression scheme is supported. */ 
    229   public boolean isSupportedCompression() { 
    230     return this == UNCOMPRESSED || this == LZW || this == JPEG || 
    231       this == JPEG_2000 || this == JPEG_2000_LOSSY; 
    232   } 
    233  
    234   /** Encodes a strip of data with the given compression scheme. */ 
    235   public static byte[] compress(byte[] input, IFD ifd) 
    236     throws FormatException, IOException 
    237   { 
    238     TiffCompression compression = get(ifd.getIFDIntValue( 
    239         IFD.COMPRESSION, false, UNCOMPRESSED.getCode())); 
    240  
    241     if (!compression.isSupportedCompression()) { 
    242       String compressionName = compression.getCodecName(); 
    243       if (compressionName != null) { 
    244         throw new FormatException("Sorry, " + compressionName + 
    245           " compression mode is not supported"); 
    246       } 
    247       else { 
    248         throw new FormatException( 
    249           "Unknown Compression type (" + compression + ")"); 
    250       } 
    251     } 
    252  
     210   
     211  /** 
     212   * Creates a set of codec options for compression. 
     213   * @param ifd The IFD to create codec options for. 
     214   * @return A new codec options instance populated using metadata from 
     215   * <code>ifd</code>. 
     216   */ 
     217  public CodecOptions getCompressionCodecOptions(IFD ifd) 
     218    throws FormatException{ 
    253219    CodecOptions options = new CodecOptions(); 
    254220    options.width = (int) ifd.getImageWidth(); 
     
    259225    options.interleaved = true; 
    260226    options.signed = false; 
    261  
    262     if (compression == UNCOMPRESSED) return input; 
    263     else if (compression == LZW) { 
    264       return new LZWCodec().compress(input, options); 
    265     } 
    266     else if (compression == JPEG) { 
    267       return new JPEGCodec().compress(input, options); 
    268     } 
    269     else if (compression == JPEG_2000) { 
    270       options.lossless = true; 
    271       options = JPEG2000CodecOptions.getDefaultOptions(options); 
    272       return new JPEG2000Codec().compress(input, options); 
    273     } 
    274     else if (compression == JPEG_2000_LOSSY) { 
    275       options.lossless = false; 
    276       options = JPEG2000CodecOptions.getDefaultOptions(options); 
    277       return new JPEG2000Codec().compress(input, options); 
    278     } 
    279     throw new FormatException("Unhandled compression (" + compression + ")"); 
     227    return options; 
     228  } 
     229 
     230  /** Encodes a strip of data. */ 
     231  public byte[] compress(byte[] input, CodecOptions options) 
     232    throws FormatException, IOException 
     233  { 
     234    if (codec == null) { 
     235      throw new FormatException( 
     236          "Sorry, " + getCodecName() + " compression mode is not supported"); 
     237    } 
     238    return codec.compress(input, options); 
    280239  } 
    281240 
  • branches/cleanup/components/bio-formats/src/loci/formats/tiff/TiffParser.java

    r5958 r5962  
    544544      System.arraycopy(jpegTable, 0, q, 0, jpegTable.length - 2); 
    545545      System.arraycopy(tile, 2, q, jpegTable.length - 2, tile.length - 2); 
    546       tile = compression.uncompress(q, options); 
    547     } 
    548     else tile = compression.uncompress(tile, options); 
     546      tile = compression.decompress(q, options); 
     547    } 
     548    else tile = compression.decompress(tile, options); 
    549549 
    550550    TiffCompression.undifference(tile, ifd); 
  • branches/cleanup/components/bio-formats/test/loci/formats/utests/testng.xml

    r5944 r5962  
    1 <suite name="Service Tests"> 
     1<suite name="BioFormats Unit Tests"> 
    22    <test name="ServiceTests"> 
    33        <groups/> 
     
    1919        <packages/> 
    2020    </test> 
     21    <test name="TIFFTests"> 
     22        <groups/> 
     23        <classes/> 
     24        <packages> 
     25            <package name="loci.formats.utests.tiff"/> 
     26        </packages> 
     27    </test> 
    2128</suite> 
Note: See TracChangeset for help on using the changeset viewer.