package Classes; /** * @author Michael Wilhelm * @since 1.0 * @version 1.0 */ public class Bitmap { private final short VERSION = 2; String filename = ""; byte[] array = null; public String fileID=""; public short version=0; public int width=0; public int height=0; public short numberOfColors=0; public int[] colors=null; public void readBitmap(String filename, byte[] array) { this.filename = filename; this.array = array; if (!readHeader()) { System.err.println("Fehlerhafter Header"); } else { System.out.println("Header okay"); } if (!readMeasurements()) { System.err.println("Fehlerhafter Breiten/Höhen"); } else { System.out.println("Breiten/Höhen okay"); } if (!readColors()) { System.err.println("Fehlerhafte Farben"); } else { System.out.println("Farben okay"); } System.out.println(this); printColors(); } // readBitmap private boolean readColors() { numberOfColors = getShort(12); if (numberOfColors<=0) { return false; } colors = new int[numberOfColors]; for (int i=0; i0 && height>0; } /** * description read the header: fileID and version * @return true if the header is okay */ private boolean readHeader() { // FileID MW 0x4D 0x57 int id1 = array[0]; int id2 = array[1]; if (id1==0x4D && id2==0x57) { char ch1 = (char)id1; char ch2 = (char)id2; fileID = Character.toString(ch1)+Character.toString(ch2); } else { return false; } version = getShort(2); if (version != VERSION) { return false; } return true; } // readHeader private short getShort(int offset) { short sh = (short) (array[offset]<<8 | array[offset+1]); return sh; } private int getInt4Bytes(int offset) { int intValue = (int) (array[offset]<<24 | array[offset+1]<<16 | array[offset+2]<<8 | array[offset+3]); return intValue; } private int getInt3Bytes(int offset) { int i1 = array[offset]; if (i1<0) i1+=256; int i2 = array[offset+1]; if (i2<0) i2+=256; int i3 = array[offset+2]; if (i3<0) i3+=256; int intValue = (int) (i1<<16 | i2<<8 | i3); return intValue; } @Override public String toString() { return "Bitmap{" + "filename='" + filename + '\'' + ", fileID='" + fileID + '\'' + ", version=" + version + ", width=" + width + ", height=" + height + '}'; } public void printColors() { System.out.println("Anzahl der Farben: "+numberOfColors); for (int color:colors){ System.out.print( color>>16 & 0xFF ); System.out.print( " "); System.out.print( color>>8 & 0xFF ); System.out.print( " "); System.out.println( color & 0xFF ); } } }