Changeset 6077

Show
Ignore:
Timestamp:
02/11/10 17:36:40 (6 months ago)
Author:
cblackburn
Message:

Provide paths to jpg thumbnails of repository images, with basic caching.

Location:
trunk/components/blitz
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/components/blitz/resources/omero/Repositories.ice

    r6032 r6077  
    117117            void transfer(string srcPath, Repository* target, string targetPath) throws ServerError; 
    118118 
     119            string getThumbnail(string path) throws ServerError; 
    119120        }; 
    120121 
  • trunk/components/blitz/src/ome/services/blitz/repo/PublicRepositoryI.java

    r6035 r6077  
    1313import java.io.File; 
    1414import java.io.FileFilter; 
     15import java.io.IOException; 
     16import java.io.PrintWriter; 
     17import java.io.StringWriter; 
    1518import java.util.ArrayList; 
    1619import java.util.Arrays; 
     
    3841import omero.util.IceMapper; 
    3942 
     43import loci.formats.*; // need to close this down once the r/w are sorted. 
     44import loci.formats.meta.IMetadata; 
     45 
    4046import org.apache.commons.io.FileUtils; 
    4147import org.apache.commons.io.filefilter.FileFileFilter; 
     
    6470 
    6571    private final Principal principal; 
     72     
     73    private final static String OMERO_PATH = ".omero"; 
     74    private final static String THUMB_PATH = "thumbnails"; 
    6675 
    6776    public PublicRepositoryI(File root, long repoObjectId, Executor executor, 
     
    282291    } 
    283292 
     293    /** 
     294     * Get the format object for a file. 
     295     *  
     296     * @param path 
     297     *            A path on a repository. 
     298     * @param __current 
     299     *            ice context. 
     300     * @return Format object 
     301     * 
     302     */ 
    284303    public Format format(String path, Current __current) throws ServerError { 
    285         File f = new File(path).getAbsoluteFile(); 
    286         return getFileFormat(f); 
    287     } 
     304        File file = checkPath(path); 
     305        return getFileFormat(file); 
     306    } 
     307 
     308    /** 
     309     * Get (the path of) the thumbnail image for an image file on the repository. 
     310     *  
     311     * @param path 
     312     *            A path on a repository. 
     313     * @param __current 
     314     *            ice context. 
     315     * @return The path of the thumbnail 
     316     * 
     317     */ 
     318    public String getThumbnail(String path, Current __current)  throws ServerError { 
     319        File file = checkPath(path); 
     320        String tnPath; 
     321        try { 
     322            tnPath = createThumbnail(file);    
     323        } catch (ServerError exc) { 
     324            throw exc; 
     325        } 
     326        return tnPath; 
     327    } 
     328 
    288329 
    289330    // 
     
    443484    } 
    444485 
    445  
    446  
     486    /** 
     487     * Create a jpeg thumbnail from an image file  
     488     *  
     489     * @param path 
     490     *            A path to a file. 
     491     * @return The path of the thumbnail 
     492     * 
     493     * TODO Weak at present, no caching 
     494     */ 
     495     private String createThumbnail(File file)  throws ServerError { 
     496         
     497        IFormatReader reader; 
     498        byte[] thumb; 
     499         
     500        File parent = file.getParentFile(); 
     501        File tnParent = new File(new File(parent, OMERO_PATH), THUMB_PATH); 
     502        tnParent.mkdirs(); // Need to check if this exists after? 
     503        File tnFile = new File(tnParent, file.getName() + "_tn.jpg"); 
     504         
     505        // Very basic caching...if a file exists return it. 
     506        if (tnFile.exists()) { 
     507            return tnFile.getAbsolutePath(); 
     508        } 
     509         
     510        // As it doesn't exist, create it. 
     511        reader = new ImageReader(); 
     512        reader.setNormalized(true); 
     513        try { 
     514            reader.setId(file.getAbsolutePath()); 
     515            // open middle image thumbnail 
     516            int z = reader.getSizeZ() / 2; 
     517            int t = reader.getSizeT() / 2; 
     518            int ndx = reader.getIndex(z, 0, t); 
     519            thumb = reader.openThumbBytes(ndx);  
     520        } catch (FormatException exc) {  
     521            throw new ServerError(null, null, "Thumbnail error, read failed.");  
     522        } catch (IOException exc) {  
     523            throw new ServerError(null, null, "Thumbnail error, read failed.");  
     524        } 
     525         
     526        // How much of this is needed for a jpeg?  
     527        // At present provides monochrome images, need to provide colour? 
     528        IMetadata meta = MetadataTools.createOMEXMLMetadata(); 
     529        int thumbSizeX = reader.getThumbSizeX(); 
     530        int thumbSizeY = reader.getThumbSizeY();   
     531        int pixelType = FormatTools.UINT8;             
     532        meta.createRoot(); 
     533        meta.setPixelsBigEndian(Boolean.TRUE, 0, 0); 
     534        meta.setPixelsDimensionOrder("XYZCT", 0, 0); 
     535        meta.setPixelsPixelType(FormatTools.getPixelTypeString(pixelType), 0, 0); 
     536        meta.setPixelsSizeX(thumbSizeX, 0, 0); 
     537        meta.setPixelsSizeY(thumbSizeY, 0, 0); 
     538        meta.setPixelsSizeZ(1, 0, 0); 
     539        meta.setPixelsSizeC(1, 0, 0); 
     540        meta.setPixelsSizeT(1, 0, 0); 
     541        meta.setLogicalChannelSamplesPerPixel(1, 0, 0); 
     542             
     543        try { 
     544            IFormatWriter writer = new ImageWriter(); 
     545            writer.setMetadataRetrieve(meta); 
     546            writer.setId(tnFile.getAbsolutePath()); 
     547            writer.saveBytes(thumb, true); 
     548            writer.close(); 
     549            return tnFile.getAbsolutePath();   
     550        } catch (FormatException exc) {  
     551            throw new ServerError(null, stackTraceAsString(exc), "Thumbnail error, write failed.");  
     552        } catch (IOException exc) {  
     553            throw new ServerError(null, stackTraceAsString(exc), "Thumbnail error, write failed.");  
     554        } 
     555         
     556        } 
     557 
     558    // Utility function for passing stack traces back in exceptions. 
     559    private String stackTraceAsString(Exception exception) { 
     560        StringWriter sw = new StringWriter(); 
     561        exception.printStackTrace(new PrintWriter(sw)); 
     562        return sw.toString(); 
     563    } 
    447564} 

1.2.1-PRO © 2008-2009 agile42 all rights reserved (this page was served in: 0.93023 sec.)