Exemple #1
0
static void
computeOutputName(char          const outputFilePattern[], 
                  unsigned int  const padCount,
                  unsigned int  const imageSeq,
                  const char ** const outputNameP) {
/*----------------------------------------------------------------------------
   Compute the name of an output file given the pattern
   outputFilePattern[] and the image sequence number 'imageSeq'.
   outputFilePattern[] contains at least one instance of the string
   "%d" and we substitute the ASCII decimal representation of
   imageSeq for the firstone of them to generate the output file
   name.  We add leading zeroes as necessary to bring the number up to
   at least 'padCount' characters.
-----------------------------------------------------------------------------*/
    char * beforeSub;
    const char * afterSub;
    const char * filenameFormat;
        /* A format string for asprintfN for the file name */

    beforeSub = strdup(outputFilePattern);
    *(strstr(beforeSub, "%d")) = '\0';

    afterSub = strstr(outputFilePattern, "%d") + 2;

    /* Make filenameFormat something like "%s%04u%s" */
    asprintfN(&filenameFormat, "%%s%%0%ud%%s", padCount);

    asprintfN(outputNameP, filenameFormat, beforeSub, imageSeq, afterSub);

    strfree(filenameFormat);

    free(beforeSub);
}
Exemple #2
0
static void
getOrFakeAndMap(const char *      const andPgmFname,
                int               const xorCols,
                int               const xorRows,
                gray ***          const andPGMarrayP,
                pixval *          const andMaxvalP,
                colorhash_table * const andChtP,
                const char **     const errorP) {

    int andRows, andCols;
    
    if (!andPgmFname) {
        /* He's not supplying a bitmap for 'and'.  Fake the bitmap. */
        *andPGMarrayP = NULL;
        *andMaxvalP   = 1;
        *andChtP      = NULL;
        *errorP       = NULL;
    } else {
        FILE * andfile;
        andfile = pm_openr(andPgmFname);
        *andPGMarrayP = pgm_readpgm(andfile, &andCols, &andRows, andMaxvalP);
        pm_close(andfile);

        if ((andCols != xorCols) || (andRows != xorRows)) {
            asprintfN(errorP,
                      "And mask and image have different dimensions "
                     "(%d x %d vs %d x %d).  Aborting.",
                     andCols, xorCols, andRows, xorRows);
        } else
            *errorP = NULL;
    }
}
Exemple #3
0
static void
alloctupletable(const struct pam * const pamP, 
                unsigned int       const size,
                tupletable *       const tupletableP,
                const char **      const errorP) {
    
    if (UINT_MAX / sizeof(struct tupleint) < size)
        asprintfN(errorP, "size %u is too big for arithmetic", size);
    else {
        unsigned int const mainTableSize = size * sizeof(struct tupleint *);
        unsigned int const tupleIntSize = 
            sizeof(struct tupleint) - sizeof(sample) 
            + pamP->depth * sizeof(sample);

        /* To save the enormous amount of time it could take to allocate
           each individual tuple, we do a trick here and allocate everything
           as a single malloc block and suballocate internally.
        */
        if ((UINT_MAX - mainTableSize) / tupleIntSize < size)
            asprintfN(errorP, "size %u is too big for arithmetic", size);
        else {
            unsigned int const allocSize = mainTableSize + size * tupleIntSize;
            void * pool;
    
            pool = malloc(allocSize);

            if (!pool)
                asprintfN(errorP, "Unable to allocate %u bytes for a %u-entry "
                          "tuple table", allocSize, size);
            else {
                tupletable const tbl = (tupletable) pool;

                unsigned int i;

                *errorP = NULL;

                for (i = 0; i < size; ++i)
                    tbl[i] = (struct tupleint *)
                        ((char*)pool + mainTableSize + i * tupleIntSize);

                *tupletableP = tbl;
            }
        }
    }
}
Exemple #4
0
static void
makePalette(pixel **          const xorPPMarray,
            int               const xorCols,
            int               const xorRows,
            pixval            const xorMaxval,
            IC_Palette *      const paletteP,
            colorhash_table * const xorChtP,
            int *             const colorsP,
            const char **     const errorP) {
   /*
    * Figure out the colormap and turn it into the appropriate GIF
    * colormap - this code's pretty much straight from ppmtobpm
    */
    colorhist_vector xorChv;
    unsigned int i;
    int colors;
    IC_Palette palette = createCleanPalette();

    if (verbose) pm_message("computing colormap...");
    xorChv = ppm_computecolorhist(xorPPMarray, xorCols, xorRows, MAXCOLORS, 
                                  &colors);
    if (xorChv == NULL)
        asprintfN(errorP,
                  "image has too many colors - try doing a 'pnmquant %d'",
                  MAXCOLORS);
    else {
        *errorP = NULL;

        if (verbose) pm_message("%d colors found", colors);
        
        if (verbose && (xorMaxval > 255))
            pm_message("maxval is not 255 - automatically rescaling colors");
        for (i = 0; i < colors; ++i) {
            if (xorMaxval == 255) {
                addColorToPalette(palette,i,
                                  PPM_GETR(xorChv[i].color),
                                  PPM_GETG(xorChv[i].color),
                                  PPM_GETB(xorChv[i].color));
            } else {
                addColorToPalette(palette,i,
                                  PPM_GETR(xorChv[i].color) * 255 / xorMaxval,
                                  PPM_GETG(xorChv[i].color) * 255 / xorMaxval,
                                  PPM_GETB(xorChv[i].color) * 255 / xorMaxval);
            }
        }
        
        /* And make a hash table for fast lookup. */
        *xorChtP = ppm_colorhisttocolorhash(xorChv, colors);
        
        ppm_freecolorhist(xorChv);
        
        *paletteP = palette;
        *colorsP = colors;
    }
}
Exemple #5
0
static void
stringToUint(const char *   const string,
             unsigned int * const uintP,
             const char **  const errorP) {

    /* TODO: move this to nstring.c */

    if (strlen(string) == 0)
        asprintfN(errorP, "Value is a null string");
    else {
        char * tailptr;

        *uintP = strtoul(string, &tailptr, 10);

        if (*tailptr != '\0')
            asprintfN(errorP, "Non-numeric crap in string: '%s'", tailptr);
        else
            *errorP = NULL;
    }
}
Exemple #6
0
static void
parseGeometry(const char *   const wxl,
              unsigned int * const widthP,
              unsigned int * const heightP,
              const char **  const errorP) {

    char * const xPos = strchr(wxl, 'x');
    if (!xPos)
        asprintfN(errorP, "There is no 'x'.  It should be WIDTHxHEIGHT");
    else {
        *widthP  = atoi(wxl);
        *heightP = atoi(xPos + 1);

        if (*widthP == 0)
            asprintfN(errorP, "Width is zero.");
        else if (*heightP == 0)
            asprintfN(errorP, "Height is zero.");
        else
            *errorP = NULL;
    }
}
Exemple #7
0
void
pm_make_tmpfile(FILE **       const filePP,
                const char ** const filenameP) {

    int fd;
    FILE * fileP;
    const char * filenameTemplate;
    char * filenameBuffer;  /* malloc'ed */
    const char * tbuf;
    unsigned int fnamelen;
    const char * tmpdir;
    const char * dirseparator;

    fnamelen = strlen (pm_progname) + 10; /* "/" + "_XXXXXX\0" */

    tbuf = getenv("TMPDIR");

    if ((tbuf == NULL) || (strlen(tbuf) == 0))
        /* environment variable not suitable to construct file name.
           Use default.
        */
        tmpdir = TMPDIR;
    else
        tmpdir = tbuf;

    if (tmpdir[strlen(tmpdir) - 1] == '/')
        dirseparator = "";
    else
        dirseparator = "/";
    
    asprintfN(&filenameTemplate, "%s%s%s%s", 
              tmpdir, dirseparator, pm_progname, "_XXXXXX");

    if (filenameTemplate == NULL)
        pm_error("Unable to allocate storage for temporary file name");

    filenameBuffer = (char*)filenameTemplate;
    fd = mkstemp(filenameBuffer);

    if (fd < 0)
        pm_error("Unable to create temporary file.  mkstemp() failed with "
                 "errno %d (%s)", errno, strerror(errno));
    else {
        fileP = fdopen(fd, "w+b");

        if (fileP == NULL)
            pm_error("Unable to create temporary file.  fdopen() failed "
                     "with errno %d (%s)", errno, strerror(errno));
    }
    *filenameP = filenameBuffer;
    *filePP = fileP;
}
Exemple #8
0
const char *
pm_basename(const char * const fileName) {
/*----------------------------------------------------------------------------
   Return the filename portion of a file name, e.g. "foo.ppm" from
   "/home/bryanh/foo.ppm".

   Return it as a malloc'ed string.
-----------------------------------------------------------------------------*/
    unsigned int basenameStart;
    unsigned int i;
    const char * retval;

    basenameStart = 0;  /* initial assumption */

    for (i = 0; fileName[i]; ++i) {
        if (fileName[i] == '/')
            basenameStart = i+1;
    }
    asprintfN(&retval, "%s", &fileName[basenameStart]);

    return retval;
}
Exemple #9
0
int
main(int argc, char **argv) {

    struct cmdlineInfo cmdline;
    FILE *vf,*uf,*yf;
    int cols, rows;
    pixel *pixelrow1,*pixelrow2;
    int row;
    unsigned char  *y1buf,*y2buf,*ubuf,*vbuf;
    const char * ufname;
    const char * vfname;
    const char * yfname;

    ppm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);
        
    asprintfN(&ufname, "%s.U", cmdline.filenameBase);
    asprintfN(&vfname, "%s.V", cmdline.filenameBase);
    asprintfN(&yfname, "%s.Y", cmdline.filenameBase);

    uf = pm_openr(ufname);
    vf = pm_openr(vfname);
    yf = pm_openr(yfname);

    ppm_writeppminit(stdout, cmdline.width, cmdline.height, 255, 0);

    if (cmdline.width % 2 != 0) {
        pm_message("Warning: odd width; last column ignored");
        cols = cmdline.width - 1;
    } else
        cols = cmdline.width;

    if (cmdline.height % 2 != 0) {
        pm_message("Warning: odd height; last row ignored");
        rows = cmdline.height - 1;
    } else 
        rows = cmdline.height;

    pixelrow1 = ppm_allocrow(cols);
    pixelrow2 = ppm_allocrow(cols);

    MALLOCARRAY_NOFAIL(y1buf, cmdline.width);
    MALLOCARRAY_NOFAIL(y2buf, cmdline.width);
    MALLOCARRAY_NOFAIL(ubuf,  cmdline.width/2);
    MALLOCARRAY_NOFAIL(vbuf,  cmdline.width/2);

    for (row = 0; row < rows; row += 2) {
        fread(y1buf, cmdline.width,   1, yf);
        fread(y2buf, cmdline.width,   1, yf);
        fread(ubuf,  cmdline.width/2, 1, uf);
        fread(vbuf,  cmdline.width/2, 1, vf);

        computeTwoOutputRows(cols, cmdline.ccir601,
                             y1buf, y2buf, ubuf, vbuf,
                             pixelrow1, pixelrow2);

        ppm_writeppmrow(stdout, pixelrow1, cols, (pixval) 255, 0);
        ppm_writeppmrow(stdout, pixelrow2, cols, (pixval) 255, 0);
    }
    pm_close(stdout);

    strfree(yfname);
    strfree(vfname);
    strfree(ufname);

    pm_close(yf);
    pm_close(uf);
    pm_close(vf);

    exit(0);
}
Exemple #10
0
static void
writeJpc(jas_image_t *      const jasperP, 
         struct cmdlineInfo const cmdline,
         FILE *             const ofP) {

    jas_stream_t * outStreamP;
    const char * options;
    const char * ilyrratesOpt;
    const char * prgValue;
    char rateOpt[20+1];

    /* Note: ilyrrates is a hack because we're too lazy to properly parse
       command line options to get the information and then compose
       a proper input to Jasper.  So the user can screw things up by 
       specifying garbage for the -ilyrrates option 
    */
    if (strlen(cmdline.ilyrrates) > 0)
        asprintfN(&ilyrratesOpt, "ilyrrates=%s", cmdline.ilyrrates);
    else
        ilyrratesOpt = strdup("");

    switch(cmdline.progression) {
    case PROG_LRCP: prgValue = "lrcp"; break;
    case PROG_RLCP: prgValue = "rlcp"; break;
    case PROG_RPCL: prgValue = "rcpc"; break;
    case PROG_PCRL: prgValue = "pcrl"; break;
    case PROG_CPRL: prgValue = "cprl"; break;
    }

    /* Note that asprintfN() doesn't understand %f, but sprintf() does */

    sprintf(rateOpt, "%1.9f", 1.0/cmdline.compressionRatio);

    asprintfN(&options, 
              "imgareatlx=%u "
              "imgareatly=%u "
              "tilegrdtlx=%u "
              "tilegrdtly=%u "
              "tilewidth=%u "
              "tileheight=%u "
              "prcwidth=%u "
              "prcheight=%u "
              "cblkwidth=%u "
              "cblkheight=%u "
              "mode=%s "
              "rate=%s "
              "%s "
              "prg=%s "
              "numrlvls=%u "
              "numgbits=%u "
              "%s %s %s %s %s %s %s %s %s",

              cmdline.imgareatlx,
              cmdline.imgareatly,
              cmdline.tilegrdtlx,
              cmdline.tilegrdtlx,
              cmdline.tilewidth,
              cmdline.tileheight,
              cmdline.prcwidth,
              cmdline.prcheight,
              cmdline.cblkwidth,
              cmdline.cblkheight,
              cmdline.compmode == COMPMODE_INTEGER ? "int" : "real",
              rateOpt,
              ilyrratesOpt,
              prgValue,
              cmdline.numrlvls,
              cmdline.numgbits,
              cmdline.nomct     ? "nomct"     : "",
              cmdline.sop       ? "sop"       : "",
              cmdline.eph       ? "eph"       : "",
              cmdline.lazy      ? "lazy"      : "",
              cmdline.termall   ? "termall"   : "",
              cmdline.segsym    ? "segsym"    : "",
              cmdline.vcausal   ? "vcausal"   : "",
              cmdline.pterm     ? "pterm"     : "",
              cmdline.resetprob ? "resetprob" : ""
        );
    strfree(ilyrratesOpt);


    /* Open the output image file (Standard Output) */
    outStreamP = jas_stream_fdopen(fileno(ofP), "w+b");
    if (outStreamP == NULL)
        pm_error("Unable to open output stream.  jas_stream_fdopen() "
                 "failed");

    {
        int rc;

        if (cmdline.verbose)
            pm_message("Using Jasper to encode to 'jpc' format with options "
                       "'%s'", options);

        rc = jas_image_encode(jasperP, outStreamP, 
                              jas_image_strtofmt((char*)"jpc"), 
                              (char*)options);
        if (rc != 0)
            pm_error("jas_image_encode() failed to encode the JPEG 2000 "
                     "image.  Rc=%d", rc);
    }
	jas_stream_flush(outStreamP);

    {
        int rc;

        rc = jas_stream_close(outStreamP);
            
        if (rc != 0)
            pm_error("Failed to close output stream, "
                     "jas_stream_close() rc = %d", rc);
    }                     

	jas_image_clearfmts();

    strfree(options);
}