Hallo Leute!
Ich bin gerade dabei ein Dateiformat zu entwickeln, welches sowohl Audio-, Font- und Bildressourcen speichern kann. Ich mache das Ganze textbasierend, d.h. dass eine Ressource in einen String gequetscht und dieser dann reingeschrieben wird.
Bis jetzt habe ich es nur mit Bilddateien hinbekommen, indem ich die Farbe jedes Pixels in 4 Werte zerlegt habe(rot, grün, blau, alpha), diesen Wert in ein Zeichen konvertiert habe, sie aneinander geheftet und ein '#' vor den Block gesetzt habe. Zudem besteht der erste Block aus der Größe des Bildes.
Das Auslesen funktioniert auch ganz gut, allerdings habe ich keine Ahnung, wie ich das mit Audio- oder Fontdatein machen soll.
Hier ist nochmal der Code für die Bildressorce, damit ihr ein bisschen besser verstehen könnt was ich meine:
Ich bin gerade dabei ein Dateiformat zu entwickeln, welches sowohl Audio-, Font- und Bildressourcen speichern kann. Ich mache das Ganze textbasierend, d.h. dass eine Ressource in einen String gequetscht und dieser dann reingeschrieben wird.
Bis jetzt habe ich es nur mit Bilddateien hinbekommen, indem ich die Farbe jedes Pixels in 4 Werte zerlegt habe(rot, grün, blau, alpha), diesen Wert in ein Zeichen konvertiert habe, sie aneinander geheftet und ein '#' vor den Block gesetzt habe. Zudem besteht der erste Block aus der Größe des Bildes.
Das Auslesen funktioniert auch ganz gut, allerdings habe ich keine Ahnung, wie ich das mit Audio- oder Fontdatein machen soll.
Hier ist nochmal der Code für die Bildressorce, damit ihr ein bisschen besser verstehen könnt was ich meine:
Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageProducer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
public class ImageResource {
public static final int SCALE_METHOD_FAST = Image.SCALE_FAST;
public static final int SCALE_METHOD_DEFAULT = Image.SCALE_DEFAULT;
public static final int SCALE_METHOD_SMOOTH = Image.SCALE_SMOOTH;
private ResourceLocation location;
private String path;
private BufferedImage image;
private final String[] buffers = {
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d",
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", ",", "µ", "@", "€", "!", "§", "$", "%",
"&", "/", "(", ")", "=", "?", "²", "³", "{", "}",
"+", "*", "~", "<", ">", "|", ";", "\\", "\"", "-",
"_", "'", "ß", "Ä", "Ö", "Ü", "^", "°", "´", "`",
" ", "¡", "¸", "¹", "º", "»", "¼", "½", "¾", "¿",
"·", "¶", "±", "¯", "®", "¬", "«", "ª", "©", "¨",
"¦", "¥", "¤", "£", "¢", "Â", "Ê", "Î", "Ô", "Û",
"ý", "â", "ê", "î", "ô", "û", "Á", "É", "Í", "Ó",
"Ú", "á", "é", "í", "ó", "ú", "À", "È", "Ì", "Ò",
"Ù", "à", "è", "ì", "ò", "ù", "Ã", "Ą", "ą", "Ċ",
"Ă", "ā", "Ā", "ÿ", "þ", "ü", "ø", "÷", "õ", "ă",
"ñ", "ð", "ï", "ë", "ç", "æ", "å", "ã", "Þ", "Ý",
"Ø", "×", "Õ", "Ñ", "Ð", "Ï", "Ë", "Ç", "Æ", "Å",
"Č", "IJ", "ı", "İ", "į", "Į", "ĭ", "Ĭ", "ī", "Ī",
"Ġ", "ġ", "Ģ", "ģ", "Ĥ", "ĥ", "Ħ", "ħ", "Ĩ", "ĩ",
"ğ", "Ğ", "ĝ", "Ĝ", "ě", "Ě", "ę", "Ę", "ė", "Ė",
"ĕ", "Ĕ", "ē", "Ē", "đ", "Đ", "ď", "Ď", "č", "Ĺ",
"Ō", "ō", "Ŏ", "ŏ", "Ő", "Œ", "œ", "Ŗ", "ŗ", "Ļ",
"Ŋ", "ʼn", "ň", "Ň", "ŋ", "ő", "Ŕ", "ŕ", "Ł", "Ř",
"ij", "Ĵ", "ĵ", "Ķ", "ķ", "ř"
};
public ImageResource(String imageCode) {
String[] codeBlocks = imageCode.split("#");
String[] values = new String[codeBlocks.length - 1];
int width = Integer.parseInt(codeBlocks[0].split("x")[0]);
int height = Integer.parseInt(codeBlocks[0].split("x")[1]);
this.image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
for(int i = 1; i < codeBlocks.length; i++) {
values[i-1] = codeBlocks[i];
}
Color[] pixels = new Color[codeBlocks.length - 1];
int color = 0;
for(int i = 0; i < pixels.length; i++) {
int[] translatedColorCode = new int[4];
String colorCode = "";
for(int pos = 0; pos < 4; pos++) {
colorCode = values[i].split("|")[pos];
for(int c = 0; c < 256; c++) {
if(colorCode.equals(buffers[c])) {
translatedColorCode[pos] = c;
break;
}
}
}
pixels[i] = new Color(translatedColorCode[0], translatedColorCode[1], translatedColorCode[2], translatedColorCode[3]);
}
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
this.image.setRGB(x, y, pixels[color].getRGB());
color++;
}
}
}
public ImageResource(String path, ResourceLocation location) {
this.path = path;
this.location = location;
try {
if(!path.toUpperCase().endsWith(".RGF")) {
if(location.equals(ResourceLocation.JAR)) {
this.image = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
} else if(location.equals(ResourceLocation.WEB)) {
this.image = ImageIO.read(new URL(path));
} else if(location.equals(ResourceLocation.PC)) {
this.image = ImageIO.read(new File(path));
} else {
throw new Exception("This location does not exist!");
}
} else {
String code = null;
if(location.equals(ResourceLocation.JAR)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(path)));
code = reader.readLine();
reader.close();
} else if(location.equals(ResourceLocation.PC)) {
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
code = reader.readLine();
reader.close();
} else if(location.equals(ResourceLocation.WEB)) {
BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(path).openStream()));
code = reader.readLine();
reader.close();
} else {
throw new Exception("This location does not exist!");
}
String[] codeBlocks = code.split("#");
String[] values = new String[codeBlocks.length - 1];
int width = Integer.parseInt(codeBlocks[0].split("x")[0]);
int height = Integer.parseInt(codeBlocks[0].split("x")[1]);
this.image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
for(int i = 1; i < codeBlocks.length; i++) {
values[i-1] = codeBlocks[i];
}
Color[] pixels = new Color[codeBlocks.length - 1];
int color = 0;
for(int i = 0; i < pixels.length; i++) {
int[] translatedColorCode = new int[4];
String colorCode = "";
for(int pos = 0; pos < 4; pos++) {
colorCode = values[i].split("|")[pos];
for(int c = 0; c < 256; c++) {
if(colorCode.equals(buffers[c])) {
translatedColorCode[pos] = c;
break;
}
}
}
pixels[i] = new Color(translatedColorCode[0], translatedColorCode[1], translatedColorCode[2], translatedColorCode[3]);
}
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
this.image.setRGB(x, y, pixels[color].getRGB());
color++;
}
}
}
} catch(MalformedURLException exception) {exception.printStackTrace();
} catch(IOException exception) {exception.printStackTrace();
} catch(Exception exception) {exception.printStackTrace();
}
}
public String getImageAsString() {
String imageString = this.image.getWidth() + "x" + this.image.getHeight();
for(int x = 0; x < this.image.getWidth(); x++) {
for(int y = 0; y < this.image.getHeight(); y++) {
int rgb = this.image.getRGB(x, y);
Color color = new Color(rgb);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int alpha = color.getAlpha();
imageString += "#" + buffers[red] + buffers[green] + buffers[blue] + buffers[alpha];
}
}
return imageString;
}
public void scale(int width, int height, int method) {
Image buffer = this.getScaledInstance(width, height, method);
Color[] colors = new Color[buffer.getWidth(null) * buffer.getHeight(null)];
this.image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
int color = 0;
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
this.image.setRGB(x, y, colors[color].getRGB());
color++;
}
}
}
public void rotate(float degrees) {
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(degrees), image.getWidth() / 2, image.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
image = op.filter(image, null);
}
public int getRGB(int x, int y) {
return this.image.getRGB(x, y);
}
public ImageProducer getSource() {
return this.image.getSource();
}
public Graphics getGraphics() {
return this.image.getGraphics();
}
public int getWidth() {
return this.image.getWidth();
}
public int getHeight() {
return this.image.getHeight();
}
public BufferedImage getImage() {
return this.image;
}
public Image getTile(int x, int y, int width, int height) {
return this.image.getSubimage(x, y, width, height);
}
public Image getScaledInstance(int width, int height, int method) {
return this.image.getScaledInstance(width, height, method);
}
public String getPath() {
return this.path;
}
public ResourceLocation getLocation() {
return this.location;
}
}
public enum ResourceLocation {
PC,
WEB,
JAR;
}
Zuletzt bearbeitet von einem Moderator: