1 /* SCCS Id: @(#)xpm2img.c 3.4 2002/03/17 */
2 /* Copyright (c) Christian Bressler 2002 */
3 /* NetHack may be freely redistributed. See license for details. */
4 /* This is mainly a reworked tile2bmp.c + xpm2iff.c -- Marvin */
11 void get_color(unsigned int colind, struct RGB *rgb);
12 void get_pixel(int x, int y, unsigned int *colind);
14 unsigned int **Bild_daten;
15 /* translation table from xpm characters to RGB and colormap slots */
19 int slot; /* output colortable index */
23 int width=0, height=0;
32 int row, col, planeno;
35 fprintf(stderr, "usage: tile2img infile.xpm outfile.img\n");
39 fp = fopen(argv[2],"wb");
41 printf("Error creating IMG-file %s, aborting.\n",argv[2]);
45 if(fopen_xpm_file(argv[1],"r")!=TRUE){
46 printf("Error reading xpm-file %s, aborting.\n",argv[1]);
49 Bild_daten=(unsigned int **)malloc((long)height*sizeof(unsigned int *));
51 Bild_daten[i]=(unsigned int *)malloc((long)width*sizeof(unsigned int));
52 for(row = 0;row<height;row++){
53 char *xb = xpmgetline();
56 printf("Error to few lines in xpm-file %s, aborting.\n",argv[1]);
59 for(col = 0;col<width;col++){
61 if(!ttable[color].flag)
62 fprintf(stderr, "Bad image data\n");
63 Bild_daten[row][col]= ttable[color].slot;
67 fprintf(stderr,"ERROR: zuviele Farben\n");
69 }else if(num_colors>16){
72 }else if(num_colors>2){
79 bitmap_to_file(XIMG, width, height, 372, 372, planes, farben, argv[2], get_color, get_pixel );
84 void get_color(unsigned int colind, struct RGB *rgb){
85 rgb->r=(1000L*(long)ColorMap[colind].r)/0xFF;
86 rgb->g=(1000L*(long)ColorMap[colind].g)/0xFF;
87 rgb->b=(1000L*(long)ColorMap[colind].b)/0xFF;
89 void get_pixel(int x, int y, unsigned int *colind){
90 *colind=Bild_daten[y][x];
94 char *xpmbuf = initbuf;
95 /* version 1. Reads the raw xpm file, NOT the compiled version. This is
96 * not a particularly good idea but I don't have time to do the right thing
97 * at this point, even if I was absolutely sure what that was. */
98 fopen_xpm_file(const char *fn, const char *mode){
101 if(strcmp(mode, "r"))return FALSE; /* no choice now */
102 if(xpmfh)return FALSE; /* one file at a time */
103 xpmfh = fopen(fn, mode);
104 if(!xpmfh)return FALSE; /* I'm hard to please */
105 /* read the header */
107 if(xb == 0)return FALSE;
108 if(4 != sscanf(xb,"%d %d %d %d",
109 &width, &height,&num_colors, &temp))
110 return FALSE; /* bad header */
111 /* replace the original buffer with one big enough for
115 xpmbuf = malloc(width * 2);
117 fprintf(stderr,"ERROR: Can't allocate line buffer\n");
120 if(temp != 1)return FALSE; /* limitation of this code */
122 /* read the colormap and translation table */
124 ColorMap = (struct RGB *)malloc((long)num_colors*sizeof(struct RGB));
125 while(ccount++ < (num_colors-1)){
129 if(xb==0)return FALSE;
130 if(4 != sscanf(xb,"%c c #%2x%2x%2x",&index,&r,&g,&b)){
131 fprintf(stderr,"Bad color entry: %s\n",xb);
134 ttable[index].flag = 1; /* this color is valid */
135 ttable[index].col.r = r;
136 ttable[index].col.g = g;
137 ttable[index].col.b = b;
138 ttable[index].slot = ccount;
139 ColorMap[ccount].r=r;
140 ColorMap[ccount].g=g;
141 ColorMap[ccount].b=b;
146 /* This deserves better. Don't read it too closely - you'll get ill. */
153 if(fgets(buf, bufsz, xpmfh) == 0)return 0;
154 } while(buf[0] != '"');
155 /* strip off the trailing <",> if any */
156 for(bp = buf;*bp;bp++);
158 while(isspace(*bp))bp--;