Esempio n. 1
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. 2
0
static void
convertRaster(FILE *       const ifP,
              unsigned int const cols,
              unsigned int const rows,
              pixval       const maxval,
              int          const format, 
              pixel *      const inputRow,
              gray *       const outputRow, 
              FILE *       const ofP) {

    unsigned int row;

    for (row = 0; row < rows; ++row) {
        ppm_readppmrow( ifP, inputRow, cols, maxval, format );
        if (maxval <= 255) {
            /* Use fast approximation to 0.299 r + 0.587 g + 0.114 b */
            unsigned int col;
            for (col = 0; col < cols; ++col)
                outputRow[col] = (gray) ppm_fastlumin(inputRow[col]);
        } else {
            /* Can't use fast approximation, so fall back on floats. */
            unsigned int col;
            for (col = 0; col < cols; ++col) 
                outputRow[col] = ppm_luminosity(inputRow[col]);
        }
        pgm_writepgmrow(ofP, outputRow, cols, maxval, 0);
    }
}
Esempio n. 3
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. 4
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. 5
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. 6
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. 7
0
xImage::xImage (xScreen *scr, char *name)
{
	char buf [100];
	int wid, hgh, format;
	pixval maxval;

	// Build file name with pixmap.

	int pipeflag = 0;

	// Remove suffix .Z from file name.
	char *p = name;
	while (*p) ++p;
	if (*--p == 'Z' && *--p == '.')
		*p = 0;

	if (access (name, 4) == 0)
		strcpy (buf, name);
	else {
		strcpy (buf, name);
		strcat (buf, ".Z");
		if (access (buf, 4) == 0)
			pipeflag = 1;
		else if (! contains (name, '/')) {
			strcpy (buf, "/usr/include/X11/pixmaps/");
			strcat (buf, name);
			if (access (buf, 4) != 0) {
				strcat (buf, ".Z");
				if (access (buf, 4) == 0)
					pipeflag = 1;
			}
		}
	}

	// Open file, read PPM header, create array for row of pixels

	FILE *fd;
	if (pipeflag) {
		char cmd [80];
		strcpy (cmd, "/usr/bin/X11/uncompress -c ");
		strcat (cmd, buf);
		fd = popen (cmd, "r");
	} else
		fd = fopen (buf, "r");
	if (! fd)
		xFatal ("Cannot open image file\n");
	ppm_readppminit (fd, &wid, &hgh, &maxval, &format);
	pixel *pixels = new pixel [wid];

	// Create Xlib image structure.

	screen = scr;
	width = wid;
	height = hgh;
	ximage = XCreateImage (screen->xdpy, screen->xvisual, screen->depth,
		ZPixmap, 0, (char *) 0, width, height, 8, 0);
	if (! ximage)
		xFatal ("Cannot allocate XImage\n");
	ximage->data = new char [ximage->bytes_per_line * height];
	if (! ximage->data)
		xFatal ("Cannot allocate image data\n");

	// Read in pixels.

	for (int y=0; y<height; ++y) {
		ppm_readppmrow (fd, pixels, wid, maxval, format);
		pixel *p = pixels;
		for (int x=0; x<width; ++x, ++p) {
			int r = PPM_GETR (*p) * xColorMax / maxval;
			int g = PPM_GETG (*p) * xColorMax / maxval;
			int b = PPM_GETB (*p) * xColorMax / maxval;
			xPixel xpix = screen->Pixel (r, g, b);
			PutPixel (x, y, xpix);
		}
	}
	delete pixels;
	if (pipeflag)
		pclose (fd);
	else
		fclose (fd);
}
Esempio n. 8
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. 9
0
int
main(int argc, const char ** argv) {

    FILE * ifP;
    FILE *vf, *uf, *yf;
    pixel *pixelrow1, *pixelrow2;
    int rows, cols;
    int format;
    unsigned int row;
    pixval maxval;
    unsigned char *y1buf, *y2buf, *ubuf, *vbuf;
    struct FileNameSet fname;
        /* Output file names - .U, .V, .Y */

    pm_proginit(&argc, argv);

    if ((argc-1 > 2) || (argc-1 < 1))
        pm_error("Wrong number of arguments: %u.  "
                 "Arguments are basename for output files "
                 "and optional input file name", argc-1);

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

    makeOutputFileName(argv[1], &fname);

    uf = pm_openw(fname.u);
    vf = pm_openw(fname.v);
    yf = pm_openw(fname.y);

    termFileNameSet(fname);

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

    if (cols % 2 == 1)
        pm_message("Warning: odd columns count %u, excess ignored", cols);

    if (rows % 2 == 1)
        pm_message("Warning: odd rows count %u, excess ignored", rows);

    pixelrow1 = ((pixel*) pm_allocrow(cols, sizeof(pixel)));
    pixelrow2 = ((pixel*) pm_allocrow(cols, sizeof(pixel)));

    y1buf = (unsigned char *) pm_allocrow(cols, 1);
    y2buf = (unsigned char *) pm_allocrow(cols, 1);
    ubuf = (unsigned char *) pm_allocrow(cols, 1);
    vbuf = (unsigned char *) pm_allocrow(cols, 1);

    for (row = 0; row < (rows & ~1); row += 2) {
        unsigned char *y1ptr, *y2ptr, *uptr, *vptr;
        pixel *pP1, *pP2;
        unsigned int col;

        ppm_readppmrow(ifP, pixelrow1, cols, maxval, format);
        ppm_readppmrow(ifP, pixelrow2, cols, maxval, format);

        pP1 = &pixelrow1[0]; pP2 = &pixelrow2[0];
        y1ptr = y1buf; y2ptr = y2buf; vptr = vbuf; uptr = ubuf;

        for (col = 0 ; col < (cols & ~1); col += 2) {
            pixval r0,g0,b0,r1,g1,b1,r2,g2,b2,r3,g3,b3;
            myLONG u, v, y0, y1, y2, y3, u0, u1, u2, u3, v0, v1, v2, v3;

            /* first pixel */
            r0 = PPM_GETR(*pP1);
            g0 = PPM_GETG(*pP1);
            b0 = PPM_GETB(*pP1);
            pP1++;
            /* 2nd pixel */
            r1 = PPM_GETR(*pP1);
            g1 = PPM_GETG(*pP1);
            b1 = PPM_GETB(*pP1);
            pP1++;
            /* 3rd pixel */
            r2 = PPM_GETR(*pP2);
            g2 = PPM_GETG(*pP2);
            b2 = PPM_GETB(*pP2);
            pP2++;
            /* 4th pixel */
            r3 = PPM_GETR(*pP2);
            g3 = PPM_GETG(*pP2);
            b3 = PPM_GETB(*pP2);
            pP2++;


            /* The JFIF RGB to YUV Matrix for $00010000 = 1.0

               [Y]   [19595   38469    7471][R]
               [U] = [-11056  -21712  32768][G]
               [V]   [32768   -27440  -5328][B]

            */

            y0 =  19595 * r0 + 38469 * g0 +  7471 * b0;
            u0 = -11056 * r0 - 21712 * g0 + 32768 * b0;
            v0 =  32768 * r0 - 27440 * g0 -  5328 * b0;

            y1 =  19595 * r1 + 38469 * g1 +  7471 * b1;
            u1 = -11056 * r1 - 21712 * g1 + 32768 * b1;
            v1 =  32768 * r1 - 27440 * g1 -  5328 * b1;

            y2 =  19595 * r2 + 38469 * g2 +  7471 * b2;
            u2 = -11056 * r2 - 21712 * g2 + 32768 * b2;
            v2 =  32768 * r2 - 27440 * g2 -  5328 * b2;

            y3 =  19595 * r3 + 38469 * g3 +  7471 * b3;
            u3 = -11056 * r3 - 21712 * g3 + 32768 * b3;
            v3 =  32768 * r3 - 27440 * g3 -  5328 * b3;

            /* mean the chroma for subsampling */

            u  = (u0+u1+u2+u3)>>2;
            v  = (v0+v1+v2+v3)>>2;

            y0 = (y0 * 219)/255 + 1048576;
            y1 = (y1 * 219)/255 + 1048576;
            y2 = (y2 * 219)/255 + 1048576;
            y3 = (y3 * 219)/255 + 1048576;

            u  = (u * 224)/255 ;
            v  = (v * 224)/255 ;

            *y1ptr++  = (y0 >> 16) ;
            *y1ptr++  = (y1 >> 16) ;
            *y2ptr++  = (y2 >> 16) ;
            *y2ptr++  = (y3 >> 16) ;


            *uptr++   = (u >> 16)+128 ;
            *vptr++   = (v >> 16)+128 ;

        }
        fwrite(y1buf, (cols & ~1), 1, yf);
        fwrite(y2buf, (cols & ~1), 1, yf);
        fwrite(ubuf, cols/2, 1, uf);
        fwrite(vbuf, cols/2, 1, vf);
    }

    pm_close(ifP);
    fclose(yf);
    fclose(uf);
    fclose(vf);
    return 0;
}