File: pgmramp8.c

package info (click to toggle)
grunch 1.3-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 204 kB
  • ctags: 25
  • sloc: sh: 830; ansic: 155; perl: 91; makefile: 69
file content (103 lines) | stat: -rw-r--r-- 2,321 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* pgmramp.c - generate a grayscale ramp
**
** 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 "pgm.h"

int
main( argc, argv )
int argc;
char *argv[];
    {
    gray *grayrow;
    register gray *gP;
    int rows, cols, rowso2, colso2, row;
    register int col;
    int ramptype;
#define RT_LR 1
#define RT_TB 2
#define RT_RECT 3
#define RT_ELLIP 4
    char *usage = "-lr|-tb|-rectangle|-ellipse <width> <height>";


    pgm_init( &argc, argv );

    if ( argc != 4 )
	pm_usage( usage );

    if ( pm_keymatch( argv[1], "-lr", 2 ) )
	ramptype = RT_LR;
    else if ( pm_keymatch( argv[1], "-tb", 2 ) )
	ramptype = RT_TB;
    else if ( pm_keymatch( argv[1], "-rectangle", 2 ) )
	ramptype = RT_RECT;
    else if ( pm_keymatch( argv[1], "-ellipse", 2 ) )
	ramptype = RT_ELLIP;
    else
	pm_usage( usage );
    
    if ( sscanf( argv[2], "%d", &cols ) != 1 )
	pm_usage( usage );
    if ( sscanf( argv[3], "%d", &rows ) != 1 )
	pm_usage( usage );

    colso2 = cols / 2;
    rowso2 = rows / 2;

    pgm_writepgminit( stdout, cols, rows, 255, 0 );
    grayrow = pgm_allocrow( cols );

    for ( row = 0; row < rows; ++row )
	{
	for ( col = 0, gP = grayrow; col < cols; ++col, ++gP )
	    {
	    switch ( ramptype )
		{
		case RT_LR:
		*gP = col * 255 / ((cols == 1) ? 1 : (cols - 1));
		break;

		case RT_TB:
		*gP = row * 255 / ((rows == 1) ? 1 : (rows - 1));
		break;

		case RT_RECT:
		{
		float r, c;
		r = abs( rowso2 - row ) / (float) rowso2;
		c = abs( colso2 - col ) / (float) colso2;
		*gP = 255 - ( r + c ) / 2.0 * 255;
		}
		break;

		case RT_ELLIP:
		{
		float r, c, v;
		r = abs( rowso2 - row ) / (float) rowso2;
		c = abs( colso2 - col ) / (float) colso2;
		v = r * r + c * c;
		if ( v < 0.0 ) v = 0.0;
		else if ( v > 1.0 ) v = 1.0;
		*gP = 255 - v * 255;
		}
		break;

		default:
		pm_error( "can't happen" );
		}
	    }
	pgm_writepgmrow( stdout, grayrow, cols, 255, 0 );
	}

    pm_close( stdout );
    exit( 0 );
    }