Fabre wrote:The problem that I am not sure what program I should use to convert my BMPs into C files. What is the best converter to use?
If you have Linux or Gimp, you can save it as XPM, which is simply a C program. If you want to store it in some other format, you can use for example the little Java program below, which can read PNG, GIF and all other formats, which are supported by Java. It writes it as a special compressed format with a palette, initially programmed by me, because a customer doesn't want to include GIF graphics in an Java-Applet, but you can change the write block without problems (and if not, don't think writing a PSP game is easier :-)
Code: Select all
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import sun.net.www.content.image.gif;
public class gif2fgf {
public static void main(String args[]) {
// check arguments
if (args.length != 1) {
System.out.println("Usage: gif2fgf pic.gif");
return;
}
// load GIF, PNG or JPG file
Frame dummy = new Frame();
String filename = args[0];
Image gif = Toolkit.getDefaultToolkit().getImage(filename);
MediaTracker tracker = new MediaTracker(dummy);
tracker.addImage(gif, 0);
try {
tracker.waitForID(0);
} catch (Exception e) {
}
// determine pixels
int w = gif.getWidth(dummy);
int h = gif.getHeight(dummy);
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(gif, 0, 0, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// calculate palette
HashSet palUnique = new HashSet();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int pixel = pixels[y * w + x];
palUnique.add(new Integer(pixel));
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
}
}
Object pal[] = palUnique.toArray();
if (pal.length > 255) {
System.out.println("internal error: too many palette colors");
return;
}
HashMap palLookup = new HashMap();
for (int i = 0; i < pal.length; i++)
palLookup.put(pal[i], new Integer(i));
// save as FGF (Frank's Grafik Fileformat)
try {
// open zlib output stream
FileOutputStream fileOut =
new FileOutputStream(filename.substring(0, filename.length() - 4) + ".fgf");
DeflaterOutputStream out = new DeflaterOutputStream(fileOut);
// write dimension
out.write(w & 255);
out.write((w >> 8) & 255);
out.write(h & 255);
out.write((h >> 8) & 255);
// write palette
out.write(pal.length);
for (int i = 0; i < pal.length; i++) {
int pixel = ((Integer) pal[i]).intValue();
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
out.write(alpha);
out.write(red);
out.write(green);
out.write(blue);
}
// write pixels
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int pixelIndex =
((Integer) palLookup.get(new Integer(pixels[y * w + x]))).intValue();
out.write(pixelIndex);
}
}
out.close();
} catch (IOException e) {
System.out.println("error while writing");
e.printStackTrace();
}
System.exit(0);
}
/*
* Use this method in Applets to load an image.
*/
/*
private Image loadFGF(String imageName)
{
try {
InputStream fileIn = (new URL(getDocumentBase(), imageName)).openStream();
InflaterInputStream in = new InflaterInputStream(fileIn);
// read dimension
int w = in.read();
w |= in.read() << 8;
int h = in.read();
w |= in.read() << 8;
// read palette
int palCount = in.read();
int pal[] = new int[palCount];
for (int i = 0; i < palCount; i++) {
int alpha = in.read();
int red = in.read();
int green = in.read();
int blue = in.read();
pal[i] = blue | (green << 8) | (red << 16) | (alpha << 24);
}
// read pixels
int pixels[] = new int[w * h];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
pixels[x + y * w] = pal[in.read()];
}
}
// return new image
return Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(w, h, ColorModel.getRGBdefault(), pixels, 0, w)
);
} catch (IOException e) {
System.out.println("error while reading");
e.printStackTrace();
}
return null;
}
*/
}