Advertisement
Guest User

Bitmap class

a guest
Dec 25th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package com.dazkins.CityTycoon.gfx;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.DataBufferInt;
  5. import java.util.Arrays;
  6. import java.util.Random;
  7.  
  8. public class Bitmap implements Cloneable{
  9.     public int w,h;
  10.     public int[] pixels;
  11.    
  12.     public Bitmap(Bitmap b){
  13.         this.w=b.w;
  14.         this.h=b.h;
  15.         this.pixels=b.pixels;
  16.     }
  17.    
  18.     public Bitmap(BufferedImage img){
  19.         if(img!=null){     
  20.             this.w=img.getWidth();
  21.             this.h=img.getHeight();
  22.             pixels=((DataBufferInt)img.getRaster().getDataBuffer()).getData();
  23.         }
  24.     }
  25.    
  26.     public Bitmap(int w, int h, int[] pixels){
  27.         this.w=w;
  28.         this.h=h;
  29.         this.pixels=pixels;
  30.     }
  31.    
  32.     public Bitmap(int w,int h,int color){
  33.         this.w=w;
  34.         this.h=h;
  35.         this.pixels = new int[w*h];
  36.         for(int i = 0; i < w * h; i++){
  37.             this.pixels[i] = color;
  38.         }
  39.     }
  40.    
  41.     public Bitmap(int width, int height) {
  42.         this.w=width;
  43.         this.h=height;
  44.         this.pixels = new int[w*h];
  45.     }
  46.  
  47.     public void randomize(){
  48.         for(int i=0;i<pixels.length;i++){
  49.             pixels[i] = new Random().nextInt(0xFFFFFF);
  50.         }
  51.     }
  52.    
  53.     public void render(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip){
  54.         int x0 = xp;
  55.         int y0 = yp;
  56.         int x1 = xp + b.w;
  57.         int y1 = yp + b.h;
  58.        
  59.         if(x0 < 0) x0 = 0;
  60.         if(x1 > this.w) x1 = this.w;
  61.         if(y0 < 0) y0 = 0;
  62.         if(y1 > this.h) y1 = this.h;
  63.        
  64.         for(int x = x0; x < x1; x++){
  65.             for(int y = y0; y < y1; y++){
  66.                 int pixelPos = (x-xp) + ((y-yp)*b.w);
  67.                 if(xFlip && !yFlip){
  68.                     pixelPos = (b.w-(x-xp+1)) + (((y-yp))*b.w);
  69.                 }else if(yFlip && !xFlip){
  70.                     pixelPos = ((x-xp)) + ((b.h-(y-yp+1))*b.w);
  71.                 }else if(yFlip && xFlip){
  72.                     pixelPos = (b.w-(x-xp+1)) + ((b.h-(y-yp+1))*b.w);
  73.                 }
  74.                 if(b.pixels[pixelPos] != 0xFFFF00FF){
  75.                     this.pixels[x+y*w] = b.pixels[pixelPos];
  76.                 }
  77.             }
  78.         }
  79.     }
  80.    
  81.     public void renderWithModifiedColour(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col){
  82.         Bitmap b0 = new Bitmap(b.w,b.h);
  83.        
  84.         for(int i=0;i<b.pixels.length;i++){
  85.             b0.pixels[i] = b.pixels[i];
  86.         }
  87.        
  88.         for(int i=0;i<b0.pixels.length;i++){
  89.             if(b0.pixels[i] != 0xFFFF00FF){
  90.                 b0.pixels[i]+=col;
  91.             }
  92.         }
  93.  
  94.         render(b,xp,yp,xFlip,yFlip);
  95.     }
  96.    
  97.     public void renderWithColourReplace(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col){
  98.         Bitmap b0 = new Bitmap(b.w,b.h);
  99.        
  100.         for(int i=0;i<b.pixels.length;i++){
  101.             b0.pixels[i] = b.pixels[i];
  102.         }
  103.        
  104.         for(int i=0;i<b0.pixels.length;i++){
  105.             if(b0.pixels[i] != 0xFFFF00FF){
  106.                 b0.pixels[i] = col;
  107.             }
  108.         }
  109.        
  110.         render(b0,xp,yp,xFlip,yFlip);
  111.     }
  112.    
  113.     public void renderWithColourOffset(Bitmap b,int xp,int yp,boolean xFlip,boolean yFlip,int col1[],int col2[]){
  114.         Bitmap b0 = new Bitmap(b);
  115.        
  116.         for(int i=0;i<b0.pixels.length;i++){
  117.             for(int c=0;c<col1.length;c++){
  118.                 if(b0.pixels[i]==col1[c]){
  119.                     b0.pixels[i] = col2[c];
  120.                 }
  121.             }
  122.         }
  123.        
  124.         render(b0,xp,yp,xFlip,yFlip);
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement