Skip to content

Commit

Permalink
Added getImageType method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayahr committed Oct 31, 2009
1 parent 414ff90 commit 5a8a6c5
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 61 deletions.
27 changes: 22 additions & 5 deletions src/main/java/de/ailis/scilter/AbstractScaleFilter.java
Expand Up @@ -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$
*/
Expand All @@ -35,7 +37,7 @@ public abstract class AbstractScaleFilter implements ScaleFilter
* @see ScaleFilter#scale(java.awt.image.BufferedImage)
*/

public BufferedImage scale(BufferedImage image)
public BufferedImage scale(final BufferedImage image)
{
int[] pixels;
int width, height;
Expand All @@ -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;
}
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/de/ailis/scilter/ScaleFilter.java
Expand Up @@ -36,10 +36,25 @@ public interface ScaleFilter
*
* @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.
*
Expand All @@ -59,7 +74,8 @@ public interface ScaleFilter
* The pixel array
* @param width
* The image width
* @param height The image height
* @param height
* The image height
* @return The scaled pixel data
*/

Expand Down
36 changes: 24 additions & 12 deletions src/main/java/de/ailis/scilter/filter/Hq2xFilter.java
Expand Up @@ -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;

Expand Down Expand Up @@ -73,7 +75,7 @@ public class Hq2xFilter extends AbstractScaleFilter

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)
Expand All @@ -91,7 +93,7 @@ private static int[] buildLut16to32()

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++)
Expand Down Expand Up @@ -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);
}
Expand All @@ -146,7 +148,7 @@ public int[] scale(int[] image, int width, int height)
* @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;
}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -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];
Expand All @@ -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;

Expand Down Expand Up @@ -3054,4 +3056,14 @@ private int[] scalePixels(int[] pixels, int width, int height)

return newPixels;
}


/**
* @see de.ailis.scilter.ScaleFilter#getImageType()
*/

public int getImageType()
{
return BufferedImage.TYPE_INT_RGB;
}
}
36 changes: 24 additions & 12 deletions src/main/java/de/ailis/scilter/filter/Hq3xFilter.java
Expand Up @@ -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;

Expand Down Expand Up @@ -73,7 +75,7 @@ public class Hq3xFilter extends AbstractScaleFilter

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)
Expand All @@ -91,7 +93,7 @@ private static int[] buildLut16to32()

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++)
Expand Down Expand Up @@ -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);
}
Expand All @@ -146,7 +148,7 @@ public int[] scale(int[] image, int width, int height)
* @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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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];
Expand All @@ -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;

Expand Down Expand Up @@ -4495,4 +4497,14 @@ private int[] scalePixels(int[] pixels, int width, int height)

return newPixels;
}


/**
* @see de.ailis.scilter.ScaleFilter#getImageType()
*/

public int getImageType()
{
return BufferedImage.TYPE_INT_RGB;
}
}

0 comments on commit 5a8a6c5

Please sign in to comment.