Exemplo n.º 1
0
/** Write a signed 16-bit integer
 * \param[in] n number to be written
 */
void BufferedWriter::putNum(int16_t n) {
  if (n < 0) {
    putChar('-');
    n = -n;
  }
  putNum((uint16_t)n);
}
Exemplo n.º 2
0
//------------------------------------------------------------------------------
void ostream::putNum(int32_t n) {
  bool neg = n < 0 && flagsToBase() == 10;
  if (neg) {
    n = -n;
  }
  putNum(n, neg);
}
Exemplo n.º 3
0
/** Write a unsigned 16-bit integer
 * \param[in] n number to be written
 */
void BufferedWriter::putNum(uint16_t n) {
  if (n < 256) {
    putNum((uint8_t)n);
    return;
  }
  uint8_t len = n < 1000 ? 3 : n < 10000 ? 4 : 5;
  if ((in_ + len) >= sizeof(buf_)) {
    writeBuf();
  }
  in_ += len;
  char* str = buf_ + in_ - 1;
  do {
    // avoid mod, it's slow.
    uint16_t tmp = n;
    n /= 10;
    *str-- = '0' + tmp - 10 * n;
  } while (n);
}
Exemplo n.º 4
0
/** Write an unsigned 32-bit number
 * \param[in] n number to be written
 */
void BufferedWriter::putNum(uint32_t n) {
  uint8_t len;
  if (n < 0X10000) {
    putNum((uint16_t)n);
    return;
  }
  if (n < 10000000) {
    len = n < 100000 ? 5 : n < 1000000 ? 6 : 7;
  } else {
    len = n < 100000000 ? 8 : n < 1000000000 ? 9 : 10;
  }
  if ((in_ + len) >= sizeof(buf_)) {
    writeBuf();
  }
  in_ += len;
  char* str = buf_ + in_ - 1;
  do {
    // avoid mod, it's slow.
    uint32_t tmp = n;
    n /= 10;
    *str-- = '0' + tmp - 10 * n;
  } while (n);
}
Exemplo n.º 5
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;
}