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

    struct cmdlineInfo cmdline;
    FILE* ifP;
    struct pam inpam;   /* Input PAM image */
    struct pam outpam;  /* Output PNM image */

    pnm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFilespec);

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

    validateTupleType(inpam, cmdline.assume);

    outpam = inpam;
    outpam.file = stdout;
    
    if (inpam.depth < 3) {
        outpam.depth = 1;
        if (inpam.maxval == 1)
            outpam.format = PBM_FORMAT;
        else 
            outpam.format = PGM_FORMAT;
    } else {
        outpam.depth = 3;
        outpam.format = PPM_FORMAT;
    }

    pnm_writepaminit(&outpam);

    {
        tuple *tuplerow;
        
        tuplerow = pnm_allocpamrow(&inpam);      
        { 
            int row;
            
            for (row = 0; row < inpam.height; row++) {
                pnm_readpamrow(&inpam, tuplerow);
                pnm_writepamrow(&outpam, tuplerow);
            }
        }
        pnm_freepamrow(tuplerow);        
    }
    return 0;
}
Exemple #2
0
static void
computeImageType_cht(struct pam *            const pamP,
                     struct cmdlineInfo      const cmdline, 
                     tuple **                const tuples,
                     enum TGAbaseImageType * const baseImgTypeP,
                     bool *                  const withAlphaP,
                     tupletable *            const chvP,
                     tuplehash *             const chtP, 
                     int *                   const ncolorsP) {

    unsigned int ncolors;
    enum TGAbaseImageType baseImgType;
    bool withAlpha;

    validateTupleType(pamP);

    withAlpha = (streq(pamP->tuple_type, "RGB_ALPHA"));

    if (cmdline.defaultFormat) {
        /* default the image type */
        if (withAlpha) {
            baseImgType = TGA_RGB_TYPE;
            *chvP = NULL;
        } else if (pamP->depth > 1) {
            pm_message("computing colormap...");
            *chvP = 
                pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors);
            if (*chvP == NULL) {
                pm_message("Too many colors for colormapped TGA.  Doing RGB.");
                baseImgType = TGA_RGB_TYPE;
            } else 
                baseImgType = TGA_MAP_TYPE;
        } else {
            baseImgType = TGA_MONO_TYPE;
            *chvP = NULL;
        }
    } else {
        baseImgType = cmdline.imgType;

        if (baseImgType == TGA_MAP_TYPE) {
            if (withAlpha)
                pm_error("Can't do a colormap because image has transparency "
                         "information");
            pm_message("computing colormap...");
            *chvP = 
                pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors);
            if (*chvP == NULL) 
                pm_error("Too many colors for colormapped TGA.  "
                         "Use 'pnmquant %d' to reduce the number of colors.", 
                         MAXCOLORS);
        } else
            *chvP = NULL;
        if (baseImgType == TGA_MONO_TYPE && pamP->depth > 1)
            pm_error("For Mono TGA output, input must be "
                     "GRAYSCALE or BLACKANDWHITE PAM or PBM or PGM");
    }
    
    if (baseImgType == TGA_MAP_TYPE) {
        pm_message("%d colors found.", ncolors);
        /* Make a hash table for fast color lookup. */
        *chtP = pnm_computetupletablehash(pamP, *chvP, ncolors);
    } else
        *chtP = NULL;

    *baseImgTypeP = baseImgType;
    *withAlphaP   = withAlpha;
    *ncolorsP     = ncolors;
}