home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
309.lha
/
PBM_PLUS
/
pnm
/
pnmflip.c
< prev
next >
Wrap
C/C++ Source or Header
|
1980-12-04
|
5KB
|
188 lines
/* pnmflip.c - perform one or more flip operations on a portable anymap
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#include <stdio.h>
#ifdef SYSV
#include <string.h>
#else SYSV
#include <strings.h>
#endif SYSV
#include "pnm.h"
#define max(a,b) ((a) > (b) ? (a) : (b))
main( argc, argv )
int argc;
char *argv[];
{
FILE *ifd;
xel **xels, **transpose();
void leftright(), topbottom();
int argn, rows, cols, format;
xelval maxval;
char *usage = "[-leftright|-lr] [-topbottom|-tb] [-transpose|-xy]\n [-rotate90|-r90|-ccw] [-rotate270|r270|-cw]\n [-rotate180|-r180] [pnmfile]";
pm_progname = argv[0];
argn = 1;
/* Just check the validity of arguments here. */
while ( argn < argc && argv[argn][0] == '-' )
{
if ( strncmp(argv[argn],"-lr",max(strlen(argv[argn]),2)) == 0 ||
strncmp(argv[argn],"-leftright",max(strlen(argv[argn]),2)) == 0 )
{ }
else if ( strncmp(argv[argn],"-tb",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-topbottom",max(strlen(argv[argn]),3)) == 0 )
{ }
else if ( strncmp(argv[argn],"-xy",max(strlen(argv[argn]),2)) == 0 ||
strncmp(argv[argn],"-transpose",max(strlen(argv[argn]),3)) == 0 )
{ }
else if ( strncmp(argv[argn],"-r90",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-rotate90",max(strlen(argv[argn]),8)) == 0 ||
strncmp(argv[argn],"-ccw",max(strlen(argv[argn]),3)) == 0 )
{ }
else if ( strncmp(argv[argn],"-r270",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-rotate270",max(strlen(argv[argn]),8)) == 0 ||
strncmp(argv[argn],"-cw",max(strlen(argv[argn]),3)) == 0 )
{ }
else if ( strncmp(argv[argn],"-r180",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-rotate180",max(strlen(argv[argn]),8)) == 0 )
{ }
else
pm_usage( usage );
argn++;
}
if ( argn != argc )
{
ifd = pm_openr( argv[argn] );
argn++;
}
else
ifd = stdin;
if ( argn != argc )
pm_usage( usage );
xels = pnm_readpnm( ifd, &cols, &rows, &maxval, &format );
pm_close( ifd );
/* Now go through the flags again, this time executing them. */
argn = 1;
while ( argn < argc && argv[argn][0] == '-' )
{
if ( strncmp(argv[argn],"-lr",max(strlen(argv[argn]),2)) == 0 ||
strncmp(argv[argn],"-leftright",max(strlen(argv[argn]),2)) == 0 )
leftright( xels, rows, cols );
else if ( strncmp(argv[argn],"-tb",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-topbottom",max(strlen(argv[argn]),3)) == 0 )
topbottom( xels, rows, cols );
else if ( strncmp(argv[argn],"-xy",max(strlen(argv[argn]),2)) == 0 ||
strncmp(argv[argn],"-transpose",max(strlen(argv[argn]),3)) == 0 )
xels = transpose( xels, &rows, &cols );
else if ( strncmp(argv[argn],"-r90",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-rotate90",max(strlen(argv[argn]),8)) == 0 ||
strncmp(argv[argn],"-ccw",max(strlen(argv[argn]),3)) == 0 )
{
xels = transpose( xels, &rows, &cols );
topbottom( xels, rows, cols );
}
else if ( strncmp(argv[argn],"-r270",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-rotate270",max(strlen(argv[argn]),8)) == 0 ||
strncmp(argv[argn],"-cw",max(strlen(argv[argn]),3)) == 0 )
{
xels = transpose( xels, &rows, &cols );
leftright( xels, rows, cols );
}
else if ( strncmp(argv[argn],"-r180",max(strlen(argv[argn]),3)) == 0 ||
strncmp(argv[argn],"-rotate180",max(strlen(argv[argn]),8)) == 0 )
{
leftright( xels, rows, cols );
topbottom( xels, rows, cols );
}
else
pm_error( "shouldn't happen!", 0,0,0,0,0 );
argn++;
}
/* All done. */
pnm_writepnm( stdout, xels, cols, rows, maxval, format );
exit( 0 );
}
void
leftright( xels, rows, cols )
xel **xels;
int rows, cols;
{
int row;
register int col;
register xel *x1P, *x2P;
xel p;
for ( row = 0; row < rows; row++ )
for ( col = 0, x1P = xels[row], x2P = &(xels[row][cols-1]);
col < cols / 2;
col++, x1P++, x2P-- )
{
p = *x1P;
*x1P = *x2P;
*x2P = p;
}
}
void
topbottom( xels, rows, cols )
xel **xels;
int rows, cols;
{
int row;
register int col;
register xel *x1P, *x2P;
xel p;
for ( row = 0; row < rows / 2; row++ )
for ( col = 0, x1P = xels[row], x2P = xels[rows - 1 - row];
col < cols;
col++, x1P++, x2P++ )
{
p = *x1P;
*x1P = *x2P;
*x2P = p;
}
}
xel **
transpose( xels, rowsP, colsP )
xel **xels;
int *rowsP, *colsP;
{
int row, t;
register int col;
register xel **newxels, *xP;
newxels = pnm_allocarray( *rowsP, *colsP ); /* note parameter reversal */
for ( row = 0; row < *rowsP; row++ )
for ( col = 0, xP = xels[row]; col < *colsP; col++, xP++ )
newxels[col][row] = *xP; /* reversal here too */
pnm_freearray( xels, *rowsP );
t = *rowsP;
*rowsP = *colsP;
*colsP = t;
return newxels;
}