⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp.c

📁 在SOPC平台上
💻 C
字号:
#include "io.h"
#include "fat/fat_file.h"
#include "lcd_controller.h"
#include "graphics_lib/simple_graphics.h"
#include "bmp.h"

/**************************************************************************
 *  draw_bitmap                                                           *
 *    Draws a bitmap.                                                     *
 **************************************************************************/


void draw_bitmap(bitmap_struct *bmp,int x,int y, frame_buffer_struct* frame_buffer)
{
  int i, j;
  int bitmap_offset = 0;

  for(j=0; j < bmp->biHeight; j++)
  {
    for(i=0; i < bmp->biWidth; i++, bitmap_offset++)
    {
      vid_set_pixel(x+i, y+j, (bmp->data[bitmap_offset]), frame_buffer);
    }
  }
}

/**************************************************************************
 *  draw_transparent_bitmap                                               *
 *    Draws a transparent bitmap.  Transparent color is 0x0020            *
 **************************************************************************/

void draw_transparent_bitmap(bitmap_struct *bmp,int x,int y, frame_buffer_struct* frame_buffer)
{
  int i,j;
  short bitmap_offset = 0;
  short data;

  for(j=0; j < bmp->biHeight; j++)
  {
    for(i=0; i < bmp->biWidth; i++, bitmap_offset++)
    {
      data = bmp->data[bitmap_offset];
      if (data != 0x0020) 
      {
        vid_set_pixel(x+i, y+j, data, frame_buffer);
      }
    }
  }
}


/**************************************************************************
 *  fskip                                                                 *
 *     Skips bytes in a file.                                             *
 **************************************************************************/

void fskip(FILE *fp, int num_bytes)
{
   int i;
   for (i=0; i<num_bytes; i++)
      ide_getc(fp);
}

/**************************************************************************
 *  load_bmp                                                              *
 *    Loads a bitmap file into memory.                                    *
 **************************************************************************/

void load_bmp(char *file, bitmap_struct *b)
{
  int fp;
  long index;
  int x, byte_count, temp;
  short red, green, blue, color_index, color;
  int pixel;

  /* Make sure filename string is termintated */
  file[strlen(file)] = 0x0;

  /* open the file */
  if ((fp = ide_open(file, O_RDONLY)) < 0x0)
  {
    printf("Error opening file %s.\n",file);
    exit(1);
  }

  /* check to see if it is a valid bitmap file */
  if (ide_getc(fp)!='B' || ide_getc(fp)!='M')
  {
    ide_close(fp);
    printf("%s is not a bitmap file.\n",file);
    exit(1);
  }

  /* read in the width and height of the image, and the
     number of colors used; ignore the rest */
  ide_lseek( fp, 16, SEEK_CUR);
  ide_read( fp, &b->biWidth, sizeof(short) );
  ide_lseek( fp, 2, SEEK_CUR);
  ide_read( fp, &b->biHeight, sizeof(short) );
  ide_lseek( fp, 4, SEEK_CUR);
  ide_read( fp, &b->biBitCount, sizeof(short) );
  ide_lseek( fp, 24, SEEK_CUR);
  
  /* try to allocate memory for palette data */
  if (b->biBitCount != 24)
  {
    if ((b->bicolor_palatte = (char *) malloc((1 << (b->biBitCount))*3)) == NULL)
    {
      ide_close( fp );
      printf("Error allocating memory for file %s.\n",file);
      exit(1);
    }
  }

  /* try to allocate memory for bitmap data */
  if ((b->data = (short *) malloc((b->biWidth * b->biHeight) * 2)) == NULL)
  {
    ide_close( fp ) ;
    printf("Error allocating memory for file %s.\n",file);
    exit(1);
  }

  /* read the palette information */
  if (b->biBitCount != 24)
  {
    for(index=0;index<(1 << (b->biBitCount));index++)
    {
      b->bicolor_palatte[(int)(index*3+2)] = ide_getc(fp);
      b->bicolor_palatte[(int)(index*3+1)] = ide_getc(fp);
      b->bicolor_palatte[(int)(index*3+0)] = ide_getc(fp);
      x=ide_getc(fp);
    }
  }

  /* read an 8-bit bitmap */
  if (b->biBitCount == 8)
  {
    for(index=(b->biHeight - 1)* b->biWidth; index >= 0; index -= b->biWidth)
      for(x=0;x<b->biWidth;x++)
      {
        color_index = (int)ide_getc( fp );
        blue = (short)b->bicolor_palatte[(color_index * 3) + 2];
        green = (short)b->bicolor_palatte[(color_index * 3) + 1];
        red = (short)b->bicolor_palatte[(color_index * 3) + 0];
        color = vid_merge_colors(((unsigned short)red) >> 3, ((unsigned short)green) >> 3, ((unsigned short)blue) >> 3);
        b->data[index+x]=(short)color;
      }
  }

  /* read a 24-bit bitmap */
  if (b->biBitCount == 24)
  {
    for(index=(b->biHeight - 1)* b->biWidth; index >= 0; index -= b->biWidth)
    {
      for(x=0, byte_count=0; x < b->biWidth; x++, byte_count += 3)
      {
//        blue  = (short)ide_getc(fp);
//        green = (short)ide_getc(fp);
//        red = (short)ide_getc(fp);
        ide_read( fp, &pixel, 3 );
//        pixel = red | (green << 8) | (blue << 16);
        color = bmp_color_convert24_16(&pixel);
//        blue  = (short)ide_getc(fp);
//        green = (short)ide_getc(fp);
//        red = (short)ide_getc(fp);
//        color = vid_merge_colors(((unsigned short)red) >> 3, ((unsigned short)green) >> 3, ((unsigned short)blue) >> 3);
        b->data[index+x]=(short)color;
      }
      for(;byte_count%4 != 0;)
      {
        temp = (char)ide_getc(fp);
        byte_count++;
      }
    }
  }
  
  ide_close(fp);
}


/******************************************************************
*  Function: bmp_color_convert24_16
*
*  Purpose: Takes a pointer to a 24-bit (3-byte) 24-bit RGB color 
*           sample and converts it to 16-bits, displayable by the 
*           VGA controller in 16-bit mode. BMP files sequence colors
*           BGR
*
******************************************************************/
unsigned short bmp_color_convert24_16(char* color24)
{
  unsigned char red, green, blue;
  unsigned short output;
  
  red = *(color24 + 2) & 0xF8;
  green = *(color24 + 1) & 0xFC; // green is actualy 6 bits
  blue = *(color24 + 0) & 0xF8;

  output = ((blue >> 3) | (green << 3) | (red << 8));  
  return output;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -