home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuam4cs / zslicevolumefilter.java < prev   
Encoding:
Java Source  |  1996-08-14  |  4.2 KB  |  129 lines

  1. /*
  2.  * @(#)/ZSliceVolumeFilter.java    1.3 96/03/30 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.  * This one's easy.
  22.  *
  23.  * @see CropImageFilter
  24.  * @see FilteredImageSource
  25.  * @see ImageFilter
  26.  * @see SliceVolumeFilter
  27.  *
  28.  * @version    1.1 95/12/06
  29.  * @author     Andrew Barclay
  30.  */
  31.  
  32. public class ZSliceVolumeFilter extends SliceVolumeFilter {
  33.     boolean debug = false ;
  34.  
  35.     /**
  36.      * Construct a SliceVolumeFilter that extracts a slice of the source
  37.      * Volume specified by the ul, ll and ur parameters.
  38.      * @param srcSlices[] the array of x,y positions of the source image slices
  39.      * @param volWidth the width of the source volume
  40.      * @param volHeight the height of the source volume
  41.      * @param slice the slice number to be extracted
  42.      * @param width the width of the slice to be extracted
  43.      * @param height the height of the slice to be extracted
  44.      */
  45.     public ZSliceVolumeFilter( Point srcSlices[], int volWidth, int volHeight,
  46.         double slice, int width, int height ) {
  47.     super( srcSlices, volWidth, volHeight, slice, width, height ) ;
  48.     type = 'Z' ;
  49.     }
  50.  
  51.     /**
  52.      * Determine if the delivered pixels intersect the slice to
  53.      * be extracted and pass through only that subset of pixels that
  54.      * appear in the output slice.
  55.      */
  56.     public void setPixels(int x, int y, int w, int h,
  57.               ColorModel model, byte pixels[], int off,
  58.               int scansize) {
  59.     long ct = System.currentTimeMillis() ;
  60.     Rectangle sr = new Rectangle( x, y, w, h ) ;
  61.     int sz1 = firstZSlice( sr ) ;
  62.     int sz2 = lastZSlice( sr ) ;
  63.     /*
  64.     dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  65.     dbg( "off="+off+" scansize="+scansize+" type="+type+" slice="+slice ) ;
  66.     */
  67.  
  68.     // implement this properly later -- for now, just take orthogonal
  69.     // slices.
  70.     int islice = (int)( 0.5 + slice ) ;
  71.     if( islice >= sz1 && islice < sz2 ) {
  72.         int x1 = srcSlices[islice].x ;
  73.         int y1 = srcSlices[islice].y ;
  74.         int dx = (x1>x) ? x1-x : 0 ;
  75.         int dy = (y1>y) ? y1-y : 0 ;
  76.         Rectangle ir = sr.intersection( 
  77.         new Rectangle( x1, y1, width, height )
  78.         ) ;
  79.         dbg( "zslice="+islice+" ir="+ir ) ;
  80.         dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  81.         if( ir.width > 0 && ir.height > 0 ) {
  82.         consumer.setPixels( ir.x-x1, ir.y-y1, ir.width, ir.height,
  83.             model, pixels, off + dx + dy*scansize, scansize ) ;
  84.         dbg( "byte time= "+(System.currentTimeMillis()-ct)+" ms.\n" ) ;
  85.         } else {
  86.         Rectangle dr = new Rectangle( x1, y1, voldims[0], voldims[1] ) ;
  87.         dbg( "Error: h<0, dr="+dr ) ;
  88.         }
  89.     }
  90.     }
  91.     
  92.     public void setPixels(int x, int y, int w, int h,
  93.               ColorModel model, int pixels[], int off,
  94.               int scansize) {
  95.     long ct = System.currentTimeMillis() ;
  96.     Rectangle sr = new Rectangle( x, y, w, h ) ;
  97.     int sz1 = firstZSlice( sr ) ;
  98.     int sz2 = lastZSlice( sr ) ;
  99.     /*
  100.     dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  101.     dbg( "off="+off+" scansize="+scansize+" type="+type+" slice="+slice ) ;
  102.     */
  103.  
  104.     // implement this properly later -- for now, just take orthogonal
  105.     // slices.
  106.     int islice = (int)( 0.5 + slice ) ;
  107.     if( islice >= sz1 && islice < sz2 ) {
  108.         int x1 = srcSlices[islice].x ;
  109.         int y1 = srcSlices[islice].y ;
  110.         int dx = (x1>x) ? x1-x : 0 ;
  111.         int dy = (y1>y) ? y1-y : 0 ;
  112.         Rectangle ir = sr.intersection( 
  113.         new Rectangle( x1, y1, width, height )
  114.         ) ;
  115.         dbg( "zslice="+islice+" ir="+ir ) ;
  116.         dbg( "sr="+sr+" sz1="+sz1+" sz2="+sz2 ) ;
  117.         if( ir.width > 0 && ir.height > 0 ) {
  118.         consumer.setPixels( ir.x-x1, ir.y-y1, ir.width, ir.height,
  119.             model, pixels, off + dx + dy*scansize, scansize ) ;
  120.         dbg( "int time= "+(System.currentTimeMillis()-ct)+" ms.\n" ) ;
  121.         } else {
  122.         Rectangle dr = new Rectangle( x1, y1, voldims[0], voldims[1] ) ;
  123.         dbg( "Error: h<0, dr="+dr ) ;
  124.         }
  125.     }
  126.     }
  127.     
  128. }
  129.