 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
frozon
Joined: 16 May 2005 Posts: 5
|
Posted: Mon May 16, 2005 7:26 am Post subject: bmp to const unsigned short |
|
|
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. |
|
| Back to top |
|
 |
Tychom
Joined: 14 May 2005 Posts: 7
|
Posted: Mon May 16, 2005 8:05 am Post subject: |
|
|
I use this C++ code under win32:
| Code: |
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(const_cast<char *>(fileName.c_str()));
if (image == 0)
{
std::cout << "//Could not find " << fileName << std::endl;
}
else
{
std::cout << std::dec;
std::cout << "//Image size: (" << image->sizeX << "," << image->sizeY << ")" << std::endl;
std::cout << std::hex;
std::cout << "const unsigned short " << varName << "[] = {\n";
for (int y = image->sizeY - 1; y >= 0; --y)
{
std::cout << "\t";
for (int x = 0; x < (image->sizeX * 3); x += 3)
{
std::cout << "0x" << rgb2col(
(unsigned short)image->data[(y * image->sizeX * 3) + x],
(unsigned short)image->data[(y * image->sizeX * 3) + x + 1],
(unsigned short)image->data[(y * image->sizeX * 3) + x + 2]);
if ((x + 3) != (image->sizeX * 3) || ((y - 1) != -1))
std::cout << ",";
}
std::cout << "\n";
}
std::cout << "};\n";
}
}
|
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. |
|
| Back to top |
|
 |
MrSiir[S]
Joined: 14 Sep 2004 Posts: 32
|
Posted: Wed May 18, 2005 3:58 am Post subject: |
|
|
Hi,
I'm try with this code:
| Code: | #include <stdio.h>
#include "glbmp.h" // http://chaoslizard.sourceforge.net/glbmp/
unsigned short rgb2col(unsigned char r, unsigned char g, unsigned char b)
{
return ((((b>>3) & 0x1F)<<10)+(((g>>3) & 0x1F)<<5)+(((r>>3) & 0x1F)<<0)+0x8000);
}
int main()
{
int x, y;
glbmp_t bitmap;
glbmp_LoadBitmap("test.bmp", 0, &bitmap);
printf("//Image size: (%i ,%i)\n", bitmap.width, bitmap.height);
printf("const unsigned short image_test[] = {\n");
for(y=bitmap.width-1; y >= 0; --y)
{
printf("\t");
for (x=0; x<(bitmap.height * 3); x+=3)
{
printf("0x%x", rgb2col((unsigned short)bitmap.rgb_data[(y * bitmap.width * 3) + x],
(unsigned short)bitmap.rgb_data[(y * bitmap.width * 3) + x + 1],
(unsigned short)bitmap.rgb_data[(y * bitmap.width * 3) + x + 2]));
if((x + 3) != (bitmap.width * 3) || ((y - 1) != -1))
{
printf(",");
}
}
printf("\n");
}
printf("};\n\n");
glbmp_FreeBitmap(&bitmap);
return 0;
} |
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: | | pgBitBlt(160, 40, 96, 21, 2, image_test); |
some idea so that it leaves to me bad?
P.D.: Sorry for my poor english |
|
| Back to top |
|
 |
Orion_
Joined: 27 Jan 2005 Posts: 69
|
Posted: Wed May 18, 2005 4:00 am Post subject: |
|
|
| I would say the unsigned short cast ? |
|
| Back to top |
|
 |
Grover
Joined: 23 Feb 2005 Posts: 50
|
Posted: Wed May 18, 2005 4:25 am Post subject: |
|
|
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: |
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <atlimage.h>
#include <string>
int main(int argc, char **argv)
{
if((argc != 3) || (argv[1] == 0))
{
printf("Invalid params: ConvImage16 <image filename> <image output name>\n");
return 1;
}
CImage image;
image.Load(argv[1]);
std::string OutName = argv[2];
std::string Name = OutName;
OutName += ".c";
FILE *out = fopen(OutName.c_str(), "w");
fprintf(out, "// ********************************************************\n");
fprintf(out, "// auto generated with ConvImage.\n");
fprintf(out, "// \n");
fprintf(out, "// ********************************************************\n");
fprintf(out, "\n");
fprintf(out, "const unsigned short %sData[] = \n", Name.c_str());
fprintf(out, "{\n\t");
for(int i=0; i<image.GetHeight(); i++)
{
for(int j=0; j<image.GetWidth(); j++)
{
if(j % 40 == 39)
fprintf(out, "\n\t");
COLORREF col = image.GetPixel(j,i);
unsigned short col16 = ((col>>3) & 0x1f);
col16 |= ((col>>11) & 0x1f) << 5;
col16 |= ((col>>19) & 0x1f) << 10;
fprintf(out, " 0x%x,", col16);
}
}
fprintf(out, "\n};\n\n");
fprintf(out, "// ********************************************************\n");
fclose(out);
return 0;
}
|
Hope this helps..
Cheers.. _________________ Bye. |
|
| Back to top |
|
 |
MrSiir[S]
Joined: 14 Sep 2004 Posts: 32
|
Posted: Wed May 18, 2005 10:58 pm Post subject: |
|
|
| 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
Hope this helps..
Cheers.. |
Works fine, but only in Windows, how to translate to Linux?
Thks! |
|
| Back to top |
|
 |
th0mas
Joined: 24 Apr 2005 Posts: 43 Location: Canada
|
Posted: Wed May 18, 2005 11:18 pm Post subject: |
|
|
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. _________________ http://th0mas.xbox-scene.com |
|
| Back to top |
|
 |
MrSiir[S]
Joined: 14 Sep 2004 Posts: 32
|
Posted: Tue May 24, 2005 3:39 am Post subject: |
|
|
Ok, i have SDL version working for Linux.
You need, SDL, SDL_image and this is the code:
| Code: | #include <stdio.h>
#include "SDL.h"
#include "SDL_image.h"
Uint32 getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0;
}
}
int main(int argc, char *argv[])
{
int x = 0;
int y = 0;
FILE *out;
SDL_Surface *image;
unsigned short col16;
unsigned long col;
char *OutName;
if((argc != 3) || (argv[1] == 0))
{
printf("Invalid params: ConvImage16SDL <image filename> <image output name>\n");
return 1;
}
image = IMG_Load(argv[1]);
if(image == NULL)
{
printf("Couldn't load %s\n", argv[1]);
return 1;
}
sprintf(OutName, "%s%s", argv[2], ".c");
out = fopen(OutName, "w");
fprintf(out, "// ********************************************************\n");
fprintf(out, "// auto generated with ConvImage (SDL / Linux).\n");
fprintf(out, "// \n");
fprintf(out, "// ********************************************************\n");
fprintf(out, "\n");
fprintf(out, "const unsigned short %sData[] = \n", argv[2]);
fprintf(out, "{\n\t");
for(y=0; y<image->h; y++)
{
for(x=0; x<image->w; x++)
{
if(x % 40 == 39)
{
fprintf(out, "\n\t");
}
col = getpixel(image, x, y);
col16 = ((col>>3) & 0x1f);
col16 |= ((col>>11) & 0x1f) << 5;
col16 |= ((col>>19) & 0x1f) << 10;
fprintf(out, " 0x%x,", col16);
}
}
fprintf(out, "\n};\n\n");
fprintf(out, "// ********************************************************\n");
SDL_FreeSurface(image);
SDL_Quit();
return(0);
} |
I compile with:
| Code: | | gcc -g -O2 -I/usr/local/include/SDL -o ConvImage16SDL ConvImage16SDL.c -lSDL -lSDL_image |
And use:
| Code: | | ConvImage16SDL test.png test |
Thanks th0mas!
P.D.: Sorry for my poor english |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|