Java ByteBuffer Save saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height)

Here you can find the source of saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height)

Description

save As BMP

License

Open Source License

Declaration

public static void saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import java.nio.IntBuffer;

public class Main {
    public static void saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height) {
        long before = System.currentTimeMillis();
        int pad = 4 - (width * 3) % 4;
        if (pad == 4)
            pad = 0;// w ww . ja  v  a2s . c  o  m
        int size = (width * 3 + pad) * height + 54;
        ByteBuffer buffer = ByteBuffer.allocate(size);

        //write BMP header
        buffer.put((byte) 0x42); // signature, must be 4D42 hex
        buffer.put((byte) 0x4D); // ...
        buffer.put((byte) (size & 0x000000ff)); // size of BMP file in bytes
        buffer.put((byte) ((size & 0x0000ff00) >> 8)); // ...
        buffer.put((byte) ((size & 0x00ff0000) >> 16)); // ...
        buffer.put((byte) ((size & 0xff000000) >> 24)); // ...
        buffer.put((byte) 0); // reserved, must be zero
        buffer.put((byte) 0); // reserved, must be zero
        buffer.put((byte) 0); // reserved, must be zero
        buffer.put((byte) 0); // reserved, must be zero
        buffer.put((byte) 54); // offset to start of image data in bytes
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 40); // size of BITMAPINFOHEADER structure, must be 40
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) (width & 0x000000ff)); // image width in pixels
        buffer.put((byte) ((width & 0x0000ff00) >> 8)); // ...
        buffer.put((byte) ((width & 0x00ff0000) >> 16)); // ...
        buffer.put((byte) ((width & 0xff000000) >> 24)); // ...
        buffer.put((byte) (height & 0x000000ff)); // image width in pixels
        buffer.put((byte) ((height & 0x0000ff00) >> 8)); // ...
        buffer.put((byte) ((height & 0x00ff0000) >> 16));// ...
        buffer.put((byte) ((height & 0xff000000) >> 24));// ...
        buffer.put((byte) 1); // number of planes in the image, must be 1
        buffer.put((byte) 0); // ...
        buffer.put((byte) 24); // number of bits per pixel (1, 4, 8, or 24)
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // compression type (0=none, 1=RLE-8, 2=RLE-4)
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) ((size - 54) & 0x000000ff)); // size of image data in bytes (including padding)
        buffer.put((byte) (((size - 54) & 0x0000ff00) >> 8)); // ...
        buffer.put((byte) (((size - 54) & 0x00ff0000) >> 16)); // ...
        buffer.put((byte) (((size - 54) & 0xff000000) >> 24)); // ...
        buffer.put((byte) 0); // horizontal resolution in pixels per meter (unreliable)
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // vertical resolution in pixels per meter (unreliable)
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // number of colors in image, or zero
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // number of important colors, or zero
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...
        buffer.put((byte) 0); // ...

        pixel_data.rewind();
        IntBuffer int_pixel_data = pixel_data.asIntBuffer();
        //write BMP image data
        for (int y = height - 1; y >= 0; y--) {
            for (int x = 0; x < width; x++) {
                int pixel = int_pixel_data.get(y * width + x);
                byte r = (byte) ((pixel >> 24) & 0xff);
                byte g = (byte) ((pixel >> 16) & 0xff);
                byte b = (byte) ((pixel >> 8) & 0xff);
                buffer.put(b);
                buffer.put(g);
                buffer.put(r);
            }
            for (int i = 0; i < pad; i++) {
                buffer.put((byte) 0);
            }

        }
        buffer.rewind();
        File image_file = new File(filename);
        try (FileOutputStream fout = new FileOutputStream(image_file)) {
            fout.write(buffer.array());
            fout.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long after = System.currentTimeMillis();
        System.out.println("File " + filename + " saved in " + (after - before) + " milliseconds");
    }
}

Related

  1. save(Path path, ByteBuffer bb)
  2. saveAsFile(File targetFile, ByteBuffer bb)
  3. saveAsTempFile(ByteBuffer byteBuffer, String extension)
  4. saveAsTGA(String filename, ByteBuffer pixel_data, int width, int height)
  5. saveBytesBufferToFile(ByteBuffer buf, String filename)