Ejemplo n.º 1
0
void
ppm_readcolordict(const char *      const fileName,
                  int               const mustOpen,
                  unsigned int *    const nColorsP,
                  const char ***    const colornamesP,
                  pixel **          const colorsP,
                  colorhash_table * const chtP) {

    colorhash_table cht;
    const char ** colornames;
    pixel * colors;
    unsigned int nColors;

    cht = ppm_alloccolorhash();

    MALLOCARRAY(colornames, MAXCOLORNAMES);

    colors = ppm_allocrow(MAXCOLORNAMES);

    if (colornames == NULL)
        pm_error("Unable to allocate space for colorname table.");

    readcolordict(fileName, mustOpen, &nColors, colornames, colors, cht);

    if (chtP)
        *chtP = cht;
    else
        ppm_freecolorhash(cht);
    if (colornamesP)
        *colornamesP = colornames;
    else
        ppm_freecolornames(colornames);
    if (colorsP)
        *colorsP = colors;
    else
        ppm_freerow(colors);
    if (nColorsP)
        *nColorsP = nColors;
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[]) {

    FILE *ifp;
    int rows, cols;
    unsigned int ncolors;
    bool transparentSomewhere;
    pixval maxval, alphaMaxval;
    colorhash_table cht;
    colorhist_vector chv;

    colorhash_table colornameHash;
        /* Hash table to map colors to their names */
    const char ** colornames;
        /* Table of color names; 'colornameHash' yields an index into this
           array.
        */

    pixel **pixels;
    gray **alpha;

    /* Used for rgb value -> character-pixel string mapping */
    cixel_map *cmap;  /* malloc'ed */
        /* The XPM colormap */
    unsigned int cmapSize;
        /* Number of entries in 'cmap' */
    unsigned int transIndex;
        /* Index into 'cmap' of the transparent color, if there is one */

    unsigned int charsPerPixel;  

    struct cmdlineInfo cmdline;

    ppm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifp = pm_openr(cmdline.inputFilename);
    pixels = ppm_readppm(ifp, &cols, &rows, &maxval);
    pm_close(ifp);

    if (cmdline.alpha_filename) 
        readAlpha(cmdline.alpha_filename, &alpha, cols, rows, &alphaMaxval);
    else
        alpha = NULL;

    computeColormap(pixels, alpha, cols, rows, alphaMaxval,
                    &chv, &cht, &ncolors, &transparentSomewhere);

    if (cmdline.hexonly)
        colornameHash = NULL;
    else if (cmdline.rgb)
        ppm_readcolornamefile(cmdline.rgb, TRUE, &colornameHash, &colornames);
    else
        ppm_readcolornamefile(NULL, FALSE, &colornameHash, &colornames);

    /* Now generate the character-pixel colormap table. */
    genCmap(chv, ncolors, maxval, 
            colornameHash, colornames, transparentSomewhere, 
            &cmap, &transIndex, &cmapSize, &charsPerPixel);

    writeXpmFile(stdout, pixels, alpha, alphaMaxval,
                 cmdline.name, cols, rows, cmapSize,
                 charsPerPixel, cmap, cht, transIndex);
    
    if (colornameHash) {
        ppm_freecolorhash(colornameHash);
        ppm_freecolornames(colornames);
    }
    destroyCmap(cmap, cmapSize);
    ppm_freearray(pixels, rows);
    if (alpha) pgm_freearray(alpha, rows);

    return 0;
}