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 PbmFileInfo extends AbstractFileInfo {
28      private int bitCache;
29      private int bitsInCache;
30  
31      PbmFileInfo(final int width, final int height, final boolean rawbits) {
32          super(width, height, rawbits);
33      }
34  
35      @Override
36      public int getBitDepth() {
37          return 1;
38      }
39  
40      @Override
41      public ImageInfo.ColorType getColorType() {
42          return ImageInfo.ColorType.BW;
43      }
44  
45      @Override
46      public ImageFormat getImageType() {
47          return ImageFormats.PBM;
48      }
49  
50      @Override
51      public String getImageTypeDescription() {
52          return "PBM: portable bitmap fileformat";
53      }
54  
55      @Override
56      public String getMimeType() {
57          return "image/x-portable-bitmap";
58      }
59  
60      @Override
61      public int getNumComponents() {
62          return 1;
63      }
64  
65      @Override
66      public int getRgb(final InputStream is) throws IOException {
67          if (bitsInCache < 1) {
68              final int bits = is.read();
69              if (bits < 0) {
70                  throw new ImagingException("PBM: Unexpected EOF");
71              }
72              bitCache = 0xff & bits;
73              bitsInCache += 8;
74          }
75  
76          final int bit = 0x1 & bitCache >> 7;
77          bitCache <<= 1;
78          bitsInCache--;
79  
80          if (bit == 0) {
81              return 0xffffffff;
82          }
83          if (bit == 1) {
84              return 0xff000000;
85          }
86          throw new ImagingException("PBM: bad bit: " + bit);
87      }
88  
89      @Override
90      public int getRgb(final WhiteSpaceReader wsr) throws IOException {
91          final int bit = Integer.parseInt(wsr.readtoWhiteSpace());
92          if (bit == 0) {
93              return 0xff000000;
94          }
95          if (bit == 1) {
96              return 0xffffffff;
97          }
98          throw new ImagingException("PBM: bad bit: " + bit);
99      }
100 
101     @Override
102     public boolean hasAlpha() {
103         return false;
104     }
105 
106     @Override
107     protected void newline() {
108         bitCache = 0;
109         bitsInCache = 0;
110     }
111 
112 }