• How to create bitmap in C ?

    BrittneTabor Member

    I need a bit help with creating bitmap file in C. Explanation or some references or a book which could help me about exact theme.

  • SapnaVishwas Member
    #include 
    #include 
    #include 
    #include 
    #define _height 600
    #define _width 800
    #define _bitsperpixel 24
    #define _planes 1
    #define _compression 0
    #define _pixelbytesize _height*_width*_bitsperpixel/8
    #define _filesize _pixelbytesize+sizeof(bitmap)
    #define _xpixelpermeter 0x130B //2835 , 72 DPI
    #define _ypixelpermeter 0x130B //2835 , 72 DPI
    #define pixel 0xFF
    #pragma pack(push,1)
    typedef struct{
        uint8_t signature[2];
        uint32_t filesize;
        uint32_t reserved;
        uint32_t fileoffset_to_pixelarray;
    } fileheader;
    typedef struct{
        uint32_t dibheadersize;
        uint32_t width;
        uint32_t height;
        uint16_t planes;
        uint16_t bitsperpixel;
        uint32_t compression;
        uint32_t imagesize;
        uint32_t ypixelpermeter;
        uint32_t xpixelpermeter;
        uint32_t numcolorspallette;
        uint32_t mostimpcolor;
    } bitmapinfoheader;
    typedef struct {
        fileheader fileheader;
        bitmapinfoheader bitmapinfoheader;
    } bitmap;
    #pragma pack(pop)
    
    int main (int argc , char *argv[]) {
        FILE *fp = fopen("test.bmp","wb");
        bitmap *pbitmap  = (bitmap*)calloc(1,sizeof(bitmap));
        uint8_t *pixelbuffer = (uint8_t*)malloc(_pixelbytesize);
        strcpy(pbitmap->fileheader.signature,"BM");
        pbitmap->fileheader.filesize = _filesize;
        pbitmap->fileheader.fileoffset_to_pixelarray = sizeof(bitmap);
        pbitmap->bitmapinfoheader.dibheadersize =sizeof(bitmapinfoheader);
        pbitmap->bitmapinfoheader.width = _width;
        pbitmap->bitmapinfoheader.height = _height;
        pbitmap->bitmapinfoheader.planes = _planes;
        pbitmap->bitmapinfoheader.bitsperpixel = _bitsperpixel;
        pbitmap->bitmapinfoheader.compression = _compression;
        pbitmap->bitmapinfoheader.imagesize = _pixelbytesize;
        pbitmap->bitmapinfoheader.ypixelpermeter = _ypixelpermeter ;
        pbitmap->bitmapinfoheader.xpixelpermeter = _xpixelpermeter ;
        pbitmap->bitmapinfoheader.numcolorspallette = 0;
        fwrite (pbitmap, 1, sizeof(bitmap),fp);
        memset(pixelbuffer,pixel,_pixelbytesize);
        fwrite(pixelbuffer,1,_pixelbytesize,fp);
        fclose(fp);
        free(pbitmap);
        free(pixelbuffer);
    }
    
  • Abhey Member

    Link:
    http://code.google.com/p/libbmp/

    DESCRIPTION:

    libbmp is a simple, cross-platform, open source (revised LGPL) C library designed for easily reading, writing, and modifying Windows bitmap (BMP) image files. The library is oriented towards the novice programmer with little formal experience, but it is sufficiently capable for anybody who desires to do I/O and pixel operations on uncompressed 1, 4, 8, 16, 24, and 32 bpp (bits per pixel) BMP files.

    libbmp is intended to be cross-platform on both little-endian (e.g., x86, x86-64) and big-endian (e.g., IBM PowerPC, Sun Sparc) architectures. So far, it has been tested on x86 with Linux (2.6.x kernels and gcc and MinGW/gcc) and Windows (XP Pro with msvc6).

    About BMP file format, please see also http://en.wikipedia.org/wiki/BMP_file_format.

    EXAMPLE:

    #include 
    #include 
    int
    main(int argc, char **argv)
    {
      bmpfile_t *bmp;
      int i, j;
      rgb_pixel_t pixel = {128, 64, 0, 0};
    
      if (argc < 5) {
        printf("Usage: %s filename width height depth.\n", argv[0]);
        return 1;
      }
    
      if ((bmp = bmp_create(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]))) == NULL) {
        printf("Invalid depth value: %s.\n", argv[4]);
        return 1;
      }
    
      for (i = 10, j = 10; j < atoi(argv[3]); ++i, ++j) {
        bmp_set_pixel(bmp, i, j, pixel);
        pixel.red++;
        pixel.green++;
        pixel.blue++;
        bmp_set_pixel(bmp, i + 1, j, pixel);
        bmp_set_pixel(bmp, i, j + 1, pixel);
      }
    
      bmp_save(bmp, argv[1]);
      bmp_destroy(bmp);
    
      return 0;
    }
    
  • ShikhaTan Member

    how much programming do you know?

    do you understand structs? do you understand pointers? file input output? strings? number variables?binary? hexadecimal? RGB colouring system?

    if you understand all of the above you have everything you need to be able to write to bitmap

    next all you need to see is how a bitmap file is structured (look into wikipedia) , understand how the header part looks like, add teh header part and then add all the pixels and colours you need and you can read and write to a file as you wish

    if you understand all of this you wont have any problems

    there really isn’t anything else to it

    a bmp has a header part which identifies its size, type details etc

    and the source code part which is obviously the pixels you intend to write to it to create whatever picture or graphic, really it can be learned in hours

Viewing 3 reply threads
  • You must be logged in to reply to this topic.