package Phase2;
import magick.ImageInfo;
import magick.MagickImage;
import magick.MagickException;
import magick.MagickApiException;
import magick.ColorspaceType;
import java.awt.Dimension;
import magick.PixelPacket;
public class TASK1
{
public static MagickImage scaleColorTable(MagickImage image, int choice)
{
try
{
Dimension dimensions = image.getDimension();
float[] tempY = new float[dimensions.width * dimensions.height];
float[] tempU = new float[dimensions.width * dimensions.height];
float[] tempV = new float[dimensions.width * dimensions.height];
float tempYcounter = 0;
float tempUcounter = 0;
float tempVcounter = 0;
byte[] newPixels = new byte[dimensions.width * dimensions.height* 3];
for (int i = 0; i < dimensions.height; i++)
{
for (int j = 0; j < dimensions.width; j++)
{
PixelPacket pp = image.getOnePixel(j, i);
float x = pp.getRed();
float y = pp.getGreen();
float z = pp.getBlue();
float[] abc;
abc = new float[10];
rgb2yuv((float) x, y, z, abc);
tempY[(int) tempYcounter] = abc[0];
tempYcounter++;
}
}
for (int i = 0; i < dimensions.height; i++)
{
for (int j = 0; j < dimensions.width; j++)
{
PixelPacket pp = image.getOnePixel(j, i);
float x = pp.getRed();
float y = pp.getGreen();
float z = pp.getBlue();
float[] abc;
abc = new float[10];
rgb2yuv((float) x, y, z, abc);
tempU[(int) tempUcounter] = abc[1];
tempUcounter++;
}
}
for (int i = 0; i < dimensions.height; i++)
{
for (int j = 0; j < dimensions.width; j++)
{
PixelPacket pp = image.getOnePixel(j, i);
float x = pp.getRed();
float y = pp.getGreen();
float z = pp.getBlue();
float[] abc;
abc = new float[10];
rgb2yuv((float) x, y, z, abc);
tempV[(int) tempVcounter] = abc[2];
tempVcounter++;
}
}
if(choice == 1){
PC1(tempY);
PC1(tempU);
PC1(tempV);
}
if(choice == 2){
PC2(tempY);
PC2(tempU);
PC2(tempV);
}
if(choice == 3){
PC3(tempY);
PC3(tempU);
PC3(tempV);
}
if(choice == 4){
PC4(tempY);
PC4(tempU);
PC4(tempV);
}
if(choice == 5){
PC5(tempY);
PC5(tempU);
PC5(tempV);
}
if(choice == 6){
PC6(tempY);
PC6(tempU);
PC6(tempV);
}
image = new MagickImage();
image.constituteImage(dimensions.width, dimensions.height, "RGB", newPixels);
}
catch (MagickApiException ex)
{
System.err.println("MagickException: " + ex + ": " + ex.getReason()
+ ", " + ex.getDescription());
ex.printStackTrace();
}
catch (MagickException ex)
{
System.err.println("MagickException: " + ex);
ex.printStackTrace();
}
return image;
}
public static void PC1(float[] array) {
float[] arr2 = new float[array.length];
for(int i=0; i<array.length; i++)
{
arr2[i] = array[i];
}
System.out.println();
PRINT(arr2);
}
public static void PC2(float[] array) {
float[] arr2 = new float[array.length];
for(int i=0; i<array.length; i++)
{
if(i==0)
{
arr2[i] = array[i];
}
else
arr2[i] = array[i] - array[i-1];
}
System.out.println();
PRINT(arr2);
}
public static void PC3(float[] array) {
float[] arr2 = new float[array.length];
for(int i=0; i<array.length; i++)
{
if(i==0)
{
arr2[i] = array[i];
}
else if (i==1)
{
arr2[i] = array[i] - array[i-1];
}
else
arr2[i] = array[i]-((array[i-1] + array[i-2])/2);
}
System.out.println();
PRINT(arr2);
}
public static void PC4(float[] array) {
float[] arr2 = new float[array.length];
for(int i=0; i<array.length; i++)
{
{
if(i==0)
{
arr2[i] = array[i];
}
else if (i==1)
{
arr2[i] = array[i] - array[i-1];
}
else
arr2[i] = array[i]-((2*array[i-1] + array[i-2])/3);
}
}
System.out.println();
PRINT(arr2);
}
public static void PC5(float[] array) {
float[] arr2 = new float[array.length];
for(int i=0; i<array.length; i++)
{
if(i==0)
{
arr2[i] = array[i];
}
else if (i==1)
{
arr2[i] = array[i] - array[i-1];
}
else
arr2[i] = array[i]-((array[i-1] + 2*array[i-2])/3);
}
System.out.println();
PRINT(arr2);
}
public static void PC6(float[] array) {
float[] arr2 = new float[array.length];
for(int i=0; i<array.length; i++)
{
if(i==0)
{
arr2[i] = array[i];
}
else if (i==1)
{
arr2[i] = array[i] - (10*array[i-1])/10;
}
else if (i==2)
{
arr2[i] = array[i] - (array[i-1]+9*array[i-2])/10;
}
else if (i==3)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+8*array[i-3])/10;
}
else if (i==4)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+7*array[i-4])/10;
}
else if (i==5)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+array[i-4]+6*array[i-5])/10;
}
else if (i==6)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+array[i-4]+array[i-5]+5*array[i-6])/10;
}
else if (i==7)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+array[i-4]+array[i-5]+array[i-6]+4*array[i-7])/10;
}
else if (i==8)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+array[i-4]+array[i-5]+array[i-6]+array[i-7]+3*array[i-8])/10;
}
else if (i==9)
{
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+array[i-4]+array[i-5]+array[i-6]+array[i-7]+array[i-8]+2*array[i-9])/10;
}
else
arr2[i] = array[i] - (array[i-1]+array[i-2]+array[i-3]+array[i-4]+array[i-5]+array[i-6]+array[i-7]+array[i-8]+array[i-9]+array[i-10])/10;
}
System.out.println();
PRINT(arr2);
}
public static void PRINT(float[] array) {
for(int x=0; x<array.length; x++) {
System.out.print(array[x] + " ");
}
System.out.println("\n");
}
private static float[] rgb2yuv(float x, float y2, float z, float[] abc)
{
float y = (float) (0.299 * x + 0.587 * y2 + 0.114 * z);
float u = (float) ((z - y) * 0.492f);
float v = (float) ((x - y) * 0.877f);
abc[0] = y;
abc[1] = u;
abc[2] = v;
return abc;
}
public static void main(String filepath, int choice)
{
try
{
ImageInfo info = new ImageInfo(filepath);
info.setColorspace(ColorspaceType.RGBColorspace);
MagickImage image = new MagickImage(info);
@SuppressWarnings("unused")
MagickImage alteredImage = scaleColorTable(image, choice);
}
catch (MagickApiException ex)
{
System.err.println("MagickException: " + ex + ": " + ex.getReason()
+ ", " + ex.getDescription());
ex.printStackTrace();
}
catch (MagickException ex)
{
System.err.println("MagickException: " + ex);
ex.printStackTrace();
}
}
}
This is the algorithm to do predictive coding and produce seperate YUV streams our school group finished. I was wondering if theres some example or algorithm I could use to implement quantization to start encoding?