home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / pnmshear.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  5KB  |  175 lines

  1. /* pnmshear.c - read a portable anymap and shear it by some angle
  2. **
  3. ** Copyright (C) 1989, 1991 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <math.h>
  14. #include "pnm.h"
  15. #ifndef M_PI
  16. #define M_PI    3.14159265358979323846
  17. #endif /*M_PI*/
  18.  
  19. #define SCALE 4096
  20. #define HALFSCALE 2048
  21.  
  22. int
  23. main( argc, argv )
  24.     int argc;
  25.     char* argv[];
  26.     {
  27.     FILE* ifp;
  28.     xel* xelrow;
  29.     register xel* newxelrow;
  30.     register xel* xP;
  31.     register xel* nxP;
  32.     xel bgxel, prevxel;
  33.     int argn, rows, cols, format, newformat, newcols, row, col;
  34.     xelval maxval;
  35.     int antialias;
  36.     float fangle, shearfac, new0;
  37.     int intnew0;
  38.     register long fracnew0, omfracnew0;
  39.     char* usage = "[-noantialias] <angle> [pnmfile]";
  40.  
  41.     pnm_init( &argc, argv );
  42.  
  43.     argn = 1;
  44.     antialias = 1;
  45.  
  46.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' &&
  47.      ( argv[argn][1] < '0' || argv[argn][1] > '9' ) )
  48.         {
  49.         if ( pm_keymatch( argv[argn], "-antialias", 2 ) )
  50.             antialias = 1;
  51.         else if ( pm_keymatch( argv[argn], "-noantialias", 2 ) )
  52.             antialias = 0;
  53.         else
  54.             pm_usage( usage );
  55.         ++argn;
  56.         }
  57.  
  58.     if ( argn == argc )
  59.     pm_usage( usage );
  60.     if ( sscanf( argv[argn], "%f", &fangle ) != 1 )
  61.     pm_usage( usage );
  62.     ++argn;
  63.     if ( fangle <= -90.0 || fangle >= 90.0 )
  64.     pm_error( "angle must be between -90 and 90" );
  65.     fangle = fangle * M_PI / 180.0;    /* convert to radians */
  66.     shearfac = tan( fangle );
  67.     if ( shearfac < 0.0 )
  68.     shearfac = -shearfac;
  69.  
  70.     if ( argn != argc )
  71.     {
  72.     ifp = pm_openr( argv[argn] );
  73.     ++argn;
  74.     }
  75.     else
  76.     ifp = stdin;
  77.  
  78.     if ( argn != argc )
  79.     pm_usage( usage );
  80.  
  81.     pnm_pbmmaxval = PNM_MAXMAXVAL;  /* use larger value for better results */
  82.     pnm_readpnminit( ifp, &cols, &rows, &maxval, &format );
  83.     xelrow = pnm_allocrow( cols );
  84.  
  85.     /* Promote PBM files to PGM. */
  86.     if ( antialias && PNM_FORMAT_TYPE(format) == PBM_TYPE )
  87.     {
  88.     newformat = PGM_TYPE;
  89.     pm_message( "promoting from PBM to PGM - use -noantialias to avoid this" );
  90.     }
  91.     else
  92.     newformat = format;
  93.  
  94.     newcols = rows * shearfac + cols + 0.999999;
  95.  
  96.     pnm_writepnminit( stdout, newcols, rows, maxval, newformat, 0 );
  97.     newxelrow = pnm_allocrow( newcols );
  98.  
  99.     bgxel = pnm_backgroundxelrow( xelrow, cols, maxval, format );
  100.  
  101.     for ( row = 0; row < rows; ++row )
  102.     {
  103.     pnm_readpnmrow( ifp, xelrow, cols, maxval, format );
  104.  
  105.     if ( fangle > 0.0 )
  106.         new0 = row * shearfac;
  107.     else
  108.         new0 = ( rows - row ) * shearfac;
  109.     intnew0 = (int) new0;
  110.  
  111.     if ( antialias )
  112.         {
  113.         fracnew0 = ( new0 - intnew0 ) * SCALE;
  114.         omfracnew0 = SCALE - fracnew0;
  115.  
  116.         for ( col = 0, nxP = newxelrow; col < newcols; ++col, ++nxP )
  117.         *nxP = bgxel;
  118.  
  119.         prevxel = bgxel;
  120.         for ( col = 0, nxP = &(newxelrow[intnew0]), xP = xelrow; col < cols; ++col, ++nxP, ++xP )
  121.         {
  122.         switch ( PNM_FORMAT_TYPE(format) )
  123.             {
  124.             case PPM_TYPE:
  125.             PPM_ASSIGN( *nxP,
  126.             ( fracnew0 * PPM_GETR(prevxel) + omfracnew0 * PPM_GETR(*xP) + HALFSCALE ) / SCALE,
  127.             ( fracnew0 * PPM_GETG(prevxel) + omfracnew0 * PPM_GETG(*xP) + HALFSCALE ) / SCALE,
  128.             ( fracnew0 * PPM_GETB(prevxel) + omfracnew0 * PPM_GETB(*xP) + HALFSCALE ) / SCALE );
  129.             break;
  130.  
  131.             default:
  132.             PNM_ASSIGN1( *nxP,
  133.             ( fracnew0 * PNM_GET1(prevxel) + omfracnew0 * PNM_GET1(*xP) + HALFSCALE ) / SCALE );
  134.             break;
  135.             }
  136.         prevxel = *xP;
  137.         }
  138.         if ( fracnew0 > 0 )
  139.         {
  140.         nxP = &(newxelrow[intnew0 + cols]);
  141.         switch ( PNM_FORMAT_TYPE(format) )
  142.             {
  143.             case PPM_TYPE:
  144.             PPM_ASSIGN( *nxP,
  145.             ( fracnew0 * PPM_GETR(prevxel) + omfracnew0 * PPM_GETR(bgxel) + HALFSCALE ) / SCALE,
  146.             ( fracnew0 * PPM_GETG(prevxel) + omfracnew0 * PPM_GETG(bgxel) + HALFSCALE ) / SCALE,
  147.             ( fracnew0 * PPM_GETB(prevxel) + omfracnew0 * PPM_GETB(bgxel) + HALFSCALE ) / SCALE );
  148.             break;
  149.  
  150.             default:
  151.             PNM_ASSIGN1( *nxP,
  152.             ( fracnew0 * PNM_GET1(prevxel) + omfracnew0 * PNM_GET1(bgxel) + HALFSCALE ) / SCALE );
  153.             break;
  154.             }
  155.         }
  156.         }
  157.     else
  158.         {
  159.         for ( col = 0, nxP = newxelrow; col < intnew0; ++col, ++nxP )
  160.         *nxP = bgxel;
  161.         for ( col = 0, xP = xelrow; col < cols; ++col, ++nxP, ++xP )
  162.         *nxP = *xP;
  163.         for ( col = intnew0 + cols; col < newcols; ++col, ++nxP )
  164.         *nxP = bgxel;
  165.         }
  166.  
  167.     pnm_writepnmrow( stdout, newxelrow, newcols, maxval, newformat, 0 );
  168.     }
  169.  
  170.     pm_close( ifp );
  171.     pm_close( stdout );
  172.  
  173.     exit( 0 );
  174.     }
  175.