v13i029: ImageMagick - Graphics display programs, Part13/21

cristy at dupont.com cristy at dupont.com
Fri May 24 13:19:19 AEST 1991


Submitted-by: cristy at dupont.com
Posting-number: Volume 13, Issue 29
Archive-name: imagemagic/part13

#!/bin/sh
# this is img.13 (part 13 of ImageMagick)
# do not concatenate these parts, unpack them in order with /bin/sh
# file ImageMagick/filters/MIFFtoTIFF.c continued
#
if test ! -r _shar_seq_.tmp; then
	echo 'Please unpack part 1 first!'
	exit 1
fi
(read Scheck
 if test "$Scheck" != 13; then
	echo Please unpack part "$Scheck" next!
	exit 1
 else
	exit 0
 fi
) < _shar_seq_.tmp || exit 1
if test ! -f _shar_wnt_.tmp; then
	echo 'x - still skipping ImageMagick/filters/MIFFtoTIFF.c'
else
echo 'x - continuing file ImageMagick/filters/MIFFtoTIFF.c'
sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/filters/MIFFtoTIFF.c' &&
%                                                                             %
%                                                                             %
%  Copyright 1991 E. I. Dupont de Nemours & Company                           %
%                                                                             %
%  Permission to use, copy, modify,distribute , and sell this software and    %
%  its documentation for any purpose is hereby granted without fee,           %
%  provided that the above Copyright notice appear in all copies and that     %
%  both that Copyright notice and this permission notice appear in            %
%  supporting documentation, and that the name of E. I. Dupont de Nemours     %
%  & Company not be used in advertising or publicity pertaining to            %
%  distribution of the software without specific, written prior               %
%  permission.  E. I. Dupont de Nemours & Company makes no representations    %
%  about the suitability of this software for any purpose.  It is provided    %
%  "as is" without express or implied warranty.                               %
%                                                                             %
%  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
%  to this software, including all implied warranties of merchantability      %
%  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
%  liable for any special, indirect or consequential damages or any           %
%  damages whatsoever resulting from loss of use, data or profits, whether    %
%  in an action of contract, negligence or other tortious action, arising     %
%  out of or in connection with the use or performance of this software.      %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Command syntax:
%
%  export image.miff image.tiff
%
%  Specify 'image.miff' as '-' for standard input.  
%  Specify 'image.tiff' as '-' for standard output.
%
%
*/
X
#include <string.h>
#include "display.h"
#include "image.h"
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   E r r o r                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function Error displays an error message and then terminates the program.
%
%  The format of the Error routine is:
%
%      Error(message,qualifier)
%
%  A description of each parameter follows:
%
%    o message:  Specifies the message to display before terminating the
%      program.
%
%    o qualifier:  Specifies any qualifier to the message.
%
%
*/
void Error(message,qualifier)
char
X  *message,
X  *qualifier;
{
X  (void) fprintf(stderr,"%s: %s",application_name,message); 
X  if (qualifier != (char *) NULL)
X    (void) fprintf(stderr," %s",qualifier);
X  (void) fprintf(stderr,".\n");
X  exit(1);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U s a g e                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure Usage displays the program usage;
%
%  The format of the Usage routine is:
%
%      Usage(message)
%
%  A description of each parameter follows:
%
%    message:  Specifies a specific message to display to the user.
%
*/
static void Usage(message)
char
X  *message;
{
X  if (message != (char *) NULL)
X    (void) fprintf(stderr,"Can't continue,%s\n\n",message);
X  (void) fprintf(stderr,"Usage: %s image.miff image.tiff\n\n",application_name);
X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard input.\n");
X  (void) fprintf(stderr,"Specify 'image.tiff' as '-' for standard output.\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   W r i t e T i f f I m a g e                                               %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function WriteTiffImage writes a TIFF image to a file.
%
%  The format of the WriteTiffImage routine is:
%
%      status=WriteTiffImage(image,verbose)
%
%  A description of each parameter follows:
%
%    o status:  Function WriteTiffImage return True if the image is written.
%      False is returned is there is of a memory shortage or if the image
%      file cannot be opened for writing.
%
%    o image:  A pointer to a Image structure.
%
%    o  verbose:  A value greater than zero prints detailed information about
%       the image.
%
%
*/
static unsigned int WriteTiffImage(image,verbose)
Image
X  *image;
X
unsigned int
X  verbose;
{
#include "tiffio.h"
X
X  register RunlengthPacket
X    *p;
X
X  register int
X    i,
X    j,
X    x,
X    y;
X
X  register unsigned char
X    *q;
X
X  TIFF
X    *file;
X
X  unsigned char
X    *scanline;
X
X  unsigned short
X    rows_per_strip;
X
X  file=TIFFOpen(image->filename,"w");
X  if (file == (TIFF *) NULL)
X    exit(-1);
X  /*
X    Initialize TIFF fields.
X  */
X  TIFFSetField(file,TIFFTAG_BITSPERSAMPLE,8);
X  TIFFSetField(file,TIFFTAG_COMPRESSION,COMPRESSION_NONE);
X  TIFFSetField(file,TIFFTAG_DOCUMENTNAME,image->filename);
X  TIFFSetField(file,TIFFTAG_IMAGEDESCRIPTION,"Converted from MIFF");
X  TIFFSetField(file,TIFFTAG_IMAGELENGTH,image->rows);
X  TIFFSetField(file,TIFFTAG_IMAGEWIDTH,image->columns);
X  TIFFSetField(file,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
X  TIFFSetField(file,TIFFTAG_PHOTOMETRIC,
X    (image->class == DirectClass ? PHOTOMETRIC_RGB : PHOTOMETRIC_PALETTE));
X  TIFFSetField(file,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
X  TIFFSetField(file,TIFFTAG_SAMPLESPERPIXEL,
X    (image->class == DirectClass ? 3 : 1));
X  TIFFSetField(file,TIFFTAG_STRIPBYTECOUNTS,image->columns);
X  rows_per_strip=8192/TIFFScanlineSize(file);
X  if (rows_per_strip == 0)
X    rows_per_strip=1;
X  TIFFSetField(file,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
X  scanline=(unsigned char *) malloc((unsigned int) TIFFScanlineSize(file));
X  if (scanline == (unsigned char *) NULL)
X    Error("memory allocation error",(char *) NULL);
X  if ((image->class == DirectClass) || (image->colors > 256))
X    {
X      /*
X        Convert DirectClass packets to contiguous RGB scanlines.
X      */
X      p=image->pixels;
X      q=scanline;
X      x=0;
X      y=0;
X      for (i=0; i < image->packets; i++)
X      {
X        for (j=0; j <= p->length; j++)
X        {
X          *q++=p->red;
X          *q++=p->green;
X          *q++=p->blue;
X          x++;
X          if (x == image->columns)
X            {
X              if (TIFFWriteScanline(file,(char *) scanline,y) < 0)
X                break;
X              q=scanline;
X              x=0;
X              y++;
X            }
X
X        }
X        p++;
X      }
X    }
X  else
X    {
X      unsigned short
X        blue[256],
X        green[256],
X        red[256];
X
X      /*
X        Initialize TIFF colormap.
X      */
X      for (i=0 ; i < image->colors; i++)
X      {
X        red[i]=(unsigned short) (image->colormap[i].red << 8);
X        green[i]=(unsigned short) (image->colormap[i].green << 8);
X        blue[i]=(unsigned short) (image->colormap[i].blue << 8);
X      }
X      TIFFSetField(file,TIFFTAG_COLORMAP,red,green,blue);
X      /*
X        Convert PseudoClass packets to contiguous colormap indexed scanlines.
X      */
X      p=image->pixels;
X      q=scanline;
X      x=0;
X      y=0;
X      for (i=0; i < image->packets; i++)
X      {
X        for (j=0; j <= p->length; j++)
X        {
X          *q++=(unsigned char) p->index;
X          x++;
X          if (x == image->columns)
X            {
X              if (TIFFWriteScanline(file,(char *) scanline,y) < 0)
X                break;
X              q=scanline;
X              x=0;
X              y++;
X            }
X        }
X        p++;
X      }
X    }
X  (void) free((char *) scanline);
X  (void) TIFFFlushData(file);
X  if (verbose)
X    TIFFPrintDirectory(file,stderr,False,False,False);
X  (void) TIFFClose(file);
X  return(True);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%    M a i n                                                                  %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
int main(argc,argv)
int
X  argc;
X
char
X  *argv[];
{
X  Image
X    *image;
X
X  application_name=argv[0];
X  if (argc < 3)
X    Usage((char *) NULL);
X  image=ReadImage(argv[1]);
X  if (image == (Image *) NULL)
X    exit(1);
X  (void) strcpy(image->filename,argv[2]);
X  WriteTiffImage(image,True);
X  DestroyImage(image);
}
SHAR_EOF
echo 'File ImageMagick/filters/MIFFtoTIFF.c is complete' &&
chmod 0755 ImageMagick/filters/MIFFtoTIFF.c ||
echo 'restore of ImageMagick/filters/MIFFtoTIFF.c failed'
Wc_c="`wc -c < 'ImageMagick/filters/MIFFtoTIFF.c'`"
test 11939 -eq "$Wc_c" ||
	echo 'ImageMagick/filters/MIFFtoTIFF.c: original size 11939, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= ImageMagick/filters/MIFFtoSUN.c ==============
if test -f 'ImageMagick/filters/MIFFtoSUN.c' -a X"$1" != X"-c"; then
	echo 'x - skipping ImageMagick/filters/MIFFtoSUN.c (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting ImageMagick/filters/MIFFtoSUN.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/MIFFtoSUN.c' &&
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                 EEEEE  X   X  PPPP    OOO   RRRR    TTTTT                   %
%                 E       X X   P   P  O   O  R   R     T                     %
%                 EEE      X    PPPP   O   O  RRRR      T                     %
%                 E       X X   P      O   O  R R       T                     %
%                 EEEEE  X   X  P       OOO   R  R      T                     %
%                                                                             %
%                                                                             %
%                  Export MIFF image to a SUN raster format.                  %
%                                                                             %
%                                                                             %
%                                                                             %
%                           Software Design                                   %
%                             John Cristy                                     %
%                            January  1991                                    %
%                                                                             %
%                                                                             %
%  Copyright 1991 E. I. Dupont de Nemours & Company                           %
%                                                                             %
%  Permission to use, copy, modify, distribute, and sell this software and    %
%  its documentation for any purpose is hereby granted without fee,           %
%  provided that the above Copyright notice appear in all copies and that     %
%  both that Copyright notice and this permission notice appear in            %
%  supporting documentation, and that the name of E. I. Dupont de Nemours     %
%  & Company not be used in advertising or publicity pertaining to            %
%  distribution of the software without specific, written prior               %
%  permission.  E. I. Dupont de Nemours & Company makes no representations    %
%  about the suitability of this software for any purpose.  It is provided    %
%  "as is" without express or implied warranty.                               %
%                                                                             %
%  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
%  to this software, including all implied warranties of merchantability      %
%  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
%  liable for any special, indirect or consequential damages or any           %
%  damages whatsoever resulting from loss of use, data or profits, whether    %
%  in an action of contract, negligence or other tortious action, arising     %
%  out of or in connection with the use or performance of this software.      %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Command syntax:
%
%  export image.miff image.sun
%
%  Specify 'image.miff' as '-' for standard input.  
%  Specify 'image.sun' as '-' for standard output.
%
%
*/
X
#include <string.h>
#include "display.h"
#include "image.h"
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   E r r o r                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function Error displays an error message and then terminates the program.
%
%  The format of the Error routine is:
%
%      Error(message,qualifier)
%
%  A description of each parameter follows:
%
%    o message:  Specifies the message to display before terminating the
%      program.
%
%    o qualifier:  Specifies any qualifier to the message.
%
%
*/
void Error(message,qualifier)
char
X  *message,
X  *qualifier;
{
X  (void) fprintf(stderr,"%s: %s",application_name,message); 
X  if (qualifier != (char *) NULL)
X    (void) fprintf(stderr," %s",qualifier);
X  (void) fprintf(stderr,".\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  M S B F i r s t O r d e r                                                  %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function MSBFirstOrder converts a least-significant byte first buffer of
%  integers to most-significant byte first.
%
%  The format of the MSBFirstOrder routine is:
%
%       MSBFirstOrder(p,length);
%
%  A description of each parameter follows.
%
%   o  p:  Specifies a pointer to a buffer of integers.
%
%   o  length:  Specifies the length of the buffer.
%
%
*/
MSBFirstOrder(p,length)
register char 
X  *p;
X
register unsigned 
X  length;
{
X  register char 
X    c,
X    *q,
X    *sp;
X
X  q=p+length;
X  while (p < q) 
X  {
X    sp=p+3;
X    c=(*sp);
X    *sp=(*p);
X    *p++=c;
X    sp=p+1;
X    c=(*sp);
X    *sp=(*p);
X    *p++=c;
X    p+=2;
X  }
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U s a g e                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure Usage displays the program usage;
%
%  The format of the Usage routine is:
%
%      Usage(message)
%
%  A description of each parameter follows:
%
%    message:  Specifies a specific message to display to the user.
%
*/
static void Usage(message)
char
X  *message;
{
X  if (message != (char *) NULL)
X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
X  (void) fprintf(stderr,"Usage: %s image.miff image.sun\n\n",application_name);
X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard input.\n");
X  (void) fprintf(stderr,"Specify 'image.sun' as '-' for standard output.\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   W r i t e R a s t e r I m a g e                                           %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure WriteRasterImage writes an image to a file on disk in SUN 
%  rasterfile format.
%
%  The format of the WriteRasterImage routine is:
%
%      WriteRasterImage(image)
%
%  A description of each parameter follows.
%
%    o image:  A pointer to a Image structure.
%
%
*/
static void WriteRasterImage(image)
Image
X  *image;
{
#define RMT_EQUAL_RGB  1
#define RMT_NONE  0
#define RMT_RAW  2
#define RT_STANDARD  1
#define RT_FORMAT_RGB  3
X
X  typedef struct Raster
X  {
X    int
X      magic,
X      width,
X      height,
X      depth,
X      length,
X      type,
X      maptype,
X      maplength;
X  } Raster;
X
X  Raster
X    sun_header;
X
X  register int
X    i,
X    j,
X    x;
X
X  register RunlengthPacket
X    *p;
X
X  register unsigned char
X    *q;
X
X  unsigned char
X    *sun_pixels;
X
X  unsigned long
X    lsb_first;
X
X  /*
X    Open output image file.
X  */
X  if (*image->filename == '-')
X    image->file=stdout;
X  else
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      image->file=fopen(image->filename,"w");
X    else
X      {
X        char
X          command[256];
X
X        /*
X          Image file is compressed-- uncompress it.
X        */
X        (void) sprintf(command,"compress -c > %s",image->filename);
X        image->file=(FILE *) popen(command,"w");
X      }
X  if (image->file == (FILE *) NULL)
X    Error("unable to open file",image->filename);
X  /*
X    Initialize raster file sun_header.
X  */
X  sun_header.magic=0x59a66a95;
X  sun_header.width=image->columns;
X  sun_header.height=image->rows;
X  sun_header.depth=(image->class == DirectClass ? 24 : 8);
X  sun_header.length=image->columns*image->rows*(sun_header.depth >> 3)+
X    (image->columns % 2 ? image->rows : 0);
X  sun_header.type=RT_STANDARD;
X  sun_header.maptype=(image->class == DirectClass ? RMT_NONE : RMT_EQUAL_RGB);
X  sun_header.maplength=(image->class == DirectClass ? 0 : image->colors*3);
X  /*
X    Ensure the sun_header byte-order is most-significant byte first.
X  */
X  lsb_first=1;
X  if ((*(char *) &lsb_first) == 0)
X    (void) fwrite((char *) &sun_header,sizeof(sun_header),1,image->file);
X  else
X    {
X      Raster
X        reverse_header;
X
X      reverse_header=sun_header;
X      MSBFirstOrder((char *) &reverse_header,sizeof(reverse_header));
X      (void) fwrite((char *) &reverse_header,sizeof(reverse_header),1,
X        image->file);
X    }
X  if (image->class == PseudoClass)
X    {
X      unsigned char
X        *sun_colormap;
X
X      /*
X         Dump colormap to file.
X      */
X      sun_colormap=(unsigned char *) 
X        malloc((unsigned int) sun_header.maplength);
X      if (sun_colormap == (unsigned char *) NULL)
X        Error("unable to allocate memory",(char *) NULL);
X      q=sun_colormap;
X      for (i=0; i < image->colors; i++)
X        *q++=image->colormap[i].red;
X      for (i=0; i < image->colors; i++)
X        *q++=image->colormap[i].green;
X      for (i=0; i < image->colors; i++)
X        *q++=image->colormap[i].blue;
X      (void) fwrite((char *) sun_colormap,sizeof(char),sun_header.maplength,
X        image->file);
X      (void) free((char *) sun_colormap);
X    }
X  /*
X    Convert MIFF to SUN raster pixels.
X  */
X  sun_pixels=(unsigned char *) malloc((unsigned int) sun_header.length);
X  if (sun_pixels == (unsigned char *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  p=image->pixels;
X  q=sun_pixels;
X  x=0;
X  for (i=0; i < image->packets; i++)
X  {
X    for (j=0; j <= p->length; j++)
X    {
X      if (image->class == PseudoClass)
X        *q++=p->index;
X      else
X        {
X          *q++=p->blue;
X          *q++=p->green;
X          *q++=p->red;
X        }
X      if ((image->columns % 2) != 0)
X        {
X          x++;
X          if ((x % image->columns) == 0)
X            *q++;  /* pad scanline */
X        }
X    }
X    p++;
X  }
X  (void) fwrite((char *) sun_pixels,sizeof(char),sun_header.length,image->file);
X  (void) free((char *) sun_pixels);
X  if (image->file != stdin)
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      (void) fclose(image->file);
X    else
X      (void) pclose(image->file);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%    M a i n                                                                  %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
int main(argc,argv)
int
X  argc;
X
char
X  *argv[];
{
X  Image
X    *image;
X
X  application_name=argv[0];
X  if (argc < 3)
X    Usage((char *) NULL);
X  image=ReadImage(argv[1]);
X  if (image == (Image *) NULL)
X    exit(1);
X  (void) strcpy(image->filename,argv[2]);
X  WriteRasterImage(image);
X  (void) fprintf(stderr,"%s=> %s  %dx%d\n",argv[1],argv[2],image->columns,
X    image->rows);
X  return(False);
}
SHAR_EOF
chmod 0755 ImageMagick/filters/MIFFtoSUN.c ||
echo 'restore of ImageMagick/filters/MIFFtoSUN.c failed'
Wc_c="`wc -c < 'ImageMagick/filters/MIFFtoSUN.c'`"
test 13529 -eq "$Wc_c" ||
	echo 'ImageMagick/filters/MIFFtoSUN.c: original size 13529, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= ImageMagick/filters/MIFFtoAVS.c ==============
if test -f 'ImageMagick/filters/MIFFtoAVS.c' -a X"$1" != X"-c"; then
	echo 'x - skipping ImageMagick/filters/MIFFtoAVS.c (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting ImageMagick/filters/MIFFtoAVS.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/MIFFtoAVS.c' &&
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                 EEEEE  X   X  PPPP    OOO   RRRR    TTTTT                   %
%                 E       X X   P   P  O   O  R   R     T                     %
%                 EEE      X    PPPP   O   O  RRRR      T                     %
%                 E       X X   P      O   O  R R       T                     %
%                 EEEEE  X   X  P       OOO   R  R      T                     %
%                                                                             %
%                                                                             %
%                  Export MIFF image to a AVS raster format.                  %
%                                                                             %
%                                                                             %
%                                                                             %
%                           Software Design                                   %
%                             John Cristy                                     %
%                            January  1991                                    %
%                                                                             %
%                                                                             %
%  Copyright 1991 E. I. Dupont de Nemours & Company                           %
%                                                                             %
%  Permission to use, copy, modify, distribute, and sell this software and    %
%  its documentation for any purpose is hereby granted without fee,           %
%  provided that the above Copyright notice appear in all copies and that     %
%  both that Copyright notice and this permission notice appear in            %
%  supporting documentation, and that the name of E. I. Dupont de Nemours     %
%  & Company not be used in advertising or publicity pertaining to            %
%  distribution of the software without specific, written prior               %
%  permission.  E. I. Dupont de Nemours & Company makes no representations    %
%  about the suitability of this software for any purpose.  It is provided    %
%  "as is" without express or implied warranty.                               %
%                                                                             %
%  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
%  to this software, including all implied warranties of merchantability      %
%  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
%  liable for any special, indirect or consequential damages or any           %
%  damages whatsoever resulting from loss of use, data or profits, whether    %
%  in an action of contract, negligence or other tortious action, arising     %
%  out of or in connection with the use or performance of this software.      %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Command syntax:
%
%  export image.miff image.avs
%
%  Specify 'image.miff' as '-' for standard input.  
%  Specify 'image.avs' as '-' for standard output.
%
%
*/
X
#include <string.h>
#include "display.h"
#include "image.h"
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   E r r o r                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function Error displays an error message and then terminates the program.
%
%  The format of the Error routine is:
%
%      Error(message,qualifier)
%
%  A description of each parameter follows:
%
%    o message:  Specifies the message to display before terminating the
%      program.
%
%    o qualifier:  Specifies any qualifier to the message.
%
%
*/
void Error(message,qualifier)
char
X  *message,
X  *qualifier;
{
X  (void) fprintf(stderr,"%s: %s",application_name,message); 
X  if (qualifier != (char *) NULL)
X    (void) fprintf(stderr," %s",qualifier);
X  (void) fprintf(stderr,".\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U s a g e                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure Usage displays the program usage;
%
%  The format of the Usage routine is:
%
%      Usage(message)
%
%  A description of each parameter follows:
%
%    message:  Specifies a specific message to display to the user.
%
*/
static void Usage(message)
char
X  *message;
{
X  if (message != (char *) NULL)
X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
X  (void) fprintf(stderr,"Usage: %s image.miff image.avs\n\n",application_name);
X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard input.\n");
X  (void) fprintf(stderr,"Specify 'image.avs' as '-' for standard output.\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   W r i t e A v s I m a g e                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure WriteAvsImage writes an image to a file on disk in AVS rasterfile
%  format.
%
%  The format of the WriteAvsImage routine is:
%
%      WriteAvsImage(image)
%
%  A description of each parameter follows.
%
%    o image:  A pointer to a Image structure.
%
%
*/
static void WriteAvsImage(image)
Image
X  *image;
{
X  typedef struct Raster
X  {
X    int
X      width,
X      height;
X  } Raster;
X
X  Raster
X    header;
X
X  register int
X    i,
X    j;
X
X  register RunlengthPacket
X    *p;
X
X  register unsigned char
X    *q;
X
X  unsigned char
X    *avs_pixels;
X
X  /*
X    Open output image file.
X  */
X  if (*image->filename == '-')
X    image->file=stdout;
X  else
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      image->file=fopen(image->filename,"w");
X    else
X      {
X        char
X          command[256];
X
X        /*
X          Image file is compressed-- uncompress it.
X        */
X        (void) sprintf(command,"compress -c > %s",image->filename);
X        image->file=(FILE *) popen(command,"w");
X      }
X  if (image->file == (FILE *) NULL)
X    Error("unable to open file",image->filename);
X  /*
X    Initialize raster file header.
X  */
X  header.width=image->columns;
X  header.height=image->rows;
X  avs_pixels=(unsigned char *) malloc(image->columns*image->rows*4);
X  if (avs_pixels == (unsigned char *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  (void) fwrite((char *) &header,sizeof(header),1,image->file);
X  p=image->pixels;
X  q=avs_pixels;
X  for (i=0; i < image->packets; i++)
X  {
X    for (j=0; j <= p->length; j++)
X    {
X      *q++=0;
X      *q++=p->red;
X      *q++=p->green;
X      *q++=p->blue;
X    }
X    p++;
X  }
X  (void) fwrite((char *) avs_pixels,sizeof(char),(int) 
X    (image->columns*image->rows*4),image->file);
X  if (image->file != stdin)
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      (void) fclose(image->file);
X    else
X      (void) pclose(image->file);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%    M a i n                                                                  %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
int main(argc,argv)
int
X  argc;
X
char
X  *argv[];
{
X  Image
X    *image;
X
X  application_name=argv[0];
X  if (argc < 3)
X    Usage((char *) NULL);
X  image=ReadImage(argv[1]);
X  if (image == (Image *) NULL)
X    exit(1);
X  (void) strcpy(image->filename,argv[2]);
X  WriteAvsImage(image);
X  (void) fprintf(stderr,"%s=> %s  %dx%d\n",argv[1],argv[2],image->columns,
X    image->rows);
X  DestroyImage(image);
X  return(False);
}
SHAR_EOF
chmod 0755 ImageMagick/filters/MIFFtoAVS.c ||
echo 'restore of ImageMagick/filters/MIFFtoAVS.c failed'
Wc_c="`wc -c < 'ImageMagick/filters/MIFFtoAVS.c'`"
test 10081 -eq "$Wc_c" ||
	echo 'ImageMagick/filters/MIFFtoAVS.c: original size 10081, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= ImageMagick/filters/AVStoMIFF.c ==============
if test -f 'ImageMagick/filters/AVStoMIFF.c' -a X"$1" != X"-c"; then
	echo 'x - skipping ImageMagick/filters/AVStoMIFF.c (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting ImageMagick/filters/AVStoMIFF.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/AVStoMIFF.c' &&
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                 IIIII  M   M  PPPP    OOO   RRRR    TTTTT                   %
%                   I    MM MM  P   P  O   O  R   R     T                     %
%                   I    M M M  PPPP   O   O  RRRR      T                     %
%                   I    M   M  P      O   O  R R       T                     %
%                 IIIII  M   M  P       OOO   R  R      T                     % 
%                                                                             %
%                                                                             %
%                 Import AVS raster image to a MIFF format.                   %
%                                                                             %
%                                                                             %
%                                                                             %
%                           Software Design                                   %
%                             John Cristy                                     %
%                            January  1991                                    %
%                                                                             %
%                                                                             %
%  Copyright 1991 E. I. Dupont de Nemours & Company                           %
%                                                                             %
%  Permission to use, copy, modify, distribute, and sell this software and    %
%  its documentation for any purpose is hereby granted without fee,           %
%  provided that the above Copyright notice appear in all copies and that     %
%  both that Copyright notice and this permission notice appear in            %
%  supporting documentation, and that the name of E. I. Dupont de Nemours     %
%  & Company not be used in advertising or publicity pertaining to            %
%  distribution of the software without specific, written prior               %
%  permission.  E. I. Dupont de Nemours & Company makes no representations    %
%  about the suitability of this software for any purpose.  It is provided    %
%  "as is" without express or implied warranty.                               %
%                                                                             %
%  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
%  to this software, including all implied warranties of merchantability      %
%  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
%  liable for any special, indirect or consequential damages or any           %
%  damages whatsoever resulting from loss of use, data or profits, whether    %
%  in an action of contract, negligence or other tortious action, arising     %
%  out of or in connection with the use or performance of this software.      %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
%  Command syntax:
%
%  import [-scene #] image.AVS image.miff
%  
%  Specify 'image.AVS' as '-' for standard input.  
%  Specify 'image.miff' as '-' for standard output.
%  
%
*/
X
#include "display.h"
#include "image.h"
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   E r r o r                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function Error displays an error message and then terminates the program.
%
%  The format of the Error routine is:
%
%      Error(message,qualifier)
%
%  A description of each parameter follows:
%
%    o message:  Specifies the message to display before terminating the
%      program.
%
%    o qualifier:  Specifies any qualifier to the message.
%
%
*/
void Error(message,qualifier)
char
X  *message,
X  *qualifier;
{
X  (void) fprintf(stderr,"%s: %s",application_name,message); 
X  if (qualifier != (char *) NULL)
X    (void) fprintf(stderr," %s",qualifier);
X  (void) fprintf(stderr,".\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  R e a d A v s I m a g e                                                    %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function ReadAVSImage reads an image file and returns it.  It allocates the 
%  memory necessary for the new Image structure and returns a pointer to the 
%  new image.
%
%  The format of the ReadAVSImage routine is:
%
%      image=ReadAVSImage(filename)
%
%  A description of each parameter follows:
%
%    o image:  Function ReadAVSImage returns a pointer to the image after 
%      reading. A null image is returned if there is a a memory shortage or if 
%      the image cannot be read.
%
%    o filename:  Specifies the name of the image to read.
%
%
*/
static Image *ReadAVSImage(filename)
char
X  *filename;
{
#define RT_STANDARD  1
#define RMT_NONE  0
#define RMT_EQUAL_RGB  1
X
X  typedef struct _Rasterfile
X  {
X    int
X      width,
X      height;
X  } Rasterfile;
X
X  Image
X    *image;
X
X  Rasterfile
X    avs_header;
X
X  register int
X    i;
X
X  register unsigned char
X    blue,
X    green,
X    *p,
X    red;
X
X  register RunlengthPacket
X    *q;
X
X  unsigned char
X    *avs_pixels;
X
X  /*
X    Allocate image structure.
X  */
X  image=(Image *) malloc(sizeof(Image));
X  if (image == (Image *) NULL)
X    Error("memory allocation error",(char *) NULL);
X  /*
X    Initialize Image structure.
X  */
X  image->id=UnknownId;
X  image->class=DirectClass;
X  image->compression=RunlengthEncodedCompression;
X  image->columns=0;
X  image->rows=0;
X  image->packets=0;
X  image->colors=0;
X  image->scene=0;
X  image->colormap=(ColorPacket *) NULL;
X  image->pixels=(RunlengthPacket *) NULL;
X  image->comments=(char *) NULL;
X  /*
X    Open image file.
X  */
X  (void) strcpy(image->filename,filename);
X  if (*image->filename == '-')
X    image->file=stdin;
X  else
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      image->file=fopen(image->filename,"r");
X    else
X      {
X        char
X          command[256];
X
X        /*
X          Image file is compressed-- uncompress it.
X        */
X        (void) sprintf(command,"uncompress -c %s",image->filename);
X        image->file=(FILE *) popen(command,"r");
X      }
X  if (image->file == (FILE *) NULL)
X    {
X      Warning("unable to open file",image->filename);
X      (void) DestroyImage(image);
X      return((Image *) NULL);
X    }
X  /*
X    Read AVS image.
X  */
X  (void) ReadData((char *) &avs_header,1,sizeof(Rasterfile),image->file);
X  avs_pixels=(unsigned char *) 
X    malloc((unsigned int) avs_header.width*avs_header.height*4);
X  if (avs_pixels == (unsigned char *) NULL)
X    Error("memory allocation error",(char *) NULL);
X  (void) ReadData((char *) avs_pixels,1,avs_header.width*avs_header.height*4,
X    image->file);
X  /*
X    Create image.
X  */
X  image->comments=(char *) malloc((unsigned int) (strlen(image->filename)+256));
X  if (image->comments == (char *) NULL)
X    Error("memory allocation error",(char *) NULL);
X  (void) sprintf(image->comments,"\n  Imported from AVS raster image:  %s\n",
X    image->filename);
X  image->columns=avs_header.width;
X  image->rows=avs_header.height;
X  image->pixels=(RunlengthPacket *) 
X    malloc(image->columns*image->rows*sizeof(RunlengthPacket));
X  if (image->pixels == (RunlengthPacket *) NULL)
X    Error("memory allocation error",(char *) NULL);
X  /*
X    Convert AVS raster image to runlength-encoded packets.
X  */
X  p=avs_pixels;
X  image->packets=0;
X  q=image->pixels;
X  q->length=MaxRunlength;
X  for (i=0; i < (image->columns*image->rows); i++)
X  {
X    p++;
X    red=(*p++);
X    green=(*p++);
X    blue=(*p++);
X    if ((red == q->red) && (green == q->green) && (blue == q->blue) && 
X        (q->length < MaxRunlength))
X      q->length++;
X    else
X      {
X        if (image->packets > 0)
X          q++;
X        image->packets++;
X        q->red=red;
X        q->green=green;
X        q->blue=blue;
X        q->index=0;
X        q->length=0;
X      }
X  }
X  (void) free((char *) avs_pixels);
X  if (image->file != stdin)
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      (void) fclose(image->file);
X    else
X      (void) pclose(image->file);
X  if (image->packets > ((image->columns*image->rows*3) >> 2))
X    image->compression=NoCompression;
X  image->pixels=(RunlengthPacket *)
X    realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
X  return(image);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U s a g e                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure Usage displays the program usage;
%
%  The format of the Usage routine is:
%
%      Usage(message)
%
%  A description of each parameter follows:
%
%    message:  Specifies a specific message to display to the user.
%
*/
static void Usage(message)
char
X  *message;
{
X  if (message != (char *) NULL)
X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
X  (void) fprintf(stderr,"Usage: %s [-scene #] image.avs image.miff\n\n",
X    application_name);
X  (void) fprintf(stderr,"Specify 'image.avs' as '-' for standard input. \n");
X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard output.\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  M a i n                                                                    %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
int main(argc,argv)
int
X  argc;
X   
char
X  *argv[];
{
X  char
X    filename[256];
X
X  Image
X    *image;
X
X  int
X    i;
X
X  unsigned int
X    scene;
X
X  /*
X    Initialize program variables.
X  */
X  application_name=argv[0];
X  i=1;
X  scene=0;
X  if (argc < 3)
X    Usage((char *) NULL);
X  /*
X    Read image and convert to MIFF format.
X  */
X  if (strncmp(argv[i],"-scene",2) == 0)
X    {
X      i++;
X      scene=atoi(argv[i++]);
X    }
X  (void) strcpy(filename,argv[i++]);
X  image=ReadAVSImage(filename);
X  if (image == (Image *) NULL)
X    exit(1);
X  (void) strcpy(image->filename,argv[i++]);
X  image->scene=scene;
X  (void) WriteImage(image);
X  (void) fprintf(stderr,"%s => %s  %dx%d\n",filename,image->filename,
X    image->columns,image->rows);
X  DestroyImage(image);
X  return(False);
}
SHAR_EOF
chmod 0755 ImageMagick/filters/AVStoMIFF.c ||
echo 'restore of ImageMagick/filters/AVStoMIFF.c failed'
Wc_c="`wc -c < 'ImageMagick/filters/AVStoMIFF.c'`"
test 12689 -eq "$Wc_c" ||
	echo 'ImageMagick/filters/AVStoMIFF.c: original size 12689, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= ImageMagick/filters/XWDtoMIFF.c ==============
if test -f 'ImageMagick/filters/XWDtoMIFF.c' -a X"$1" != X"-c"; then
	echo 'x - skipping ImageMagick/filters/XWDtoMIFF.c (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting ImageMagick/filters/XWDtoMIFF.c (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/XWDtoMIFF.c' &&
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%                 IIIII  M   M  PPPP    OOO   RRRR    TTTTT                   %
%                   I    MM MM  P   P  O   O  R   R     T                     %
%                   I    M M M  PPPP   O   O  RRRR      T                     %
%                   I    M   M  P      O   O  R R       T                     %
%                 IIIII  M   M  P       OOO   R  R      T                     %
%                                                                             %
%                                                                             %
%               Import X11 XWD raster image to a MIFF format.                 %
%                                                                             %
%                                                                             %
%                                                                             %
%                           Software Design                                   %
%                             John Cristy                                     %
%                            January  1991                                    %
%                                                                             %
%                                                                             %
%  Copyright 1991 E. I. Dupont de Nemours & Company                           %
%                                                                             %
%  Permission to use, copy, modify, distribute, and sell this software and    %
%  its documentation for any purpose is hereby granted without fee,           %
%  provided that the above Copyright notice appear in all copies and that     %
%  both that Copyright notice and this permission notice appear in            %
%  supporting documentation, and that the name of E. I. Dupont de Nemours     %
%  & Company not be used in advertising or publicity pertaining to            %
%  distribution of the software without specific, written prior               %
%  permission.  E. I. Dupont de Nemours & Company makes no representations    %
%  about the suitability of this software for any purpose.  It is provided    %
%  "as is" without express or implied warranty.                               %
%                                                                             %
%  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
%  to this software, including all implied warranties of merchantability      %
%  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
%  liable for any special, indirect or consequential damages or any           %
%  damages whatsoever resulting from loss of use, data or profits, whether    %
%  in an action of contract, negligence or other tortious action, arising     %
%  out of or in connection with the use or performance of this software.      %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Command syntax:
%
%  import [-scene #] image.xwd image.miff
%
%  Specify 'image.xwd' as '-' for standard input.
%  Specify 'image.miff' as '-' for standard output.
%
%
*/
X
#include "display.h"
#include "image.h"
#include "X.h"
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   E r r o r                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function Error displays an error message and then terminates the program.
%
%  The format of the Error routine is:
%
%      Error(message,qualifier)
%
%  A description of each parameter follows:
%
%    o message:  Specifies the message to display before terminating the
%      program.
%
%    o qualifier:  Specifies any qualifier to the message.
%
%
*/
void Error(message,qualifier)
char
X  *message,
X  *qualifier;
{
X  (void) fprintf(stderr,"%s: %s",application_name,message);
X  if (qualifier != (char *) NULL)
X    (void) fprintf(stderr," %s",qualifier);
X  (void) fprintf(stderr,".\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  M S B F i r s t O r d e r L o n g                                          %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function MSBFirstOrderLong converts a least-significant byte first buffer
%  of long integers to most-significant byte first.
%
%  The format of the MSBFirstOrderLong routine is:
%
%       MSBFirstOrderLong(p,length);
%
%  A description of each parameter follows.
%
%   o  p:  Specifies a pointer to a buffer of integers.
%
%   o  length:  Specifies the length of the buffer.
%
%
*/
MSBFirstOrderLong(p,length)
register char
X  *p;
X
register unsigned
X  length;
{
X  register char
X    c,
X    *q,
X    *sp;
X
X  q=p+length;
X  while (p < q)
X  {
X    sp=p+3;
X    c=(*sp);
X    *sp=(*p);
X    *p++=c;
X    sp=p+1;
X    c=(*sp);
X    *sp=(*p);
X    *p++=c;
X    p+=2;
X  }
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  M S B F i r s t O r d e r S h o r t                                        %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function MSBFirstOrderShort converts a least-significant byte first buffer
%  of short integers to most-significant byte first.
%
%  The format of the MSBFirstOrderShort routine is:
%
%       MSBFirstOrderShort(p,length);
%
%  A description of each parameter follows.
%
%   o  p:  Specifies a pointer to a buffer of integers.
%
%   o  length:  Specifies the length of the buffer.
%
%
*/
MSBFirstOrderShort(p,length)
register char
X  *p;
X
register unsigned
X  length;
{
X  register char
X    c,
X    *q;
X
X  q=p+length;
X  while (p < q)
X  {
X    c=(*p);
X    *p=(*(p+1));
X    p++;
X    *p++=c;
X  }
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  R e a d X W D I m a g e                                                    %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Function ReadXWDImage reads an image file and returns it.  It allocates the 
%  memory necessary for the new Image structure and returns a pointer to the 
%  new image.
%
%  The format of the ReadXWDImage routine is:
%
%      image=ReadXWDImage(filename)
%
%  A description of each parameter follows:
%
%    o image:  Function ReadXWDImage returns a pointer to the image after 
%      reading.  A null image is returned if there is a a memory shortage or 
%      if the image cannot be read.
%
%    o filename:  Specifies the name of the image to read.
%
%
*/
static Image *ReadXWDImage(filename)
char
X  *filename;
{
#include <X11/XWDFile.h>
X
X  char
X    *window_name;
X
X  Image
X    *image;
X
X  int
X    status,
X    x,
X    y;
X
X  register int
X    i;
X
X  register RunlengthPacket
X    *p;
X
X  register unsigned long
X    pixel;
X
X  unsigned char
X    blue,
X    green,
X    red;
X
X  unsigned int
X    packets;
X
X  unsigned long
X    lsb_first;
X
X  XColor
X    *colors;
X
X  XImage
X    ximage;
X
X  XWDFileHeader
X    header;
X
X  /*
X    Allocate image structure.
X  */
X  image=(Image *) malloc(sizeof(Image));
X  if (image == (Image *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  /*
X    Initialize Image structure.
X  */
X  image->id=UnknownId;
X  image->class=DirectClass;
X  image->compression=RunlengthEncodedCompression;
X  image->columns=0;
X  image->rows=0;
X  image->packets=0;
X  image->colors=0;
X  image->scene=0;
X  image->colormap=(ColorPacket *) NULL;
X  image->pixels=(RunlengthPacket *) NULL;
X  image->comments=(char *) NULL;
X  /*
X    Open image file.
X  */
X  (void) strcpy(image->filename,filename);
X  if (*image->filename == '-')
X    image->file=stdin;
X  else
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      image->file=fopen(image->filename,"r");
X    else
X      {
X        char
X          command[256];
X
X        /*
X          Image file is compressed-- uncompress it.
X        */
X        (void) sprintf(command,"uncompress -c %s",image->filename);
X        image->file=(FILE *) popen(command,"r");
X      }
X  if (image->file == (FILE *) NULL)
X    Error("unable to open file",image->filename);
X  /*
X     Read in header information.
X  */
X  status=ReadData((char *) &header,sizeof(header),1,image->file);
X  if (status == False)
X    Error("Unable to read dump file header",image->filename);
X  /*
X    Ensure the header byte-order is most-significant byte first.
X  */
X  lsb_first=1;
X  if (*(char *) &lsb_first)
X    MSBFirstOrderLong((char *) &header,sizeof(header));
X  /*
X    Check to see if the dump file is in the proper format.
X  */
X  if (header.file_version != XWD_FILE_VERSION)
X    Error("XWD file format version mismatch",image->filename);
X  if (header.header_size < sizeof(header))
X    Error("XWD header size is too small",image->filename);
X  packets=(header.header_size-sizeof(header));
X  window_name=(char *) malloc((unsigned int) packets);
X  if (window_name == (char *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  status=ReadData((char *) window_name,sizeof(char),(int) packets,image->file);
X  if (status == False)
X    Error("unable to read window name from dump file",image->filename);
X  /*
X    Initialize the X image.
X  */
X  ximage.width=(int) header.pixmap_width;
X  ximage.height=(int) header.pixmap_height;
X  ximage.xoffset=(int) header.xoffset;
X  ximage.format=(int) header.pixmap_format;
X  ximage.byte_order=(int) header.byte_order;
X  ximage.bitmap_unit=(int) header.bitmap_unit;
X  ximage.bitmap_bit_order=(int) header.bitmap_bit_order;
X  ximage.bitmap_pad=(int) header.bitmap_pad;
X  ximage.depth=(int) header.pixmap_depth;
X  ximage.bits_per_pixel=(int) header.bits_per_pixel;
X  ximage.bytes_per_line=(int) header.bytes_per_line;
X  ximage.red_mask=header.red_mask;
X  ximage.green_mask=header.green_mask;
X  ximage.blue_mask=header.blue_mask;
X  ximage.obdata=NULL;
X  _XInitImageFuncPtrs(&ximage);
X  /*
X    Read colormap.
X  */
X  if (header.ncolors > 0)
X    {
X      colors=(XColor *) malloc((unsigned) header.ncolors*sizeof(XColor));
X      if (colors == (XColor *) NULL)
X        Error("unable to allocate memory",(char *) NULL);
X      status=ReadData((char *) colors,sizeof(XColor),(int) header.ncolors,
X	image->file);
X      if (status == False)
X        Error("unable to read color map from dump file",image->filename);
X      /*
X        Ensure the header byte-order is most-significant byte first.
X      */
X      lsb_first=1;
X      if (*(char *) &lsb_first)
X        for (i=0; i < header.ncolors; i++)
X        {
X          MSBFirstOrderLong((char *) &colors[i].pixel,sizeof(unsigned long));
X          MSBFirstOrderShort((char *) &colors[i].red,3*sizeof(unsigned short));
X        }
X    }
X  /*
X    Allocate the pixel buffer.
X  */
X  if (ximage.format == ZPixmap)
X    packets=ximage.bytes_per_line*ximage.height;
X  else
X    packets=ximage.bytes_per_line*ximage.height*ximage.depth;
X  ximage.data=(char *) malloc((unsigned int) packets);
X  if (ximage.data == (char *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  status=ReadData(ximage.data,sizeof(char),(int) packets,image->file);
X  if (status == False)
X    Error("unable to read dump pixmap",image->filename);
X  if (*image->filename != '-')
X    (void) fclose(image->file);
X  /*
X    Convert image to MIFF format.
X  */
X  image->columns=ximage.width;
X  image->rows=ximage.height;
X  /*
X    Initial image comment.
X  */
X  image->comments=(char *) 
X    malloc((unsigned int) (strlen(image->filename)+256));
X  if (image->comments == (char *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  (void) sprintf(image->comments,"\n  Imported from X11 dump file:  %s\n\0",
X    image->filename);
X  if ((ximage.red_mask > 0) || (ximage.green_mask > 0) ||
X      (ximage.blue_mask > 0))
X    image->class=DirectClass;
X  else
X    image->class=PseudoClass;
X  image->colors=header.ncolors;
X  image->pixels=(RunlengthPacket *)
X    malloc(image->columns*image->rows*sizeof(RunlengthPacket));
X  if (image->pixels == (RunlengthPacket *) NULL)
X    Error("unable to allocate memory",(char *) NULL);
X  image->packets=0;
X  p=image->pixels;
X  p->length=MaxRunlength;
X  switch (image->class)
X  {
X    case DirectClass:
X    {
X      register unsigned long
X        color,
X        index;
X
X      unsigned long
X        blue_mask,
X        blue_shift,
X        green_mask,
X        green_shift,
X        red_mask,
X        red_shift;
X
X      /*
X        Determine shift and mask for red, green, and blue.
X      */
X      red_mask=ximage.red_mask;
X      red_shift=0;
X      while ((red_mask & 0x01) == 0)
X      {
X        red_mask>>=1;
X        red_shift++;
X      }
X      green_mask=ximage.green_mask;
X      green_shift=0;
X      while ((green_mask & 0x01) == 0)
X      {
X        green_mask>>=1;
X        green_shift++;
X      }
X      blue_mask=ximage.blue_mask;
X      blue_shift=0;
X      while ((blue_mask & 0x01) == 0)
X      {
X        blue_mask>>=1;
X        blue_shift++;
X      }
X      /*
X        Convert X image to DirectClass packets.
X      */
X      if (image->colors > 0)
X        for (y=0; y < image->rows; y++)
X        {
X          for (x=0; x < image->columns; x++)
X          {
X            pixel=XGetPixel(&ximage,x,y);
X            index=(pixel >> red_shift) & red_mask;
X            red=(unsigned char) (colors[index].red >> 8);
X            index=(pixel >> green_shift) & green_mask;
X            green=(unsigned char) (colors[index].green >> 8);
X            index=(pixel >> blue_shift) & blue_mask;
X            blue=(unsigned char) (colors[index].blue >> 8);
X            if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
X                (p->length < MaxRunlength))
X                p->length++;
X              else
X                {
X                  if (image->packets > 0)
X                    p++;
X                  image->packets++;
X                  p->red=red;
X                  p->green=green;
X                  p->blue=blue;
X                  p->index=0;
X                  p->length=0;
X                }
X          }
X        }
X      else
X        for (y=0; y < image->rows; y++)
X          for (x=0; x < image->columns; x++)
X          {
X            pixel=XGetPixel(&ximage,x,y);
X            color=(pixel >> red_shift) & red_mask;
X            red=(unsigned char)
X              ((((unsigned long) color*65535)/red_mask) >> 8);
X            color=(pixel >> green_shift) & green_mask;
X            green=(unsigned char)
X              ((((unsigned long) color*65535)/green_mask) >> 8);
X            color=(pixel >> blue_shift) & blue_mask;
X            blue=(unsigned char)
X              ((((unsigned long) color*65535)/blue_mask) >> 8);
X            if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
X                (p->length < MaxRunlength))
X                p->length++;
X              else
X                {
X                  if (image->packets > 0)
X                    p++;
X                  image->packets++;
X                  p->red=red;
X                  p->green=green;
X                  p->blue=blue;
X                  p->index=0;
X                  p->length=0;
X                }
X          }
X      break;
X    }
X    case PseudoClass:
X    {
X      /*
X        Convert X image to PseudoClass packets.
X      */
X      image->colormap=(ColorPacket *)
X        malloc((unsigned) (image->colors*sizeof(ColorPacket)));
X      if (image->colormap == (ColorPacket *) NULL)
X        Error("unable to allocate memory",(char *) NULL);
X      for (i=0; i < image->colors; i++)
X      {
X        image->colormap[i].red=colors[i].red >> 8;
X        image->colormap[i].green=colors[i].green >> 8;
X        image->colormap[i].blue=colors[i].blue >> 8;
X      }
X      for (y=0; y < image->rows; y++)
X        for (x=0; x < image->columns; x++)
X        {
X          pixel=XGetPixel(&ximage,x,y);
X          red=(unsigned char) (colors[pixel].red >> 8);
X          green=(unsigned char) (colors[pixel].green >> 8);
X          blue=(unsigned char) (colors[pixel].blue >> 8);
X          if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
X              (p->length < MaxRunlength))
X              p->length++;
X            else
X              {
X                if (image->packets > 0)
X                  p++;
X                image->packets++;
X                p->red=red;
X                p->green=green;
X                p->blue=blue;
X                p->index=(unsigned short) pixel;
X                p->length=0;
X              }
X        }
X      break;
X    }
X  }
X  /*
X    Free image and colormap.
X  */
X  (void) free((char *) window_name);
X  if (header.ncolors > 0)
X    (void) free((char *) colors);
X  (void) free((char *) ximage.data);
X  if (image->file != stdin)
X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
X      (void) fclose(image->file);
X    else
X      (void) pclose(image->file);
X  if (image->packets > ((image->columns*image->rows*3) >> 2))
X    image->compression=NoCompression;
X  image->pixels=(RunlengthPacket *)
X    realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
X  return(image);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%   U s a g e                                                                 %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Procedure Usage displays the program usage;
%
%  The format of the Usage routine is:
%
%      Usage(message)
%
%  A description of each parameter follows:
%
%    message:  Specifies a specific message to display to the user.
%
*/
static void Usage(message)
char
X  *message;
{
X  if (message != (char *) NULL)
X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
X  (void) fprintf(stderr,"Usage: %s [-scene #] image.xwd image.miff\n\n",
X    application_name);
X  (void) fprintf(stderr,"Specify 'image.xwd' as '-' for standard input.\n");
X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard output.\n");
X  exit(1);
}
X
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%                                                                             %
%  M a i n                                                                    %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
int main(argc,argv)
int
X  argc;
X   
char
X  *argv[];
{
X  char
X    filename[256];
X
X  Image
X    *image;
X
X  int
X    i;
X
X  unsigned int
X    scene;
X
X  /*
X    Initialize program variables.
X  */
X  application_name=argv[0];
X  i=1;
X  scene=0;
X  if (argc < 3)
X    Usage((char *) NULL);
X  /*
X    Read image and convert to MIFF format.
X  */
X  if (strncmp(argv[i],"-scene",2) == 0)
X    {
X      i++;
X      scene=atoi(argv[i++]);
X    }
X  (void) strcpy(filename,argv[i++]);
X  image=ReadXWDImage(filename);
X  if (image == (Image *) NULL)
X    exit(1);
X  (void) strcpy(image->filename,argv[i++]);
X  image->scene=scene;
X  (void) WriteImage(image);
X  (void) fprintf(stderr,"%s => %s  %dx%d\n",filename,image->filename,
X    image->columns,image->rows);
X  DestroyImage(image);
X  return(False);
}
SHAR_EOF
chmod 0755 ImageMagick/filters/XWDtoMIFF.c ||
echo 'restore of ImageMagick/filters/XWDtoMIFF.c failed'
Wc_c="`wc -c < 'ImageMagick/filters/XWDtoMIFF.c'`"
test 22134 -eq "$Wc_c" ||
	echo 'ImageMagick/filters/XWDtoMIFF.c: original size 22134, current size' "$Wc_c"
rm -f _shar_wnt_.tmp
fi
# ============= ImageMagick/filters/Imakefile ==============
if test -f 'ImageMagick/filters/Imakefile' -a X"$1" != X"-c"; then
	echo 'x - skipping ImageMagick/filters/Imakefile (File already exists)'
	rm -f _shar_wnt_.tmp
else
> _shar_wnt_.tmp
echo 'x - extracting ImageMagick/filters/Imakefile (Text)'
sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/Imakefile' &&
EXTRA_INCLUDES= -I/usr/local/include -I..
SHAR_EOF
true || echo 'restore of ImageMagick/filters/Imakefile failed'
fi
echo 'End of ImageMagick part 13'
echo 'File ImageMagick/filters/Imakefile is continued in part 14'
echo 14 > _shar_seq_.tmp
exit 0
--
Dan Heller
O'Reilly && Associates       Z-Code Software    Comp-sources-x:
Senior Writer                President          comp-sources-x at uunet.uu.net
argv at ora.com                 argv at zipcode.com



More information about the Comp.sources.x mailing list