/*
 * Copyright 2009 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.zxing.client.j2se;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.ByteMatrix;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.util.Hashtable;

/**
 * Writes a {@link BitMatrix} or {@link ByteMatrix} to {@link BufferedImage}, file or stream.
 * Provided here instead of core since it depends on Java SE libraries.
 *
 * @author Sean Owen
 */
public final class MatrixToImageWriter {

  private static final int BLACK = 0xFF000000;
  private static final int WHITE = 0xFFFFFFFF;

  private MatrixToImageWriter() {}

  public static BufferedImage toBufferedImage(BitMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
      }
    }
    return image;
  }

  public static BufferedImage toBufferedImage(ByteMatrix matrix) {
    int width = matrix.getWidth();
    int height = matrix.getHeight();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {
        image.setRGB(x, y, matrix.get(x, y) == 0 ? BLACK : WHITE);
      }
    }
    return image;
  }

  public static void writeToFile(BitMatrix matrix, String format, File file)
          throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    ImageIO.write(image, format, file);
  }

  public static void writeToFile(ByteMatrix matrix, String format, File file)
          throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    ImageIO.write(image, format, file);
  }

  public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
          throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    ImageIO.write(image, format, stream);
  }

  public static void writeToStream(ByteMatrix matrix, String format, OutputStream stream)
          throws IOException {
    BufferedImage image = toBufferedImage(matrix);
    ImageIO.write(image, format, stream);
  }

}

@@ -635,90 +635,8 @@
 e;%0A%0A
-import com.google.zxing.BarcodeFormat;%0Aimport com.google.zxing.MultiFormatWriter;%0A
 impo
@@ -865,36 +865,8 @@
 age;
-%0Aimport java.util.Hashtable;
 %0A%0A/*
@@ -944,16 +944,19 @@
 dImage%7D,
+%0A *
  file or
@@ -963,19 +963,16 @@
  stream.
-%0A *
  Provide
@@ -1013,16 +1013,19 @@
 pends on
+%0A *
  Java SE
@@ -1237,16 +1237,160 @@
 r() %7B%7D%0A%0A
+  /**%0A   * Renders a %7B@link BitMatrix%7D as an image, where %22false%22 bits are rendered%0A   * as white, and %22true%22 bits are rendered as black.%0A   */%0A
   public
@@ -1778,32 +1778,296 @@
 urn image;%0A  %7D%0A%0A
+  /**%0A   * Renders a %7B@link ByteMatrix%7D as an image, as a%0A   * %7B@link BufferedImage%7D. The byte values are construed as (unsigned)%0A   * luminance values, in theory.%0A   * However, anything but 0 will be rendered as white, and 0 will be%0A   * rendered as black.%0A   */%0A
   public static 
@@ -2457,24 +2457,122 @@
 image;%0A  %7D%0A%0A
+  /**%0A   * Writes a %7B@link BitMatrix%7D to a file.%0A   *%0A   * @see #toBufferedImage(BitMatrix)%0A   */%0A
   public sta
@@ -2755,32 +2755,132 @@
 at, file);%0A  %7D%0A%0A
+  /**%0A   * Writes a %7B@link ByteMatrix%7D to a file.%0A   *%0A   * @see #toBufferedImage(ByteMatrix)%0A   */%0A
   public static 
@@ -3060,32 +3060,132 @@
 at, file);%0A  %7D%0A%0A
+  /**%0A   * Writes a %7B@link BitMatrix%7D to a stream.%0A   *%0A   * @see #toBufferedImage(BitMatrix)%0A   */%0A
   public static 
@@ -3386,16 +3386,118 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Writes a %7B@link ByteMatrix%7D to a stream.%0A   *%0A   * @see #toBufferedImage(ByteMatrix)%0A   */%0A
   public

Fix a typo and add better javadoc