Package org.photovault.dbhelper

Examples of org.photovault.dbhelper.ODMGXAWrapper


     exported in its "natural" resolution (i.e. not scaled)
     @param height Height of the exported image in pixels
     @throws PhotovaultException if exporting the photo fails for some reason.
     */
    public void exportPhoto( File file, int width, int height ) throws PhotovaultException {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
       
        // Find the original image to use as a staring point
        ImageInstance original = null;
        for ( int n = 0; n < instances.size(); n++ ) {
            ImageInstance instance = (ImageInstance) instances.get( n );
            if ( instance.getInstanceType() == ImageInstance.INSTANCE_TYPE_ORIGINAL
                    && instance.getImageFile() != null
                    && instance.getImageFile().exists() ) {
                original = instance;
                txw.lock( original, Transaction.READ );
                break;
            }
        }
        if ( original == null ) {
            // If there are no instances, nothing can be exported
            log.warn( "Error - no original image was found!!!" );
            txw.commit();
            throw new PhotovaultException( "No image file found to export photo" );
        }
       
        // Read the image
        RenderedImage exportImage = null;
        try {
            File imageFile = original.getImageFile();
            String fname = imageFile.getName();
            int lastDotPos = fname.lastIndexOf( "." );
            if ( lastDotPos <= 0 || lastDotPos >= fname.length()-1 ) {
                throw new IOException( "Cannot determine file type extension of " + imageFile.getAbsolutePath() );
            }
            PhotovaultImageFactory imageFactory = new PhotovaultImageFactory();
            PhotovaultImage img = null;
            try {
                /*
                 Do not read the image yet since setting raw conversion
                 parameters later may force a re-read.
                 */
                img = imageFactory.create(imageFile, false, false);
            } catch (PhotovaultException ex) {
                log.error( ex.getMessage() );
            }
            img.setCropBounds( this.getCropBounds() );
            img.setRotation( prefRotation - original.getRotated() );
            if ( channelMap != null ) {
                img.setColorAdjustment( channelMap );
            }
            if ( img instanceof RawImage ) {
                RawImage ri = (RawImage) img;
                if ( rawSettings != null ) {
                    ri.setRawSettings( rawSettings );
                } else if ( rawSettings == null ) {
                    // No raw settings for this photo yet, let's use
                    // the thumbnail settings
                    rawSettings = ri.getRawSettings();
                    txw.lock( rawSettings, Transaction.WRITE );
                }
            }
            if ( width > 0 ) {
                exportImage =img.getRenderedImage( width, height, false );
            } else {
                exportImage =img.getRenderedImage( 1.0, false );
            }
        } catch ( Exception e ) {
            log.warn( "Error reading image: " + e.getMessage() );
            txw.abort();
            throw new PhotovaultException( "Error reading image: " + e.getMessage(), e );
        }
               
        // Reduce to 8 bit samples if we have more...
        if ( exportImage.getSampleModel().getSampleSize( 0 ) == 16 ) {
            double[] subtract = new double[1]; subtract[0] = 0;
            double[] divide   = new double[1]; divide[0]   = 1./256.;
            // Now we can rescale the pixels gray levels:
            ParameterBlock pbRescale = new ParameterBlock();
            pbRescale.add(divide);
            pbRescale.add(subtract);
            pbRescale.addSource( exportImage );
            PlanarImage outputImage = (PlanarImage)JAI.create("rescale", pbRescale, null);
            // Make sure it is a byte image - force conversion.
            ParameterBlock pbConvert = new ParameterBlock();
            pbConvert.addSource(outputImage);
            pbConvert.add(DataBuffer.TYPE_BYTE);
            exportImage = JAI.create("format", pbConvert);
        }
       
        // Try to determine the file type based on extension
        String ftype = "jpg";
        String imageFname = file.getName();
        int extIndex = imageFname.lastIndexOf( "." ) + 1;
        if ( extIndex > 0 ) {
            ftype = imageFname.substring( extIndex );
        }
       
        try {
            // Find a writer for that file extensions
            ImageWriter writer = null;
            Iterator iter = ImageIO.getImageWritersByFormatName( ftype );
            if (iter.hasNext()) writer = (ImageWriter)iter.next();
            if (writer != null) {
                ImageOutputStream ios = null;
                try {
                    // Prepare output file
                    ios = ImageIO.createImageOutputStream( file );
                    writer.setOutput(ios);
                    // Set some parameters
                    ImageWriteParam param = writer.getDefaultWriteParam();
                    // if bi has type ARGB and alpha is false, we have
                    // to tell the writer to not use the alpha
                    // channel: this is especially needed for jpeg
                    // files where imageio seems to produce wrong jpeg
                    // files right now...
//                    if (exportImage.getType() == BufferedImage.TYPE_INT_ARGB ) {
//                        // this is not so obvious: create a new
//                        // ColorModel without OPAQUE transparency and
//                        // no alpha channel.
//                        ColorModel cm = new ComponentColorModel(exportImage.getColorModel().getColorSpace(),
//                                false, false,
//                                Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
//                        // tell the writer to only use the first 3 bands (skip alpha)
//                        int[] bands = {0, 1, 2};
//                        param.setSourceBands(bands);
//                        // although the java documentation says that
//                        // SampleModel can be null, an exception is
//                        // thrown in that case therefore a 1*1
//                        // SampleModel that is compatible to cm is
//                        // created:
//                        param.setDestinationType(new ImageTypeSpecifier(cm,
//                                cm.createCompatibleSampleModel(1, 1)));
//                    }
                    // Write the image
                    writer.write(null, new IIOImage(exportImage, null, null), param);
                   
                    // Cleanup
                    ios.flush();
                } finally {
                    if (ios != null) ios.close();
                    writer.dispose();
                    if ( exportImage != null & exportImage instanceof PlanarImage ) {
                        ((PlanarImage)exportImage).dispose();
                        System.gc();
                    }
                }
            }
           
        } catch ( IOException e ) {
            log.warn( "Error writing exported image: " + e.getMessage() );
            txw.abort();
            throw new PhotovaultException( "Error writing exported image: " + e.getMessage(), e );
        }
       
        txw.commit();
    }
View Full Code Here


    /**
     Sets the original instance hash. This is intended for only internal use
     @param hash MD5 hash value for original instance
     */
    protected void setOrigInstanceHash( byte[] hash ) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        origInstanceHash = (byte[]) hash.clone();
        txw.commit();
    }
View Full Code Here

     * Get the value of shootTime. Note that shoot time can also be
     null (to mean that the time is unspecified)1
     @return value of shootTime.
     */
    public java.util.Date getShootTime() {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.READ );
        txw.commit();
        return shootTime != null ? (java.util.Date) shootTime.clone() : null;
    }
View Full Code Here

    /**
     * Set the value of shootTime.
     * @param v  Value to assign to shootTime.
     */
    public void setShootTime(java.util.Date  v) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        this.shootTime = (v != null) ? (java.util.Date) v.clone()  : null;
        modified();
        txw.commit();
    }
View Full Code Here

    /**
     Set both shooting time & accuracy directly using a FuzzyTime object
     @param v FuzzyTime containing new values.
     */
    public void setFuzzyShootTime( FuzzyDate v ) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        this.shootTime = (java.util.Date) v.getDate().clone();
        this.timeAccuracy = v.getAccuracy();
        modified();
        txw.commit();       
    }
View Full Code Here

    /**
     * Get the value of desc.
     * @return value of desc.
     */
    public String getDesc() {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.READ );
        txw.commit();
        return desc;
    }
View Full Code Here

    /**
     * Set the value of desc.
     * @param v  Value to assign to desc.
     */
    public void setDesc(String  v) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        this.desc = v;
        modified();
        txw.commit();
    }
View Full Code Here

    /**
     * Get the value of FStop.
     * @return value of FStop.
     */
    public double getFStop() {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.READ );
        txw.commit();
        return FStop;
    }
View Full Code Here

    /**
     * Set the value of FStop.
     * @param v  Value to assign to FStop.
     */
    public void setFStop(double  v) {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.WRITE );
        this.FStop = v;
        modified();
        txw.commit();
    }
View Full Code Here

    /**
     * Get the value of focalLength.
     * @return value of focalLength.
     */
    public double getFocalLength() {
        ODMGXAWrapper txw = new ODMGXAWrapper();
        txw.lock( this, Transaction.READ );
        txw.commit();
        return focalLength;
    }
View Full Code Here

TOP

Related Classes of org.photovault.dbhelper.ODMGXAWrapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.