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;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.stream.Stream;
25  
26  import org.apache.commons.io.FilenameUtils;
27  import org.junit.jupiter.params.ParameterizedTest;
28  import org.junit.jupiter.params.provider.MethodSource;
29  
30  public class ImagingGuessFormatTest extends AbstractImagingTest {
31  
32      public static final String BMP_IMAGE_FILE = "bmp\\1\\Oregon Scientific DS6639 - DSC_0307 - small.bmp";
33      public static final String PNG_IMAGE_FILE = "png\\1\\Oregon Scientific DS6639 - DSC_0307 - small.png";
34      public static final String GIF_IMAGE_FILE = "gif\\single\\1\\Oregon Scientific DS6639 - DSC_0307 - small.gif";
35      public static final String ICNS_IMAGE_FILE = "icns\\1\\poedit48x48.icns";
36      public static final String ICO_IMAGE_FILE = "ico\\1\\Oregon Scientific DS6639 - DSC_0307 - small.ico";
37      public static final String TIFF_IMAGE_FILE = "tiff\\1\\Oregon Scientific DS6639 - DSC_0307 - small.tif";
38      public static final String JPEG_IMAGE_FILE = "jpg\\1\\Oregon Scientific DS6639 - DSC_0307 - small.jpg";
39      public static final String PSD_IMAGE_FILE = "psd\\1\\Oregon Scientific DS6639 - DSC_0307 - small.psd";
40      public static final String PBM_IMAGE_FILE = "pbm\\1\\Oregon Scientific DS6639 - DSC_0307 - small.pbm";
41      public static final String PGM_IMAGE_FILE = "pbm\\1\\Oregon Scientific DS6639 - DSC_0307 - small.pgm";
42      public static final String PPM_IMAGE_FILE = "pbm\\1\\Oregon Scientific DS6639 - DSC_0307 - small.ppm";
43      public static final String TGA_IMAGE_FILE = "tga\\1\\Oregon Scientific DS6639 - DSC_0307 - small.tga";
44      public static final String UNKNOWN_IMAGE_FILE = "info.txt";
45  
46      public static Stream<Object[]> data() {
47          return Arrays.asList(new Object[] { ImageFormats.PNG, PNG_IMAGE_FILE }, new Object[] { ImageFormats.GIF, GIF_IMAGE_FILE },
48                  new Object[] { ImageFormats.ICNS, ICNS_IMAGE_FILE },
49                  // TODO(cmchen): add ability to sniff ICOs if possible.
50                  // new Object[] { ImageFormat.IMAGE_FORMAT_ICO, ICO_IMAGE_FILE },
51                  new Object[] { ImageFormats.TIFF, TIFF_IMAGE_FILE }, new Object[] { ImageFormats.JPEG, JPEG_IMAGE_FILE },
52                  new Object[] { ImageFormats.BMP, BMP_IMAGE_FILE }, new Object[] { ImageFormats.PSD, PSD_IMAGE_FILE },
53                  new Object[] { ImageFormats.PBM, PBM_IMAGE_FILE }, new Object[] { ImageFormats.PGM, PGM_IMAGE_FILE },
54                  new Object[] { ImageFormats.PPM, PPM_IMAGE_FILE },
55                  // TODO(cmchen): add ability to sniff TGAs if possible.
56                  // new Object[] { ImageFormat.IMAGE_FORMAT_TGA, TGA_IMAGE_FILE },
57                  // TODO(cmchen): Add test images for these formats.
58                  // new Object[] { ImageFormat.IMAGE_FORMAT_PNM, PNM_IMAGE_FILE },
59                  // new Object[] { ImageFormat.IMAGE_FORMAT_JBIG2, JBIG2_IMAGE_FILE },
60                  new Object[] { ImageFormats.UNKNOWN, UNKNOWN_IMAGE_FILE }).stream();
61      }
62  
63      @ParameterizedTest
64      @MethodSource("data")
65      public void testGuessFormat(final ImageFormats expectedFormat, final String pathToFile) throws Exception {
66          final String imagePath = FilenameUtils.separatorsToSystem(pathToFile);
67          final File imageFile = new File(ImagingTestConstants.TEST_IMAGE_FOLDER, imagePath);
68  
69          final ImageFormat guessedFormat = Imaging.guessFormat(imageFile);
70          assertEquals(expectedFormat, guessedFormat);
71      }
72  
73  }