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 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
119 /* SHOWSETS.PAS (from ZIICS) states that images may only
120 use color numbers 0, 2, 14, and 15 */
122 z2xim z2xim_tab[NR_ZIICS_COLORS] = {
128 z2xpm z2xpm_tab[NR_ZIICS_COLORS] = {
129 { 15, 'X', "light_piece", "white" },
130 { 0, ' ', "dark_piece", "black" },
131 { 14, '*', "light_square", "gray" },
132 { 2, '.', "dark_square", "green" } };
137 printf("Fatal error: %s\n", str );
141 z2xim *lookup_xim_color( color )
146 for( i=0; i<NR_ZIICS_COLORS; ++i )
148 if ( z2xim_tab[i].zval == color )
149 return (z2xim_tab + i);
152 fatal("Illegal color in image.");
155 return NULL; /* Make compiler happy */
158 z2xpm *lookup_xpm_color( color )
163 for( i=0; i<NR_ZIICS_COLORS; ++i )
165 if ( z2xpm_tab[i].cval == color )
166 return (z2xpm_tab + i);
169 fatal("Illegal color in image.");
172 return NULL; /* Make compiler happy */
189 unsigned int vga_imagesize( w, h )
197 s = 4 + w8/2 * h + 2;
202 unsigned char *decode_byte( dest, b, w )
203 unsigned char *dest, *b;
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.
234 unsigned char *decode_line( dest, src, w )
235 unsigned char *dest, *src;
247 /* 4 planes, bpp BYTES per plane */
248 /* Planes are MSB -> LSB */
257 dest = decode_byte( dest, b, 8 );
259 dest = decode_byte( dest, b, w );
265 return (src + bpp * 4);
268 int write_xim_header( fp, w, h )
278 int write_xpm_header( fp, w, h )
285 fprintf(fp, "/* XPM */\n");
286 fprintf(fp, "/* This file was automatically generated from the file %s\n",
288 fprintf(fp, "using the program ``zic2xpm''.\n");
289 fprintf(fp, "\n %s\n %s\n %s\n %s\n %s\n %s */\n",
290 "NOTICE: The piece images distributed with ZIICS are",
291 " copyrighted works of their original creators. Images",
292 " converted with zic2xpm may not be redistributed without",
293 " the permission of the copyright holders. Do not contact",
294 " the authors of zic2xpm or of ZIICS itself to request",
296 fprintf( fp, "static char * image_name[] = {\n" );
297 fprintf( fp, "\"%d %d %d 1\",\n", h, w, NR_ZIICS_COLORS );
301 for( i=0; i<NR_ZIICS_COLORS; ++i, ++cv )
303 fprintf( fp, "\"%c\tc %s s %s\",\n", cv->xchar,
304 cv->cdefault, cv->csym );
310 void create_piece_xim( outname, fpin, W, H )
317 unsigned char *lump, *p, *line;
321 fpout = fopen( outname, "wb" );
323 fatal( "Can't create output file.");
325 /* Header is two ints -- Width then Height, x86 format */
327 w = (fgetc(fpin) << 8) | c;
330 h = (fgetc(fpin) << 8) | c;
334 if ( w != W || h != H )
335 fatal( "Bad header." );
337 size = vga_imagesize( w, h ) - 4;
338 lump = (unsigned char*)malloc( size );
339 line = (unsigned char*)malloc( w );
341 if ( !lump || !line )
342 fatal( "Out of memory." );
344 fread( lump, 1, size, fpin );
346 /* Write XIM header */
347 write_xim_header( fpout, w, h );
354 p = decode_line( line, p, w );
358 ent = lookup_xim_color( line[j] );
359 fputc( ent->xval, fpout );
368 void create_piece_xpm( outname, fpin, W, H )
375 unsigned char *lump, *p, *line;
379 fpout = fopen( outname, "wb" );
381 fatal( "Can't create output file.");
383 /* Header is two ints -- Width then Height, x86 format */
385 w = (fgetc(fpin) << 8) | c;
388 h = (fgetc(fpin) << 8) | c;
392 if ( w != W || h != H )
393 fatal( "Bad header." );
395 size = vga_imagesize( w, h ) - 4;
396 lump = (unsigned char*)malloc( size );
397 line = (unsigned char*)malloc( w );
399 if ( !lump || !line )
400 fatal( "Out of memory." );
402 fread( lump, 1, size, fpin );
404 /* Write XPM header */
405 write_xpm_header( fpout, w, h );
412 p = decode_line( line, p, w );
414 fprintf( fpout, "\"" );
417 cv = lookup_xpm_color( line[j] );
418 fprintf( fpout, "%c", cv->xchar );
420 fprintf( fpout, "\",\n" );
423 fprintf( fpout, "};\n" );
430 /* The order of the pieces in the ZIICS piece file (from SHOWSETS.PAS) */
431 char *pieces = "prkqbn";
432 char *pname[] = { "Pawn", "Rook", "King", "Queen", "Bishop", "Knight" };
434 /* The suborder - Light/Light, Light/Dark, etc. */
435 char *prefixes[] = { "ll", "ld", "dl", "dd" };
437 int process_file_xim( filename )
440 int w, h, piece, kind, c;
448 fp = fopen( filename, "rb" );
450 fatal( "Can't open input file." );
452 /* Header is two ints -- Width then Height, x86 format */
454 w = (fgetc(fp) << 8) | c;
457 h = (fgetc(fp) << 8) | c;
463 printf("ERROR: Can only convert square pieces.\n");
464 printf(" (This set is %dx%d)\n", w, h );
468 printf("Creating XIM files...\n");
469 printf("File: %s, W=%d, H=%d\n", filename, w, h );
470 fseek( fp, 0, SEEK_SET );
472 /* Write .XIM files */
473 for( piece = 0; piece < nr_pieces; ++piece )
475 printf("%s ", pname[piece] );
477 for( kind = 0; kind < nr_kinds; ++kind )
480 /* Form output filename -- <piece><kind><size>.xim */
481 sprintf(buf, "%c%s%d.xim", pieces[piece], prefixes[kind], w);
482 create_piece_xim( buf, fp, w, h );
487 /* Write the light & dark squares */
488 sprintf( buf, "lsq%d.xim", w );
489 printf("Light Square" );
490 create_piece_xim( buf, fp, w, h );
492 sprintf( buf, "dsq%d.xim", w );
493 printf("\nDark Square" );
494 create_piece_xim( buf, fp, w, h );
497 printf("Successfully converted!!\n" );
504 int process_file_xpm( filename )
507 int w, h, piece, kind, c;
515 fp = fopen( filename, "rb" );
517 fatal( "Can't open input file." );
519 /* Header is two ints -- Width then Height, x86 format */
521 w = (fgetc(fp) << 8) | c;
524 h = (fgetc(fp) << 8) | c;
530 printf("ERROR: Can only convert square pieces.\n");
531 printf(" (This set is %dx%d)\n", w, h );
535 printf("Creating XPM files...\n");
536 printf("File: %s, W=%d, H=%d\n", filename, w, h );
537 fseek( fp, 0, SEEK_SET );
539 /* Write .XPM files */
540 for( piece = 0; piece < nr_pieces; ++piece )
542 printf("%s ", pname[piece] );
544 for( kind = 0; kind < nr_kinds; ++kind )
547 /* Form output filename -- <piece><kind><size>.xpm */
548 sprintf(buf, "%c%s%d.xpm", pieces[piece], prefixes[kind], w);
549 create_piece_xpm( buf, fp, w, h );
554 /* Write the light & dark squares */
555 sprintf( buf, "lsq%d.xpm", w );
556 printf("Light Square" );
557 create_piece_xpm( buf, fp, w, h );
559 sprintf( buf, "dsq%d.xpm", w );
560 printf("\nDark Square" );
561 create_piece_xpm( buf, fp, w, h );
564 printf("Successfully converted!!\n" );
571 int main( argc, argv )
579 printf("ZIC2XPM 2.01 - by Frank McIngvale (frankm@hiwaay.net)\n");
580 printf("Copyright (C) 1996 Free Software Foundation, Inc.\n\n");
581 printf("Usage: zic2xpm file1 [file2 ...]\n\n");
582 printf(" Splits each file (ZIICS piece files) into 26 XPM & XIM files\n");
583 printf(" suitable for use in XBoard 3.5 or later.\n");
584 printf("\n* ZIICS is a copyrighted work of Andy McFarland (Zek on ICC) *\n");
589 setbuf( stdout, NULL );
591 for( i=1; i<argc; ++i )
593 process_file_xpm( argv[i] );
594 process_file_xim( argv[i] );
597 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
598 "NOTICE: The piece images distributed with ZIICS are",
599 " copyrighted works of their original creators. Images",
600 " converted with zic2xpm may not be redistributed without",
601 " the permission of the copyright holders. Do not contact",
602 " the authors of zic2xpm or of ZIICS itself to request",