int 
main(int argc, const char **argv) {

    struct CmdlineInfo cmdline;
    struct pam * imgPam;  /* malloced */
    struct pam outimg;
    unsigned int fileCt;
    Coord * coords;  /* malloced */
    FILE * headerFileP;
    FILE * dataFileP;
    const char ** names; /* malloced */
    unsigned int qfactor;  /* In per cent */

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    headerFileP = cmdline.header ? pm_openw(cmdline.header) : NULL;
    dataFileP = cmdline.data ? pm_openw(cmdline.data) : NULL;

    qfactor = qfactorFromQuality(cmdline.quality, cmdline.quality2);

    openFiles(cmdline, &fileCt, &imgPam, &names);

    readFileHeaders(imgPam, fileCt);

    sortImagesByArea(fileCt, imgPam, names);

    findpack(imgPam, fileCt, &coords, cmdline.quality2, qfactor);

    computeOutputType(&outimg.maxval, &outimg.format, outimg.tuple_type,
                      &outimg.depth, fileCt, imgPam);

    computeOutputDimensions(&outimg.width, &outimg.height, fileCt,
                            imgPam, coords);
    outimg.size = sizeof(outimg);
    outimg.len = PAM_STRUCT_SIZE(allocation_depth);
    pnm_setminallocationdepth(&outimg, outimg.depth);
    outimg.plainformat = false;
    outimg.file = stdout;
 
    writePam(&outimg, fileCt, coords, imgPam);

    if (dataFileP)
        writeData(dataFileP, outimg.width, outimg.height,
                  fileCt, names, coords, imgPam);

    if (headerFileP)
        writeHeader(headerFileP, cmdline.prefix, outimg.width, outimg.height,
                    fileCt, names, coords, imgPam);

    closeFiles(imgPam, fileCt, headerFileP, dataFileP);

    free(coords);
    free(imgPam);
    free(names);

    return 0;
}
Exemple #2
0
static void
remap(FILE *             const ifP,
      const struct pam * const outpamCommonP,
      tupletable         const colormap, 
      unsigned int       const colormapSize,
      bool               const floyd,
      bool               const randomize,
      tuple              const defaultColor,
      bool               const verbose) {
/*----------------------------------------------------------------------------
   Remap the pixels from the raster on *ifP to the 'colormapSize' colors in
   'colormap'.

   Where the input pixel's color is in the map, just use that for the output.
   Where it isn't, use 'defaultColor', except if that is NULL, use the
   closest color in the map to the input color.

   But if 'floyd' is true and 'defaultColor' is NULL, also do Floyd-Steinberg
   dithering on the output so the aggregate color of a region is about the
   same as that of the input even though the individual pixels have different
   colors.
-----------------------------------------------------------------------------*/
    int eof;
    eof = FALSE;
    while (!eof) {
        struct pam inpam, outpam;
        unsigned int missingCount;
            /* Number of pixels that were mapped to 'defaultColor' because
               they weren't present in the color map.
            */

        pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(allocation_depth));
    
        outpam = *outpamCommonP;
        outpam.width  = inpam.width;
        outpam.height = inpam.height;

        pnm_writepaminit(&outpam);

        /* Set up so input buffers have extra space as needed to
           convert the input to the output depth.
        */
        pnm_setminallocationdepth(&inpam, outpam.depth);
    
        copyRaster(&inpam, &outpam, colormap, colormapSize, floyd,
                   randomize, defaultColor, &missingCount);
        
        if (verbose)
            pm_message("%u pixels not matched in color map", missingCount);
        
        pnm_nextimage(ifP, &eof);
    }
}
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;
}
Exemple #4
0
int
main(int argc, const char *argv[]) {

    struct cmdlineInfo cmdline;
    FILE * underlayFileP;
    FILE * overlayFileP;
    FILE * alphaFileP;
    struct pam underlayPam;
    struct pam overlayPam;
    struct pam alphaPam;
    struct pam composedPam;
    int originLeft, originTop;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    overlayFileP = pm_openr(cmdline.overlayFilespec);

    overlayPam.comment_p = NULL;
    pnm_readpaminit(overlayFileP, &overlayPam, 
                    PAM_STRUCT_SIZE(opacity_plane));

    if (overlayPam.len < PAM_STRUCT_SIZE(opacity_plane))
        pm_error("Libnetpbm is too old.  This program requires libnetpbm from "
                 "Netpbm 10.56 (September 2011) or newer");

    if (!overlayPam.visual)
        pm_error("Overlay image has tuple type '%s', which is not a "
                 "standard visual type.  We don't know how to compose.",
                 overlayPam.tuple_type);

    initAlphaFile(cmdline, &overlayPam, &alphaFileP, &alphaPam);

    underlayFileP = pm_openr(cmdline.underlyingFilespec);

    underlayPam.comment_p = NULL;
    pnm_readpaminit(underlayFileP, &underlayPam, 
                    PAM_STRUCT_SIZE(opacity_plane));

    assert(underlayPam.len >= PAM_STRUCT_SIZE(opacity_plane));

    if (!overlayPam.visual)
        pm_error("Overlay image has tuple type '%s', which is not a "
                 "standard visual type.  We don't know how to compose.",
                 overlayPam.tuple_type);
    
    computeOverlayPosition(underlayPam.width, underlayPam.height, 
                           overlayPam.width,  overlayPam.height,
                           cmdline, &originLeft, &originTop);

    composedPam.size             = PAM_STRUCT_SIZE(opacity_plane);
    composedPam.len              = PAM_STRUCT_SIZE(allocation_depth);
    composedPam.allocation_depth = 0;
    composedPam.file             = pm_openw(cmdline.outputFilespec);
    composedPam.comment_p        = NULL;

    determineOutputType(&underlayPam, &overlayPam, &composedPam);

    pnm_setminallocationdepth(&underlayPam, composedPam.depth);
    pnm_setminallocationdepth(&overlayPam,  composedPam.depth);
    
    composite(originLeft, originTop,
              &underlayPam, &overlayPam, alphaFileP ? &alphaPam : NULL,
              cmdline.alphaInvert, cmdline.opacity,
              &composedPam, cmdline.linear, cmdline.mixtransparency);

    if (alphaFileP)
        pm_close(alphaFileP);
    pm_close(overlayFileP);
    pm_close(underlayFileP);
    pm_close(composedPam.file);

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

    struct cmdlineInfo cmdline;
    FILE * underlayFileP;
    FILE * overlayFileP;
    FILE * alphaFileP;
    struct pam underlayPam;
    struct pam overlayPam;
    struct pam alphaPam;
    struct pam composedPam;
    int originLeft, originTop;

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    overlayFileP = pm_openr(cmdline.overlayFilespec);
    pnm_readpaminit(overlayFileP, &overlayPam, 
                    PAM_STRUCT_SIZE(allocation_depth));
    if (cmdline.alphaFilespec) {
        alphaFileP = pm_openr(cmdline.alphaFilespec);
        pnm_readpaminit(alphaFileP, &alphaPam, 
                        PAM_STRUCT_SIZE(allocation_depth));

        if (overlayPam.width != alphaPam.width || 
            overlayPam.height != alphaPam.height)
            pm_error("Opacity map and overlay image are not the same size");
    } else
        alphaFileP = NULL;

    underlayFileP = pm_openr(cmdline.underlyingFilespec);

    pnm_readpaminit(underlayFileP, &underlayPam, 
                    PAM_STRUCT_SIZE(allocation_depth));

    computeOverlayPosition(underlayPam.width, underlayPam.height, 
                           overlayPam.width,  overlayPam.height,
                           cmdline, &originLeft, &originTop);

    composedPam.size             = sizeof(composedPam);
    composedPam.len              = PAM_STRUCT_SIZE(allocation_depth);
    composedPam.allocation_depth = 0;
    composedPam.file             = pm_openw(cmdline.outputFilespec);

    determineOutputType(&composedPam, &underlayPam, &overlayPam);

    pnm_setminallocationdepth(&underlayPam, composedPam.depth);
    pnm_setminallocationdepth(&overlayPam,  composedPam.depth);
    
    composite(originLeft, originTop,
              &underlayPam, &overlayPam, alphaFileP ? &alphaPam : NULL,
              cmdline.alphaInvert, cmdline.opacity,
              &composedPam, cmdline.linear);

    if (alphaFileP)
        pm_close(alphaFileP);
    pm_close(overlayFileP);
    pm_close(underlayFileP);
    pm_close(composedPam.file);

    /* If the program failed, it previously aborted with nonzero completion
       code, via various function calls.
    */
    return 0;
}