bmp to const unsigned short
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.
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.
I use this C++ code under win32:
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.
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(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";
}
}
Hi,
I'm try with this code:
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:
some idea so that it leaves to me bad?
P.D.: Sorry for my poor english
I'm try with this code:
Code: Select all
#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;
}
Code: Select all
pgBitBlt(160, 40, 96, 21, 2, image_test);
P.D.: Sorry for my poor english
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..
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(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;
}
Cheers..
Bye.
Works fine, but only in Windows, how to translate to Linux?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..Code: Select all
...
Cheers..
Thks!
Ok, i have SDL version working for Linux.
You need, SDL, SDL_image and this is the code:
I compile with:
And use:
Thanks th0mas!
P.D.: Sorry for my poor english
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(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);
}
Code: Select all
gcc -g -O2 -I/usr/local/include/SDL -o ConvImage16SDL ConvImage16SDL.c -lSDL -lSDL_image
Code: Select all
ConvImage16SDL test.png test
P.D.: Sorry for my poor english