Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- typedef struct /**** BMP file header structure ****/
- {
- unsigned short bfType; /* Magic number for file */
- unsigned int bfSize; /* Size of file */
- unsigned short bfReserved1; /* Reserved */
- unsigned short bfReserved2; /* ... */
- unsigned int bfOffBits; /* Offset to bitmap data */
- } BITMAPFILEHEADER;
- # define BF_TYPE 0x4D42 /* "MB" */
- typedef struct /**** BMP file info structure ****/
- {
- unsigned int biSize; /* Size of info header */
- int biWidth; /* Width of image */
- int biHeight; /* Height of image */
- unsigned short biPlanes; /* Number of color planes */
- unsigned short biBitCount; /* Number of bits per pixel */
- unsigned int biCompression; /* Type of compression to use */
- unsigned int biSizeImage; /* Size of image data */
- int biXPelsPerMeter; /* X pixels per meter */
- int biYPelsPerMeter; /* Y pixels per meter */
- unsigned int biClrUsed; /* Number of colors used */
- unsigned int biClrImportant; /* Number of important colors */
- } BITMAPINFOHEADER;
- /*
- * Constants for the biCompression field...
- */
- # define BI_RGB 0 /* No compression - straight BGR data */
- # define BI_RLE8 1 /* 8-bit run-length compression */
- # define BI_RLE4 2 /* 4-bit run-length compression */
- # define BI_BITFIELDS 3 /* RGB bitmap with RGB masks */
- typedef struct /**** Colormap entry structure ****/
- {
- unsigned char rgbBlue; /* Blue value */
- unsigned char rgbGreen; /* Green value */
- unsigned char rgbRed; /* Red value */
- unsigned char rgbReserved; /* Reserved */
- } RGBQUAD;
- typedef struct /**** Bitmap information structure ****/
- {
- BITMAPINFOHEADER bmiHeader; /* Image header */
- RGBQUAD bmiColors[256]; /* Image colormap */
- } BITMAPINFO;
- char * /* O - Bitmap data */
- LoadDIBitmap(const char *filename, /* I - File to load */
- BITMAPINFO **info) /* O - Bitmap information */
- {
- FILE *fp; /* Open file pointer */
- char *bits; /* Bitmap pixel bits */
- int bitsize; /* Size of bitmap */
- int infosize; /* Size of header information */
- BITMAPFILEHEADER header; /* File header */
- /* Try opening the file; use "rb" mode to read this *binary* file. */
- if ((fp = fopen(filename, "rb")) == NULL)
- return (NULL);
- /* Read the file header and any following bitmap information... */
- if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
- {
- /* Couldn't read the file header - return NULL... */
- fclose(fp);
- return (NULL);
- }
- if (header.bfType != 'MB') /* Check for BM reversed... */
- {
- /* Not a bitmap file - return NULL... */
- fclose(fp);
- return (NULL);
- }
- infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
- if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
- {
- /* Couldn't allocate memory for bitmap info - return NULL... */
- fclose(fp);
- return (NULL);
- }
- if (fread(*info, 1, infosize, fp) < infosize)
- {
- /* Couldn't read the bitmap header - return NULL... */
- free(*info);
- fclose(fp);
- return (NULL);
- }
- /* Now that we have all the header info read in, allocate memory for *
- * the bitmap and read *it* in... */
- if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
- bitsize = ((*info)->bmiHeader.biWidth *
- (*info)->bmiHeader.biBitCount + 7) / 8 *
- abs((*info)->bmiHeader.biHeight);
- if ((bits = (char *) malloc(bitsize)) == NULL)
- {
- /* Couldn't allocate memory - return NULL! */
- free(*info);
- fclose(fp);
- return (NULL);
- }
- if (fread(bits, 1, bitsize, fp) < bitsize)
- {
- /* Couldn't read bitmap - free memory and return NULL! */
- free(*info);
- free(bits);
- fclose(fp);
- return (NULL);
- }
- /* OK, everything went fine - return the allocated bitmap... */
- fclose(fp);
- return (bits);
- }
- int main()
- {
- BITMAPINFO *bi;
- char *bits;
- bits = LoadDIBitmap("file.bmp", &bi);
- int bitSize = bi->bmiHeader.biBitCount;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement