#include <stdio.h>
#include <X11/copyright.h>
#include <X11/Xlib.h>
#include <sysexits.h>
  
/*
 * xwdconvert
 * converts x10 window dumps to x11 window dump format.
 * More specifically, converts from version 6 to version 7 format...
 * no arguments, uses stdin, stdout. Dumps v6 header to stderr.
 *
 * Mark W. Eichin, Project Athena, 23 Jan 1988
 * 
 */

  
  
  
/* Copyright 1985, 1986, Massachusetts Institute of Technology */

/* $Header: XWFix.c,v 1.3 88/01/23 08:32:08 eichin Exp $ */
/*
 * XWDFile.h	MIT Project Athena, X Window system window raster
 *		image dumper, dump file format header file.
 *
 *  Author:	Tony Della Fera, DEC
 *		27-Jun-85
 * 
 * Modifier:    William F. Wyatt, SAO
 *              18-Nov-86  - version 6 for saving/restoring color maps
 */

typedef unsigned long xwdval;

typedef struct _xwd_file_header_7 {
	xwdval header_size;	  /* Size of the entire file header (bytes). */
	xwdval file_version;	  /* XWD_FILE_VERSION */
	xwdval pixmap_format;	  /* Pixmap format */
	xwdval pixmap_depth;	  /* Pixmap depth */
	xwdval pixmap_width;	  /* Pixmap width */
	xwdval pixmap_height;	  /* Pixmap height */
	xwdval xoffset;           /* Bitmap x offset */
	xwdval byte_order;        /* MSBFirst, LSBFirst */
	xwdval bitmap_unit;       /* Bitmap unit */
	xwdval bitmap_bit_order;  /* MSBFirst, LSBFirst */
	xwdval bitmap_pad;	  /* Bitmap scanline pad */
	xwdval bits_per_pixel;	  /* Bits per pixel */
	xwdval bytes_per_line;	  /* Bytes per scanline */
	xwdval visual_class;	  /* Class of colormap */
	xwdval red_mask;	  /* Z red mask */
	xwdval green_mask;	  /* Z green mask */
	xwdval blue_mask;	  /* Z blue mask */
	xwdval bits_per_rgb;	  /* Log base 2 of distinct color values */
	xwdval colormap_entries;  /* Number of entries in colormap */
	xwdval ncolors;		  /* Number of Color structures */
	xwdval window_width;	  /* Window width */
	xwdval window_height;	  /* Window height */
	long window_x;		  /* Window upper left X coordinate */
	long window_y;		  /* Window upper left Y coordinate */
	xwdval window_bdrwidth;	  /* Window border width */
} XWDFileHeader7;

typedef struct _xwd_file_header_6 {
	int header_size;	/* Size of the entire file header (bytes). */
	int file_version;	/* XWD_FILE_VERSION */
	int display_type;	/* Display type. */
	int display_planes;	/* Number of display planes. */
	int pixmap_format;	/* Pixmap format. */
	int pixmap_width;	/* Pixmap width. */
	int pixmap_height;	/* Pixmap height. */
	short window_width;	/* Window width. */
	short window_height;	/* Window height. */
	short window_x;		/* Window upper left X coordinate. */
	short window_y;		/* Window upper left Y coordinate. */
	short window_bdrwidth;	/* Window border width. */
	short window_ncolors;   /* number of Color entries in this window */
} XWDFileHeader6;

main(argc,argv)
     int argc;
     char *argv[];
{
  /* assume stdin for now */
  XWDFileHeader6 xf6;
  XWDFileHeader7 xf7;
  int cc;
  char buf[1024];
  
  if(argc>1)
    {
      fprintf(stderr,"Usage: %s < x10.dmp > x11.dmp\n", argv[0]);
      exit(EX_USAGE);
    }
  
  read(0, &xf6, sizeof(xf6));
  dump_6(xf6);
  
  
  xf7.header_size = xf6.header_size-sizeof(xf6)+sizeof(xf7);
				/* Size of the entire file header (bytes). */
  xf7.file_version=7;	  /* XWD_FILE_VERSION */
  xf7.pixmap_format=xf6.pixmap_format;	  /* Pixmap format */
  xf7.pixmap_depth=xf6.display_planes;	  /* Pixmap depth */
  xf7.pixmap_width=xf6.pixmap_width;	  /* Pixmap width */
  xf7.pixmap_height=xf6.pixmap_height;	  /* Pixmap height */
  xf7.xoffset=0;           /* Bitmap x offset */
  xf7.byte_order=LSBFirst;        /* MSBFirst, LSBFirst */
  xf7.bitmap_unit=8;       /* Bitmap unit */
  xf7.bitmap_bit_order=LSBFirst;  /* MSBFirst, LSBFirst */
  xf7.bitmap_pad=16;	  /* Bitmap scanline pad */
  xf7.bits_per_pixel=xf6.display_planes;	  /* Bits per pixel */
  /* Note: some bitmaps need the following tweaked by +1 or +2... */
  /* eg. mandril*, lenna*, ucastle and utiger... */
  xf7.bytes_per_line=xf7.pixmap_width/xf7.bitmap_unit; /* Bytes per scanline */
  xf7.visual_class=StaticColor;	  /* Class of colormap */
  xf7.red_mask=0;	  /* Z red mask */
  xf7.green_mask=0;	  /* Z green mask */
  xf7.blue_mask=0;	  /* Z blue mask */
  xf7.bits_per_rgb=1;	  /* Log base 2 of distinct color values */
  xf7.colormap_entries=2;  /* Number of entries in colormap */
  xf7.ncolors=xf6.window_ncolors;	  /* Number of Color structures */
  xf7.window_width=xf6.window_width;	  /* Window width */
  xf7.window_height=xf6.window_height;	  /* Window height */
  xf7.window_x=xf6.window_x;		  /* Window upper left X coordinate */
  xf7.window_y=xf6.window_y;		  /* Window upper left Y coordinate */
  xf7.window_bdrwidth=xf6.window_bdrwidth;	  /* Window border width */

  write_7(xf7);
  

  while(cc = read(0, buf, sizeof(buf)))
    {
      write(1, buf, cc);
    }
}
#include <sys/types.h>
#include <netinet/in.h>

#define put(x) c=ntohl(xf.x); write(1, &c, sizeof(long))

write_7(xf)
     XWDFileHeader7 xf;
{
  long c;
  
  put(header_size);	  /* Size of the entire file header (bytes). */
  put(file_version);	  /* XWD_FILE_VERSION */
  put(pixmap_format);	  /* Pixmap format */
  put(pixmap_depth);	  /* Pixmap depth */
  put(pixmap_width);	  /* Pixmap width */
  put(pixmap_height);	  /* Pixmap height */
  put(xoffset);           /* Bitmap x offset */
  put(byte_order);        /* MSBFirst, LSBFirst */
  put(bitmap_unit);       /* Bitmap unit */
  put(bitmap_bit_order);  /* MSBFirst, LSBFirst */
  put(bitmap_pad);	  /* Bitmap scanline pad */
  put(bits_per_pixel);	  /* Bits per pixel */
  put(bytes_per_line);	  /* Bytes per scanline */
  put(visual_class);	  /* Class of colormap */
  put(red_mask);	  /* Z red mask */
  put(green_mask);	  /* Z green mask */
  put(blue_mask);	  /* Z blue mask */
  put(bits_per_rgb);	  /* Log base 2 of distinct color values */
  put(colormap_entries);  /* Number of entries in colormap */
  put(ncolors);		  /* Number of Color structures */
  put(window_width);	  /* Window width */
  put(window_height);	  /* Window height */
  put(window_x);		  /* Window upper left X coordinate */
  put(window_y);		  /* Window upper left Y coordinate */
  put(window_bdrwidth);	  /* Window border width */
}

#define prti(ss,i) fprintf(stderr, "Field %s: 0x%x\n",ss,(int)xf.i)
#define prts(ss,i) fprintf(stderr, "Field %s: 0x%x\n",ss,(int)xf.i)
/* try changing arg ss to s! */
dump_6(xf)
     XWDFileHeader6 xf;

{
  
  prti("header_size",header_size);	/* Size of the entire file header (bytes). */
  prti("file_version",file_version);	/* XWD_FILE_VERSION */
  prti("display_type",display_type);	/* Display type. */
  prti("display_planes",display_planes);	/* Number of display planes. */
  prti("pixmap_format",pixmap_format);	/* Pixmap format. */
  prti("pixmap_width",pixmap_width);	/* Pixmap width. */
  prti("pixmap_height",pixmap_height);	/* Pixmap height. */

  prts("window_width",window_width);	/* Window width. */
  prts("window_height",window_height);	/* Window height. */
  prts("window_x",window_x);		/* Window upper left X coordinate. */
  prts("window_y",window_y);		/* Window upper left Y coordinate. */
  prts("window_bdrwidth",window_bdrwidth);	/* Window border width. */
  prts("window_ncolors",window_ncolors);   /* number of Color entries in this window */
}
