added sources for Slick

Former-commit-id: 1647fa32ef6894bd7db44f741f07c2f4dcdf9054
Former-commit-id: 0e5810dcfbe1fd59b13e7cabe9f1e93c5542da2d
This commit is contained in:
Song Minjae
2016-12-30 23:29:12 +09:00
parent d1f01a203d
commit d3080ffb78
329 changed files with 58400 additions and 7 deletions

View File

@@ -0,0 +1,94 @@
package org.newdawn.slick.imageout;
import java.awt.Point;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.PixelInterleavedSampleModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import javax.imageio.ImageIO;
import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
/**
* A utility to write a Slick image out using ImageIO
*
* @author Jon
*/
public class ImageIOWriter implements ImageWriter {
/**
* @see org.newdawn.slick.imageout.ImageWriter#saveImage(org.newdawn.slick.Image,
* java.lang.String, java.io.OutputStream, boolean)
*/
public void saveImage(Image image, String format, OutputStream output, boolean hasAlpha)
throws IOException {
// conver the image into a byte buffer by reading each pixel in turn
int len = 4 * image.getWidth() * image.getHeight();
if (!hasAlpha) {
len = 3 * image.getWidth() * image.getHeight();
}
ByteBuffer out = ByteBuffer.allocate(len);
Color c;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
c = image.getColor(x, y);
out.put((byte) (c.r * 255.0f));
out.put((byte) (c.g * 255.0f));
out.put((byte) (c.b * 255.0f));
if (hasAlpha) {
out.put((byte) (c.a * 255.0f));
}
}
}
// create a raster of the correct format and fill it with our buffer
DataBufferByte dataBuffer = new DataBufferByte(out.array(), len);
PixelInterleavedSampleModel sampleModel;
ColorModel cm;
if (hasAlpha) {
int[] offsets = { 0, 1, 2, 3 };
sampleModel = new PixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 4,
4 * image.getWidth(), offsets);
cm = new ComponentColorModel(ColorSpace
.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 },
true, false, ComponentColorModel.TRANSLUCENT,
DataBuffer.TYPE_BYTE);
} else {
int[] offsets = { 0, 1, 2};
sampleModel = new PixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 3,
3 * image.getWidth(), offsets);
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[] {8,8,8,0},
false,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
}
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
// finally create the buffered image based on the data from the texture
// and spit it through to ImageIO
BufferedImage img = new BufferedImage(cm, raster, false, null);
ImageIO.write(img, format, output);
}
}

View File

@@ -0,0 +1,131 @@
package org.newdawn.slick.imageout;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
/**
* A static hook to access all the Image output utilities. The list of format strings
* provided is not the limit of capability. These are provided for utility, use @see {@link #getSupportedFormats()}
* for a full list of supported formats.
*
* @author kevin
*/
public class ImageOut {
/** The default setting for writing out the alpha channel */
private static final boolean DEFAULT_ALPHA_WRITE = false;
/** The format string for TGA */
public static String TGA = "tga";
/** The format string for PNG */
public static String PNG = "png";
/** The format string for JPG */
public static String JPG = "jpg";
/**
* Get a list of supported formats
*
* @see ImageWriterFactory#getSupportedFormats()
* @return The list of supported format strings
*/
public static String[] getSupportedFormats() {
return ImageWriterFactory.getSupportedFormats();
}
/**
* Write an image out to a specified output stream
*
* @param image The image to write out to
* @param format The format to write the image out in
* @param out The output stream to which the image should be written
* @throws SlickException Indicates a failure to write the image in the specified format
*/
public static void write(Image image, String format, OutputStream out) throws SlickException {
write(image, format, out, DEFAULT_ALPHA_WRITE);
}
/**
* Write an image out to a specified output stream
*
* @param image The image to write out to
* @param format The format to write the image out in
* @param out The output stream to which the image should be written
* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)
* @throws SlickException Indicates a failure to write the image in the specified format
*/
public static void write(Image image, String format, OutputStream out, boolean writeAlpha) throws SlickException {
try {
ImageWriter writer = ImageWriterFactory.getWriterForFormat(format);
writer.saveImage(image, format, out, writeAlpha);
} catch (IOException e) {
throw new SlickException("Unable to write out the image in format: "+format, e);
}
}
/**
* Write an image out to a file on the local file system. The format of the output
* is determined based on the file name extension
*
* @param image The image to be written out
* @param dest The destination path to write to
* @throws SlickException Indicates a failure to write the image in the determined format
*/
public static void write(Image image, String dest) throws SlickException {
write(image, dest, DEFAULT_ALPHA_WRITE);
}
/**
* Write an image out to a file on the local file system. The format of the output
* is determined based on the file name extension
*
* @param image The image to be written out
* @param dest The destination path to write to
* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)
* @throws SlickException Indicates a failure to write the image in the determined format
*/
public static void write(Image image, String dest, boolean writeAlpha) throws SlickException {
try {
int ext = dest.lastIndexOf('.');
if (ext < 0) {
throw new SlickException("Unable to determine format from: "+dest);
}
String format = dest.substring(ext+1);
write(image, format, new FileOutputStream(dest), writeAlpha);
} catch (IOException e) {
throw new SlickException("Unable to write to the destination: "+dest, e);
}
}
/**
* Write an image out to a file on the local file system.
*
* @param image The image to be written out
* @param format The format to write the image out in
* @param dest The destination path to write to
* @throws SlickException Indicates a failure to write the image in the determined format
*/
public static void write(Image image, String format, String dest) throws SlickException {
write(image, format, dest, DEFAULT_ALPHA_WRITE);
}
/**
* Write an image out to a file on the local file system.
*
* @param image The image to be written out
* @param format The format to write the image out in
* @param dest The destination path to write to
* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)
* @throws SlickException Indicates a failure to write the image in the determined format
*/
public static void write(Image image, String format, String dest, boolean writeAlpha) throws SlickException {
try {
write(image, format, new FileOutputStream(dest), writeAlpha);
} catch (IOException e) {
throw new SlickException("Unable to write to the destination: "+dest, e);
}
}
}

View File

@@ -0,0 +1,25 @@
package org.newdawn.slick.imageout;
import java.io.IOException;
import java.io.OutputStream;
import org.newdawn.slick.Image;
/**
* The description of any class that can produce data to an output stream reprsenting
* some image in memory.
*
* @author Jon
*/
public interface ImageWriter {
/**
* Save an Image to an given location
*
* @param image The image to be written
* @param format The format that this writer is expected to be produced in
* @param out The output stream to which the image data should be written
* @param writeAlpha True if we should write alpha information to the file
* @throws IOException Indicates a failure to write out the image to the specified location
*/
void saveImage(Image image, String format, OutputStream out, boolean writeAlpha) throws IOException;
}

View File

@@ -0,0 +1,76 @@
package org.newdawn.slick.imageout;
import java.util.HashMap;
import javax.imageio.ImageIO;
import org.newdawn.slick.SlickException;
/**
* A factory to produce image writers based on format names
*
* @author kevin
*/
public class ImageWriterFactory {
/** The map from format names to image writer instances */
private static HashMap writers = new HashMap();
// Initialise the list of writers based on the classes we know about
static {
String[] formats = ImageIO.getWriterFormatNames();
ImageIOWriter writer = new ImageIOWriter();
for (int i=0;i<formats.length;i++) {
registerWriter(formats[i], writer);
}
TGAWriter tga = new TGAWriter();
registerWriter("tga", tga);
}
/**
* Register an image writer with the factory. This will allow users
* to use it to write out the explicit format
*
* @param format The format (usually extension) of the files that will be written out
* @param writer The writer to use for the given format
*/
public static void registerWriter(String format, ImageWriter writer) {
writers.put(format, writer);
}
/**
* Get the list of support format strings for this factory
*
* @return The list of support format strings for this factory
*/
public static String[] getSupportedFormats() {
return (String[]) writers.keySet().toArray(new String[0]);
}
/**
* Get a Slick image writer for the given format
*
* @param format The format of the image to write
* @return The image write to use to produce these images
* @throws SlickException
*/
public static ImageWriter getWriterForFormat(String format) throws SlickException
{
ImageWriter writer = (ImageWriter) writers.get(format);
if (writer != null) {
return writer;
}
writer = (ImageWriter) writers.get(format.toLowerCase());
if (writer != null) {
return writer;
}
writer = (ImageWriter) writers.get(format.toUpperCase());
if (writer != null) {
return writer;
}
throw new SlickException("No image writer available for: "+format);
}
}

View File

@@ -0,0 +1,86 @@
package org.newdawn.slick.imageout;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
/**
* A utility to save TGA's given a Slick image.
*
* @author Jon
*/
public class TGAWriter implements ImageWriter {
/**
* Flip the endian-ness of the short
*
* @param signedShort The short to flip
* @return The flipped short
*/
private static short flipEndian(short signedShort) {
int input = signedShort & 0xFFFF;
return (short) (input << 8 | (input & 0xFF00) >>> 8);
}
/**
* @see org.newdawn.slick.imageout.ImageWriter#saveImage(org.newdawn.slick.Image, java.lang.String, java.io.OutputStream, boolean)
*/
public void saveImage(Image image, String format, OutputStream output, boolean writeAlpha) throws IOException {
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(output));
// ID Length
out.writeByte((byte) 0);
// Color Map
out.writeByte((byte) 0);
// Image Type
out.writeByte((byte) 2);
// Color Map - Ignored
out.writeShort(flipEndian((short) 0));
out.writeShort(flipEndian((short) 0));
out.writeByte((byte) 0);
// X, Y Offset
out.writeShort(flipEndian((short) 0));
out.writeShort(flipEndian((short) 0));
// Width, Height, Depth
out.writeShort(flipEndian((short) image.getWidth()));
out.writeShort(flipEndian((short) image.getHeight()));
if (writeAlpha) {
out.writeByte((byte) 32);
// Image Descriptor (can't be 0 since we're using 32-bit TGAs)
// needs to not have 0x20 set to indicate it's not a flipped image
out.writeByte((byte) 1);
} else {
out.writeByte((byte) 24);
// Image Descriptor (must be 0 since we're using 24-bit TGAs)
// needs to not have 0x20 set to indicate it's not a flipped image
out.writeByte((byte) 0);
}
// Write out the image data
Color c;
for (int y = image.getHeight()-1; y <= 0; y--) {
for (int x = 0; x < image.getWidth(); x++) {
c = image.getColor(x, y);
out.writeByte((byte) (c.b * 255.0f));
out.writeByte((byte) (c.g * 255.0f));
out.writeByte((byte) (c.r * 255.0f));
if (writeAlpha) {
out.writeByte((byte) (c.a * 255.0f));
}
}
}
out.close();
}
}

View File

@@ -0,0 +1,3 @@
<BODY>
Supports saving of slick images to various file types.
</BODY>