Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.ColorModel;
- import java.awt.image.IndexColorModel;
- import java.awt.image.MemoryImageSource;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import java.io.*;
- public class BMPImageReader{
- InputStream is;
- int curPos = 0;
- int bitmapOffset; // starting position of image data
- int width; // image width in pixels
- int height; // image height in pixels
- short bitsPerPixel; // 1, 4, 8, or 24 (no color map)
- int compression; // 0 (none), 1 (8-bit RLE), or 2 (4-bit RLE)
- int actualSizeOfBitmap;
- int scanLineSize;
- int actualColorsUsed;
- byte r[], g[], b[]; // color palette
- int noOfEntries;
- byte[] byteData; // Unpacked data
- int[] intData; // Unpacked data
- boolean topDown;
- // Get the Interger Value from 4 bytes
- private int readInt() throws IOException {
- int b1 = is.read();
- int b2 = is.read();
- int b3 = is.read();
- int b4 = is.read();
- curPos += 4;
- //
- System.out.println(" int : ");
- System.out.println(" "+b1 +" " +b2 +" "+b3 +" "+b4);
- return ((b4 << 24) + (b3 << 16) + (b2 << 8) + (b1 << 0));
- }
- private short readShort() throws IOException {
- int b1 = is.read();
- int b2 = is.read();
- curPos += 2;
- System.out.println(" "+b1 +" " +b2);
- return (short) ((b2 << 8) + b1);
- }
- void getFileHeader() throws IOException, Exception {
- // Actual contents (14 bytes):
- short fileType = 0x4d42;// always "BM"
- int fileSize; // size of file in bytes
- short reserved1 = 0; // always 0
- short reserved2 = 0; // always 0
- // stored in LE notation means 77 66(M B)
- System.out.println("\n Image Magic Code is :");
- fileType = readShort(); // getting BM here 2
- if (fileType != 0x4d42)
- throw new Exception("Not a BMP file"); // wrong file type
- System.out.println("\n File size is :");
- fileSize = readInt(); // 6
- System.out.println(" "+fileSize);
- //
- reserved1 = readShort(); // 8
- reserved2 = readShort(); // 10
- bitmapOffset = readInt(); // 14
- }
- void getBitmapHeader() throws IOException {
- // Actual contents (40 bytes):
- int size; // size of this header in bytes
- short planes; // no. of color planes: always 1
- int sizeOfBitmap; // size of bitmap in bytes (may be 0: if so,
- // calculate)
- int horzResolution; // horizontal resolution, pixels/meter (may be 0)
- int vertResolution; // vertical resolution, pixels/meter (may be 0)
- int colorsUsed; // no. of colors in palette (if 0, calculate)
- int colorsImportant; // no. of important colors (appear first in
- // palette) (0 means all are important)
- int noOfPixels;
- size = readInt();
- width = readInt();
- height = readInt();
- planes = readShort();
- bitsPerPixel = readShort(); // this field indicates the bits per pixel of image
- compression = readInt();
- sizeOfBitmap = readInt();
- System.out.println("Size of Bitmap "+sizeOfBitmap);
- horzResolution = readInt();
- vertResolution = readInt();
- colorsUsed = readInt();
- colorsImportant = readInt();
- topDown = (height < 0);
- if (topDown)
- height = -height;
- noOfPixels = width * height;
- // Scan line is padded with zeroes to be a multiple of four bytes
- scanLineSize = ((width * bitsPerPixel + 31) / 32) * 4; /// calculate the scanline size in byte == ( width * bitperpixel +31)/8
- actualSizeOfBitmap = scanLineSize * height;
- //System.out.println("Size of Scannlin "+scanLineSize);
- //System.out.println("act Size of Bitmap "+actualSizeOfBitmap);
- if (colorsUsed != 0)
- actualColorsUsed = colorsUsed;
- else
- // a value of 0 means we determine this based on the bits per pixel
- if (bitsPerPixel < 16)
- actualColorsUsed = 1 << bitsPerPixel; // for only monocrome and grayscale images
- else
- actualColorsUsed = 0; // no palette
- }
- void getPalette() throws IOException {
- noOfEntries = actualColorsUsed;
- System.out.println("No of Color pallet "+noOfEntries);
- if (noOfEntries > 0) {
- r = new byte[noOfEntries];
- g = new byte[noOfEntries];
- b = new byte[noOfEntries];
- int reserved;
- for (int i = 0; i < noOfEntries; i++) {
- b[i] = (byte) is.read();
- System.out.println("Blue "+b[i]);
- g[i] = (byte) is.read();
- System.out.println("green "+g[i]);
- r[i] = (byte) is.read();
- System.out.println("red "+r[i]);
- reserved = is.read();
- curPos += 4;
- }
- }
- }
- void unpack(byte[] rawData, int rawOffset, int bpp, byte[] byteData,
- int byteOffset, int w) throws Exception {
- int j = byteOffset;
- int k = rawOffset;
- byte mask;
- int pixPerByte;
- switch (bpp) {
- case 1:
- mask = (byte) 0x01; // when the image is monocrome , we have to get 1 bit from byte
- pixPerByte = 8;
- break;
- case 8: // when the image is monocrome , we have to get 1111 1111 one bit from byte
- mask = (byte) 0xff;
- pixPerByte = 1;
- break;
- default:
- throw new Exception("Unsupported bits-per-pixel value: " + bpp);
- }
- for (int i = 0;;) // Pack the the bytes of rawdata into bits of byte
- {
- int shift = 8 - bpp; // calculate the no_of _time shift
- for (int ii = 0; ii < pixPerByte; ii++)
- {
- byte br = rawData[k]; // get Bytes
- br >>= shift;
- byteData[j] = (byte) (br & mask); // extract no of bits fro bits of images
- System.out.println("Setting byteData[" + j + "]=" +byteData[j] );
- // Test.byteToHex(byteData[j]));
- j++;
- i++;
- if (i == w)
- return;
- shift -= bpp;
- }
- k++;
- }
- }
- void unpack24(byte[] rawData, int rawOffset, int[] intData, int intOffset,int w)
- {
- int j = intOffset;
- int k = rawOffset; //rawdata contain actual data , we have to convert in 24 bit pack format
- int mask = 0xff;
- for (int i = 0; i < w; i++) {
- System.out.println( "===================");
- System.out.print( " b: "+rawData[k]);
- int b0 = (((int) (rawData[k++])) & mask);
- System.out.print( " g: "+rawData[k]); // blue
- int b1 = (((int) (rawData[k++])) & mask) << 8; // Green
- System.out.print( " r: "+rawData[k]);
- int b2 = (((int) (rawData[k++])) & mask) << 16; // Red
- intData[j] = 0xff000000 | b0 | b1 | b2; // oxff00.. for intData is 32 bit variable , but we are use only 24 bit so other are ff
- //System.out.print( " b: "+intData[j]);
- //System.out.println( ">>>>>>>>>>>>>>>>>>>>>>>");
- j++;
- }
- }
- void getPixelData() throws IOException, Exception {
- byte[] rawData; // the raw unpacked data
- // Skip to the start of the bitmap data (if we are not already there)
- long skip = bitmapOffset - curPos;
- if (skip > 0) {
- is.skip(skip);
- curPos += skip;
- }
- int len = scanLineSize;
- if (bitsPerPixel > 8)
- intData = new int[width * height]; // unpack
- else
- byteData = new byte[width * height]; // pack
- rawData = new byte[actualSizeOfBitmap];
- int rawOffset = 0;
- int flg=0;
- int offset = (height - 1) * width; // point to FIRST PIXEL
- for (int i = height - 1; i >= 0; i--) {
- int n = is.read(rawData, rawOffset, len);
- if (n < len)
- throw new Exception("Scan line ended prematurely after " + n
- + " bytes");
- if (bitsPerPixel == 24)
- {
- unpack24(rawData, rawOffset, intData, offset, width);
- }
- else
- // 8-bits or less
- unpack(rawData, rawOffset, bitsPerPixel, byteData, offset,
- width);
- rawOffset += len; // rowoffset increase becase, its point ro raw data of inputstream
- offset -= width;//
- }
- }
- public void read(InputStream is) throws IOException, Exception {
- this.is = is;
- System.out.println("===============Hi This is Header of Bmp Image ==========");
- getFileHeader();
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
- String s=br.readLine();
- System.out.println("=============================================================");
- getBitmapHeader();
- if (compression != 0)
- throw new Exception("Compression not supported");
- getPalette();
- getPixelData();
- }
- public MemoryImageSource makeImageSource() {
- ColorModel cm;
- MemoryImageSource mis;
- if (noOfEntries > 0) {
- // There is a color palette; create an IndexColorModel
- cm = new IndexColorModel(bitsPerPixel, noOfEntries, r, g, b);
- } else {
- // There is no palette; use the default RGB color model
- cm = ColorModel.getRGBdefault();
- }
- // Create MemoryImageSource
- if (bitsPerPixel > 8) {
- // use one int per pixel
- mis = new MemoryImageSource(width, height, cm, intData, 0, width);
- } else {
- // use one byte per pixel
- mis = new MemoryImageSource(width, height, cm, byteData, 0, width);
- }
- return mis; // this can be used by Component.createImage()
- }
- public static void main(String[] aqgs) {
- BMPImageReader bd = new BMPImageReader();
- try {
- bd.read(BMPImageReader.class.getResourceAsStream("atm.bmp"));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- JFrame jf = new JFrame();
- JLabel jl = new JLabel();
- ImageIcon ii = new ImageIcon( jl.createImage(bd.makeImageSource()));
- jl.setIcon(ii);
- jf.add(jl);
- jf.pack();
- jf.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement