Beispiel #1
0
void
pnm_writepnmrow(FILE *      const fileP, 
                const xel * const xelrow, 
                int         const cols, 
                xelval      const maxval, 
                int         const format, 
                int         const forceplain) {

    bool const plainFormat = forceplain || pm_plain_output;
    
    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        ppm_writeppmrow(fileP, (pixel*) xelrow, cols, (pixval) maxval, 
                        plainFormat);
        break;

    case PGM_TYPE:
        writepgmrow(fileP, xelrow, cols, maxval, format, plainFormat);
        break;

    case PBM_TYPE:
        writepbmrow(fileP, xelrow, cols, plainFormat);
        break;
    
    default:
        pm_error("invalid format argument received by pnm_writepnmrow(): %d"
                 "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", 
                 format, PBM_TYPE, PGM_TYPE, PPM_TYPE);
    }
}
Beispiel #2
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);
}
Beispiel #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;
}
Beispiel #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);
}
Beispiel #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);
}    
Beispiel #6
0
void
pnm_writepnmrow(FILE * const fileP, 
                xel *  const xelrow, 
                int    const cols, 
                xelval const maxval, 
                int    const format, 
                int    const forceplain) {

    bool const plainFormat = forceplain || pm_plain_output;
    
    switch (PNM_FORMAT_TYPE(format)) {
    case PPM_TYPE:
        ppm_writeppmrow(fileP, (pixel*) xelrow, cols, (pixval) maxval, 
                        plainFormat);
        break;

    case PGM_TYPE: {
        gray* grayrow;
        unsigned int col;

        grayrow = pgm_allocrow(cols);

        for (col = 0; col < cols; ++col)
            grayrow[col] = PNM_GET1(xelrow[col]);

        pgm_writepgmrow(fileP, grayrow, cols, (gray) maxval, plainFormat);

        pgm_freerow( grayrow );
    }
    break;

    case PBM_TYPE: {
        bit* bitrow;
        unsigned int col;

        bitrow = pbm_allocrow(cols);

        for (col = 0; col < cols; ++col)
            bitrow[col] = PNM_GET1(xelrow[col]) == 0 ? PBM_BLACK : PBM_WHITE;

        pbm_writepbmrow(fileP, bitrow, cols, plainFormat);

        pbm_freerow(bitrow);
    }    
    break;
    
    default:
        pm_error("invalid format argument received by pnm_writepnmrow(): %d"
                 "PNM_FORMAT_TYPE(format) must be %d, %d, or %d", 
                 format, PBM_TYPE, PGM_TYPE, PPM_TYPE);
    }
}
Beispiel #7
0
void
ppm_writeppm(FILE *  const file, 
             pixel** const pixels, 
             int     const cols, 
             int     const rows, 
             pixval  const maxval, 
             int     const forceplain)  {
    int row;
    
    ppm_writeppminit(file, cols, rows, maxval, forceplain);
    
    for (row = 0; row < rows; ++row)
        ppm_writeppmrow(file, pixels[row], cols, maxval, forceplain);
}
Beispiel #8
0
static void
convertLinear(FILE * const ifP,
              unsigned int const cols,
              unsigned int const rows,
              gray         const maxval,
              int          const format,
              const char * const colorNameBlack,
              const char * const colorNameWhite,
              FILE *       const ofP,
              gray *       const grayrow,
              pixel *      const pixelrow) {

    pixel colorBlack, colorWhite;
    pixval red0, grn0, blu0, red1, grn1, blu1;
    unsigned int row;

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

    colorBlack = ppm_parsecolor(colorNameBlack, maxval);
    colorWhite = ppm_parsecolor(colorNameWhite, maxval);
 
    red0 = PPM_GETR(colorBlack);
    grn0 = PPM_GETG(colorBlack);
    blu0 = PPM_GETB(colorBlack);
    red1 = PPM_GETR(colorWhite);
    grn1 = PPM_GETG(colorWhite);
    blu1 = PPM_GETB(colorWhite);

    for (row = 0; row < rows; ++row) {
        unsigned int col;

        pgm_readpgmrow(ifP, grayrow, cols, maxval, format);

        for (col = 0; col < cols; ++col) {
            gray const input = grayrow[col];
            PPM_ASSIGN(
                pixelrow[col],
                (red0 * (maxval - input) + red1 * input) / maxval,
                (grn0 * (maxval - input) + grn1 * input) / maxval,
                (blu0 * (maxval - input) + blu1 * input) / maxval);
        }
        ppm_writeppmrow(ofP, pixelrow, cols, maxval, 0);
    }
}
Beispiel #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);
}
Beispiel #10
0
static void
convertWithMap(FILE * const ifP,
               unsigned int const cols,
               unsigned int const rows,
               gray         const maxval,
               int          const format,
               const char * const mapFileName,
               FILE *       const ofP,
               gray *       const grayrow,
               pixel *      const pixelrow) {

    unsigned int row;
    FILE * mapFileP;
    int mapcols, maprows;
    pixval mapmaxval;
    pixel ** mappixels;
    unsigned int mapmaxcolor;
    
    mapFileP = pm_openr(mapFileName);
    mappixels = ppm_readppm(mapFileP, &mapcols, &maprows, &mapmaxval);
    pm_close(mapFileP);
    mapmaxcolor = maprows * mapcols - 1;

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

    for (row = 0; row < rows; ++row) {
        unsigned int col;
            
        pgm_readpgmrow(ifP, grayrow, cols, maxval, format);

        for (col = 0; col < cols; ++col) {
            unsigned int c;
            if (maxval == mapmaxcolor)
                c = grayrow[col];
            else
                c = grayrow[col] * mapmaxcolor / maxval;
            pixelrow[col] = mappixels[c / mapcols][c % mapcols];
        }
        ppm_writeppmrow(ofP, pixelrow, cols, mapmaxval, 0);
    }
    ppm_freearray(mappixels, maprows);
}
Beispiel #11
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);
}
Beispiel #12
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;
}
Beispiel #13
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;
}