All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.io7m.calino.imageproc.awt.internal.CLNImageDecode8 Maven / Gradle / Ivy

/*
 * Copyright © 2021 Mark Raynsford  https://www.io7m.com
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package com.io7m.calino.imageproc.awt.internal;

import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Objects;

/**
 * An 8-bit decoder.
 */

public final class CLNImageDecode8 implements CLNImageDecodeType
{
  private final ByteOrder byteOrder;
  private final int targetComponents;
  private final int pixelSizeBytes;

  /**
   * An 8-bit decoder.
   *
   * @param inByteOrder        The byte order
   * @param inTargetComponents The component count
   */

  public CLNImageDecode8(
    final ByteOrder inByteOrder,
    final int inTargetComponents)
  {
    this.byteOrder =
      Objects.requireNonNull(inByteOrder, "byteOrder");
    this.pixelSizeBytes =
      inTargetComponents;
    this.targetComponents =
      inTargetComponents;
  }

  @Override
  public byte[] execute(
    final BufferedImage image)
  {
    final var imageAccess = new CLNImageAccess(image);
    final var pixel = new double[4];
    final var width = image.getWidth();
    final var height = image.getHeight();
    final var data = new byte[(width * this.pixelSizeBytes) * height];
    final var buffer = ByteBuffer.wrap(data);
    buffer.order(this.byteOrder);

    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        pixel[0] = 0.0;
        pixel[1] = 0.0;
        pixel[2] = 0.0;
        pixel[3] = 1.0;
        imageAccess.pixelAt(x, y, pixel);

        for (int c = 0; c < this.targetComponents; ++c) {
          buffer.put((byte) (pixel[c] * 255.0));
        }
      }
    }

    return data;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy