raSystem  1.0 bata
raIBMP.cpp
Go to the documentation of this file.
1 #include "..\include\raMain.h"
2 
3 namespace System
4 {
5  unsigned char* raIBMP::LoadDIBitmap(raString filename, BITMAPINFO **info)
6  {
7  FILE *fp; /* Open file pointer */
8  unsigned char* bits; /* Bitmap pixel bits */
9  int bitsize; /* Size of bitmap */
10  int infosize; /* Size of header information */
11  BITMAPFILEHEADER header; /* File header */
12 
13  /* Try opening the file; use "rb" mode to read this *binary* file. */
14  if ((fp = fopen(filename.c_str(), "rb")) == NULL)
15  return (NULL);
16 
17  /* Read the file header and any following bitmap information... */
18  if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
19  {
20  /* Couldn't read the file header - return NULL... */
21  fclose(fp);
22  return (NULL);
23  }
24 
25  if (header.bfType != 'MB') /* Check for BM reversed... */
26  {
27  /* Not a bitmap file - return NULL... */
28  fclose(fp);
29  return (NULL);
30  }
31 
32  infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
33  if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
34  {
35  /* Couldn't allocate memory for bitmap info - return NULL... */
36  fclose(fp);
37  return (NULL);
38  }
39 
40  if (fread(*info, 1, infosize, fp) < infosize)
41  {
42  /* Couldn't read the bitmap header - return NULL... */
43  free(*info);
44  fclose(fp);
45  return (NULL);
46  }
47 
48  /* Now that we have all the header info read in, allocate memory for *
49  * the bitmap and read *it* in... */
50  if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
51  bitsize = ((*info)->bmiHeader.biWidth *
52  (*info)->bmiHeader.biBitCount + 7) / 8 *
53  abs((*info)->bmiHeader.biHeight);
54 
55  if ((bits = (unsigned char*)malloc(bitsize)) == NULL)
56  {
57  /* Couldn't allocate memory - return NULL! */
58  free(*info);
59  fclose(fp);
60  return (NULL);
61  }
62 
63  if (fread(bits, 1, bitsize, fp) < bitsize)
64  {
65  /* Couldn't read bitmap - free memory and return NULL! */
66  free(*info);
67  free(bits);
68  fclose(fp);
69  return (NULL);
70  }
71 
72  /* OK, everything went fine - return the allocated bitmap... */
73  fclose(fp);
74  return (bits);
75  }
76  bool raIBMP::SaveDIBitmap(raString filename, BITMAPINFO *info, unsigned char* bits)
77  {
78  FILE *fp; /* Open file pointer */
79  int size, /* Size of file */
80  infosize, /* Size of bitmap info */
81  bitsize; /* Size of bitmap pixels */
82  BITMAPFILEHEADER header; /* File header */
83 
84  /* Try opening the file;use "wb" mode to write this *binary* file. */
85  if ((fp = fopen(filename.c_str(), "wb")) == NULL)
86  return false;
87 
88  /* Figure out the bitmap size */
89  if (info->bmiHeader.biSizeImage == 0)
90  bitsize = (info->bmiHeader.biWidth *
91  info->bmiHeader.biBitCount + 7) / 8 *
92  abs(info->bmiHeader.biHeight);
93  else
94  bitsize = info->bmiHeader.biSizeImage;
95 
96  /* Figure out the header size */
97  infosize = sizeof(BITMAPINFOHEADER);
98  switch (info->bmiHeader.biCompression)
99  {
100  case BI_BITFIELDS :
101  infosize += 12; /* Add 3 RGB doubleword masks */
102  if (info->bmiHeader.biClrUsed == 0)
103  break;
104  case BI_RGB :
105  if (info->bmiHeader.biBitCount > 8 &&
106  info->bmiHeader.biClrUsed == 0)
107  break;
108  case BI_RLE8 :
109  case BI_RLE4 :
110  if (info->bmiHeader.biClrUsed == 0)
111  infosize += (1 << info->bmiHeader.biBitCount) * 4;
112  else
113  infosize += info->bmiHeader.biClrUsed * 4;
114  break;
115  }
116 
117  size = sizeof(BITMAPFILEHEADER) + infosize + bitsize;
118 
119  /* Write the file header, bitmap information, and bitmap pixel data... */
120  header.bfType = 'MB'; /* Non-portable... sigh */
121  header.bfSize = size;
122  header.bfReserved1 = 0;
123  header.bfReserved2 = 0;
124  header.bfOffBits = sizeof(BITMAPFILEHEADER) + infosize;
125 
126  if (fwrite(&header, 1, sizeof(BITMAPFILEHEADER), fp) < sizeof(BITMAPFILEHEADER))
127  {
128  /* Couldn't write the file header - return... */
129  fclose(fp);
130  return false;
131  }
132 
133  if (fwrite(info, 1, infosize, fp) < infosize)
134  {
135  /* Couldn't write the bitmap header - return... */
136  fclose(fp);
137  return false;
138  }
139 
140  if (fwrite(bits, 1, bitsize, fp) < bitsize)
141  {
142  /* Couldn't write the bitmap - return... */
143  fclose(fp);
144  return false;
145  }
146 
147  /* OK, everything went fine - return... */
148  fclose(fp);
149  return true;
150  }
151 };
std::string raString
Definition: raMain.h:107