Ejemplo n.º 1
1
  // tokenizes a string for PBM and PGM headers and plain PBM and PGM data.  If oneChar is true,
  // then
  // rather than parsing a whole string, a single character is read (not including whitespace or
  // comments)
  static String tokenizeString(InputStream stream, boolean oneChar) throws IOException {
    final int EOF = -1;

    StringBuilder b = new StringBuilder();
    int c;
    boolean inComment = false;

    while (true) {
      c = stream.read();
      if (c == EOF)
        throw new IOException(
            "Stream ended prematurely, before table reading was completed."); // no more tokens
      else if (inComment) {
        if (c == '\r' || c == '\n') // escape the comment
        inComment = false;
        else {
        } // do nothing
      } else if (Character.isWhitespace((char) c)) {
      } // do nothing
      else if (c == '#') // start of a comment
      {
        inComment = true;
      } else // start of a string
      {
        b.append((char) c);
        break;
      }
    }

    if (oneChar) return b.toString();

    // at this point we have a valid string.  We read until whitespace or a #
    while (true) {
      c = stream.read();
      if (c == EOF) break;
      else if (c == '#') // start of comment, read until a '\n'
      {
        while (true) {
          c = stream.read(); // could hit EOF, which is fine
          if (c == EOF) break;
          else if (c == '\r' || c == '\n') break;
        }
        // break;   // comments are not delimiters
      } else if (Character.isWhitespace((char) c)) break;
      else b.append((char) c);
    }
    return b.toString();
  }
Ejemplo n.º 2
1
 private String getStringFromStreamSource(StreamSource src, int length) throws Exception {
   byte buf[] = null;
   if (src == null) return null;
   InputStream outStr = src.getInputStream();
   if (outStr != null) {
     int len = outStr.available();
     if (outStr.markSupported()) outStr.reset();
     buf = new byte[len];
     outStr.read(buf, 0, len);
     // System.out.println("From inputstream: "+new String(buf));
     return new String(buf);
   } else {
     char buf1[] = new char[length];
     Reader r = src.getReader();
     if (r == null) return null;
     r.reset();
     r.read(buf1);
     // System.out.println("From Reader: "+new String(buf));
     return new String(buf1);
   }
 }
Ejemplo n.º 3
0
  // Loads raw PGM files after the first-line header is stripped
  static int[][] loadRawPGM(InputStream stream) throws IOException {
    int width = tokenizeInt(stream);
    int height = tokenizeInt(stream);
    int maxVal = tokenizeInt(stream);
    if (width < 0) throw new IOException("Invalid width: " + width);
    if (height < 0) throw new IOException("Invalid height: " + height);
    if (maxVal <= 0) throw new IOException("Invalid maximum value: " + maxVal);

    // this single whitespace character will have likely already been consumed by reading maxVal
    // stream.read();  // must be a whitespace

    int[][] field = new int[width][height];
    for (int i = 0; i < height; i++)
      for (int j = 0; j < width; j++) {
        if (maxVal < 256) // one byte
        field[j][i] = stream.read();
        else if (maxVal < 65536) // two bytes
        field[j][i] =
              (stream.read() << 8) & stream.read(); // two bytes, most significant byte first
        else if (maxVal < 16777216) // three bytes -- this is nonstandard
        field[j][i] =
              (stream.read() << 16)
                  & (stream.read() << 8)
                  & stream.read(); // three bytes, most significant byte first
        else // four bytes -- this is nonstandard
        field[j][i] =
              (stream.read() << 24)
                  & (stream.read() << 16)
                  & (stream.read() << 8)
                  & stream.read(); // three bytes, most significant byte first
      }

    return field;
  }
Ejemplo n.º 4
0
  // Loads raw PBM files after the first-line header is stripped
  static int[][] loadRawPBM(InputStream stream) throws IOException {
    int width = tokenizeInt(stream);
    int height = tokenizeInt(stream);
    if (width < 0) throw new IOException("Invalid width in PBM: " + width);
    if (height < 0) throw new IOException("Invalid height in PBM: " + height);

    // this single whitespace character will have likely already been consumed by reading height
    // stream.read();  // must be a whitespace

    int[][] field = new int[width][height];
    for (int i = 0; i < height; i++) {
      int data = 0;
      int count = 0;
      for (int j = 0; j < width; j++) {
        if (count == 0) {
          data = stream.read();
          count = 8;
        }
        count--;
        field[j][i] = (data >> count) & 0x1;
      }
    }

    return field;
  }
Ejemplo n.º 5
0
 private static String readln(InputStream s) {
   try {
     int c;
     StringBuffer sb = new StringBuffer();
     while ((c = s.read()) > 0 && c != 255 && (c == '\n' || c == '\r'))
       ; // Skip carriage returns and line feeds
     while (c > 0 && c != 255 && c != '\n' && c != '\r') {
       sb.append((char) c);
       c = s.read();
     }
     if ((c == -1 || c == 255) && sb.length() == 0) return null;
     return new String(sb);
   } catch (IOException e) {
     return null;
   }
 }
Ejemplo n.º 6
0
  static byte[] readStream(InputStream input) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    int read;
    byte[] buffer = new byte[256];

    while ((read = input.read(buffer, 0, 256)) != -1) {
      bytes.write(buffer, 0, read);
    }

    return bytes.toByteArray();
  }
  public static void main(String[] args) {
    // read filename and N 2 parameters
    String fileName = args[0];
    N = Integer.parseInt(args[1]);

    // output two images, one original image at left, the other result image at right
    BufferedImage imgOriginal =
        new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
    BufferedImage img = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);

    try {
      File file = new File(fileName);
      InputStream is = new FileInputStream(file);

      long len = file.length();
      byte[] bytes = new byte[(int) len];

      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length
          && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
      }

      int ind = 0;
      for (int y = 0; y < IMAGE_HEIGHT; y++) {
        for (int x = 0; x < IMAGE_WIDTH; x++) {
          // for reading .raw image to show as a rgb image
          byte r = bytes[ind];
          byte g = bytes[ind];
          byte b = bytes[ind];

          // set pixel for display original image
          int pixOriginal = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
          imgOriginal.setRGB(x, y, pixOriginal);
          ind++;
        }
      }
      is.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    int[] vectorSpace = calculateVectorSpace(imgOriginal);

    // quantization
    for (int y = 0; y < IMAGE_HEIGHT; y++) {
      for (int x = 0; x < IMAGE_WIDTH; x++) {
        int clusterId = vectorSpace[IMAGE_WIDTH * y + x];
        img.setRGB(x, y, clusters[clusterId].getPixel());
      }
    }

    // Use a panel and label to display the image
    JPanel panel = new JPanel();
    panel.add(new JLabel(new ImageIcon(imgOriginal)));
    panel.add(new JLabel(new ImageIcon(img)));

    JFrame frame = new JFrame("Display images");

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }