From b7363d3a819fac57e0c83afa46346f3fdb9a17f8 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 1 Apr 2014 21:07:10 -0400 Subject: [PATCH] extremely minor performance tweaks tested with criterium benchmarks, on my imac, marking all these local vars final helps out by 0.3ms... negligible, but sorta interesting! --- src/java/toascii/images/ImageToAscii.java | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/java/toascii/images/ImageToAscii.java b/src/java/toascii/images/ImageToAscii.java index 9f9da5a..bfc389f 100644 --- a/src/java/toascii/images/ImageToAscii.java +++ b/src/java/toascii/images/ImageToAscii.java @@ -4,36 +4,37 @@ import java.awt.image.BufferedImage; public class ImageToAscii { static final char[] asciiChars = {'#', 'A', '@', '%', '$', '+', '=', '*', ':', ',', '.', ' '}; + static final int numAsciiChars = asciiChars.length - 1; static final int spanLength = "X".length(); static final int lineTerminatorLength = "
".length(); public static String convert(BufferedImage image, boolean useColor) { - int width = image.getWidth(); - int height = image.getHeight(); + final int width = image.getWidth(); + final int height = image.getHeight(); - int maxLength = (useColor ? - (width * height * spanLength) + (height * lineTerminatorLength) : - (width * height) + height); + final int maxLength = (useColor ? + (width * height * spanLength) + (height * lineTerminatorLength) : + (width * height) + height); - StringBuilder sb = new StringBuilder(maxLength); + final StringBuilder sb = new StringBuilder(maxLength); - int[] pixels = image.getRGB(0, 0, width, height, null, 0, width); + final int[] pixels = image.getRGB(0, 0, width, height, null, 0, width); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { - int argb = pixels[(y * width) + x]; - int r = (0x00ff0000 & argb) >> 16; - int g = (0x0000ff00 & argb) >> 8; - int b = (0x000000ff & argb); - int brightness = (int)Math.sqrt((r * r * 0.241f) + + final int argb = pixels[(y * width) + x]; + final int r = (0x00ff0000 & argb) >> 16; + final int g = (0x0000ff00 & argb) >> 8; + final int b = (0x000000ff & argb); + final double brightness = Math.sqrt((r * r * 0.241f) + (g * g * 0.691f) + (b * b * 0.068f)); int charIndex; if (brightness == 0.0f) - charIndex = asciiChars.length - 1; + charIndex = numAsciiChars; else - charIndex = (int)((brightness / 255.0f) * asciiChars.length) - 1; + charIndex = (int)((brightness / 255.0f) * numAsciiChars); - char pixelChar = asciiChars[charIndex > 0 ? charIndex : 0]; + final char pixelChar = asciiChars[charIndex > 0 ? charIndex : 0]; if (useColor) { sb.append("