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.utils;
020
021//import com.sun.image.codec.jpeg.JPEGCodec;
022//import com.sun.image.codec.jpeg.JPEGImageEncoder;
023import java.awt.Graphics2D;
024import java.awt.Image;
025import java.awt.image.BufferedImage;
026import java.io.*;
027import java.util.Iterator;
028import javax.imageio.ImageIO;
029import javax.imageio.ImageReader;
030import javax.imageio.stream.ImageInputStream;
031import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;
032
033/**
034 *
035 * @author Carlos Costa
036 */
037public class Dicom2JPEG {
038    
039    public static boolean convertDicom2Jpeg(File dcmFile, File jpgFile){
040        return convertDicom2Jpeg(dcmFile, jpgFile, 0);
041    }
042    
043    public static boolean convertDicom2Jpeg(File dcmFile, File jpgFile, int scaleHeight){
044        try {
045            return convertDicom2Jpeg(dcmFile, new BufferedOutputStream(new FileOutputStream(jpgFile)), scaleHeight);
046        } catch (FileNotFoundException ex) {
047            System.out.println("\nError: can not write to JPG file!"+ ex.getMessage());
048        }
049        
050        return false;
051    }
052    
053    public static boolean convertDicom2Jpeg(File dcmFile, OutputStream jpgStream){
054        return convertDicom2Jpeg(dcmFile, jpgStream, 0);
055    }
056    
057    public static boolean convertDicom2Jpeg(File dcmFile, OutputStream jpgStream, int scaleHeight){
058        boolean result = false;
059        ByteArrayOutputStream jpgMem = Dicom2MemJPEG(dcmFile, scaleHeight);
060        
061        if (jpgMem != null){
062            try {
063                jpgMem.writeTo(jpgStream);
064                jpgStream.close();
065                result = true;
066            } 
067            catch(IOException e) {
068                System.out.println("\nError: can not write to JPG file!"+ e.getMessage());
069            }
070        }
071        
072        return result;
073    }
074    
075    
076    
077    public static ByteArrayOutputStream Dicom2MemJPEG(File dcmFile){
078        return Dicom2MemJPEG(dcmFile, 0);
079    }
080        
081    public static ByteArrayOutputStream Dicom2MemJPEG(File dcmFile, int scaleHeight){
082        //File myDicomFile = new File("c:/dicomImage.dcm");
083        BufferedImage myJpegImage = null;
084        
085        // returns an Iterator containing all currently registered ImageReaders 
086        // that claim to be able to decode the named format 
087        // (e.g., "DICOM", "jpeg", "tiff")
088        Iterator iter = ImageIO.getImageReadersByFormatName("DICOM");  
089        ImageReader reader = (ImageReader) iter.next();
090        DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();
091        
092        try {
093            ImageInputStream iis = ImageIO.createImageInputStream(dcmFile);
094            reader.setInput(iis, false); 
095            myJpegImage = reader.read(0, param); 
096            iis.close();
097
098            if (myJpegImage == null) {
099                System.out.println("\nError: couldn't read dicom image!");
100                return null;
101            }
102            
103
104            // Resize Image -> Thumbnails....
105            if (scaleHeight > 0){
106                if (scaleHeight < 24) 
107                    scaleHeight = 24; // minimum
108                myJpegImage = getScaledImageWithHeight(myJpegImage, scaleHeight);           
109            }
110                    
111            //OutputStream output = new BufferedOutputStream(new FileOutputStream(jpgFile));
112            ByteArrayOutputStream jpgArray = new ByteArrayOutputStream();
113            OutputStream output = new BufferedOutputStream(jpgArray); 
114            
115            //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
116            //encoder.encode(myJpegImage);
117            output.close();             // Has no effect to ByteArrayOutputStream
118            
119            return jpgArray;
120        } 
121        catch(IOException e) {
122            System.out.println("\nError: couldn't read dicom image!"+ e.getMessage());
123            return null;
124        }
125        catch(Exception e) {
126            System.out.println("\nError: "+ e.getMessage());
127            return null;
128        }
129    }
130    
131    
132    /**A method that scales a Buffered image and takes the required height as a refference point**/
133    public static BufferedImage getScaledImageWithHeight(BufferedImage image, int height) throws java.lang.Exception {
134        int width = (int) (((float) image.getWidth() / (float) image.getHeight()) * height);
135
136        Image scaledImage = image.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
137        BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
138        Graphics2D g2 = (Graphics2D) outImage.createGraphics();
139        g2.drawImage(scaledImage, 0, 0, null);
140
141        return outImage;
142    }
143    
144}