Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/imageout/ImageIOWriter.java
1461 views
1
package org.newdawn.slick.imageout;
2
3
import java.awt.Point;
4
import java.awt.color.ColorSpace;
5
import java.awt.image.BufferedImage;
6
import java.awt.image.ColorModel;
7
import java.awt.image.ComponentColorModel;
8
import java.awt.image.DataBuffer;
9
import java.awt.image.DataBufferByte;
10
import java.awt.image.PixelInterleavedSampleModel;
11
import java.awt.image.Raster;
12
import java.awt.image.WritableRaster;
13
import java.io.IOException;
14
import java.io.OutputStream;
15
import java.nio.ByteBuffer;
16
17
import javax.imageio.ImageIO;
18
19
import org.newdawn.slick.Color;
20
import org.newdawn.slick.Image;
21
22
/**
23
* A utility to write a Slick image out using ImageIO
24
*
25
* @author Jon
26
*/
27
public class ImageIOWriter implements ImageWriter {
28
/**
29
* @see org.newdawn.slick.imageout.ImageWriter#saveImage(org.newdawn.slick.Image,
30
* java.lang.String, java.io.OutputStream, boolean)
31
*/
32
public void saveImage(Image image, String format, OutputStream output, boolean hasAlpha)
33
throws IOException {
34
// conver the image into a byte buffer by reading each pixel in turn
35
int len = 4 * image.getWidth() * image.getHeight();
36
if (!hasAlpha) {
37
len = 3 * image.getWidth() * image.getHeight();
38
}
39
40
ByteBuffer out = ByteBuffer.allocate(len);
41
Color c;
42
43
for (int y = 0; y < image.getHeight(); y++) {
44
for (int x = 0; x < image.getWidth(); x++) {
45
c = image.getColor(x, y);
46
47
out.put((byte) (c.r * 255.0f));
48
out.put((byte) (c.g * 255.0f));
49
out.put((byte) (c.b * 255.0f));
50
if (hasAlpha) {
51
out.put((byte) (c.a * 255.0f));
52
}
53
}
54
}
55
56
// create a raster of the correct format and fill it with our buffer
57
DataBufferByte dataBuffer = new DataBufferByte(out.array(), len);
58
59
PixelInterleavedSampleModel sampleModel;
60
61
ColorModel cm;
62
63
if (hasAlpha) {
64
int[] offsets = { 0, 1, 2, 3 };
65
sampleModel = new PixelInterleavedSampleModel(
66
DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 4,
67
4 * image.getWidth(), offsets);
68
69
cm = new ComponentColorModel(ColorSpace
70
.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 },
71
true, false, ComponentColorModel.TRANSLUCENT,
72
DataBuffer.TYPE_BYTE);
73
} else {
74
int[] offsets = { 0, 1, 2};
75
sampleModel = new PixelInterleavedSampleModel(
76
DataBuffer.TYPE_BYTE, image.getWidth(), image.getHeight(), 3,
77
3 * image.getWidth(), offsets);
78
79
cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
80
new int[] {8,8,8,0},
81
false,
82
false,
83
ComponentColorModel.OPAQUE,
84
DataBuffer.TYPE_BYTE);
85
}
86
WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
87
88
// finally create the buffered image based on the data from the texture
89
// and spit it through to ImageIO
90
BufferedImage img = new BufferedImage(cm, raster, false, null);
91
92
ImageIO.write(img, format, output);
93
}
94
}
95
96