Changeset 6508


Ignore:
Timestamp:
06/08/10 16:46:32 (10 years ago)
Author:
melissa
Message:
  • Added basic TIFF tile export. Closes #413.
  • Throw an exception in writers that don't support tiled export.
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • branches/4.2/components/bio-formats/src/loci/formats/out/APNGWriter.java

    r6328 r6508  
    7575  { 
    7676    checkParams(no, buf, x, y, w, h); 
     77    if (!isFullPlane(x, y, w, h)) { 
     78      throw new FormatException( 
     79        "APNGWriter does not yet support saving image tiles."); 
     80    } 
    7781    MetadataRetrieve meta = getMetadataRetrieve(); 
    7882 
  • branches/4.2/components/bio-formats/src/loci/formats/out/AVIWriter.java

    r6328 r6508  
    104104  { 
    105105    checkParams(no, buf, x, y, w, h); 
     106    if (!isFullPlane(x, y, w, h)) { 
     107      throw new FormatException( 
     108        "AVIWriter does not yet support saving image tiles."); 
     109    } 
    106110    int nChannels = getSamplesPerPixel(); 
    107111 
  • branches/4.2/components/bio-formats/src/loci/formats/out/JPEG2000Writer.java

    r6294 r6508  
    5959  { 
    6060    checkParams(no, buf, x, y, w, h); 
     61    if (!isFullPlane(x, y, w, h)) { 
     62      throw new FormatException( 
     63        "JPEG2000Writer does not yet support saving image tiles."); 
     64    } 
    6165    MetadataRetrieve retrieve = getMetadataRetrieve(); 
    6266    boolean littleEndian = 
  • branches/4.2/components/bio-formats/src/loci/formats/out/JavaWriter.java

    r6328 r6508  
    5959  { 
    6060    checkParams(no, buf, x, y, w, h); 
     61    if (!isFullPlane(x, y, w, h)) { 
     62      throw new FormatException( 
     63        "JavaWriter does not yet support saving image tiles."); 
     64    } 
    6165 
    6266    // check pixel type 
  • branches/4.2/components/bio-formats/src/loci/formats/out/OMEXMLWriter.java

    r6328 r6508  
    121121  { 
    122122    checkParams(no, buf, x, y, w, h); 
     123    if (!isFullPlane(x, y, w, h)) { 
     124      throw new FormatException( 
     125        "OMEXMLWriter does not yet support saving image tiles."); 
     126    } 
    123127    MetadataRetrieve retrieve = getMetadataRetrieve(); 
    124128 
  • branches/4.2/components/bio-formats/src/loci/formats/out/TiffWriter.java

    r6294 r6508  
    178178    out.seek(out.length()); 
    179179    ifd.putIFDValue(IFD.PLANAR_CONFIGURATION, interleaved ? 1 : 2); 
     180    RandomAccessInputStream in = new RandomAccessInputStream(currentId); 
     181    tiffSaver.setInputStream(in); 
    180182    tiffSaver.writeImage(buf, ifd, no, type, x, y, w, h, 
    181183      no == getPlaneCount() - 1); 
     184    in.close(); 
    182185  } 
    183186 
  • branches/4.2/components/bio-formats/src/loci/formats/tiff/TiffSaver.java

    r6495 r6508  
    6363  protected RandomAccessOutputStream out; 
    6464 
     65  /** Input stream to use when overwriting data. */ 
     66  protected RandomAccessInputStream in; 
     67 
    6568  /** Whether or not to write BigTIFF data. */ 
    6669  private boolean bigTiff = false; 
     
    8285 
    8386  // -- TiffSaver methods -- 
     87 
     88  /** Sets the input stream. */ 
     89  public void setInputStream(RandomAccessInputStream in) { 
     90    this.in = in; 
     91  } 
    8492 
    8593  /** Gets the stream from which TIFF data is being saved. */ 
     
    167175    } 
    168176 
     177    if (in == null) { 
     178      throw new FormatException("RandomAccessInputStream is null. " + 
     179        "Call setInputStream(RandomAccessInputStream) first."); 
     180    } 
     181 
    169182    if (ifd == null) { 
    170183      throw new FormatException("IFD cannot be null"); 
     
    172185 
    173186    int width = (int) ifd.getImageWidth(); 
     187    if (w != width) { 
     188      // TODO : allow the tile to be narrower than the full image 
     189      throw new FormatException("Tile must be as wide as the full image."); 
     190    } 
    174191    int height = (int) ifd.getImageLength(); 
    175192    int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType); 
    176193    int plane = width * height * bytesPerPixel; 
    177     int nChannels = buf.length / plane; 
     194    int nChannels = buf.length / (w * h * bytesPerPixel); 
    178195    boolean interleaved = ifd.getPlanarConfiguration() == 1; 
    179196 
     
    185202 
    186203    TiffCompression compression = ifd.getCompression(); 
    187     boolean compressFullImage = compression == TiffCompression.JPEG_2000 || 
    188       compression == TiffCompression.JPEG_2000_LOSSY || 
    189       compression == TiffCompression.JPEG || 
    190       compression == TiffCompression.ALT_JPEG; 
    191  
    192204    int pixels = width; 
    193     if (compressFullImage) pixels *= height; 
    194205 
    195206    int rowsPerStrip = (int) ifd.getRowsPerStrip()[0]; 
     
    209220 
    210221    // write pixel strips to output buffers 
    211     for (int row=0; row<height; row++) { 
     222    for (int row=y; row<h+y; row++) { 
    212223      int strip = row / rowsPerStrip; 
    213224      for (int col=0; col<width; col++) { 
    214         int ndx = row * width * bytesPerPixel + col * bytesPerPixel; 
     225        int ndx = (row - y) * width * bytesPerPixel + col * bytesPerPixel; 
    215226        for (int c=0; c<nChannels; c++) { 
    216227          for (int n=0; n<bps[c]/8; n++) { 
     
    238249    } 
    239250 
     251    TiffParser parser = new TiffParser(in); 
     252    long[] ifdOffsets = parser.getIFDOffsets(); 
     253    if (no < ifdOffsets.length) { 
     254      out.seek(ifdOffsets[no]); 
     255      ifd = parser.getIFD(ifdOffsets[no]); 
     256    } 
     257 
    240258    // record strip byte counts and offsets 
    241259 
     
    243261    long[] stripOffsets = new long[nStrips]; 
    244262 
     263    if (ifd.containsKey(IFD.STRIP_BYTE_COUNTS)) { 
     264      stripByteCounts = ifd.getStripByteCounts(); 
     265    } 
     266    if (ifd.containsKey(IFD.STRIP_OFFSETS)) { 
     267      stripOffsets = ifd.getStripOffsets(); 
     268    } 
     269 
    245270    for (int i=0; i<nStrips; i++) { 
    246       stripByteCounts[i] = strips[i].length; 
     271      if (stripByteCounts[i] == 0) { 
     272        stripByteCounts[i] = strips[i].length; 
     273      } 
    247274    } 
    248275 
     
    254281 
    255282    for (int i=0; i<strips.length; i++) { 
    256       stripOffsets[i] = out.getFilePointer(); 
    257       out.write(strips[i]); 
    258     } 
     283      if (stripOffsets[i] > 0 && strips[i].length == 0) { 
     284        out.seek(stripOffsets[i] + stripByteCounts[i]); 
     285      } 
     286      else { 
     287        stripOffsets[i] = out.getFilePointer(); 
     288        out.write(strips[i]); 
     289      } 
     290    } 
     291    ifd.putIFDValue(IFD.STRIP_BYTE_COUNTS, stripByteCounts); 
     292    ifd.putIFDValue(IFD.STRIP_OFFSETS, stripOffsets); 
    259293    long endFP = out.getFilePointer(); 
    260294    out.seek(fp); 
  • trunk/components/bio-formats/src/loci/formats/out/APNGWriter.java

    r6328 r6508  
    7575  { 
    7676    checkParams(no, buf, x, y, w, h); 
     77    if (!isFullPlane(x, y, w, h)) { 
     78      throw new FormatException( 
     79        "APNGWriter does not yet support saving image tiles."); 
     80    } 
    7781    MetadataRetrieve meta = getMetadataRetrieve(); 
    7882 
  • trunk/components/bio-formats/src/loci/formats/out/AVIWriter.java

    r6328 r6508  
    104104  { 
    105105    checkParams(no, buf, x, y, w, h); 
     106    if (!isFullPlane(x, y, w, h)) { 
     107      throw new FormatException( 
     108        "AVIWriter does not yet support saving image tiles."); 
     109    } 
    106110    int nChannels = getSamplesPerPixel(); 
    107111 
  • trunk/components/bio-formats/src/loci/formats/out/JPEG2000Writer.java

    r6294 r6508  
    5959  { 
    6060    checkParams(no, buf, x, y, w, h); 
     61    if (!isFullPlane(x, y, w, h)) { 
     62      throw new FormatException( 
     63        "JPEG2000Writer does not yet support saving image tiles."); 
     64    } 
    6165    MetadataRetrieve retrieve = getMetadataRetrieve(); 
    6266    boolean littleEndian = 
  • trunk/components/bio-formats/src/loci/formats/out/JavaWriter.java

    r6328 r6508  
    5959  { 
    6060    checkParams(no, buf, x, y, w, h); 
     61    if (!isFullPlane(x, y, w, h)) { 
     62      throw new FormatException( 
     63        "JavaWriter does not yet support saving image tiles."); 
     64    } 
    6165 
    6266    // check pixel type 
  • trunk/components/bio-formats/src/loci/formats/out/OMEXMLWriter.java

    r6328 r6508  
    121121  { 
    122122    checkParams(no, buf, x, y, w, h); 
     123    if (!isFullPlane(x, y, w, h)) { 
     124      throw new FormatException( 
     125        "OMEXMLWriter does not yet support saving image tiles."); 
     126    } 
    123127    MetadataRetrieve retrieve = getMetadataRetrieve(); 
    124128 
  • trunk/components/bio-formats/src/loci/formats/out/TiffWriter.java

    r6294 r6508  
    178178    out.seek(out.length()); 
    179179    ifd.putIFDValue(IFD.PLANAR_CONFIGURATION, interleaved ? 1 : 2); 
     180    RandomAccessInputStream in = new RandomAccessInputStream(currentId); 
     181    tiffSaver.setInputStream(in); 
    180182    tiffSaver.writeImage(buf, ifd, no, type, x, y, w, h, 
    181183      no == getPlaneCount() - 1); 
     184    in.close(); 
    182185  } 
    183186 
  • trunk/components/bio-formats/src/loci/formats/tiff/TiffSaver.java

    r6495 r6508  
    6363  protected RandomAccessOutputStream out; 
    6464 
     65  /** Input stream to use when overwriting data. */ 
     66  protected RandomAccessInputStream in; 
     67 
    6568  /** Whether or not to write BigTIFF data. */ 
    6669  private boolean bigTiff = false; 
     
    8285 
    8386  // -- TiffSaver methods -- 
     87 
     88  /** Sets the input stream. */ 
     89  public void setInputStream(RandomAccessInputStream in) { 
     90    this.in = in; 
     91  } 
    8492 
    8593  /** Gets the stream from which TIFF data is being saved. */ 
     
    167175    } 
    168176 
     177    if (in == null) { 
     178      throw new FormatException("RandomAccessInputStream is null. " + 
     179        "Call setInputStream(RandomAccessInputStream) first."); 
     180    } 
     181 
    169182    if (ifd == null) { 
    170183      throw new FormatException("IFD cannot be null"); 
     
    172185 
    173186    int width = (int) ifd.getImageWidth(); 
     187    if (w != width) { 
     188      // TODO : allow the tile to be narrower than the full image 
     189      throw new FormatException("Tile must be as wide as the full image."); 
     190    } 
    174191    int height = (int) ifd.getImageLength(); 
    175192    int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType); 
    176193    int plane = width * height * bytesPerPixel; 
    177     int nChannels = buf.length / plane; 
     194    int nChannels = buf.length / (w * h * bytesPerPixel); 
    178195    boolean interleaved = ifd.getPlanarConfiguration() == 1; 
    179196 
     
    185202 
    186203    TiffCompression compression = ifd.getCompression(); 
    187     boolean compressFullImage = compression == TiffCompression.JPEG_2000 || 
    188       compression == TiffCompression.JPEG_2000_LOSSY || 
    189       compression == TiffCompression.JPEG || 
    190       compression == TiffCompression.ALT_JPEG; 
    191  
    192204    int pixels = width; 
    193     if (compressFullImage) pixels *= height; 
    194205 
    195206    int rowsPerStrip = (int) ifd.getRowsPerStrip()[0]; 
     
    209220 
    210221    // write pixel strips to output buffers 
    211     for (int row=0; row<height; row++) { 
     222    for (int row=y; row<h+y; row++) { 
    212223      int strip = row / rowsPerStrip; 
    213224      for (int col=0; col<width; col++) { 
    214         int ndx = row * width * bytesPerPixel + col * bytesPerPixel; 
     225        int ndx = (row - y) * width * bytesPerPixel + col * bytesPerPixel; 
    215226        for (int c=0; c<nChannels; c++) { 
    216227          for (int n=0; n<bps[c]/8; n++) { 
     
    238249    } 
    239250 
     251    TiffParser parser = new TiffParser(in); 
     252    long[] ifdOffsets = parser.getIFDOffsets(); 
     253    if (no < ifdOffsets.length) { 
     254      out.seek(ifdOffsets[no]); 
     255      ifd = parser.getIFD(ifdOffsets[no]); 
     256    } 
     257 
    240258    // record strip byte counts and offsets 
    241259 
     
    243261    long[] stripOffsets = new long[nStrips]; 
    244262 
     263    if (ifd.containsKey(IFD.STRIP_BYTE_COUNTS)) { 
     264      stripByteCounts = ifd.getStripByteCounts(); 
     265    } 
     266    if (ifd.containsKey(IFD.STRIP_OFFSETS)) { 
     267      stripOffsets = ifd.getStripOffsets(); 
     268    } 
     269 
    245270    for (int i=0; i<nStrips; i++) { 
    246       stripByteCounts[i] = strips[i].length; 
     271      if (stripByteCounts[i] == 0) { 
     272        stripByteCounts[i] = strips[i].length; 
     273      } 
    247274    } 
    248275 
     
    254281 
    255282    for (int i=0; i<strips.length; i++) { 
    256       stripOffsets[i] = out.getFilePointer(); 
    257       out.write(strips[i]); 
    258     } 
     283      if (stripOffsets[i] > 0 && strips[i].length == 0) { 
     284        out.seek(stripOffsets[i] + stripByteCounts[i]); 
     285      } 
     286      else { 
     287        stripOffsets[i] = out.getFilePointer(); 
     288        out.write(strips[i]); 
     289      } 
     290    } 
     291    ifd.putIFDValue(IFD.STRIP_BYTE_COUNTS, stripByteCounts); 
     292    ifd.putIFDValue(IFD.STRIP_OFFSETS, stripOffsets); 
    259293    long endFP = out.getFilePointer(); 
    260294    out.seek(fp); 
Note: See TracChangeset for help on using the changeset viewer.