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 :

Extracting a Raster from an Array of Pixels

A less common method for obtaining a raster is to define an array of pixels and convert the array to a raster. Listing 3 deomonstrates this technique. Figure 2 shows the possible results.


Figure 2. RasterFromPixelArray.java.

Share DataBuffers Between Rasters

Because Java allows rasters to share DataBuffers, not every raster has its own DataBuffer object. This can really help save your application resources. You can create rasters that share the same DataBuffer explicitly or you can define a "parent-child" relationship by using the following two methods from the Raster class:
  • public Raster createChild(int px, int py, int w, int h, int cx, int cy, int[] bands): This method creates a "child" of the current Raster with the following specifications:
    • The (px,py,w,h) parameters define the rectangular area that will be shared by the selected raster with its "child". The px,py parameters represents the coordinates of the current raster origins.
    • The cx,cy parameters represent the coordinates of origins for the "child" raster. This can be the same as the px,py parameters or can be different.
    • The bands parameter represents a subset of color bands that belong to the current raster. This subset of bands belongs to the "child" raster, as well. If this parameter value is null, then all the bands that belong to the selected raster will also belong to the "child" raster.
  • public Raster createTranslatedChild(int cx, int cy): This method creates a "child" raster identical to the selected raster. In this case, the "child" raster will have other coordinates of the origin, (cx,cy).


Figure 3. Parent-Child: Two rasters that share the same DataBuffer object.

You should know that the WritableRaster class also has its own methods for defining parent-child relationships with WritableRaster objects:

  • public WritableRaster createWritableChild(int px, int py, int w, int h, int cx, int cy, int[] bands)
  • public WritableRaster createWritableTranslatedChild(int cx, int cy)
To retrieve the "parent" of a raster, use this getParent method:
public Raster getParent()

Translation of a Raster's Origins

The coordinates of origin for a raster can be any cartesian coordinates. Unfortunately, the SampleModel coordinates of origin must be always (0,0). For compatibility between these two systems of coordinates, you must translate the origin of the SampleModel to the origin of the raster using the following methods:
  • public final int getSampleModelTranslateX()
  • public final int getSampleModelTranslateY()

Apply Filters to Rasters

Now it's time to learn how to apply filters to rasters. You'll be using the most common filters found in the Java2D API.

Note: If you are not familiar with the Java2D API—especially the Ops classes and interfaces—you can find some very helpful resources here.

Here's a list of the principal Java 2D API classes you can use for image filters:

  • java.awt.image.AffineTransformOp: This provides methods for filters like translations, rotations, etc.
  • java.awt.image.ConvolveOp: This provides methods for filters based on the convolution operator.
  • java.awt.image.LookupOp: This provides methods for filters used for colors transformations based on a table of colors.
  • java.awt.image.ColorConvertOp: This provides methods for changing the colors of an image.
  • java.awt.image.RescaleOp: This provides methods for rescaling an image.
  • java.awt.image.BandCombineOp: This provides methods for creating filters based on a table of values. This class is specific only to rasters.
All of the above classes implement the java.awt.image.RasterOp interface, which defines the method you call when you want to apply a filter to a raster:
  • WritableRaster filter(Raster sourceR, WritableRaster destR): This applies a filter to the sourceR raster and returns the result to the destR raster.
Applying a filter to a raster is a two steps process. First, create an instance of one of the above classes. Second, call the filter method.

Listing 4 applies a filter (created with the ConvolveOp class) to a raster obtained from a BufferedImage. This filter is known as the "sharpen" filter.


Figure 4. RasterConvolveOp.java: A possible result.

Listing 5 shows an AffineTransformOp filter applied to a raster obtained for an array of pixels. The filter consists in scaling the raster.


Figure 5. RasterAffineTransformOp.java: A possible result.

A particulary useful set of filters you can apply to a raster are the java.awt.image.BandCombineOp filters. These filters are for use only with rasters and are based on the multiplication of a bands table with a table of values. The number of columns from the table of values must be equal or greater to the number of bands from the raster. If the table of values has the same number of columns as the number of raster bands plus 1, then a value of 1 will be added at the end of the bands table. Also, the number of rows from the table of values must be equal to the number of bands from the table bands.


Figure 6. The java.awt.image.BandCombineOp Filters: The table of values and the raster bands table.

public BandCombineOp(float[][] table, RenderingHints rh) constructs the BandCombineOp class by creating a BandCombineOp object with the specified table of values. The rh parameter represents a hint.

Listing 6 demonstrates how to apply a BandCombineOp filter to a raster to obtain a cubist style image.


Figure 7. RasterBandCombineOp.java: A possible result.

A Powerful Tool

As you can see, rasters are fundamental in image proceesing, especially when you want a full control over the image pixels. They're a very powerful tool when it comes to applications with image filters, video processing, movie tricks, photo adjusting, video games, creating transparency, and creating opacity. Digital image artists understand that an image is just a collection of 10, 100, 1000000 rasters that can give an image a new face.

Home / Articles / Using Rasters for Image Processing, Part 2 / 1 / 2 / 3 /

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%.

RIM Ups Ante With Mobile Software Push
Novell Readies Silverlight Clone for Linux
Yahoo Pitches The 'Next Generation of Search'
Alfresco's Latest ECM: Prying Open a Sector?
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

Create Secure Java Applications Productively, Part 1: Use Rational Application Developer and Data Studio
.NET Building Blocks: Custom User Control Fundamentals
Secure Internet File-Sharing with PHP, MySQL, and JavaScript
Getting Started with TBB on Windows
Moving to VoIP: Should You Go It Alone?
Introduction to the WPF Command Framework
7.0, Microsoft's Lucky Version?
Will Hyper-V Make VMware This Decade's Netscape?
Eliminate Fragmentation Frustration with Netbiscuits
Taming Trees: Building Branching Structures

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: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
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