/* pnmenlarge.c - read a portable anymap and enlarge it N times
**
** 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 "pnm.h"

int
main(int argc, char * argv[]) {

    FILE* ifP;
    xel* xelrow;
    xel* newxelrow;
    int argn, n, rows, cols, format;
    xelval maxval;
    bool eof;
    const char* const usage = "N [pnmfile]";

    pnm_init(&argc, argv);

    argn = 1;
    
    if ( argn == argc )
        pm_usage( usage );
    if ( sscanf( argv[argn], "%d", &n ) != 1 )
        pm_usage( usage );
    if ( n < 2 )
        pm_error( "N must be greater than 1" );
    ++argn;

    if (argn != argc) {
        ifP = pm_openr( argv[argn] );
        ++argn;
    }
    else
        ifP = stdin;

    if ( argn != argc )
        pm_usage( usage );
    
    eof = FALSE;
    while (!eof) {
        int row;

        pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);
        xelrow = pnm_allocrow(cols);
        pnm_writepnminit(stdout, cols * n, rows * n, maxval, format, 0);
        newxelrow = pnm_allocrow(cols * n);
        
        for (row = 0; row < rows; ++row) {
            int subrow;
            pnm_readpnmrow(ifP, xelrow, cols, maxval, format);
            for (subrow = 0; subrow < n; ++subrow) {
                int col;
                for (col = 0; col < cols; ++col) {
                    int subcol;
                    for (subcol = 0; subcol < n; ++subcol)
                        newxelrow[col * n + subcol] = xelrow[col];
                }
                pnm_writepnmrow(stdout, newxelrow, cols * n, maxval, 
                                format, 0);
            }
        }
        
        pnm_freerow(newxelrow);
        pnm_freerow(xelrow);

        pnm_nextimage(ifP, &eof);
    }

    pm_close(ifP);
    pm_close(stdout);

    return 0;
}

