Structure of BMP file parsed in C language

Source: Internet
Author: User
Tags assert bmp image fread strlen

Windows GDI provides
Typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, * PBITMAPFILEHEADER;
The BITMAPINFO structure of the BMP information header is as follows:

Typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors [1];
} BITMAPINFO, FAR * LPBITMAPINFO, * PBITMAPINFO;

Typedef struct tagBITMAPCOREINFO {
BITMAPCOREHEADER bmciHeader;
RGBTRIPLE bmciColors [1];
} BITMAPCOREINFO, FAR * LPBITMAPCOREINFO, * PBITMAPCOREINFO;

# Include <pshpack2.h>
Typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, FAR * LPBITMAPFILEHEADER, * PBITMAPFILEHEADER;

One time, a friend gave a set of BMP text images. To convert BMP into a font, he needed to remove the header and BMP information of BMP and only take the data part and coexist as an array, based on the BMP structure, write the following small program, including file read/write and file search:

# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
# Include <windows. h>

BITMAPFILEHEADER file_head;
BITMAPINFO fileinfo;

// Convert the color image into black and white. Enter the source file name and the name of the transferred file.
# Define FONT_WIDTH_1 (28)
# Define FONT_WIDTH_2 (22)


Char * getfilename (char * filename)
{
Char strResult [128] = {0}; // Save the result
Char * temp, * ret;
Temp = filename;
Int nStrLen = strlen (filename); // The length of the original string.
For (int I = nStrLen; I> 0; I --) // reverse query, comparison between each character and the backslash
 {
If (filename [I] = '\') // if the current character is a backslash
  {
// Copy the data behind the first diagonal bar and remove .bmp
Strncpy (strResult, (char *) (temp + I + 1), nStrLen-i-1-4); break;
  }
Else
  {
// Copy the batch string to remove .bmp
If (I = 1)
   {
Strncpy (strResult, (char *) temp, nStrLen-4 );
Break;
   }
  }
 }
Ret = strResult;
Return (ret );
}
# If 1
Char * getfilepath (char * filename)
{
Char strResult [128] = {0}; // Save the result
Char * temp, * ret;
Temp = filename;
Int nStrLen = strlen (filename); // The length of the original string.
For (int I = nStrLen; I> 0; I --) // reverse query, comparison of each character and slash
 {
If (filename [I] = '\') // if the current character is a slash
  {
// Copy path, including the slash
Strncpy (strResult, (char *) temp, I + 1 );
Break;
  }
 
 }
Ret = strResult;
Return (ret );
}
# Endif
Int colorbmp 2bwbmp (char * f_in, char * f_out)
{
 
Int infileLen; // file length
Int n = 0; // n-byte counter
Unsigned char c, c_in; // C_in file byte, C conversion
FILE * fh_in;
FILE * fh_out;
 
Assert (f_in! = NULL) & (f_out! = NULL ));
 
Fh_in = fopen (f_in, "rb ");
If (NULL = fh_in)
 {
Printf ("open read file error !! ");
Return 1;
 }
 
Fseek (fh_in, 0, SEEK_END );
InfileLen = ftell (fh_in );
Fseek (fh_in, 0, SEEK_SET );
 
/* Read bmp file head, 14 bytes */
If (sizeof (file_head )! = Fread (& file_head, 1, sizeof (file_head), fh_in ))
 {
Printf ("read bmp file error !! ");
Fclose (fh_in );
Return 1;
 }
/* Determine if it is a BMP File */
If (file_head.bfType! = 0x4d42)
 {
Printf ("bmp file error !! ");
Fclose (fh_in );
Return 1;
 }
/* Move the file pointer to the beginning of the file */
Fseek (fh_in, 0, SEEK_SET );
 
 
Fh_out = fopen (f_out, "wb ");
If (NULL = fh_out)
 {
Printf ("open write file error !! ");
Return 1;
 }
/* Read the file header into the target file */
While (int) file_head.bfOffBits --)
 {
C_in = getc (fh_in );
C = c_in;
Putc (c, fh_out );
 }
/* Convert to black/white slices */
While (n <(infileLen-(int) file_head.bfOffBits ))
 {
C_in = getc (fh_in );
C = c_in;
 
If (c> 0x7f)
  {
C = 0xff;
  }
Else
  {
C = 0x00;
  }
Putc (c, fh_out );
 
N ++;
 
 }
Fclose (fh_in );
Fclose (fh_out );
Return 0;
}
// Convert byte to BIT
Void ByteToBit (char * Out, const char * In, unsigned char bits)
{
Unsigned char I;

 

For (I = 0; I <bits; I ++)
    {
Out [I] = (In [I/8]> (I % 8) & 1;
    }
}
// Convert BIT to byte
Void BitToByte (char * Out, const char * In, unsigned bits)
{
Unsigned char I;

Memset (Out, 0, (bits + 7)/8 );
For (I = 0; I <bits; I ++)
    {
Out [I/8] | = In [I] <(I % 8 );
    }
}

// Convert the bitmap into a file and input the bmp file name and header file name.
Int BMP 2headfile (char * BMP file, char * headfile)
{
Int infileLen; // file length
Int n = 0, num = 1; // n bytes counter, NUM line feed indication
Unsigned char c, c_in; // C_in file byte, C conversion
FILE * fh_in;
FILE * fh_out;
Char com [256] = {0 };
Char ch [2] = {0 };
Long wid, hig;
 
Assert (bmp file! = NULL) & (headfile! = NULL ));
 
Fh_in = fopen (bmp file, "rb ");
If (NULL = fh_in)
 {
Printf ("open read file error !! ");
Return 1;
 }
 
Fseek (fh_in, 0, SEEK_END );
InfileLen = ftell (fh_in );
Fseek (fh_in, 0, SEEK_SET );
 
/* Read the BMP file header */
If (sizeof (file_head )! = Fread (& file_head, 1, sizeof (file_head), fh_in ))
 {
Printf ("read bmp file error !! ");
Fclose (fh_in );
Return 1;
 }
/* Read BMP file information */
If (sizeof (fileinfo )! = Fread (& fileinfo, 1, sizeof (fileinfo), fh_in ))
 {
Printf ("read bmp file error !! ");
Fclose (fh_in );
Return 1;
 }
/* Determine whether the image is a BMP image */
If (file_head.bfType! = 0x4d42)
 {
Printf ("bmp file error !! ");
Fclose (fh_in );
Return 1;
 }
 
Fseek (fh_in, file_head.bfOffBits, SEEK_SET );
 
Fh_out = fopen (headfile, "AB ");
If (NULL = fh_out)
 {
Printf ("open write file error !! ");
Return 1;
 }
/* Write comments */
Memcpy (ch, getfilename (bmp file), sizeof (getfilename (bmp file )));
Wid = fileinfo. bmiHeader. biWidth;
Hig = fileinfo. bmiHeader. biHeight;
Sprintf (com, "/* The size is: % dX % d. The char is: % s. */", wid, hig, ch );
Fputs (com, fh_out );
Putc (0x0d, fh_out );
Putc ('N', fh_out );
/* Write data */
While (n <(infileLen-(int) file_head.bfOffBits ))
 {
Putc ('0', fh_out );
Putc ('X', fh_out );
C_in = getc (fh_in );
C = c_in;
 
C = (c> 4) & 0x0f; // Obtain the content of the four bits in height.
If (c <0x0a)
  {
& Nbsp; c ++ = 0x30; // Convert the symbol into a number.
  }
Else
  {
C + = 0x37; // Convert it to a to f
  }
Putc (c, fh_out );
C = c_in & 0x0f; // get the four lower BIT content
If (c <0x0a)
  {
C + = 0x30;
  }
Else
  {
C + = 0x37;
  }
Putc (c, fh_out );
Putc (',', fh_out );
N ++;
 
If (num ++ % (fileinfo. bmiHeader. biWidth/8 + 1) = 0)
  {
Putc (0x0d, fh_out );
Putc ('N', fh_out );
  }
 
 
 }
Putc (0x0d, fh_out );
Putc ('N', fh_out );
 
Fclose (fh_in );
Fclose (fh_out );
Return 0;
}

 

Int main (int argc, char * argv [])
{
WIN32_FIND_DATA fd;
 
If (argc> 2)
 {
Printf ("***************** bitmap HELP **************** n ");
Printf ("BMP tofile [drive:] [path] [filename]");
Return 1;
 }
Else if (argc = 2)
 {
If (strchr (argv [1], '.')! = NULL)
  {
BMP 2headfile (argv [1], (char *) "font. h ");
  }
Else // if (memcmp (argv [1], "? ", Sizeof (argv [1]) = 0)
  {
Printf ("***************** bitmap HELP **************** n ");
Printf ("BMP tofile [drive:] [path] [filename]");
Return 1;
  }
 }
Else if (argc = 1)
 {
HANDLE hd =: FindFirstFile (LPCTSTR) "*. bmp", & fd); // start searching
If (hd = INVALID_HANDLE_VALUE)
  {
Printf ("File Not Found ");
Return 0;
  }
BMP 2headfile (fd. cFileName, (char *) "font. h ");
While (FindNextFile (hd, & fd) // continue searching
  {
BMP 2headfile (fd. cFileName, (char *) "font. h ");
  }
FindClose (hd); // Close the search
 }
 
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.