Recommendations for a good BMP>C app?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Fabre
Posts: 18
Joined: Sun May 22, 2005 7:29 am

Recommendations for a good BMP>C app?

Post by Fabre »

Hi, I have programmed for the GP32 and PC (and a little on the GBA) in the past, but I feel that the GP32 is a dying system, so I am moving to the PSP. My first project will be a PSP version of my GP32 Sudoku game. Sudoku is an insanely addictive puzzle game (go to http://www.sudoku.com if you want to learn more about it)

Getting back on topic, I have set up my PSP development environment, and successfully built the HelloPSP example. I am now moving on to making my game. 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? (and yes, I have looked around already)

As a little gift for reading my post, here's a preview of my game :D

Image
pixel
Posts: 791
Joined: Fri Jan 30, 2004 11:43 pm

Post by pixel »

It's quite easy to build up your own using SDL for example, by using SDL_LoadBMP then parsing'n'writing down the created surface.
pixel: A mischievous magical spirit associated with screen displays. The computer industry has frequently borrowed from mythology. Witness the sprites in computer graphics, the demons in artificial intelligence and the trolls in the marketing department.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: Recommendations for a good BMP>C app?

Post by Shine »

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 &#40;int y = 0; y < h; y++&#41; &#123;
			for &#40;int x = 0; x < w; x++&#41; &#123;
				int pixel = pixels&#91;y * w + x&#93;;
				palUnique.add&#40;new Integer&#40;pixel&#41;&#41;;
				int alpha = &#40;pixel >> 24&#41; & 0xff;
				int red = &#40;pixel >> 16&#41; & 0xff;
				int green = &#40;pixel >> 8&#41; & 0xff;
				int blue = &#40;pixel&#41; & 0xff;
			&#125;
		&#125;
		Object pal&#91;&#93; = palUnique.toArray&#40;&#41;;
		if &#40;pal.length > 255&#41; &#123;
			System.out.println&#40;"internal error&#58; too many palette colors"&#41;;
			return;
		&#125;
		HashMap palLookup = new HashMap&#40;&#41;;
		for &#40;int i = 0; i < pal.length; i++&#41;
			palLookup.put&#40;pal&#91;i&#93;, new Integer&#40;i&#41;&#41;;

		// save as FGF &#40;Frank's Grafik Fileformat&#41;
		try &#123;
			// open zlib output stream
			FileOutputStream fileOut =
				new FileOutputStream&#40;filename.substring&#40;0, filename.length&#40;&#41; - 4&#41; + ".fgf"&#41;;
			DeflaterOutputStream out = new DeflaterOutputStream&#40;fileOut&#41;;

			// write dimension
			out.write&#40;w & 255&#41;;
			out.write&#40;&#40;w >> 8&#41; & 255&#41;;
			out.write&#40;h & 255&#41;;
			out.write&#40;&#40;h >> 8&#41; & 255&#41;;

			// write palette
			out.write&#40;pal.length&#41;;
			for &#40;int i = 0; i < pal.length; i++&#41; &#123;
				int pixel = &#40;&#40;Integer&#41; pal&#91;i&#93;&#41;.intValue&#40;&#41;;
				int alpha = &#40;pixel >> 24&#41; & 0xff;
				int red = &#40;pixel >> 16&#41; & 0xff;
				int green = &#40;pixel >> 8&#41; & 0xff;
				int blue = &#40;pixel&#41; & 0xff;
				out.write&#40;alpha&#41;;
				out.write&#40;red&#41;;
				out.write&#40;green&#41;;
				out.write&#40;blue&#41;;
			&#125;

			// write pixels
			for &#40;int y = 0; y < h; y++&#41; &#123;
				for &#40;int x = 0; x < w; x++&#41; &#123;
					int pixelIndex =
						&#40;&#40;Integer&#41; palLookup.get&#40;new Integer&#40;pixels&#91;y * w + x&#93;&#41;&#41;&#41;.intValue&#40;&#41;;
					out.write&#40;pixelIndex&#41;;
				&#125;
			&#125;
			out.close&#40;&#41;;
		&#125; catch &#40;IOException e&#41; &#123;
			System.out.println&#40;"error while writing"&#41;;
			e.printStackTrace&#40;&#41;;
		&#125;
		System.exit&#40;0&#41;;
	&#125;

	/*
	 * Use this method in Applets to load an image.
	 */
	/*
		private Image loadFGF&#40;String imageName&#41;
		&#123;
			try &#123;
				InputStream fileIn = &#40;new URL&#40;getDocumentBase&#40;&#41;, imageName&#41;&#41;.openStream&#40;&#41;;
				InflaterInputStream in = new InflaterInputStream&#40;fileIn&#41;;
				
				// read dimension
				int w = in.read&#40;&#41;;
				w |= in.read&#40;&#41; << 8;
				int h = in.read&#40;&#41;;
				w |= in.read&#40;&#41; << 8;
				
				// read palette
				int palCount = in.read&#40;&#41;;
				int pal&#91;&#93; = new int&#91;palCount&#93;;
				for &#40;int i = 0; i < palCount; i++&#41; &#123;
					int alpha = in.read&#40;&#41;;
					int red = in.read&#40;&#41;;
					int green = in.read&#40;&#41;;
					int blue = in.read&#40;&#41;;
					pal&#91;i&#93; = blue | &#40;green << 8&#41; | &#40;red << 16&#41; | &#40;alpha << 24&#41;;
				&#125;
				
				// read pixels
				int pixels&#91;&#93; = new int&#91;w * h&#93;;
				for &#40;int y = 0; y < h; y++&#41; &#123;
					for &#40;int x = 0; x < w; x++&#41; &#123;
						pixels&#91;x + y * w&#93; = pal&#91;in.read&#40;&#41;&#93;;
					&#125;
				&#125;
				
				// return new image
				return Toolkit.getDefaultToolkit&#40;&#41;.createImage&#40;
					new MemoryImageSource&#40;w, h, ColorModel.getRGBdefault&#40;&#41;, pixels, 0, w&#41;
				&#41;;
			&#125; catch &#40;IOException e&#41; &#123;
				System.out.println&#40;"error while reading"&#41;;
				e.printStackTrace&#40;&#41;;
			&#125;
			return null;
		&#125;
		*/
&#125;
User avatar
alonetrio
Posts: 34
Joined: Sun May 15, 2005 12:10 am
Contact:

Post by alonetrio »

or even more simple : http://www.psp.to/tools
this an online jpg -> C converter ;)

AloneTrio
Fabre
Posts: 18
Joined: Sun May 22, 2005 7:29 am

Post by Fabre »

I may write my own converter some time in the future, but for now, I prefer to just use an existing tool, and the http://www.psp.to/tools website has just what I want. Thanks to everyone who replied!
Post Reply