1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package ij.plugin;
import ij.*;
import ij.io.*;
import ij.process.*;
import java.awt.*;
import java.io.*;
import java.awt.image.*;
/** Implements the File/Save As/BMP command. Based on BMPFile class from
http://www.javaworld.com/javaworld/javatips/jw-javatip60-p2.html */
public class BMP_Writer implements PlugIn {
//--- Private constants
private final static int BITMAPFILEHEADER_SIZE = 14;
private final static int BITMAPINFOHEADER_SIZE = 40;
//--- Private variable declaration
//--- Bitmap file header
private byte bitmapFileHeader [] = new byte [14];
private byte bfType [] = {(byte)'B', (byte)'M'};
private int bfSize = 0;
private int bfReserved1 = 0;
private int bfReserved2 = 0;
private int bfOffBits = BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE;
//--- Bitmap info header
private byte bitmapInfoHeader [] = new byte [40];
private int biSize = BITMAPINFOHEADER_SIZE;
private int biWidth = 0;
private int padWidth = 0;
private int biHeight = 0;
private int biPlanes = 1;
private int biBitCount = 24;
private int biCompression = 0;
private int biSizeImage = 0;
private int biXPelsPerMeter = 0x0;
private int biYPelsPerMeter = 0x0;
private int biClrUsed = 0;
private int biClrImportant = 0;
//--- Bitmap raw data
private int intBitmap [];
private byte byteBitmap [];
//--- File section
private FileOutputStream fo;
private BufferedOutputStream bfo;
ImagePlus imp;
public void run(String path) {
IJ.showProgress(0);
imp = WindowManager.getCurrentImage();
if (imp==null)
{IJ.noImage(); return;}
try {
writeImage(imp, path);
} catch (Exception e) {
String msg = e.getMessage();
if (msg==null || msg.equals(""))
msg = ""+e;
IJ.error("BMP Writer", "An error occured writing the file.\n \n" + msg);
}
IJ.showProgress(1);
IJ.showStatus("");
}
void writeImage(ImagePlus imp, String path) throws Exception {
if(imp.getBitDepth()==24)
biBitCount = 24;
else {
biBitCount = 8;
LookUpTable lut = imp.createLut();
biClrUsed=lut.getMapSize(); // 8 bit color image may use less
bfOffBits+=biClrUsed*4;
}
if (path==null || path.equals("")) {
String prompt = "Save as " + biBitCount + " bit BMP";
SaveDialog sd = new SaveDialog(prompt, imp.getTitle(), ".bmp");
if(sd.getFileName()==null)
return;
path = sd.getDirectory()+sd.getFileName();
}
imp.startTiming();
saveBitmap (path, imp.getImage(), imp.getWidth(), imp.getHeight() );
}
public void saveBitmap (String parFilename, Image parImage, int parWidth, int parHeight) throws Exception {
fo = new FileOutputStream (parFilename);
bfo = new BufferedOutputStream(fo);
save (parImage, parWidth, parHeight);
bfo.close();
fo.close ();
}
/*
* The saveMethod is the main method of the process. This method
* will call the convertImage method to convert the memory image to
* a byte array; method writeBitmapFileHeader creates and writes
* the bitmap file header; writeBitmapInfoHeader creates the
* information header; and writeBitmap writes the image.
*
*/
private void save (Image parImage, int parWidth, int parHeight) throws Exception {
convertImage (parImage, parWidth, parHeight);
writeBitmapFileHeader ();
writeBitmapInfoHeader ();
if(biBitCount == 8)
writeBitmapPalette ();
writeBitmap ();
}
private void writeBitmapPalette() throws Exception {
LookUpTable lut = imp.createLut();
byte[] g = lut.getGreens();
byte[] r = lut.getReds();
byte[] b = lut.getBlues();
for(int i = 0;i<lut.getMapSize();i++) {
bfo.write(b[i]);
bfo.write(g[i]);
bfo.write(r[i]);
bfo.write(0x00);
}
}
/*
* convertImage converts the memory image to the bitmap format (BRG).
* It also computes some information for the bitmap info header.
*
*/
private boolean convertImage (Image parImage, int parWidth, int parHeight) {
int pad;
if(biBitCount == 24)
intBitmap = (int[]) imp.getProcessor().getPixels();
else
byteBitmap = (byte[]) imp.getProcessor().convertToByte(true).getPixels();
biWidth = parWidth;
biHeight = parHeight;
if(biBitCount==24)
pad = 4 - ((biWidth * 3) % 4);
else
pad = 4 - ((biWidth) % 4);
if (pad == 4) // <==== Bug correction
pad = 0; // <==== Bug correction
padWidth = biWidth*(biBitCount==24?3:1)+pad;
return (true);
}
/*
* writeBitmap converts the image returned from the pixel grabber to
* the format required. Remember: scan lines are inverted in
* a bitmap file!
*
* Each scan line must be padded to an even 4-byte boundary.
*/
private void writeBitmap () throws Exception {
int value;
int i;
int pad;
byte rgb [] = new byte [3];
if(biBitCount==24)
pad = 4 - ((biWidth * 3) % 4);
else
pad = 4 - ((biWidth) % 4);
if (pad == 4) // <==== Bug correction
pad = 0; // <==== Bug correction
int counter=0;
for(int row = biHeight; row>0; row--) {
if (row%20==0)
IJ.showProgress((double)(biHeight-row)/biHeight);
for(int col = 0; col<biWidth; col++) {
if(biBitCount==24) {
value = intBitmap [(row-1)*biWidth + col ];
rgb [0] = (byte) (value & 0xFF);
rgb [1] = (byte) ((value >> 8) & 0xFF);
rgb [2] = (byte) ((value >> 16) & 0xFF);
bfo.write(rgb);
} else
bfo.write(byteBitmap [(row-1)*biWidth + col ]);
++counter;
}
for (i = 1; i <= pad; i++)
bfo.write (0x00);
counter += pad;
}
}
/*
* writeBitmapFileHeader writes the bitmap file header to the file.
*
*/
private void writeBitmapFileHeader() throws Exception {
fo.write (bfType);
// calculate bfSize
bfSize = bfOffBits+padWidth*biHeight;
fo.write (intToDWord (bfSize));
fo.write (intToWord (bfReserved1));
fo.write (intToWord (bfReserved2));
fo.write (intToDWord (bfOffBits));
}
/*
*
* writeBitmapInfoHeader writes the bitmap information header
* to the file.
*
*/
private void writeBitmapInfoHeader () throws Exception {
fo.write (intToDWord (biSize));
fo.write (intToDWord (biWidth));
fo.write (intToDWord (biHeight));
fo.write (intToWord (biPlanes));
fo.write (intToWord (biBitCount));
fo.write (intToDWord (biCompression));
fo.write (intToDWord (biSizeImage));
fo.write (intToDWord (biXPelsPerMeter));
fo.write (intToDWord (biYPelsPerMeter));
fo.write (intToDWord (biClrUsed));
fo.write (intToDWord (biClrImportant));
}
/*
*
* intToWord converts an int to a word, where the return
* value is stored in a 2-byte array.
*
*/
private byte [] intToWord (int parValue) {
byte retValue [] = new byte [2];
retValue [0] = (byte) (parValue & 0x00FF);
retValue [1] = (byte) ((parValue >> 8) & 0x00FF);
return (retValue);
}
/*
*
* intToDWord converts an int to a double word, where the return
* value is stored in a 4-byte array.
*
*/
private byte [] intToDWord (int parValue) {
byte retValue [] = new byte [4];
retValue [0] = (byte) (parValue & 0x00FF);
retValue [1] = (byte) ((parValue >> 8) & 0x000000FF);
retValue [2] = (byte) ((parValue >> 16) & 0x000000FF);
retValue [3] = (byte) ((parValue >> 24) & 0x000000FF);
return (retValue);
}
}
|