View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.common.bytesource;
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.awt.*;
25  import java.awt.image.BufferedImage;
26  import java.io.File;
27  import java.io.IOException;
28  import java.lang.reflect.InvocationTargetException;
29  import java.lang.reflect.Method;
30  import java.lang.reflect.Modifier;
31  import java.util.Collection;
32  import java.util.HashMap;
33  import java.util.Map;
34  
35  import org.apache.commons.imaging.ImageFormat;
36  import org.apache.commons.imaging.ImageFormats;
37  import org.apache.commons.imaging.ImageInfo;
38  import org.apache.commons.imaging.ImageReadException;
39  import org.apache.commons.imaging.Imaging;
40  import org.apache.commons.imaging.ImagingConstants;
41  import org.apache.commons.imaging.util.Debug;
42  import org.apache.commons.io.FileUtils;
43  import org.junit.Test;
44  import org.junit.runner.RunWith;
45  import org.junit.runners.Parameterized;
46  
47  @RunWith(Parameterized.class)
48  public class ByteSourceImageTest extends ByteSourceTest {
49  
50      private final File imageFile;
51  
52      @Parameterized.Parameters
53      public static Collection<File> data() throws Exception {
54          return getTestImages();
55      }
56  
57      public ByteSourceImageTest(final File imageFile) {
58          this.imageFile = imageFile;
59      }
60  
61      @Test
62      public void test() throws Exception {
63          Debug.debug("imageFile", imageFile);
64          assertNotNull(imageFile);
65  
66          final byte imageFileBytes[] = FileUtils.readFileToByteArray(imageFile);
67          assertNotNull(imageFileBytes);
68          assertTrue(imageFileBytes.length == imageFile.length());
69  
70          if (imageFile.getName().toLowerCase().endsWith(".ico")
71                  || imageFile.getName().toLowerCase().endsWith(".tga")
72                  || imageFile.getName().toLowerCase().endsWith(".jb2")
73                  || imageFile.getName().toLowerCase().endsWith(".pcx")
74                  || imageFile.getName().toLowerCase().endsWith(".dcx")
75                  || imageFile.getName().toLowerCase().endsWith(".psd")
76                  || imageFile.getName().toLowerCase().endsWith(".wbmp")
77                  || imageFile.getName().toLowerCase().endsWith(".xbm")
78                  || imageFile.getName().toLowerCase().endsWith(".xpm")) {
79              // these formats can't be parsed without a filename hint.
80              // they have ambiguous "magic number" signatures.
81              return;
82          }
83  
84          checkGuessFormat(imageFile, imageFileBytes);
85  
86          if (imageFile.getName().toLowerCase().endsWith(".png")
87                  && imageFile.getParentFile().getName().equalsIgnoreCase("pngsuite")
88                  && imageFile.getName().toLowerCase().startsWith("x")) {
89              return;
90          }
91  
92          checkGetICCProfileBytes(imageFile, imageFileBytes);
93  
94          if (!imageFile.getParentFile().getName().toLowerCase().equals("@broken")) {
95              checkGetImageInfo(imageFile, imageFileBytes);
96          }
97  
98          checkGetImageSize(imageFile, imageFileBytes);
99  
100         final ImageFormat imageFormat = Imaging.guessFormat(imageFile);
101         if (ImageFormats.JPEG != imageFormat
102                 && ImageFormats.UNKNOWN != imageFormat) {
103             checkGetBufferedImage(imageFile, imageFileBytes);
104         }
105     }
106 
107     public void checkGetBufferedImage(final File file, final byte[] bytes) throws Exception {
108         final BufferedImage bufferedImage = Imaging.getBufferedImage(file);
109         assertNotNull(bufferedImage);
110         assertTrue(bufferedImage.getWidth() > 0);
111         assertTrue(bufferedImage.getHeight() > 0);
112         final int imageFileWidth = bufferedImage.getWidth();
113         final int imageFileHeight = bufferedImage.getHeight();
114 
115         final BufferedImage imageBytes = Imaging.getBufferedImage(bytes);
116         assertNotNull(imageBytes);
117         assertTrue(imageFileWidth == imageBytes.getWidth());
118         assertTrue(imageFileHeight == imageBytes.getHeight());
119     }
120 
121     public void checkGetImageSize(final File imageFile, final byte[] imageFileBytes)
122             throws Exception {
123         final Dimension imageSizeFile = Imaging.getImageSize(imageFile);
124         assertNotNull(imageSizeFile);
125         assertTrue(imageSizeFile.width > 0);
126         assertTrue(imageSizeFile.height > 0);
127 
128         final Dimension imageSizeBytes = Imaging.getImageSize(imageFileBytes);
129         assertNotNull(imageSizeBytes);
130         assertTrue(imageSizeFile.width == imageSizeBytes.width);
131         assertTrue(imageSizeFile.height == imageSizeBytes.height);
132     }
133 
134     public void checkGuessFormat(final File imageFile, final byte[] imageFileBytes)
135             throws Exception {
136         // check guessFormat()
137         final ImageFormat imageFormatFile = Imaging.guessFormat(imageFile);
138         assertNotNull(imageFormatFile);
139         assertTrue(imageFormatFile != ImageFormats.UNKNOWN);
140         // Debug.debug("imageFormatFile", imageFormatFile);
141 
142         final ImageFormat imageFormatBytes = Imaging.guessFormat(imageFileBytes);
143         assertNotNull(imageFormatBytes);
144         assertTrue(imageFormatBytes != ImageFormats.UNKNOWN);
145         // Debug.debug("imageFormatBytes", imageFormatBytes);
146 
147         assertTrue(imageFormatBytes == imageFormatFile);
148     }
149 
150     public void checkGetICCProfileBytes(final File imageFile, final byte[] imageFileBytes)
151             throws Exception {
152         // check guessFormat()
153         final byte iccBytesFile[] = Imaging.getICCProfileBytes(imageFile);
154 
155         final byte iccBytesBytes[] = Imaging.getICCProfileBytes(imageFileBytes);
156 
157         assertTrue((iccBytesFile != null) == (iccBytesBytes != null));
158 
159         if (iccBytesFile == null) {
160             return;
161         }
162 
163         assertArrayEquals(iccBytesFile, iccBytesBytes);
164     }
165 
166     public void checkGetImageInfo(final File imageFile, final byte[] imageFileBytes)
167             throws IOException, ImageReadException, IllegalAccessException,
168             IllegalArgumentException, InvocationTargetException {
169         final Map<String, Object> params = new HashMap<>();
170         final boolean ignoreImageData = isPhilHarveyTestImage(imageFile);
171         final ImageFormat imageFormat = Imaging.guessFormat(imageFile);
172         if (imageFormat.equals(ImageFormats.TIFF)
173                 || imageFormat.equals(ImageFormats.JPEG)) {
174             params.put(ImagingConstants.PARAM_KEY_READ_THUMBNAILS, Boolean.valueOf(!ignoreImageData));
175         }
176 
177         final ImageInfo imageInfoFile = Imaging.getImageInfo(imageFile, params);
178 
179         final ImageInfo imageInfoBytes = Imaging.getImageInfo(imageFileBytes, params);
180 
181         assertNotNull(imageInfoFile);
182         assertNotNull(imageInfoBytes);
183 
184         final Method methods[] = ImageInfo.class.getMethods();
185         for (final Method method2 : methods) {
186             final Method method = method2;
187             method.getModifiers();
188             if (!Modifier.isPublic(method.getModifiers())) {
189                 continue;
190             }
191             if (!method.getName().startsWith("get")) {
192                 continue;
193             }
194             if (method.getName().equals("getClass"))
195              {
196                 continue;
197             // if (method.getGenericParameterTypes().length > 0)
198             // continue;
199             }
200 
201             final Object valueFile = method.invoke(imageInfoFile, (Object[])null);
202             final Object valueBytes = method.invoke(imageInfoBytes, (Object[])null);
203 
204             assertTrue(valueFile.equals(valueBytes));
205         }
206 
207         // only have to test values from imageInfoFile; we already know values
208         // match.
209         assertTrue(imageInfoFile.getBitsPerPixel() > 0);
210 
211         assertNotNull(imageInfoFile.getFormat());
212         assertTrue(imageInfoFile.getFormat() != ImageFormats.UNKNOWN);
213 
214         assertNotNull(imageInfoFile.getFormatName());
215 
216         assertTrue(imageInfoFile.getWidth() > 0);
217         assertTrue(imageInfoFile.getHeight() > 0);
218 
219         assertNotNull(imageInfoFile.getMimeType());
220 
221         // TODO: not all adapters count images yet.
222         // assertTrue(imageInfoFile.getNumberOfImages() > 0);
223 
224     }
225 }