1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*
bitmap.c
AUTHORS:
nuha@sr-research.com nj
based on code by Michael Sweet
PLATFORMS:
All.
*/
#include "PsychEyelink.h"
#include "bitmap.h"
#include <errno.h>
#ifdef WIN32
/*
* 'LoadDIBitmap()' - Load a DIB/BMP file from disk.
*
* Returns a pointer to the bitmap if successful, NULL otherwise...
*/
psych_uint8* LoadDIBitmap(const char *filename, BITMAPINFO **info) /* O - Bitmap information */
{
FILE *fp; // Open file pointer
psych_uint8 *bits; // Bitmap pixel bits
int bitsize; // Size of bitmap
int infosize; // Size of header information
BITMAPFILEHEADER header; // File header
if ((fp = fopen(filename, "rb")) == NULL){
printf("Bitmap Loader: can't open file\n");
return (NULL);
}
if (fread(&header, sizeof(BITMAPFILEHEADER), 1, fp) < 1)
{
printf("Bitmap Loader: No header information\n");
fclose(fp);
return (NULL);
}
if (header.bfType != 'MB') // Check for BM reversed...
{
printf("Bitmap Loader: not a bitmap file\n");
fclose(fp);
return (NULL);
}
infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL)
{
printf("Bitmap Loader: bitmapinfo memory allocation failed\n");
fclose(fp);
return (NULL);
}
if (fread(*info, 1, infosize, fp) < infosize)
{
printf("Bitmap Loader: couldn't read memory\n");
free(*info);
fclose(fp);
return (NULL);
}
if ((*info)->bmiHeader.biBitCount != 32 && (*info)->bmiHeader.biBitCount != 24){
printf("Bitmap loading error: Bitmap must be 24 bits or 32 bits\n");
free(*info);
fclose(fp);
return (NULL);
}
if ((*info)->bmiHeader.biCompression != 0){
printf("Bitmap Loading Error: Bitmap must not be compressed\n");
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);
else
bitsize = (*info)->bmiHeader.biSizeImage;
if ((bits = malloc(bitsize)) == NULL) {
// Couldn't allocate memory - return NULL!
printf("Bitmap Loader: Couldn't allocate memory for bitmap\n");
free(*info);
fclose(fp);
return (NULL);
}
if (fread(bits, 1, bitsize, fp) < bitsize) {
// Couldn't read bitmap - free memory and return NULL!
printf("Bitmap Loader: Couldn't read bitmap\n");
free(*info);
free(bits);
fclose(fp);
return (NULL);
}
// OK, everything went fine - return the allocated bitmap...
fclose(fp);
if (bits == NULL){
printf("Bitmap Loader: output is NULL\n");
free(*info);
return (NULL);
}
return (bits);
}
#else /* !WIN32 */
/*
* Functions for reading and writing 16- and 32-bit little-endian integers.
*/
static unsigned short read_word(FILE *fp);
static unsigned int read_dword(FILE *fp);
static int read_long(FILE *fp);
psych_uint8* LoadDIBitmap(const char *filename, BITMAPINFO **info)
{
int bitsize; // Size of bitmap
int infosize; // Size of header information
int index;
int newIndex = 0;
int r;
int c;
int pitch;
FILE *fp; // Open file pointer
psych_uint8 *bits; // Bitmap pixel bits
BITMAPFILEHEADER header; // File header
if ((fp = fopen(filename, "rb")) == NULL) {
printf("Bitmap Loader: can't open file\n");
return (NULL);
}
// Read the file header and any following bitmap information...
header.bfType = read_word(fp);
header.bfSize = read_dword(fp);
header.bfReserved1 = read_word(fp);
header.bfReserved2 = read_word(fp);
header.bfOffBits = read_dword(fp);
if (header.bfType != BF_TYPE) // Check for BM reversed...
{
// Not a bitmap file - return NULL...
fclose(fp);
printf("Bitmap Loader: not a bitmap file\n");
return (NULL);
}
infosize = header.bfOffBits - 18;
if ((*info = (BITMAPINFO *)malloc(sizeof (BITMAPINFO))) == NULL)
{
// Couldn't allocate memory for bitmap info - return NULL...
fclose(fp);
printf("Bitmap Loader: bitmapinfo memory allocation failed\n");
return (NULL);
}
(*info)->bmiHeader.biSize = read_dword(fp);
(*info)->bmiHeader.biWidth = read_long(fp);
(*info)->bmiHeader.biHeight = read_long(fp);
(*info)->bmiHeader.biPlanes = read_word(fp);
(*info)->bmiHeader.biBitCount = read_word(fp);
(*info)->bmiHeader.biCompression = read_dword(fp);
(*info)->bmiHeader.biSizeImage = read_dword(fp);
(*info)->bmiHeader.biXPelsPerMeter = read_long(fp);
(*info)->bmiHeader.biYPelsPerMeter = read_long(fp);
(*info)->bmiHeader.biClrUsed = read_dword(fp);
(*info)->bmiHeader.biClrImportant = read_dword(fp);
if ((*info)->bmiHeader.biBitCount != 32 && (*info)->bmiHeader.biBitCount != 24){
printf("Bitmap loading error: Bitmap must be either 24 or 32 bits");
free(*info);
fclose(fp);
return (NULL);
}
if ((*info)->bmiHeader.biCompression != 0){
printf("Bitmap Loading Error: Bitmap must not be compressed");
free(*info);
fclose(fp);
return (NULL);
}
if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
bitsize = ((*info)->bmiHeader.biWidth * (*info)->bmiHeader.biBitCount + 7) / 8 * abs((*info)->bmiHeader.biHeight);
else
bitsize = (*info)->bmiHeader.biSizeImage;
if ((bits = malloc(bitsize)) == NULL)
{
free(*info);
fclose(fp);
printf("Bitmap Loader: memory allocation failed\n");
return (NULL);
}
if (fread(bits, 1, bitsize, fp) < bitsize)
{
free(*info);
free(bits);
fclose(fp);
printf("Bitmap Loader: couldn't read memory\n");
return (NULL);
}
fclose(fp);
return (bits);
}
/*
* 'read_word()' - Read a 16-bit unsigned integer.
*/
static unsigned short read_word(FILE *fp) /* I - File to read from */
{
unsigned char b0, b1;
b0 = getc(fp);
b1 = getc(fp);
return ((b1 << 8) | b0);
}
/*
* 'read_dword()' - Read a 32-bit unsigned integer.
*/
static unsigned int read_dword(FILE *fp)
{
unsigned char b0, b1, b2, b3;
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
/*
* 'read_long()' - Read a 32-bit signed integer.
*/
static int read_long(FILE *fp) {
unsigned char b0, b1, b2, b3;
b0 = getc(fp);
b1 = getc(fp);
b2 = getc(fp);
b3 = getc(fp);
return ((int)(((((b3 << 8) | b2) << 8) | b1) << 8) | b0);
}
#endif /* WIN32 */
|