Beispiel #1
0
static void
cropOneImage(struct cmdlineInfo const cmdline,
             FILE *             const ifP,
             FILE *             const bdfP,
             FILE *             const ofP) {
/*----------------------------------------------------------------------------
   Crop the image to which the stream *ifP is presently positioned
   and write the results to *ofP.  If bdfP is non-null, use the image
   to which stream *bdfP is presently positioned as the borderfile
   (the file that tells us where the existing borders are in the input
   image).  Leave *ifP and *bdfP positioned after the image.

   Both files are seekable.
-----------------------------------------------------------------------------*/
    xelval maxval, bmaxval;
    int format, bformat;
    int rows, cols, brows, bcols;
    bool hasBorders;
    borderSet oldBorder;
        /* The sizes of the borders in the input image */
    cropSet crop;
        /* The crops we have to do on each side */
    xel background;

    pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);

    if (bdfP)
        pnm_readpnminit(bdfP, &bcols, &brows, &bmaxval, &bformat);

    if (bdfP)
        analyzeImage(bdfP, bcols, brows, bmaxval, bformat, cmdline.background,
                     FILEPOS_END,
                     &background, &hasBorders, &oldBorder);
    else
        analyzeImage(ifP, cols, rows, maxval, format, cmdline.background,
                     FILEPOS_BEG,
                     &background, &hasBorders, &oldBorder);

    if (cmdline.verbose) {
        pixel const backgroundPixel = pnm_xeltopixel(background, format);
        pm_message("Background color is %s", 
                   ppm_colorname(&backgroundPixel, maxval, TRUE /*hexok*/));
    }
    if (!hasBorders)
        pm_error("The image is entirely background; "
                 "there is nothing to crop.");

    determineCrops(cmdline, &oldBorder, &crop);

    validateComputableSize(cols, rows, crop);

    if (cmdline.verbose) 
        reportCroppingParameters(crop);

    if (PNM_FORMAT_TYPE(format) == PBM_TYPE)
        writeCroppedPBM(ifP, cols, rows, format, crop, background, ofP);
    else
        writeCroppedNonPbm(ifP, cols, rows, maxval, format, crop,
                           background, ofP);
}
Beispiel #2
0
/*-----------------------------------------------------------------------------
 *                                        Read the pnm image file header.
 */
static void
read_pnm_header()
{
    pnm_readpnminit(fp, &width, &height, &maxval, &format);
    switch (format) {
    case PBM_FORMAT:
        VPRINTF(stderr, "Image type: plain pbm format\n");
        break;
    case RPBM_FORMAT:
        VPRINTF(stderr, "Image type: raw pbm format\n");
        break;
    case PGM_FORMAT:
        VPRINTF(stderr, "Image type: plain pgm format\n");
        break;
    case RPGM_FORMAT:
        VPRINTF(stderr, "Image type: raw pgm format\n");
        break;
    case PPM_FORMAT:
        VPRINTF(stderr, "Image type: plain ppm format\n");
        break;
    case RPPM_FORMAT:
        VPRINTF(stderr, "Image type: raw ppm format\n");
        break;
    }
    VPRINTF(stderr, "Full image: %dx%d\n", width, height);
    VPRINTF(stderr, "Maxval:     %d\n", maxval);
    if (do_alpha)
        VPRINTF(stderr, "Computing alpha channel...\n");
}
Beispiel #3
0
static void
extract_one_image(FILE *infile, const char outputfilename[]) {

    FILE *outfile;
    xelval maxval;
    int rows, cols, format;
    enum pm_check_code check_retval;
    
    int row;
    xel *xelrow;

    pnm_readpnminit(infile, &cols, &rows, &maxval, &format);

    pnm_check(infile, PM_CHECK_BASIC, format, cols, rows, maxval, 
              &check_retval);

    outfile = pm_openw(outputfilename);
    pnm_writepnminit(outfile, cols, rows, maxval, format, 0);

    xelrow = pnm_allocrow(cols);
    for (row = 0; row < rows; row++) {
        pnm_readpnmrow(infile, xelrow, cols, maxval, format);
        pnm_writepnmrow(outfile, xelrow, cols, maxval, format, 0);
    }
    pnm_freerow(xelrow);
    pm_close(outfile);
}
int
main(int argc, const char ** argv) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    int cols, rows;
    xelval maxval;
    int format;
    unsigned int histWidth;
    unsigned int range;
    unsigned int hmax;
    xelval startval, endval;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);

    startval = cmdline.lval;
    endval   = MIN(maxval, cmdline.rval) + 1;

    range = endval - startval;

    if (cmdline.widthSpec)
        histWidth = cmdline.width;
    else
        histWidth = range;

    reportScale(histWidth, range, cmdline.verbose);
    if (cmdline.nmaxSpec)
        hmax = cols * rows / histWidth * cmdline.nmax;

    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        ppmHist(ifP, cols, rows, maxval, format,
                cmdline.dots, cmdline.white, cmdline.black,
                cmdline.colorWanted,
                cmdline.verbose, startval, endval,
                histWidth, cmdline.height, cmdline.nmaxSpec, hmax);
        break;
    case PGM_TYPE:
        pgmHist(ifP, cols, rows, maxval, format,
                cmdline.dots, cmdline.white, cmdline.black,
                cmdline.verbose, startval, endval,
                histWidth, cmdline.height, cmdline.nmaxSpec, hmax);
        break;
    case PBM_TYPE:
        pm_error("Cannot do a histogram of a a PBM file");
        break;
    }
    pm_close(ifP);

    return 0;
}
/** load image info - not the data */
void GeoImage::load()
{
  QString fname = filename();
  qDebug("GeoImage::load(%s)", fname.toLatin1().constData());
  FILE *fp;
  fp = fopen(fname.toLatin1().constData(), "r");
  if (!fp) {
    qDebug("#  (ERROR)GeoImage::load(%s) Can't open file for reading!",
           fname.toLatin1().constData());

    throw FileIOException(FileIOException::FILE_NOT_EXISTS, fname);
  }
  int pxmtype;
  xelval max_x;
  freeData();
  float minval_, maxval_;

  if (pfm_readpfm_header(fp, &cols_, &rows_, &minval_, &maxval_, &pxmtype))
    type_ = (IMGTYPE) pxmtype;  //0 = FALSE, 1 = TRUE
  else {                        /* now check for all the other p?m-formats */
    pnm_readpnminit(fp, &cols_, &rows_, &max_x, &pxmtype);
    rewind(fp);
    minval_ = 0.0;
    maxval_ = (float) max_x;
    if (pxmtype == PBM_FORMAT || pxmtype == RPBM_FORMAT)
      type_ = PBM;
    if (pxmtype == PGM_FORMAT || pxmtype == RPGM_FORMAT)
      type_ = PGM;
    if (pxmtype == PPM_FORMAT || pxmtype == RPPM_FORMAT)
      type_ = PPM;
  }
  fclose(fp);
  qDebug("##  Image: (%d, %d) %f - %f, type: %d",
         cols_, rows_, minval_, maxval_, type_);
  if (type_ == UNKNOWN)
    qDebug("##  (ERROR) unknown image type!");
#ifdef WIN32
  replace("size_x", &QString::number(cols_));
  replace("size_y", &QString::number(rows_));
  resolutionX_ = (geoEast_ - geoWest_) / cols_;
  replace("res_x", &QString::number(resolutionX_));
  resolutionY_ = (geoNorth_ - geoSouth_) / rows_;
  replace("res_y", &QString::number(resolutionY_));
#else
  replace("size_x", cols_);
  replace("size_y", rows_);
  resolutionX_ = (geoEast_ - geoWest_) / cols_;
  replace("res_x", resolutionX_);
  resolutionY_ = (geoNorth_ - geoSouth_) / rows_;
  replace("res_y", resolutionY_);
#endif
}
Beispiel #6
0
int
main(int argc, char *argv[]) {

    FILE* ifp;
    xel* xelrow;  /* Row from input image */
    xel* output_row;  /* Row of output image */
    xelval maxval;
    int rows, cols, format, row;
    int leftcol, rightcol, toprow, bottomrow;
    int output_cols;  /* Width of output image */
    struct cmdline_info cmdline;

    pnm_init( &argc, argv );

    parse_command_line(argc, argv, &cmdline);

    ifp = pm_openr(cmdline.input_filespec);

    pnm_readpnminit(ifp, &cols, &rows, &maxval, &format);
    xelrow = pnm_allocrow(cols);

    black_xel = pnm_blackxel(maxval, format);

    compute_cut_bounds(cols, rows, 
                       cmdline.left, cmdline.right, 
                       cmdline.top, cmdline.bottom, 
                       cmdline.width, cmdline.height, 
                       &leftcol, &rightcol, &toprow, &bottomrow);

    if (!cmdline.pad)
        reject_out_of_bounds(cols, rows, leftcol, rightcol, toprow, bottomrow);

    if (cmdline.verbose) {
        pm_message("Image goes from Row 0, Column 0 through Row %d, Column %d",
                   rows-1, cols-1);
        pm_message("Cutting from Row %d, Column %d through Row %d Column %d",
                   toprow, leftcol, bottomrow, rightcol);
    }

    output_cols = rightcol-leftcol+1;
    output_row = pnm_allocrow(output_cols);
    
    pnm_writepnminit(stdout, output_cols, bottomrow-toprow+1, 
                     maxval, format, 0 );

    /* Implementation note:  If speed is ever an issue, we can probably
       speed up significantly the non-padding case by writing a special
       case loop here for the case cmdline.pad == FALSE.
       */

    /* Write out top padding */
    write_black_rows(stdout, 0 - toprow, output_cols, output_row, 
                     maxval, format);
    
    /* Read input and write out rows extracted from it */
    for (row = 0; row < rows; row++) {
        pnm_readpnmrow(ifp, xelrow, cols, maxval, format);
        if (row >= toprow && row <= bottomrow) {
            int col;
            /* Put in left padding */
            for (col = leftcol; col < 0; col++) { 
                output_row[col-leftcol] = black_xel;
            }
            /* Put in extracted columns */
            for (col = MAX(leftcol, 0); col <= MIN(rightcol, cols-1); col++) {
                output_row[col-leftcol] = xelrow[col];
            }
            /* Put in right padding */
            for (col = MAX(cols, leftcol); col <= rightcol; col++) {
                output_row[col-leftcol] = black_xel;
            }
            pnm_writepnmrow(stdout, output_row, output_cols, 
                            maxval, format, 0);
        }
    }
    /* Note that we may be tempted just to quit after reaching the bottom
       of the extracted image, but that would cause a broken pipe problem
       for the process that's feeding us the image.
       */
    /* Write out bottom padding */
    write_black_rows(stdout, bottomrow - (rows-1), output_cols, output_row, 
                     maxval, format);

    pnm_freerow(output_row);
    pnm_freerow(xelrow);
    pm_close(ifp);
    pm_close(stdout);
    
    exit( 0 );
}
Beispiel #7
0
int
main(int argc, char * argv[]) {
    FILE* ifp;
    xel* xelrow;
    xel* newxelrow;
    xel bgxel;
    int rows, cols, format; 
    int newformat, newcols; 
    int row;
    xelval maxval, newmaxval;
    double shearfac;

    struct cmdline_info cmdline;

    pnm_init( &argc, argv );

    parse_command_line( argc, argv, &cmdline );

    ifp = pm_openr( cmdline.input_filespec );

    pnm_readpnminit( ifp, &cols, &rows, &maxval, &format );
    xelrow = pnm_allocrow( cols );

    /* Promote PBM files to PGM. */
    if ( !cmdline.noantialias && PNM_FORMAT_TYPE(format) == PBM_TYPE ) {
        newformat = PGM_TYPE;
        newmaxval = PGM_MAXMAXVAL;
        pm_message( "promoting from PBM to PGM - "
                    "use -noantialias to avoid this" );
    } else {
        newformat = format;
        newmaxval = maxval;
    }

    shearfac = tan( cmdline.angle );
    if ( shearfac < 0.0 )
        shearfac = -shearfac;

    if(rows * shearfac >= INT_MAX-1)
    	pm_error("image too large");
    
    overflow_add(rows * shearfac, cols+1);
    
    newcols = rows * shearfac + cols + 0.999999;

    pnm_writepnminit( stdout, newcols, rows, newmaxval, newformat, 0 );
    newxelrow = pnm_allocrow( newcols );

    bgxel = pnm_backgroundxelrow( xelrow, cols, newmaxval, format );

    for ( row = 0; row < rows; ++row ) {
        double shearCols;

        pnm_readpnmrow( ifp, xelrow, cols, newmaxval, format );

        if ( cmdline.angle > 0.0 )
            shearCols = row * shearfac;
        else
            shearCols = ( rows - row ) * shearfac;

        shear_row(xelrow, cols, newxelrow, newcols, 
                  shearCols, format, bgxel, !cmdline.noantialias);

        pnm_writepnmrow( stdout, newxelrow, newcols, newmaxval, newformat, 0 );
    }

    pm_close( ifp );
    pm_close( stdout );

    exit( 0 );
}
Beispiel #8
0
int
main(int           argc,
     const char ** argv) {

    struct cmdlineInfo cmdline;
    struct imgInfo * img;  /* malloc'ed array */
    xelval newmaxval;
    int newformat;
    unsigned int i;
    unsigned int newrows, newcols;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    MALLOCARRAY_NOFAIL(img, cmdline.nfiles);

    for (i = 0; i < cmdline.nfiles; ++i) {
        img[i].ifP = pm_openr(cmdline.inputFilespec[i]);
        pnm_readpnminit(img[i].ifP, &img[i].cols, &img[i].rows,
                        &img[i].maxval, &img[i].format);
    }

    computeOutputParms(cmdline.nfiles, cmdline.orientation, img,
                       &newcols, &newrows, &newmaxval, &newformat);

    pnm_writepnminit(stdout, newcols, newrows, newmaxval, newformat, 0);

    if (PNM_FORMAT_TYPE(newformat) == PBM_TYPE) {
        switch (cmdline.orientation) {
        case LEFTRIGHT:
            concatenateLeftRightPbm(stdout, cmdline.nfiles,
                                    newcols, newrows, cmdline.justification,
                                    img, cmdline.backcolor);
            break;
        case TOPBOTTOM:
            concatenateTopBottomPbm(stdout, cmdline.nfiles,
                                    newcols, newrows, cmdline.justification,
                                    img, cmdline.backcolor);
            break;
        }
    } else {
        switch (cmdline.orientation) {
        case LEFTRIGHT:
            concatenateLeftRightGen(stdout, cmdline.nfiles,
                                    newcols, newrows, newmaxval, newformat,
                                    cmdline.justification, img,
                                    cmdline.backcolor);
            break;
        case TOPBOTTOM:
            concatenateTopBottomGen(stdout, cmdline.nfiles,
                                    newcols, newrows, newmaxval, newformat,
                                    cmdline.justification, img,
                                    cmdline.backcolor);
            break;
        }
    }
    for (i = 0; i < cmdline.nfiles; ++i)
        pm_close(img[i].ifP);
    free(cmdline.inputFilespec);
    free(img);
    pm_close(stdout);

    return 0;
}
Beispiel #9
0
int main(int argc, char *argv[])
{
    FILE           *ifd;
    FILE       *ofd;
    int             rows, cols;
    xelval          maxval;
    int             format;
    const char     * const usage = "[-resolution x y] [pnmfile [ddiffile]]";
    int             i, j;
    char           *outfile;
    int       argn;
    int hor_resolution = 75;
    int ver_resolution = 75;
    imageparams ip;
    unsigned char  *data, *p;

    pnm_init(&argc, argv);

    for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) {
        int arglen = strlen(argv[argn]);

        if (!strncmp (argv[argn],"-resolution", arglen)) {
            if (argn + 2 < argc) {
                hor_resolution = atoi(argv[argn+1]);
                ver_resolution = atoi(argv[argn+2]);
                argn += 2;
                continue;
            } else {
                pm_usage(usage);
            }
        } else {
            pm_usage(usage);
        }
    }

    if (hor_resolution <= 0 || ver_resolution <= 0) {
        fprintf(stderr,"Unreasonable resolution values: %d x %d\n",
                hor_resolution,ver_resolution);
        exit(1);
    }

    if (argn == argc - 2) {
        ifd = pm_openr(argv[argn]);
        outfile = argv[argn+1];
        if (!(ofd = fopen(outfile,"wb"))) {
            perror(outfile);
            exit(1);
        }
    } else if (argn == argc - 1) {
        ifd = pm_openr(argv[argn]);
        ofd = stdout;
    } else {
        ifd = stdin;
        ofd = stdout;
    }

    pnm_readpnminit(ifd, &cols, &rows, &maxval, &format);

    ip.width = cols;
    ip.height = rows;
    ip.h_res = hor_resolution;
    ip.v_res = ver_resolution;

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
        ip.bits_per_pixel = 1;
        ip.bytes_per_line = (cols + 7) / 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 1;
        ip.polarity = 1;
        break;
    case PGM_TYPE:
        ip.bytes_per_line = cols;
        ip.bits_per_pixel = 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    case PPM_TYPE:
        ip.bytes_per_line = 3 * cols;
        ip.bits_per_pixel = 24;
        ip.spectral = 5;
        ip.components = 3;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    default:
        fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format);
        exit(1);
    }

    if (!write_header(ofd,&ip)) {
        perror("Writing header");
        exit(1);
    }

    if (!(p = data = (unsigned char*)  malloc(ip.bytes_per_line))) {
        perror("allocating line buffer");
        exit(1);
    }

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
    {
        bit            *pixels;
        int             mask;
        int             k;

        pixels = pbm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            pbm_readpbmrow(ifd, pixels, cols, format);
            mask = 0;
            p = data;
            for (j = 0, k = 0; j < cols; j++) {
                if (pixels[j] == PBM_BLACK) {
                    mask |= 1 << k;
                }
                if (k == 7) {
                    *p++ = mask;
                    mask = 0;
                    k = 0;
                } else {
                    k++;
                }
            }
            if (k != 7) {       /* Flush the rest of the column */
                *p = mask;
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
    }
    break;
    case PGM_TYPE:
    {
        gray          *pixels = pgm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            p = data;
            pgm_readpgmrow(ifd, pixels, cols, maxval, format);
            for (j = 0; j < cols; j++) {
                *p++ = (unsigned char) pixels[j];
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
        pgm_freerow(pixels);
    }
    break;
    case PPM_TYPE:
    {
        pixel          *pixels = ppm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            p = data;
            ppm_readppmrow(ifd, pixels, cols, maxval, format);
            for (j = 0; j < cols; j++) {
                *p++ = PPM_GETR(pixels[j]);
                *p++ = PPM_GETG(pixels[j]);
                *p++ = PPM_GETB(pixels[j]);
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
        ppm_freerow(pixels);
    }
    break;
    }

    pm_close(ifd);

    free(data);

    if (!write_trailer(ofd)) {
        perror("Writing trailer");
        exit(1);
    }

    if (fclose(ofd) == EOF) {
        perror("Closing output file");
        exit(1);
    };

    return(0);
}
Beispiel #10
0
int
main(int argc, const char * argv[]) {

    FILE * ifP;
    xel * xelrow;
    xel * newxelrow;
    xel bgxel;
    int rows, cols, format; 
    int newformat, newcols; 
    int row;
    xelval maxval, newmaxval;
    double shearfac;

    struct CmdlineInfo cmdline;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    pnm_readpnminit(ifP, &cols, &rows, &maxval, &format);
    xelrow = pnm_allocrow(cols);

    /* Promote PBM files to PGM. */
    if (!cmdline.noantialias && PNM_FORMAT_TYPE(format) == PBM_TYPE) {
        newformat = PGM_TYPE;
        newmaxval = PGM_MAXMAXVAL;
        pm_message("promoting from PBM to PGM - "
                   "use -noantialias to avoid this");
    } else {
        newformat = format;
        newmaxval = maxval;
    }

    shearfac = fabs(tan(cmdline.angle));

    newcols = rows * shearfac + cols + 0.999999;

    pnm_writepnminit(stdout, newcols, rows, newmaxval, newformat, 0);
    newxelrow = pnm_allocrow(newcols);
    
    for (row = 0; row < rows; ++row) {
        double shearCols;

        pnm_readpnmrow(ifP, xelrow, cols, newmaxval, format);

        if (row == 0)
            bgxel = backgroundColor(cmdline.background,
                                    xelrow, cols, newmaxval, format);

        if (cmdline.angle > 0.0)
            shearCols = row * shearfac;
        else
            shearCols = (rows - row) * shearfac;

        shearRow(xelrow, cols, newxelrow, newcols, 
                 shearCols, format, bgxel, !cmdline.noantialias);

        pnm_writepnmrow(stdout, newxelrow, newcols, newmaxval, newformat, 0);
    }
    
    pm_close(ifP);
    pm_close(stdout);

    return 0;
}
Beispiel #11
0
int
main(int argc, char *argv[]) {
    FILE           *ifd;
    FILE           *ofd;
    int             rows, cols;
    xelval          maxval;
    int             format;
    const char     * const usage = "[-resolution x y] [pnmfile [ddiffile]]";
    char           *outfile;
    int       argn;
    int hor_resolution = 75;
    int ver_resolution = 75;
    imageparams ip;

    pnm_init(&argc, argv);

    for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) {
        int arglen = strlen(argv[argn]);

        if (!strncmp (argv[argn],"-resolution", arglen)) {
            if (argn + 2 < argc) {
                hor_resolution = atoi(argv[argn+1]);
                ver_resolution = atoi(argv[argn+2]);
                argn += 2;
                continue;
            } else {
                pm_usage(usage);
            }
        } else {
            pm_usage(usage);
        }
    }

    if (hor_resolution <= 0 || ver_resolution <= 0) {
        fprintf(stderr,"Unreasonable resolution values: %d x %d\n",
                hor_resolution,ver_resolution);
        exit(1);
    }

    if (argn == argc - 2) {
        ifd = pm_openr(argv[argn]);
        outfile = argv[argn+1];
        if (!(ofd = fopen(outfile,"wb"))) {
            perror(outfile);
            exit(1);
        }
    } else if (argn == argc - 1) {
        ifd = pm_openr(argv[argn]);
        ofd = stdout;
    } else {
        ifd = stdin;
        ofd = stdout;
    }

    pnm_readpnminit(ifd, &cols, &rows, &maxval, &format);

    ip.width = cols;
    ip.height = rows;
    ip.h_res = hor_resolution;
    ip.v_res = ver_resolution;

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
        ip.bits_per_pixel = 1;
        ip.bytes_per_line = (cols + 7) / 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 1;
        ip.polarity = 1;
        break;
    case PGM_TYPE:
        ip.bytes_per_line = cols;
        ip.bits_per_pixel = 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    case PPM_TYPE:
        ip.bytes_per_line = 3 * cols;
        ip.bits_per_pixel = 24;
        ip.spectral = 5;
        ip.components = 3;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    default:
        fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format);
        exit(1);
    }

    if (!write_header(ofd,&ip)) {
        perror("Writing header");
        exit(1);
    }

    convertRaster(ifd, format, maxval, cols, rows, ofd, ip.bytes_per_line);

    pm_close(ifd);

    if (!write_trailer(ofd)) {
        perror("Writing trailer");
        exit(1);
    }

    if (fclose(ofd) == EOF) {
        perror("Closing output file");
        exit(1);
    };

    return(0);
}