Пример #1
0
  /**
   * Load image data for file and put user data attributes into file.
   *
   * @param file File
   * @return true if file image is loaded.
   * @throws java.io.IOException if image can not be loaded
   */
  private static boolean refresh(@NotNull VirtualFile file) throws IOException {
    Long loadedTimeStamp = file.getUserData(TIMESTAMP_KEY);
    SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
    if (loadedTimeStamp == null
        || loadedTimeStamp.longValue() != file.getTimeStamp()
        || SoftReference.dereference(imageRef) == null) {
      try {
        final byte[] content = file.contentsToByteArray();

        if (ICO_FORMAT.equalsIgnoreCase(file.getExtension())) {
          try {
            final BufferedImage image =
                ICO_IMAGE_PARSER.getBufferedImage(new ByteSourceArray(content), null);
            file.putUserData(FORMAT_KEY, ICO_FORMAT);
            file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
            return true;
          } catch (ImageReadException ignore) {
          }
        }

        InputStream inputStream = new ByteArrayInputStream(content, 0, content.length);
        ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
        try {
          Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
          if (imageReaders.hasNext()) {
            ImageReader imageReader = imageReaders.next();
            try {
              file.putUserData(FORMAT_KEY, imageReader.getFormatName());
              ImageReadParam param = imageReader.getDefaultReadParam();
              imageReader.setInput(imageInputStream, true, true);
              int minIndex = imageReader.getMinIndex();
              BufferedImage image = imageReader.read(minIndex, param);
              file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
              return true;
            } finally {
              imageReader.dispose();
            }
          }
        } finally {
          imageInputStream.close();
        }
      } finally {
        // We perform loading no more needed
        file.putUserData(TIMESTAMP_KEY, file.getTimeStamp());
      }
    }
    return false;
  }
 public Tuple2<BufferedImage, String> readImageAndFormat(InputStream is)
     throws IOException, IllegalArgumentException {
   ImageInputStream iis = ImageIO.createImageInputStream(is);
   Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
   if (!readers.hasNext()) {
     iis.close();
     throw new IllegalArgumentException("unsupported image type");
   }
   ImageReader reader = readers.next();
   String inputFormat = reader.getFormatName();
   reader.setInput(iis, true, true);
   BufferedImage src = reader.read(0);
   reader.dispose();
   iis.close();
   return new Tuple2<BufferedImage, String>(src, inputFormat.toLowerCase());
 }
  /**
   * This method will scale the photo data to the specified size. It will only decrease the size of
   * the photo, not increase it.
   *
   * @param longEdgeSize
   * @param photoData the raw binary photo data
   * @return the resized photo data
   * @throws IOException
   */
  private static byte[] scalePhoto(int longEdgeSize, byte[] photoData) throws IOException {

    ByteArrayInputStream dataStream = new ByteArrayInputStream(photoData);
    ImageInputStream iis = ImageIO.createImageInputStream(dataStream);
    Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);

    // Determine image format
    final String formatName;
    if (readers.hasNext()) {
      ImageReader reader = readers.next();
      formatName = reader.getFormatName();
    } else {
      logger.log(Level.SEVERE, "Unsupported image type");
      return null;
    }

    BufferedImage image = ImageIO.read(iis);

    int baseWidth = image.getWidth();
    int baseHeight = image.getHeight();

    float aspectRatio = (float) baseHeight / (float) baseWidth;

    final int modWidth;
    final int modHeight;

    if (baseWidth > baseHeight) {
      // width is long edge so scale based on that
      modWidth = longEdgeSize;
      modHeight = Math.round(aspectRatio * longEdgeSize);
    } else {
      modHeight = longEdgeSize;
      modWidth = Math.round(aspectRatio * longEdgeSize);
    }

    return scalePhoto(modWidth, modHeight, image, formatName);
  }
Пример #4
0
  private static JSONObject getResultJson(File file, boolean extractLiveProject)
      throws IOException, JSONException, ScriptException {
    final JSONObject result = new JSONObject();
    result.put("formatChecked", true);
    final ImageInputStream iis = ImageIO.createImageInputStream(file);
    final Iterator<ImageReader> iterator = ImageIO.getImageReaders(iis);
    if (!iterator.hasNext()) {
      result.put("rejected", true);
      result.put("message", "Unknown image format: can't create an ImageInputStream");
      return result;
    }
    final ImageReader reader = iterator.next();
    final JSONObject imageType = SimagisLiveUtils.openObject(result, "imageType");
    try {
      reader.setInput(iis);
      JSONObject formatSpecific = new JSONObject();
      formatSpecific.put("javaFormatType", "DICOM");
      formatSpecific.put("javaFormatName", reader.getFormatName());
      result.put("formatSpecific", formatSpecific);

      final int dimX = reader.getWidth(0);
      final int dimY = reader.getHeight(0);
      if ((long) dimX * (long) dimY >= 512L * 1024L * 1024L) {
        result.put("rejected", true);
        result.put("message", "Too large image (more than 512 million pixels)");
        return result;
        // We are little reinsuring here: Java API can try to create a single byte[]
        // or short[] array with packed RGB triples (3 * dimX * dimY), and we would like
        // to be sure that its size will be far from the Java language limit 2 GB.
      }
      result.put("dimX", dimX);
      result.put("dimY", dimY);
      final Iterator<ImageTypeSpecifier> iioImageTypes = reader.getImageTypes(0);
      if (iioImageTypes != null && iioImageTypes.hasNext()) {
        // some 3rd party implementation can still return null here, though it is prohibited in
        // JavaDoc
        ImageTypeSpecifier imageTypeSpecifier = iioImageTypes.next();
        imageType.put("numComponents", imageTypeSpecifier.getNumComponents());
        imageType.put("numBands", imageTypeSpecifier.getNumBands());
        if (imageTypeSpecifier.getNumComponents() >= 4) {
          result.put("recommendedRenderingFormat", "png");
        }
      }
      final IIOMetadata streamMetadata = reader.getStreamMetadata();
      final IIOMetadataToJsonConverter converter = new IIOMetadataToJsonConverter();
      if (streamMetadata != null) {
        DICOMImageIOMetadata imageIOMetadata = DICOMImageIOMetadata.getInstance(streamMetadata, 0);
        if (imageIOMetadata != null) {
          result.put("DICOMMetadata", new JSONObject(imageIOMetadata));
        }
        result.put("streamMetadata", converter.toJson(streamMetadata));
      }
      final IIOMetadata imageMetadata = reader.getImageMetadata(0);
      if (imageMetadata != null) {
        result.put("imageMetadata", converter.toJson(imageMetadata));
      }
    } finally {
      reader.dispose();
      iis.close();
    }
    if (extractLiveProject) {
      JSONObject liveProject = extractLiveProject(result);
      result.put("liveProject", liveProject);
      result.remove("imageMetadata");
      result.remove("streamMetadata");
    }
    result.put("rejected", false);
    return result;
  }