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 */
019package pt.ua.dicoogle.server.web.rest;
020
021import java.awt.image.BufferedImage;
022import java.io.*;
023import java.util.Iterator;
024import org.slf4j.LoggerFactory;
025import javax.imageio.ImageIO;
026import javax.imageio.ImageReader;
027import javax.imageio.stream.ImageInputStream;
028import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;
029import org.restlet.data.MediaType;
030import org.restlet.representation.ByteArrayRepresentation;
031import org.restlet.resource.Get;
032import org.restlet.resource.ServerResource;
033import pt.ua.dicoogle.plugins.PluginController;
034
035/**
036 *
037 * @author psytek
038 *
039 * WARNING: lame workaround implementation to get some images
040 * lets not even discuss security
041 *
042 */
043public class RestDcmImageResource extends ServerResource {
044
045    private PluginController core;
046
047    public RestDcmImageResource() {
048        this.core = PluginController.getInstance();
049    }
050    
051    @Get("image/jpeg")
052    public void getJPEG() {
053        BufferedImage dicomImage=null;
054        String imgPath = getRequest().getResourceRef().getQueryAsForm().getValues("path");
055        if (imgPath == null) {return;}
056        
057        int indexExt = imgPath.lastIndexOf('.');
058        if(indexExt == -1) imgPath += ".dcm"; //a lot of dicom files have no extension
059        
060        String extension = imgPath.substring(indexExt);
061        switch(extension.toLowerCase()){
062            case ".jpg":    //these are not indexed
063            case ".png":
064            case ".gif":
065            case ".bmp":
066            case ".tiff":
067            case ".jpeg":{
068                File file = new File(imgPath);
069
070                ByteArrayOutputStream baos = new ByteArrayOutputStream();
071                try {
072                    BufferedImage img = ImageIO.read(file);
073                    ImageIO.write(img, "jpg", baos);
074                    baos.flush();
075                    ByteArrayRepresentation bar = new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_JPEG);
076                    getResponse().setEntity(bar);
077                    baos.close();
078                }
079                catch (IOException e) {
080                    LoggerFactory.getLogger(RestDcmImageResource.class).error(e.getMessage(), e);
081                }
082                break;
083            }
084            case ".dicom":  //these are
085            case ".dcm":{
086                File file = new File(imgPath);
087                //todo:if not file...
088
089                Iterator<ImageReader> iterator =ImageIO.getImageReadersByFormatName("DICOM");
090                while (iterator.hasNext()) {
091                    ImageReader imageReader = (ImageReader) iterator.next();
092                    DicomImageReadParam dicomImageReadParam = (DicomImageReadParam) imageReader.getDefaultReadParam();
093                    try {
094                        ImageInputStream iis = ImageIO.createImageInputStream(file);
095                        imageReader.setInput(iis,false);
096                        dicomImage = imageReader.read(0, dicomImageReadParam);
097                        iis.close();
098                        if(dicomImage == null){
099                            System.err.println("Could not read image!!");
100                        }
101                    } catch (IOException e) {
102                        e.printStackTrace();
103                    }
104
105                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
106                    try{
107                        ImageIO.write(dicomImage, "jpg", baos);
108                        baos.flush();
109                        ByteArrayRepresentation bar = new ByteArrayRepresentation(baos.toByteArray(), MediaType.IMAGE_JPEG);
110                        getResponse().setEntity(bar);
111                        baos.close();
112                    }
113                    catch(IOException e){
114                        LoggerFactory.getLogger(RestDcmImageResource.class).error(e.getMessage(), e);
115                        return;
116                    }
117                }
118                break;
119            }
120        }
121    }     
122}
123
124    
125