about summary refs log tree commit diff
path: root/converter/pbm/pbmtoplot.c
blob: 8c9075b261f6a1c106f2e6f0ea692c4ea6a42728 (plain) (blame)
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
/* pbmtoplot.c - read a portable bitmap and produce a UNIX-format plot file.
**
** Copyright (C) 1990 by Arthur David Olson.
**
** 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>
#include "pbm.h"

static void puttwo ARGS((int i));
static void
puttwo( i )
    int i;
    {
    (void) putchar(i);
    (void) putchar(i >> 8);
    }

int
main( argc, argv )
    int argc;
    char* argv[];
    {
    FILE* ifp;
    register bit** bits;
    register int row, col, scol;
    int	rows, cols;


    pbm_init( &argc, argv );

    if ( argc > 2 )
	pm_usage( "[pbmfile]" );

    ifp = (argc == 2) ? pm_openr( argv[1] ) : stdin;

    bits = pbm_readpbm( ifp, &cols, &rows );

    pm_close( ifp );

    (void) putchar( 's' );
    puttwo( 0 );
    puttwo( 0 );
    puttwo( rows - 1 );
    puttwo( cols - 1 );
    for ( row = 0; row < rows; ++row )
	{
	for ( col = 0; col < cols; ++col )
	    {
	    if ( bits[row][col] == PBM_WHITE )
		continue;
	    scol = col;
	    while ( ++col < cols && bits[row][col] == PBM_BLACK )
		; /* nothing */
	    --col;
	    if ( col == scol )
		(void) putchar( 'p' );
	    else
		{
		(void) putchar( 'l' );
		puttwo( scol );
		puttwo( rows - 1 - row );
		}
	    puttwo( col );
	    puttwo( rows - 1 - row );
	    }
	}

    exit( 0 );
    }