From: Toby D. <tj...@sf...> - 2005-07-22 21:38:58
|
Hi, Can anyone help me to figure out why the code below is so slow? It converts and image to grayscale pixel by pixel, but when I've run it (on two different computers --- Windows and Mac OS X), it takes 20 or more seconds to process a medium sized image. I would have expected it take less than a second or so. The bit-twiddling code should be quite efficient, since such operations are (hopefully) converted to single assembly-language instructions. I've been using JDK 5.0 (aka 1.5), so maybe that's causing the problem? Toby -- Dr. Toby Donaldson School of Computing Science Simon Fraser University (Surrey) public static void dprint(String s) { System.out.println(s + " " + (new Date())); } public static void test4(String fname) { try { dprint("Creating buffered image:"); BufferedImage img = ImageIO.read(new File(fname)); dprint(".. done"); final int width = img.getWidth(); final int height = img.getHeight(); dprint("Starting grayscale conversion:"); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int rgb = img.getRGB(i, j); int b = (rgb & 0x000000FF); int g = (rgb & 0x0000FF00) >> 8; int r = (rgb & 0x00FF0000) >> 16; int lum = (int) ((r + g + b) / 3.0); int alpha = rgb & 0xFF000000; int red = lum << 16; int green = lum << 8; int blue = lum; int newARGB = alpha | red | green | blue; img.setRGB(i, j, newARGB); } } dprint("... done"); } catch (IOException e) { e.printStackTrace(); } } |