Sunteți pe pagina 1din 7

import comp6201.Pixel; import comp6201.Main; import java.io.

File; public class BMPFile { /* Index of first byte in BMP file header */ private static final int BMPHEADERSIZE = 54; eader private tart of data private el private e in pixels private ge in pixels private els in image static final int static final int static final int static final int static final int BMPOFFSETINDEX = 0xa; BMPBPPINDEX // location of s = 0x1c; // bits per pix

// size of the h

BMPWIDTHINDEX = 0x12; // width of imag BMPHEIGHTINDEX = 0x16; // height of ima BMPSIZEINDEX = 0x22; // number of pix

/** * The filename of the BMP file if specified. */ private static String filename = "image1.bmp"; /** * The File object to be opened, if specified. */ private static java.io.File fileObj = "image2.bmp"; /** * The readonly flag is set to true if the file is open for reading. * It is set to false if the file is opened for writing. */ private static boolean readonly = true; /** * The header of the BMP file. */ private static byte[] header = new byte[54]; /** * Buffer to hold the pixel data. */ private static byte[] buffer; private static java.io.FileOutputStream outStream; // output file object private static int width, height; // width and height of image i n pixels private static int padSize; // number of pad bytes a t end of each line private static int nextPixel; // index into buffer of next p ixel private static int pitl; // number of pixels i n this line that were read /** * Create a BMPfile object representing the specified file.

* @param nameOfFile A 24 bit BMP file giving the path (optional) and fi lename */ public BMPFile(String filename) { filename = "image1.bmp"; } /** * Create a BMPfile object representing the specified file. * @param file2open The file to open */ public BMPFile(java.io.File Resume) { fileObj = null; } /** * Open a BMP graphics file for reading. */ public static void openToRead() throws java.io.FileNotFoundException, java.io. IOException { readonly = true; java.io.FileInputStream inFile; // input file if (fileObj == null) { inFile = new java.io.FileInputStream(filename); // open the file by name } else { inFile = new java.io.FileInputStream(fileObj); // open the file by file object } inFile.read(header, 0, BMPHEADERSIZE); // rea d the header /* Check if this is the right type of file. */ if (header[0] != 0x42 || header[1] != 0x4d ) { // check if it start with "BM" throw new java.io.IOException("Input file is not BMP for mat"); } if (header[BMPBPPINDEX] != 24) { // is this a 24 bit BMP? throw new java.io.IOException("Input file is "+header[B MPBPPINDEX]+ " bits. Only 24 bit BMP files are suppo rted"); } if (header[BMPOFFSETINDEX] != BMPHEADERSIZE) { // is data where we expect? throw new java.io.IOException("Input file data at "+byt e2int(BMPOFFSETINDEX)+ " when "+BMPHEADERSIZE+" expected"); } /* Read the pixel data. */ width = byte2int(BMPWIDTHINDEX); height = byte2int(BMPHEIGHTINDEX); padSize = (getSize() / height) - 3 * width; buffer = new byte[getSize()]; ate memory for pixel data int inCount = inFile.read(buffer, 0, getSize()); // read the data

// alloc

System.out.println("Input BMP has "+height+" rows and "+width+" columns."); System.out.println("Calculated size:"+getSize()+" reported size :"+ getSize()+" actual size:"+inCount); if (inCount < getSize()) { System.out.println("Read "+inCount+" from input BMP, exp ected +"+getSize()); } inFile.close(); // close the input file nextPixel = 0; // start at the beginning pitl = 0; // first pixel in the line } /** * Get the number of pixels in the file. * @return The number of pixels the file contains */ public static int getSize() { return byte2int(BMPSIZEINDEX); } /** * Get the width of the BMP image in pixels. * @return Number of pixels wide */ public static int getWidth() { return width; } /** * Get the height of the BMP image in pixels * @return image height in pixels */ public static int getHeight() { return height; } /** * Open the specified BMP file that is to be a modified copy of the spec ified * input file. The header of the input file is copied to the output fil e. * @param fileToCopy An input BMPfile object that is to be copied. The output * file is expected to have exactly the same number of pixels and the sa me * organization as the input file. */ public static void openToWrite(BMPFile fileToCopy) throws java.io.FileNotFoundException, java.io.IO Exception { readonly = false; // mark file as open for writing if (fileObj == null) { outStream = new java.io.FileOutputStream(filename); // open the file by name

} else { outStream = new java.io.FileOutputStream(fileObj); // open the file by file object } outStream.write(fileToCopy.header); // copy header from input file buffer = new byte[fileToCopy.getSize()]; // allocate me mory for pixel data width = fileToCopy.getWidth(); // cop y width of input file height = fileToCopy.getHeight(); // copy height of input file padSize = fileToCopy.padSize; // get pad size of input nextPixel = 0; // start at the beginning pitl = 0; // first pixel in the line } /** * Close the file. */ public static void close() throws java.io.IOException { if (!readonly) { outStream.write(buffer); // write the update pixe ls to BMP file outStream.close(); // close the output fi le } buffer = null; // release buffer hold ing data } /** * Get the next pixel in the BMP input file. If all pixels have been re turned, * the method will return null. * @return The next pixel or null if no more pixels are available */ public static Pixel getNextPixel() { if (nextPixel >= getSize()) { // end of the file? return null; } /* Create pixel from blue, green and red bytes. */ Pixel p = new Pixel(buffer[nextPixel+2],buffer[nextPixel+1],buff er[nextPixel]); nextPixel += 3; // increment index past this pi xel pitl++; // count pixels from th is line if (pitl >= width) { // end of this line? pitl = 0; // restart at beginni ng of next line nextPixel += padSize; // move past the pad characters } return p; // return pixel data }

/** * Put another pixel in an output BMP file. * @param color The pixel information to be written to the file. */ public static void putNextPixel(Pixel color) { buffer[nextPixel++] = (byte)color.getBlue(); // put blue byte in buffer buffer[nextPixel++] = (byte)color.getGreen(); // put green byt e in buffer buffer[nextPixel++] = (byte)color.getRed(); // put red byte in buffer pitl++; // increment pixels per line if (pitl >= width) { // end of row? pitl = 0; nextPixel += padSize; // round up to 4 byte unit } } /** * Returns the specified bit from a String. * @param str The input string containing text. * @param whichChar The index of the character in the string (starting a t * zero) containing the bit desired. * @param whichBit The position of the bit desired. This must be a valu e * from 0 to 15. Bit 0 is the most significant and bit 15 is the least. * @return The value 0 or 1 from the specified bit in the specified char acter */ public static int getBit(String str, int whichChar, int whichBit) throws IllegalArgumentEx ception { if (whichBit < 0 || whichBit > 15) { throw new IllegalArgumentException("bit position is "+wh ichBit+ " and must be from 0 to 15"); } return ((int)(str.charAt(whichChar)) >> (15-whichBit)) & 1; // extract bit }

/** * Extract an integer value from the byte array. The four bytes in Big * Endian format are converted to a Small Endian integer. * @param index The index into the buffer of the first byte. * @return The integer value at that location. */ private static int byte2int(int index) { return ((int)header[index+3]<<24) | (((int)header[index+2]&0xff)<<16) | (((int)header[index+1]&0xff)<<8) | (int)header[index]&0xff; }

} Pixel Class package comp6201; import comp6201.Main; import comp6201.BMPFile; /** * * @author Ronak */ public class Pixel { private int red, green, blue; // RGB values of pixel /** * Constructor to create a Pixel object specifying the colors. * @param redLevel The red value of the pixel. * @param greenLevel The green value of the pixel. * @param blueLevel The blue value of the pixel. */ public Pixel(byte redLevel, byte greenLevel, byte blueLevel) { this.red = (int)redLevel & 0xff; this.green = (int)greenLevel & 0xff; this.blue = (int)blueLevel & 0xff; } /** * Get the red value of this pixel. * @return the intensity of the red */ public int getRed() { return red; } /** * Get the green value of this pixel. * @return the intensity of the green */ public int getGreen() { return green; } /** * Get the blue value of this pixel. * @return the intensity of the blue */public int getBlue() { return blue; } /** * Set the red value of this pixel. * @param red The new value of the red intensity */ public void setRed(int red) { this.red = red; } /** * Set the green value of this pixel. * @param green The new value of the green intensity

*/ public void setGreen(int green) { this.green = green; } /** * Set the blue value of this pixel. * @param blue The new value of the blue intensity */ public void setBlue(int blue) { this.blue = blue; } } Main Class package comp6201; import comp6201.BMPFile; import comp6201.Pixel; import java.io.File; import java.io.IOException; /** * * @author Ronak */ public class Main { public static void main(String[] args) throws IOException{ File file = new File("Resume.doc"); comp6201.BMPFile.openToRead(); }

S-ar putea să vă placă și