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  package org.apache.commons.imaging.formats.pnm;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.apache.commons.imaging.ImageFormat;
23  import org.apache.commons.imaging.ImageFormats;
24  import org.apache.commons.imaging.ImageInfo;
25  import org.apache.commons.imaging.ImagingException;
26  
27  final class PpmFileInfo extends AbstractFileInfo {
28      private final int max;
29      private final float scale;
30      private final int bytesPerSample;
31  
32      PpmFileInfo(final int width, final int height, final boolean rawBits, final int max) throws ImagingException {
33          super(width, height, rawBits);
34          if (max <= 0) {
35              throw new ImagingException("PPM maxVal " + max + " is out of range [1;65535]");
36          }
37          if (max <= 255) {
38              scale = 255f;
39              bytesPerSample = 1;
40          } else if (max <= 65535) {
41              scale = 65535f;
42              bytesPerSample = 2;
43          } else {
44              throw new ImagingException("PPM maxVal " + max + " is out of range [1;65535]");
45          }
46          this.max = max;
47      }
48  
49      @Override
50      public int getBitDepth() {
51          return max;
52      }
53  
54      @Override
55      public ImageInfo.ColorType getColorType() {
56          return ImageInfo.ColorType.RGB;
57      }
58  
59      @Override
60      public ImageFormat getImageType() {
61          return ImageFormats.PPM;
62      }
63  
64      @Override
65      public String getImageTypeDescription() {
66          return "PPM: portable pixmap file format";
67      }
68  
69      @Override
70      public String getMimeType() {
71          return "image/x-portable-pixmap";
72      }
73  
74      @Override
75      public int getNumComponents() {
76          return 3;
77      }
78  
79      @Override
80      public int getRgb(final InputStream is) throws IOException {
81          int red = readSample(is, bytesPerSample);
82          int green = readSample(is, bytesPerSample);
83          int blue = readSample(is, bytesPerSample);
84  
85          red = scaleSample(red, scale, max);
86          green = scaleSample(green, scale, max);
87          blue = scaleSample(blue, scale, max);
88          final int alpha = 0xff;
89  
90          return (0xff & alpha) << 24 | (0xff & red) << 16 | (0xff & green) << 8 | (0xff & blue) << 0;
91      }
92  
93      @Override
94      public int getRgb(final WhiteSpaceReader wsr) throws IOException {
95          int red = Integer.parseInt(wsr.readtoWhiteSpace());
96          int green = Integer.parseInt(wsr.readtoWhiteSpace());
97          int blue = Integer.parseInt(wsr.readtoWhiteSpace());
98  
99          red = scaleSample(red, scale, max);
100         green = scaleSample(green, scale, max);
101         blue = scaleSample(blue, scale, max);
102         final int alpha = 0xff;
103 
104         return (0xff & alpha) << 24 | (0xff & red) << 16 | (0xff & green) << 8 | (0xff & blue) << 0;
105     }
106 
107     @Override
108     public boolean hasAlpha() {
109         return false;
110     }
111 }