4 Program to convert pieces from ZIICS format to XPM & XIM format.
5 (C version) By Frank McIngvale <frankm@hiwaay.net>.
7 Copyright (C) 1996, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
9 NOTICE: The piece images distributed with ZIICS are
10 copyrighted works of their original creators. Images
11 converted with zic2xpm may not be redistributed without
12 the permission of the copyright holders. Do not contact
13 the authors of zic2xpm or of ZIICS itself to request
16 NOTICE: The format of the ZIICS piece file was gleaned from
17 SHOWSETS.PAS, a part of ZIICS. Thanks to Andy McFarland
18 (Zek on ICC) for making this source available! ZIICS is a
19 completely separate and copyrighted work of Andy
20 McFarland. Use and distribution of ZIICS falls under the
21 ZIICS license, NOT the GNU General Public License.
23 NOTICE: The format of the VGA imageblocks was determined
24 by experimentation, and without access to any
25 of Borland Inc.'s BGI library source code.
28 GNU XBoard is free software: you can redistribute it
29 and/or modify it under the terms of the GNU General Public
30 License as published by the Free Software Foundation,
31 either version 3 of the License, or (at your option) any
34 GNU XBoard is distributed in the hope that it will be
35 useful, but WITHOUT ANY WARRANTY; without even the implied
36 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
37 PURPOSE. See the GNU General Public License for more
40 You should have received a copy of the GNU General Public
41 License along with this program. If not, see
42 http://www.gnu.org/licenses/.
45 ** If you find a bug in zic2xpm.c, please report it to me,
46 Frank McIngvale (frankm@hiwaay.net) so that I may fix it. **
50 Usage: zic2xpm file1 [file2 ...]
52 We split the ZIICS file(s) into 24 XPM & 24 XIM files with names:
54 <piece><type><size>.(xpm|xim).
57 piece = p, n, b, r, q, k
61 Plus 4 files for the light & dark squares.
63 This means that you can extract multiple SIZES in one directory
64 without name clashes. Extracting two sets of the SAME
65 size in a directory will cause the second to overwrite
70 Technical note: Yes, this file is huge. I made it by cramming
71 `zic2xpm' and `zic2xim' together. This should
72 be less confusing to use, though.
91 Data (1 byte per pixel, row major)
95 Map colors from ZIICS -> XIM :
104 int zval; /* ZIICS value */
105 int xval; /* XIM value */
108 /* Associate VGA color with XPM color/sym */
111 int cval; /* VGA pixel value */
112 char xchar; /* XPM character for this color */
113 char *csym; /* Symbolic name */
114 char *cdefault; /* Default color */
117 #define NR_ZIICS_COLORS 4
120 /* SHOWSETS.PAS (from ZIICS) states that images may only
121 use color numbers 0, 2, 14, and 15 */
123 z2xim z2xim_tab[NR_ZIICS_COLORS] = {
129 z2xpm z2xpm_tab[NR_ZIICS_COLORS] = {
130 { 15, 'X', "light_piece", "white" },
131 { 0, ' ', "dark_piece", "black" },
132 { 14, '*', "light_square", "gray" },
133 { 2, '.', "dark_square", "green" } };
138 printf("Fatal error: %s\n", str );
143 lookup_xim_color (int color)
147 for( i=0; i<NR_ZIICS_COLORS; ++i )
149 if ( z2xim_tab[i].zval == color )
150 return (z2xim_tab + i);
153 fatal("Illegal color in image.");
156 return NULL; /* Make compiler happy */
160 lookup_xpm_color (int color)
164 for( i=0; i<NR_ZIICS_COLORS; ++i )
166 if ( z2xpm_tab[i].cval == color )
167 return (z2xpm_tab + i);
170 fatal("Illegal color in image.");
173 return NULL; /* Make compiler happy */
191 vga_imagesize (int w, int h)
198 s = 4 + w8/2 * h + 2;
204 decode_byte (unsigned char *dest, unsigned char *b, int w)
207 unsigned char byte, bit;
209 for( i=7; w > 0; --i, --w )
213 /* 1 bit from each plane */
230 W is width of image in PIXELS.
231 SRC is in packed pixel format.
232 DEST is filled with 1 BYTE per PIXEL.
235 decode_line (unsigned char *dest, unsigned char *src, int w)
246 /* 4 planes, bpp BYTES per plane */
247 /* Planes are MSB -> LSB */
256 dest = decode_byte( dest, b, 8 );
258 dest = decode_byte( dest, b, w );
264 return (src + bpp * 4);
268 write_xim_header (FILE *fp, int w, int h)
277 write_xpm_header (FILE *fp, int w, int h)
282 fprintf(fp, "/* XPM */\n");
283 fprintf(fp, "/* This file was automatically generated from the file %s\n",
285 fprintf(fp, "using the program ``zic2xpm''.\n");
286 fprintf(fp, "\n %s\n %s\n %s\n %s\n %s\n %s */\n",
287 "NOTICE: The piece images distributed with ZIICS are",
288 " copyrighted works of their original creators. Images",
289 " converted with zic2xpm may not be redistributed without",
290 " the permission of the copyright holders. Do not contact",
291 " the authors of zic2xpm or of ZIICS itself to request",
293 fprintf( fp, "static char * image_name[] = {\n" );
294 fprintf( fp, "\"%d %d %d 1\",\n", h, w, NR_ZIICS_COLORS );
298 for( i=0; i<NR_ZIICS_COLORS; ++i, ++cv )
300 fprintf( fp, "\"%c\tc %s s %s\",\n", cv->xchar,
301 cv->cdefault, cv->csym );
308 create_piece_xim (char *outname, FILE *fpin, int W, int H)
312 unsigned char *lump, *p, *line;
316 fpout = fopen( outname, "wb" );
318 fatal( "Can't create output file.");
320 /* Header is two ints -- Width then Height, x86 format */
322 w = (fgetc(fpin) << 8) | c;
325 h = (fgetc(fpin) << 8) | c;
329 if ( w != W || h != H )
330 fatal( "Bad header." );
332 size = vga_imagesize( w, h ) - 4;
333 lump = (unsigned char*)malloc( size );
334 line = (unsigned char*)malloc( w );
336 if ( !lump || !line )
337 fatal( "Out of memory." );
339 fread( lump, 1, size, fpin );
341 /* Write XIM header */
342 write_xim_header( fpout, w, h );
349 p = decode_line( line, p, w );
353 ent = lookup_xim_color( line[j] );
354 fputc( ent->xval, fpout );
364 create_piece_xpm (char *outname, FILE *fpin, int W, int H)
368 unsigned char *lump, *p, *line;
372 fpout = fopen( outname, "wb" );
374 fatal( "Can't create output file.");
376 /* Header is two ints -- Width then Height, x86 format */
378 w = (fgetc(fpin) << 8) | c;
381 h = (fgetc(fpin) << 8) | c;
385 if ( w != W || h != H )
386 fatal( "Bad header." );
388 size = vga_imagesize( w, h ) - 4;
389 lump = (unsigned char*)malloc( size );
390 line = (unsigned char*)malloc( w );
392 if ( !lump || !line )
393 fatal( "Out of memory." );
395 fread( lump, 1, size, fpin );
397 /* Write XPM header */
398 write_xpm_header( fpout, w, h );
405 p = decode_line( line, p, w );
407 fprintf( fpout, "\"" );
410 cv = lookup_xpm_color( line[j] );
411 fprintf( fpout, "%c", cv->xchar );
413 fprintf( fpout, "\",\n" );
416 fprintf( fpout, "};\n" );
423 /* The order of the pieces in the ZIICS piece file (from SHOWSETS.PAS) */
424 char *pieces = "prkqbn";
425 char *pname[] = { "Pawn", "Rook", "King", "Queen", "Bishop", "Knight" };
427 /* The suborder - Light/Light, Light/Dark, etc. */
428 char *prefixes[] = { "ll", "ld", "dl", "dd" };
431 process_file_xim (char *filename)
433 int w, h, piece, kind, c;
441 fp = fopen( filename, "rb" );
443 fatal( "Can't open input file." );
445 /* Header is two ints -- Width then Height, x86 format */
447 w = (fgetc(fp) << 8) | c;
450 h = (fgetc(fp) << 8) | c;
456 printf("ERROR: Can only convert square pieces.\n");
457 printf(" (This set is %dx%d)\n", w, h );
461 printf("Creating XIM files...\n");
462 printf("File: %s, W=%d, H=%d\n", filename, w, h );
463 fseek( fp, 0, SEEK_SET );
465 /* Write .XIM files */
466 for( piece = 0; piece < nr_pieces; ++piece )
468 printf("%s ", pname[piece] );
470 for( kind = 0; kind < nr_kinds; ++kind )
473 /* Form output filename -- <piece><kind><size>.xim */
474 snprintf(buf, BUFLEN, "%c%s%d.xim", pieces[piece], prefixes[kind], w);
475 create_piece_xim( buf, fp, w, h );
480 /* Write the light & dark squares */
481 snprintf( buf, BUFLEN, "lsq%d.xim", w );
482 printf("Light Square" );
483 create_piece_xim( buf, fp, w, h );
485 snprintf( buf, BUFLEN, "dsq%d.xim", w );
486 printf("\nDark Square" );
487 create_piece_xim( buf, fp, w, h );
490 printf("Successfully converted!!\n" );
498 process_file_xpm (char *filename)
500 int w, h, piece, kind, c;
508 fp = fopen( filename, "rb" );
510 fatal( "Can't open input file." );
512 /* Header is two ints -- Width then Height, x86 format */
514 w = (fgetc(fp) << 8) | c;
517 h = (fgetc(fp) << 8) | c;
523 printf("ERROR: Can only convert square pieces.\n");
524 printf(" (This set is %dx%d)\n", w, h );
528 printf("Creating XPM files...\n");
529 printf("File: %s, W=%d, H=%d\n", filename, w, h );
530 fseek( fp, 0, SEEK_SET );
532 /* Write .XPM files */
533 for( piece = 0; piece < nr_pieces; ++piece )
535 printf("%s ", pname[piece] );
537 for( kind = 0; kind < nr_kinds; ++kind )
540 /* Form output filename -- <piece><kind><size>.xpm */
541 snprintf(buf, BUFLEN, "%c%s%d.xpm", pieces[piece], prefixes[kind], w);
542 create_piece_xpm( buf, fp, w, h );
547 /* Write the light & dark squares */
548 snprintf( buf, BUFLEN, "lsq%d.xpm", w );
549 printf("Light Square" );
550 create_piece_xpm( buf, fp, w, h );
552 snprintf( buf, BUFLEN, "dsq%d.xpm", w );
553 printf("\nDark Square" );
554 create_piece_xpm( buf, fp, w, h );
557 printf("Successfully converted!!\n" );
565 main (int argc, char **argv)
571 printf("ZIC2XPM 2.01 - by Frank McIngvale (frankm@hiwaay.net)\n");
572 printf("Copyright (C) 1996 Free Software Foundation, Inc.\n\n");
573 printf("Usage: zic2xpm file1 [file2 ...]\n\n");
574 printf(" Splits each file (ZIICS piece files) into 26 XPM & XIM files\n");
575 printf(" suitable for use in XBoard 3.5 or later.\n");
576 printf("\n* ZIICS is a copyrighted work of Andy McFarland (Zek on ICC) *\n");
581 setbuf( stdout, NULL );
583 for( i=1; i<argc; ++i )
585 process_file_xpm( argv[i] );
586 process_file_xim( argv[i] );
589 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
590 "NOTICE: The piece images distributed with ZIICS are",
591 " copyrighted works of their original creators. Images",
592 " converted with zic2xpm may not be redistributed without",
593 " the permission of the copyright holders. Do not contact",
594 " the authors of zic2xpm or of ZIICS itself to request",