Search in sources :

Example 1 with GIFOutputStream

use of GIFUtils.GIFOutputStream in project vcell by virtualcell.

the class IMGExporter method createMedia.

private static void createMedia(Vector<ExportOutput> exportOutputV, VCDataIdentifier vcdID, String dataID, ExportSpecs exportSpecs, boolean bEndVars, boolean bEndSlice, boolean bBeginTime, boolean bEndTime, boolean bSingleTimePoint, String[] varNameArr, DisplayPreferences[] displayPreferencesArr, MovieHolder movieHolder, int[] pixels, int mirrorWidth, int mirrorHeight, FileDataContainerManager fileDataContainerManager) throws Exception {
    boolean isGrayScale = true;
    for (int i = 0; i < displayPreferencesArr.length; i++) {
        if (!displayPreferencesArr[i].isGrayScale()) {
            isGrayScale = false;
            break;
        }
    }
    FormatSpecificSpecs formatSpecificSpecs = exportSpecs.getFormatSpecificSpecs();
    if (exportSpecs.getFormat() == ExportFormat.GIF && formatSpecificSpecs instanceof ImageSpecs) /* && ((ImageSpecs)formatSpecificSpecs).getFormat() == ExportConstants.GIF*/
    {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        GIFOutputStream gifOut = new GIFOutputStream(bytesOut);
        GIFImage gifImage = new GIFUtils.GIFImage(pixels, mirrorWidth);
        gifImage.write(gifOut);
        gifOut.close();
        byte[] data = bytesOut.toByteArray();
        ExportOutput exportOutput = new ExportOutput(true, ".gif", vcdID.getID(), dataID, fileDataContainerManager);
        fileDataContainerManager.append(exportOutput.getFileDataContainerID(), data);
        exportOutputV.add(exportOutput);
    } else if (exportSpecs.getFormat() == ExportFormat.FORMAT_JPEG && formatSpecificSpecs instanceof ImageSpecs) /* && ((ImageSpecs)formatSpecificSpecs).getFormat() == ExportConstants.JPEG*/
    {
        VideoMediaSample jpegEncodedVideoMediaSample = FormatSpecificSpecs.getVideoMediaSample(mirrorWidth, mirrorHeight, 1, isGrayScale, FormatSpecificSpecs.CODEC_JPEG, ((ImageSpecs) formatSpecificSpecs).getcompressionQuality(), pixels);
        ExportOutput exportOutput = new ExportOutput(true, ".jpg", vcdID.getID(), dataID, fileDataContainerManager);
        fileDataContainerManager.append(exportOutput.getFileDataContainerID(), jpegEncodedVideoMediaSample.getDataBytes());
        exportOutputV.add(exportOutput);
    } else if (exportSpecs.getFormat() == ExportFormat.ANIMATED_GIF && formatSpecificSpecs instanceof ImageSpecs) /* && ((ImageSpecs)formatSpecificSpecs).getFormat() == ExportConstants.ANIMATED_GIF*/
    {
        // 1/100's of a second
        int imageDuration = (int) Math.ceil((movieHolder.getSampleDurationSeconds() * 100));
        if (bEndTime && (((ImageSpecs) formatSpecificSpecs).getLoopingMode() != 0 || bSingleTimePoint)) {
            imageDuration = 0;
        }
        if (bBeginTime) {
            movieHolder.setGifImage(new GIFUtils.GIFImage(pixels, mirrorWidth));
            movieHolder.getGifImage().setDelay(imageDuration);
        } else {
            movieHolder.getGifImage().addImage(pixels, mirrorWidth, true);
            movieHolder.getGifImage().setDelay(movieHolder.getGifImage().countImages() - 1, imageDuration);
        }
        if (bEndTime) {
            movieHolder.getGifImage().setIterationCount(((ImageSpecs) formatSpecificSpecs).getLoopingMode());
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
            GIFOutputStream gifOut = new GIFOutputStream(bytesOut);
            movieHolder.getGifImage().write(gifOut);
            gifOut.close();
            byte[] data = bytesOut.toByteArray();
            ExportOutput exportOutput = new ExportOutput(true, ".gif", vcdID.getID(), dataID, fileDataContainerManager);
            fileDataContainerManager.append(exportOutput.getFileDataContainerID(), data);
            exportOutputV.add(exportOutput);
        }
    } else if (exportSpecs.getFormat() == ExportFormat.QUICKTIME && formatSpecificSpecs instanceof MovieSpecs) {
        String VIDEOMEDIACHUNKID = (varNameArr.length == 1 ? varNameArr[0] : "OVERLAY");
        // number of units per second in movie
        final int TIMESCALE = 1000;
        boolean bQTVR = ((MovieSpecs) exportSpecs.getFormatSpecificSpecs()).isQTVR();
        int sampleDuration = (bQTVR ? TIMESCALE : (int) (TIMESCALE * movieHolder.getSampleDurationSeconds()));
        VideoMediaSample videoMediaSample = FormatSpecificSpecs.getVideoMediaSample(mirrorWidth, mirrorHeight, sampleDuration, isGrayScale, FormatSpecificSpecs.CODEC_JPEG, ((MovieSpecs) formatSpecificSpecs).getcompressionQuality(), pixels);
        if (bBeginTime && (!bQTVR || movieHolder.getVarNameVideoMediaChunkHash().get(VIDEOMEDIACHUNKID) == null)) {
            movieHolder.getVarNameVideoMediaChunkHash().put(VIDEOMEDIACHUNKID, new Vector<VideoMediaChunk>());
            movieHolder.getVarNameDataIDHash().put(VIDEOMEDIACHUNKID, dataID);
        }
        movieHolder.getVarNameVideoMediaChunkHash().get(VIDEOMEDIACHUNKID).add(new VideoMediaChunk(videoMediaSample, fileDataContainerManager));
        if (bEndTime && !bQTVR) {
            String simID = exportSpecs.getVCDataIdentifier().getID();
            double[] allTimes = exportSpecs.getTimeSpecs().getAllTimes();
            int beginTimeIndex = exportSpecs.getTimeSpecs().getBeginTimeIndex();
            int endTimeIndex = exportSpecs.getTimeSpecs().getEndTimeIndex();
            VideoMediaChunk[] videoMediaChunkArr = movieHolder.getVarNameVideoMediaChunkHash().get(VIDEOMEDIACHUNKID).toArray(new VideoMediaChunk[0]);
            MediaTrack videoTrack = new MediaTrack(videoMediaChunkArr);
            MediaMovie newMovie = new MediaMovie(videoTrack, videoTrack.getDuration(), TIMESCALE);
            newMovie.addUserDataEntry(new UserDataEntry("cpy", "�" + (new GregorianCalendar()).get(Calendar.YEAR) + ", UCHC"));
            newMovie.addUserDataEntry(new UserDataEntry("des", "Dataset name: " + simID));
            newMovie.addUserDataEntry(new UserDataEntry("cmt", "Time range: " + allTimes[beginTimeIndex] + " - " + allTimes[endTimeIndex]));
            for (int i = 0; varNameArr != null && i < varNameArr.length; i++) {
                newMovie.addUserDataEntry(new UserDataEntry("v" + (i < 10 ? "0" : "") + i, "Variable name: " + varNameArr[i] + "\nmin: " + (displayPreferencesArr == null || displayPreferencesArr[i].getScaleSettings() == null ? "default" : displayPreferencesArr[i].getScaleSettings().getMin()) + "\nmax: " + (displayPreferencesArr == null || displayPreferencesArr[i].getScaleSettings() == null ? "default" : displayPreferencesArr[i].getScaleSettings().getMax())));
            }
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            DataOutputStream movieos = null;
            try {
                ExportOutput exportOutput = new ExportOutput(true, ".mov", simID, dataID, fileDataContainerManager);
                File tempMovieFile = File.createTempFile("Movie", "temp");
                fileDataContainerManager.manageExistingTempFile(exportOutput.getFileDataContainerID(), tempMovieFile);
                fos = new FileOutputStream(tempMovieFile);
                bos = new BufferedOutputStream(fos);
                movieos = new DataOutputStream(bos);
                MediaMethods.writeMovie(movieos, newMovie);
                movieos.close();
                exportOutputV.add(exportOutput);
                movieHolder.getVarNameVideoMediaChunkHash().clear();
                movieHolder.getVarNameDataIDHash().clear();
            } finally {
                if (movieos != null) {
                    try {
                        movieos.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } else if (bEndVars && bEndTime && bEndSlice && bQTVR) {
            String simID = exportSpecs.getVCDataIdentifier().getID();
            Enumeration<String> allStoredVarNamesEnum = movieHolder.getVarNameVideoMediaChunkHash().keys();
            while (allStoredVarNamesEnum.hasMoreElements()) {
                String varName = allStoredVarNamesEnum.nextElement();
                String storedDataID = movieHolder.getVarNameDataIDHash().get(varName);
                VideoMediaChunk[] videoMediaChunkArr = movieHolder.getVarNameVideoMediaChunkHash().get(varName).toArray(new VideoMediaChunk[0]);
                int beginTimeIndex = exportSpecs.getTimeSpecs().getBeginTimeIndex();
                int endTimeIndex = exportSpecs.getTimeSpecs().getEndTimeIndex();
                int numTimes = endTimeIndex - beginTimeIndex + 1;
                FileOutputStream fos = null;
                BufferedOutputStream bos = null;
                DataOutputStream movieos = null;
                try {
                    ExportOutput exportOutput = new ExportOutput(true, ".mov", simID, storedDataID, fileDataContainerManager);
                    File tempMovieFile = File.createTempFile("Movie", "temp");
                    fileDataContainerManager.manageExistingTempFile(exportOutput.getFileDataContainerID(), tempMovieFile);
                    fos = new FileOutputStream(tempMovieFile);
                    bos = new BufferedOutputStream(fos);
                    movieos = new DataOutputStream(bos);
                    writeQTVRWorker(movieos, videoMediaChunkArr, numTimes, videoMediaChunkArr.length / numTimes, mirrorWidth, mirrorHeight);
                    movieos.close();
                    exportOutputV.add(exportOutput);
                } finally {
                    if (movieos != null) {
                        try {
                            movieos.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (bos != null) {
                        try {
                            bos.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            movieHolder.getVarNameVideoMediaChunkHash().clear();
            movieHolder.getVarNameDataIDHash().clear();
        }
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) VideoMediaSample(cbit.vcell.export.gloworm.quicktime.VideoMediaSample) GIFImage(GIFUtils.GIFImage) GIFOutputStream(GIFUtils.GIFOutputStream) VideoMediaChunk(cbit.vcell.export.gloworm.quicktime.VideoMediaChunk) Vector(java.util.Vector) BufferedOutputStream(java.io.BufferedOutputStream) GIFImage(GIFUtils.GIFImage) UserDataEntry(cbit.vcell.export.gloworm.atoms.UserDataEntry) GregorianCalendar(java.util.GregorianCalendar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GIFFormatException(GIFUtils.GIFFormatException) RemoteException(java.rmi.RemoteException) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) UserCancelException(org.vcell.util.UserCancelException) MediaTrack(cbit.vcell.export.gloworm.quicktime.MediaTrack) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MediaMovie(cbit.vcell.export.gloworm.quicktime.MediaMovie) VRMediaMovie(cbit.vcell.export.gloworm.quicktime.VRMediaMovie)

Aggregations

GIFFormatException (GIFUtils.GIFFormatException)1 GIFImage (GIFUtils.GIFImage)1 GIFOutputStream (GIFUtils.GIFOutputStream)1 UserDataEntry (cbit.vcell.export.gloworm.atoms.UserDataEntry)1 MediaMovie (cbit.vcell.export.gloworm.quicktime.MediaMovie)1 MediaTrack (cbit.vcell.export.gloworm.quicktime.MediaTrack)1 VRMediaMovie (cbit.vcell.export.gloworm.quicktime.VRMediaMovie)1 VideoMediaChunk (cbit.vcell.export.gloworm.quicktime.VideoMediaChunk)1 VideoMediaSample (cbit.vcell.export.gloworm.quicktime.VideoMediaSample)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 GregorianCalendar (java.util.GregorianCalendar)1 Vector (java.util.Vector)1 DataFormatException (java.util.zip.DataFormatException)1 DataAccessException (org.vcell.util.DataAccessException)1