open-axiom repository from github
/*1Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.2All rights reserved.3Copyright (C) 2007-2010, Gabriel Dos Reis.4All rights reserved.56Redistribution and use in source and binary forms, with or without7modification, are permitted provided that the following conditions are8met:910- Redistributions of source code must retain the above copyright11notice, this list of conditions and the following disclaimer.1213- Redistributions in binary form must reproduce the above copyright14notice, this list of conditions and the following disclaimer in15the documentation and/or other materials provided with the16distribution.1718- Neither the name of The Numerical Algorithms Group Ltd. nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS23IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED24TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A25PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER26OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,27EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,28PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR29PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF30LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING31NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS32SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.33*/3435#include "openaxiom-c-macros.h"36#include "debug.h"37#include "halloc.h"38#include "sockio.h"39#include "hyper.h"4041static int read_hot(FILE * fd , char Line[] , int * x_hot , int * y_hot);42static int read_w_and_h(FILE * fd , unsigned int * width , unsigned int * height);4344#define MAXLINE 2564546/*47* This file was produced by J.M. Wiley with some help from the bitmap editor48* routine. It reads in a bitmap file, and calls XCreatePixmapFromBitmapData49* to transform it into a Pixmap. He did this because the routine50* XReadBitmapFile does not seeem to work to well (whatecer that means).51*/5253XImage *54HTReadBitmapFile(Display *display,int screen,char * filename,55int *width, int *height)56{57XImage *image;58FILE *fd;59char Line[256], Buff[256];60int num_chars;61char *ptr;62int rch;63int version;64int padding, chars_line, file_chars_line, file_chars;65int bytes;66int x_hot, y_hot;676869image = XCreateImage(display, DefaultVisual(display, screen), 1,70XYBitmap, 0, NULL, 0, 0, 8, 0);717273(image)->byte_order = LSBFirst; /* byte_order */74(image)->bitmap_unit = 8; /* bitmap-unit */75(image)->bitmap_bit_order = LSBFirst; /* bitmap-bit-order */7677if (!(fd = zzopen(filename, "r"))) {78fprintf(stderr, "ReadBitmapFile: File >%s< not found\n", filename);79exit(-1);80}8182/*83* Once it is open, lets get the width and height84*/8586if ((read_w_and_h(fd, (unsigned int *)width,(unsigned int *) height)) < 0) {87fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);88exit(-1);89}909192/*93* Now get the next line, and see if it is hot spots or bits94*/95if (fgets(Line, MAXLINE, fd) == NULL) {96fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);97exit(-1);98}99100/*101* Now check the first character to see if it is a # or an s102*/103104if (Line[0] == '#') {105if ((read_hot(fd, Line, &x_hot, &y_hot)) < 0) {106fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);107exit(-1);108}109}110111(image)->width = *width;112(image)->height = *height;113114/*115* figure out what version116*/117118if (sscanf(Line, "static short %s = {", Buff) == 1)119version = 10;120else if (sscanf(Line, "static unsigned char %s = {", Buff) == 1)121version = 11;122else if (sscanf(Line, "static char %s = {", Buff) == 1)123version = 11;124else {125fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);126exit(-1);127}128129padding = 0;130if ((*width % 16) && ((*width % 16) < 9) && (version == 10))131padding = 1;132133(image)->bytes_per_line = chars_line = (*width + 7) / 8;134file_chars_line = chars_line + padding;135136num_chars = chars_line * (*height);137file_chars = file_chars_line * (*height);138(image)->data = (char *) halloc((image)->bytes_per_line * (image)->height,139"Read Pixmap--Image data");140141/*142* Since we are just holding the first line of the declaration, we can143* just start reading from fd144*/145146if (version == 10)147for (bytes = 0, ptr = (image)->data; bytes < file_chars; (bytes += 2)) {148if (fscanf(fd, " 0x%x%*[,}]%*[ \n]", &rch) != 1) {149fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);150exit(-1);151}152*(ptr++) = rch & 0xff;153if (!padding || ((bytes + 2) % file_chars_line))154*(ptr++) = rch >> 8;155}156else157for (bytes = 0, ptr = (image)->data; bytes < file_chars; bytes++, ptr++) {158if (fscanf(fd, " 0x%x%*[,}]%*[ \n]", &rch) != 1) {159fprintf(stderr, "ReadBitmapFile: Bad file format in %s\n", filename);160exit(-1);161}162*ptr = rch;163}164165fclose(fd);166167return image;168}169170static int171read_hot(FILE *fd,char Line[],int *x_hot,int *y_hot)172{173char Buff[256];174175/*176* Works much the same as get width and height, just new variables177*/178179if (sscanf(Line, "#define %s %d", Buff, x_hot) != 2)180return -1;181182if (fgets(Line, MAXLINE, fd) == NULL)183return -1;184185if (sscanf(Line, "#define %s %d", Buff, y_hot) != 2)186return -1;187188if (fgets(Line, MAXLINE, fd) == NULL)189return -1;190return 1;191}192193static int194read_w_and_h(FILE *fd,unsigned int *width,unsigned int *height)195{196char Line[256], Buff[256];197198if (fgets(Line, MAXLINE, fd) == NULL)199return -1;200201/*202* Once we have the line, scan it for the width203*/204205if (sscanf(Line, "#define %s %d", Buff, width) != 2)206return -1;207208/*209* Hopefully we have the width, now get the height the same way210*/211212if (fgets(Line, MAXLINE, fd) == NULL)213return -1;214215216/*217* Once we have the line, scan it for the height218*/219220if (sscanf(Line, "#define %s %d", Buff, height) != 2)221return -1;222223return 1;224}225226227/* read a bitmap file into memory */228229ImageStruct *230insert_image_struct(char *filename)231{232int bm_width, bm_height;233XImage *im;234ImageStruct *image;235236if (*filename == ' ')237filename++;238if ((image = (ImageStruct *) hash_find(&gImageHashTable, filename)) == NULL) {239im = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename,240&bm_width, &bm_height);241242/*243* now add the image to the gImageHashTable244*/245246image = (ImageStruct *) halloc(sizeof(ImageStruct), "ImageStruct");247image->image.xi = im;248image->width = image->image.xi->width;249image->height = image->image.xi->height;250image->filename = (char *) halloc(sizeof(char) * strlen(filename) +1,251"insert_image--filename");252253/* strcpy(image->filename, filename); */254255sprintf(image->filename, "%s", filename);256hash_insert(&gImageHashTable,(char *) image, image->filename);257}258return image;259}260261262