View Javadoc
1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *       http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   *  under the License.
14   */
15  
16  package org.apache.commons.imaging.formats.png;
17  
18  import java.util.Collections;
19  import java.util.List;
20  
21  import org.apache.commons.imaging.common.XmpImagingParameters;
22  
23  /**
24   * PNG format parameters.
25   *
26   * @since 1.0-alpha3
27   */
28  public class PngImagingParameters extends XmpImagingParameters<PngImagingParameters> {
29  
30      public static final byte DEFAULT_BIT_DEPTH = 8;
31  
32      /**
33       * Bit depth. Default value is {@literal 8}.
34       */
35      private byte bitDepth = DEFAULT_BIT_DEPTH;
36  
37      private boolean forceIndexedColor;
38  
39      private boolean forceTrueColor;
40  
41      private boolean predictorEnabled;
42  
43      /**
44       * Used in write operations to indicate the Physical Scale - sCAL.
45       *
46       * <p>
47       * Valid values: PhysicalScale
48       * </p>
49       *
50       * @see org.apache.commons.imaging.formats.png.PhysicalScale
51       */
52      private PhysicalScale physicalScale;
53  
54      /**
55       * <p>
56       * Only used when writing PNG images.
57       * </p>
58       *
59       * <p>
60       * Valid values: a list of WriteTexts.
61       * </p>
62       */
63      private List<? extends AbstractPngText> textChunks;
64  
65      public byte getBitDepth() {
66          return bitDepth;
67      }
68  
69      public PhysicalScale getPhysicalScale() {
70          return physicalScale;
71      }
72  
73      public List<? extends AbstractPngText> getTextChunks() {
74          return textChunks != null ? Collections.unmodifiableList(textChunks) : null;
75      }
76  
77      public boolean isForceIndexedColor() {
78          return forceIndexedColor;
79      }
80  
81      public boolean isForceTrueColor() {
82          return forceTrueColor;
83      }
84  
85      /**
86       * Indicates that the PNG write operation should enable the predictor.
87       *
88       * @return true if the predictor is enabled; otherwise, false.
89       */
90      public boolean isPredictorEnabled() {
91          return predictorEnabled;
92      }
93  
94      public PngImagingParameters setBitDepth(final byte bitDepth) {
95          this.bitDepth = bitDepth;
96          return asThis();
97      }
98  
99      public PngImagingParameters setForceIndexedColor(final boolean forceIndexedColor) {
100         this.forceIndexedColor = forceIndexedColor;
101         return asThis();
102     }
103 
104     public PngImagingParameters setForceTrueColor(final boolean forceTrueColor) {
105         this.forceTrueColor = forceTrueColor;
106         return asThis();
107     }
108 
109     public PngImagingParameters setPhysicalScale(final PhysicalScale physicalScale) {
110         this.physicalScale = physicalScale;
111         return asThis();
112     }
113 
114     /**
115      * Sets the enabled status of the predictor. When performing data compression on an image, a PNG predictor often results in a reduced file size. Predictors
116      * are particularly effective on photographic images, but may also work on graphics. The specification of a predictor may result in an increased processing
117      * time when writing an image, but will not affect the time required to read an image.
118      *
119      * @param predictorEnabled true if a predictor is enabled; otherwise, false.
120      * @return this
121      */
122     public PngImagingParameters setPredictorEnabled(final boolean predictorEnabled) {
123         this.predictorEnabled = predictorEnabled;
124         return asThis();
125     }
126 
127     public PngImagingParameters setTextChunks(final List<? extends AbstractPngText> textChunks) {
128         this.textChunks = Collections.unmodifiableList(textChunks);
129         return asThis();
130     }
131 }