You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/07/16 19:11:24 UTC

[GitHub] [commons-imaging] darkma773r commented on a change in pull request #116: [IMAGING-159] Add ImagingParameters interface and BaseParameters (POJO)

darkma773r commented on a change in pull request #116:
URL: https://github.com/apache/commons-imaging/pull/116#discussion_r671470893



##########
File path: src/main/java/org/apache/commons/imaging/ImageFormats.java
##########
@@ -16,40 +16,92 @@
  */
 package org.apache.commons.imaging;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.apache.commons.imaging.formats.bmp.BmpImagingParameters;
+import org.apache.commons.imaging.formats.gif.GifImagingParameters;
+import org.apache.commons.imaging.formats.icns.IcnsImagingParameters;
+import org.apache.commons.imaging.formats.ico.IcoImagingParameters;
+import org.apache.commons.imaging.formats.jpeg.JpegImagingParameters;
+import org.apache.commons.imaging.formats.pcx.PcxImagingParameters;
+import org.apache.commons.imaging.formats.png.PngImagingParameters;
+import org.apache.commons.imaging.formats.pnm.PnmImagingParameters;
+import org.apache.commons.imaging.formats.psd.PsdImagingParameters;
+import org.apache.commons.imaging.formats.rgbe.RgbeImagingParameters;
+import org.apache.commons.imaging.formats.tiff.TiffImagingParameters;
+import org.apache.commons.imaging.formats.wbmp.WbmpImagingParameters;
+import org.apache.commons.imaging.formats.xbm.XbmImagingParameters;
+import org.apache.commons.imaging.formats.xpm.XpmImagingParameters;
+
 /**
  * Enum of known image formats.
  */
 public enum ImageFormats implements ImageFormat {
-    UNKNOWN,
-    BMP,
-    DCX,
-    GIF,
-    ICNS,
-    ICO,
-    JBIG2,
-    JPEG,
-    PAM,
-    PSD,
-    PBM,
-    PGM,
-    PNM,
-    PPM,
-    PCX,
-    PNG,
-    RGBE,
-    TGA,
-    TIFF,
-    WBMP,
-    XBM,
-    XPM;
+    UNKNOWN(ImagingParameters.class),
+    BMP(BmpImagingParameters.class, "bmp", "dib"),
+    DCX(PcxImagingParameters.class, "dcx"),
+    GIF(GifImagingParameters.class, "gif"),
+    ICNS(IcnsImagingParameters.class, "icns"),
+    ICO(IcoImagingParameters.class, "ico"),
+    JBIG2(ImagingParameters.class),
+    JPEG(JpegImagingParameters.class, "jpg", "jpeg"),
+    PAM(PnmImagingParameters.class, "pam"),
+    PSD(PsdImagingParameters.class, "psd"),
+    PBM(PnmImagingParameters.class, "pbm"),
+    PGM(PnmImagingParameters.class, "pgm"),
+    PNM(PnmImagingParameters.class, "pnm"),
+    PPM(PnmImagingParameters.class, "ppm"),
+    PCX(PcxImagingParameters.class, "pcx", "pcc"),
+    PNG(PngImagingParameters.class, "png"),
+    RGBE(RgbeImagingParameters.class, "rgbe"),
+    TGA(ImagingParameters.class),
+    TIFF(TiffImagingParameters.class, "tif", "tiff"),
+    WBMP(WbmpImagingParameters.class, "wbmp"),
+    XBM(XbmImagingParameters.class, "xbm"),
+    XPM(XpmImagingParameters.class, "xpm");
+
+    private static final Logger LOGGER = Logger.getLogger(ImageFormats.class.getName());
+
+    private String[] extensions;
+    private Class<? extends ImagingParameters> parametersClass;
+
+    ImageFormats(Class<? extends ImagingParameters> parametersClass, String ...extensions) {
+        this.extensions = extensions;
+        this.parametersClass = parametersClass;
+    }
 
     @Override
     public String getName() {
         return name();
     }
 
     @Override
-    public String getExtension() {
-        return name();
+    public String[] getExtensions() {
+        return this.extensions.clone();
+    }
+
+    @Override
+    public String getDefaultExtension() {
+        return this.extensions[0];
+    }
+
+    @Override
+    public ImagingParameters createImagingParameters() {
+        ImagingParameters parameters = null;
+        if (this.parametersClass != null) {
+            Constructor<?> ctor;
+            try {
+                ctor = this.parametersClass.getConstructor();
+                parameters = (ImagingParameters) ctor.newInstance();
+            } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+                LOGGER.log(Level.WARNING, "Failed to create imaging parameters: " + e.getMessage(), e);
+                parameters = new ImagingParameters();
+            }
+            parameters.setImageFormat(this);
+        }
+        return parameters;

Review comment:
       Maybe use factory functions instead of reflection here. Ex:
   ```java
   enum ImageFormat {
   
       PNG(PngImagingParameter::new);
   
       private final Supplier<? extends ImagingParameters> factory;
   
       // ....
       public ImagingParameters createImagingParameters() {
           return factory.get();
       }
      
   }
   ```

##########
File path: src/main/java/org/apache/commons/imaging/ImagingParameters.java
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging;
+
+import org.apache.commons.imaging.common.BufferedImageFactory;
+
+/**
+ * Imaging parameters.
+ *
+ * <p>Contains parameters that are common to all formats. Implementations must include
+ * the specific parameters for each image format.</p>
+ *
+ * @since 1.0-alpha3
+ */
+public class ImagingParameters {
+
+    /**
+     * Whether to throw an exception when any issue occurs during reading
+     * or writing a file format. Default is {@code false}.
+     */
+    private boolean strict = false;
+
+    /**
+     * An optional file name, used for the description of input streams
+     * where a file name would be hard (or not possible) to be identified.
+     * Default is {@code null}.
+     */
+    private String fileName = null;
+
+    /**
+     * Factory to create {@code BufferedImage}s. Default is {@code null}.
+     */
+    private BufferedImageFactory bufferedImageFactory = null;
+
+    /**
+     * Image format used in write operations to indicate desired image format.
+     * Default is {@code null}.
+     *
+     * <p>Valid values: Any format defined in ImageFormat, such as
+     * ImageFormat.IMAGE_FORMAT_PNG.</p>
+     *
+     * @see org.apache.commons.imaging.ImageFormats
+     */
+    private ImageFormat imageFormat;

Review comment:
       Should this be final? I'm wondering since otherwise it would be possible to create an instance with a class name that doesn't match the image format type. Ex:
   ```java
   PngImagingParameters params = new PngImagingParameters();
   params.setImageFormat(ImageFormat.BMP); // doesn't seem like a good thing
   ```
   
   Perhaps something like this would work?
   ```java
   public abstract class ImagingParameters {
       private final ImageFormat imageFormat;
   
       protected ImagingParameters(final ImageFormat imageFormat) {
           this.imageFormat = imageFormat;
       }
   }
   
   public class PngImageParameters {
       public PngImageParameters() {
           super(ImageFormat.PNG);
       }
   }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org