cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dacatluong
Newbie
Newbie
4,875 Views
Registered: ‎12-20-2007

display file.bmp on TFT Help me

Hi , i found code C about "parses a BMP file and  creates a filename.h file in a custom format " . But i don't understand how use it . Because I just use EDK , please you show how i can use this code for my project . I try to use this code for my project but after download --> run XMD -->dow executable.elf-->run --> ???????????? .
----------------------------------------------------------------------------------------------------------------------

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define BUFSIZE   (640*480*4)
#define TRUE 1
#define FALSE 2

/* Structure Definitions */

typedef struct {
   unsigned short int type;                 /* Magic identifier            */
   unsigned int size;                       /* File size in bytes          */
   unsigned short int reserved1, reserved2;
   unsigned int offset;                     /* Offset to image data, bytes */
} HEADER;

typedef struct {
   unsigned int size;               /* Header size in bytes      */
   int width,height;                /* Width and height of image */
   unsigned short int planes;       /* Number of colour planes   */
   unsigned short int bits;         /* Bits per pixel            */
   unsigned int compression;        /* Compression type          */
   unsigned int imagesize;          /* Image size in bytes       */
   int xresolution,yresolution;     /* Pixels per meter          */
   unsigned int ncolours;           /* Number of colours         */
   unsigned int importantcolours;   /* Important colours         */
} INFOHEADER;

/* Functions */

static unsigned short ReadUShort(FILE *fptr)
    {
    unsigned char b0, b1;

    b0 = getc(fptr);
    b1 = getc(fptr);

    return ((b1 << 8) | b0);
    }

static unsigned int  ReadUInt(FILE *fptr)
    {
    unsigned char b0, b1, b2, b3;

    b0 = getc(fptr);
    b1 = getc(fptr);
    b2 = getc(fptr);
    b3 = getc(fptr);

    return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);

    }

/* Main */

int main(int argc,char **argv)
{
   int i,j,count,align;
   int gotindex = FALSE;
   unsigned char r,g,b;
   HEADER header;
   INFOHEADER infoheader;
   FILE *outfile;
   FILE *infile;
   unsigned char filebuff[BUFSIZE];

   /* Check arguments */
   if (argc < 2) {
      fprintf(stderr,"Usage: %s <filename>\n\n",argv[0]);
      fprintf(stderr,"%s creates a filename.h file in the format:\n",argv[0]);
      fprintf(stderr,"width,\n");
      fprintf(stderr,"height,\n");
      fprintf(stderr,"0x00000000,\n");
      fprintf(stderr,"0x00000000,\n");
      fprintf(stderr,"image data...\n");
      exit(-1);
   }

   /* Open files */
   if ((infile = fopen(argv[1],"r")) == NULL) {
      fprintf(stderr,"Unable to open BMP file \"%s\"\n",argv[1]);
      exit(-1);
   }

   if ((outfile = fopen(strcat(argv[1],".h"),"w")) == NULL) {
      fprintf(stderr,"Unable to open output file \"xlogoc.h\"\n");
      exit(-1);
   }


   /* Read and check the header */
   header.type = ReadUShort(infile);
   fprintf(stderr,"ID is: %d, should be %d\n",header.type,'M'*256+'B');
   header.size = ReadUInt(infile);
   fprintf(stderr,"File size is %d bytes\n",header.size);
   header.reserved1 = ReadUShort(infile);
   header.reserved2 = ReadUShort(infile);
   header.offset = ReadUInt(infile);
   fprintf(stderr,"Offset to image data is %d bytes\n",header.offset);

   infoheader.size = ReadUInt(infile);
   infoheader.width = ReadUInt(infile);
   infoheader.height = ReadUInt(infile);

   infoheader.planes = ReadUShort(infile);
   infoheader.bits = ReadUShort(infile);

   infoheader.compression = ReadUInt(infile);
   infoheader.imagesize = ReadUInt(infile);
   infoheader.xresolution = ReadUInt(infile);
   infoheader.yresolution = ReadUInt(infile);
   infoheader.ncolours = ReadUInt(infile);
   infoheader.importantcolours = ReadUInt(infile);

   fprintf(stderr,"Image size = %d x %d\n",infoheader.width,infoheader.height);
   fprintf(stderr,"Number of colour planes is %d\n",infoheader.planes);
   fprintf(stderr,"Bits per pixel is %d\n",infoheader.bits);
   fprintf(stderr,"Compression type is %d\n",infoheader.compression);
   fprintf(stderr,"Number of colours is %d\n",infoheader.ncolours);
   fprintf(stderr,"Number of required colours is %d\n",
      infoheader.importantcolours);

   fread(filebuff,1,BUFSIZE,infile);

  fprintf(outfile, "unsigned long bitmap[] = { \r\n");
  fprintf(outfile, "0x%.8x, ",infoheader.width);
  fprintf(outfile, "0x%.8x, ",infoheader.height);
  fprintf(outfile, "0x00000000, ");
  fprintf(outfile, "0x00000000, ");

  align = (infoheader.width%4) * infoheader.height;
  if (infoheader.width%4 == 2) align -= 2;

   for (j=infoheader.height-1;j>=0;j--) {
      fprintf(outfile, "\r\n// Line %d",infoheader.height-j);
      for (i=0;i<infoheader.width;i++) {
        if (i%4 == 0) fprintf(outfile, "\r\n");
        fprintf(outfile,
                "0x%.2x%.2x%.2x%.2x, ",
                0x00,
                filebuff[(j*(infoheader.width)*3)+(i*3)+2+align],  //R
                filebuff[(j*(infoheader.width)*3)+(i*3)+1+align],  //G
                filebuff[(j*(infoheader.width)*3)+(i*3)+align]); //B
      } /* i */

          align -= infoheader.width % 4;

   } /* j */

   fprintf(outfile, "};\r\n\r\n");

   fclose(infile);
   fclose(outfile);
}
---------------------------------------------------------------------------------------------------------------        
                      
0 Kudos
Reply
0 Replies