Changeset 1306


Ignore:
Timestamp:
08/21/06 11:10:36 (14 years ago)
Author:
melissa
Message:
  • added methods to ImageTools to pad images with 0's
  • fixed how FileStitcher determines dimensions
  • fixed QuickTime bug
Location:
trunk/loci/formats
Files:
3 edited

Legend:

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

    r1284 r1306  
    247247    } 
    248248 
     249    // always set the width and height to the maximum values 
     250    
     251    for (int i=0; i<dims.length; i++) { 
     252      if (dims[i][0] > dimensions[0]) { 
     253        dimensions[0] = dims[i][0]; 
     254      } 
     255      if (dims[i][1] > dimensions[1]) { 
     256        dimensions[1] = dims[i][1]; 
     257      } 
     258    } 
     259 
    249260    if (varyZ || varyC || varyT) { 
    250       for (int j=0; j<3; j++) { 
    251         int max = 0; 
    252         int maxIndex = 0; 
    253         for (int i=0; i<dims.length; i++) { 
    254           if (dims[i][j] > max) { 
    255             max = dims[i][j]; 
    256             maxIndex = i; 
    257           } 
    258         } 
    259  
    260         dimensions[j] = dims[maxIndex][j]; 
    261       } 
     261      int max = 0; 
     262      int maxIndex = 0; 
     263      for (int i=0; i<dims.length; i++) { 
     264        if (dims[i][2] > max) { 
     265          max = dims[i][2]; 
     266          maxIndex = i; 
     267        } 
     268      } 
     269 
     270      dimensions[2] = dims[maxIndex][2]; 
    262271    } 
    263272    else { 
    264       dimensions[0] = dims[0][0]; 
    265       dimensions[1] = dims[0][1]; 
    266273      dimensions[2] = dims[0][2]; 
    267274      dimensions[3] = dims[0][3]; 
  • trunk/loci/formats/ImageTools.java

    r1282 r1306  
    717717  } 
    718718 
    719  
     719  /**  
     720   * Pad the image to the given width and height. The image will be centered 
     721   * within the new bounds. 
     722   */ 
     723  public static BufferedImage padImage(BufferedImage img, int width, int height) 
     724  { 
     725    if (img == null) { 
     726      byte[][] data = new byte[1][width * height]; 
     727      return makeImage(data, width, height); 
     728    } 
     729     
     730    boolean needsPadding = img.getWidth() != width || img.getHeight() != height; 
     731 
     732    if (needsPadding) { 
     733      int totalX = width - img.getWidth(); 
     734      int totalY = height - img.getHeight(); 
     735 
     736      int xpad = totalX / 2; 
     737      int ypad = totalY / 2; 
     738 
     739      if (xpad == 0 && totalX > 0) xpad = totalX; 
     740      if (ypad == 0 && totalY > 0) ypad = totalY; 
     741 
     742      Object pixels = getPixels(img); 
     743 
     744      if (pixels instanceof byte[][]) { 
     745        byte[][] b = (byte[][]) pixels; 
     746        byte[][] newBytes = new byte[b.length][width * height]; 
     747        for (int i=0; i<b.length; i++) { 
     748          newBytes[i] = padImage(b[i], img.getWidth(), width, height); 
     749        } 
     750        return makeImage(newBytes, width, height); 
     751      } 
     752      else if (pixels instanceof short[][]) { 
     753        short[][] b = (short[][]) pixels; 
     754        short[][] newShorts = new short[b.length][width * height]; 
     755        for (int i=0; i<b.length; i++) { 
     756          newShorts[i] = padImage(b[i], img.getWidth(), width, height); 
     757        } 
     758        return makeImage(newShorts, width, height); 
     759      } 
     760      else if (pixels instanceof int[][]) { 
     761        int[][] b = (int[][]) pixels; 
     762        int[][] newInts = new int[b.length][width * height]; 
     763        for (int i=0; i<b.length; i++) { 
     764          newInts[i] = padImage(b[i], img.getWidth(), width, height); 
     765        } 
     766        return makeImage(newInts, width, height); 
     767      } 
     768      else if (pixels instanceof float[][]) { 
     769        float[][] b = (float[][]) pixels; 
     770        float[][] newFloats = new float[b.length][width * height]; 
     771        for (int i=0; i<b.length; i++) { 
     772          newFloats[i] = padImage(b[i], img.getWidth(), width, height); 
     773        } 
     774        return makeImage(newFloats, width, height); 
     775      } 
     776      else if (pixels instanceof double[][]) { 
     777        double[][] b = (double[][]) pixels; 
     778        double[][] newDoubles = new double[b.length][width * height]; 
     779        for (int i=0; i<b.length; i++) { 
     780          newDoubles[i] = padImage(b[i], img.getWidth(), width, height); 
     781        } 
     782        return makeImage(newDoubles, width, height); 
     783      } 
     784      return null; 
     785    } 
     786    return img; 
     787  } 
     788 
     789  /** 
     790   * Pad the byte array to the given width and height. The image will be 
     791   * centered within the new bounds. 
     792   */ 
     793  public static byte[] padImage(byte[] b, int oldWidth, int width, int height) { 
     794    boolean needsPadding =  
     795      (oldWidth != width) || ((b.length / oldWidth) != height); 
     796 
     797    if (needsPadding) { 
     798      int totalX = width - oldWidth; 
     799      int totalY = height - (b.length / oldWidth); 
     800 
     801      int xpad = totalX / 2; 
     802      int ypad = totalY / 2; 
     803 
     804      if (xpad == 0 && totalX > 0) xpad = totalX; 
     805      if (ypad == 0 && totalY > 0) ypad = totalY; 
     806 
     807      byte[] padded = new byte[width * height]; 
     808 
     809      for (int i=ypad; i<height - ypad; i++) { 
     810        System.arraycopy(b, (i - ypad) * oldWidth, padded, i*width + xpad,  
     811          oldWidth);  
     812      } 
     813      return padded; 
     814    } 
     815    return b; 
     816  } 
     817 
     818  /** 
     819   * Pad the short array to the given width and height. The image will be 
     820   * centered within the new bounds. 
     821   */ 
     822  public static short[] padImage(short[] b, int oldWidth, int width, int height) 
     823  { 
     824    boolean needsPadding = oldWidth != width || (b.length / oldWidth) != height; 
     825 
     826    if (needsPadding) { 
     827      int totalX = width - oldWidth; 
     828      int totalY = height - (b.length / oldWidth); 
     829 
     830      int xpad = totalX / 2; 
     831      int ypad = totalY / 2; 
     832 
     833      if (xpad == 0 && totalX > 0) xpad = totalX; 
     834      if (ypad == 0 && totalY > 0) ypad = totalY; 
     835 
     836      short[] padded = new short[width * height]; 
     837 
     838      for (int i=ypad; i<height - ypad; i++) { 
     839        System.arraycopy(b, (i - ypad) * oldWidth, padded, i*width + xpad,  
     840          oldWidth);  
     841      } 
     842      return padded; 
     843    } 
     844    return b; 
     845  } 
     846 
     847  /** 
     848   * Pad the int array to the given width and height. The image will be 
     849   * centered within the new bounds. 
     850   */ 
     851  public static int[] padImage(int[] b, int oldWidth, int width, int height) { 
     852    boolean needsPadding = oldWidth != width || (b.length / oldWidth) != height; 
     853 
     854    if (needsPadding) { 
     855      int totalX = width - oldWidth; 
     856      int totalY = height - (b.length / oldWidth); 
     857 
     858      int xpad = totalX / 2; 
     859      int ypad = totalY / 2; 
     860 
     861      if (xpad == 0 && totalX > 0) xpad = totalX; 
     862      if (ypad == 0 && totalY > 0) ypad = totalY; 
     863 
     864      int[] padded = new int[width * height]; 
     865 
     866      for (int i=ypad; i<height - ypad; i++) { 
     867        System.arraycopy(b, (i - ypad) * oldWidth, padded, i*width + xpad,  
     868          oldWidth);  
     869      } 
     870      return padded; 
     871    } 
     872    return b; 
     873  } 
     874 
     875  /** 
     876   * Pad the float array to the given width and height. The image will be 
     877   * centered within the new bounds. 
     878   */ 
     879  public static float[] padImage(float[] b, int oldWidth, int width, int height) 
     880  { 
     881    boolean needsPadding = oldWidth != width || (b.length / oldWidth) != height; 
     882 
     883    if (needsPadding) { 
     884      int totalX = width - oldWidth; 
     885      int totalY = height - (b.length / oldWidth); 
     886 
     887      int xpad = totalX / 2; 
     888      int ypad = totalY / 2; 
     889 
     890      if (xpad == 0 && totalX > 0) xpad = totalX; 
     891      if (ypad == 0 && totalY > 0) ypad = totalY; 
     892 
     893      float[] padded = new float[width * height]; 
     894 
     895      for (int i=ypad; i<height - ypad; i++) { 
     896        System.arraycopy(b, (i - ypad) * oldWidth, padded, i*width + xpad,  
     897          oldWidth);  
     898      } 
     899      return padded; 
     900    } 
     901    return b; 
     902  } 
     903 
     904  /** 
     905   * Pad the double array to the given width and height. The image will be 
     906   * centered within the new bounds. 
     907   */ 
     908  public static double[] padImage(double[] b, int oldWidth, int width,  
     909    int height)  
     910  { 
     911    boolean needsPadding = oldWidth != width || (b.length / oldWidth) != height; 
     912 
     913    if (needsPadding) { 
     914      int totalX = width - oldWidth; 
     915      int totalY = height - (b.length / oldWidth); 
     916 
     917      int xpad = totalX / 2; 
     918      int ypad = totalY / 2; 
     919 
     920      if (xpad == 0 && totalX > 0) xpad = totalX; 
     921      if (ypad == 0 && totalY > 0) ypad = totalY; 
     922 
     923      double[] padded = new double[width * height]; 
     924 
     925      for (int i=ypad; i<height - ypad; i++) { 
     926        System.arraycopy(b, (i - ypad) * oldWidth, padded, i*width + xpad,  
     927          oldWidth);  
     928      } 
     929      return padded; 
     930    } 
     931    return b; 
     932  } 
     933   
    720934  // -- Image scaling -- 
    721935 
  • trunk/loci/formats/in/QTReader.java

    r1264 r1306  
    209209  private byte[] prevPixels; 
    210210 
     211  /** Previous plane number. */ 
     212  private int prevPlane; 
     213 
     214  /** Flag indicating whether we can safely use prevPixels. */ 
     215  private boolean canUsePrevious; 
     216 
    211217  /** Video codec used by this movie. */ 
    212218  private String codec; 
     
    354360    in.read(pixs); 
    355361 
     362    canUsePrevious = (prevPixels != null) && (prevPlane == no - 1); 
     363     
    356364    if (codec.equals("jpeg") || codec.equals("mjpb")) { 
    357365      return ImageTools.getBytes(openImage(id, no), isRGB(id) && separated, 
     
    361369    byte[] bytes = uncompress(pixs, codec); 
    362370    // on rare occassions, we need to trim the data 
    363     if ((prevPixels != null) && (prevPixels.length < bytes.length)) { 
     371    if (canUsePrevious && (prevPixels.length < bytes.length)) { 
    364372      byte[] temp = bytes; 
    365373      bytes = new byte[prevPixels.length]; 
     
    368376 
    369377    prevPixels = bytes; 
     378    prevPlane = no; 
    370379 
    371380    // determine whether we need to strip out any padding bytes 
     
    469478    in.seek(pixelOffset + offset); 
    470479    in.read(pixs); 
     480 
     481    canUsePrevious = (prevPixels != null) && (prevPlane == no - 1); 
    471482 
    472483    if (codec.equals("jpeg")) { 
     
    773784  /** Uncompresses an image plane according to the the codec identifier. */ 
    774785  public byte[] uncompress(byte[] pixs, String code) 
    775     throws FormatException 
     786    throws FormatException, IOException 
    776787  { 
    777788    // JPEG and mjpb codecs handled separately, so not included in this list 
     
    10531064    } 
    10541065 
    1055  
    10561066    // add start-of-scan header 
    10571067 
     
    10741084      v.add((byte) 0x01);  // DC and AC table numbers 
    10751085    } 
    1076  
    10771086 
    10781087    v.add((byte) 0x00); 
     
    11471156 
    11481157  /** Uncompresses a QT RLE compressed image plane. */ 
    1149   public byte[] rleUncompress(byte[] input) throws FormatException { 
     1158  public byte[] rleUncompress(byte[] input) throws FormatException, IOException 
     1159  { 
    11501160    if (input.length < 8) return prevPixels; 
    11511161 
     
    11771187      // copy appropriate lines from prevPixels 
    11781188 
    1179       if (prevPixels != null) { 
     1189      if (canUsePrevious) { 
    11801190        for (int i=0; i<start; i++) { 
    11811191          off = i * width * ebpp; 
     
    12041214      if (skip < 0) skip += 256; 
    12051215 
    1206       if (prevPixels != null) { 
     1216      if (canUsePrevious) { 
    12071217        try { 
    12081218          System.arraycopy(prevPixels, rowPointer, output, rowPointer, 
     
    12211231          skip = input[pt]; 
    12221232 
    1223           if (prevPixels != null) { 
     1233          if (canUsePrevious) { 
    12241234            try { 
    12251235              System.arraycopy(prevPixels, off, output, off, 
     
    12351245          // make sure we copy enough pixels to fill the line 
    12361246 
    1237           if (off < (rowPointer + (width * ebpp))) { 
     1247          if (off < (rowPointer + (width * ebpp)) && canUsePrevious) { 
    12381248            System.arraycopy(prevPixels, off, output, off, 
    12391249              (rowPointer + (width * ebpp)) - off); 
Note: See TracChangeset for help on using the changeset viewer.