bmp to const unsigned short

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

Moderators: cheriff, TyRaNiD

Post Reply
frozon
Posts: 5
Joined: Mon May 16, 2005 7:22 am

bmp to const unsigned short

Post by frozon »

Hi, i'm currently developing a little shoot'm up for psp but i'm having trouble with the convertion of bmp into a const unsigned short like the picture in HELLOPSP.
Some can explain a way to do it plz?

I'll let you know when the first release of the game will be ready.
The title of the game is "Uchuu no koe". Look forward to it.
Tychom
Posts: 7
Joined: Sat May 14, 2005 10:22 pm

Post by Tychom »

I use this C++ code under win32:

Code: Select all

AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image
{
	FILE *File=NULL;									// File Handle

	if (!Filename)										// Make Sure A Filename Was Given
	{
		return NULL;									// If Not Return NULL
	}

	File=fopen(Filename,"r");							// Check To See If The File Exists

	if (File)											// Does The File Exist?
	{
		fclose(File);									// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;										// If Load Failed Return NULL
}

void printFile(const std::string & varName,const std::string & fileName)
{
	AUX_RGBImageRec * image = LoadBMP&#40;const_cast<char *>&#40;fileName.c_str&#40;&#41;&#41;&#41;;

	if &#40;image == 0&#41;
	&#123;
		std&#58;&#58;cout << "//Could not find " << fileName << std&#58;&#58;endl;
	&#125;
	else
	&#123;
		std&#58;&#58;cout << std&#58;&#58;dec;
		std&#58;&#58;cout << "//Image size&#58; &#40;" << image->sizeX << "," << image->sizeY << "&#41;" << std&#58;&#58;endl;
		std&#58;&#58;cout << std&#58;&#58;hex;
		std&#58;&#58;cout << "const unsigned short " << varName << "&#91;&#93; = &#123;\n";

		for &#40;int y = image->sizeY - 1; y >= 0; --y&#41;
		&#123;
			std&#58;&#58;cout << "\t";

			for &#40;int x = 0; x < &#40;image->sizeX * 3&#41;; x += 3&#41;
			&#123;
				std&#58;&#58;cout << "0x" << rgb2col&#40;
					&#40;unsigned short&#41;image->data&#91;&#40;y * image->sizeX * 3&#41; + x&#93;,
					&#40;unsigned short&#41;image->data&#91;&#40;y * image->sizeX * 3&#41; + x + 1&#93;,
					&#40;unsigned short&#41;image->data&#91;&#40;y * image->sizeX * 3&#41; + x + 2&#93;&#41;;

				if &#40;&#40;x + 3&#41; != &#40;image->sizeX * 3&#41; || &#40;&#40;y - 1&#41; != -1&#41;&#41;
					std&#58;&#58;cout << ",";
			&#125;

			std&#58;&#58;cout << "\n";
		&#125;

		std&#58;&#58;cout << "&#125;;\n";
	&#125;
&#125;
where LoadBMP is taken from the source code at Nehe.gamedev and AUX_RGBImageRec is part of gl\glaux.h / glaux.lib and rgb2col you've probably already come across.
MrSiir[S]
Posts: 32
Joined: Tue Sep 14, 2004 11:08 am

Post by MrSiir[S] »

Hi,

I'm try with this code:

Code: Select all

#include <stdio.h>
#include "glbmp.h" // http&#58;//chaoslizard.sourceforge.net/glbmp/

unsigned short rgb2col&#40;unsigned char r, unsigned char g, unsigned char b&#41;
&#123;
	return &#40;&#40;&#40;&#40;b>>3&#41; & 0x1F&#41;<<10&#41;+&#40;&#40;&#40;g>>3&#41; & 0x1F&#41;<<5&#41;+&#40;&#40;&#40;r>>3&#41; & 0x1F&#41;<<0&#41;+0x8000&#41;;
&#125;

int main&#40;&#41;
&#123;
	int x, y;
	glbmp_t bitmap;
	glbmp_LoadBitmap&#40;"test.bmp", 0, &bitmap&#41;;
   
	printf&#40;"//Image size&#58; &#40;%i ,%i&#41;\n", bitmap.width, bitmap.height&#41;;
	printf&#40;"const unsigned short image_test&#91;&#93; = &#123;\n"&#41;;
	
	for&#40;y=bitmap.width-1; y >= 0; --y&#41;
	&#123;
		printf&#40;"\t"&#41;;
		for &#40;x=0; x<&#40;bitmap.height * 3&#41;; x+=3&#41;
		&#123;	
			printf&#40;"0x%x", rgb2col&#40;&#40;unsigned short&#41;bitmap.rgb_data&#91;&#40;y * bitmap.width * 3&#41; + x&#93;,
								   &#40;unsigned short&#41;bitmap.rgb_data&#91;&#40;y * bitmap.width * 3&#41; + x + 1&#93;,
								   &#40;unsigned short&#41;bitmap.rgb_data&#91;&#40;y * bitmap.width * 3&#41; + x + 2&#93;&#41;&#41;;
			
			if&#40;&#40;x + 3&#41; != &#40;bitmap.width * 3&#41; || &#40;&#40;y - 1&#41; != -1&#41;&#41;
			&#123;
				printf&#40;","&#41;;
			&#125;
		&#125;
		printf&#40;"\n"&#41;;
	&#125;
	printf&#40;"&#125;;\n\n"&#41;;
	glbmp_FreeBitmap&#40;&bitmap&#41;;
	
	return 0;
&#125;
But don't work, apparently the array is correct but when attempt to display the image in the screen leaves to me very badly, like deformed, i display the image with:

Code: Select all

pgBitBlt&#40;160, 40, 96, 21, 2, image_test&#41;;
some idea so that it leaves to me bad?

P.D.: Sorry for my poor english
Orion_
Posts: 69
Joined: Thu Jan 27, 2005 8:47 am

Post by Orion_ »

I would say the unsigned short cast ?
Grover
Posts: 50
Joined: Wed Feb 23, 2005 3:13 am

Post by Grover »

This is how I mage ConvImage.. CImage in the atl lib supports a heap of formats.. so you dont have to worry about deciphering the image type :-)

Flips the 32bit ARGB to 16 bit BGR

Code: Select all

#include <stdio.h>
#include <math.h>

#include <windows.h>
#include <atlimage.h>

#include <string>

int main&#40;int argc, char **argv&#41;
&#123;

	if&#40;&#40;argc != 3&#41; || &#40;argv&#91;1&#93; == 0&#41;&#41;
	&#123;
		printf&#40;"Invalid params&#58; ConvImage16 <image filename> <image output name>\n"&#41;;
		return 1;
	&#125;

	CImage		image;
	image.Load&#40;argv&#91;1&#93;&#41;;
	std&#58;&#58;string		OutName = argv&#91;2&#93;;
	std&#58;&#58;string		Name = OutName;
	OutName += ".c";

	FILE *out = fopen&#40;OutName.c_str&#40;&#41;, "w"&#41;;
	fprintf&#40;out, "// ********************************************************\n"&#41;;
	fprintf&#40;out, "// auto generated with ConvImage.\n"&#41;;
	fprintf&#40;out, "// \n"&#41;;
	fprintf&#40;out, "// ********************************************************\n"&#41;;

	fprintf&#40;out, "\n"&#41;;
	fprintf&#40;out, "const unsigned short %sData&#91;&#93; = \n", Name.c_str&#40;&#41;&#41;;
	fprintf&#40;out, "&#123;\n\t"&#41;;
	for&#40;int i=0; i<image.GetHeight&#40;&#41;; i++&#41;
	&#123;
		for&#40;int j=0; j<image.GetWidth&#40;&#41;; j++&#41;
		&#123;
			if&#40;j % 40 == 39&#41;
				fprintf&#40;out, "\n\t"&#41;;
			COLORREF col = image.GetPixel&#40;j,i&#41;;
			unsigned short col16 = &#40;&#40;col>>3&#41; & 0x1f&#41;;
			col16 |= &#40;&#40;col>>11&#41; & 0x1f&#41; << 5;
			col16 |= &#40;&#40;col>>19&#41; & 0x1f&#41; << 10;
			fprintf&#40;out, " 0x%x,", col16&#41;;
		&#125;
	&#125;

	fprintf&#40;out, "\n&#125;;\n\n"&#41;;
	fprintf&#40;out, "// ********************************************************\n"&#41;;

	fclose&#40;out&#41;;
	return 0;
&#125;
Hope this helps..
Cheers..
Bye.
MrSiir[S]
Posts: 32
Joined: Tue Sep 14, 2004 11:08 am

Post by MrSiir[S] »

Grover wrote:This is how I mage ConvImage.. CImage in the atl lib supports a heap of formats.. so you dont have to worry about deciphering the image type :-)

Flips the 32bit ARGB to 16 bit BGR

Code: Select all

...
Hope this helps..
Cheers..
Works fine, but only in Windows, how to translate to Linux?
Thks!
th0mas
Posts: 43
Joined: Sun Apr 24, 2005 1:59 am
Location: Canada
Contact:

Post by th0mas »

I'd suggest using SDL_Image and then accessing surface->pixels to write out the data. I'll put something together this weekend for linux users.
MrSiir[S]
Posts: 32
Joined: Tue Sep 14, 2004 11:08 am

Post by MrSiir[S] »

Ok, i have SDL version working for Linux.

You need, SDL, SDL_image and this is the code:

Code: Select all

#include <stdio.h>
#include "SDL.h"
#include "SDL_image.h"

Uint32 getpixel&#40;SDL_Surface *surface, int x, int y&#41;
&#123;
    int bpp = surface->format->BytesPerPixel;
    Uint8 *p = &#40;Uint8 *&#41;surface->pixels + y * surface->pitch + x * bpp;

    switch&#40;bpp&#41;
	&#123;
		case 1&#58;
			return *p;
		case 2&#58;
			return *&#40;Uint16 *&#41;p;
		case 3&#58;
			if&#40;SDL_BYTEORDER == SDL_BIG_ENDIAN&#41;
				return p&#91;0&#93; << 16 | p&#91;1&#93; << 8 | p&#91;2&#93;;
			else
				return p&#91;0&#93; | p&#91;1&#93; << 8 | p&#91;2&#93; << 16;
		case 4&#58;
			return *&#40;Uint32 *&#41;p;
		default&#58;
			return 0;
    &#125;
&#125;

int main&#40;int argc, char *argv&#91;&#93;&#41;
&#123;
	int x = 0;
	int y = 0;
	FILE *out;
	SDL_Surface *image;
	unsigned short col16;
	unsigned long col;
	char *OutName;

	if&#40;&#40;argc != 3&#41; || &#40;argv&#91;1&#93; == 0&#41;&#41;
	&#123;
		printf&#40;"Invalid params&#58; ConvImage16SDL <image filename> <image output name>\n"&#41;;
		return 1;
	&#125;

	image = IMG_Load&#40;argv&#91;1&#93;&#41;;
	if&#40;image == NULL&#41;
	&#123;
		printf&#40;"Couldn't load %s\n", argv&#91;1&#93;&#41;;
		return 1;
	&#125;

	sprintf&#40;OutName, "%s%s", argv&#91;2&#93;, ".c"&#41;;

	out = fopen&#40;OutName, "w"&#41;;
	fprintf&#40;out, "// ********************************************************\n"&#41;;
	fprintf&#40;out, "// auto generated with ConvImage &#40;SDL / Linux&#41;.\n"&#41;;
	fprintf&#40;out, "// \n"&#41;;
	fprintf&#40;out, "// ********************************************************\n"&#41;;
	fprintf&#40;out, "\n"&#41;;
	fprintf&#40;out, "const unsigned short %sData&#91;&#93; = \n", argv&#91;2&#93;&#41;;
	fprintf&#40;out, "&#123;\n\t"&#41;;
	
	for&#40;y=0; y<image->h; y++&#41;
	&#123;
		for&#40;x=0; x<image->w; x++&#41;
		&#123;
			if&#40;x % 40 == 39&#41;
			&#123;
				fprintf&#40;out, "\n\t"&#41;;
			&#125;
			col = getpixel&#40;image, x, y&#41;;
			col16 = &#40;&#40;col>>3&#41; & 0x1f&#41;;
			col16 |= &#40;&#40;col>>11&#41; & 0x1f&#41; << 5;
			col16 |= &#40;&#40;col>>19&#41; & 0x1f&#41; << 10;
			fprintf&#40;out, " 0x%x,", col16&#41;;
		&#125;
	&#125;

	fprintf&#40;out, "\n&#125;;\n\n"&#41;;
	fprintf&#40;out, "// ********************************************************\n"&#41;;

	SDL_FreeSurface&#40;image&#41;;
	SDL_Quit&#40;&#41;;
	
	return&#40;0&#41;;
&#125;
I compile with:

Code: Select all

gcc -g -O2 -I/usr/local/include/SDL -o ConvImage16SDL ConvImage16SDL.c -lSDL -lSDL_image
And use:

Code: Select all

ConvImage16SDL test.png test
Thanks th0mas!

P.D.: Sorry for my poor english
Post Reply