Path: blob/master/SLICK_HOME/src/org/newdawn/slick/imageout/TGAWriter.java
1461 views
package org.newdawn.slick.imageout;12import java.io.BufferedOutputStream;3import java.io.DataOutputStream;4import java.io.IOException;5import java.io.OutputStream;67import org.newdawn.slick.Color;8import org.newdawn.slick.Image;910/**11* A utility to save TGA's given a Slick image.12*13* @author Jon14*/15public class TGAWriter implements ImageWriter {16/**17* Flip the endian-ness of the short18*19* @param signedShort The short to flip20* @return The flipped short21*/22private static short flipEndian(short signedShort) {23int input = signedShort & 0xFFFF;24return (short) (input << 8 | (input & 0xFF00) >>> 8);25}2627/**28* @see org.newdawn.slick.imageout.ImageWriter#saveImage(org.newdawn.slick.Image, java.lang.String, java.io.OutputStream, boolean)29*/30public void saveImage(Image image, String format, OutputStream output, boolean writeAlpha) throws IOException {31DataOutputStream out = new DataOutputStream(new BufferedOutputStream(output));3233// ID Length34out.writeByte((byte) 0);3536// Color Map37out.writeByte((byte) 0);3839// Image Type40out.writeByte((byte) 2);4142// Color Map - Ignored43out.writeShort(flipEndian((short) 0));44out.writeShort(flipEndian((short) 0));45out.writeByte((byte) 0);4647// X, Y Offset48out.writeShort(flipEndian((short) 0));49out.writeShort(flipEndian((short) 0));5051// Width, Height, Depth52out.writeShort(flipEndian((short) image.getWidth()));53out.writeShort(flipEndian((short) image.getHeight()));54if (writeAlpha) {55out.writeByte((byte) 32);56// Image Descriptor (can't be 0 since we're using 32-bit TGAs)57// needs to not have 0x20 set to indicate it's not a flipped image58out.writeByte((byte) 1);59} else {60out.writeByte((byte) 24);61// Image Descriptor (must be 0 since we're using 24-bit TGAs)62// needs to not have 0x20 set to indicate it's not a flipped image63out.writeByte((byte) 0);64}656667// Write out the image data68Color c;6970for (int y = 0; y < image.getHeight(); y++) {71for (int x = 0; x < image.getWidth(); x++) {72c = image.getColor(x, y);7374out.writeByte((byte) (c.b * 255.0f));75out.writeByte((byte) (c.g * 255.0f));76out.writeByte((byte) (c.r * 255.0f));77if (writeAlpha) {78out.writeByte((byte) (c.a * 255.0f));79}80}81}8283out.close();84}85}868788