1 | /* |
2 | * $Id: BITMAPFILEHEADER.java,v 1.1.1.5 2004/05/25 20:23:20 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.bindings.platform_sdk.windows_api; |
14 | |
15 | import java.awt.image.BufferedImage; |
16 | import java.io.IOException; |
17 | import java.io.OutputStream; |
18 | import java.nio.ByteBuffer; |
19 | import java.nio.ByteOrder; |
20 | |
21 | import com.moesol.bindings.NativeStructure; |
22 | |
23 | /** |
24 | * @author Robert Hastings |
25 | */ |
26 | public class BITMAPFILEHEADER extends NativeStructure { |
27 | // BEGIN_KEEP |
28 | public static final short BM = (short)('B' | ('M' << 8)); |
29 | /** |
30 | * Write a 32 bit bmp file. This method does not close the |
31 | * output stream <code>os</code> so that it is possible to |
32 | * write multiple images into the stream. |
33 | * |
34 | * @param buffered_image |
35 | * @param os |
36 | */ |
37 | public static void writeBmp(BufferedImage buffered_image, OutputStream os) |
38 | throws IOException |
39 | { |
40 | int width = buffered_image.getWidth(); |
41 | int height = buffered_image.getHeight(); |
42 | int pixels[] = buffered_image.getRGB(0, 0, width, height, null, 0, width); |
43 | |
44 | BITMAPFILEHEADER bmp = new BITMAPFILEHEADER(); |
45 | bmp.set_bfType(BITMAPFILEHEADER.BM); |
46 | bmp.set_bfOffBits(BITMAPFILEHEADER.sizeof() + BITMAPINFOHEADER.sizeof()); |
47 | bmp.set_bfSize(BITMAPFILEHEADER.sizeof() + BITMAPINFOHEADER.sizeof() + 4*pixels.length); |
48 | |
49 | BITMAPINFOHEADER bih = new BITMAPINFOHEADER(); |
50 | bih.set_biSize(BITMAPINFOHEADER.sizeof()); |
51 | bih.set_biWidth(width); |
52 | bih.set_biHeight(height); |
53 | bih.set_biPlanes((short)1); |
54 | bih.set_biBitCount((short)32); |
55 | bih.set_biCompression(BITMAPINFOHEADER.BI_RGB); |
56 | bih.set_biSizeImage(0); |
57 | bih.set_biClrUsed(0); |
58 | bih.set_biClrImportant(0); |
59 | |
60 | os.write(bmp._getStructureBytes()); |
61 | os.write(bih._getStructureBytes()); |
62 | |
63 | ByteBuffer bb = ByteBuffer.allocate(4); |
64 | bb.order(ByteOrder.LITTLE_ENDIAN); |
65 | |
66 | for (int row=height-1; row >= 0; row--) { |
67 | for (int col=0; col < width; col++) { |
68 | bb.putInt(0, pixels[row*width+col]); |
69 | os.write(bb.array()); |
70 | } |
71 | } |
72 | } |
73 | // END_KEEP |
74 | |
75 | public BITMAPFILEHEADER() { |
76 | super( new byte[sizeof()] ); |
77 | } |
78 | public static int sizeof() { |
79 | return 14; |
80 | } |
81 | public void set_bfType(short v) { |
82 | putShort(0, v); |
83 | } |
84 | public short get_bfType() { |
85 | return getShort(0); |
86 | } |
87 | public void set_bfSize(int v) { |
88 | putInt(2, v); |
89 | } |
90 | public int get_bfSize() { |
91 | return getInt(2); |
92 | } |
93 | public void set_bfReserved1(short v) { |
94 | putShort(6, v); |
95 | } |
96 | public short get_bfReserved1() { |
97 | return getShort(6); |
98 | } |
99 | public void set_bfReserved2(short v) { |
100 | putShort(8, v); |
101 | } |
102 | public short get_bfReserved2() { |
103 | return getShort(8); |
104 | } |
105 | public void set_bfOffBits(int v) { |
106 | putInt(10, v); |
107 | } |
108 | public int get_bfOffBits() { |
109 | return getInt(10); |
110 | } |
111 | } |