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.awt.image.BufferedImage;
20  import java.io.IOException;
21  import java.io.OutputStream;
22  import java.nio.charset.StandardCharsets;
23  
24  import org.apache.commons.imaging.ImagingException;
25  
26  final class PpmWriter implements PnmWriter {
27  
28      private final boolean rawbits;
29  
30      PpmWriter(final boolean rawbits) {
31          this.rawbits = rawbits;
32      }
33  
34      @Override
35      public void writeImage(final BufferedImage src, final OutputStream os, final PnmImagingParameters params) throws ImagingException, IOException {
36          // System.out.println
37          // (b1 == 0x50 && b2 == 0x36)
38          os.write(0x50);
39          os.write(rawbits ? 0x36 : 0x33);
40          os.write(PnmConstants.PNM_SEPARATOR);
41  
42          final int width = src.getWidth();
43          final int height = src.getHeight();
44  
45          os.write(Integer.toString(width).getBytes(StandardCharsets.US_ASCII));
46          os.write(PnmConstants.PNM_SEPARATOR);
47  
48          os.write(Integer.toString(height).getBytes(StandardCharsets.US_ASCII));
49          os.write(PnmConstants.PNM_SEPARATOR);
50  
51          os.write(Integer.toString(255).getBytes(StandardCharsets.US_ASCII)); // max component value
52          os.write(PnmConstants.PNM_NEWLINE);
53  
54          for (int y = 0; y < height; y++) {
55              for (int x = 0; x < width; x++) {
56                  final int argb = src.getRGB(x, y);
57                  final int red = 0xff & argb >> 16;
58                  final int green = 0xff & argb >> 8;
59                  final int blue = 0xff & argb >> 0;
60  
61                  if (rawbits) {
62                      os.write((byte) red);
63                      os.write((byte) green);
64                      os.write((byte) blue);
65                  } else {
66                      os.write(Integer.toString(red).getBytes(StandardCharsets.US_ASCII)); // max component
67                      // value
68                      os.write(PnmConstants.PNM_SEPARATOR);
69                      os.write(Integer.toString(green).getBytes(StandardCharsets.US_ASCII)); // max
70                      // component
71                      // value
72                      os.write(PnmConstants.PNM_SEPARATOR);
73                      os.write(Integer.toString(blue).getBytes(StandardCharsets.US_ASCII)); // max component
74                      // value
75                      os.write(PnmConstants.PNM_SEPARATOR);
76                  }
77              }
78          }
79      }
80  }