Example #1
0
  /**
   * Builds a new BufferedImage that is the difference between the two input images
   *
   * @param ref the reference bitmap
   * @param gen the newly generated bitmap
   * @return the diff bitmap
   */
  public static BufferedImage buildDiffImage(BufferedImage ref, BufferedImage gen) {
    BufferedImage diff =
        new BufferedImage(ref.getWidth(), ref.getHeight(), BufferedImage.TYPE_INT_ARGB);
    WritableRaster refWR = ref.getRaster();
    WritableRaster genWR = gen.getRaster();
    WritableRaster dstWR = diff.getRaster();

    boolean refPre = ref.isAlphaPremultiplied();
    if (!refPre) {
      ColorModel cm = ref.getColorModel();
      cm = GraphicsUtil.coerceData(refWR, cm, true);
      ref = new BufferedImage(cm, refWR, true, null);
    }
    boolean genPre = gen.isAlphaPremultiplied();
    if (!genPre) {
      ColorModel cm = gen.getColorModel();
      cm = GraphicsUtil.coerceData(genWR, cm, true);
      gen = new BufferedImage(cm, genWR, true, null);
    }

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

    int y, i, val;
    int[] refPix = null;
    int[] genPix = null;
    for (y = 0; y < h; y++) {
      refPix = refWR.getPixels(0, y, w, 1, refPix);
      genPix = genWR.getPixels(0, y, w, 1, genPix);
      for (i = 0; i < refPix.length; i++) {
        // val = ((genPix[i] - refPix[i]) * 5) + 128;
        val = ((refPix[i] - genPix[i]) * 10) + 128;
        if ((val & 0xFFFFFF00) != 0) {
          if ((val & 0x80000000) != 0) {
            val = 0;
          } else {
            val = 255;
          }
        }
        genPix[i] = val;
      }
      dstWR.setPixels(0, y, w, 1, genPix);
    }

    if (!genPre) {
      ColorModel cm = gen.getColorModel();
      cm = GraphicsUtil.coerceData(genWR, cm, false);
    }

    if (!refPre) {
      ColorModel cm = ref.getColorModel();
      cm = GraphicsUtil.coerceData(refWR, cm, false);
    }

    return diff;
  }
Example #2
0
 public BufferedImage deepCopy() {
   return new BufferedImage(
       imageBuffer.getColorModel(),
       imageBuffer.copyData(null),
       imageBuffer.isAlphaPremultiplied(),
       null);
 }
Example #3
0
 public void setDimensions(int w, int h) {
   if (image == null) {
     if (compatible != null) {
       // create a compatible image with the new dimensions:
       image =
           new BufferedImage(
               compatible.getColorModel(),
               compatible.getRaster().createCompatibleWritableRaster(w, h),
               compatible.isAlphaPremultiplied(),
               null);
     } else {
       // assume standard format:
       image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
     }
   }
   width = image.getWidth();
   height = image.getHeight();
 }
  private void writeBufferedImageGzipCompression(DataOutput out, BufferedImage image)
      throws IOException {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream);
    DataOutputStream dataOut = new DataOutputStream(gzipStream);

    writeColorModel(dataOut, image.getColorModel());
    writeWritableRaster(dataOut, image.getRaster());
    dataOut.writeBoolean(image.isAlphaPremultiplied());

    dataOut.flush();
    gzipStream.finish();

    byte[] buffer = byteStream.toByteArray();

    out.writeInt(buffer.length);
    out.write(buffer);
    dataOut.close();
  }
  private void writeBufferedImageNoCompression(DataOutput out, BufferedImage image)
      throws IOException {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    DataOutputStream dataOut = new DataOutputStream(byteStream);
    ColorModel colorModel = (ColorModel) image.getColorModel();

    if (colorModel instanceof ComponentColorModel) {
      ComponentColorModel cm = (ComponentColorModel) colorModel;
      int numComponents = cm.getNumComponents();
      int type;
      switch (numComponents) {
        case 3:
          type = BufferedImage.TYPE_INT_RGB;
          break;
        case 4:
          type = BufferedImage.TYPE_INT_ARGB;
          break;
        default:
          throw new SGIORuntimeException(
              "Unsupported ColorModel " + colorModel.getClass().getName());
      }

      BufferedImage tmpBuf = new BufferedImage(image.getWidth(), image.getHeight(), type);
      WritableRaster dstRaster = tmpBuf.getRaster();
      Raster srcRaster = image.getRaster();
      dstRaster.setRect(srcRaster);
      image = tmpBuf;
    }

    writeColorModel(dataOut, image.getColorModel());
    writeWritableRaster(dataOut, image.getRaster());
    dataOut.writeBoolean(image.isAlphaPremultiplied());

    dataOut.close();

    byte[] buffer = byteStream.toByteArray();
    out.writeInt(buffer.length);
    out.write(buffer);
  }
Example #6
0
 public BufferedImage deepCopy(BufferedImage bi) {
   return new BufferedImage(
       bi.getColorModel(), bi.copyData(null), bi.isAlphaPremultiplied(), null);
 }
Example #7
0
 public BufferedImage cloneImage(BufferedImage image) {
   return new BufferedImage(
       image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
 }