001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019/**
020 */
021
022package pt.ua.dicoogle.server.web.utils;
023
024import java.io.ByteArrayInputStream;
025import java.io.ByteArrayOutputStream;
026import java.io.IOException;
027import java.net.URI;
028import java.util.Iterator;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import pt.ua.dicoogle.core.ServerSettings;
032import pt.ua.dicoogle.plugins.PluginController;
033import pt.ua.dicoogle.sdk.StorageInputStream;
034import pt.ua.dicoogle.sdk.StorageInterface;
035import pt.ua.dicoogle.server.web.dicom.Convert2PNG;
036
037/**
038 *
039 * @author Eduardo Pinho <eduardopinho@ua.pt>
040 */
041public class SimpleImageRetriever implements ImageRetriever {
042
043    private static final Logger logger = LoggerFactory.getLogger(SimpleImageRetriever.class);
044    
045    @Override
046    public ByteArrayInputStream get(URI uri, int frame, boolean thumbnail) throws IOException {
047        return getPNGStream(fromURI(uri), frame, thumbnail);
048    }
049
050    private static StorageInputStream fromURI(URI uri) throws IOException {
051        StorageInterface storage = PluginController.getInstance().getStorageForSchema(uri);
052        Iterator<StorageInputStream> store = storage.at(uri).iterator();
053        if (!store.hasNext()) {
054            throw new IOException("No storage item found at the given URI");
055        }
056        return store.next();
057    }
058
059    private static ByteArrayInputStream getPNGStream(StorageInputStream imgFile, int frame, boolean thumbnail) throws IOException {
060        ByteArrayOutputStream pngStream;
061        if (thumbnail) {
062            int thumbSize;
063            try {
064                // retrieve thumbnail dimension settings
065                thumbSize = Integer.parseInt(ServerSettings.getInstance().getThumbnailsMatrix());
066            } catch (NumberFormatException ex) {
067                logger.warn("Failed to parse ThumbnailMatrix, using default thumbnail size");
068                thumbSize = 64;
069            }
070            pngStream = Convert2PNG.DICOM2ScaledPNGStream(imgFile, frame, thumbSize, thumbSize);
071        } else {
072            pngStream = Convert2PNG.DICOM2PNGStream(imgFile, frame);
073        }
074        return new ByteArrayInputStream(pngStream.toByteArray());
075    }}