main() { int c ; while ( ( c = getchar() ) != EOF ) if ( Sputc( 1, c ) != c ) exit( 1 ) ; exit( 0 ) ; }
static int putNum(int n, IOSTREAM *fd) { if ( file_col != 0 && Sputc(' ', fd) == EOF ) return -1; do { if ( Sputc(n % 10 + '0', fd) == EOF ) return -1; file_col++; n /= 10; } while( n > 0 ); if ( file_col >= 70 ) { if ( Sputc('\n', fd) == EOF ) return -1; file_col = 0; } return 0; }
int write_pnm_file(IOSTREAM *fd, XImage *img, Display *disp, Colormap cmap, int scale, int fmt, int encode) { int width = img->width; int height = img->height; XColor cdata[256]; XPixelInfo info; int x, y; if ( !scale ) scale = 255; if ( !fmt ) { if ( img->format == XYBitmap ) fmt = PNM_PBM; else fmt = PNM_PPM; } if ( fmt == PNM_PBM && encode == PNM_RUNLEN ) encode = PNM_RAWBITS; /* no use to runlen encode a bitmap */ if ( img->format != XYBitmap ) { info.cinfo = cdata; makeXPixelInfo(&info, img, disp, cmap); } Sfprintf(fd, "P%c\n", fmt + encode + '0'); Sfprintf(fd, "# Creator: XPCE version %s\n", strName(get(PCE,NAME_version,EAV))); Sfprintf(fd, "%d %d\n", width, height); if ( fmt != PNM_PBM ) Sfprintf(fd, "%d\n", scale); file_col = 0; switch(encode) { case PNM_ASCII: { switch(fmt) { case PNM_PBM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { unsigned long pixel = XGetPixel(img, x, y); if ( img->format != XYBitmap ) { XColor *c; int r; c = pixelToColor(img, pixel, &info); r = intensityXColor(c); pixel = r < 32768 ? 1 : 0; } if ( putNum(pixel, fd) < 0 ) return -1; } } break; } case PNM_PGM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { XColor *c; unsigned int r; c = pixelToColor(img, XGetPixel(img, x, y), &info); r = intensityXColor(c); r = rescale(r, BRIGHT, scale); if ( putNum(r, fd) < 0 ) return -1; } } break; } case PNM_PPM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { XColor *c; unsigned int r, g, b; c = pixelToColor(img, XGetPixel(img, x, y), &info); r = rescale(c->red, BRIGHT, scale); g = rescale(c->green, BRIGHT, scale); b = rescale(c->blue, BRIGHT, scale); if ( putNum(r, fd) < 0 || putNum(g, fd) < 0 || putNum(b, fd) < 0 ) return -1; } } break; } } if ( file_col && Sputc('\n', fd) == EOF ) return -1; file_col = 0; } case PNM_RAWBITS: { switch(fmt) { case PNM_PBM: { int byte = 0; int bit = 7; for(y=0; y<height; y++) { for(x=0; x<width; x++) { unsigned long pixel = XGetPixel(img, x, y); if ( img->format != XYBitmap ) { XColor *c; int r; c = pixelToColor(img, pixel, &info); r = intensityXColor(c); pixel = r < 32768 ? 1 : 0; } if ( pixel ) byte |= 1<<bit; if ( bit-- == 0 ) { if ( Sputc(byte, fd) == EOF ) return -1; bit = 7; byte = 0; } } if ( bit != 7 ) /* flush after finishing scanline */ { if ( Sputc(byte, fd) == EOF ) return -1; bit = 7; byte = 0; } } if ( bit != 7 ) { if ( Sputc(byte, fd) == EOF ) return -1; } break; } case PNM_PGM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { XColor *c; unsigned int r; c = pixelToColor(img, XGetPixel(img, x, y), &info); r = intensityXColor(c); r = rescale(r, BRIGHT, scale); if ( Sputc(r, fd) == EOF ) return -1; } } break; } case PNM_PPM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { XColor *c; unsigned int r, g, b; c = pixelToColor(img, XGetPixel(img, x, y), &info); r = rescale(c->red, BRIGHT, scale); g = rescale(c->green, BRIGHT, scale); b = rescale(c->blue, BRIGHT, scale); if ( Sputc(r, fd) == EOF || Sputc(g, fd) == EOF || Sputc(b, fd) == EOF ) return -1; } } break; } } } case PNM_RUNLEN: { int rlen=-1; unsigned long cpixel = NOPIXEL; switch(fmt) { case PNM_PGM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { unsigned long pixel = XGetPixel(img, x, y); if ( pixel == cpixel && rlen < 255 ) rlen++; else { XColor *c; int r; if ( rlen > 0 && Sputc(rlen, fd) == EOF ) return -1; cpixel = pixel; rlen = 1; c = pixelToColor(img, pixel, &info); r = intensityXColor(c); r = rescale(r, BRIGHT, scale); if ( Sputc(r, fd) == EOF ) return -1; } } } if ( Sputc(rlen, fd) == EOF ) return -1; break; } case PNM_PPM: { for(y=0; y<height; y++) { for(x=0; x<width; x++) { unsigned long pixel = XGetPixel(img, x, y); if ( pixel == cpixel && rlen < 255 ) rlen++; else { XColor *c; unsigned int r, g, b; if ( rlen > 0 && Sputc(rlen, fd) == EOF ) return -1; cpixel = pixel; rlen = 1; c = pixelToColor(img, pixel, &info); r = rescale(c->red, BRIGHT, scale); g = rescale(c->green, BRIGHT, scale); b = rescale(c->blue, BRIGHT, scale); if ( Sputc(r, fd) == EOF || Sputc(g, fd) == EOF || Sputc(b, fd) == EOF ) return -1; } } } if ( Sputc(rlen, fd) == EOF ) return -1; break; } } } } return 0; }