Search in sources :

Example 1 with ImageFormat

use of net.pms.image.ImageFormat in project UniversalMediaServer by UniversalMediaServer.

the class DLNAImageResElement method getComparator.

/**
 * Constructs a {@link Comparator} for sorting {@link DLNAImageResElement}s
 * by priority with the highest priority first.
 *
 * @param sourceFormat the {@link ImageFormat} of the source image, use to
 *            decide the preferred {@link DLNAImageProfile}s.
 * @return The {@link Comparator}.
 */
public static Comparator<DLNAImageResElement> getComparator(ImageFormat sourceFormat) {
    // This defines what DLNA format should be preferred for per source format
    final ImageFormat preferredFormat;
    if (sourceFormat != null) {
        switch(sourceFormat) {
            case GIF:
                preferredFormat = ImageFormat.GIF;
                break;
            case CUR:
            case ICNS:
            case ICO:
            case PNG:
            case PSD:
            case TIFF:
            case WEBP:
                preferredFormat = ImageFormat.PNG;
                break;
            case ARW:
            case BMP:
            case CR2:
            case CRW:
            case DCX:
            case JPEG:
            case NEF:
            case ORF:
            case PCX:
            case PNM:
            case RAF:
            case RW2:
            case WBMP:
            default:
                preferredFormat = ImageFormat.JPEG;
                break;
        }
    } else {
        preferredFormat = ImageFormat.JPEG;
    }
    return new Comparator<DLNAImageResElement>() {

        @Override
        public int compare(DLNAImageResElement o1, DLNAImageResElement o2) {
            if (o1 == null && o2 == null) {
                return 0;
            } else if (o1 == null) {
                return 1;
            } else if (o2 == null) {
                return -1;
            }
            if (o1.isThumbnail() != o2.isThumbnail()) {
                return (o1.isThumbnail() ? 1 : 0) - (o2.isThumbnail() ? 1 : 0);
            }
            int i = (o1.getCiFlag() == null ? 2 : o1.getCiFlag()) - (o2.getCiFlag() == null ? 2 : o2.getCiFlag());
            if (i != 0) {
                return i;
            }
            ImageFormat o1Format = o1.getProfile() != null ? o1.getProfile().getFormat() : null;
            ImageFormat o2Format = o2.getProfile() != null ? o2.getProfile().getFormat() : null;
            if (o1Format != o2Format) {
                if (o1Format == null) {
                    return 1;
                } else if (o2Format == null) {
                    return -1;
                }
                if (o1Format == preferredFormat) {
                    return -1;
                }
                if (o2Format == preferredFormat) {
                    return 1;
                }
                return o1Format.compareTo(o2Format);
            }
            if ((DLNAImageProfile.JPEG_RES_H_V.equals(o1.getProfile()) || DLNAImageProfile.JPEG_RES_H_V.equals(o2.getProfile())) && (!DLNAImageProfile.JPEG_RES_H_V.equals(o1.getProfile()) || !DLNAImageProfile.JPEG_RES_H_V.equals(o2.getProfile()))) {
                if (DLNAImageProfile.JPEG_RES_H_V.equals(o1.getProfile())) {
                    return -1;
                }
                return 1;
            }
            if (o1.getWidth() != o2.getWidth()) {
                return o2.getWidth() - o1.getWidth();
            }
            if (o1.getHeight() != o2.getHeight()) {
                return o2.getHeight() - o1.getHeight();
            }
            if (o1.getProfile() != null || o2.getProfile() != null) {
                if (o1.getProfile() == null) {
                    return 1;
                }
                if (o2.getProfile() == null) {
                    return -1;
                }
                if (!o1.getProfile().equals(o2.getProfile())) {
                    return o1.getProfile().toInt() - o2.getProfile().toInt();
                }
            }
            long l = (o2.getSize() == null ? 0 : o2.getSize()) - (o1.getSize() == null ? 0 : o1.getSize());
            if (l != 0) {
                return (int) l;
            }
            if (o1.getHypotheticalResult() != null || o2.getHypotheticalResult() != null) {
                // to fulfill the contract with equals().
                if (o1.getHypotheticalResult() == null) {
                    return 1;
                }
                if (o2.getHypotheticalResult() == null) {
                    return -1;
                }
                if (o1.getHypotheticalResult().conversionNeeded != o2.getHypotheticalResult().conversionNeeded) {
                    return (o1.getHypotheticalResult().conversionNeeded ? 1 : 0) - (o2.getHypotheticalResult().conversionNeeded ? 1 : 0);
                }
            }
            return 0;
        }
    };
}
Also used : ImageFormat(net.pms.image.ImageFormat) Comparator(java.util.Comparator)

Example 2 with ImageFormat

use of net.pms.image.ImageFormat 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)

Aggregations

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