Changeset 3170


Ignore:
Timestamp:
09/18/07 09:46:23 (12 years ago)
Author:
melissa
Message:

Fixed how RGB images are handled; added support for indexed color.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/in/DicomReader.java

    r3097 r3170  
    2525package loci.formats.in; 
    2626 
    27 import java.awt.image.BufferedImage; 
     27import java.awt.image.*; 
    2828import java.io.*; 
    2929import java.text.*; 
     
    9191  private boolean inSequence; 
    9292  private boolean bigEndianTransferSyntax; 
     93  private boolean indexed = false; 
     94  private byte[][] lut; 
    9395 
    9496  // -- Constructor -- 
     
    112114  } 
    113115 
     116  /* @see loci.formats.IFormatReader#isIndexed() */ 
     117  public boolean isIndexed() { 
     118    FormatTools.assertId(currentId, true, 1); 
     119    return indexed; 
     120  } 
     121 
     122  /* @see loci.formats.IFormatReader#get8BitLookupTable() */ 
     123  public byte[][] get8BitLookupTable() { 
     124    FormatTools.assertId(currentId, true, 1); 
     125    return lut; 
     126  } 
     127 
    114128  /* @see loci.formats.IFormatReader#openBytes(int) */ 
    115129  public byte[] openBytes(int no) throws FormatException, IOException { 
    116130    FormatTools.assertId(currentId, true, 1); 
    117     byte[] buf = new byte[core.sizeX[0] * core.sizeY[0] * (bitsPerPixel / 8)]; 
     131    int bytes = core.sizeX[0] * core.sizeY[0] * (bitsPerPixel / 8); 
     132    if (!indexed) bytes *= core.sizeC[0]; 
     133    byte[] buf = new byte[bytes]; 
    118134    return openBytes(no, buf); 
    119135  } 
     
    129145 
    130146    int bytes = core.sizeX[0] * core.sizeY[0] * (bitsPerPixel / 8); 
     147    if (!indexed) bytes *= core.sizeC[0]; 
    131148 
    132149    if (buf.length < bytes) { 
     
    142159  public BufferedImage openImage(int no) throws FormatException, IOException { 
    143160    FormatTools.assertId(currentId, true, 1); 
    144     return ImageTools.makeImage(openBytes(no), core.sizeX[0], core.sizeY[0], 
    145       1, false, bitsPerPixel / 8, core.littleEndian[0]); 
     161    BufferedImage b = ImageTools.makeImage(openBytes(no), core.sizeX[0],  
     162      core.sizeY[0], indexed ? 1 : core.sizeC[0], core.interleaved[0],  
     163      bitsPerPixel / 8, core.littleEndian[0]); 
     164    if (indexed) { 
     165      byte[][] table = get8BitLookupTable(); 
     166      IndexedColorModel model = 
     167        new IndexedColorModel(8, table[0].length, table); 
     168      WritableRaster raster = Raster.createWritableRaster(b.getSampleModel(), 
     169        b.getData().getDataBuffer(), null); 
     170      b = new BufferedImage(model, raster, false, null); 
     171    } 
     172    return b; 
    146173  } 
    147174 
     
    270297 
    271298    core.sizeZ[0] = core.imageCount[0]; 
    272     core.sizeC[0] = 1; 
     299    if (core.sizeC[0] == 0) core.sizeC[0] = 1; 
     300    core.rgb[0] = core.sizeC[0] > 1; 
    273301    core.sizeT[0] = 1; 
    274     core.currentOrder[0] = "XYZTC"; 
    275     core.rgb[0] = false; 
    276     core.interleaved[0] = false; 
     302    core.currentOrder[0] = "XYCZT"; 
     303    core.interleaved[0] = true; 
    277304 
    278305    // The metadata store we're working with. 
     
    341368 
    342369  private void addInfo(int tag, String value) throws IOException { 
     370    long oldFp = in.getFilePointer(); 
    343371    String info = getHeaderInfo(tag, value); 
    344372    if (inSequence && info != null && vr != SQ) info = ">" + info; 
     
    346374      String key = (String) TYPES.get(new Integer(tag)); 
    347375      if (key == null) key = "" + tag; 
     376      if (key.equals("Samples per pixel")) { 
     377        core.sizeC[0] = Integer.parseInt(info.trim()); 
     378        if (core.sizeC[0] > 1) core.rgb[0] = true; 
     379      } 
     380      else if (key.equals("Photometric Interpretation")) { 
     381        if (info.trim().equals("PALETTE COLOR")) { 
     382          indexed = true; 
     383          core.sizeC[0] = 3; 
     384          core.rgb[0] = true; 
     385          lut = new byte[3][]; 
     386        } 
     387      } 
     388      else if (key.indexOf("Palette Color LUT Data") != -1) { 
     389        String color = key.substring(0, key.indexOf(" ")).trim(); 
     390        int ndx = color.equals("Red") ? 0 : color.equals("Green") ? 1 : 2; 
     391        long fp = in.getFilePointer(); 
     392        in.seek(oldFp + ndx*ndx); 
     393        lut[ndx] = new byte[elementLength / 2]; 
     394        for (int i=0; i<lut[ndx].length; i++) { 
     395          lut[ndx][i] = (byte) (in.read() & 0xff); 
     396          in.skipBytes(1); 
     397        } 
     398        in.seek(fp); 
     399      } 
     400 
    348401      if (tag != PIXEL_DATA) addMeta(key, info); 
    349402    } 
     
    408461        value = ""; 
    409462        boolean privateTag = ((tag >> 16) & 1) != 0; 
    410         if (tag == ICON_IMAGE_SEQUENCE || privateTag) skip = true; 
    411         break; 
     463        if (tag != ICON_IMAGE_SEQUENCE && !privateTag) break; 
    412464      default: 
    413465        skip = true; 
     
    498550 
    499551    int elementWord = in.readShort(); 
    500     int tag = groupWord << 16 | elementWord; 
     552    int tag = ((groupWord << 16) & 0xffff0000) | (elementWord & 0xffff); 
    501553    elementLength = getLength(); 
    502554 
Note: See TracChangeset for help on using the changeset viewer.