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 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.
27 This program is free software; you can redistribute it and/or modify
28 it under the terms of the GNU General Public License as published by
29 the Free Software Foundation; either version 2 of the License, or
30 (at your option) any later version. However, the above notices
31 MUST BE RETAINED in any copy that you redistribute or modify.
33 This program is distributed in the hope that it will be useful,
34 but WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 GNU General Public License for more details.
38 You should have received a copy of the GNU General Public License
39 along with this program; if not, write to the Free Software
40 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
42 ** If you find a bug in zic2xpm.c, please report it to me,
43 Frank McIngvale (frankm@hiwaay.net) so that I may fix it. **
47 Usage: zic2xpm file1 [file2 ...]
49 We split the ZIICS file(s) into 24 XPM & 24 XIM files with names:
51 <piece><type><size>.(xpm|xim).
54 piece = p, n, b, r, q, k
58 Plus 4 files for the light & dark squares.
60 This means that you can extract multiple SIZES in one directory
61 without name clashes. Extracting two sets of the SAME
62 size in a directory will cause the second to overwrite
67 Technical note: Yes, this file is huge. I made it by cramming
68 `zic2xpm' and `zic2xim' together. This should
69 be less confusing to use, though.
88 Data (1 byte per pixel, row major)
92 Map colors from ZIICS -> XIM :
101 int zval; /* ZIICS value */
102 int xval; /* XIM value */
105 /* Associate VGA color with XPM color/sym */
108 int cval; /* VGA pixel value */
109 char xchar; /* XPM character for this color */
110 char *csym; /* Symbolic name */
111 char *cdefault; /* Default color */
114 #define NR_ZIICS_COLORS 4
116 /* SHOWSETS.PAS (from ZIICS) states that images may only
117 use color numbers 0, 2, 14, and 15 */
119 z2xim z2xim_tab[NR_ZIICS_COLORS] = {
125 z2xpm z2xpm_tab[NR_ZIICS_COLORS] = {
126 { 15, 'X', "light_piece", "white" },
127 { 0, ' ', "dark_piece", "black" },
128 { 14, '*', "light_square", "gray" },
129 { 2, '.', "dark_square", "green" } };
134 printf("Fatal error: %s\n", str );
138 z2xim *lookup_xim_color( color )
143 for( i=0; i<NR_ZIICS_COLORS; ++i )
145 if ( z2xim_tab[i].zval == color )
146 return (z2xim_tab + i);
149 fatal("Illegal color in image.");
152 return NULL; /* Make compiler happy */
155 z2xpm *lookup_xpm_color( color )
160 for( i=0; i<NR_ZIICS_COLORS; ++i )
162 if ( z2xpm_tab[i].cval == color )
163 return (z2xpm_tab + i);
166 fatal("Illegal color in image.");
169 return NULL; /* Make compiler happy */
186 unsigned int vga_imagesize( w, h )
194 s = 4 + w8/2 * h + 2;
199 unsigned char *decode_byte( dest, b, w )
200 unsigned char *dest, *b;
204 unsigned char byte, bit;
206 for( i=7; w > 0; --i, --w )
210 /* 1 bit from each plane */
227 W is width of image in PIXELS.
228 SRC is in packed pixel format.
229 DEST is filled with 1 BYTE per PIXEL.
231 unsigned char *decode_line( dest, src, w )
232 unsigned char *dest, *src;
244 /* 4 planes, bpp BYTES per plane */
245 /* Planes are MSB -> LSB */
254 dest = decode_byte( dest, b, 8 );
256 dest = decode_byte( dest, b, w );
262 return (src + bpp * 4);
265 int write_xim_header( fp, w, h )
275 int write_xpm_header( fp, w, 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 );
307 void create_piece_xim( outname, fpin, W, H )
314 unsigned char *lump, *p, *line;
318 fpout = fopen( outname, "wb" );
320 fatal( "Can't create output file.");
322 /* Header is two ints -- Width then Height, x86 format */
324 w = (fgetc(fpin) << 8) | c;
327 h = (fgetc(fpin) << 8) | c;
331 if ( w != W || h != H )
332 fatal( "Bad header." );
334 size = vga_imagesize( w, h ) - 4;
335 lump = (unsigned char*)malloc( size );
336 line = (unsigned char*)malloc( w );
338 if ( !lump || !line )
339 fatal( "Out of memory." );
341 fread( lump, 1, size, fpin );
343 /* Write XIM header */
344 write_xim_header( fpout, w, h );
351 p = decode_line( line, p, w );
355 ent = lookup_xim_color( line[j] );
356 fputc( ent->xval, fpout );
365 void create_piece_xpm( outname, fpin, W, H )
372 unsigned char *lump, *p, *line;
376 fpout = fopen( outname, "wb" );
378 fatal( "Can't create output file.");
380 /* Header is two ints -- Width then Height, x86 format */
382 w = (fgetc(fpin) << 8) | c;
385 h = (fgetc(fpin) << 8) | c;
389 if ( w != W || h != H )
390 fatal( "Bad header." );
392 size = vga_imagesize( w, h ) - 4;
393 lump = (unsigned char*)malloc( size );
394 line = (unsigned char*)malloc( w );
396 if ( !lump || !line )
397 fatal( "Out of memory." );
399 fread( lump, 1, size, fpin );
401 /* Write XPM header */
402 write_xpm_header( fpout, w, h );
409 p = decode_line( line, p, w );
411 fprintf( fpout, "\"" );
414 cv = lookup_xpm_color( line[j] );
415 fprintf( fpout, "%c", cv->xchar );
417 fprintf( fpout, "\",\n" );
420 fprintf( fpout, "};\n" );
427 /* The order of the pieces in the ZIICS piece file (from SHOWSETS.PAS) */
428 char *pieces = "prkqbn";
429 char *pname[] = { "Pawn", "Rook", "King", "Queen", "Bishop", "Knight" };
431 /* The suborder - Light/Light, Light/Dark, etc. */
432 char *prefixes[] = { "ll", "ld", "dl", "dd" };
434 int process_file_xim( filename )
437 int w, h, piece, kind, c;
445 fp = fopen( filename, "rb" );
447 fatal( "Can't open input file." );
449 /* Header is two ints -- Width then Height, x86 format */
451 w = (fgetc(fp) << 8) | c;
454 h = (fgetc(fp) << 8) | c;
460 printf("ERROR: Can only convert square pieces.\n");
461 printf(" (This set is %dx%d)\n", w, h );
465 printf("Creating XIM files...\n");
466 printf("File: %s, W=%d, H=%d\n", filename, w, h );
467 fseek( fp, 0, SEEK_SET );
469 /* Write .XIM files */
470 for( piece = 0; piece < nr_pieces; ++piece )
472 printf("%s ", pname[piece] );
474 for( kind = 0; kind < nr_kinds; ++kind )
477 /* Form output filename -- <piece><kind><size>.xim */
478 sprintf(buf, "%c%s%d.xim", pieces[piece], prefixes[kind], w);
479 create_piece_xim( buf, fp, w, h );
484 /* Write the light & dark squares */
485 sprintf( buf, "lsq%d.xim", w );
486 printf("Light Square" );
487 create_piece_xim( buf, fp, w, h );
489 sprintf( buf, "dsq%d.xim", w );
490 printf("\nDark Square" );
491 create_piece_xim( buf, fp, w, h );
494 printf("Successfully converted!!\n" );
501 int process_file_xpm( filename )
504 int w, h, piece, kind, c;
512 fp = fopen( filename, "rb" );
514 fatal( "Can't open input file." );
516 /* Header is two ints -- Width then Height, x86 format */
518 w = (fgetc(fp) << 8) | c;
521 h = (fgetc(fp) << 8) | c;
527 printf("ERROR: Can only convert square pieces.\n");
528 printf(" (This set is %dx%d)\n", w, h );
532 printf("Creating XPM files...\n");
533 printf("File: %s, W=%d, H=%d\n", filename, w, h );
534 fseek( fp, 0, SEEK_SET );
536 /* Write .XPM files */
537 for( piece = 0; piece < nr_pieces; ++piece )
539 printf("%s ", pname[piece] );
541 for( kind = 0; kind < nr_kinds; ++kind )
544 /* Form output filename -- <piece><kind><size>.xpm */
545 sprintf(buf, "%c%s%d.xpm", pieces[piece], prefixes[kind], w);
546 create_piece_xpm( buf, fp, w, h );
551 /* Write the light & dark squares */
552 sprintf( buf, "lsq%d.xpm", w );
553 printf("Light Square" );
554 create_piece_xpm( buf, fp, w, h );
556 sprintf( buf, "dsq%d.xpm", w );
557 printf("\nDark Square" );
558 create_piece_xpm( buf, fp, w, h );
561 printf("Successfully converted!!\n" );
568 int main( argc, argv )
576 printf("ZIC2XPM 2.01 - by Frank McIngvale (frankm@hiwaay.net)\n");
577 printf("Copyright (C) 1996 Free Software Foundation, Inc.\n\n");
578 printf("Usage: zic2xpm file1 [file2 ...]\n\n");
579 printf(" Splits each file (ZIICS piece files) into 26 XPM & XIM files\n");
580 printf(" suitable for use in XBoard 3.5 or later.\n");
581 printf("\n* ZIICS is a copyrighted work of Andy McFarland (Zek on ICC) *\n");
586 setbuf( stdout, NULL );
588 for( i=1; i<argc; ++i )
590 process_file_xpm( argv[i] );
591 process_file_xim( argv[i] );
594 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
595 "NOTICE: The piece images distributed with ZIICS are",
596 " copyrighted works of their original creators. Images",
597 " converted with zic2xpm may not be redistributed without",
598 " the permission of the copyright holders. Do not contact",
599 " the authors of zic2xpm or of ZIICS itself to request",