GLuint TextureHandler::load(std::string name) { if ((names.find(name) == names.end()) && (commitQueue.find(name) == commitQueue.end())) { TextureImage img = loadppm(name); if (img.data) { // is not null commit(name, img); } } return use(name); }
int loadbmp(char *filename, unsigned char **buf, int *w, int *h, enum BMPPIXELFORMAT f, int align, int dstbottomup) { int fd=-1, bytesread, srcpitch, srcbottomup=1, srcps, dstpitch, retcode=0; unsigned char *tempbuf=NULL; bmphdr bh; int flags=O_RDONLY; dstbottomup=dstbottomup? 1:0; #ifdef _WIN32 flags|=O_BINARY; #endif if(!filename || !buf || !w || !h || f<0 || f>BMPPIXELFORMATS-1 || align<1) _throw("invalid argument to loadbmp()"); if((align&(align-1))!=0) _throw("Alignment must be a power of 2"); _unix(fd=open(filename, flags)); readme(fd, &bh.bfType, sizeof(unsigned short)); if(!littleendian()) bh.bfType=byteswap16(bh.bfType); if(bh.bfType==0x3650) { _catch(loadppm(&fd, buf, w, h, f, align, dstbottomup, 0)); goto finally; } if(bh.bfType==0x3350) { _catch(loadppm(&fd, buf, w, h, f, align, dstbottomup, 1)); goto finally; } readme(fd, &bh.bfSize, sizeof(unsigned int)); readme(fd, &bh.bfReserved1, sizeof(unsigned short)); readme(fd, &bh.bfReserved2, sizeof(unsigned short)); readme(fd, &bh.bfOffBits, sizeof(unsigned int)); readme(fd, &bh.biSize, sizeof(unsigned int)); readme(fd, &bh.biWidth, sizeof(int)); readme(fd, &bh.biHeight, sizeof(int)); readme(fd, &bh.biPlanes, sizeof(unsigned short)); readme(fd, &bh.biBitCount, sizeof(unsigned short)); readme(fd, &bh.biCompression, sizeof(unsigned int)); readme(fd, &bh.biSizeImage, sizeof(unsigned int)); readme(fd, &bh.biXPelsPerMeter, sizeof(int)); readme(fd, &bh.biYPelsPerMeter, sizeof(int)); readme(fd, &bh.biClrUsed, sizeof(unsigned int)); readme(fd, &bh.biClrImportant, sizeof(unsigned int)); if(!littleendian()) { bh.bfSize=byteswap(bh.bfSize); bh.bfOffBits=byteswap(bh.bfOffBits); bh.biSize=byteswap(bh.biSize); bh.biWidth=byteswap(bh.biWidth); bh.biHeight=byteswap(bh.biHeight); bh.biPlanes=byteswap16(bh.biPlanes); bh.biBitCount=byteswap16(bh.biBitCount); bh.biCompression=byteswap(bh.biCompression); bh.biSizeImage=byteswap(bh.biSizeImage); bh.biXPelsPerMeter=byteswap(bh.biXPelsPerMeter); bh.biYPelsPerMeter=byteswap(bh.biYPelsPerMeter); bh.biClrUsed=byteswap(bh.biClrUsed); bh.biClrImportant=byteswap(bh.biClrImportant); } if(bh.bfType!=0x4d42 || bh.bfOffBits<BMPHDRSIZE || bh.biWidth<1 || bh.biHeight==0) _throw("Corrupt bitmap header"); if((bh.biBitCount!=24 && bh.biBitCount!=32) || bh.biCompression!=BI_RGB) _throw("Only uncompessed RGB bitmaps are supported"); *w=bh.biWidth; *h=bh.biHeight; srcps=bh.biBitCount/8; if(*h<0) {*h=-(*h); srcbottomup=0;} srcpitch=(((*w)*srcps)+3)&(~3); dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1)); if(srcpitch*(*h)+bh.bfOffBits!=bh.bfSize) _throw("Corrupt bitmap header"); if((tempbuf=(unsigned char *)malloc(srcpitch*(*h)))==NULL || (*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL) _throw("Memory allocation error"); if(lseek(fd, (long)bh.bfOffBits, SEEK_SET)!=(long)bh.bfOffBits) _throw(strerror(errno)); _unix(bytesread=read(fd, tempbuf, srcpitch*(*h))); if(bytesread!=srcpitch*(*h)) _throw("Read error"); pixelconvert(tempbuf, BMP_BGR, srcpitch, *buf, f, dstpitch, *w, *h, srcbottomup!=dstbottomup); finally: if(tempbuf) free(tempbuf); if(fd!=-1) close(fd); return retcode; }
int main(int argc,char *argv[]) { #ifdef _XLIB_ char *name="rplot"; int xdf=0; int xd=0; struct XwinDisplay *dp; struct XwinWindow *win; char *display_name=NULL; int xdoff=-1; int ydoff=-1; #endif int s=0; int arg=0; FILE *fp; char lbuf[256]; struct FrameBuffer *img=NULL; struct FrameBufferRplot *fbplot=NULL; struct PostScriptRplot *psplot=NULL; struct PostScript *psdata=NULL; struct FrameBufferFontDB *fontdb=NULL; struct FrameBufferDB *imagedb=NULL; struct XMLdata *xmldata=NULL; struct RplotInPlot *rplotin=NULL; struct RplotRplot *rplotrplot=NULL; struct Rplot *rplot=NULL; unsigned char land=0; float xpoff=40,ypoff=40; char *fntdbfname=NULL; unsigned char psflg=0; unsigned char xmlflg=0; unsigned char ppmflg=0; unsigned char ppmxflg=0; unsigned char rflg=0; unsigned char gflg=0; unsigned char pflg=0; unsigned char help=0; unsigned char option=0; struct OptionText *ppmnme=NULL; struct OptionText *ppmxnme=NULL; struct OptionText *xmlnme=NULL; OptionAdd(&opt,"-help",'x',&help); OptionAdd(&opt,"-option",'x',&option); OptionAdd(&opt,"ps",'x',&psflg); OptionAdd(&opt,"ppm",'x',&ppmflg); OptionAdd(&opt,"ppmx",'x',&ppmxflg); OptionAdd(&opt,"xml",'x',&xmlflg); OptionAdd(&opt,"land",'x',&land); OptionAdd(&opt,"xp",'f',&xpoff); OptionAdd(&opt,"yp",'f',&ypoff); OptionAdd(&opt,"ippm",'a',&ppmnme); OptionAdd(&opt,"ippmx",'a',&ppmxnme); OptionAdd(&opt,"ixml",'a',&xmlnme); #ifdef _XLIB_ OptionAdd(&opt,"x",'x',&xd); OptionAdd(&opt,"display",'t',&display_name); OptionAdd(&opt,"xoff",'i',&xdoff); OptionAdd(&opt,"yoff",'i',&ydoff); #endif OptionAdd(&opt,"fdb",'t',&fntdbfname); arg=OptionProcess(1,argc,argv,&opt,NULL); if (help==1) { OptionPrintInfo(stdout,hlpstr); exit(0); } if (option==1) { OptionDump(stdout,&opt); exit(0); } if (psflg) pflg=1; if (xmlflg) gflg=1; if (ppmflg) gflg=1; if (ppmxflg) gflg=1; #ifdef _XLIB_ if (xd !=0) { pflg=0; gflg=1; } #endif if (pflg) gflg=0; if ((pflg==0) && (gflg==0)) rflg=1; if (gflg) { if (fntdbfname==NULL) fntdbfname=getenv("FONTDB"); if (fntdbfname !=NULL) { fp=fopen(fntdbfname,"r"); if (fp !=NULL) { fontdb=FrameBufferFontDBLoad(fp); fclose(fp); } } if (fontdb==NULL) { fprintf(stderr,"Could not load fonts.\n"); exit(-1); } } imagedb=FrameBufferDBMake(); loadxml(imagedb,xmlnme); loadppm(imagedb,ppmnme); loadppmx(imagedb,ppmxnme); xmldata=XMLMake(); rplotin=RplotInMake(xmldata); if (gflg) { fbplot=FrameBufferRplotMake(fontdb,imagedb); FrameBufferRplotSetAll(rplotin,fbplot); } if (pflg) { psdata=PostScriptMake(); PostScriptSetText(psdata,stream,stdout); psplot=PostScriptRplotMake(psdata,imagedb); PostScriptRplotSetAll(rplotin,psplot); psplot->land=land; psplot->xoff=xpoff; psplot->yoff=ypoff; } if (rflg) { rplot=RplotMake(); RplotSetText(rplot,stream,stdout); rplotrplot=RplotRplotMake(rplot,imagedb); RplotRplotSetAll(rplotin,rplotrplot); RplotInSetPass(rplotin,clean_newline,stdout); } XMLSetStart(xmldata,RplotInStart,rplotin); XMLSetEnd(xmldata,RplotInEnd,rplotin); XMLSetText(xmldata,RplotInText,rplotin); if (arg<argc) { fp=fopen(argv[arg],"r"); if (fp==NULL) { fprintf(stderr,"Error opening file.\n"); exit(-1); } } else fp=stdin; while(fgets(lbuf,255,fp) !=NULL) { s=XMLDecode(xmldata,lbuf,strlen(lbuf)); if (s !=0) break; } if (s !=0) { fprintf(stderr,"Error parsing document.\n"); exit(0); } if (arg<argc) fclose(fp); if (fbplot==NULL) exit(0); if (fbplot->img==NULL) { fprintf(stderr,"Nothing to plot.\n"); exit(-1); } img=fbplot->img; #ifdef _XLIB_ if (xd !=0) { dp=XwinOpenDisplay(display_name,&xdf); if (dp==NULL) { fprintf(stderr,"Could not open display.\n"); exit(-1); } if (xdoff==-1) xdoff=(dp->wdt-img->wdt)/2; if (ydoff==-1) ydoff=(dp->hgt-img->hgt)/2; win=XwinMakeWindow(xdoff,ydoff,img->wdt,img->hgt,0, dp,name, name,argv[0],name,argc,argv,&xdf); if (win==NULL) { fprintf(stderr,"Could not create window.\n"); exit(-1); } XwinFrameBufferWindow(img,win); XwinShowWindow(win); XwinDisplayEvent(dp,1,&win,1,NULL); XwinFreeWindow(win); XwinCloseDisplay(dp); } else { if (xmlflg==1) FrameBufferSaveXML(img,stream,stdout); else if (ppmflg==1) FrameBufferSavePPM(img,stdout); else FrameBufferSavePPMX(img,stdout); } #else if (xmlflg==1) FrameBufferSaveXML(img,stream,stdout); else if (ppmflg==1) FrameBufferSavePPM(img,stdout); else FrameBufferSavePPMX(img,stdout); #endif return 0; }