Advertisement
4da

reading bmp depth

4da
Aug 16th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. typedef struct                       /**** BMP file header structure ****/
  6. {
  7.     unsigned short bfType;           /* Magic number for file */
  8.     unsigned int   bfSize;           /* Size of file */
  9.     unsigned short bfReserved1;      /* Reserved */
  10.     unsigned short bfReserved2;      /* ... */
  11.     unsigned int   bfOffBits;        /* Offset to bitmap data */
  12. } BITMAPFILEHEADER;
  13.  
  14. #  define BF_TYPE 0x4D42             /* "MB" */
  15.  
  16. typedef struct                       /**** BMP file info structure ****/
  17. {
  18.     unsigned int   biSize;           /* Size of info header */
  19.     int            biWidth;          /* Width of image */
  20.     int            biHeight;         /* Height of image */
  21.     unsigned short biPlanes;         /* Number of color planes */
  22.     unsigned short biBitCount;       /* Number of bits per pixel */
  23.     unsigned int   biCompression;    /* Type of compression to use */
  24.     unsigned int   biSizeImage;      /* Size of image data */
  25.     int            biXPelsPerMeter;  /* X pixels per meter */
  26.     int            biYPelsPerMeter;  /* Y pixels per meter */
  27.     unsigned int   biClrUsed;        /* Number of colors used */
  28.     unsigned int   biClrImportant;   /* Number of important colors */
  29. } BITMAPINFOHEADER;
  30.  
  31. /*
  32.  * Constants for the biCompression field...
  33.  */
  34.  
  35. #  define BI_RGB       0             /* No compression - straight BGR data */
  36. #  define BI_RLE8      1             /* 8-bit run-length compression */
  37. #  define BI_RLE4      2             /* 4-bit run-length compression */
  38. #  define BI_BITFIELDS 3             /* RGB bitmap with RGB masks */
  39.  
  40. typedef struct                       /**** Colormap entry structure ****/
  41. {
  42.     unsigned char  rgbBlue;          /* Blue value */
  43.     unsigned char  rgbGreen;         /* Green value */
  44.     unsigned char  rgbRed;           /* Red value */
  45.     unsigned char  rgbReserved;      /* Reserved */
  46. } RGBQUAD;
  47.  
  48. typedef struct                       /**** Bitmap information structure ****/
  49. {
  50.     BITMAPINFOHEADER bmiHeader;      /* Image header */
  51.     RGBQUAD          bmiColors[256]; /* Image colormap */
  52. } BITMAPINFO;
  53.  
  54. char *                          /* O - Bitmap data */
  55. LoadDIBitmap(const char *filename, /* I - File to load */
  56.              BITMAPINFO **info)    /* O - Bitmap information */
  57. {
  58.     FILE             *fp;          /* Open file pointer */
  59.     char          *bits;        /* Bitmap pixel bits */
  60.     int              bitsize;      /* Size of bitmap */
  61.     int              infosize;     /* Size of header information */
  62.     BITMAPFILEHEADER header;       /* File header */
  63.  
  64.  
  65.     /* Try opening the file; use "rb" mode to read this *binary* file. */
  66.     if ((fp = fopen(filename, "rb")) == NULL)
  67.         return (NULL);
  68.  
  69.     /* Read the file header and any following bitmap information... */
  70.     if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
  71.     {
  72.         /* Couldn't read the file header - return NULL... */
  73.         fclose(fp);
  74.         return (NULL);
  75.     }
  76.  
  77.     if (header.bfType != 'MB')  /* Check for BM reversed... */
  78.     {
  79.         /* Not a bitmap file - return NULL... */
  80.         fclose(fp);
  81.         return (NULL);
  82.     }
  83.  
  84.     infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
  85.     if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
  86.     {
  87.         /* Couldn't allocate memory for bitmap info - return NULL... */
  88.         fclose(fp);
  89.         return (NULL);
  90.     }
  91.  
  92.     if (fread(*info, 1, infosize, fp) < infosize)
  93.     {
  94.         /* Couldn't read the bitmap header - return NULL... */
  95.         free(*info);
  96.         fclose(fp);
  97.         return (NULL);
  98.     }
  99.  
  100.     /* Now that we have all the header info read in, allocate memory for *
  101.      * the bitmap and read *it* in...                                    */
  102.     if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
  103.         bitsize = ((*info)->bmiHeader.biWidth *
  104.                    (*info)->bmiHeader.biBitCount + 7) / 8 *
  105.             abs((*info)->bmiHeader.biHeight);
  106.  
  107.     if ((bits = (char *) malloc(bitsize)) == NULL)
  108.     {
  109.         /* Couldn't allocate memory - return NULL! */
  110.         free(*info);
  111.         fclose(fp);
  112.         return (NULL);
  113.     }
  114.  
  115.     if (fread(bits, 1, bitsize, fp) < bitsize)
  116.     {
  117.         /* Couldn't read bitmap - free memory and return NULL! */
  118.         free(*info);
  119.         free(bits);
  120.         fclose(fp);
  121.         return (NULL);
  122.     }
  123.  
  124.     /* OK, everything went fine - return the allocated bitmap... */
  125.     fclose(fp);
  126.     return (bits);
  127. }
  128.  
  129. int main()
  130. {
  131.     BITMAPINFO *bi;
  132.     char *bits;
  133.  
  134.     bits = LoadDIBitmap("file.bmp", &bi);
  135.  
  136.     int bitSize = bi->bmiHeader.biBitCount;
  137.    
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement