need some help for constructing a .bmp file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mvkml
    New Member
    • Jan 2007
    • 5

    need some help for constructing a .bmp file

    hi!
    I am working with bmp files for my project.I am having the pixel data (binary.. i.e;10001001001 ..)and i need to represent it as a .bmp file.I need to construct an image(.bmp) from this data using "C".can anybody tell me how to represent it as a .bmp file?I could read data from an existing .bmp file...but couldnt construct that type of file...I tried to add header to the data directly..but the constructed image is not the one which should be for the given data.should the input data be given in any other format..?ascii. .?or any thing?cant we directly read the binary data from a text file as character and write it in a .bmp file after adding header?
    hope somebody understands my problem and kindly gives me a reply...
    thanks in advance!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Are you saysing you want to read a text file an use the data to create an image (bitmap) of the text contained in it?

    To do this you will need a font definition to got from text to the image of the letters, i.e. an 'A' value 65 has a given pixel layout and you will need a way to look this up.

    Then you will need to write the header and the pixel data to a file.

    Comment

    • mvkml
      New Member
      • Jan 2007
      • 5

      #3
      thank u..for giving me a reply..
      yes i need to represent data in a text file as a .bmp image....
      are u saying that we need to give the data as ASCII values..?OR should we give in some other format?PLEASE give me a suggestion..

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        No you will read the data as ASCII values from the text file, you will need to use some sort of font definition to look up the ASCII value and then work out how to draw it on a bitmap. Having worked out what colour each pixel in the bitmap should be you can then save it.

        Comment

        • macklin01
          New Member
          • Aug 2005
          • 145

          #5
          Once you write the file headers of the image, you could overwrite the pixel data with the text. On a standard, 24 bit per pixel image, red, green, and blue are 1 byte each, i.e., 3 characters.

          If you decide to use C++ instead of C, you can use my open source / cross-platform EasyBMP C++ bitmap library. Using that, it's very easy.

          Code:
          #include "EasyBMP.h" 
          
          // ... 
          
          // assume that you have an array of N characters in a char array.
          
          // decide an appropriate image size. it must meet or exceed 
          // the data size.
          
          BMP Output;
          
          int NumberOfPixels = N / 3;
          if( 3*NumberOfPixels < N )
          { NumerOfPixels++; }
          
          int Width = (int) sqrt( NumerOfPixels ); 
          int Height = NumerOfPixels / Width;
          while( Width*Height < NumerOfPixels )
          { Height++; }
          
          Output.SetSize( Width, Height );
          
          // scroll through the image and place each character
          
          int i=0; 
          int j=0; 
          int n = 0;
          while( n < N )
          {
           Output(i,j)->Red = (ebmpBYTE) text[n];
           n++;
           i++; 
           if( i >= Output.TellWidth() )
           { j++; i=0; }
          
           if( n < N )
           { Output(i,j)->Green = (ebmpBYTE) text[n]; }
           n++;
           i++; 
           if( i >= Output.TellWidth() )
           { j++; i=0; }
          
           if( n < N )
           { Output(i,j)->Blue = (ebmpBYTE) text[n]; }
           n++;
           i++; 
           if( i >= Output.TellWidth() )
           { j++; i=0; }
          }
          
          Output.WriteToFile("blah.bmp" );
          -- Paul

          Comment

          • mvkml
            New Member
            • Jan 2007
            • 5

            #6
            thank you banfa and macklin..for your suggestions..
            i wil try in these lines...

            Comment

            • mvkml
              New Member
              • Jan 2007
              • 5

              #7
              [to macklin] thank u for u r source code...but i dont know c++..so i need to do it with c only..wil it not be possible with c?
              [to banfa] can u tell me what is that font defintion u hav asked me to look up for the ascii?i couldnt understand...

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by mvkml
                [to banfa] can u tell me what is that font defintion u hav asked me to look up for the ascii?i couldnt understand...
                No you need to create a font definition and then when you read a character using it to put the right pixels into the bitmap.

                Typically a simply font definition consists of a 2 dimensional array of bits in rows and colums for each character in the font where a bit is 1 where the foreground colour must be used and 0 where the background colour must be used.

                There is anouther way to do this, uou can

                Create a bitmap in memory
                Create a device context(DC) for the bitmap
                Load the text from the file
                Use Windows API calls to draw the text in any loaded font in windows onto the bitmap
                Save the bitmap as a BMP file.
                Destroy the DC
                Destroy the memory bitmap

                Comment

                • mvkml
                  New Member
                  • Jan 2007
                  • 5

                  #9
                  thank u very much..
                  i read the ascii values of da data from text file ...and added header..as you told before..now i could get da correct bmp.so i think i need not try da last method u hav said as i could get the bmp img with u r 2nd reply itself...thank u very much..for suuggesting me to read data as ascii...

                  Comment

                  Working...