-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRgbLoader.java
More file actions
executable file
·48 lines (36 loc) · 1.41 KB
/
Copy pathRgbLoader.java
File metadata and controls
executable file
·48 lines (36 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package io.github.USN484259;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.ByteOrder;
class badFormatException extends Exception{
public badFormatException(){
super("bad image format");
}
}
public class RgbLoader {
public static BufferedImage read(File in) throws IOException,badFormatException {
try{
FileImageInputStream stream = new FileImageInputStream(in);
stream.setByteOrder(ByteOrder.LITTLE_ENDIAN);
if (stream.readByte() != 2 || stream.readByte() != 4)
throw new badFormatException();
int width = stream.readUnsignedShort();
int height = stream.readUnsignedShort();
if (stream.readUnsignedShort() != 0)
throw new badFormatException();
BufferedImage img = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int y = 0;y != height;++y){
for (int x = 0;x != width;++x){
int pixel = stream.readInt();
img.setRGB(x,y,pixel);
}
}
return img;
}
catch(EOFException e){
throw new badFormatException();
}
}
}