Exemple #1
0
 // read a jpeg file into a buffered image
 protected static Image loadJPG(String filename) {
   FileInputStream in = null;
   try {
     in = new FileInputStream(filename);
   } catch (java.io.FileNotFoundException io) {
     System.out.println("File Not Found");
   }
   JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
   BufferedImage bi = null;
   try {
     bi = decoder.decodeAsBufferedImage();
     in.close();
   } catch (java.io.IOException io) {
     System.out.println("IOException");
   }
   return bi;
 }
Exemple #2
0
  private static ImageData readPPM(File file) throws IOException, BadImageFileException {
    FileInputStream s = new FileInputStream(file);

    ImageData imageData;
    try {
      String S1 = readln(s);

      int width;
      int height;
      int bands;
      int dataType;
      if (S1.equals("P5") || S1.equals("P6")) {
        bands = S1.equals("P5") ? 1 : 3;
        String S2 = readln(s);
        String S3 = readln(s);

        String dimensions[] = S2.split("\\s");
        width = Integer.parseInt(dimensions[0]);
        height = Integer.parseInt(dimensions[1]);

        dataType = S3.equals("255") ? DataBuffer.TYPE_BYTE : DataBuffer.TYPE_USHORT;

        imageData = new ImageData(width, height, bands, dataType);
      } else if (S1.equals("P7")) {
        String WIDTH = "WIDTH ";
        String HEIGHT = "HEIGHT ";
        String DEPTH = "DEPTH ";
        String MAXVAL = "MAXVAL ";
        // String TUPLTYPE = "TUPLTYPE ";
        // String ENDHDR = "ENDHDR";
        String SWIDTH = readln(s);
        width = Integer.parseInt(SWIDTH.substring(WIDTH.length()));
        String SHEIGHT = readln(s);
        height = Integer.parseInt(SHEIGHT.substring(HEIGHT.length()));
        String SDEPTH = readln(s);
        bands = Integer.parseInt(SDEPTH.substring(DEPTH.length()));
        String SMAXVAL = readln(s);
        dataType =
            SMAXVAL.substring(MAXVAL.length()).equals("65535")
                ? DataBuffer.TYPE_USHORT
                : DataBuffer.TYPE_BYTE;
        // String STUPLTYPE = readln(s);
        // String SENDHDR = readln(s);
        imageData = new ImageData(width, height, bands, dataType);
      } else return null;

      int totalData = width * height * bands * (dataType == DataBuffer.TYPE_BYTE ? 1 : 2);

      FileChannel c = s.getChannel();

      if (file.length() != totalData + c.position()) {
        c.close();
        throw new BadImageFileException(file);
      }

      ByteBuffer bb = c.map(FileChannel.MapMode.READ_ONLY, c.position(), totalData);

      if (dataType == DataBuffer.TYPE_USHORT) {
        // bb.order(ByteOrder.BIG_ENDIAN);
        bb.order(ByteOrder.nativeOrder());
        bb.asShortBuffer().get((short[]) imageData.data);

        // Darty hack to prevent crash on Arch Linux (issue #125)
        if (ByteOrder.nativeOrder() != ByteOrder.BIG_ENDIAN)
          for (int i = 0; i < ((short[]) imageData.data).length; ++i)
            ((short[]) imageData.data)[i] = Short.reverseBytes(((short[]) imageData.data)[i]);
      } else bb.get((byte[]) imageData.data);

      if (bb instanceof DirectBuffer) ((DirectBuffer) bb).cleaner().clean();

      c.close();
    } catch (Exception e) {
      e.printStackTrace();
      s.close();
      throw new BadImageFileException(file, e);
    } finally {
      s.close();
    }

    return imageData;
  }