Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.operations.load

File AbstractLoader.java

 

Coverage histogram

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

Code metrics

10
22
4
1
123
67
10
0.45
5.5
4
2.5
18.2% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
AbstractLoader 56 22 18.2% 10 6
0.833333383.3%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2018 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  17 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  29 if (loaded == null) {
74  0 throw new ImagingException("Could not load image for " + filterParams);
75    }
76   
77  29 int resolvedTargetImageType = ImageUtil.getImageType(loaded);
78    // when having background or anything else than 8bits per pixel
79  29 if (backgroundColor != null || loaded.getType() != resolvedTargetImageType) {
80  9 final BufferedImage img = new BufferedImage(loaded.getWidth(), loaded.getHeight(), resolvedTargetImageType);
81   
82  9 final Graphics2D g = img.createGraphics();
83  9 if (backgroundColor != null) {
84  1 g.setColor(backgroundColor);
85  1 g.fill(new Rectangle(0, 0, img.getWidth(), img.getHeight()));
86    }
87  9 g.drawImage(loaded, null, 0, 0);
88   
89  9 g.dispose();
90  9 return img;
91    }
92  20 return loaded;
93    }
94   
95    protected abstract BufferedImage loadSource(P filterParams) throws ImagingException;
96   
 
97  19 toggle protected BufferedImage doReadAndClose(InputStream inputStream) throws IOException, ImagingException {
98  19 if (inputStream == null) {
99  0 throw new IOException("No input");
100    }
101  19 try {
102  19 return imageDecoder.read(inputStream);
103    } finally {
104  19 inputStream.close();
105    }
106    }
107   
 
108    toggle public ImageDecoder getImageDecoder() {
109    return imageDecoder;
110    }
111   
 
112    toggle public void setImageDecoder(ImageDecoder imageDecoder) {
113    this.imageDecoder = imageDecoder;
114    }
115   
 
116    toggle public Color getBackgroundColor() {
117    return backgroundColor;
118    }
119   
 
120    toggle public void setBackgroundColor(Color backgroundColor) {
121    this.backgroundColor = backgroundColor;
122    }
123    }