예제 #1
0
파일: DCRaw.java 프로젝트: Yuniks/LightZone
  public synchronized RenderedImage runDCRaw(dcrawMode mode, boolean secondaryPixels)
      throws IOException, UnknownImageTypeException, BadImageFileException {
    if (!m_decodable || (mode == dcrawMode.full && m_rawColors != 3))
      throw new UnknownImageTypeException("Unsuported Camera");

    RenderedImage result = null;

    File of = null;

    try {
      if (mode == dcrawMode.preview) {
        if (m_thumbWidth >= 1024 && m_thumbHeight >= 768) {
          mode = dcrawMode.thumb;
        }
      }

      long t1 = System.currentTimeMillis();

      of = File.createTempFile("LZRAWTMP", ".ppm");

      boolean four_colors = false;
      final String makeModel = m_make + ' ' + m_model;
      for (String s : four_color_cameras)
        if (s.equalsIgnoreCase(makeModel)) {
          four_colors = true;
          break;
        }

      if (secondaryPixels) runDCRawInfo(true);

      String cmd[];
      switch (mode) {
        case full:
          if (four_colors)
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-f",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-o",
                  "0",
                  "-4",
                  m_fileName
                };
          else if (m_filters == -1 || (m_make != null && m_make.equalsIgnoreCase("SIGMA")))
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-o",
                  "0",
                  "-4",
                  m_fileName
                };
          else if (secondaryPixels)
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-j",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-s",
                  "1",
                  "-d",
                  "-4",
                  m_fileName
                };
          else
            cmd =
                new String[] {
                  DCRAW_PATH,
                  "-F",
                  of.getAbsolutePath(),
                  "-v",
                  "-j",
                  "-H",
                  "1",
                  "-t",
                  "0",
                  "-d",
                  "-4",
                  m_fileName
                };
          break;
        case preview:
          cmd =
              new String[] {
                DCRAW_PATH,
                "-F",
                of.getAbsolutePath(),
                "-v",
                "-t",
                "0",
                "-o",
                "1",
                "-w",
                "-h",
                m_fileName
              };
          break;
        case thumb:
          cmd = new String[] {DCRAW_PATH, "-F", of.getAbsolutePath(), "-v", "-e", m_fileName};
          break;
        default:
          throw new IllegalArgumentException("Unknown mode " + mode);
      }

      String ofName = null;

      synchronized (DCRaw.class) {
        Process p = null;
        InputStream dcrawStdErr;
        InputStream dcrawStdOut;
        if (ForkDaemon.INSTANCE != null) {
          ForkDaemon.INSTANCE.invoke(cmd);
          dcrawStdErr = ForkDaemon.INSTANCE.getStdErr();
          dcrawStdOut = ForkDaemon.INSTANCE.getStdOut();
        } else {
          p = Runtime.getRuntime().exec(cmd);
          dcrawStdErr = new BufferedInputStream(p.getErrorStream());
          dcrawStdOut = p.getInputStream();
        }

        String line, args;
        // output expected on stderr
        while ((line = readln(dcrawStdErr)) != null) {
          // System.out.println(line);

          if ((args = match(line, DCRAW_OUTPUT)) != null)
            ofName = args.substring(0, args.indexOf(" ..."));
        }

        // Flush stdout just in case...
        while ((line = readln(dcrawStdOut)) != null) ; // System.out.println(line);

        if (p != null) {
          dcrawStdErr.close();

          try {
            p.waitFor();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          m_error = p.exitValue();
          p.destroy();
        } else m_error = 0;
      }

      System.out.println("dcraw value: " + m_error);

      if (m_error > 0) {
        of.delete();
        throw new BadImageFileException(of);
      }

      if (!ofName.equals(of.getPath())) {
        of.delete();
        of = new File(ofName);
      }

      if (of.getName().endsWith(".jpg") || of.getName().endsWith(".tiff")) {
        if (of.getName().endsWith(".jpg")) {
          try {
            LCJPEGReader jpegReader = new LCJPEGReader(of.getPath());
            result = jpegReader.getImage();
          } catch (Exception e) {
            e.printStackTrace();
          }
        } else {
          try {
            LCTIFFReader tiffReader = new LCTIFFReader(of.getPath());
            result = tiffReader.getImage(null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
        long t2 = System.currentTimeMillis();

        int totalData =
            result.getWidth()
                * result.getHeight()
                * result.getColorModel().getNumColorComponents()
                * (result.getColorModel().getTransferType() == DataBuffer.TYPE_BYTE ? 1 : 2);

        System.out.println("Read " + totalData + " bytes in " + (t2 - t1) + "ms");
      } else {
        ImageData imageData;
        try {
          imageData = readPPM(of);
        } catch (Exception e) {
          e.printStackTrace();
          throw new BadImageFileException(of, e);
        }

        // do not change the initial image geometry
        // m_width = Math.min(m_width, imageData.width);
        // m_height = Math.min(m_height, imageData.height);

        long t2 = System.currentTimeMillis();

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

        System.out.println("Read " + totalData + " bytes in " + (t2 - t1) + "ms");

        final ColorModel cm;
        if (mode == dcrawMode.full) {
          if (imageData.bands == 1) {
            cm =
                new ComponentColorModel(
                    ColorSpace.getInstance(ColorSpace.CS_GRAY),
                    false,
                    false,
                    Transparency.OPAQUE,
                    DataBuffer.TYPE_USHORT);
          } else {
            cm = JAIContext.colorModel_linear16;
          }
        } else {
          if (imageData.bands == 3)
            cm =
                new ComponentColorModel(
                    JAIContext.sRGBColorSpace,
                    false,
                    false,
                    Transparency.OPAQUE,
                    DataBuffer.TYPE_BYTE);
          else if (imageData.bands == 4)
            cm =
                new ComponentColorModel(
                    JAIContext.CMYKColorSpace,
                    false,
                    false,
                    Transparency.OPAQUE,
                    imageData.dataType);
          else throw new UnknownImageTypeException("Weird number of bands: " + imageData.bands);
        }

        final DataBuffer buf =
            imageData.dataType == DataBuffer.TYPE_BYTE
                ? new DataBufferByte(
                    (byte[]) imageData.data, imageData.bands * imageData.width * imageData.height)
                : new DataBufferUShort(
                    (short[]) imageData.data, imageData.bands * imageData.width * imageData.height);

        final WritableRaster raster =
            Raster.createInterleavedRaster(
                buf,
                imageData.width,
                imageData.height,
                imageData.bands * imageData.width,
                imageData.bands,
                imageData.bands == 3 ? new int[] {0, 1, 2} : new int[] {0},
                null);

        result = new BufferedImage(cm, raster, false, null);
      }
    } catch (IOException e) {
      if (of != null) of.delete();
      throw e;
    } finally {
      if (of != null) of.delete();
    }
    return result;
  }
예제 #2
0
파일: DCRaw.java 프로젝트: Yuniks/LightZone
  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;
  }