View Javadoc
1   /***
2    * Copyright (C) 2009 TM-Search Community.
3    *
4    * This file is part of TM-Search Services.
5    *
6    * Foobar is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Foobar is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package eu.europa.tmsearch.services.business.util;
21  
22  import java.awt.Dimension;
23  import java.awt.Point;
24  import java.awt.Transparency;
25  import java.awt.color.ColorSpace;
26  import java.awt.image.BufferedImage;
27  import java.awt.image.ColorModel;
28  import java.awt.image.ComponentColorModel;
29  import java.awt.image.DataBuffer;
30  import java.awt.image.DataBufferByte;
31  import java.awt.image.PixelInterleavedSampleModel;
32  import java.awt.image.Raster;
33  import java.awt.image.WritableRaster;
34  
35  import magick.ImageInfo;
36  import magick.MagickException;
37  import magick.MagickImage;
38  
39  import org.apache.log4j.Logger;
40  
41  import eu.europa.tmsearch.services.business.exception.BusinessServiceException;
42  import eu.europa.tmsearch.services.business.exception.ImageProcessorRuntimeException;
43  import eu.europa.tmsearch.services.model.TradeMarkImage;
44  
45  /***
46   * An ImageProcessor implementation using JMagick for manipulating images.
47   * Requires native ImageMagick and JMagick libraries to be installed on the
48   * platform
49   * 
50   * 
51   */
52  public class JMagickImageProcessorImpl implements ImageProcessor {
53  
54      private static Logger log = Logger.getLogger(JMagickImageProcessorImpl.class);
55  
56      /***
57       * Echoes java.library.path to debug log
58       */
59      public JMagickImageProcessorImpl() {
60  	if (log.isDebugEnabled()) {
61  	    String javaLibraryPath = System.getProperty("java.library.path");
62  	    log.debug("Environment variable java.library.path points to '" + javaLibraryPath + "'");
63  	}
64      }
65  
66      /***
67       * Generates a thumbnail trade mark image from a master's TradeMark image.
68       * 
69       * @param tradeMarkImage
70       *            Master trademark image
71       * @param height
72       *            Desired height. If 0 thumbnail height is calculated to
73       *            preserve master image's aspect ratio.
74       * @param width
75       *            Desired width
76       * @return A TradeMarkImage containing the thumbnail
77       * @throws ImageProcessorRuntimeException
78       *             if native ImageMagick and JMagick libraries are not installed
79       *             or properly configured in the running platform
80       */
81      public TradeMarkImage getThumbnailImage(TradeMarkImage tradeMarkImage, int height, int width)
82  	    throws ImageProcessorRuntimeException {
83  	TradeMarkImage trademarkThumbnailImage = new TradeMarkImage();
84  	try {
85  
86  	    ImageInfo info = new ImageInfo();
87  	    MagickImage blobImage = new MagickImage(info, tradeMarkImage.getImageData());
88  	    height = (height > 0) ? height : (width * blobImage.getDimension().height / blobImage
89  		    .getDimension().width);
90  	    log.debug("buffer size: " + tradeMarkImage.getImageData().length + " HxW: " + height + "x"
91  		    + width);
92  	    MagickImage scaled = blobImage.zoomImage(width, height);
93  	    trademarkThumbnailImage.setImageData(scaled.imageToBlob(info));
94  	    blobImage.destroyImages();
95  	} catch (MagickException me) {
96  	    log.error("Caught MagickException while converting image", me);
97  	    throw new BusinessServiceException();
98  	} catch (UnsatisfiedLinkError ule) {
99  	    log.error("Unable to find required native library in 'java.library.path'. Details: "
100 		    + ule.getMessage());
101 	    throw new ImageProcessorRuntimeException(ule);
102 	} catch (NoClassDefFoundError ncdfe) {
103 	    log
104 		    .error("Unable to find required class for manipulating images. Details: "
105 			    + ncdfe.getMessage());
106 	    log.error("Complete info: ", ncdfe);
107 	    throw new ImageProcessorRuntimeException(ncdfe);
108 	}
109 	return trademarkThumbnailImage;
110     }
111     
112     public TradeMarkImage getImage(TradeMarkImage tradeMarkImage) {
113 	throw new UnsupportedOperationException();
114     }
115 }