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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 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%.

Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'
Oracle Focuses on Grid, Developer Tools

eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization
Microsoft Shows Some Ankle With Visual Studio
Top 5 Reasons to Adopt SQL Server 2008
Make Application Development Easy With Software Plus Services
SharePoint Applied: 10 Things You Wish They Had Told You?Part 2
Introducing Zend_Search_Lucene

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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES