public static PaintContext getContext(
     BufferedImage bufImg, AffineTransform xform, RenderingHints hints, Rectangle devBounds) {
   WritableRaster raster = bufImg.getRaster();
   ColorModel cm = bufImg.getColorModel();
   int maxw = devBounds.width;
   Object val = hints.get(hints.KEY_INTERPOLATION);
   boolean filter =
       (val == null
           ? (hints.get(hints.KEY_RENDERING) == hints.VALUE_RENDER_QUALITY)
           : (val != hints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
   if (raster instanceof IntegerInterleavedRaster && (!filter || isFilterableDCM(cm))) {
     IntegerInterleavedRaster iir = (IntegerInterleavedRaster) raster;
     if (iir.getNumDataElements() == 1 && iir.getPixelStride() == 1) {
       return new Int(iir, cm, xform, maxw, filter);
     }
   } else if (raster instanceof ByteInterleavedRaster) {
     ByteInterleavedRaster bir = (ByteInterleavedRaster) raster;
     if (bir.getNumDataElements() == 1 && bir.getPixelStride() == 1) {
       if (filter) {
         if (isFilterableICM(cm)) {
           return new ByteFilter(bir, cm, xform, maxw);
         }
       } else {
         return new Byte(bir, cm, xform, maxw);
       }
     }
   }
   return new Any(raster, cm, xform, maxw, filter);
 }
Ejemplo n.º 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;
  }
Ejemplo n.º 3
0
  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");
        }
      }
    }
  }
Ejemplo n.º 4
0
  public static void checkEquals(WritableRaster imgA, MultiSpectral imgB, float tol) {

    if (imgA.getNumBands() != imgB.getNumBands()) {
      throw new RuntimeException("Number of bands not equals");
    }

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

      byte dataA[] = raster.getDataStorage();
      int strideA = raster.getScanlineStride();
      int offsetA = raster.getDataOffset(0) - raster.getPixelStride() + 1;

      // handle a special case where the RGB conversion is screwed
      for (int y = 0; y < imgA.getHeight(); y++) {
        int indexA = offsetA + strideA * y;

        for (int x = 0; x < imgA.getWidth(); x++) {
          for (int k = 0; k < imgB.getNumBands(); k++) {
            int valueA = dataA[indexA++] & 0xFF;
            double valueB = GeneralizedImageOps.get(imgB.getBand(k), x, y);
            if (Math.abs(valueA - valueB) > tol)
              throw new RuntimeException("Images are not equal: A = " + valueA + " B = " + valueB);
          }
        }
      }

    } else if (imgA instanceof IntegerInterleavedRaster) {
      IntegerInterleavedRaster raster = (IntegerInterleavedRaster) imgA;

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

      // handle a special case where the RGB conversion is screwed
      for (int y = 0; y < imgA.getHeight(); y++) {
        int indexA = offsetA + strideA * y;

        for (int x = 0; x < imgA.getWidth(); x++) {
          int valueA = dataA[indexA++];
          if (imgB.getNumBands() == 4) {
            int found0 = (valueA >> 24) & 0xFF;
            int found1 = (valueA >> 16) & 0xFF;
            int found2 = (valueA >> 8) & 0xFF;
            int found3 = valueA & 0xFF;

            double expected0 = GeneralizedImageOps.get(imgB.getBand(0), x, y);
            double expected1 = GeneralizedImageOps.get(imgB.getBand(1), x, y);
            double expected2 = GeneralizedImageOps.get(imgB.getBand(2), x, y);
            double expected3 = GeneralizedImageOps.get(imgB.getBand(3), x, y);

            if (Math.abs(found0 - expected0) > tol)
              throw new RuntimeException("Images are not equal");
            if (Math.abs(found1 - expected1) > tol)
              throw new RuntimeException("Images are not equal");
            if (Math.abs(found2 - expected2) > tol)
              throw new RuntimeException("Images are not equal");
            if (Math.abs(found3 - expected3) > tol)
              throw new RuntimeException("Images are not equal");
          } else if (imgB.getNumBands() == 3) {
            int found0 = (valueA >> 16) & 0xFF;
            int found1 = (valueA >> 8) & 0xFF;
            int found2 = valueA & 0xFF;

            double expected0 = GeneralizedImageOps.get(imgB.getBand(0), x, y);
            double expected1 = GeneralizedImageOps.get(imgB.getBand(1), x, y);
            double expected2 = GeneralizedImageOps.get(imgB.getBand(2), x, y);

            if (Math.abs(found0 - expected0) > tol)
              throw new RuntimeException("Images are not equal");
            if (Math.abs(found1 - expected1) > tol)
              throw new RuntimeException("Images are not equal");
            if (Math.abs(found2 - expected2) > tol)
              throw new RuntimeException("Images are not equal");
          } else {
            throw new RuntimeException("Unexpectd number of bands");
          }
        }
      }
    } else {
      throw new RuntimeException("Add support for raster type " + imgA.getClass().getSimpleName());
    }
  }