--- OLD/pnm/pnmtops.c Thu Jan 1 00:00:00 1970 +++ NEW/pnm/pnmtops.c Thu Jan 1 00:00:00 1970 @@ -13,14 +13,28 @@ ** -nocenter option added November 1993 by Wolfgang Stuerzlinger, ** wrzl@gup.uni-linz.ac.at. ** +** -scale split into -xscale and -yscale, and -keepaspect added, +** October 2006 by der Mouse. +** */ - #include "pnm.h" #define MARGIN 0.95 -static void putinit ARGS(( char* name, int cols, int rows, int padright, int bps, float scale, int dpi, int pagewid, int pagehgt, int format, int turnflag, int turnokflag, int rleflag, int center )); +static void putinit ARGS(( + char* name, + int cols, int rows, + int padright, + int bps, + float xscale, float yscale, int splitscale, int keepaspect, + int dpi, + int pagewid, int pagehgt, + int format, + int turnflag, int turnokflag, + int rleflag, + int center, + int forcescale )); static void putitem ARGS(( void )); static void putxelval ARGS(( xelval xv )); static void putrest ARGS(( void )); @@ -41,16 +55,25 @@ int argn, turnflag, turnokflag, rleflag, center; int rows, cols, format, bps, padright, row, col; xelval maxval, nmaxval; - float scale, f; + float f; + int forcescale; + float xscale; + float yscale; + int splitscale; + int keepaspect; int dpi, pagewid, pagehgt; char name[100]; char* cp; - char* usage = "[-scale ] [-turn|-noturn] [-rle|-runlength] [-dpi ] [-width ] [-height ] [-rle|-runlength] [-center|-nocenter] [pnmfile]"; + char* usage = "[-scale ] [-force] [-turn|-noturn] [-rle|-runlength] [-dpi ] [-width ] [-height ] [-rle|-runlength] [-center|-nocenter] [pnmfile]"; pnm_init( &argc, argv ); argn = 1; - scale = 1.0; + xscale = 1.0; + yscale = 1.0; + splitscale = 0; + keepaspect = 0; + forcescale = 0; turnflag = 0; center = 1; turnokflag = 1; @@ -66,11 +89,35 @@ if ( pm_keymatch( argv[argn], "-scale", 2 ) ) { ++argn; - if ( argn == argc || sscanf( argv[argn], "%f", &scale ) != 1 ) + if ( argn == argc || sscanf( argv[argn], "%f", &f ) != 1 ) pm_usage( usage ); + xscale = f; + yscale = f; + splitscale = 0; } + else if (pm_keymatch(argv[argn],"-xscale",2)) + { argn ++; + if ((argn >= argc) || (sscanf(argv[argn],"%f",&f) != 1)) + { pm_usage(usage); + } + xscale = f; + splitscale = 1; + } + else if (pm_keymatch(argv[argn],"-yscale",2)) + { argn ++; + if ((argn >= argc) || (sscanf(argv[argn],"%f",&f) != 1)) + { pm_usage(usage); + } + yscale = f; + splitscale = 1; + } + else if (pm_keymatch(argv[argn],"-keepaspect",2)) + { keepaspect = 1; + } else if ( pm_keymatch( argv[argn], "-turn", 2 ) ) turnflag = 1; + else if ( pm_keymatch( argv[argn], "-force", 2 ) ) + forcescale = 1; else if ( pm_keymatch( argv[argn], "-noturn", 4 ) ) turnokflag = 0; else if ( pm_keymatch( argv[argn], "-center", 2 ) ) @@ -141,8 +188,9 @@ /* Compute padding to round cols * bps up to the nearest multiple of 8. */ padright = ( ( ( cols * bps + 7 ) / 8 ) * 8 - cols * bps ) / bps; - putinit(name, cols, rows, padright, bps, scale, dpi, pagewid, pagehgt, - format, turnflag, turnokflag, rleflag, center ); + putinit(name, cols, rows, padright, bps, + xscale, yscale, splitscale, keepaspect, dpi, pagewid, pagehgt, + format, turnflag, turnokflag, rleflag, center, forcescale ); for ( row = 0; row < rows; ++row ) { pnm_readpnmrow( ifp, xelrow, cols, maxval, format ); @@ -231,21 +279,28 @@ #if __STDC__ static void -putinit( char* name, int cols, int rows, int padright, int bps, float scale, +putinit( char* name, int cols, int rows, int padright, int bps, + float xscale, float yscale, int splitscale, int keepaspect, int dpi, int pagewid, int pagehgt, int format, int turnflag, - int turnokflag, int rleflag, int center ) + int turnokflag, int rleflag, int center, int forcescale ) #else /*__STDC__*/ static void -putinit( name, cols, rows, padright, bps, scale, dpi, pagewid, pagehgt, format, - turnflag, turnokflag, rleflag, center ) +putinit( name, cols, rows, padright, bps, xscale, yscale, + splitscale, keepaspect, dpi, pagewid, pagehgt, format, + turnflag, turnokflag, rleflag, center, forcescale ) char* name; int cols, rows, padright, bps; - float scale; + float xscale; + float yscale; + int splitscale; + int keepaspect; int dpi, pagewid, pagehgt, format, turnflag, turnokflag, rleflag, center; + int forcescale; #endif /*__STDC__*/ { int icols, irows, devpix; float pixfac, scols, srows, llx, lly; + float f; /* Turn? */ icols = cols; @@ -255,29 +310,42 @@ turnflag = 1; cols = irows; rows = icols; + f = xscale; + xscale = yscale; + yscale = f; } /* Figure out size. */ devpix = dpi / 72.0 + 0.5; /* device pixels per unit, approx. */ pixfac = 72.0 / dpi * devpix; /* 1, approx. */ - scols = scale * cols * pixfac; - srows = scale * rows * pixfac; - if ( scols > pagewid * MARGIN || srows > pagehgt * MARGIN ) + scols = xscale * cols * pixfac; + srows = yscale * rows * pixfac; + if (! splitscale) keepaspect = 1; + if (!forcescale && (scols > pagewid * MARGIN || srows > pagehgt * MARGIN)) { if ( scols > pagewid * MARGIN ) { - scale *= pagewid / scols * MARGIN; - scols = scale * cols * pixfac; - srows = scale * rows * pixfac; + xscale *= pagewid / scols * MARGIN; + if (keepaspect) yscale *= pagewid / scols * MARGIN; + scols = xscale * cols * pixfac; + srows = yscale * rows * pixfac; } if ( srows > pagehgt * MARGIN ) { - scale *= pagehgt / srows * MARGIN; - scols = scale * cols * pixfac; - srows = scale * rows * pixfac; + yscale *= pagehgt / srows * MARGIN; + if (keepaspect) xscale *= pagehgt / srows * MARGIN; + scols = xscale * cols * pixfac; + srows = yscale * rows * pixfac; } - pm_message( - "warning, image too large for page, rescaling to %g", scale ); + if (splitscale) + { pm_message( + "warning, image too large for page, rescaling to %g, %g", + xscale, yscale ); + } + else + { pm_message( + "warning, image too large for page, rescaling to %g", xscale); + } } llx = (center) ? ( pagewid - scols ) / 2 : 0; lly = (center) ? ( pagehgt - srows ) / 2 : 0;