/******************************************************************************* * bmp1.c * Microsoft Windows bitmap (BMP) file functions. * * 1.0 01-27-92 drt. First cut. * 1.1 08-25-92 drt. Renamed bmp_header members. * * Copyright (c)1992-94, David R. Tribble. */ /* includes */ #include #include #include #include #include #include #include "bmp.h" /* public variables */ unsigned int bmp_pow2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768U }; /* private variables */ static unsigned short h_id = BMP_HDR_MAGIC; /******************************************************************************* * bmp_open * Opens a BMP file for either reading, writing, or both. * * returns * Pointer to a malloc'd BMP_FILE struct referring to an newly opened * BMP file, or NULL on error. */ BMP_FILE * bmp_open(const char *name, int mode) { int err; BMP_FILE * fp; char * m; /* check args */ if (name == NULL || name[0] == '\0') { /* invalid file name */ errno = EINVAL; return NULL; } /* check mode */ if (mode & O_RDONLY) m = "rb"; else if (mode & O_WRONLY) m = "wb"; else if (mode & O_RDWR) m = "w+b"; else { /* invalid mode */ errno = EINVAL; return NULL; } /* allocate a file control struct */ fp = malloc(sizeof(BMP_FILE)); if (fp == NULL) { /* out of memory */ errno = ENOMEM; return NULL; } memset(fp, 0, sizeof(*fp)); /* open the file */ fp->fp = fopen(name, m); err = errno; if (fp->fp == NULL) { /* can't open file */ free(fp); errno = err; return NULL; } /* initialize file control info */ fp->h.id = h_id; fp->h.filesize = 0; fp->h.r[0] = 0; fp->h.r[1] = 0; fp->h.r[2] = 0; fp->h.r[3] = 0; fp->h.headersize = 0; fp->h.infoSize = 0; fp->h.width = 0; fp->h.depth = 0; fp->h.biPlanes = 0; fp->h.bits = 0; fp->h.biCompression = BMP_CMP_NONE; fp->h.biSizeImage = 0; fp->h.biXPelsPerMeter = 0; fp->h.biYPelsPerMeter = 0; fp->h.biClrUsed = 0; fp->h.biClrImportant = 0; fp->map = NULL; fp->mode = mode; fp->flags = 0; fp->ncols = 0; fp->rep = 0; fp->pix = 0; fp->byteno = 0; fp->bitno = 0; fp->byte = 0; return fp; } /******************************************************************************* * bmp_close * Close a previously opened BMP file. * * returns * Zero if successful, otherwise a errno. */ int bmp_close(BMP_FILE *fp) { int rc = 0; /* check args */ if (fp == NULL) { /* invalid file pointer */ errno = EINVAL; return NULL; } /* close file, if open */ if (fp->fp != NULL) fclose(fp->fp); /* deallocate file control struct */ fp->fp = NULL; free(fp); return rc; } /* end bmp1.c */