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

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

Description

save As TGA

License

Open Source License

Declaration

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

Method Source Code

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

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

public class Main {
    public static void saveAsTGA(String filename, ByteBuffer pixel_data, int width, int height) {
        long before = System.currentTimeMillis();
        try (FileOutputStream fout = new FileOutputStream(filename + ".tga")) {

            //write TGA header
            fout.write(0); //ID length, 0 because no image id field
            fout.write(0); //no color map
            fout.write(2); //image type (24 bit RGB, uncompressed)
            fout.write(0); //color map origin, ignore because no color map
            fout.write(0); //color map origin, ignore because no color map
            fout.write(0); //color map origin, ignore because no color map
            fout.write(0); //color map length, ignore because no color map
            fout.write(0); //color map entry size, ignore because no color map
            fout.write(0); //x origin
            fout.write(0); //x origin
            fout.write(0); //x origin
            fout.write(0); //y origin
            short s = (short) width;
            fout.write((byte) (s & 0x00ff)); //image width low byte
            fout.write((byte) ((s & 0xff00) >> 8)); //image width high byte
            s = (short) height;
            fout.write((byte) (s & 0x00ff)); //image height low byte
            fout.write((byte) ((s & 0xff00) >> 8)); //image height high byte
            fout.write(32); //bpp
            fout.write(0); //description bits

            pixel_data.rewind();/*from  ww  w.  j  ava  2 s .  c  o m*/
            //write TGA image data
            fout.getChannel().write(pixel_data);

            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. saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height)
  3. saveAsFile(File targetFile, ByteBuffer bb)
  4. saveAsTempFile(ByteBuffer byteBuffer, String extension)
  5. saveBytesBufferToFile(ByteBuffer buf, String filename)
  6. savePageBitMask(ByteBuffer buffer, BitSet freePagesBitSet)
  7. saveStruct(ByteBuffer o, String[] struct, Object instance)