/**
   * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8
   *
   * @param imgA BufferedImage
   * @param imgB ImageUInt8
   */
  public static void checkEquals(BufferedImage imgA, ImageInt16 imgB) {

    if (imgA.getRaster() instanceof ByteInterleavedRaster
        && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) {
      ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        int strideA = raster.getScanlineStride();
        int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1;

        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            int valB = imgB.get(j, i);
            int valA = raster.getDataStorage()[offsetA + i * strideA + j];
            if (!imgB.getTypeInfo().isSigned()) valA &= 0xFFFF;

            if (valA != valB)
              throw new RuntimeException("Images are not equal: " + valA + " " + valB);
          }
        }
        return;
      }
    } else if (imgA.getRaster() instanceof ShortInterleavedRaster) {
      ShortInterleavedRaster raster = (ShortInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        int strideA = raster.getScanlineStride();
        int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1;

        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            int valB = imgB.get(j, i);
            int valA = raster.getDataStorage()[offsetA + i * strideA + j];
            if (!imgB.getTypeInfo().isSigned()) valA &= 0xFFFF;

            if (valA != valB)
              throw new RuntimeException("Images are not equal: " + valA + " " + valB);
          }
        }
      }
    } else {
      for (int y = 0; y < imgA.getHeight(); y++) {
        for (int x = 0; x < imgA.getWidth(); x++) {
          int rgb = imgA.getRGB(x, y);

          int gray = ((((rgb >>> 16) & 0xFF) + ((rgb >>> 8) & 0xFF) + (rgb & 0xFF)) / 3);
          int grayB = imgB.get(x, y);
          if (!imgB.getTypeInfo().isSigned()) gray &= 0xFFFF;

          if (Math.abs(gray - grayB) != 0) {
            throw new RuntimeException(
                "images are not equal: (" + x + " , " + y + ") A = " + gray + " B = " + grayB);
          }
        }
      }
    }
  }
Exemple #2
0
  private static ByteInterleavedRaster normalizeRaster(ByteInterleavedRaster raster) {
    boolean reallocBuffer = false;
    int scanLineStride = raster.getScanlineStride();
    int dataOffsets[] = raster.getDataOffsets();
    for (int i = 0; i < dataOffsets.length; i++)
      if (dataOffsets[i] != i) {
        reallocBuffer = true;
        break;
      }
    int bands = raster.getNumBands();
    if (!reallocBuffer
        && (raster.getPixelStride() != bands || scanLineStride != raster.getWidth() * bands))
      reallocBuffer = true;
    if (reallocBuffer) {
      PixelInterleavedSampleModel sm = (PixelInterleavedSampleModel) raster.getSampleModel();

      PixelInterleavedSampleModel newSM =
          new PixelInterleavedSampleModel(
              sm.getDataType(),
              raster.getWidth(),
              raster.getHeight(),
              bands,
              bands * raster.getWidth(),
              bands == 1 ? new int[] {0} : new int[] {0, 1, 2});

      ByteInterleavedRaster newRaster =
          new ByteInterleavedRaster(newSM, new Point(raster.getMinX(), raster.getMinY()));
      Functions.copyData(newRaster, raster);
      raster = newRaster;
    }
    return raster;
  }
  /**
   * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8
   *
   * @param imgA BufferedImage
   * @param imgB ImageUInt8
   */
  public static void checkEquals(BufferedImage imgA, ImageFloat32 imgB, float tol) {

    if (imgA.getRaster() instanceof ByteInterleavedRaster
        && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) {
      ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        int strideA = raster.getScanlineStride();
        int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1;

        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            float valB = imgB.get(j, i);
            int valA = raster.getDataStorage()[offsetA + i * strideA + j];
            valA &= 0xFF;

            if (Math.abs(valA - valB) > tol)
              throw new RuntimeException("Images are not equal: A = " + valA + " B = " + valB);
          }
        }
        return;
      }
    }

    for (int y = 0; y < imgA.getHeight(); y++) {
      for (int x = 0; x < imgA.getWidth(); x++) {
        int rgb = imgA.getRGB(x, y);

        float gray = (((rgb >>> 16) & 0xFF) + ((rgb >>> 8) & 0xFF) + (rgb & 0xFF)) / 3.0f;
        float grayB = imgB.get(x, y);

        if (Math.abs(gray - grayB) > tol) {
          throw new RuntimeException("images are not equal: A = " + gray + " B = " + grayB);
        }
      }
    }
  }
  private static void renderBinary(
      GrayU8 binaryImage, boolean invert, ByteInterleavedRaster raster) {
    int rasterIndex = 0;
    byte data[] = raster.getDataStorage();

    int w = binaryImage.getWidth();
    int h = binaryImage.getHeight();

    int numBands = raster.getNumBands();
    if (numBands == 1) {
      if (invert) {
        for (int y = 0; y < h; y++) {
          int indexSrc = binaryImage.startIndex + y * binaryImage.stride;
          for (int x = 0; x < w; x++) {
            data[rasterIndex++] = (byte) ((1 - binaryImage.data[indexSrc++]) * 255);
          }
        }
      } else {
        for (int y = 0; y < h; y++) {
          int indexSrc = binaryImage.startIndex + y * binaryImage.stride;
          for (int x = 0; x < w; x++) {
            data[rasterIndex++] = (byte) (binaryImage.data[indexSrc++] * 255);
          }
        }
      }
    } else {
      if (invert) {
        for (int y = 0; y < h; y++) {
          int indexSrc = binaryImage.startIndex + y * binaryImage.stride;
          for (int x = 0; x < w; x++) {
            byte val = (byte) ((1 - binaryImage.data[indexSrc++]) * 255);
            for (int i = 0; i < numBands; i++) {
              data[rasterIndex++] = val;
            }
          }
        }
      } else {
        for (int y = 0; y < h; y++) {
          int indexSrc = binaryImage.startIndex + y * binaryImage.stride;
          for (int x = 0; x < w; x++) {
            byte val = (byte) (binaryImage.data[indexSrc++] * 255);
            for (int i = 0; i < numBands; i++) {
              data[rasterIndex++] = val;
            }
          }
        }
      }
    }
  }
Exemple #5
0
 public void doTransform(ByteInterleavedRaster input, ByteInterleavedRaster output) {
   if (cmsTransform != null) {
     ByteInterleavedRaster ri = normalizeRaster(input);
     ByteInterleavedRaster ro;
     int outBands = output.getNumBands();
     if (!input.getBounds().equals(output.getBounds())) {
       int[] offsets = outBands == 1 ? new int[] {0} : new int[] {0, 1, 2};
       SampleModel sm =
           new PixelInterleavedSampleModel(
               DataBuffer.TYPE_BYTE,
               ri.getWidth(),
               ri.getHeight(),
               outBands,
               outBands * ri.getWidth(),
               offsets);
       ro = new ByteInterleavedRaster(sm, new Point(ri.getMinX(), ri.getMinY()));
     } else ro = normalizeRaster(output);
     int pixels =
         outBands == 1 ? ro.getDataStorage().length : ro.getDataStorage().length / outBands;
     cmsDoTransform(cmsTransform.handle, ri.getDataStorage(), ro.getDataStorage(), pixels);
     if (ro != output) Functions.copyData(output, ro);
   }
 }
  /**
   * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8
   *
   * @param imgA BufferedImage
   * @param imgB ImageUInt8
   */
  public static void checkEquals(BufferedImage imgA, InterleavedU8 imgB) {

    if (imgA.getRaster() instanceof ByteInterleavedRaster) {
      ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            int valB = imgB.getBand(j, i, 0);
            int valA = raster.getDataStorage()[i * imgA.getWidth() + j] & 0xFF;

            if (valA != valB) throw new RuntimeException("Images are not equal");
          }
        }
        return;
      }
    }

    for (int y = 0; y < imgA.getHeight(); y++) {
      for (int x = 0; x < imgA.getWidth(); x++) {
        int rgb = imgA.getRGB(x, y);

        int r = (rgb >>> 16) & 0xFF;
        int g = (rgb >>> 8) & 0xFF;
        int b = rgb & 0xFF;

        if (Math.abs(b - imgB.getBand(x, y, 0) & 0xFF) != 0)
          throw new RuntimeException("images are not equal: ");
        if (Math.abs(g - imgB.getBand(x, y, 1) & 0xFF) != 0)
          throw new RuntimeException("images are not equal: ");
        if (Math.abs(r - imgB.getBand(x, y, 2) & 0xFF) != 0)
          throw new RuntimeException("images are not equal: ");
      }
    }
  }
  public static void checkEquals(
      BufferedImage imgA, MultiSpectral imgB, boolean boofcvBandOrder, float tol) {

    if (imgA.getRaster() instanceof ByteInterleavedRaster
        && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) {
      ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        GImageSingleBand band = FactoryGImageSingleBand.wrap(imgB.getBand(0));

        int strideA = raster.getScanlineStride();
        int offsetA = raster.getDataOffset(0) - raster.getPixelStride() + 1;

        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            double valB = band.get(j, i).doubleValue();
            int valA = raster.getDataStorage()[offsetA + i * strideA + j];
            valA &= 0xFF;

            if (Math.abs(valA - valB) > tol)
              throw new RuntimeException("Images are not equal: A = " + valA + " B = " + valB);
          }
        }
        return;
      }
    }

    int bandOrder[];

    if (boofcvBandOrder) {
      if (imgB.getNumBands() == 4) {
        bandOrder = new int[] {1, 2, 3, 0};
      } else {
        bandOrder = new int[] {0, 1, 2};
      }
    } else {
      if (imgA.getType() == BufferedImage.TYPE_INT_RGB) {
        bandOrder = new int[] {0, 1, 2};
      } else if (imgA.getType() == BufferedImage.TYPE_INT_BGR
          || imgA.getType() == BufferedImage.TYPE_3BYTE_BGR) {
        bandOrder = new int[] {2, 1, 0};
      } else if (imgA.getType() == BufferedImage.TYPE_4BYTE_ABGR) {
        bandOrder = new int[] {0, 3, 2, 1};
      } else if (imgA.getType() == BufferedImage.TYPE_INT_ARGB) {
        bandOrder = new int[] {0, 1, 2, 3};
      } else {
        bandOrder = new int[] {0, 1, 2};
      }
    }

    int expected[] = new int[4];

    for (int y = 0; y < imgA.getHeight(); y++) {
      for (int x = 0; x < imgA.getWidth(); x++) {
        // getRGB() automatically converts the band order to ARGB
        int rgb = imgA.getRGB(x, y);

        expected[0] = ((rgb >>> 24) & 0xFF); // alpha
        expected[1] = ((rgb >>> 16) & 0xFF); // red
        expected[2] = ((rgb >>> 8) & 0xFF); // green
        expected[3] = (rgb & 0xFF); // blue

        if (imgB.getNumBands() == 4) {

          for (int i = 0; i < 4; i++) {
            double found = GeneralizedImageOps.get(imgB.getBand(bandOrder[i]), x, y);
            if (Math.abs(Math.exp(expected[i] - found)) > tol) {
              for (int j = 0; j < 4; j++) {
                System.out.println(
                    expected[j] + " " + GeneralizedImageOps.get(imgB.getBand(bandOrder[j]), x, y));
              }
              throw new RuntimeException(
                  "Images are not equal: band - " + i + " type " + imgA.getType());
            }
          }

        } else if (imgB.getNumBands() == 3) {

          for (int i = 0; i < 3; i++) {
            double found = GeneralizedImageOps.get(imgB.getBand(bandOrder[i]), x, y);
            if (Math.abs(expected[i + 1] - found) > tol) {
              for (int j = 0; j < 3; j++) {
                System.out.println(
                    expected[j + 1]
                        + " "
                        + GeneralizedImageOps.get(imgB.getBand(bandOrder[j]), x, y));
              }
              throw new RuntimeException(
                  "Images are not equal: band - " + i + " type " + imgA.getType());
            }
          }

        } else {
          throw new RuntimeException("Unexpectd number of bands");
        }
      }
    }
  }