home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / UNUAM4CS.TAR / unlimited / UNUAM4CS / YSliceVolumeFilter.java < prev    next >
Encoding:
Java Source  |  1996-05-21  |  4.9 KB  |  159 lines

  1. /*
  2.  * @(#)/YSliceVolumeFilter.java    1.3 96/03/31 by Andrew Barclay abb@nuccard.eushc.org
  3.  *
  4.  * Copyright (c) 1995 Andrew B. Barclay All Rights Reserved.
  5.  */
  6.  
  7. import java.awt.image.ColorModel ;
  8. import java.awt.Point ;
  9. import java.awt.Rectangle ;
  10.  
  11. import SliceVolumeFilter ;
  12.  
  13. /**
  14.  * An ImageFilter class for sliceing volume images.
  15.  * This class extends the basic ImageFilter Class to extract a given
  16.  * slice of an existing Volume and provide a source for a
  17.  * new image containing just the extracted slice.  It is meant to
  18.  * be used in conjunction with a FilteredImageSource object to produce
  19.  * sliced versions of existing volumes.
  20.  *
  21.  * @see CropImageFilter
  22.  * @see FilteredImageSource
  23.  * @see ImageFilter
  24.  * @see SliceVolumeFilter
  25.  *
  26.  * @version    1.1 95/12/06
  27.  * @author     Andrew Barclay
  28.  */
  29.  
  30. public class YSliceVolumeFilter extends SliceVolumeFilter {
  31.     byte outpixelsB[] ;
  32.     int outpixelsI[] ;
  33.     double szincr ;
  34.     boolean debug = false ;
  35.  
  36.     /**
  37.      * Construct a SliceVolumeFilter that extracts a slice of the source
  38.      * Volume specified by the ul, ll and ur parameters.
  39.      * @param srcSlices[] the array of x,y positions of the source image slices
  40.      * @param volWidth the width of the source volume
  41.      * @param volHeight the height of the source volume
  42.      * @param slice the slice number to be extracted
  43.      * @param width the width of the slice to be extracted
  44.      * @param height the height of the slice to be extracted
  45.      */
  46.     public YSliceVolumeFilter( Point srcSlices[], int volWidth, int volHeight,
  47.         double slice, int width, int height ) {
  48.     super( srcSlices, volWidth, volHeight, slice, width, height ) ;
  49.     type = 'Y' ;
  50.     szincr = (double)voldims[2] / (double)height ;
  51.     }
  52.  
  53.     /**
  54.      * Determine if the delivered pixels intersect the slice to
  55.      * be extracted and pass through only that subset of pixels that
  56.      * appear in the output slice.
  57.      */
  58.     public void setPixels(int x, int y, int w, int h,
  59.               ColorModel model, byte pixels[], int off,
  60.               int scansize) {
  61.     long ct = System.currentTimeMillis() ;
  62.     Rectangle sr = new Rectangle( x, y, w, h ) ;
  63.     int sz1 = firstZSlice( sr ) ;
  64.     int sz2 = lastZSlice( sr ) ;
  65.     /*
  66.     dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  67.     dbg( "off="+off+" scansize="+scansize+" type="+type+" slice="+slice ) ;
  68.     */
  69.  
  70.     // implement this properly later -- for now, just take orthogonal
  71.     // slices.
  72.     int islice = (int)( 0.5 + slice ) ;
  73.     if( islice >= 0 && islice < voldims[1] ) {
  74.         double sz = -0.5*szincr + sz1 ;
  75.         int ystart = (int)( 0.5 + sz/szincr ) ;
  76.         int iyp = 0, nlines = 0 ;
  77.         if( outpixelsB == null ) outpixelsB = new byte[width*height] ;
  78.         byte outpixels[] = outpixelsB ;
  79.  
  80.         for( ; sz < (-0.5 + sz2) ; sz += szincr ) {
  81.         int isz = (int)( 0.5 + sz ) ;
  82.         int x1 = srcSlices[isz].x ;
  83.         int y1 = srcSlices[isz].y + islice ;
  84.         if( y <= y1 && (y+h) > y1 && x <= x1 && (x+w) > x1 ) {
  85.             int dx = (x1>x) ? x1-x : 0 ;
  86.             int dy = (y1>y) ? y1-y : 0 ;
  87.             int is = off + dx + dy*scansize ;
  88.             System.arraycopy( pixels, is, outpixels, iyp, width ) ;
  89.             iyp += width ;
  90.             nlines++ ;
  91.         } else if( nlines == 0 ) {
  92.             ystart++ ;
  93.         }
  94.         }
  95.  
  96.         if( nlines > 0 ) {
  97.         dbg( "yslice="+islice ) ;
  98.         dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  99.         dbg( "ystart="+ystart+" nlines="+nlines ) ;
  100.  
  101.         consumer.setPixels( 0, ystart, width, nlines,
  102.             model, outpixels, 0, width ) ;
  103.         dbg( "byte time= "+(System.currentTimeMillis()-ct)+" ms.\n" ) ;
  104.         }
  105.     }
  106.     }
  107.     
  108.     public void setPixels(int x, int y, int w, int h,
  109.               ColorModel model, int pixels[], int off,
  110.               int scansize) {
  111.     long ct = System.currentTimeMillis() ;
  112.     Rectangle sr = new Rectangle( x, y, w, h ) ;
  113.     int sz1 = firstZSlice( sr ) ;
  114.     int sz2 = lastZSlice( sr ) ;
  115.     /*
  116.     dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  117.     dbg( "off="+off+" scansize="+scansize+" type="+type+" slice="+slice ) ;
  118.     */
  119.  
  120.     // implement this properly later -- for now, just take orthogonal
  121.     // slices.
  122.     int islice = (int)( 0.5 + slice ) ;
  123.     if( islice >= 0 && islice < voldims[1] ) {
  124.         double sz = -0.5*szincr + sz1 ;
  125.         int ystart = (int)( 0.5 + sz/szincr ) ;
  126.         int iyp = 0, nlines = 0 ;
  127.         if( outpixelsI == null ) outpixelsI = new int[width*height] ;
  128.         int outpixels[] = outpixelsI ;
  129.  
  130.         for( ; sz < (-0.5 + sz2) ; sz += szincr ) {
  131.         int isz = (int)( 0.5 + sz ) ;
  132.         int x1 = srcSlices[isz].x ;
  133.         int y1 = srcSlices[isz].y + islice ;
  134.         if( y <= y1 && (y+h) > y1 && x <= x1 && (x+w) > x1 ) {
  135.             int dx = (x1>x) ? x1-x : 0 ;
  136.             int dy = (y1>y) ? y1-y : 0 ;
  137.             int is = off + dx + dy*scansize ;
  138.             System.arraycopy( pixels, is, outpixels, iyp, width ) ;
  139.             iyp += width ;
  140.             nlines++ ;
  141.         } else if( nlines == 0 ) {
  142.             ystart++ ;
  143.         }
  144.         }
  145.  
  146.         if( nlines > 0 ) {
  147.         dbg( "yslice="+islice ) ;
  148.         dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  149.         dbg( "ystart="+ystart+" nlines="+nlines ) ;
  150.  
  151.         consumer.setPixels( 0, ystart, width, nlines,
  152.             model, outpixels, 0, width ) ;
  153.         dbg( "int time= "+(System.currentTimeMillis()-ct)+" ms.\n" ) ;
  154.         }
  155.     }
  156.     }
  157.     
  158. }
  159.