From 1c016a8de705e2705194258aea50a50bbf0fa260 Mon Sep 17 00:00:00 2001 From: Vince Weaver Date: Fri, 4 May 2012 10:18:29 -0400 Subject: [PATCH] Import vmw_pcx.c from the tb1 svmwgraph --- vmw_pcx.c | 306 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 vmw_pcx.c diff --git a/vmw_pcx.c b/vmw_pcx.c new file mode 100644 index 00000000..dcc9e8e9 --- /dev/null +++ b/vmw_pcx.c @@ -0,0 +1,306 @@ +/* WARNING! These functions only work at 320x200x8bpp right now */ + +/* It is easy to obtain the docs to make this load/save arbitrary */ +/* PCX files. However, I don't have the time nor the inclination */ +/* To do it right now ;) */ + +/* Routines to Load/Save PCX graphics files. */ + +#include "svmwgraph.h" + +#include /* For FILE I/O */ +#include /* For strncmp */ +#include /* for open() */ +#include /* for lseek() */ +#include /* for file modes */ + + +int vmwGetPCXInfo(char *FileName, int *xsize, int *ysize, int *type) { + + unsigned char pcx_header[128]; + int xmin,ymin,xmax,ymax,version=PCX_UNKNOWN,bpp,debug=1,pcx_fd; + + /* Open the file */ + pcx_fd=open(FileName,O_RDONLY); + + if (pcx_fd<0) { + printf("ERROR! File \"%s\" not found!\n",FileName); + return VMW_ERROR_FILE; + } + + lseek(pcx_fd,0,SEEK_SET); + + read(pcx_fd,&pcx_header,128); + + xmin=(pcx_header[5]<<8)+pcx_header[4]; + ymin=(pcx_header[7]<<8)+pcx_header[6]; + + xmax=(pcx_header[9]<<8)+pcx_header[8]; + ymax=(pcx_header[11]<<8)+pcx_header[10]; + + version=pcx_header[1]; + bpp=pcx_header[3]; + + if (debug) { + + printf("Manufacturer: "); + if (pcx_header[0]==10) printf("Zsoft\n"); + else printf("Unknown %i\n",pcx_header[0]); + + printf("Version: "); + + switch(version) { + case 0: printf("2.5\n"); break; + case 2: printf("2.8 w palette\n"); break; + case 3: printf("2.8 w/o palette\n"); break; + case 4: printf("Paintbrush for Windows\n"); break; + case 5: printf("3.0+\n"); break; + default: printf("Unknown %i\n",version); + } + printf("Encoding: "); + if (pcx_header[2]==1) printf("RLE\n"); + else printf("Unknown %i\n",pcx_header[2]); + + printf("BitsPerPixelPerPlane: %i\n",bpp); + printf("File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax); + + printf("Horizontal DPI: %i\n",(pcx_header[13]<<8)+pcx_header[12]); + printf("Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]); + + printf("Number of colored planes: %i\n",pcx_header[65]); + printf("Bytes per line: %i\n",(pcx_header[67]<<8)+pcx_header[66]); + printf("Palette Type: %i\n",(pcx_header[69]<<8)+pcx_header[68]); + printf("Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]); + printf("Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]); + + } + +// *xsize=(xmax-xmin+1); +// *ysize=(ymax-ymin+1); + *xsize=(xmax-xmin+1); + *ysize=(ymax-ymin+1); + + if ((version==5) && (bpp==8) && (pcx_header[65]==3)) *type=PCX_24BIT; + else if (version==5) *type=PCX_8BITPAL; + else *type=PCX_UNKNOWN; + + close(pcx_fd); + + return 0; +} + +int vmwLoadPCX(int x1,int y1,vmwVisual *target, + int LoadPal,int LoadPic,char *FileName, + vmwSVMWGraphState *graph_state) + +{ + + int pcx_fd,x,y,i,numacross,xsize,ysize,xmin,ymin; + unsigned int r,g,b; + int bpp,planes,bpl,xmax,ymax,version; + unsigned char pcx_header[128]; + unsigned char temp_byte; + + /* Open the file */ + pcx_fd=open(FileName,O_RDONLY); + + if (pcx_fd<0) { + printf("ERROR! File \"%s\" not found!\n",FileName); + return VMW_ERROR_FILE; + } + + + + /*************** DECODE THE HEADER *************************/ + read(pcx_fd,&pcx_header,128); + + xmin=(pcx_header[5]<<8)+pcx_header[4]; + ymin=(pcx_header[7]<<8)+pcx_header[6]; + + xmax=(pcx_header[9]<<8)+pcx_header[8]; + ymax=(pcx_header[11]<<8)+pcx_header[10]; + + version=pcx_header[1]; + bpp=pcx_header[3]; + planes=pcx_header[65]; + bpl=(pcx_header[67]<<8)+pcx_header[66]; + + xsize=((xmax-xmin)+1); + ysize=((ymax-ymin)+1); + + /* Possibly add some sanity checking in the header at some point... */ + /* Or actually even get the proper VALUES from the header. Some day... */ + + if (LoadPic) { + + + unsigned char *pointer=target->memory; + + x=0; y=0; + + while (x=x1+xsize) { + x=0; + y++; + numacross=1; +// printf("%i %i %i\n",x,y,numacross); +// fflush(stdout); + } + } + + /* Urgh obscure */ + temp_byte=12; + write(pcx_fd,&temp_byte,1); + + /* Write num_colors r,g,b */ + for(i=0;i<256;i++) { + temp_byte=palette[i].r; + write(pcx_fd,&temp_byte,1); + temp_byte=palette[i].g; + write(pcx_fd,&temp_byte,1); + temp_byte=palette[i].b; + write(pcx_fd,&temp_byte,1); + } + + close(pcx_fd); + return 0; +}