Search in sources :

Example 1 with FileType

use of com.drew.imaging.FileType in project UniversalMediaServer by UniversalMediaServer.

the class RAW method parse.

@Override
public void parse(DLNAMediaInfo media, InputFile file, int type, RendererConfiguration renderer) {
    boolean trace = LOGGER.isTraceEnabled();
    if (media == null || file == null || file.getFile() == null) {
        // Parsing is impossible
        if (trace) {
            if (file != null && file.getFile() != null) {
                LOGGER.trace("Not parsing RAW file \"{}\" because media is null", file.getFile().getName());
            } else {
                LOGGER.error("Not parsing RAW file because file is null");
            }
        }
        return;
    }
    PmsConfiguration configuration = PMS.getConfiguration(renderer);
    try {
        // Only parse using DCRaw if it is enabled
        DCRaw dcraw = (DCRaw) PlayerFactory.getEnabledPlayer(DCRaw.class, this);
        if (dcraw != null) {
            if (trace) {
                LOGGER.trace("Parsing RAW image \"{}\" with DCRaw", file.getFile().getName());
            }
            dcraw.parse(media, file.getFile());
            media.setCodecV(FormatConfiguration.RAW);
            media.setContainer(FormatConfiguration.RAW);
            ImageInfo imageInfo = null;
            Metadata metadata = null;
            FileType fileType = null;
            try (BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(file.getFile().toPath()))) {
                fileType = FileTypeDetector.detectFileType(inputStream);
                metadata = ImagesUtil.getMetadata(inputStream, fileType);
            } catch (IOException e) {
                metadata = new Metadata();
                LOGGER.debug("Error reading \"{}\": {}", file.getFile().getAbsolutePath(), e.getMessage());
                LOGGER.trace("", e);
            } catch (ImageProcessingException e) {
                metadata = new Metadata();
                LOGGER.debug("Error parsing {} metadata for \"{}\": {}", fileType.toString().toUpperCase(Locale.ROOT), file.getFile().getAbsolutePath(), e.getMessage());
                LOGGER.trace("", e);
            }
            if (fileType == FileType.Arw && !ImagesUtil.isARW(metadata)) {
                fileType = FileType.Tiff;
            }
            ImageFormat format = ImageFormat.toImageFormat(fileType);
            if (format == null || format == ImageFormat.TIFF) {
                format = ImageFormat.toImageFormat(metadata);
                if (format == null || format == ImageFormat.TIFF) {
                    format = ImageFormat.RAW;
                }
            }
            try {
                imageInfo = ImageInfo.create(media.getWidth(), media.getHeight(), metadata, format, file.getSize(), true, false);
                if (trace) {
                    LOGGER.trace("Parsing of RAW image \"{}\" completed: {}", file.getFile().getName(), imageInfo);
                }
            } catch (ParseException e) {
                LOGGER.warn("Unable to parse \"{}\": {}", file.getFile().getAbsolutePath(), e.getMessage());
                LOGGER.trace("", e);
            }
            media.setImageInfo(imageInfo);
            if (media.getWidth() > 0 && media.getHeight() > 0 && configuration.getImageThumbnailsEnabled()) {
                byte[] image = new DCRaw().getThumbnail(null, file.getFile().getAbsolutePath(), imageInfo);
                media.setThumb(DLNAThumbnail.toThumbnail(image, 320, 320, ScaleType.MAX, ImageFormat.JPEG, false));
            }
        } else {
            if (trace) {
                LOGGER.trace("Parsing RAW image \"{}\" as a regular image because DCRaw is disabled", file.getFile().getName());
            }
            ImagesUtil.parseImage(file.getFile(), media);
        }
        media.setSize(file.getSize());
        media.setImageCount(1);
        media.postParse(type, file);
        media.setMediaparsed(true);
    } catch (IOException e) {
        LOGGER.error("Error parsing RAW file \"{}\": {}", file.getFile().getAbsolutePath(), e.getMessage());
        LOGGER.trace("", e);
    }
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) DCRaw(net.pms.encoders.DCRaw) PmsConfiguration(net.pms.configuration.PmsConfiguration) FileType(com.drew.imaging.FileType) BufferedInputStream(java.io.BufferedInputStream) Metadata(com.drew.metadata.Metadata) IOException(java.io.IOException) ParseException(net.pms.util.ParseException) ImageInfo(net.pms.image.ImageInfo) ImageFormat(net.pms.image.ImageFormat)

Example 2 with FileType

use of com.drew.imaging.FileType in project UniversalMediaServer by UniversalMediaServer.

the class ImagesUtil method parseImage.

/**
 * Parses an image file and stores the results in the given
 * {@link DLNAMediaInfo}. Parsing is performed using both
 * <a href=https://github.com/drewnoakes/metadata-extractor>Metadata Extractor</a>
 * and {@link ImageIO}. While Metadata Extractor offers more detailed
 * information, {@link ImageIO} offers information that is convenient for
 * image transformation with {@link ImageIO}. Parsing will be performed if
 * just one of the two methods produces results, but some details will be
 * missing if either one failed.
 * <p><b>
 * This method consumes and closes {@code inputStream}.
 * </b>
 * @param file the {@link File} to parse.
 * @param media the {@link DLNAMediaInfo} instance to store the parsing
 *              results to.
 * @throws IOException if an IO error occurs or no information can be parsed.
 */
public static void parseImage(File file, DLNAMediaInfo media) throws IOException {
    // 1 MB
    final int MAX_BUFFER = 1048576;
    if (file == null) {
        throw new IllegalArgumentException("parseImage: file cannot be null");
    }
    if (media == null) {
        throw new IllegalArgumentException("parseImage: media cannot be null");
    }
    boolean trace = LOGGER.isTraceEnabled();
    if (trace) {
        LOGGER.trace("Parsing image file \"{}\"", file.getAbsolutePath());
    }
    long size = file.length();
    ResettableInputStream inputStream = new ResettableInputStream(Files.newInputStream(file.toPath()), MAX_BUFFER);
    try {
        Metadata metadata = null;
        FileType fileType = null;
        try {
            fileType = FileTypeDetector.detectFileType(inputStream);
            metadata = getMetadata(inputStream, fileType);
        } catch (IOException e) {
            metadata = new Metadata();
            LOGGER.debug("Error reading \"{}\": {}", file.getAbsolutePath(), e.getMessage());
            LOGGER.trace("", e);
        } catch (ImageProcessingException e) {
            metadata = new Metadata();
            LOGGER.debug("Error parsing {} metadata for \"{}\": {}", fileType.toString().toUpperCase(Locale.ROOT), file.getAbsolutePath(), e.getMessage());
            LOGGER.trace("", e);
        }
        ImageFormat format = ImageFormat.toImageFormat(fileType);
        if (format == null || format == ImageFormat.TIFF) {
            ImageFormat tmpformat = ImageFormat.toImageFormat(metadata);
            if (tmpformat != null) {
                format = tmpformat;
            }
        }
        if (inputStream.isFullResetAvailable()) {
            inputStream.fullReset();
        } else {
            // If we can't reset it, close it and create a new
            inputStream.close();
            inputStream = new ResettableInputStream(Files.newInputStream(file.toPath()), MAX_BUFFER);
        }
        ImageInfo imageInfo = null;
        try {
            imageInfo = ImageIOTools.readImageInfo(inputStream, size, metadata, false);
        } catch (UnknownFormatException | IIOException | ParseException e) {
            if (format == null) {
                throw new UnknownFormatException("Unable to recognize image format for \"" + file.getAbsolutePath() + "\" - parsing failed", e);
            }
            LOGGER.debug("Unable to parse \"{}\" with ImageIO because the format is unsupported, image information will be limited", file.getAbsolutePath());
            LOGGER.trace("ImageIO parse failure reason: {}", e.getMessage());
            // Gather basic information from the data we have
            if (metadata != null) {
                try {
                    imageInfo = ImageInfo.create(metadata, format, size, true, true);
                } catch (ParseException pe) {
                    LOGGER.debug("Unable to parse metadata for \"{}\": {}", file.getAbsolutePath(), pe.getMessage());
                    LOGGER.trace("", pe);
                }
            }
        }
        if (imageInfo == null && format == null) {
            throw new ParseException("Parsing of \"" + file.getAbsolutePath() + "\" failed");
        }
        if (format == null) {
            format = imageInfo.getFormat();
        } else if (imageInfo != null && imageInfo.getFormat() != null && format != imageInfo.getFormat()) {
            if (imageInfo.getFormat() == ImageFormat.TIFF && format.isRaw()) {
                if (format == ImageFormat.ARW && !isARW(metadata)) {
                    // XXX Remove this if https://github.com/drewnoakes/metadata-extractor/issues/217 is fixed
                    // Metadata extractor misidentifies some Photoshop created TIFFs for ARW, correct it
                    format = ImageFormat.toImageFormat(metadata);
                    if (format == null) {
                        format = ImageFormat.TIFF;
                    }
                    LOGGER.trace("Correcting misidentified image format ARW to {} for \"{}\"", format, file.getAbsolutePath());
                } else {
                    /*
						 * ImageIO recognizes many RAW formats as TIFF because
						 * of their close relationship let's treat them as what
						 * they really are.
						 */
                    imageInfo = ImageInfo.create(imageInfo.getWidth(), imageInfo.getHeight(), format, size, imageInfo.getBitDepth(), imageInfo.getNumComponents(), imageInfo.getColorSpace(), imageInfo.getColorSpaceType(), metadata, false, imageInfo.isImageIOSupported());
                    LOGGER.trace("Correcting misidentified image format TIFF to {} for \"{}\"", format.toString(), file.getAbsolutePath());
                }
            } else {
                LOGGER.debug("Image parsing for \"{}\" was inconclusive, metadata parsing " + "detected {} format while ImageIO detected {}. Choosing {}.", file.getAbsolutePath(), format, imageInfo.getFormat(), imageInfo.getFormat());
                format = imageInfo.getFormat();
            }
        }
        media.setImageInfo(imageInfo);
        if (format != null) {
            media.setCodecV(format.toFormatConfiguration());
            media.setContainer(format.toFormatConfiguration());
        }
        if (trace) {
            LOGGER.trace("Parsing of image \"{}\" completed", file.getName());
        }
    } finally {
        inputStream.close();
    }
}
Also used : ImageProcessingException(com.drew.imaging.ImageProcessingException) Metadata(com.drew.metadata.Metadata) IIOException(javax.imageio.IIOException) IIOException(javax.imageio.IIOException) FileType(com.drew.imaging.FileType) UnknownFormatException(net.pms.util.UnknownFormatException) ParseException(net.pms.util.ParseException) ResettableInputStream(net.pms.util.ResettableInputStream)

Aggregations

FileType (com.drew.imaging.FileType)2 ImageProcessingException (com.drew.imaging.ImageProcessingException)2 Metadata (com.drew.metadata.Metadata)2 ParseException (net.pms.util.ParseException)2 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 IIOException (javax.imageio.IIOException)1 PmsConfiguration (net.pms.configuration.PmsConfiguration)1 DCRaw (net.pms.encoders.DCRaw)1 ImageFormat (net.pms.image.ImageFormat)1 ImageInfo (net.pms.image.ImageInfo)1 ResettableInputStream (net.pms.util.ResettableInputStream)1 UnknownFormatException (net.pms.util.UnknownFormatException)1