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.bmp;
18  
19  import java.awt.image.BufferedImage;
20  import java.io.ByteArrayOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.commons.imaging.common.AbstractBinaryOutputStream;
24  
25  final class BmpWriterRgb implements BmpWriter {
26      // private final boolean alpha;
27      //
28      // public BmpWriterRgb(boolean alpha)
29      // {
30      // this.alpha = alpha;
31      // }
32  
33      @Override
34      public int getBitsPerPixel() {
35          // return alpha ? 32 : 24;
36          return 24;
37      }
38  
39      @Override
40      public byte[] getImageData(final BufferedImage src) {
41          final int width = src.getWidth();
42          final int height = src.getHeight();
43  
44          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
45          // BinaryOutputStream bos = new BinaryOutputStream(baos,
46          // BYTE_ORDER_Network);
47  
48          int bytecount = 0;
49          for (int y = height - 1; y >= 0; y--) {
50              // for (int y = 0; y < height; y++)
51              for (int x = 0; x < width; x++) {
52                  final int argb = src.getRGB(x, y);
53                  final int rgb = 0xffffff & argb;
54  
55                  final int red = 0xff & rgb >> 16;
56                  final int green = 0xff & rgb >> 8;
57                  final int blue = 0xff & rgb >> 0;
58  
59                  baos.write(blue);
60                  baos.write(green);
61                  baos.write(red);
62                  bytecount += 3;
63              }
64              while (bytecount % 4 != 0) {
65                  baos.write(0);
66                  bytecount++;
67              }
68          }
69  
70          return baos.toByteArray();
71      }
72  
73      @Override
74      public int getPaletteSize() {
75          return 0;
76      }
77  
78      @Override
79      public void writePalette(final AbstractBinaryOutputStream bos) throws IOException {
80          // no palette
81      }
82  }