1 /* NetHack 3.6 xpm2img.c $NHDT-Date: 1432512809 2015/05/25 00:13:29 $ $NHDT-Branch: master $:$NHDT-Revision: 1.7 $ */
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]);
50 (unsigned int **) malloc((long) height * sizeof(unsigned int *));
51 for (i = 0; i < height; i++)
53 (unsigned int *) malloc((long) width * sizeof(unsigned int));
54 for (row = 0; row < height; row++) {
55 char *xb = xpmgetline();
58 printf("Error to few lines in xpm-file %s, aborting.\n", argv[1]);
61 for (col = 0; col < width; col++) {
63 if (!ttable[color].flag)
64 fprintf(stderr, "Bad image data\n");
65 Bild_daten[row][col] = ttable[color].slot;
68 if (num_colors > 256) {
69 fprintf(stderr, "ERROR: zuviele Farben\n");
71 } else if (num_colors > 16) {
74 } else if (num_colors > 2) {
81 bitmap_to_file(XIMG, width, height, 372, 372, planes, farben, argv[2],
82 get_color, get_pixel);
88 get_color(unsigned int colind, struct RGB *rgb)
90 rgb->r = (1000L * (long) ColorMap[colind].r) / 0xFF;
91 rgb->g = (1000L * (long) ColorMap[colind].g) / 0xFF;
92 rgb->b = (1000L * (long) ColorMap[colind].b) / 0xFF;
95 get_pixel(int x, int y, unsigned int *colind)
97 *colind = Bild_daten[y][x];
101 char *xpmbuf = initbuf;
102 /* version 1. Reads the raw xpm file, NOT the compiled version. This is
103 * not a particularly good idea but I don't have time to do the right thing
104 * at this point, even if I was absolutely sure what that was. */
105 fopen_xpm_file(const char *fn, const char *mode)
109 if (strcmp(mode, "r"))
110 return FALSE; /* no choice now */
112 return FALSE; /* one file at a time */
113 xpmfh = fopen(fn, mode);
115 return FALSE; /* I'm hard to please */
116 /* read the header */
120 if (4 != sscanf(xb, "%d %d %d %d", &width, &height, &num_colors, &temp))
121 return FALSE; /* bad header */
122 /* replace the original buffer with one big enough for
126 xpmbuf = malloc(width * 2);
128 fprintf(stderr, "ERROR: Can't allocate line buffer\n");
132 return FALSE; /* limitation of this code */
134 /* read the colormap and translation table */
137 (struct RGB *) malloc((long) num_colors * sizeof(struct RGB));
138 while (ccount++ < (num_colors - 1)) {
144 if (4 != sscanf(xb, "%c c #%2x%2x%2x", &index, &r, &g, &b)) {
145 fprintf(stderr, "Bad color entry: %s\n", xb);
148 ttable[index].flag = 1; /* this color is valid */
149 ttable[index].col.r = r;
150 ttable[index].col.g = g;
151 ttable[index].col.b = b;
152 ttable[index].slot = ccount;
153 ColorMap[ccount].r = r;
154 ColorMap[ccount].g = g;
155 ColorMap[ccount].b = b;
160 /* This deserves better. Don't read it too closely - you'll get ill. */
168 if (fgets(buf, bufsz, xpmfh) == 0)
170 } while (buf[0] != '"');
171 /* strip off the trailing <",> if any */
172 for (bp = buf; *bp; bp++)