Skip to main content
gamedev.net
🔒 Locked

[java] Working with Pixels

Started by caseyd 0 replies 500+ views
Original Post
caseyd
caseyd
I've been messing around with the PixelGrabber and trying to access things directly through BufferedImage and I can't help but wonder if I am taking the long way around. What is the fastest way to directly access pixels of some image? Thanks, Casey
Son of Cain
Son of Cain
I use getRGB().

I wrote this a long time ago, and for a very quick task of changing the value of the alpha channel of all transparent pixels in a PNG image. I could get GIMP working for the task, but I didn't want to bother to learn how to perform this with it. So, I wrote this messy code, that might be helpful to you, only if to know what to avoid :p

import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.FilenameFilter;import javax.imageio.ImageIO;/** * * @author rodrigo */public class Main {        private static int getAsRgb(int a, int r, int g, int b) {        return ((a & 0xFF) << 24) | ((r & 0xFF) << 16) |                ((g & 0xFF) <<  8) | ((b & 0xFF) <<  0);    }        public static void main(String[] args) {        try {                        String url = "/home&#47;rodrigo&#<span class="java-number">47</span>;projects&#<span class="java-number">47</span>;SIInfo&#<span class="java-number">47</span>;FirstDay&#<span class="java-number">47</span>;resources&#<span class="java-number">47</span>;"</span>;<br>            File dir = <span class="java-keyword">new</span> File(url);<br>            <br>            File[] files = dir.listFiles(<span class="java-keyword">new</span> FilenameFilter() {<br>                <span class="java-visibilitymodifier">public</span> <span class="java-primitives">boolean</span> accept(File dir, String name) {<br>                    <span class="java-keyword">return</span> name.endsWith(<span class="java-literal">".png"</span>);<br>                }<br>            });<br>            <br>            <span class="java-primitives">int</span> rgb = getAsRgb(<span class="java-number">0</span>, <span class="java-number">0</span>, <span class="java-number">0</span>, <span class="java-number">255</span>);<br>            <br>            <span class="java-keyword">for</span> ( File f : files ) {<br>                <br>                BufferedImage img = ImageIO.read(f);<br>                <span class="java-primitives">int</span> w = img.getWidth(<span class="java-keyword">null</span>);<br>                <span class="java-primitives">int</span> h = img.getHeight(<span class="java-keyword">null</span>);<br>                <br>                <span class="java-comment">// all pixels</span><br>                <span class="java-primitives">int</span>[] pixels = <span class="java-keyword">new</span> <span class="java-primitives">int</span> [ w * h ];<br>                img.getRGB(<span class="java-number">0</span>, <span class="java-number">0</span>, w, h, pixels, <span class="java-number">0</span>, w);<br>                <br>                <span class="java-primitives">int</span>[] converted = <span class="java-keyword">new</span> <span class="java-primitives">int</span> [ pixels.length ];<br>                <span class="java-keyword">for</span> ( <span class="java-primitives">int</span> x = <span class="java-number">0</span> ; x &lt; pixels.length ; x ++ )<br>                    converted[x] = ( ((pixels[x] &amp; 0xFF000000) == <span class="java-number">0</span> ) ) ? rgb : pixels[x];<br>                img.setRGB(<span class="java-number">0</span>, <span class="java-number">0</span>, w, h, converted, <span class="java-number">0</span>, w);<br>                <br>                String filename = dir.getPath() + f.getName();<br>                ImageIO.write(img, <span class="java-literal">"png"</span>, <span class="java-keyword">new</span> FileOutputStream(filename));<br>            }<br>            <br>            System.err.printf(<span class="java-literal">"%nPixel conversion successfull.%n"</span>);<br>            <br>        } <span class="java-keyword">catch</span> (Exception e) {<br>            e.printStackTrace();<br>        }<br>    }<br>    <br>}<br>[/java]<br><br></pre></div><!--ENDSCRIPT--><br><br>Edit: For those wondering why I was trying to do that, it was for the sake of reusing images in a web application, PNG with a transparent background; As you know, it works well in Firefox, but not in the old IE 6...<br><br><!--EDIT--><span class=editedby><!--/EDIT-->[Edited by - Son of Cain on March 28, 2007 3:17:48 PM]<!--EDIT--></span><!--/EDIT--></code></pre></div>
a.k.a javabeats at yahoo.ca

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.