Convert bitmap ( bmp ) files to raw 16 bit data using C program in Linux

bmptoraw is utility to convert bitmap files to raw 16 bit data. This program assumes a 24 bit bitmap file with a 54 byte header, which is the most common form of bitmap. One can convert other image files to that format using GIMP or MS Paint.

 $ vim bmptoraw.c 
#include<unistd.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<sys/time.h>
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>

int main(int argc, char **argv) {

  volatile unsigned char *bmp;
  int fd;
  int i, j, offset;
  int length, page;
  char red, blue, green, char1, char2;
  
  assert(argc = 2);
  
  page = getpagesize();
  length = (((480*800*3 + 56)/page) + 1) * page;
  
  fd = open(argv[1], O_RDONLY);
  bmp = (unsigned char *)mmap(0, length,
    PROT_READ, MAP_SHARED, fd, 0);
  
  assert(bmp[0] == 'B');
  assert(bmp[1] == 'M');
  
  assert(bmp[0xa] == 54);
  
  for(i=0;i<480;i++) {
    for(j=0;j<800;j++) {
      offset = 2400 * (479 - i) + 54 + j * 3;
      blue = bmp[offset];
      green = bmp[offset + 1];
      red = bmp[offset + 2];
      char2 = red & 0xf8;
      char2 |= green >> 5;
      char1 = blue >> 3;
      char1 |= (green & 0xf8) << 3;
      putchar(char1);
      putchar(char2);
    }
  }
  
  munmap((unsigned int *)bmp, length);
  close(fd);
  return 0;
}
 $  gcc -Wall bmptoraw.c -o bmptoraw 
 $ ./bmptoraw pic.bmp > pic.raw 

pic.raw can then be used for a splashscreen.

 $ cat pic.raw > /dev/fb0 

Reference : ftp://ftp.embeddedarm.com/ts-arm-sbc/ts-7395-linux/samples/bmptoraw.c

Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply