advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Using Rasters for Image Processing, Part 2 :

Create a WritableRaster Object from a Specific SampleModel Object

If you want to create a WritableRaster object from a specific SampleModel object, for instance, PixelInterleavedSampleModel, SinglePixelPackedSampleModel, MultiPixelPackedSampleModel, or BandedSampleModel, this a set of methods is dedicated to performing just that task:
  • public static WritableRaster createInterleavedRaster(DataBuffer DB, int w, int h, int scanlineStride , int pixelStride, int[] bandOffsets, Point location): This method creates a WritableRaster object based on the PixelInterleavedSampleModel and using the specified DataBuffer object. The location parameter represents the origin coordinates of the raster. If the parameter is null, then the origin coordinates will be (0,0). You can obtain the number of bands from bandOffsets.length.
  • public static WritableRaster createPackedRaster(DataBuffer DB, int w, int h, int scanlineStride, int[] bandMasks, Point location): This method creates a WritableRaster object based on the SinglePixelPackedSampleModel and using the specified DataBuffer object. The location parameter represents the origin coordinates of the raster. If this parameter is null, then the origin coordinates will be (0,0). The bandMasks array contain an entry for every color band so the bandMasks.length represents the number of image color bands.
  • public static WritableRaster createPackedRaster(DataBuffer DB, int w, int h, int bitsPerPixel, Point location): This method creates a WritableRaster object based on the MultiPixelPackedSampleModel and using the specified DataBuffer object. The location parameter represents the origin coordinates of the raster. If this parameter is null, then the origin coordinates will be (0,0). The bitsPerPixel parameter specifies the number of bits for each pixel.
  • public static WritableRaster createBandedRaster(int dataType, int w, int h, int bands, Point location): This creates a WritableRaster object based on the BandedSampleModel. band represents the number of bands, dataType represents the data type for storing samples (can be only TYPE_BYTE, TYPE_USHORT, and TYPE_INT) and location represents the origin coordinates of the raster. If location is null than the (0,0) coordinates will be used.
Now, if you know how to create a raster using WritableRaster class, you can use the setter methods of this class to modify it:
  • setPixel: This method uses an int array of samples for setting a pixel in the DataBuffer. The pixels coordinates are (x,y):
    public void setPixel(int x,int y,int[] intArray)(x,y)
    Similar tow the above method, these methods use an array of float and an array of double, respectively:
    public void setPixel(int x, int y, float[] floatArray)
    public void setPixel(int x, int y, double[] doubleArray)
    
  • setPixels: This method uses an int array of samples for setting all pixels into the (x,y,w,h) rectangle. Every int array element represents one sample:
    public void setPixels(int x, int y, int w, int h, int[] intArray)
    
    Similar to the above method, these methods use an array of float and an array of double, respectively:
    public void setPixels(int x,int y,int w,int h,float[] floatArray)
    public void setPixels(int x,int y,int w,int h,double[] doubleArray)
    
  • setSample: This method sets the int sample for the pixel from the (x,y) coordinates in the DataBuffer. The sample's band is specified by the b parameter:
    public void setSample(int x,int y,int b,int s)
    
    Similar to the above method, the next two methods use a float sample and a double sample respectively:
    public void setSample(int x,int y,int b,float s)
    public void setSample(int x,int y,int b,double s)
    
  • setSamples:
  • This method sets the int samples for the pixels into the (x,y,w,h) rectangle. The int samples are provided by the intArray array, one sample per array element and all samples are in the b band:
    public void setSamples(int x,int y,int w,int h,int b,int[] intArray) 
    
    Similar to the above method, the next two methods use an array of float and an array of double, respectively:
    public void setSamples(int x,int y,int w,int h,int b,float[] floatArray)
    public void setSamples(int x,int y,int w,int h,int b,double[] doubleArray)
    

Exctracting a Raster from Image and BufferedImage Objects

As you have seen, the Raster and WritableRaster classes define sets of methods for creating rasters. However, these methods aren't generally used for manually creating rasters—they are instead used for extracting rasters from images. In Java, these images can be encapsulated by the Image and BufferedImage objects.

The first step for extracting a raster from an Image object is to obtain the pixels from it, using the PixelGrabber class (click here to find a good presentation of this class). The second step is to create DataBuffer and SampleModel objects. Of course, to create these objects, you will use the pixels that have been extracted with PixelGrabber class. The last step is to call a specific method from the Raster or WritableRaster class.

Listing 1 shows an example of extracting a raster from an Image object. In addition to drawing the raster portion on the screen, the application converts the Raster object to a BufferedImage object. This type of conversion was addopted for all the applications in this section. If you'd rather, you can also draw the raster on the screen like you did in Part I.


Figure 1. RasterFromImage.java: Left side: original picture, right side: raster.

The second example in this section extracts a raster from a BufferedImage. This is simpler than extracting a raster from an Image, because the BufferedImage class defines a set of methods that can be used for extracting rasters. The most commonly used methods are:

  • public Raster getData(): This extracts a raster from the whole image.
  • public Raster getData(Rectangle rect): This extracts a raster from the image equal to the rectangle area, rect.
  • public WritableRaster getRaster(): This returns a WritableRaster from the image.
Listing 2 uses public Raster getData(Rectangle rect) to extract a raster from a BufferedImage object. The BufferedImage object is obtained from an Image object. After it obtains the raster, the application converts it back to a BufferedImage and draws it on the screen.

Note: The output of this application is the same as the output of the RasterFromImage.java application.

The application in Listing 2 demonsrates a simple way to extract a raster from a BufferedImage. However, there are many ways to extract a raster from a BufferedImage. Here are some examples:

  • Example 1:
    int W,H;
        BufferedImage BI=null;
        ...
        BI=new BufferedImage(W,H,BufferedImage.TYPE_INT_RGB);
        ...
        WritableRaster WR1=Raster.createWritableRaster
    	(BI.getSampleModel(),null);
        WritableRaster WR2=WR1.createCompatibleWritableRaster
    	(BI.getWidth()/2,BI.getHeight()/2);      
        BI.copyData(WR1); //or
        //BI.copyData(WR2); 
    
  • Example 2:
        int W,H;
        BufferedImage BI=null;
        ...
        BI=new BufferedImage(W,H,BufferedImage.TYPE_INT_RGB);
        ...
        Rectangle rect=new Rectangle(0,0,BI.getWidth()/3,BI.getHeight()/3);        
        Raster R=BI.getData(rect);  //or for the whole image                      
        //Raster R=BI.getData();
    
  • Example 3:
    	int W,H;
        BufferedImage BI=null;
        ...
        BI=new BufferedImage(W,H,BufferedImage.TYPE_INT_RGB);
        ...
        WritableRaster R=BI.getRaster();
    
  • Example 4:
        int W,H;
        BufferedImage BI=null;
        ...
        BI=new BufferedImage(W,H,BufferedImage.TYPE_INT_ARGB);
        ...
        //the bit masks must be compatible with the BufferedImage format
        //the below BM array must corespond to BufferedImage.TYPE_INT_ARGB
        int[] BM=new int[]{0xff0000,0xff00,0xff,0xff000000};
        WritableRaster WR=Raster.createPackedRaster
    	(DataBuffer.TYPE_INT,50,50,BM,null);
        BI.copyData(WR);  
    
Home / Articles / Using Rasters for Image Processing, Part 2 / 1 / 2 / Next Page

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint
Oracle: Eating its Own Open Source Food
Big Money and Open Source May Not Compute
Open Source Embrace Gives Sun New Fans
NetBeans, OpenSolaris Also in Spotlight at JavaOne

Taming Trees: Building Branching Structures
Clean Up Function Syntax Mess with decltype
Sutter Speaks: The Future of Concurrency
INTEL SCAVENGER HUNT, LENOVO X300 AND APPLE IPOD TOUCH GIVEAWAY (the "Giveaway")
Comparing Multi-Core Processors for Server Virtualization
Intel® Desktop Business Computing Solutions
Intel: What Downturn?
Managing the Evolving Data Center
Implement Drag and Drop in Your Windows Forms Applications
Processing Linked Web Data with XSLT

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES