-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormat.java
172 lines (144 loc) · 3.95 KB
/
Format.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package format;
import java.io.FileNotFoundException;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.File;
import org.apache.commons.io.FileUtils;
/**
* An abstract class that extends various image formats.
* @author Nick
*/
public abstract class Format {
/**
* logger
*/
private static final Logger log = Logger.getLogger(Format.class.getName());
/**
* Holds the image data.
*/
public BufferedImage image;
/**
* Holds file info.
*/
public List<String> info;
/**
* Filename of the image.
*/
public String filename;
/**
* File data.
*/
public byte[] data;
/**
* Size of image data in bytes.
*/
public int size;
/**
* If the file is valid or not.
*/
boolean isValid;
/**
* Image width.
*/
public int width;
/**
* Image height.
*/
public int height;
/**
* Color depth (bit depth).
*/
public int colorDepth;
/**
* Various image effects.
*/
public enum Effect {
/**
* No effect.
*/
NO_EFFECT,
/**
* Black & white effect.
*/
NOIR
}
public Effect effect;
/**
* Converts a hex string to an integer.
* @param str The hex string to be converted.
* @return The decimal representation of the hex string.
*/
public static int hexStringToInt(String str) {
return Integer.parseInt(str, 16);
}
/**
* Converts an rgb value to a single integer pixel.
* @param r The red value to be converted.
* @param g The green value to be converted.
* @param b The blue value to be converted.
* @return The integer value of the rgb value.
*/
public static int rgbToInt(int r, int g, int b) {
return ((r&0x0ff)<<16)|((g&0x0ff)<<8)|(b&0x0ff);
}
/**
* Converts a single byte into a String.
* @param b The byte to be converted.
* @return The String representation of the byte.
*/
public static String byteToString(byte b) {
return String.format("%02X", b);
}
/**
* Fetches the file, and its byte data
*/
public void getFileByteData() {
File file = new File(this.filename);
// get header byteData in bytes
try {
this.data = FileUtils.readFileToByteArray(file);
} catch (IOException ex) {
log.log(Level.SEVERE,
String.format("File %s not found!", this.filename),
ex);
}
// set image size
this.size = this.data.length;
// image is valid until proven invalid
this.isValid = true;
// make ArrayList of strings for file info
this.info = new ArrayList<>();
}
/**
* Loads an image from this format.
* @param filename the name of the file to be loaded
* @throws FileNotFoundException
*/
public abstract void load(String filename) throws FileNotFoundException;
/**
* Saves an image from this format.
* @param filename the name of the file to be saved
*/
public abstract void save(String filename);
public void setEffect(Effect effect) {
switch(effect) {
case NO_EFFECT:
System.out.println("No effect applied!\n");
break;
case NOIR:
System.out.println("Noir effect applied!\n");
break;
default:
System.out.println("Invalid effect!\n");
}
}
}