Permalink
Browse files

Added getImageType method.

  • Loading branch information...
kayahr committed Oct 31, 2009
1 parent 414ff90 commit 5a8a6c5159d1533095b2d9e68cce9334af63d4f4
@@ -20,11 +20,13 @@
package de.ailis.scilter;
import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.IndexColorModel;
/**
* A base class for the scale filters implementing some common functionality.
- *
+ *
* @author Klaus Reimer (k@ailis.de)
* @version $Revision$
*/
@@ -35,7 +37,7 @@
* @see ScaleFilter#scale(java.awt.image.BufferedImage)
*/
- public BufferedImage scale(BufferedImage image)
+ public BufferedImage scale(final BufferedImage image)
{
int[] pixels;
int width, height;
@@ -48,15 +50,30 @@ public BufferedImage scale(BufferedImage image)
height = image.getHeight();
// Scale the pixels
- pixels = scale(image.getRGB(0, 0, width, height, null, 0, width), width, height);
-
+ pixels = scale(image.getRGB(0, 0, width, height, null, 0, width),
+ width, height);
+
// Determine new picture size
scaleFactor = getScaleFactor();
newWidth = width * scaleFactor;
newHeight = height * scaleFactor;
// Create and return the new picture
- out = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
+ int outType = getImageType();
+ if (outType == -1)
+ {
+ outType = image.getType();
+ final ColorModel colorModel = image.getColorModel();
+ if (colorModel instanceof IndexColorModel)
+ out = new BufferedImage(newWidth, newHeight, outType,
+ (IndexColorModel) colorModel);
+ else
+ out = new BufferedImage(newWidth, newHeight, outType);
+ }
+ else
+ {
+ out = new BufferedImage(newWidth, newHeight, outType);
+ }
out.setRGB(0, 0, newWidth, newHeight, pixels, 0, newWidth);
return out;
}
@@ -36,10 +36,25 @@
*
* @return The scale factor.
*/
-
+
public int getScaleFactor();
-
-
+
+
+ /**
+ * Returns the image type of the result this scale filter will produce. It
+ * may return -1 to indiciate that the filter is using the input image type
+ * as output image type (The nearest neighbor and normal filter will do this
+ * for example because the first one is not adding additional colors (so the
+ * image type is not important) and the second one does not change anything
+ * at all).
+ *
+ * @return The image type of the scaled result or -1 if it is always the
+ * input image type
+ */
+
+ public int getImageType();
+
+
/**
* Scales the specified image and returns the new scaled image.
*
@@ -59,7 +74,8 @@
* The pixel array
* @param width
* The image width
- * @param height The image height
+ * @param height
+ * The image height
* @return The scaled pixel data
*/
@@ -20,6 +20,8 @@
package de.ailis.scilter.filter;
+import java.awt.image.BufferedImage;
+
import de.ailis.scilter.AbstractScaleFilter;
import de.ailis.scilter.util.ColorUtils;
@@ -73,7 +75,7 @@
private static int[] buildLut16to32()
{
- int[] lut16to32 = new int[65536];
+ final int[] lut16to32 = new int[65536];
for (int i = 0; i < 65536; i++)
{
lut16to32[i] = ((i & 0xF800) << 8) + ((i & 0x07E0) << 5)
@@ -91,7 +93,7 @@
private static int[] buildRgbToYuv()
{
- int[] rgbToYuv = new int[65536];
+ final int[] rgbToYuv = new int[65536];
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 64; j++)
@@ -129,7 +131,7 @@ public int getScaleFactor()
* @see de.ailis.scilter.ScaleFilter#scale(int[], int, int)
*/
- public int[] scale(int[] image, int width, int height)
+ public int[] scale(final int[] image, final int width, final int height)
{
return scalePixels(ColorUtils.convertRgb888To565(image), width, height);
}
@@ -146,7 +148,7 @@ public int getScaleFactor()
* @param c2
*/
- private void interp1(int[] pixels, int offset, int c1, int c2)
+ private void interp1(final int[] pixels, final int offset, final int c1, final int c2)
{
pixels[offset] = (c1 * 3 + c2) >> 2;
}
@@ -164,7 +166,7 @@ private void interp1(int[] pixels, int offset, int c1, int c2)
* @param c3
*/
- private void interp2(int[] pixels, int offset, int c1, int c2, int c3)
+ private void interp2(final int[] pixels, final int offset, final int c1, final int c2, final int c3)
{
pixels[offset] = (c1 * 2 + c2 + c3) >> 2;
}
@@ -182,7 +184,7 @@ private void interp2(int[] pixels, int offset, int c1, int c2, int c3)
* @param c3
*/
- private void interp6(int[] pixels, int offset, int c1, int c2, int c3)
+ private void interp6(final int[] pixels, final int offset, final int c1, final int c2, final int c3)
{
pixels[offset] = ((((c1 & 0x00FF00) * 5 + (c2 & 0x00FF00) * 2 + (c3 & 0x00FF00)) & 0x0007F800) + (((c1 & 0xFF00FF)
* 5 + (c2 & 0xFF00FF) * 2 + (c3 & 0xFF00FF)) & 0x07F807F8)) >> 3;
@@ -201,7 +203,7 @@ private void interp6(int[] pixels, int offset, int c1, int c2, int c3)
* @param c3
*/
- private void interp7(int[] pixels, int offset, int c1, int c2, int c3)
+ private void interp7(final int[] pixels, final int offset, final int c1, final int c2, final int c3)
{
pixels[offset] = ((((c1 & 0x00FF00) * 6 + (c2 & 0x00FF00) + (c3 & 0x00FF00)) & 0x0007F800) + (((c1 & 0xFF00FF)
* 6 + (c2 & 0xFF00FF) + (c3 & 0xFF00FF)) & 0x07F807F8)) >> 3;
@@ -220,7 +222,7 @@ private void interp7(int[] pixels, int offset, int c1, int c2, int c3)
* @param c3
*/
- private void interp9(int[] pixels, int offset, int c1, int c2, int c3)
+ private void interp9(final int[] pixels, final int offset, final int c1, final int c2, final int c3)
{
pixels[offset] = ((((c1 & 0x00FF00) * 2 + ((c2 & 0x00FF00) + (c3 & 0x00FF00)) * 3) & 0x0007F800) + (((c1 & 0xFF00FF) * 2 + ((c2 & 0xFF00FF) + (c3 & 0xFF00FF)) * 3) & 0x07F807F8)) >> 3;
}
@@ -233,7 +235,7 @@ private void interp9(int[] pixels, int offset, int c1, int c2, int c3)
* @param w2
*/
- private boolean diff(int w1, int w2)
+ private boolean diff(final int w1, final int w2)
{
this.yuv1 = RGBTOYUV[w1];
this.yuv2 = RGBTOYUV[w2];
@@ -256,13 +258,13 @@ private boolean diff(int w1, int w2)
* @return The hq3x scaled pixel data
*/
- private int[] scalePixels(int[] pixels, int width, int height)
+ private int[] scalePixels(final int[] pixels, final int width, final int height)
{
int index, newIndex;
int newWidth, newHeight;
int prevLine, newLine;
- int w[] = new int[10];
- int c[] = new int[10];
+ final int w[] = new int[10];
+ final int c[] = new int[10];
int[] newPixels;
int newIndexWidth;
@@ -3054,4 +3056,14 @@ private boolean diff(int w1, int w2)
return newPixels;
}
+
+
+ /**
+ * @see de.ailis.scilter.ScaleFilter#getImageType()
+ */
+
+ public int getImageType()
+ {
+ return BufferedImage.TYPE_INT_RGB;
+ }
}
@@ -20,6 +20,8 @@
package de.ailis.scilter.filter;
+import java.awt.image.BufferedImage;
+
import de.ailis.scilter.AbstractScaleFilter;
import de.ailis.scilter.util.ColorUtils;
@@ -73,7 +75,7 @@
private static int[] buildLut16to32()
{
- int[] lut16to32 = new int[65536];
+ final int[] lut16to32 = new int[65536];
for (int i = 0; i < 65536; i++)
{
lut16to32[i] = ((i & 0xF800) << 8) + ((i & 0x07E0) << 5)
@@ -91,7 +93,7 @@
private static int[] buildRgbToYuv()
{
- int[] rgbToYuv = new int[65536];
+ final int[] rgbToYuv = new int[65536];
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 64; j++)
@@ -129,7 +131,7 @@ public int getScaleFactor()
* @see de.ailis.scilter.ScaleFilter#scale(int[], int, int)
*/
- public int[] scale(int[] image, int width, int height)
+ public int[] scale(final int[] image, final int width, final int height)
{
return scalePixels(ColorUtils.convertRgb888To565(image), width, height);
}
@@ -146,7 +148,7 @@ public int getScaleFactor()
* @param c2
*/
- private void interp1(int[] pixels, int offset, int c1, int c2)
+ private void interp1(final int[] pixels, final int offset, final int c1, final int c2)
{
pixels[offset] = (c1 * 3 + c2) >> 2;
}
@@ -164,7 +166,7 @@ private void interp1(int[] pixels, int offset, int c1, int c2)
* @param c3
*/
- private void interp2(int[] pixels, int offset, int c1, int c2, int c3)
+ private void interp2(final int[] pixels, final int offset, final int c1, final int c2, final int c3)
{
pixels[offset] = (c1 * 2 + c2 + c3) >> 2;
}
@@ -180,7 +182,7 @@ private void interp2(int[] pixels, int offset, int c1, int c2, int c3)
* @param c2
*/
- private void interp3(int[] pixels, int offset, int c1, int c2)
+ private void interp3(final int[] pixels, final int offset, final int c1, final int c2)
{
pixels[offset] = ((((c1 & 0x00FF00) * 7 + (c2 & 0x00FF00)) & 0x0007F800) + (((c1 & 0xFF00FF) * 7 + (c2 & 0xFF00FF)) & 0x07F807F8)) >> 3;
}
@@ -198,7 +200,7 @@ private void interp3(int[] pixels, int offset, int c1, int c2)
* @param c3
*/
- private void interp4(int[] pixels, int offset, int c1, int c2, int c3)
+ private void interp4(final int[] pixels, final int offset, final int c1, final int c2, final int c3)
{
pixels[offset] = ((((c1 & 0x00FF00) * 2 + ((c2 & 0x00FF00) + (c3 & 0x00FF00)) * 7) & 0x000FF000) + (((c1 & 0xFF00FF) * 2 + ((c2 & 0xFF00FF) + (c3 & 0xFF00FF)) * 7) & 0x0FF00FF0)) >> 4;
}
@@ -215,7 +217,7 @@ private void interp4(int[] pixels, int offset, int c1, int c2, int c3)
* @param c2
*/
- private void interp5(int[] pixels, int offset, int c1, int c2)
+ private void interp5(final int[] pixels, final int offset, final int c1, final int c2)
{
pixels[offset] = (c1 + c2) >> 1;
}
@@ -228,7 +230,7 @@ private void interp5(int[] pixels, int offset, int c1, int c2)
* @param w2
*/
- private boolean diff(int w1, int w2)
+ private boolean diff(final int w1, final int w2)
{
this.yuv1 = RGBTOYUV[w1];
this.yuv2 = RGBTOYUV[w2];
@@ -251,13 +253,13 @@ private boolean diff(int w1, int w2)
* @return The hq3x scaled pixel data
*/
- private int[] scalePixels(int[] pixels, int width, int height)
+ private int[] scalePixels(final int[] pixels, final int width, final int height)
{
int index, newIndex;
int newWidth, newHeight, newWidth2;
int prevLine, newLine;
- int w[] = new int[10];
- int c[] = new int[10];
+ final int w[] = new int[10];
+ final int c[] = new int[10];
int[] newPixels;
int newIndexWidth, newIndexWidth2;
@@ -4495,4 +4497,14 @@ private boolean diff(int w1, int w2)
return newPixels;
}
+
+
+ /**
+ * @see de.ailis.scilter.ScaleFilter#getImageType()
+ */
+
+ public int getImageType()
+ {
+ return BufferedImage.TYPE_INT_RGB;
+ }
}
Oops, something went wrong.

0 comments on commit 5a8a6c5

Please sign in to comment.