예제 #1
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
              dstCM.isAlphaPremultiplied(),
              null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);
    inPixels = filterPixels(width, height, inPixels, transformedSpace);
    setRGB(dst, 0, 0, transformedSpace.width, transformedSpace.height, inPixels);

    return dst;
  }
 public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel dstCM) {
   if (dstCM == null) dstCM = src.getColorModel();
   return new BufferedImage(
       dstCM,
       dstCM.createCompatibleWritableRaster(src.getWidth(), src.getHeight()),
       dstCM.isAlphaPremultiplied(),
       null);
 }
예제 #3
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int w = src.getWidth();
    int h = src.getHeight();

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(width, height),
              dstCM.isAlphaPremultiplied(),
              null);
    }

    Graphics2D g = dst.createGraphics();
    g.drawRenderedImage(src, AffineTransform.getTranslateInstance(-x, -y));
    g.dispose();

    return dst;
  }
  /**
   * {@collect.stats} Creates a zeroed destination image with the correct size and number of bands.
   *
   * @param src Source image for the filter operation.
   * @param destCM ColorModel of the destination. If null, the ColorModel of the source will be
   *     used.
   * @return the zeroed-destination image.
   */
  public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM) {
    BufferedImage image;
    if (destCM == null) {
      ColorModel cm = src.getColorModel();
      image =
          new BufferedImage(
              cm,
              src.getRaster().createCompatibleWritableRaster(),
              cm.isAlphaPremultiplied(),
              null);
    } else {
      int w = src.getWidth();
      int h = src.getHeight();
      image =
          new BufferedImage(
              destCM,
              destCM.createCompatibleWritableRaster(w, h),
              destCM.isAlphaPremultiplied(),
              null);
    }

    return image;
  }
  public BufferedImage createCompatibleDestImage(BufferedImage src, ColorModel destCM) {
    Rectangle2D newBounds = getBounds2D(src);

    // Destination image should include (0,0) + positive part
    // of the area bounded by newBounds (in source coordinate system).
    double dstWidth = newBounds.getX() + newBounds.getWidth();
    double dstHeight = newBounds.getY() + newBounds.getHeight();

    if (dstWidth <= 0 || dstHeight <= 0) {
      // awt.251=Transformed width ({0}) and height ({1}) should be
      // greater than 0
      throw new RasterFormatException(
          Messages.getString("awt.251", dstWidth, dstHeight)); // $NON-NLS-1$
    }

    if (destCM != null) {
      return new BufferedImage(
          destCM,
          destCM.createCompatibleWritableRaster((int) dstWidth, (int) dstHeight),
          destCM.isAlphaPremultiplied(),
          null);
    }

    ColorModel cm = src.getColorModel();

    // Interpolation other than NN doesn't make any sense for index color
    if (iType != TYPE_NEAREST_NEIGHBOR && cm instanceof IndexColorModel) {
      return new BufferedImage((int) dstWidth, (int) dstHeight, BufferedImage.TYPE_INT_ARGB);
    }

    // OK, we can get source color model
    return new BufferedImage(
        cm,
        src.getRaster().createCompatibleWritableRaster((int) dstWidth, (int) dstHeight),
        cm.isAlphaPremultiplied(),
        null);
  }
예제 #6
0
  public static void main(String args[]) {
    try {
      ICC_Profile inProfile =
          ICC_Profile.getInstance("/System/Library/ColorSync/Profiles/AdobeRGB1998.icc");
      ICC_Profile outProfile =
          ICC_Profile.getInstance("/Library/ColorSync/Profiles/CIE 1931 D50 Gamma 1.icm");

      Profile cmsOutProfile = new Profile(outProfile);
      Profile cmsInProfile = new Profile(inProfile);

      BufferedImage inputImage = ImageIO.read(new File("/Stuff/Reference/small-q60-adobergb.TIF"));
      ShortInterleavedRaster inputRaster = (ShortInterleavedRaster) inputImage.getTile(0, 0);

      ColorSpace outCS = new ICC_ColorSpace(outProfile);
      ColorModel outCM =
          new ComponentColorModel(outCS, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
      ShortInterleavedRaster outputRaster =
          (ShortInterleavedRaster)
              outCM.createCompatibleWritableRaster(inputImage.getWidth(), inputImage.getHeight());
      BufferedImage outputImage = new BufferedImage(outCM, outputRaster, false, null);

      Transform cmsTransform =
          new Transform(
              cmsInProfile, TYPE_RGB_16, cmsOutProfile, TYPE_RGB_16, INTENT_PERCEPTUAL, 0);

      cmsTransform.doTransform(inputRaster, outputRaster);

      ImageIO.write(outputImage, "TIF", new File("/Stuff/small-q60-CIED65.TIF"));

      cmsTransform.dispose();
      cmsOutProfile.dispose();
      cmsInProfile.dispose();
      // System.out.println("Profile: " + hProfile + ", " + );
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
예제 #7
0
  public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);

    if (dst == null) {
      ColorModel dstCM = src.getColorModel();
      dst =
          new BufferedImage(
              dstCM,
              dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
              dstCM.isAlphaPremultiplied(),
              null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);

    if (interpolation == NEAREST_NEIGHBOUR)
      return filterPixelsNN(dst, width, height, inPixels, transformedSpace);

    int srcWidth = width;
    int srcHeight = height;
    int srcWidth1 = width - 1;
    int srcHeight1 = height - 1;
    int outWidth = transformedSpace.width;
    int outHeight = transformedSpace.height;
    int outX, outY;
    int index = 0;
    int[] outPixels = new int[outWidth];

    outX = transformedSpace.x;
    outY = transformedSpace.y;
    float[] out = new float[2];

    for (int y = 0; y < outHeight; y++) {
      for (int x = 0; x < outWidth; x++) {
        transformInverse(outX + x, outY + y, out);
        int srcX = (int) Math.floor(out[0]);
        int srcY = (int) Math.floor(out[1]);
        float xWeight = out[0] - srcX;
        float yWeight = out[1] - srcY;
        int nw, ne, sw, se;

        if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
          // Easy case, all corners are in the image
          int i = srcWidth * srcY + srcX;
          nw = inPixels[i];
          ne = inPixels[i + 1];
          sw = inPixels[i + srcWidth];
          se = inPixels[i + srcWidth + 1];
        } else {
          // Some of the corners are off the image
          nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight);
          ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight);
          sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight);
          se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight);
        }
        outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
      }
      setRGB(dst, 0, y, transformedSpace.width, 1, outPixels);
    }
    return dst;
  }