Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.operations.load

File AbstractLoader.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
50% of files have more coverage

Code metrics

8
20
4
1
121
64
8
0.4
5
4
2
20% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
AbstractLoader 56 20 20% 8 6
0.812581.2%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.load;
35   
36    import info.magnolia.imaging.ImagingException;
37    import info.magnolia.imaging.ParameterProvider;
38    import info.magnolia.imaging.operations.ImageOperation;
39    import info.magnolia.imaging.util.ImageUtil;
40    import info.magnolia.objectfactory.Components;
41   
42    import java.awt.Color;
43    import java.awt.Graphics2D;
44    import java.awt.Rectangle;
45    import java.awt.image.BufferedImage;
46    import java.io.IOException;
47    import java.io.InputStream;
48   
49    /**
50    * An abstract ImageOperation used to load images. It takes charge of converting the loaded image
51    * to the appropriate type. (ImageIO.read() will return a different image type depending on the
52    * source format). Overhead is negligible.
53    *
54    * @param <P> type of ParameterProvider's parameter
55    */
 
56    public abstract class AbstractLoader<P extends ParameterProvider<?>> implements ImageOperation<P> {
57    private ImageDecoder imageDecoder = Components.getComponentProvider().newInstance(ImageDecoder.class);
58    private Color backgroundColor;
59   
 
60  16 toggle protected AbstractLoader() {
61    }
62   
 
63  8 toggle protected AbstractLoader(Color backgroundColor) {
64  8 this.backgroundColor = backgroundColor;
65    }
66   
 
67  29 toggle @Override
68    public BufferedImage apply(BufferedImage source, P filterParams) throws ImagingException {
69  29 if (source != null) {
70  0 throw new ImagingException("This operation currently does not support overlaying images");
71    }
72  29 final BufferedImage loaded = loadSource(filterParams);
73  26 if (loaded == null) {
74  0 throw new ImagingException("Could not load image for " + filterParams);
75    }
76   
77  26 int imageType = ImageUtil.getImageType(loaded);
78  26 final BufferedImage img = new BufferedImage(loaded.getWidth(), loaded.getHeight(), imageType);
79   
80  26 final Graphics2D g = img.createGraphics();
81   
82  26 if (backgroundColor != null) {
83  1 g.setColor(backgroundColor);
84  1 g.fill(new Rectangle(0, 0, img.getWidth(), img.getHeight()));
85    }
86  26 g.drawImage(loaded, null, 0, 0);
87    // TODO would this make any difference ? g.drawRenderedImage(loaded, null);
88   
89  26 g.dispose();
90  26 return img;
91    }
92   
93    protected abstract BufferedImage loadSource(P filterParams) throws ImagingException;
94   
 
95  19 toggle protected BufferedImage doReadAndClose(InputStream inputStream) throws IOException, ImagingException {
96  19 if (inputStream == null) {
97  0 throw new IOException("No input");
98    }
99  19 try {
100  19 return imageDecoder.read(inputStream);
101    } finally {
102  19 inputStream.close();
103    }
104    }
105   
 
106    toggle public ImageDecoder getImageDecoder() {
107    return imageDecoder;
108    }
109   
 
110    toggle public void setImageDecoder(ImageDecoder imageDecoder) {
111    this.imageDecoder = imageDecoder;
112    }
113   
 
114    toggle public Color getBackgroundColor() {
115    return backgroundColor;
116    }
117   
 
118    toggle public void setBackgroundColor(Color backgroundColor) {
119    this.backgroundColor = backgroundColor;
120    }
121    }