Example #1
0
int
main(int argc, char * argv[] ) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    int format;
    struct pam colormapPam;
    struct pam outpam;
    tuple ** colormapRaster;
    tupletable2 colormap;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    computeColorMapFromInput(ifP,
                             cmdline.allcolors, cmdline.newcolors, 
                             cmdline.methodForLargest, 
                             cmdline.methodForRep,
                             &format, &colormapPam, &colormap);

    pm_close(ifP);

    colormapToImage(format, &colormapPam, colormap,
                    cmdline.sort, cmdline.square, &outpam, &colormapRaster);

    if (cmdline.verbose)
        pm_message("Generating %u x %u image", outpam.width, outpam.height);

    outpam.file = stdout;
    
    pnm_writepam(&outpam, colormapRaster);
    
    pnm_freetupletable2(&colormapPam, colormap);

    pnm_freepamarray(colormapRaster, &outpam);

    pm_close(stdout);

    return 0;
}
Example #2
0
int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    tuple ** tuples;
    struct pam pam;
    int ncolors;
    tupletable chv;
    tuplehash cht;
    struct ImageHeader tgaHeader;
    enum TGAbaseImageType baseImgType;
    bool withAlpha;
    const char *outName;

    pnm_init( &argc, argv );

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    computeOutName(cmdline, &outName);

    tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));
    pm_close(ifP);

    computeImageType_cht(&pam, cmdline, tuples, 
                         &baseImgType, &withAlpha, &chv, &cht, &ncolors);

    /* Do the Targa header */
    computeTgaHeader(&pam, baseImgType, withAlpha, !cmdline.norle,
                     ncolors, 0, outName, &tgaHeader);
    writeTgaHeader(tgaHeader);
    
    if (baseImgType == TGA_MAP_TYPE) {
        /* Write out the Targa colormap. */
        int i;
        for (i = 0; i < ncolors; ++i)
            putMapEntry(&pam, chv[i]->tuple, tgaHeader.CoSize);
    }

    writeTgaRaster(&pam, tuples, cht, baseImgType, withAlpha, 
                   !cmdline.norle, 0);

    if (cht)
        pnm_destroytuplehash(cht);
    if (chv)
        pnm_freetupletable(&pam, chv);

    releaseTgaHeader(tgaHeader);
    strfree(outName);
    pnm_freepamarray(tuples, &pam);

    return 0;
}
Example #3
0
int
main(int argc, char **argv ) {

    struct cmdlineInfo cmdline;
    FILE* ifP;
    struct pam pam;
    pfmSample * pfmRowBuffer;
    unsigned int pfmSamplesPerRow;
    unsigned int pfmRow;
    tuplen ** tuplenArray;

    pnm_init(&argc, argv);

    machineEndianness = thisMachineEndianness();

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    tuplenArray = pnm_readpamn(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));

    writePfmHeader(stdout,
                   makePfmHeader(&pam, cmdline.scale, cmdline.endian));

    pfmSamplesPerRow = pam.width * pam.depth;

    MALLOCARRAY_NOFAIL(pfmRowBuffer, pfmSamplesPerRow);

    /* PFMs are upside down like BMPs */
    for (pfmRow = 0; pfmRow < pam.height; ++pfmRow)
        writePfmRow(&pam, stdout, pfmRow, pfmSamplesPerRow,
                    tuplenArray, cmdline.endian, cmdline.scale,
                    pfmRowBuffer);

    pnm_freepamarrayn(tuplenArray, &pam);
    free(pfmRowBuffer);

    pm_close(stdout);
    pm_close(pam.file);

    return 0;
}
Example #4
0
int
main(int           argc,
     const char ** argv) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    tuple ** outTuples;        /* Output image */
    scaler * scalerP;
    struct pam inpam;
    struct pam outpam;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(allocation_depth));

    pnm_setminallocationdepth(&inpam, 3);
    
    outpam.size               = sizeof(outpam);
    outpam.len                = PAM_STRUCT_SIZE(tuple_type);
    outpam.file               = stdout;
    outpam.width              = inpam.width;
    outpam.height             = inpam.height;
    outpam.depth              = 3;
    outpam.maxval             =
        pm_lcm(cmdline.colorRes.c[RED]-1,
               cmdline.colorRes.c[GRN]-1,
               cmdline.colorRes.c[BLU]-1,
               PPM_MAXMAXVAL);
    outpam.bytes_per_sample   = inpam.bytes_per_sample;
    STRSCPY(outpam.tuple_type, "RGB");
    outpam.format             = RPPM_FORMAT;
    outpam.plainformat        = false;

    scaler_create(outpam.maxval, cmdline.colorRes, &scalerP);

    ditherImage(&inpam, scalerP, cmdline.dim, cmdline.colorRes,
                &outpam, &outTuples);

    pnm_writepam(&outpam, outTuples);

    scaler_destroy(scalerP);

    pnm_freepamarray(outTuples, &outpam);

    pm_close(ifP);

    return 0;
}
Example #5
0
int
main(int argc, char **argv)
{
    struct cmdlineInfo cmdline;
    FILE *ifP;
    struct pam inpam;
    jas_image_t * jasperP;

    pnm_init(&argc, argv);
    
    parseCommandLine(argc, argv, &cmdline);
    
    { 
        int rc;
        
        rc = jas_init();
        if ( rc != 0 )
            pm_error("Failed to initialize Jasper library.  "
                     "jas_init() returns rc %d", rc );
    }
    
    jas_setdbglevel(cmdline.debuglevel);
    
    ifP = pm_openr(cmdline.inputFilename);
    
    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
    
    convertToJasperImage(&inpam, &jasperP);
    
    writeJpc(jasperP, cmdline, stdout);
    
	jas_image_destroy(jasperP);

    pm_close(ifP);

    pm_close(stdout);
    
    return 0;
}
Example #6
0
static void
writeMap(const char * const wmapFileName,
         const gray * const lumamap,
         xelval       const maxval) {

    FILE * const wmapfP = pm_openw(wmapFileName);

    pgm_writepgminit(wmapfP, maxval+1, 1, maxval, 0);

    pgm_writepgmrow(wmapfP, lumamap, maxval+1, maxval, 0);

    pm_close(wmapfP);
}
int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;

    FILE * ifP;

    /* Parameters of input image: */
    int rows, cols;
    pixval maxval;
    int format;

    ppm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilename);

    ppm_readppminit(ifP, &cols, &rows, &maxval, &format);
    pbm_writepbminit(stdout, cols, rows, 0);
    {
        pixel * const inputRow = ppm_allocrow(cols);
        bit *   const maskRow  = pbm_allocrow(cols);

        unsigned int numPixelsMasked;

        unsigned int row;
        for (row = 0, numPixelsMasked = 0; row < rows; ++row) {
            int col;
            ppm_readppmrow(ifP, inputRow, cols, maxval, format);
            for (col = 0; col < cols; ++col) {
                if (colorIsInSet(inputRow[col], maxval, cmdline)) {
                    maskRow[col] = PBM_BLACK;
                    ++numPixelsMasked;
                } else 
                    maskRow[col] = PBM_WHITE;
            }
            pbm_writepbmrow(stdout, maskRow, cols, 0);
        }

        if (cmdline.verbose)
            pm_message("%u pixels found matching %u requested colors",
                       numPixelsMasked, cmdline.colorCount);

        pbm_freerow(maskRow);
        ppm_freerow(inputRow);
    }
    pm_close(ifP);

    return 0;
}
Example #8
0
int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;

    FILE * ifP;
    bool eof;  /* No more images in input */
    unsigned int imageSeq;  
        /* Sequence of current image in input file.  First = 0 */

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);
    
    ifP = pm_openr(cmdline.inputFileName);

    eof = FALSE;
    for (imageSeq = 0; !eof; ++imageSeq) {
        FILE * ofP;
        const char * outputFileName;  /* malloc'ed */

        computeOutputName(cmdline.outputFilePattern, cmdline.padname, 
                          imageSeq,
                          &outputFileName);
        pm_message("WRITING %s", outputFileName);

        ofP = pm_openw(outputFileName);
        extractOneImage(ifP, ofP);

        pm_close(ofP);
        strfree(outputFileName);

        pnm_nextimage(ifP, &eof);
    }
    pm_close(ifP);
    
    return 0;
}
Example #9
0
int
main(int argc, char *argv[]) {

    FILE *ifp;
    FILE *alpha_file, *imageout_file;
    pixel *colormap;
    int cols, rows;
    int transparent;  /* value of 'data' that means transparent */
    int *data;
        /* The image as an array of width * height integers, each one
           being an index int colormap[].
        */

    struct cmdline_info cmdline;

    ppm_init(&argc, argv);

    parse_command_line(argc, argv, &cmdline);

    verbose = cmdline.verbose;

    if ( cmdline.input_filespec != NULL ) 
        ifp = pm_openr( cmdline.input_filespec);
    else
        ifp = stdin;

    if (cmdline.alpha_stdout)
        alpha_file = stdout;
    else if (cmdline.alpha_filename == NULL) 
        alpha_file = NULL;
    else {
        alpha_file = pm_openw(cmdline.alpha_filename);
    }

    if (cmdline.alpha_stdout) 
        imageout_file = NULL;
    else
        imageout_file = stdout;

    ReadXPMFile(ifp, &cols, &rows, &colormap, &data, &transparent);
    
    pm_close(ifp);

    writeOutput(imageout_file, alpha_file, cols, rows, colormap, data,
                transparent);

    free(colormap);
    
    return 0;
}
Example #10
0
int 
main(int argc, char *argv[]) {

    FILE * const rlefile = stdout;

    struct cmdlineInfo cmdline;
    FILE *ifP;                 /* Input (Netpbm) file */
    struct pam pam;            /* Description of the image */
    tuple ** tupleArray;       /* The image raster */
    tupletable colormap;       /* List of all of the colors used */
    unsigned int numColors;    /* Number of unique colors in the color map */
    tuplehash colorhash; 
        /* Mapping from color to index into colormap[] */
    tuple transcolor;
        /* Color that should be considered transparent */

    pnm_init (&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    tupleArray = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type));

    transcolor = pnm_parsecolor(cmdline.transparent, pam.maxval);
    
    computeColorMap(&pam, tupleArray, &numColors, &colormap, &colorhash,
                    cmdline.showcolormap);
    
    makeDjvurleHeader(rlefile, &pam, numColors, colormap);

    /* Write the raster */

    {
        unsigned int row;
        for (row = 0; row < pam.height; ++row)
            writeDjvurleRow(rlefile, &pam, tupleArray[row], colorhash, 
                            transcolor);
    }
    /* Clean up */
    
    pnm_freepamarray(tupleArray, &pam);
    pnm_freetupletable(&pam, colormap);
    pnm_destroytuplehash(colorhash);
    pnm_freepamtuple(transcolor);
    pm_close(ifP);

    return 0;
}
Example #11
0
/* checks if romFileName points to valid MegaCD image
 * if so, checks for suitable BIOS */
int emu_cdCheck(int *pregion)
{
	unsigned char buf[32];
	pm_file *cd_f;
	int type = 0, region = 4; // 1: Japan, 4: US, 8: Europe

	cd_f = pm_open(romFileName);
	if (!cd_f) return 0; // let the upper level handle this

	if (pm_read(buf, 32, cd_f) != 32) {
		pm_close(cd_f);
		return 0;
	}

	if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) type = 1;       // Sega CD (ISO)
	if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) type = 2;       // Sega CD (BIN)
	if (type == 0) {
		pm_close(cd_f);
		return 0;
	}

	/* it seems we have a CD image here. Try to detect region now.. */
	pm_seek(cd_f, (type == 1) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
	pm_read(buf, 1, cd_f);
	pm_close(cd_f);

	if (buf[0] == 0x64) region = 8; // EU
	if (buf[0] == 0xa1) region = 1; // JAP

	lprintf("detected %s Sega/Mega CD image with %s region\n",
		type == 2 ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");

	if (pregion != NULL) *pregion = region;

	return type;
}
Example #12
0
static void
readAlpha(const char filespec[], gray *** const alphaP,
          int const cols, int const rows, pixval * const alphamaxvalP) {

    FILE * alpha_file;
    int alphacols, alpharows;
        
    alpha_file = pm_openr(filespec);
    *alphaP = pgm_readpgm(alpha_file, &alphacols, &alpharows, alphamaxvalP);
    pm_close(alpha_file);
    
    if (cols != alphacols || rows != alpharows)
        pm_error("Alpha mask is not the same dimensions as the "
                 "image.  Image is %d by %d, while mask is %d x %d.",
                 cols, rows, alphacols, alpharows);
}
Example #13
0
int
main(int argc, char **argv)
{
    struct cmdlineInfo cmdline;
    struct pam outpam;
    int * jasperCmpt;  /* malloc'ed */
       /* jaspercmpt[P] is the component number for use with the
          Jasper library that corresponds to Plane P of the PAM.  
       */
    jas_image_t * jasperP;

    pnm_init(&argc, argv);
    
    parseCommandLine(argc, argv, &cmdline);
    
    { 
        int rc;
        
        rc = jas_init();
        if ( rc != 0 )
            pm_error("Failed to initialize Jasper library.  "
                     "jas_init() returns rc %d", rc );
    }
    
    jas_setdbglevel(cmdline.debuglevel);
    
    readJpc(cmdline.inputFilename, &jasperP);

    outpam.file = stdout;
    outpam.size = sizeof(outpam);
    outpam.len  = PAM_STRUCT_SIZE(tuple_type);

    computeOutputParm(jasperP, &outpam, &jasperCmpt);

    pnm_writepaminit(&outpam);
    
    convertToPamPnm(&outpam, jasperP, jasperCmpt);
    
    free(jasperCmpt);
	jas_image_destroy(jasperP);

    pm_close(stdout);
    
    return 0;
}
Example #14
0
static void
readScriptFile(const char *  const scriptFileName,
               const char ** const scriptP) {

    FILE * scriptFileP;
    char * script;
    size_t scriptAllocation;
    size_t bytesReadSoFar;

    scriptAllocation = 4096;

    MALLOCARRAY(script, scriptAllocation);

    if (script == NULL)
        pm_error("out of memory reading script from file");

    scriptFileP = pm_openr(scriptFileName);

    bytesReadSoFar = 0;
    while (!feof(scriptFileP)) {
        size_t bytesRead;

        if (scriptAllocation - bytesReadSoFar < 2) {
            scriptAllocation += 4096;
            REALLOCARRAY(script, scriptAllocation);
            if (script == NULL)
                pm_error("out of memory reading script from file");
        }
        bytesRead = fread(script + bytesReadSoFar, 1,
                          scriptAllocation - bytesReadSoFar - 1, scriptFileP);
        bytesReadSoFar += bytesRead;
    }
    pm_close(scriptFileP);

    {
        unsigned int i;
        for (i = 0; i < bytesReadSoFar; ++i)
            if (!isprint(script[i]) && !isspace(script[i]))
                pm_error("Script contains byte that is not printable ASCII "
                         "character: 0x%02x", script[i]);
    }
    script[bytesReadSoFar] = '\0';  /* terminating NUL */

    *scriptP = script;
}
Example #15
0
int
main(int argc, char * argv[]) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    xelval lmin, lmax;
    gray * lumamap;           /* Luminosity map */
    unsigned int * lumahist;  /* Histogram of luminosity values */
    int rows, cols;           /* Rows, columns of input image */
    xelval maxval;            /* Maxval of input image */
    int format;               /* Format indicator (PBM/PGM/PPM) */
    xel ** xels;              /* Pixel array */
    unsigned int pixelCount;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    xels = pnm_readpnm(ifP, &cols, &rows, &maxval, &format);

    pm_close(ifP);

    computeLuminosityHistogram(xels, rows, cols, maxval, format,
                               cmdline.gray, &lumahist, &lmin, &lmax,
                               &pixelCount);

    getMapping(cmdline.rmap, lumahist, maxval, pixelCount, &lumamap);

    if (cmdline.verbose)
        reportMap(lumahist, maxval, lumamap);

    remap(xels, cols, rows, maxval, format, !!cmdline.gray, lumamap);

    pnm_writepnm(stdout, xels, cols, rows, maxval, format, 0);

    if (cmdline.wmap)
        writeMap(cmdline.wmap, lumamap, maxval);

    pgm_freerow(lumamap);

    return 0;
}
Example #16
0
int
main( int argc, char* argv[])
    {
    FILE* ifp;
    bit* bitrow;
    int rows, cols, format, row, col;

    pbm_init( &argc, argv );

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

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

    pbm_readpbminit( ifp, &cols, &rows, &format );

    if( rows>INT16MAX || cols>INT16MAX )
      pm_error ("Input image is too large.");


    bitrow = pbm_allocrow( cols );

    putinit (rows, cols);
    for ( row = 0; row < rows; ++row )
        {
#ifdef DEBUG
        fprintf (stderr, "row %d\n", row);
#endif
        pbm_readpbmrow( ifp, bitrow, cols, format );
        for ( col = 0; col < cols; ++col )
            putbit( bitrow[col] );
        putrow( );
        }
    flushrow ();

    pm_close( ifp );


    exit( 0 );
    }
Example #17
0
static void
writeText(IPDB *       const pdbP,
          const char * const name) {

    const char * const note = ipdb_text(pdbP);

    FILE * fP;

    if (name == NULL || note == NULL) {
    } else {
        fP = pm_openw(name);
        if (fP == NULL)
            pm_error("Could not open note file '%s' for output", name);
        
        fprintf(fP, "%s\n", note);

        pm_close(fP);
    }
}
Example #18
0
static void
convertWithMap(FILE * const ifP,
               unsigned int const cols,
               unsigned int const rows,
               gray         const maxval,
               int          const format,
               const char * const mapFileName,
               FILE *       const ofP,
               gray *       const grayrow,
               pixel *      const pixelrow) {

    unsigned int row;
    FILE * mapFileP;
    int mapcols, maprows;
    pixval mapmaxval;
    pixel ** mappixels;
    unsigned int mapmaxcolor;
    
    mapFileP = pm_openr(mapFileName);
    mappixels = ppm_readppm(mapFileP, &mapcols, &maprows, &mapmaxval);
    pm_close(mapFileP);
    mapmaxcolor = maprows * mapcols - 1;

    ppm_writeppminit(ofP, cols, rows, mapmaxval, 0);

    for (row = 0; row < rows; ++row) {
        unsigned int col;
            
        pgm_readpgmrow(ifP, grayrow, cols, maxval, format);

        for (col = 0; col < cols; ++col) {
            unsigned int c;
            if (maxval == mapmaxcolor)
                c = grayrow[col];
            else
                c = grayrow[col] * mapmaxcolor / maxval;
            pixelrow[col] = mappixels[c / mapcols][c % mapcols];
        }
        ppm_writeppmrow(ofP, pixelrow, cols, mapmaxval, 0);
    }
    ppm_freearray(mappixels, maprows);
}
Example #19
0
static void
save_exif(struct jpeg_decompress_struct const cinfo, 
          const char *                  const exif_filespec) {
/*----------------------------------------------------------------------------
  Write the contents of the first Exif header in the image into the
  file with filespec 'exif_filespec'.  Start with the two byte length
  field.  If 'exif_filespec' is "-", write to standard output.

  If there is no Exif header in the image, write just zero, as a two
  byte pure binary integer.
-----------------------------------------------------------------------------*/
    FILE * exif_file;
    struct jpeg_marker_struct * markerP;

    exif_file = pm_openw(exif_filespec);

    for (markerP = cinfo.marker_list; 
         markerP && !is_exif(*markerP);
         markerP = markerP->next);

    if (markerP) {
        pm_writebigshort(exif_file, markerP->data_length+2);
        if (ferror(exif_file))
            pm_error("Write of Exif header to %s failed on first byte.",
                     exif_filespec);
        else {
            int rc;

            rc = fwrite(markerP->data, 1, markerP->data_length, exif_file);
            if (rc != markerP->data_length)
                pm_error("Write of Exif header to '%s' failed.  Wrote "
                         "length successfully, but then failed after "
                         "%d characters of data.", exif_filespec, rc);
        }
    } else {
        /* There is no Exif header in the image */
        pm_writebigshort(exif_file, 0);
        if (ferror(exif_file))
            pm_error("Write of Exif header file '%s' failed.", exif_filespec);
    }
    pm_close(exif_file);
}
Example #20
0
int
main(int argc, const char ** const argv) {

    struct CmdlineInfo cmdline;
    struct pam inpam;
    FILE * ifP;
    struct pam lookuppam;
    tuple ** lookup;

    tuplehash lookupHash;
    
    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

    getLookup(cmdline.lookupfile, &lookup, &lookuppam);

    if (inpam.depth != lookuppam.depth)
        pm_error("The lookup image has depth %u, but the input image "
                 "has depth %u.  They must be the same",
                 lookuppam.depth, inpam.depth);
    if (!streq(inpam.tuple_type, lookuppam.tuple_type))
        pm_error("The lookup image has tupel type '%s', "
                 "but the input image "
                 "has tuple type '%s'.  They must be the same",
                 lookuppam.tuple_type, inpam.tuple_type);

    makeReverseLookupHash(&lookuppam, lookup, &lookupHash);

    doUnlookup(&inpam, lookupHash, lookuppam.width-1, stdout);

    pm_close(ifP);

    pnm_destroytuplehash(lookupHash);
    pnm_freepamarray(lookup, &lookuppam);
    
    return 0;
}
Example #21
0
int
main(int    argc,
     char * argv[]) {

    FILE * ifP;
    struct cmdlineInfo cmdline;
    gray * grayrow;
    pixel * pixelrow;
    int rows, cols, format;
    gray maxval;

    ppm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilename);

    pgm_readpgminit(ifP, &cols, &rows, &maxval, &format);
    grayrow = pgm_allocrow(cols);
    pixelrow = ppm_allocrow(cols);

    if (cmdline.map)
        convertWithMap(ifP, cols, rows, maxval, format, cmdline.map,
                       stdout, grayrow, pixelrow);
    else
        convertLinear(ifP, cols, rows, maxval, format, 
                      cmdline.colorBlack, cmdline.colorWhite, stdout,
                      grayrow, pixelrow);

    ppm_freerow(pixelrow);
    pgm_freerow(grayrow);
    pm_close(ifP);

    /* If the program failed, it previously aborted with nonzero completion
       code, via various function calls.
    */
    return 0;
}
Example #22
0
int
main(int argc, char * argv[]) {

    struct cmdlineInfo cmdline;
    FILE * ifP;
    bool eof;

    pbm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilename);

    eof = FALSE;
    while (!eof) {
        doPage(ifP, cmdline);
        pbm_nextimage(ifP, &eof);
    }

    pm_close(ifP);

    return 0;
}
Example #23
0
int 
main(int argc,char **argv) {

  struct cmdlineInfo cmdline;
  unsigned int i;

  pbm_init(&argc, argv);

  parseCommandLine(argc, argv, &cmdline);

  sent_xon = FALSE;

  for (i = 0; i < cmdline.nFiles; ++i) {
      FILE *ifP;
      pm_message("opening '%s'", cmdline.inputFile[i]);
      ifP = pm_openr(cmdline.inputFile[i]);
      process_handle(ifP, cmdline.graph_mode, cmdline.passes);
      pm_close(ifP);
  }

  free(cmdline.inputFile);

  return 0;
}
Example #24
0
int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;
    struct pam inpam;
    FILE * ifP;
    pm_filepos rasterpos;
    tuple backgroundColor;
    const unsigned char * const * pi;
    
    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr_seekable(cmdline.inputFileName);

    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));

    pm_tell2(ifP, &rasterpos, sizeof(rasterpos));

    determineBackgroundColor(&inpam, cmdline.verbose, &backgroundColor);

    pm_seek2(ifP, &rasterpos, sizeof(rasterpos));

    findBackgroundPixels(&inpam, backgroundColor, cmdline.verbose, &pi);

    writeOutput(&inpam, pi);

    destroyPi(pi, inpam.height);

    pm_close(ifP);

    pnm_freepamtuple(backgroundColor);
    
    return 0;
}
Example #25
0
int
main(int argc, const char * argv[] ) {

    struct CmdlineInfo cmdline;
    FILE * ifP;
    struct pam outpamCommon;
        /* Describes the output images.  Width and height fields are
           not meaningful, because different output images might have
           different dimensions.  The rest of the information is common
           across all output images.
        */
    tupletable colormap;
    unsigned int colormapSize;
    tuple specColor;
        /* A tuple of the color the user specified to use for input colors
           that are not in the colormap.  Arbitrary tuple if he didn't
           specify any.
        */
    tuple firstColor;
        /* A tuple of the first color present in the map file */
    tuple defaultColor;
        /* The color to which we will map an input color that is not in the
           colormap.  NULL if we are not to map such a color to a particular
           color (i.e. we'll choose an approximate match from the map).
        */

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

    processMapFile(cmdline.mapFilespec, &outpamCommon,
                   &colormap, &colormapSize, &firstColor);

    getSpecifiedMissingColor(&outpamCommon, cmdline.missingcolor, &specColor);

    switch (cmdline.missingMethod) {
    case MISSING_CLOSE:
        defaultColor = NULL;
        break;
    case MISSING_FIRST:
        defaultColor = firstColor;
        break;
    case MISSING_SPECIFIED:
        defaultColor = specColor;
        break;
    }

    remap(ifP, &outpamCommon, colormap, colormapSize, 
          cmdline.floyd, !cmdline.norandom, defaultColor,
          cmdline.verbose);

    pnm_freepamtuple(firstColor);
    pnm_freepamtuple(specColor);

    pm_close(stdout);

    pm_close(ifP);

    return 0;
}
Example #26
0
int emu_ReloadRom(void)
{
	unsigned int rom_size = 0;
	char *used_rom_name = romFileName;
	char ext[5];
	pm_file *rom;
	int ret, cd_state, cd_region, cfg_loaded = 0;

	lprintf("emu_ReloadRom(%s)\n", romFileName);

	get_ext(romFileName, ext);

	// detect wrong extensions
	if(!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) { // s.gz ~ .mds.gz
		sprintf(menuErrorMsg, "Not a ROM selected.");
		return 0;
	}

	PicoPatchUnload();

	// check for movie file
	if(movie_data) {
		free(movie_data);
		movie_data = 0;
	}
#if 0
	if(!strcmp(ext, ".gmv")) {
		// check for both gmv and rom
		int dummy;
		FILE *movie_file = fopen(romFileName, "rb");
		if(!movie_file) {
			sprintf(menuErrorMsg, "Failed to open movie.");
			return 0;
		}
		fseek(movie_file, 0, SEEK_END);
		movie_size = ftell(movie_file);
		fseek(movie_file, 0, SEEK_SET);
		if(movie_size < 64+3) {
			sprintf(menuErrorMsg, "Invalid GMV file.");
			fclose(movie_file);
			return 0;
		}
		movie_data = malloc(movie_size);
		if(movie_data == NULL) {
			sprintf(menuErrorMsg, "low memory.");
			fclose(movie_file);
			return 0;
		}
		fread(movie_data, 1, movie_size, movie_file);
		fclose(movie_file);
		if (strncmp((char *)movie_data, "Gens Movie TEST", 15) != 0) {
			sprintf(menuErrorMsg, "Invalid GMV file.");
			return 0;
		}
		dummy = try_rfn_cut() || try_rfn_cut();
		if (!dummy) {
			sprintf(menuErrorMsg, "Could't find a ROM for movie.");
			return 0;
		}
		get_ext(romFileName, ext);
	} else 

	if (!strcmp(ext, ".pat")) {
		int dummy;
		PicoPatchLoad(romFileName);
		dummy = try_rfn_cut() || try_rfn_cut();
		if (!dummy) {
			sprintf(menuErrorMsg, "Could't find a ROM to patch.");
			return 0;
		}
		get_ext(romFileName, ext);
	}
#else
	// load GG patches list
	char ggFname[512];
	romfname_ext(ggFname, NULL, ".cht");
	PicoPatchLoad(ggFname);
#endif

	if ((PicoMCD & 1) && Pico_mcd != NULL) Stop_CD();

	// check for MegaCD image
	cd_state = emu_cdCheck(&cd_region);
	if (cd_state > 0)
	{
		// valid CD image, check for BIOS..
#if 0
		// we need to have config loaded at this point
		ret = emu_ReadConfig(READ_CONFIG_FOR_GAME, READ_CONFIG_SKIP_DEF);
		if (!ret) emu_ReadConfig(READ_CONFIG_GLOBAL, READ_CONFIG_SKIP_DEF);
#endif
		cfg_loaded = 1;

		if (PicoRegionOverride) {
			cd_region = PicoRegionOverride;
			lprintf("overrided region to %s\n", cd_region != 4 ? (cd_region == 8 ? "EU" : "JAP") : "USA");
		}
		if (!emu_findBios(cd_region, &used_rom_name)) {
			// bios_help() ?
			return 0;
		}

		PicoMCD |= 1;
		get_ext(used_rom_name, ext);
	} else
	{
		if (PicoMCD & 1) Stop_CD();
		PicoMCD &= ~1;
	}

	ips_define(ipsFileName);
	rom = pm_open(used_rom_name);
	if(!rom) {
		sprintf(menuErrorMsg, "Failed to open rom.");
		return 0;
	}

	menu_romload_prepare(used_rom_name); // also CD load

	if(rom_data) {
		free(rom_data);
		rom_data = 0;
		rom_size = 0;
	}

	if( (ret = PicoCartLoad(rom, &rom_data, &rom_size)) ) {
		sprintf(menuErrorMsg, "PicoCartLoad() failed.");
		lprintf("%s\n", menuErrorMsg);
		pm_close(rom);
		menu_romload_end();
		return 0;
	}
	pm_close(rom);

	// detect wrong files (Pico crashes on very small files), also see if ROM EP is good
	if(rom_size <= 0x200 || strncmp((char *)rom_data, "Pico", 4) == 0 ||
	  ((*(unsigned char *)(rom_data+4)<<16)|(*(unsigned short *)(rom_data+6))) >= (int)rom_size) {
		if (rom_data) free(rom_data);
		rom_data = 0;
		sprintf(menuErrorMsg, "Not a ROM selected.");
		menu_romload_end();
		return 0;
	}
#if 0
	// load config for this ROM (do this before insert to get correct region)
	if (!cfg_loaded) {
		ret = emu_ReadConfig(READ_CONFIG_FOR_GAME, READ_CONFIG_SKIP_DEF);
		if (!ret) emu_ReadConfig(READ_CONFIG_GLOBAL, READ_CONFIG_SKIP_DEF);
	}
#endif
	lprintf("PicoCartInsert(%p, %d);\n", rom_data, rom_size);
	if(PicoCartInsert(rom_data, rom_size)) {
		sprintf(menuErrorMsg, "Failed to load ROM.");
		menu_romload_end();
		return 0;
	}

	Pico.m.frame_count = 0;

	// insert CD if it was detected
	if (cd_state > 0) {
		ret = Insert_CD(romFileName, cd_state == 2);
		if (ret != 0) {
			sprintf(menuErrorMsg, "Insert_CD() failed, invalid CD image?");
			lprintf("%s\n", menuErrorMsg);
			menu_romload_end();
			return 0;
		}
	}

	menu_romload_end();
#if 0
	if (!emu_isBios(romFileName))
	{
		// emu_ReadConfig() might have messed currentConfig.lastRomFile
		strncpy(currentConfig.lastRomFile, romFileName, sizeof(currentConfig.lastRomFile)-1);
		currentConfig.lastRomFile[sizeof(currentConfig.lastRomFile)-1] = 0;
	}
#endif
	if (PicoPatches) {
		PicoPatchPrepare();
		PicoPatchApply();
	}

	// additional movie stuff
	if (movie_data) {
		if(movie_data[0x14] == '6')
		     PicoOpt |=  PicoOpt_6button_gamepad; // 6 button pad
		else PicoOpt &= ~PicoOpt_6button_gamepad;
		PicoOpt |= (PicoOpt_accurate_timing | PicoOpt_disable_vdp_fifo); // accurate timing, no VDP fifo timing
		if(movie_data[0xF] >= 'A') {
			if(movie_data[0x16] & 0x80) {
				PicoRegionOverride = 8;
			} else {
				PicoRegionOverride = 4;
			}
			PicoReset(0);
			// TODO: bits 6 & 5
		}
		movie_data[0x18+30] = 0;
		sprintf(noticeMsg, "MOVIE: %s", (char *) &movie_data[0x18]);
	}
	else
	{
		PicoOpt &= ~PicoOpt_disable_vdp_fifo;
		if(Pico.m.pal) {
			strcpy(noticeMsg, "PAL SYSTEM / 50 FPS");
		} else {
			strcpy(noticeMsg, "NTSC SYSTEM / 60 FPS");
		}
	}
	emu_noticeMsgUpdated();

	// load SRAM for this ROM
	emu_SaveLoadSRAM(1);

	return 1;
}
Example #27
0
static int detect_media(const char *fname)
{
  static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };
  static const char *sms_exts[] = { "sms", "gg", "sg" };
  static const char *md_exts[] = { "gen", "bin", "smd" };
  char buff0[32], buff[32];
  unsigned short *d16;
  pm_file *pmf;
  char ext[5];
  int i;

  get_ext(fname, ext);

  // detect wrong extensions
  if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz
    return PM_BAD_DETECT;

  /* don't believe in extensions, except .cue */
  if (strcasecmp(ext, ".cue") == 0)
    return PM_CD;

  pmf = pm_open(fname);
  if (pmf == NULL)
    return PM_BAD_DETECT;

  if (pm_read(buff0, 32, pmf) != 32) {
    pm_close(pmf);
    return PM_BAD_DETECT;
  }

  if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||
      strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {
    pm_close(pmf);
    return PM_CD;
  }

  /* check for SMD evil */
  if (pmf->size >= 0x4200 && (pmf->size & 0x3fff) == 0x200) {
    if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200 &&
        pm_read(buff, 16, pmf) == 16 &&
        strncmp("TMR SEGA", buff, 8) == 0)
      goto looks_like_sms;

    /* could parse further but don't bother */
    goto extension_check;
  }

  /* MD header? Act as TMSS BIOS here */
  if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100 && pm_read(buff, 16, pmf) == 16) {
    if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0)
      goto looks_like_md;
  }

  for (i = 0; i < ARRAY_SIZE(sms_offsets); i++) {
    if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])
      continue;

    if (pm_read(buff, 16, pmf) != 16)
      continue;

    if (strncmp("TMR SEGA", buff, 8) == 0)
      goto looks_like_sms;
  }

extension_check:
  /* probably some headerless thing. Maybe check the extension after all. */
  for (i = 0; i < ARRAY_SIZE(md_exts); i++)
    if (strcasecmp(pmf->ext, md_exts[i]) == 0)
      goto looks_like_md;

  for (i = 0; i < ARRAY_SIZE(sms_exts); i++)
    if (strcasecmp(pmf->ext, sms_exts[i]) == 0)
      goto looks_like_sms;

  /* If everything else fails, make a guess on the reset vector */
  d16 = (unsigned short *)(buff0 + 4);
  if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= pmf->size) {
    lprintf("bad MD reset vector, assuming SMS\n");
    goto looks_like_sms;
  }

looks_like_md:
  pm_close(pmf);
  return PM_MD_CART;

looks_like_sms:
  pm_close(pmf);
  return PM_MARK3;
}
Example #28
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 );
}
Example #29
0
FILE *
pm_openr_seekable(const char name[]) {
/*----------------------------------------------------------------------------
  Open the file named by name[] such that it is seekable (i.e. it can be
  rewound and read in multiple passes with fseek()).

  If the file is actually seekable, this reduces to the same as
  pm_openr().  If not, we copy the named file to a temporary file
  and return that file's stream descriptor.

  We use a file that the operating system recognizes as temporary, so
  it picks the filename and deletes the file when we close it.
-----------------------------------------------------------------------------*/
    int stat_rc;
    int seekable;  /* logical: file is seekable */
    struct stat statbuf;
    FILE * original_file;
    FILE * seekable_file;

    original_file = pm_openr((char *) name);

    /* I would use fseek() to determine if the file is seekable and 
       be a little more general than checking the type of file, but I
       don't have reliable information on how to do that.  I have seen
       streams be partially seekable -- you can, for example seek to
       0 if the file is positioned at 0 but you can't actually back up
       to 0.  I have seen documentation that says the errno for an
       unseekable stream is EBADF and in practice seen ESPIPE.

       On the other hand, regular files are always seekable and even if
       some other file is, it doesn't hurt much to assume it isn't.
    */

    stat_rc = fstat(fileno(original_file), &statbuf);
    if (stat_rc == 0 && S_ISREG(statbuf.st_mode))
        seekable = TRUE;
    else 
        seekable = FALSE;

    if (seekable) {
        seekable_file = original_file;
    } else {
        seekable_file = pm_tmpfile();
        
        /* Copy the input into the temporary seekable file */
        while (!feof(original_file) && !ferror(original_file) 
               && !ferror(seekable_file)) {
            char buffer[4096];
            int bytes_read;
            bytes_read = fread(buffer, 1, sizeof(buffer), original_file);
            fwrite(buffer, 1, bytes_read, seekable_file);
        }
        if (ferror(original_file))
            pm_error("Error reading input file into temporary file.  "
                     "Errno = %s (%d)", strerror(errno), errno);
        if (ferror(seekable_file))
            pm_error("Error writing input into temporary file.  "
                     "Errno = %s (%d)", strerror(errno), errno);
        pm_close(original_file);
        {
            int seek_rc;
            seek_rc = fseek(seekable_file, 0, SEEK_SET);
            if (seek_rc != 0)
                pm_error("fseek() failed to rewind temporary file.  "
                         "Errno = %s (%d)", strerror(errno), errno);
        }
    }
    return seekable_file;
}
Example #30
0
void
pm_closew(FILE * const f) {
    pm_close(f);
}