Advertisement
Chiddix

IndexedImage

Jan 31st, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1. public class IndexedImage extends Rasterizer {
  2.    
  3.     public byte[] pixels;
  4.     public int[] palette;
  5.     public int width;
  6.     public int height;
  7.     public int xDrawOffset;
  8.     public int yDrawOffset;
  9.     public int maxWidth;
  10.     public int maxHeight;
  11.  
  12.     public IndexedImage(Archive archive, String archiveName, int offset) {
  13.         Buffer dataBuffer = new Buffer(archive.getFile(archiveName + ".dat"));
  14.         Buffer indexBuffer = new Buffer(archive.getFile("index.dat"));
  15.         indexBuffer.offset = dataBuffer.getUnsignedLEShort();
  16.         maxWidth = indexBuffer.getUnsignedLEShort();
  17.         maxHeight = indexBuffer.getUnsignedLEShort();
  18.         int palleteLength = indexBuffer.getUnsignedByte();
  19.         palette = new int[palleteLength];
  20.         for (int index = 0; index < palleteLength - 1; index++) {
  21.             palette[index + 1] = indexBuffer.get24BitInt();
  22.         }
  23.         for (int counter = 0; counter < offset; counter++) {
  24.             indexBuffer.offset += 2;
  25.             dataBuffer.offset += indexBuffer.getUnsignedLEShort() * indexBuffer.getUnsignedLEShort();
  26.             indexBuffer.offset++;
  27.         }
  28.         xDrawOffset = indexBuffer.getUnsignedByte();
  29.         yDrawOffset = indexBuffer.getUnsignedByte();
  30.         width = indexBuffer.getUnsignedLEShort();
  31.         height = indexBuffer.getUnsignedLEShort();
  32.         int type = indexBuffer.getUnsignedByte();
  33.         int pixelLength = width * height;
  34.         pixels = new byte[pixelLength];
  35.         if (type == 0) {
  36.             for (int pixel = 0; pixel < pixelLength; pixel++) {
  37.                 pixels[pixel] = dataBuffer.get();
  38.             }
  39.         } else if (type == 1) {
  40.             for (int x = 0; x < width; x++) {
  41.                 for (int y = 0; y < height; y++) {
  42.                     pixels[x + y * width] = dataBuffer.get();
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     public void resizeToHalfMax() {
  49.         maxWidth /= 2;
  50.         maxHeight /= 2;
  51.         byte[] resizedPixels = new byte[maxWidth * maxHeight];
  52.         int pixelCount = 0;
  53.         for (int x = 0; x < height; x++) {
  54.             for (int y = 0; y < width; y++) {
  55.                 resizedPixels[(y + xDrawOffset >> 1) + (x + yDrawOffset >> 1) * maxWidth] = pixels[pixelCount++];
  56.             }
  57.         }
  58.         pixels = resizedPixels;
  59.         width = maxWidth;
  60.         height = maxHeight;
  61.         xDrawOffset = 0;
  62.         yDrawOffset = 0;
  63.     }
  64.  
  65.     public void resizeToMax() {
  66.         if (width != maxWidth || height != maxHeight) {
  67.             byte[] resizedPixels = new byte[maxWidth * maxHeight];
  68.             int pixelCount = 0;
  69.             for (int y = 0; y < height; y++) {
  70.                 for (int x = 0; x < width; x++) {
  71.                     resizedPixels[x + xDrawOffset + (y + yDrawOffset) * maxWidth] = pixels[pixelCount++];
  72.                 }
  73.             }
  74.             pixels = resizedPixels;
  75.             width = maxWidth;
  76.             height = maxHeight;
  77.             xDrawOffset = 0;
  78.             yDrawOffset = 0;
  79.         }
  80.     }
  81.  
  82.     public void flipHorizontal() {
  83.         byte[] resizedPixels = new byte[width * height];
  84.         int pixelCount = 0;
  85.         for (int y = 0; y < height; y++) {
  86.             for (int x = width - 1; x >= 0; x--) {
  87.                 resizedPixels[pixelCount++] = pixels[x + y * width];
  88.             }
  89.         }
  90.         pixels = resizedPixels;
  91.         xDrawOffset = maxWidth - width - xDrawOffset;
  92.     }
  93.  
  94.     public void flipVertical() {
  95.         byte[] resizedPixels = new byte[width * height];
  96.         int pixelCount = 0;
  97.         for (int y = height - 1; y >= 0; y--) {
  98.             for (int x = 0; x < width; x++) {
  99.                 resizedPixels[pixelCount++] = pixels[x + y * width];
  100.             }
  101.         }
  102.         pixels = resizedPixels;
  103.         yDrawOffset = maxHeight - height - yDrawOffset;
  104.     }
  105.  
  106.     public void mixPalette(int red, int green, int blue) {
  107.         for (int index = 0; index < palette.length; index++) {
  108.             int r = palette[index] >> 16 & 0xff;
  109.             r += red;
  110.             if (r < 0) {
  111.                 r = 0;
  112.             } else if (r > 255) {
  113.                 r = 255;
  114.             }
  115.             int g = palette[index] >> 8 & 0xff;
  116.             g += green;
  117.             if (g < 0) {
  118.                 g = 0;
  119.             } else if (g > 255) {
  120.                 g = 255;
  121.             }
  122.             int b = palette[index] & 0xff;
  123.             b += blue;
  124.             if (b < 0) {
  125.                 b = 0;
  126.             } else if (b > 255) {
  127.                 b = 255;
  128.             }
  129.             palette[index] = (r << 16) + (g << 8) + b;
  130.         }
  131.     }
  132.  
  133.     public void drawImage(int x, int y) {
  134.         x += xDrawOffset;
  135.         y += yDrawOffset;
  136.         int offset = x + y * Rasterizer.width;
  137.         int originalOffset = 0;
  138.         int imageHeight = height;
  139.         int imageWidth = width;
  140.         int deviation = Rasterizer.width - imageWidth;
  141.         int originalDeviation = 0;
  142.         if (y < Rasterizer.topY) {
  143.             int yOffset = Rasterizer.topY - y;
  144.             imageHeight -= yOffset;
  145.             y = Rasterizer.topY;
  146.             originalOffset += yOffset * imageWidth;
  147.             offset += yOffset * Rasterizer.width;
  148.         }
  149.         if (y + imageHeight > Rasterizer.bottomY) {
  150.             imageHeight -= y + imageHeight - Rasterizer.bottomY;
  151.         }
  152.         if (x < Rasterizer.topX) {
  153.             int xOffset = Rasterizer.topX - x;
  154.             imageWidth -= xOffset;
  155.             x = Rasterizer.topX;
  156.             originalOffset += xOffset;
  157.             offset += xOffset;
  158.             originalDeviation += xOffset;
  159.             deviation += xOffset;
  160.         }
  161.         if (x + imageWidth > Rasterizer.bottomX) {
  162.             int xOffset = x + imageWidth - Rasterizer.bottomX;
  163.             imageWidth -= xOffset;
  164.             originalDeviation += xOffset;
  165.             deviation += xOffset;
  166.         }
  167.         if (imageWidth > 0 && imageHeight > 0) {
  168.             copyPixels(pixels, Rasterizer.pixels, imageWidth, imageHeight, offset, originalOffset, deviation,
  169.                     originalDeviation, palette);
  170.         }
  171.     }
  172.  
  173.     private void copyPixels(byte[] pixels, int[] rasterizerPixels, int width, int height, int offset,
  174.             int originalOffset, int deviation, int originalDeviation, int[] pallete) {
  175.         int shiftedWidth = -(width >> 2);
  176.         width = -(width & 0x3);
  177.         for (int heightCounter = -height; heightCounter < 0; heightCounter++) {
  178.             for (int shiftedWidthCounter = shiftedWidth; shiftedWidthCounter < 0; shiftedWidthCounter++) {
  179.                 int pixel = pixels[originalOffset++];
  180.                 if (pixel != 0) {
  181.                     rasterizerPixels[offset++] = pallete[pixel & 0xff];
  182.                 } else {
  183.                     offset++;
  184.                 }
  185.                 pixel = pixels[originalOffset++];
  186.                 if (pixel != 0) {
  187.                     rasterizerPixels[offset++] = pallete[pixel & 0xff];
  188.                 } else {
  189.                     offset++;
  190.                 }
  191.                 pixel = pixels[originalOffset++];
  192.                 if (pixel != 0) {
  193.                     rasterizerPixels[offset++] = pallete[pixel & 0xff];
  194.                 } else {
  195.                     offset++;
  196.                 }
  197.                 pixel = pixels[originalOffset++];
  198.                 if (pixel != 0) {
  199.                     rasterizerPixels[offset++] = pallete[pixel & 0xff];
  200.                 } else {
  201.                     offset++;
  202.                 }
  203.             }
  204.             for (int widthCounter = width; widthCounter < 0; widthCounter++) {
  205.                 int pixel = pixels[originalOffset++];
  206.                 if (pixel != 0) {
  207.                     rasterizerPixels[offset++] = pallete[pixel & 0xff];
  208.                 } else {
  209.                     offset++;
  210.                 }
  211.             }
  212.             offset += deviation;
  213.             originalOffset += originalDeviation;
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement