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 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 );
142 z2xim *lookup_xim_color( 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 */
159 z2xpm *lookup_xpm_color( 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 */
190 unsigned int vga_imagesize( w, h )
198 s = 4 + w8/2 * h + 2;
203 unsigned char *decode_byte( dest, b, w )
204 unsigned char *dest, *b;
208 unsigned char byte, bit;
210 for( i=7; w > 0; --i, --w )
214 /* 1 bit from each plane */
231 W is width of image in PIXELS.
232 SRC is in packed pixel format.
233 DEST is filled with 1 BYTE per PIXEL.
235 unsigned char *decode_line( dest, src, w )
236 unsigned char *dest, *src;
248 /* 4 planes, bpp BYTES per plane */
249 /* Planes are MSB -> LSB */
258 dest = decode_byte( dest, b, 8 );
260 dest = decode_byte( dest, b, w );
266 return (src + bpp * 4);
269 int write_xim_header( fp, w, h )
279 int write_xpm_header( fp, w, h )
286 fprintf(fp, "/* XPM */\n");
287 fprintf(fp, "/* This file was automatically generated from the file %s\n",
289 fprintf(fp, "using the program ``zic2xpm''.\n");
290 fprintf(fp, "\n %s\n %s\n %s\n %s\n %s\n %s */\n",
291 "NOTICE: The piece images distributed with ZIICS are",
292 " copyrighted works of their original creators. Images",
293 " converted with zic2xpm may not be redistributed without",
294 " the permission of the copyright holders. Do not contact",
295 " the authors of zic2xpm or of ZIICS itself to request",
297 fprintf( fp, "static char * image_name[] = {\n" );
298 fprintf( fp, "\"%d %d %d 1\",\n", h, w, NR_ZIICS_COLORS );
302 for( i=0; i<NR_ZIICS_COLORS; ++i, ++cv )
304 fprintf( fp, "\"%c\tc %s s %s\",\n", cv->xchar,
305 cv->cdefault, cv->csym );
311 void create_piece_xim( outname, fpin, W, H )
318 unsigned char *lump, *p, *line;
322 fpout = fopen( outname, "wb" );
324 fatal( "Can't create output file.");
326 /* Header is two ints -- Width then Height, x86 format */
328 w = (fgetc(fpin) << 8) | c;
331 h = (fgetc(fpin) << 8) | c;
335 if ( w != W || h != H )
336 fatal( "Bad header." );
338 size = vga_imagesize( w, h ) - 4;
339 lump = (unsigned char*)malloc( size );
340 line = (unsigned char*)malloc( w );
342 if ( !lump || !line )
343 fatal( "Out of memory." );
345 fread( lump, 1, size, fpin );
347 /* Write XIM header */
348 write_xim_header( fpout, w, h );
355 p = decode_line( line, p, w );
359 ent = lookup_xim_color( line[j] );
360 fputc( ent->xval, fpout );
369 void create_piece_xpm( outname, fpin, W, H )
376 unsigned char *lump, *p, *line;
380 fpout = fopen( outname, "wb" );
382 fatal( "Can't create output file.");
384 /* Header is two ints -- Width then Height, x86 format */
386 w = (fgetc(fpin) << 8) | c;
389 h = (fgetc(fpin) << 8) | c;
393 if ( w != W || h != H )
394 fatal( "Bad header." );
396 size = vga_imagesize( w, h ) - 4;
397 lump = (unsigned char*)malloc( size );
398 line = (unsigned char*)malloc( w );
400 if ( !lump || !line )
401 fatal( "Out of memory." );
403 fread( lump, 1, size, fpin );
405 /* Write XPM header */
406 write_xpm_header( fpout, w, h );
413 p = decode_line( line, p, w );
415 fprintf( fpout, "\"" );
418 cv = lookup_xpm_color( line[j] );
419 fprintf( fpout, "%c", cv->xchar );
421 fprintf( fpout, "\",\n" );
424 fprintf( fpout, "};\n" );
431 /* The order of the pieces in the ZIICS piece file (from SHOWSETS.PAS) */
432 char *pieces = "prkqbn";
433 char *pname[] = { "Pawn", "Rook", "King", "Queen", "Bishop", "Knight" };
435 /* The suborder - Light/Light, Light/Dark, etc. */
436 char *prefixes[] = { "ll", "ld", "dl", "dd" };
438 int process_file_xim( filename )
441 int w, h, piece, kind, c;
449 fp = fopen( filename, "rb" );
451 fatal( "Can't open input file." );
453 /* Header is two ints -- Width then Height, x86 format */
455 w = (fgetc(fp) << 8) | c;
458 h = (fgetc(fp) << 8) | c;
464 printf("ERROR: Can only convert square pieces.\n");
465 printf(" (This set is %dx%d)\n", w, h );
469 printf("Creating XIM files...\n");
470 printf("File: %s, W=%d, H=%d\n", filename, w, h );
471 fseek( fp, 0, SEEK_SET );
473 /* Write .XIM files */
474 for( piece = 0; piece < nr_pieces; ++piece )
476 printf("%s ", pname[piece] );
478 for( kind = 0; kind < nr_kinds; ++kind )
481 /* Form output filename -- <piece><kind><size>.xim */
482 snprintf(buf, BUFLEN, "%c%s%d.xim", pieces[piece], prefixes[kind], w);
483 create_piece_xim( buf, fp, w, h );
488 /* Write the light & dark squares */
489 snprintf( buf, BUFLEN, "lsq%d.xim", w );
490 printf("Light Square" );
491 create_piece_xim( buf, fp, w, h );
493 snprintf( buf, BUFLEN, "dsq%d.xim", w );
494 printf("\nDark Square" );
495 create_piece_xim( buf, fp, w, h );
498 printf("Successfully converted!!\n" );
505 int process_file_xpm( filename )
508 int w, h, piece, kind, c;
516 fp = fopen( filename, "rb" );
518 fatal( "Can't open input file." );
520 /* Header is two ints -- Width then Height, x86 format */
522 w = (fgetc(fp) << 8) | c;
525 h = (fgetc(fp) << 8) | c;
531 printf("ERROR: Can only convert square pieces.\n");
532 printf(" (This set is %dx%d)\n", w, h );
536 printf("Creating XPM files...\n");
537 printf("File: %s, W=%d, H=%d\n", filename, w, h );
538 fseek( fp, 0, SEEK_SET );
540 /* Write .XPM files */
541 for( piece = 0; piece < nr_pieces; ++piece )
543 printf("%s ", pname[piece] );
545 for( kind = 0; kind < nr_kinds; ++kind )
548 /* Form output filename -- <piece><kind><size>.xpm */
549 snprintf(buf, BUFLEN, "%c%s%d.xpm", pieces[piece], prefixes[kind], w);
550 create_piece_xpm( buf, fp, w, h );
555 /* Write the light & dark squares */
556 snprintf( buf, BUFLEN, "lsq%d.xpm", w );
557 printf("Light Square" );
558 create_piece_xpm( buf, fp, w, h );
560 snprintf( buf, BUFLEN, "dsq%d.xpm", w );
561 printf("\nDark Square" );
562 create_piece_xpm( buf, fp, w, h );
565 printf("Successfully converted!!\n" );
572 int main( argc, argv )
580 printf("ZIC2XPM 2.01 - by Frank McIngvale (frankm@hiwaay.net)\n");
581 printf("Copyright (C) 1996 Free Software Foundation, Inc.\n\n");
582 printf("Usage: zic2xpm file1 [file2 ...]\n\n");
583 printf(" Splits each file (ZIICS piece files) into 26 XPM & XIM files\n");
584 printf(" suitable for use in XBoard 3.5 or later.\n");
585 printf("\n* ZIICS is a copyrighted work of Andy McFarland (Zek on ICC) *\n");
590 setbuf( stdout, NULL );
592 for( i=1; i<argc; ++i )
594 process_file_xpm( argv[i] );
595 process_file_xim( argv[i] );
598 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
599 "NOTICE: The piece images distributed with ZIICS are",
600 " copyrighted works of their original creators. Images",
601 " converted with zic2xpm may not be redistributed without",
602 " the permission of the copyright holders. Do not contact",
603 " the authors of zic2xpm or of ZIICS itself to request",