Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/imageout/ImageOut.java
1461 views
1
package org.newdawn.slick.imageout;
2
3
import java.io.FileOutputStream;
4
import java.io.IOException;
5
import java.io.OutputStream;
6
7
import org.newdawn.slick.Image;
8
import org.newdawn.slick.SlickException;
9
10
/**
11
* A static hook to access all the Image output utilities. The list of format strings
12
* provided is not the limit of capability. These are provided for utility, use @see {@link #getSupportedFormats()}
13
* for a full list of supported formats.
14
*
15
* @author kevin
16
*/
17
public class ImageOut {
18
/** The default setting for writing out the alpha channel */
19
private static final boolean DEFAULT_ALPHA_WRITE = false;
20
21
/** The format string for TGA */
22
public static String TGA = "tga";
23
/** The format string for PNG */
24
public static String PNG = "png";
25
/** The format string for JPG */
26
public static String JPG = "jpg";
27
28
/**
29
* Get a list of supported formats
30
*
31
* @see ImageWriterFactory#getSupportedFormats()
32
* @return The list of supported format strings
33
*/
34
public static String[] getSupportedFormats() {
35
return ImageWriterFactory.getSupportedFormats();
36
}
37
38
/**
39
* Write an image out to a specified output stream
40
*
41
* @param image The image to write out to
42
* @param format The format to write the image out in
43
* @param out The output stream to which the image should be written
44
* @throws SlickException Indicates a failure to write the image in the specified format
45
*/
46
public static void write(Image image, String format, OutputStream out) throws SlickException {
47
write(image, format, out, DEFAULT_ALPHA_WRITE);
48
}
49
50
/**
51
* Write an image out to a specified output stream
52
*
53
* @param image The image to write out to
54
* @param format The format to write the image out in
55
* @param out The output stream to which the image should be written
56
* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)
57
* @throws SlickException Indicates a failure to write the image in the specified format
58
*/
59
public static void write(Image image, String format, OutputStream out, boolean writeAlpha) throws SlickException {
60
try {
61
ImageWriter writer = ImageWriterFactory.getWriterForFormat(format);
62
writer.saveImage(image, format, out, writeAlpha);
63
} catch (IOException e) {
64
throw new SlickException("Unable to write out the image in format: "+format, e);
65
}
66
}
67
68
/**
69
* Write an image out to a file on the local file system. The format of the output
70
* is determined based on the file name extension
71
*
72
* @param image The image to be written out
73
* @param dest The destination path to write to
74
* @throws SlickException Indicates a failure to write the image in the determined format
75
*/
76
public static void write(Image image, String dest) throws SlickException {
77
write(image, dest, DEFAULT_ALPHA_WRITE);
78
}
79
80
/**
81
* Write an image out to a file on the local file system. The format of the output
82
* is determined based on the file name extension
83
*
84
* @param image The image to be written out
85
* @param dest The destination path to write to
86
* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)
87
* @throws SlickException Indicates a failure to write the image in the determined format
88
*/
89
public static void write(Image image, String dest, boolean writeAlpha) throws SlickException {
90
try {
91
int ext = dest.lastIndexOf('.');
92
if (ext < 0) {
93
throw new SlickException("Unable to determine format from: "+dest);
94
}
95
96
String format = dest.substring(ext+1);
97
write(image, format, new FileOutputStream(dest), writeAlpha);
98
} catch (IOException e) {
99
throw new SlickException("Unable to write to the destination: "+dest, e);
100
}
101
}
102
103
/**
104
* Write an image out to a file on the local file system.
105
*
106
* @param image The image to be written out
107
* @param format The format to write the image out in
108
* @param dest The destination path to write to
109
* @throws SlickException Indicates a failure to write the image in the determined format
110
*/
111
public static void write(Image image, String format, String dest) throws SlickException {
112
write(image, format, dest, DEFAULT_ALPHA_WRITE);
113
}
114
115
/**
116
* Write an image out to a file on the local file system.
117
*
118
* @param image The image to be written out
119
* @param format The format to write the image out in
120
* @param dest The destination path to write to
121
* @param writeAlpha True if we should write the alpha channel out (some formats don't support this, like JPG)
122
* @throws SlickException Indicates a failure to write the image in the determined format
123
*/
124
public static void write(Image image, String format, String dest, boolean writeAlpha) throws SlickException {
125
try {
126
write(image, format, new FileOutputStream(dest), writeAlpha);
127
} catch (IOException e) {
128
throw new SlickException("Unable to write to the destination: "+dest, e);
129
}
130
}
131
}
132
133