Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. import java.awt.image.ColorModel;
  2. import java.awt.image.IndexColorModel;
  3. import java.awt.image.MemoryImageSource;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import java.io.*;
  11.  
  12. public class BMPImageReader{
  13.  
  14. InputStream is;
  15. int curPos = 0;
  16.  
  17. int bitmapOffset; // starting position of image data
  18.  
  19. int width; // image width in pixels
  20. int height; // image height in pixels
  21. short bitsPerPixel; // 1, 4, 8, or 24 (no color map)
  22. int compression; // 0 (none), 1 (8-bit RLE), or 2 (4-bit RLE)
  23. int actualSizeOfBitmap;
  24. int scanLineSize;
  25. int actualColorsUsed;
  26.  
  27. byte r[], g[], b[]; // color palette
  28. int noOfEntries;
  29.  
  30. byte[] byteData; // Unpacked data
  31. int[] intData; // Unpacked data
  32. boolean topDown;
  33.  
  34. // Get the Interger Value from 4 bytes
  35. private int readInt() throws IOException {
  36. int b1 = is.read();
  37. int b2 = is.read();
  38. int b3 = is.read();
  39. int b4 = is.read();
  40. curPos += 4;
  41.  
  42. //
  43. System.out.println(" int : ");
  44.  
  45. System.out.println(" "+b1 +" " +b2 +" "+b3 +" "+b4);
  46.  
  47. return ((b4 << 24) + (b3 << 16) + (b2 << 8) + (b1 << 0));
  48. }
  49.  
  50. private short readShort() throws IOException {
  51. int b1 = is.read();
  52. int b2 = is.read();
  53. curPos += 2;
  54. System.out.println(" "+b1 +" " +b2);
  55. return (short) ((b2 << 8) + b1);
  56.  
  57. }
  58.  
  59. void getFileHeader() throws IOException, Exception {
  60. // Actual contents (14 bytes):
  61. short fileType = 0x4d42;// always "BM"
  62. int fileSize; // size of file in bytes
  63. short reserved1 = 0; // always 0
  64. short reserved2 = 0; // always 0
  65.  
  66. // stored in LE notation means 77 66(M B)
  67. System.out.println("\n Image Magic Code is :");
  68.  
  69.  
  70. fileType = readShort(); // getting BM here 2
  71.  
  72. if (fileType != 0x4d42)
  73. throw new Exception("Not a BMP file"); // wrong file type
  74.  
  75.  
  76. System.out.println("\n File size is :");
  77.  
  78. fileSize = readInt(); // 6
  79.  
  80. System.out.println(" "+fileSize);
  81. //
  82. reserved1 = readShort(); // 8
  83. reserved2 = readShort(); // 10
  84. bitmapOffset = readInt(); // 14
  85. }
  86. void getBitmapHeader() throws IOException {
  87.  
  88. // Actual contents (40 bytes):
  89. int size; // size of this header in bytes
  90. short planes; // no. of color planes: always 1
  91. int sizeOfBitmap; // size of bitmap in bytes (may be 0: if so,
  92. // calculate)
  93. int horzResolution; // horizontal resolution, pixels/meter (may be 0)
  94. int vertResolution; // vertical resolution, pixels/meter (may be 0)
  95. int colorsUsed; // no. of colors in palette (if 0, calculate)
  96. int colorsImportant; // no. of important colors (appear first in
  97. // palette) (0 means all are important)
  98. int noOfPixels;
  99.  
  100. size = readInt();
  101.  
  102. width = readInt();
  103. height = readInt();
  104. planes = readShort();
  105. bitsPerPixel = readShort(); // this field indicates the bits per pixel of image
  106. compression = readInt();
  107. sizeOfBitmap = readInt();
  108. System.out.println("Size of Bitmap "+sizeOfBitmap);
  109.  
  110. horzResolution = readInt();
  111. vertResolution = readInt();
  112. colorsUsed = readInt();
  113. colorsImportant = readInt();
  114.  
  115. topDown = (height < 0);
  116. if (topDown)
  117. height = -height;
  118.  
  119. noOfPixels = width * height;
  120.  
  121. // Scan line is padded with zeroes to be a multiple of four bytes
  122.  
  123. scanLineSize = ((width * bitsPerPixel + 31) / 32) * 4; /// calculate the scanline size in byte == ( width * bitperpixel +31)/8
  124.  
  125. actualSizeOfBitmap = scanLineSize * height;
  126.  
  127. //System.out.println("Size of Scannlin "+scanLineSize);
  128. //System.out.println("act Size of Bitmap "+actualSizeOfBitmap);
  129.  
  130. if (colorsUsed != 0)
  131. actualColorsUsed = colorsUsed;
  132. else
  133. // a value of 0 means we determine this based on the bits per pixel
  134. if (bitsPerPixel < 16)
  135. actualColorsUsed = 1 << bitsPerPixel; // for only monocrome and grayscale images
  136. else
  137. actualColorsUsed = 0; // no palette
  138. }
  139.  
  140. void getPalette() throws IOException {
  141. noOfEntries = actualColorsUsed;
  142. System.out.println("No of Color pallet "+noOfEntries);
  143.  
  144. if (noOfEntries > 0) {
  145. r = new byte[noOfEntries];
  146. g = new byte[noOfEntries];
  147. b = new byte[noOfEntries];
  148.  
  149. int reserved;
  150. for (int i = 0; i < noOfEntries; i++) {
  151. b[i] = (byte) is.read();
  152. System.out.println("Blue "+b[i]);
  153. g[i] = (byte) is.read();
  154. System.out.println("green "+g[i]);
  155. r[i] = (byte) is.read();
  156. System.out.println("red "+r[i]);
  157. reserved = is.read();
  158. curPos += 4;
  159. }
  160.  
  161. }
  162. }
  163.  
  164. void unpack(byte[] rawData, int rawOffset, int bpp, byte[] byteData,
  165. int byteOffset, int w) throws Exception {
  166. int j = byteOffset;
  167. int k = rawOffset;
  168.  
  169. byte mask;
  170. int pixPerByte;
  171.  
  172. switch (bpp) {
  173. case 1:
  174. mask = (byte) 0x01; // when the image is monocrome , we have to get 1 bit from byte
  175. pixPerByte = 8;
  176. break;
  177.  
  178. case 8: // when the image is monocrome , we have to get 1111 1111 one bit from byte
  179. mask = (byte) 0xff;
  180. pixPerByte = 1;
  181. break;
  182. default:
  183. throw new Exception("Unsupported bits-per-pixel value: " + bpp);
  184. }
  185.  
  186. for (int i = 0;;) // Pack the the bytes of rawdata into bits of byte
  187. {
  188. int shift = 8 - bpp; // calculate the no_of _time shift
  189.  
  190. for (int ii = 0; ii < pixPerByte; ii++)
  191. {
  192. byte br = rawData[k]; // get Bytes
  193. br >>= shift;
  194.  
  195. byteData[j] = (byte) (br & mask); // extract no of bits fro bits of images
  196. System.out.println("Setting byteData[" + j + "]=" +byteData[j] );
  197. // Test.byteToHex(byteData[j]));
  198. j++;
  199.  
  200.  
  201. i++;
  202. if (i == w)
  203. return;
  204. shift -= bpp;
  205. }
  206. k++;
  207. }
  208. }
  209.  
  210. void unpack24(byte[] rawData, int rawOffset, int[] intData, int intOffset,int w)
  211. {
  212. int j = intOffset;
  213. int k = rawOffset; //rawdata contain actual data , we have to convert in 24 bit pack format
  214. int mask = 0xff;
  215. for (int i = 0; i < w; i++) {
  216. System.out.println( "===================");
  217. System.out.print( " b: "+rawData[k]);
  218. int b0 = (((int) (rawData[k++])) & mask);
  219. System.out.print( " g: "+rawData[k]); // blue
  220. int b1 = (((int) (rawData[k++])) & mask) << 8; // Green
  221. System.out.print( " r: "+rawData[k]);
  222. int b2 = (((int) (rawData[k++])) & mask) << 16; // Red
  223. intData[j] = 0xff000000 | b0 | b1 | b2; // oxff00.. for intData is 32 bit variable , but we are use only 24 bit so other are ff
  224. //System.out.print( " b: "+intData[j]);
  225. //System.out.println( ">>>>>>>>>>>>>>>>>>>>>>>");
  226. j++;
  227. }
  228. }
  229.  
  230.  
  231. void getPixelData() throws IOException, Exception {
  232. byte[] rawData; // the raw unpacked data
  233.  
  234. // Skip to the start of the bitmap data (if we are not already there)
  235. long skip = bitmapOffset - curPos;
  236. if (skip > 0) {
  237. is.skip(skip);
  238. curPos += skip;
  239. }
  240.  
  241. int len = scanLineSize;
  242. if (bitsPerPixel > 8)
  243. intData = new int[width * height]; // unpack
  244. else
  245. byteData = new byte[width * height]; // pack
  246.  
  247. rawData = new byte[actualSizeOfBitmap];
  248.  
  249. int rawOffset = 0;
  250. int flg=0;
  251. int offset = (height - 1) * width; // point to FIRST PIXEL
  252. for (int i = height - 1; i >= 0; i--) {
  253.  
  254. int n = is.read(rawData, rawOffset, len);
  255.  
  256. if (n < len)
  257. throw new Exception("Scan line ended prematurely after " + n
  258. + " bytes");
  259. if (bitsPerPixel == 24)
  260. {
  261.  
  262. unpack24(rawData, rawOffset, intData, offset, width);
  263.  
  264. }
  265.  
  266. else
  267. // 8-bits or less
  268. unpack(rawData, rawOffset, bitsPerPixel, byteData, offset,
  269. width);
  270. rawOffset += len; // rowoffset increase becase, its point ro raw data of inputstream
  271. offset -= width;//
  272. }
  273. }
  274.  
  275. public void read(InputStream is) throws IOException, Exception {
  276. this.is = is;
  277. System.out.println("===============Hi This is Header of Bmp Image ==========");
  278.  
  279. getFileHeader();
  280. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  281. String s=br.readLine();
  282. System.out.println("=============================================================");
  283.  
  284. getBitmapHeader();
  285. if (compression != 0)
  286. throw new Exception("Compression not supported");
  287. getPalette();
  288. getPixelData();
  289. }
  290.  
  291. public MemoryImageSource makeImageSource() {
  292. ColorModel cm;
  293. MemoryImageSource mis;
  294.  
  295. if (noOfEntries > 0) {
  296. // There is a color palette; create an IndexColorModel
  297. cm = new IndexColorModel(bitsPerPixel, noOfEntries, r, g, b);
  298. } else {
  299. // There is no palette; use the default RGB color model
  300. cm = ColorModel.getRGBdefault();
  301. }
  302.  
  303. // Create MemoryImageSource
  304.  
  305. if (bitsPerPixel > 8) {
  306. // use one int per pixel
  307. mis = new MemoryImageSource(width, height, cm, intData, 0, width);
  308.  
  309. } else {
  310. // use one byte per pixel
  311. mis = new MemoryImageSource(width, height, cm, byteData, 0, width);
  312. }
  313.  
  314. return mis; // this can be used by Component.createImage()
  315. }
  316.  
  317. public static void main(String[] aqgs) {
  318. BMPImageReader bd = new BMPImageReader();
  319. try {
  320. bd.read(BMPImageReader.class.getResourceAsStream("atm.bmp"));
  321. } catch (Exception e) {
  322. // TODO Auto-generated catch block
  323. e.printStackTrace();
  324. }
  325. JFrame jf = new JFrame();
  326. JLabel jl = new JLabel();
  327. ImageIcon ii = new ImageIcon( jl.createImage(bd.makeImageSource()));
  328. jl.setIcon(ii);
  329. jf.add(jl);
  330. jf.pack();
  331. jf.setVisible(true);
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement