package com.dazkins.CityTycoon.gfx;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Arrays;
import java.util.Random;
public class Bitmap implements Cloneable{
public int w,h;
public int[] pixels;
public Bitmap(Bitmap b){
this.w=b.w;
this.h=b.h;
this.pixels=b.pixels;
}
public Bitmap(BufferedImage img){
if(img!=null){
this.w=img.getWidth();
this.h=img.getHeight();
pixels=((DataBufferInt)img.getRaster().getDataBuffer()).getData();
}
}
public Bitmap(int w, int h, int[] pixels){
this.w=w;
this.h=h;
this.pixels=pixels;
}
public Bitmap(int w,int h,int color){
this.w=w;
this.h=h;
this.pixels = new int[w*h];
for(int i = 0; i < w * h; i++){
this.pixels[i] = color;
}
}
public Bitmap(int width, int height) {
this.w=width;
this.h=height;
this.pixels = new int[w*h];
}
public void randomize(){
for(int i=0;i<pixels.length;i++){
pixels[i] = new Random().nextInt(0xFFFFFF);
}
}
public void render(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip){
int x0 = xp;
int y0 = yp;
int x1 = xp + b.w;
int y1 = yp + b.h;
if(x0 < 0) x0 = 0;
if(x1 > this.w) x1 = this.w;
if(y0 < 0) y0 = 0;
if(y1 > this.h) y1 = this.h;
for(int x = x0; x < x1; x++){
for(int y = y0; y < y1; y++){
int pixelPos = (x-xp) + ((y-yp)*b.w);
if(xFlip && !yFlip){
pixelPos = (b.w-(x-xp+1)) + (((y-yp))*b.w);
}else if(yFlip && !xFlip){
pixelPos = ((x-xp)) + ((b.h-(y-yp+1))*b.w);
}else if(yFlip && xFlip){
pixelPos = (b.w-(x-xp+1)) + ((b.h-(y-yp+1))*b.w);
}
if(b.pixels[pixelPos] != 0xFFFF00FF){
this.pixels[x+y*w] = b.pixels[pixelPos];
}
}
}
}
public void renderWithModifiedColour(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col){
Bitmap b0 = new Bitmap(b.w,b.h);
for(int i=0;i<b.pixels.length;i++){
b0.pixels[i] = b.pixels[i];
}
for(int i=0;i<b0.pixels.length;i++){
if(b0.pixels[i] != 0xFFFF00FF){
b0.pixels[i]+=col;
}
}
render(b,xp,yp,xFlip,yFlip);
}
public void renderWithColourReplace(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col){
Bitmap b0 = new Bitmap(b.w,b.h);
for(int i=0;i<b.pixels.length;i++){
b0.pixels[i] = b.pixels[i];
}
for(int i=0;i<b0.pixels.length;i++){
if(b0.pixels[i] != 0xFFFF00FF){
b0.pixels[i] = col;
}
}
render(b0,xp,yp,xFlip,yFlip);
}
public void renderWithColourOffset(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col1[],int col2[]){
Bitmap b0 = new Bitmap(b);
for(int i=0;i<b0.pixels.length;i++){
for(int c=0;c<col1.length;c++){
if(b0.pixels[i]==col1[c]){
b0.pixels[i] = col2[c];
}
}
}
render(b0,xp,yp,xFlip,yFlip);
}
}