Exemplo n.º 1
0
  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;
  }
Exemplo n.º 2
0
  public BufferedImage decompress(final QuickTime.ImageDesc pDescription, final InputStream pStream)
      throws IOException {
    byte[] data = new byte[pDescription.dataSize];

    DataInputStream stream = new DataInputStream(pStream);
    try {
      stream.readFully(data, 0, pDescription.dataSize);
    } finally {
      stream.close();
    }

    DataBuffer buffer = new DataBufferByte(data, data.length);

    WritableRaster raster;

    // TODO: Depth parameter can be 1-32 (color) or 33-40 (gray scale)
    switch (pDescription.depth) {
      case 40: // 8 bit gray (untested)
        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width,
                1,
                new int[] {0},
                null);
        break;
      case 24: // 24 bit RGB
        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width * 3,
                3,
                new int[] {0, 1, 2},
                null);
        break;
      case 32: // 32 bit ARGB
        // WORKAROUND: There is a bug in the way Java 2D interprets the band offsets in
        // Raster.createInterleavedRaster (see below) before Java 6. So, instead of
        // passing a correct offset array below, we swap channel 1 & 3 to make it ABGR...
        for (int y = 0; y < pDescription.height; y++) {
          for (int x = 0; x < pDescription.width; x++) {
            int offset = 4 * y * pDescription.width + x * 4;
            byte temp = data[offset + 1];
            data[offset + 1] = data[offset + 3];
            data[offset + 3] = temp;
          }
        }

        raster =
            Raster.createInterleavedRaster(
                buffer,
                pDescription.width,
                pDescription.height,
                pDescription.width * 4,
                4,
                new int[] {3, 2, 1, 0}, // B & R mixed up. {1, 2, 3, 0} is correct
                null);
        break;
      default:
        throw new IIOException("Unsupported RAW depth: " + pDescription.depth);
    }

    ColorModel cm =
        new ComponentColorModel(
            pDescription.depth <= 32
                ? ColorSpace.getInstance(ColorSpace.CS_sRGB)
                : ColorSpace.getInstance(ColorSpace.CS_GRAY),
            pDescription.depth == 32,
            false,
            pDescription.depth == 32 ? Transparency.TRANSLUCENT : Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);

    return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
  }
Exemplo n.º 3
0
  public static UIFont createGuiFont(String name, int font_size) {
    Font font = load_ttf(StringUtils.concat(name, ".ttf"), font_size);
    UIFont tmp = new UIFont();

    tmp.font_texture = new Texture();

    Graphics2D graphics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics();
    graphics.setFont(font);

    tmp.fontMetrics = graphics.getFontMetrics();

    WritableRaster raster;
    BufferedImage bufferedImage;

    raster =
        Raster.createInterleavedRaster(
            DataBuffer.TYPE_BYTE,
            (int) tmp.getFontImageWidth(),
            (int) tmp.getFontImageHeight(),
            4,
            null);
    bufferedImage = new BufferedImage(glAlphaColorModel, raster, false, null);

    // Draw the characters on our image
    Graphics2D imageGraphics = bufferedImage.createGraphics();
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    imageGraphics.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    imageGraphics.setFont(font);
    imageGraphics.setColor(Color.white.getAWTColor());

    // draw every CHAR by line...
    for (int i : key_table.keySet())
      imageGraphics.drawString(
          key_table.get(i), 0, (int) (tmp.fontMetrics.getMaxAscent() + (tmp.getHeight() * i)));

    // Generate texture data
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    ByteBuffer imageData = BufferUtils.create_byte_buffer(data.length);
    imageData.order(ByteOrder.nativeOrder());
    imageData.put(data, 0, data.length);
    imageData.flip();

    glBindTexture(GL_TEXTURE_2D, tmp.font_texture.id);

    // Setup wrap mode
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(
        GL_TEXTURE_2D,
        0,
        GL_RGBA8,
        (int) tmp.getFontImageWidth(),
        (int) tmp.getFontImageHeight(),
        0,
        GL_RGBA,
        GL_UNSIGNED_BYTE,
        imageData);

    return tmp;
  }