Reading pattern from file and create a bmp image of that in C -


i want read text file using c language.here's file:-

enter image description here

you see there pattern in text content on file. 0 means nothing. 9 means black. there coloring scheme 0 9.

i have create bitmap image of , color according values in pattern. have adjust 0-256 color scheme. , final output shown below

enter image description here

now see pattern in content of text file opposite final output bitmap file (not necessary). darkness of colors in bitmap image according values in pattern of text content.

anyone tell me how achieve in c language.

i able create bmp file not according pattern in text file.

#include <stdio.h> #include <stdlib.h>  int main() {  char bitmap[1900];      // -- file header -- //      // bitmap signature     bitmap[0] = 0x42;     bitmap[1] = 0x4d;      // file size     bitmap[2] = 58; // 40 + 14 + 12     bitmap[3] = 0;     bitmap[4] = 0;     bitmap[5] = 0;     int i=0;     // reserved field (in hex. 00 00 00 00)     for(i = 6; < 10; i++) bitmap[i] = 0;      // offset of pixel data inside image     for(i = 10; < 14; i++) bitmap[i] = 0;      // -- bitmap header -- //      // header size     bitmap[14] = 40;     for(i = 15; < 18; i++) bitmap[i] = 0;      // width of image     bitmap[18] = 4;     for(i = 19; < 22; i++) bitmap[i] = 0;      // height of image     bitmap[22] = 1;     for(i = 23; < 26; i++) bitmap[i] = 0;      // reserved field     bitmap[26] = 1;     bitmap[27] = 0;      // number of bits per pixel     bitmap[28] = 24; // 3 byte     bitmap[29] = 0;      // compression method (no compression here)     for(i = 30; < 34; i++) bitmap[i] = 0;      // size of pixel data     bitmap[34] = 12; // 12 bits => 4 pixels     bitmap[35] = 0;     bitmap[36] = 0;     bitmap[37] = 0;      // horizontal resolution of image - pixels per meter (2835)     bitmap[38] = 0;     bitmap[39] = 0;     bitmap[40] = 0b00110000;     bitmap[41] = 0b10110001;      // vertical resolution of image - pixels per meter (2835)     bitmap[42] = 0;     bitmap[43] = 0;     bitmap[44] = 0b00110000;     bitmap[45] = 0b10110001;      // color pallette information     for(i = 46; < 50; i++) bitmap[i] = 0;      // number of important colors     for(i = 50; < 54; i++) bitmap[i] = 0;      // -- pixel data -- //     for(i = 54; < 66; i++) bitmap[i] = 255;       file *file;     file = fopen("bitmap.bmp", "w+");     for(i = 0; < 66; i++)     {         fputc(bitmap[i], file);     }     fclose(file);     return 0; } 

here working code. not check case of error (input file badly formatted, example). put commentaries, though code not difficult understand.

#include <stdlib.h> #include <stdint.h> #include <assert.h> #include <string.h> #include <stdio.h> #include <stdbool.h> #include <errno.h> #include <ctype.h>  #define bmp_assert(condition, message) \     { \         if (! (condition)) { \             fprintf(stderr, __file__ ":%d: %s: assertion `" #condition \                 "` failed.\n\t: %s\n", __line__, __func__, message); \             exit(exit_failure); \         } \     } while (false)  #define bmp_colormap_size   10  //////////////////////////////////////////////////////////////////////////////// // structs  typedef struct __attribute__((__packed__)) bmp_color  {     uint8_t             r,                         g,                         b;     uint8_t             :8; }                       bmp_color_t;  typedef struct __attribute__((__packed__)) bmp_header_infos {     uint32_t const      info_size;     uint32_t            width;     uint32_t            height;     uint16_t const      planes;     uint16_t const      bpp;     uint32_t const      compression;     uint32_t            img_size;     uint32_t const      horz_resolution;     uint32_t const      vert_resolution;     uint32_t const      n_colors;     uint32_t const      n_important_colors; }                       bmp_header_infos_t;  typedef struct __attribute__((__packed__)) bmp_header {     /* header */     char const          signature[2];     uint32_t            file_size;     uint32_t const      :32; // reserved     uint32_t const      img_offset;     /* infos */     bmp_header_infos_t  infos;     /* color map */     bmp_color_t         colormap[bmp_colormap_size]; }                       bmp_header_t;  typedef struct          bmp_file_datas {     size_t              width;     size_t              height;     /* bit map */     uint8_t             *bitmap; }                       bmp_file_datas_t;   //////////////////////////////////////////////////////////////////////////////// // functions  /* give header right magic values */ bmp_header_t * bmp_get_header(void) {     static bmp_header_t _header = {         {'b', 'm'},         0u,         sizeof(_header),         {   // struct info             sizeof(_header.infos),             0u, 0u, // width, height             1u,     // planes             8u,     // bpp             0u,     // no compression             0u,     // img size             0u, 0u, // resolution             bmp_colormap_size,             bmp_colormap_size,      // important colors         },         {{0u}}     };     bmp_header_t        *header = malloc(sizeof(_header));     size_t              i,                         color;      assert(header != null);     memcpy(header, &_header, sizeof(*header));     // setting scale of greys     (i = 0 ; < bmp_colormap_size ; ++i)     {         color = (i * 255) / bmp_colormap_size;         header->colormap[i] = (bmp_color_t){color, color, color};     }     return header; }  /* take file content , store in buffer */ char * get_file_content(char const *filename) {     file        *file_handler = fopen(filename, "r");     size_t      file_len;     char        *buff;      bmp_assert(file_handler != null, strerror(errno));     fseek(file_handler, 0, seek_end);     file_len = ftell(file_handler);     fseek(file_handler, 0, seek_set);     buff = malloc(file_len + 1);     assert(buff != null);     fread(buff, file_len, 1, file_handler);     buff[file_len] = '\0';     fclose(file_handler);     return buff; }  /* greatest multiple of 4 >= size */ static inline size_t multiple_of_four(size_t size) {     while (size % 4)         ++size;     return size; }  /* informations buffer: size of line, number of lines */ void get_file_infos(char *buff, bmp_header_t *header, bmp_file_datas_t *datas) {     /* width & height */     header->infos.width =  strchr(buff, '\n') - buff;     header->infos.height = strlen(buff) / (header->infos.width + 1);                                             // + 1 terminating '\n'     datas->width = multiple_of_four(header->infos.width);     datas->height = header->infos.height; printf("file size: %u, %u\n", header->infos.width, header->infos.height);     /* image size & bitmap allocation */     header->infos.img_size = datas->width * datas->height;     datas->bitmap = malloc(header->infos.img_size * sizeof(*datas->bitmap));     assert(datas->bitmap != null);     header->file_size = header->img_offset + header->infos.img_size; }  /* take informations buffer , store them in bitmap */ void write_bitmap(char *buff, bmp_file_datas_t *datas) {     size_t          ibuff,                     iline = 0,                     ibitmap = 0;      (ibuff = 0 ; buff[ibuff] ; ++ibuff)     {         if (isdigit(buff[ibuff]))         {             datas->bitmap[ibitmap] = bmp_colormap_size - 1 - (buff[ibuff] - '0');             ++ibitmap;         }         else if (buff[ibuff] == '\n')         {             ++iline;             ibitmap = iline * datas->width;         }     } }  /* write datas in file: header , bitmap */ void write_in_file(bmp_header_t *header,               bmp_file_datas_t *datas,               char const *filename) {     file        *file_handler = fopen(filename, "w");      bmp_assert(file_handler != null, strerror(errno));     fwrite(header, sizeof(*header), 1, file_handler);     fwrite(datas->bitmap, header->infos.img_size, 1, file_handler);     fclose(file_handler); }   int main(int argc, char **argv) {     char                *buff;     bmp_header_t        *header;     bmp_file_datas_t    datas;      if (argc != 3)     {         fprintf(stderr, "usage: %s <input filename> <output filename>\n",                                 argv[0]);         return exit_failure;     }     buff = get_file_content(argv[1]);     header = bmp_get_header();     get_file_infos(buff, header, &datas);     write_bitmap(buff, &datas);     write_in_file(header, &datas, argv[2]);     free(buff), free(header), free(datas.bitmap);     return exit_success; } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -