Esempio n. 1
0
static void ppm_writefile(uint8_t *prgb, int width, int height, int num)
{
  pixel *pixelrow;
  int i, x, y;

  pixelrow = ppm_allocrow(width);

  for (i = 0; i < num; i++) {
    char filename[16];
    FILE *fp;

    sprintf(filename, PPM_FILENAME, i);
    fp = fopen(filename, "w");

    ppm_writeppminit(fp, width, height, (pixval)255, 0);
    for (y = 0; y < height; y++) {
      for (x = 0; x < width; x++, prgb += 3)
	PPM_ASSIGN(pixelrow[x], prgb[0], prgb[1], prgb[2]);
      ppm_writeppmrow(fp, pixelrow, width, (pixval)255, 0);
    }

    fclose(fp);
  }

  ppm_freerow(pixelrow);
}
Esempio n. 2
0
static void
convertPpmRaster(FILE *          const ifP,
                 int             const format,
                 xelval          const maxval,
                 unsigned int    const cols,
                 unsigned int    const rows,
                 FILE *          const ofP,
                 unsigned int    const bytesPerLine,
                 unsigned char * const data) {

    pixel * const pixels = ppm_allocrow(cols);

    unsigned int row;

    for (row = 0; row < rows; ++row) {
        unsigned char * p;
        unsigned int col;
        size_t bytesWritten;

        p = &data[0];

        ppm_readppmrow(ifP, pixels, cols, maxval, format);

        for (col = 0; col < cols; ++col) {
            *p++ = PPM_GETR(pixels[col]);
            *p++ = PPM_GETG(pixels[col]);
            *p++ = PPM_GETB(pixels[col]);
        }
        bytesWritten =  fwrite(data, 1, bytesPerLine, ofP);
        if (bytesWritten != bytesPerLine)
            pm_error("File write error on Row %u", row);
    }
    ppm_freerow(pixels);
}
Esempio n. 3
0
int
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;
    pixel * pixrow;
    unsigned int row;

    ppm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ppm_writeppminit(stdout, cmdline.cols, cmdline.rows, cmdline.maxval, 0);
    pixrow = ppm_allocrow(cmdline.cols);

    for (row = 0; row < cmdline.rows; ++row) {
        unsigned int col;
        for (col = 0; col < cmdline.cols; ++col)
            pixrow[col] = cmdline.color;
        ppm_writeppmrow(stdout, pixrow, cmdline.cols, cmdline.maxval, 0);
	}

    ppm_freerow(pixrow);
    pm_close(stdout);

    return 0;
}
Esempio n. 4
0
int
main(int argc, char *argv[]) {
    int format;
    int rows, cols;
    pixval maxval;
    int row;
    pixel* pixelrow;
    
    ppm_init(&argc, argv);

    if (argc-1 != 0)
        pm_error("Program takes no arguments.  Input is from Standard Input");

    ppm_readppminit(stdin, &cols, &rows, &maxval, &format);

    ppm_writeppminit(stdout, cols, rows, maxval, 0);

    pixelrow = ppm_allocrow(cols);

    for (row = 0; row < rows; row++) {
        ppm_readppmrow(stdin, pixelrow, cols, maxval, format);
        ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0);
    }
    ppm_freerow(pixelrow);

    pm_close(stdin);

    exit(0);
}
Esempio n. 5
0
static void
writeOutput(FILE * const imageout_file,
            FILE * const alpha_file,
            int const cols, int const rows, 
            pixel * const colors, int * const data,
            int transparent) {
/*----------------------------------------------------------------------------
   Write the image in 'data' to open PPM file stream 'imageout_file',
   and the alpha mask for it to open PBM file stream 'alpha_file',
   except if either is NULL, skip it.

   'data' is an array of cols * rows integers, each one being an index
   into the colormap 'colors'.

   Where the index 'transparent' occurs in 'data', the pixel is supposed
   to be transparent.  If 'transparent' < 0, no pixels are transparent.
-----------------------------------------------------------------------------*/
    int row;
    pixel *pixrow;
    bit * alpharow;

    if (imageout_file)
        ppm_writeppminit(imageout_file, cols, rows, PPM_MAXMAXVAL, 0);
    if (alpha_file)
        pbm_writepbminit(alpha_file, cols, rows, 0);

    pixrow = ppm_allocrow(cols);
    alpharow = pbm_allocrow(cols);

    for (row = 0; row < rows; ++row ) {
        int col;
        int * const datarow = data+(row*cols);

        for (col = 0; col < cols; ++col) {
            pixrow[col] = colors[datarow[col]];
            if (datarow[col] == transparent)
                alpharow[col] = PBM_BLACK;
            else
                alpharow[col] = PBM_WHITE;
        }
        if (imageout_file)
            ppm_writeppmrow(imageout_file, 
                            pixrow, cols, (pixval) PPM_MAXMAXVAL, 0);
        if (alpha_file)
            pbm_writepbmrow(alpha_file, alpharow, cols, 0);
    }
    ppm_freerow(pixrow);
    pbm_freerow(alpharow);

    if (imageout_file)
        pm_close(imageout_file);
    if (alpha_file)
        pm_close(alpha_file);
}    
Esempio n. 6
0
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;
}
Esempio n. 7
0
static void
fillPpmBins(FILE *          const ifP,
            unsigned int    const cols,
            unsigned int    const rows,
            xelval          const maxval,
            int             const format,
            bool            const colorWanted[3],
            bool            const verbose,
            xelval          const startval,
            xelval          const endval,
            unsigned int    const histWidth,
            unsigned int ** const hist) {
/*----------------------------------------------------------------------------
   For each wanted color component, given by colorWanted[], hist[color] is the
   histogram.  Each histogram as 'histWidth' bins; we ignore color component
   values less than 'startval' and greater than or equal to 'endval' and
   spread the rest evenly across the 'histWidth' bins.

   We get the color component values from the PNM image on *ifP,
   which is positioned to the raster, whose format is described
   by 'cols', 'rows', 'maxval', and 'format'.
-----------------------------------------------------------------------------*/
    pixel * pixrow;
    unsigned int row;

    pixrow = ppm_allocrow(cols);

    if (verbose)
        pm_message("making histogram...");

    for (row = 0; row < rows; ++row) {
        unsigned int col;
        ppm_readppmrow(ifP, pixrow, cols, maxval, format);
        for (col = 0; col < cols; ++col) {
            if (colorWanted[WANT_RED])
                countComp(PPM_GETR(pixrow[col]),
                          startval, endval, histWidth, hist[WANT_RED]);

            if (colorWanted[WANT_GRN])
                countComp(PPM_GETG(pixrow[col]),
                          startval, endval, histWidth, hist[WANT_GRN]);

            if (colorWanted[WANT_BLU])
                countComp(PPM_GETB(pixrow[col]),
                          startval, endval, histWidth, hist[WANT_BLU]);
        }
    }
    ppm_freerow(pixrow);
}
Esempio n. 8
0
int
main(int argc, char *argv[]) {

    FILE* ifP;
    const char * inputFilespec;
    int eof;
    
    ppm_init( &argc, argv );

    if (argc-1 > 1)
        pm_error("The only argument is the (optional) input filename");

    if (argc == 2)
        inputFilespec = argv[1];
    else
        inputFilespec = "-";
    
    ifP = pm_openr(inputFilespec);

    eof = FALSE;  /* initial assumption */

    while (!eof) {
        ppm_nextimage(ifP, &eof);
        if (!eof) {
            int rows, cols, format;
            pixval maxval;
            pixel* inputRow;
            gray* outputRow;

            ppm_readppminit(ifP, &cols, &rows, &maxval, &format);
            pgm_writepgminit(stdout, cols, rows, maxval, 0);

            inputRow = ppm_allocrow(cols);
            outputRow = pgm_allocrow(cols);

            convertRaster(ifP, cols, rows, maxval, format, 
                          inputRow, outputRow, stdout);

            ppm_freerow(inputRow);
            pgm_freerow(outputRow);
        }
    }
    pm_close(ifP);
    pm_close(stdout);

    return 0;
}
Esempio n. 9
0
static void
writePpm(FILE *             const ifP,
         const xvPalette *  const xvPaletteP,
         unsigned int       const cols,
         unsigned int       const rows,
         pixval             const maxval,
         FILE *             const ofP) {
    /*----------------------------------------------------------------------------
       Write out the PPM image, from the XV-mini input file ifP, which is
       positioned to the raster.

       The raster contains indices into the palette *xvPaletteP.
    -----------------------------------------------------------------------------*/
    pixel * pixrow;
    unsigned int row;

    pixrow = ppm_allocrow(cols);

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

    for (row = 0; row < rows; ++row) {
        unsigned int col;
        for (col = 0; col < cols; ++col) {
            int byte;
            byte = fgetc(ifP);
            if (byte == EOF)
                pm_error("unexpected EOF");
            else {
                unsigned int const paletteIndex = byte;
                assert(byte >= 0);

                PPM_ASSIGN(pixrow[col],
                           xvPaletteP->red[paletteIndex],
                           xvPaletteP->grn[paletteIndex],
                           xvPaletteP->blu[paletteIndex]);
            }
        }
        ppm_writeppmrow(ofP, pixrow, cols, maxval, 0);
    }

    ppm_freerow(pixrow);
}
Esempio n. 10
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;
}
Esempio n. 11
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;
}
Esempio n. 12
0
int main(int argc, char *argv[])
{
    FILE           *ifd;
    FILE       *ofd;
    int             rows, cols;
    xelval          maxval;
    int             format;
    const char     * const usage = "[-resolution x y] [pnmfile [ddiffile]]";
    int             i, j;
    char           *outfile;
    int       argn;
    int hor_resolution = 75;
    int ver_resolution = 75;
    imageparams ip;
    unsigned char  *data, *p;

    pnm_init(&argc, argv);

    for (argn = 1;argn < argc && argv[argn][0] == '-';argn++) {
        int arglen = strlen(argv[argn]);

        if (!strncmp (argv[argn],"-resolution", arglen)) {
            if (argn + 2 < argc) {
                hor_resolution = atoi(argv[argn+1]);
                ver_resolution = atoi(argv[argn+2]);
                argn += 2;
                continue;
            } else {
                pm_usage(usage);
            }
        } else {
            pm_usage(usage);
        }
    }

    if (hor_resolution <= 0 || ver_resolution <= 0) {
        fprintf(stderr,"Unreasonable resolution values: %d x %d\n",
                hor_resolution,ver_resolution);
        exit(1);
    }

    if (argn == argc - 2) {
        ifd = pm_openr(argv[argn]);
        outfile = argv[argn+1];
        if (!(ofd = fopen(outfile,"wb"))) {
            perror(outfile);
            exit(1);
        }
    } else if (argn == argc - 1) {
        ifd = pm_openr(argv[argn]);
        ofd = stdout;
    } else {
        ifd = stdin;
        ofd = stdout;
    }

    pnm_readpnminit(ifd, &cols, &rows, &maxval, &format);

    ip.width = cols;
    ip.height = rows;
    ip.h_res = hor_resolution;
    ip.v_res = ver_resolution;

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
        ip.bits_per_pixel = 1;
        ip.bytes_per_line = (cols + 7) / 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 1;
        ip.polarity = 1;
        break;
    case PGM_TYPE:
        ip.bytes_per_line = cols;
        ip.bits_per_pixel = 8;
        ip.spectral = 2;
        ip.components = 1;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    case PPM_TYPE:
        ip.bytes_per_line = 3 * cols;
        ip.bits_per_pixel = 24;
        ip.spectral = 5;
        ip.components = 3;
        ip.bits_per_component = 8;
        ip.polarity = 2;
        break;
    default:
        fprintf(stderr, "Unrecognized PBMPLUS format %d\n", format);
        exit(1);
    }

    if (!write_header(ofd,&ip)) {
        perror("Writing header");
        exit(1);
    }

    if (!(p = data = (unsigned char*)  malloc(ip.bytes_per_line))) {
        perror("allocating line buffer");
        exit(1);
    }

    switch (PNM_FORMAT_TYPE(format)) {
    case PBM_TYPE:
    {
        bit            *pixels;
        int             mask;
        int             k;

        pixels = pbm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            pbm_readpbmrow(ifd, pixels, cols, format);
            mask = 0;
            p = data;
            for (j = 0, k = 0; j < cols; j++) {
                if (pixels[j] == PBM_BLACK) {
                    mask |= 1 << k;
                }
                if (k == 7) {
                    *p++ = mask;
                    mask = 0;
                    k = 0;
                } else {
                    k++;
                }
            }
            if (k != 7) {       /* Flush the rest of the column */
                *p = mask;
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
    }
    break;
    case PGM_TYPE:
    {
        gray          *pixels = pgm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            p = data;
            pgm_readpgmrow(ifd, pixels, cols, maxval, format);
            for (j = 0; j < cols; j++) {
                *p++ = (unsigned char) pixels[j];
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
        pgm_freerow(pixels);
    }
    break;
    case PPM_TYPE:
    {
        pixel          *pixels = ppm_allocrow(cols);

        for (i = 0; i < rows; i++) {
            p = data;
            ppm_readppmrow(ifd, pixels, cols, maxval, format);
            for (j = 0; j < cols; j++) {
                *p++ = PPM_GETR(pixels[j]);
                *p++ = PPM_GETG(pixels[j]);
                *p++ = PPM_GETB(pixels[j]);
            }
            if (fwrite(data,1,ip.bytes_per_line,ofd) != ip.bytes_per_line) {
                perror("Writing image data\n");
                exit(1);
            }
        }
        ppm_freerow(pixels);
    }
    break;
    }

    pm_close(ifd);

    free(data);

    if (!write_trailer(ofd)) {
        perror("Writing trailer");
        exit(1);
    }

    if (fclose(ofd) == EOF) {
        perror("Closing output file");
        exit(1);
    };

    return(0);
}
Esempio n. 13
0
static void
readXpm1Header(FILE * const stream, int * const widthP, int * const heightP, 
               int * const chars_per_pixelP, int * const ncolorsP, 
               pixel ** const colorsP, int ** const ptabP) {
/*----------------------------------------------------------------------------
  Read the header of the XPM file on stream 'stream'.  Assume the
  getline() stream is presently positioned to the beginning of the
  file and it is a Version 1 XPM file.  Leave the stream positioned
  after the header.
  
  Return the information from the header the same as for readXpm3Header.
-----------------------------------------------------------------------------*/
    char line[MAX_LINE+1], str1[MAX_LINE+1], str2[MAX_LINE+1];
    char *t1;
    char *t2;
    int format;
    unsigned int v;
    int i, j;
    bool processedStaticChar;  
        /* We have read up to and interpreted the "static char..." line */

    *widthP = *heightP = *ncolorsP = *chars_per_pixelP = format = -1;

    /* Read the initial defines. */
    processedStaticChar = FALSE;
    while (!processedStaticChar) {
        getline(line, sizeof(line), stream);

        if (sscanf(line, "#define %s %d", str1, &v) == 2) {
            char *t1;
            if ((t1 = strrchr(str1, '_')) == NULL)
                t1 = str1;
            else
                ++t1;
            if (STREQ(t1, "format"))
                format = v;
            else if (STREQ(t1, "width"))
                *widthP = v;
            else if (STREQ(t1, "height"))
                *heightP = v;
            else if (STREQ(t1, "ncolors"))
                *ncolorsP = v;
            else if (STREQ(t1, "pixel"))
                *chars_per_pixelP = v;
        } else if (!strncmp(line, "static char", 11)) {
            if ((t1 = strrchr(line, '_')) == NULL)
                t1 = line;
            else
                ++t1;
            processedStaticChar = TRUE;
        }
    }
    /* File is positioned to "static char" line, which is in line[] and
       t1 points to position of last "_" in the line, or the beginning of
       the line if there is no "_"
    */
    if (format == -1)
        pm_error("missing or invalid format");
    if (format != 1)
        pm_error("can't handle XPM version %d", format);
    if (*widthP == -1)
        pm_error("missing or invalid width");
    if (*heightP == -1)
        pm_error("missing or invalid height");
    if (*ncolorsP == -1)
        pm_error("missing or invalid ncolors");
    if (*chars_per_pixelP == -1)
        pm_error("missing or invalid *chars_per_pixelP");
    if (*chars_per_pixelP > 2)
        pm_message("WARNING: *chars_per_pixelP > 2 uses a lot of memory");

    /* If there's a monochrome color table, skip it. */
    if (!strncmp(t1, "mono", 4)) {
        for (;;) {
            getline(line, sizeof(line), stream);
            if (!strncmp(line, "static char", 11))
                break;
        }
    }
    /* Allocate space for color table. */
    if (*chars_per_pixelP <= 2) {
        /* Up to two chars per pixel, we can use an indexed table. */
        v = 1;
        for (i = 0; i < *chars_per_pixelP; ++i)
            v *= 256;
        *colorsP = ppm_allocrow(v);
        *ptabP = NULL;
    } else {
        /* Over two chars per pixel, we fall back on linear search. */
        *colorsP = ppm_allocrow(*ncolorsP);
        MALLOCARRAY(*ptabP, *ncolorsP);
        if (*ptabP == NULL)
            pm_error("Unable to allocate memory for %d colors", *ncolorsP);
    }

    /* Read color table. */
    for (i = 0; i < *ncolorsP; ++i) {
        getline(line, sizeof(line), stream);

        if ((t1 = strchr(line, '"')) == NULL)
            pm_error("D error scanning color table");
        if ((t2 = strchr(t1 + 1, '"')) == NULL)
            pm_error("E error scanning color table");
        if (t2 - t1 - 1 != *chars_per_pixelP)
            pm_error("wrong number of chars per pixel in color table");
        strncpy(str1, t1 + 1, t2 - t1 - 1);
        str1[t2 - t1 - 1] = '\0';

        if ((t1 = strchr(t2 + 1, '"')) == NULL)
            pm_error("F error scanning color table");
        if ((t2 = strchr(t1 + 1, '"')) == NULL)
            pm_error("G error scanning color table");
        strncpy(str2, t1 + 1, t2 - t1 - 1);
        str2[t2 - t1 - 1] = '\0';

        v = 0;
        for (j = 0; j < *chars_per_pixelP; ++j)
            v = (v << 8) + str1[j];
        if (*chars_per_pixelP <= 2)
            /* Index into table. */
            (*colorsP)[v] = ppm_parsecolor(str2,
                                           (pixval) PPM_MAXMAXVAL);
        else {
            /* Set up linear search table. */
            (*colorsP)[i] = ppm_parsecolor(str2,
                                           (pixval) PPM_MAXMAXVAL);
            (*ptabP)[i] = v;
        }
    }
    /* Position to first line of raster (which is the line after
       "static char ...").
    */
    for (;;) {
        getline(line, sizeof(line), stream);
        if (strncmp(line, "static char", 11) == 0)
            break;
    }
}
Esempio n. 14
0
static void
readXpm3Header(FILE * const stream, int * const widthP, int * const heightP, 
               int * const chars_per_pixelP, int * const ncolorsP,
               pixel ** const colorsP, int ** const ptabP,
               int * const transparentP) {
/*----------------------------------------------------------------------------
  Read the header of the XPM file on stream 'stream'.  Assume the
  getline() stream is presently positioned to the beginning of the
  file and it is a Version 3 XPM file.  Leave the stream positioned
  after the header.

  We have two ways to return the colormap, depending on the number of
  characters per pixel in the XPM:  
  
  If it is 1 or 2 characters per pixel, we return the colormap as a
  Netpbm 'pixel' array *colorsP (in newly malloc'ed storage), such
  that if a color in the raster is identified by index N, then
  (*colorsP)[N] is that color.  So this array is either 256 or 64K
  pixels.  In this case, we return *ptabP = NULL.

  If it is more than 2 characters per pixel, we return the colormap as
  both a Netpbm 'pixel' array *colorsP and a lookup table *ptabP (both
  in newly malloc'ed storage).

  If a color in the raster is identified by index N, then for some I,
  (*ptabP)[I] is N and (*colorsP)[I] is the color in question.  So 
  you iterate through *ptabP looking for N and then look at the 
  corresponding entry in *colorsP to get the color.

  Return as *transColorNumberP the value of the XPM color number that
  represents a transparent pixel, or -1 if no color number does.
-----------------------------------------------------------------------------*/
    char line[MAX_LINE+1];
    const char * xpm3_signature = "/* XPM */";
    
    *widthP = *heightP = *ncolorsP = *chars_per_pixelP = -1;

    /* Read the XPM signature comment */
    getline(line, sizeof(line), stream);
    if (strncmp(line, xpm3_signature, strlen(xpm3_signature)) != 0) 
        pm_error("Apparent XPM 3 file does not start with '/* XPM */'.  "
                 "First line is '%s'", xpm3_signature);

    /* Read the assignment line */
    getline(line, sizeof(line), stream);
    if (strncmp(line, "static char", 11) != 0)
        pm_error("Cannot find data structure declaration.  Expected a "
                 "line starting with 'static char', but found the line "
                 "'%s'.", line);

	/* Read the hints line */
    getline(line, sizeof(line), stream);
    /* skip the comment line if any */
    if (!strncmp(line, "/*", 2)) {
        while (!strstr(line, "*/"))
            getline(line, sizeof(line), stream);
        getline(line, sizeof(line), stream);
    }
    if (sscanf(line, "\"%d %d %d %d\",", widthP, heightP,
               ncolorsP, chars_per_pixelP) != 4)
        pm_error("error scanning hints line");

    if (verbose == 1) 
    {
        pm_message("Width x Height:  %d x %d", *widthP, *heightP);
        pm_message("no. of colors:  %d", *ncolorsP);
        pm_message("chars per pixel:  %d", *chars_per_pixelP);
    }

    /* Allocate space for color table. */
    if (*chars_per_pixelP <= 2) {
        /* Set up direct index (see above) */
        *colorsP = ppm_allocrow(*chars_per_pixelP == 1 ? 256 : 256*256);
        *ptabP = NULL;
    } else {
        /* Set up lookup table (see above) */
        *colorsP = ppm_allocrow(*ncolorsP);
        MALLOCARRAY(*ptabP, *ncolorsP);
        if (*ptabP == NULL)
            pm_error("Unable to allocate memory for %d colors", *ncolorsP);
    }
    
    { 
        /* Read the color table */
        int seqNum;
            /* Sequence number of entry within color table in XPM header */

        *transparentP = -1;  /* initial value */

        for (seqNum = 0; seqNum < *ncolorsP; seqNum++) {
            getline(line, sizeof(line), stream);
            /* skip the comment line if any */
            if (!strncmp(line, "/*", 2))
                getline(line, sizeof(line), stream);
            
            interpretXpm3ColorTableLine(line, seqNum, *chars_per_pixelP, 
                                        *colorsP, *ptabP, transparentP);
        }
    }
}
Esempio n. 15
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);
}
Esempio n. 16
0
int
main (int argc, char *argv[]) {

    int offset; 
    int cols, rows, row;
    pixel* pixelrow;
    pixval maxval;

    FILE* Lifp;
    pixel* Lpixelrow;
    gray* Lgrayrow;
    int Lrows, Lcols, Lformat;
    pixval Lmaxval;
   
    FILE* Rifp;
    pixel* Rpixelrow;
    gray* Rgrayrow;
    int Rrows, Rcols, Rformat;
    pixval Rmaxval;
   
    ppm_init (&argc, argv);

    if (argc-1 > 3 || argc-1 < 2) 
        pm_error("Wrong number of arguments (%d).  Arguments are "
                 "leftppmfile rightppmfile [horizontal_offset]", argc-1);

    Lifp = pm_openr (argv[1]);
    Rifp = pm_openr (argv[2]);

    if (argc-1 >= 3) 
        offset = atoi (argv[3]);
    else
        offset = 30;

    ppm_readppminit (Lifp, &Lcols, &Lrows, &Lmaxval, &Lformat);
    ppm_readppminit (Rifp, &Rcols, &Rrows, &Rmaxval, &Rformat);
    
    if ((Lcols != Rcols) || (Lrows != Rrows) || 
        (Lmaxval != Rmaxval) || 
        (PPM_FORMAT_TYPE(Lformat) != PPM_FORMAT_TYPE(Rformat)))
        pm_error ("Pictures are not of same size and format");
    
    cols = Lcols;
    rows = Lrows;
    maxval = Lmaxval;
   
    ppm_writeppminit (stdout, cols, rows, maxval, 0);
    Lpixelrow = ppm_allocrow (cols);
    Lgrayrow = pgm_allocrow (cols);
    Rpixelrow = ppm_allocrow (cols);
    Rgrayrow = pgm_allocrow (cols);
    pixelrow = ppm_allocrow (cols);

    for (row = 0; row < rows; ++row) {
        ppm_readppmrow(Lifp, Lpixelrow, cols, maxval, Lformat);
        ppm_readppmrow(Rifp, Rpixelrow, cols, maxval, Rformat);

        computeGrayscaleRow(Lpixelrow, Lgrayrow, maxval, cols);
        computeGrayscaleRow(Rpixelrow, Rgrayrow, maxval, cols);
        {
            int col;
            gray* LgP;
            gray* RgP;
            pixel* pP;
            for (col = 0, pP = pixelrow, LgP = Lgrayrow, RgP = Rgrayrow;
                 col < cols + offset;
                 ++col) {
            
                if (col < offset/2)
                    ++LgP;
                else if (col >= offset/2 && col < offset) {
                    const pixval Blue = (pixval) (float) *LgP;
                    const pixval Red = (pixval) 0;
                    PPM_ASSIGN (*pP, Red, Blue, Blue);
                    ++LgP;
                    ++pP;
                } else if (col >= offset && col < cols) {
                    const pixval Red = (pixval) (float) *RgP;
                    const pixval Blue = (pixval) (float) *LgP;
                    PPM_ASSIGN (*pP, Red, Blue, Blue);
                    ++LgP;
                    ++RgP;
                    ++pP;
                } else if (col >= cols && col < cols + offset/2) {
                    const pixval Blue = (pixval) 0;
                    const pixval Red = (pixval) (float) *RgP;
                    PPM_ASSIGN (*pP, Red, Blue, Blue);
                    ++RgP;
                    ++pP;
                } else
                    ++RgP;
            }
        }    
        ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0);
    }

    pm_close(Lifp);
    pm_close(Rifp);
    pm_close(stdout);

    return 0;
}
Esempio n. 17
0
int
main(int argc, const char ** argv) {

    FILE * ifP;
    unsigned int i;
    pixel * pixelrow;
    unsigned int row;
    Pal pal;
    short screen[ROWS*COLS/4];      /* simulates the Atari's video RAM */

    pm_proginit(&argc, argv);

    /* Check args. */
    if ( argc > 2 )
        pm_usage( "[spufile]" );

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

    /* Read the SPU file */

    /* Read the screen data. */
    for (i = 0; i < ROWS*COLS/4; ++i)
        pm_readbigshort(ifP, &screen[i]);

    readPalettes(ifP, &pal);

    pm_close(ifP);

    /* Ok, get set for writing PPM. */
    ppm_writeppminit(stdout, COLS, ROWS, MAXVAL, 0);
    pixelrow = ppm_allocrow(COLS);

    /* Now do the conversion. */
    for (row = 0; row < ROWS; ++row) {
        unsigned int col;
        for (col = 0; col < COLS; ++col) {
            /* Compute pixel value. */
            unsigned int const ind = 80 * row + ((col >> 4) << 2);
            unsigned int const b = 0x8000 >> (col & 0xf);
            unsigned int c;
            unsigned int plane;
            unsigned int x1;

            c = 0;  /* initial value */
            for (plane = 0; plane < 4; ++plane) {
                if (b & screen[ind + plane])
                    c |= (1 << plane);
            }
            /* Compute palette index. */
            x1 = 10 * c;
            if ((c & 1) != 0)
                x1 -= 5;
            else
                ++x1;
            if ((col >= x1 ) && (col < (x1 + 160)))
                c += 16;
            if (col >= (x1 + 160))
                c += 32;

            /* Set the proper color. */
            pixelrow[col] = pal.pal[row][c];
        }
        ppm_writeppmrow(stdout, pixelrow, COLS, MAXVAL, 0);
    }

    ppm_freerow(pixelrow);
    pm_close(stdout);

    return 0;
}