about summary refs log tree commit diff
path: root/converter/other/sirtopnm.c
blob: fafcc91396edb96aace7687f797b47e3b4e916a2 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* sirtopnm.c - read a Solitaire Image Recorder file and write a portable anymap
**
** Copyright (C) 1991 by Marvin Landis.
**
** 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 "pnm.h"

int main( argc, argv )
int argc;
char* argv[];
{
    FILE *ifp;
    xel *xelrow, *xP;
    unsigned char *sirarray;
    int rows, cols, row, format, picsize, planesize;
    register int col, i;
    short info;

    pnm_init( &argc, argv );

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

    if ( argc == 2 )
	ifp = pm_openr( argv[1] );
    else
	ifp = stdin;

    pm_readlittleshort( ifp, &info );
    if ( info != 0x3a4f)
	pm_error( "Input file is not a Solitaire file" );
    pm_readlittleshort( ifp, &info );
    pm_readlittleshort( ifp, &info );
    if ( info == 17 )
    {
	format = PGM_TYPE;
    }
    else if ( info == 11 )
    {
	format = PPM_TYPE;
    }
    else
	pm_error( "Input is not MGI TYPE 11 or MGI TYPE 17" );
    pm_readlittleshort( ifp, &info );
    cols = (int) ( info );
    pm_readlittleshort( ifp, &info );
    rows = (int) ( info );
    for ( i = 1; i < 1531; i++ )
	pm_readlittleshort( ifp, &info );

    pnm_writepnminit( stdout, cols, rows, 255, format, 0 );
    xelrow = pnm_allocrow( cols );
    switch ( PNM_FORMAT_TYPE(format) )
    {
	case PGM_TYPE:
            pm_message( "Writing a PGM file" );
	    for ( row = 0; row < rows; ++row )
	    {
	        for ( col = 0, xP = xelrow; col < cols; col++, xP++ )
	        	PNM_ASSIGN1( *xP, fgetc( ifp ) );
	        pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
	    }
	    break;
	case PPM_TYPE:
	    picsize = cols * rows * 3;
	    planesize = cols * rows;
            if ( !( sirarray = (unsigned char*) malloc( picsize ) ) ) 
	        pm_error( "Not enough memory to load SIR file" );
	    if ( fread( sirarray, 1, picsize, ifp ) != picsize )
	        pm_error( "Error reading SIR file" );
            pm_message( "Writing a PPM file" );
            for ( row = 0; row < rows; row++ )
	    {
	        for ( col = 0, xP = xelrow; col < cols; col++, xP++ )
        	    PPM_ASSIGN( *xP, sirarray[row*cols+col],
				 sirarray[planesize + (row*cols+col)],
				 sirarray[2*planesize + (row*cols+col)] );
                pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
	    }
	    break;
	default:
	    pm_error( "Shouldn't happen" );
    }

    pm_close( ifp );

    exit( 0 );
}