Author Topic: Managing bmp files  (Read 3242 times)

alekmaul (OP)

  • *
  • Posts: 330
    • Portabledev
Managing bmp files
« on: November 28, 2009, 08:46:52 am »
Well, before having somewhere else to update dingoo os native kit, here is a small function to add to graphics.c to manage bmp files (8 / 16 and 24 bits). Of course, all format are converted to 16 bits to be ok with dingoo screen.

The bmp file is in memory (so you must use bin2h to convert it).

Quote
gfx_texture* gfx_tex_load_mem_bmp(unsigned char *ptrMemBmp) {
  unsigned char *temp = (unsigned char *) ptrMemBmp;
  BMPHeader0 *Bmpinfo0 = (BMPHeader0 *) (temp);
  BMP_Headers *Bmpinfo = (BMP_Headers*) (temp+14);

  unsigned char *gfx = temp+Bmpinfo0->ImageStart;
  unsigned short *gfx2 = (unsigned short*) (temp+Bmpinfo0->ImageStart);//66); // For 16 bit mode ...
  int r, g, b;  short tempx, tempy;
  short lx = Bmpinfo->Width;   short ly = Bmpinfo->Height;
  unsigned short Bits = Bmpinfo->BitsperPixel;

  gfx_texture* tempTexture = (gfx_texture*)malloc(sizeof(gfx_texture) + (lx * ly * 2));
   if(tempTexture == NULL) {
      return NULL;
   }
 
   tempTexture->address = (void*)((uintptr_t)tempTexture + sizeof(gfx_texture));
   tempTexture->width = lx;
   tempTexture->height = ly;
   uint16_t* tempTexPtr = tempTexture->address;

  int i = 0;
  if (Bits > 16) { // For 24 and 32 bits
    for (tempy = ly-1; tempy > -1; tempy--){
      for (tempx = 0; tempx < lx; tempx++){
        b = (gfx[ i ] );   i++;
        g = (gfx[ i ] );   i++;
        r = (gfx[ i ] );   i++;
        if (Bits == 32) i++; // We pass the alpha bit
        tempTexPtr[tempx + ((tempy) * lx)] = gfx_color_rgb(r, g, b);
      }
      while(i&3) i++; // Padding....
    }
  }
  if (Bits == 16) {
    for (tempy = ly-1; tempy > -1; tempy--){
      for (tempx = 0; tempx < lx; tempx++) {
        b =  *gfx2     ;
        g = (*gfx2>>5) ;
        r = (*gfx2>>10);      
        tempTexPtr[tempx + ((tempy) * lx)] = gfx_color_rgb(r, g, b);
        gfx2++; // Go to next pixel
      }
      int temp = (int) gfx2;
      while(temp&3) temp++; // Padding....
      gfx2 = (unsigned short *) temp;
    }
  }
  if (Bits == 8 ) {
    // look for palette data if present //
    unsigned char *colormap = temp+14+Bmpinfo->SizeofHeader;
    for (tempy = ly-1; tempy > -1; tempy--){
      for (tempx = 0; tempx < lx; tempx++){
        b = ((colormap[(gfx[ i ]<<2)+0]));
        g = ((colormap[(gfx[ i ]<<2)+1]));
        r = ((colormap[(gfx[ i ]<<2)+2]));
        tempTexPtr[tempx + ((tempy) * lx)] = gfx_color_rgb(r, g, b);
        i++;
      }
    }
  }

  return tempTexture;
}

and in graphics.h, you must add :
Quote
#ifdef __GNUC__
 #define __PACKED __attribute__ ((__packed__))
#else
 #define __PACKED
 #pragma pack(1)
#endif

typedef struct {
  unsigned char Id[2]; // ?
  __PACKED   unsigned int Length;
  __PACKED   unsigned short Nothing1, Nothing2; // ?
  __PACKED   unsigned int ImageStart; // Offset of start of image
} BMPHeader0;

typedef struct {
  __PACKED   unsigned int SizeofHeader; // 40
  __PACKED   unsigned int Width, Height;
  __PACKED   unsigned short Colorplanes; // Usually 1
  __PACKED   unsigned short BitsperPixel; //1, 2, 4, 8, 16, 24, 32
  __PACKED   unsigned int Compression;  // 0 for none, 1...
  __PACKED   unsigned int SizeofData; // Not reliable
  __PACKED   unsigned int WidthperMeter, HeightperMeter; // Don't care
  __PACKED   unsigned int NColors, ImportantColors; // Number of colors used, important colors ?
} BMP_Headers;


extern gfx_texture* gfx_tex_load_mem_bmp(unsigned char *ptrMemBmp);

Hope it can help someone ;)

Kalisiin

  • Guest
Re: Managing bmp files
« Reply #1 on: November 28, 2009, 09:02:16 am »
I'm sorta consused at to what this is actually FOR??

Would this be something that will, like, automatically convert any 8 or 24-bit .bmps into 16-bit usable by the Dingoo, or what?

alekmaul (OP)

  • *
  • Posts: 330
    • Portabledev
Re: Managing bmp files
« Reply #2 on: November 28, 2009, 09:21:52 am »
Hum Kalisiin ... As you can see, you are in the development part of the forum, so it's for developers, not for dingoo final users ...
And developers know what to do with that ;)

Harteex

  • * Administrator
  • Posts: 709
    • Harteex Productions
Re: Managing bmp files
« Reply #3 on: November 28, 2009, 03:30:19 pm »
Thanks :) This will be useful.

Kalisiin

  • Guest
Re: Managing bmp files
« Reply #4 on: November 28, 2009, 07:32:54 pm »
Hum Kalisiin ... As you can see, you are in the development part of the forum, so it's for developers, not for dingoo final users ...
And developers know what to do with that ;)

OK, my bad.  I wrote that at 4 in the morning...

jxv

  • *
  • Posts: 38
Re: Managing bmp files
« Reply #5 on: November 29, 2009, 02:01:18 am »
Thanks, alexmaul. I will definitely be using this as a part of my sprite loader. All I need/work on now is a png file loader for the sprite loader. Thanks again. It won't go unused.

bnolsen

  • *
  • Posts: 38
Re: Managing bmp files
« Reply #6 on: November 29, 2009, 03:16:22 am »
BMP is annoying in that it's stored bottom to top, opposite from normal screen coordinate convention.  Not to mention the forced scanline padding.

TGA is the simplest format that supports dingoo color.
PGM/PPM is the simplest portable format using 8bit channels (cross platform development)
XPM allows you to have an indexed file that can be compiled directly into the code.

 

Post a new topic
Post a new topic