public void testCreateIndexed() {
    byte[] redLUT = new byte[] {1, 10};
    byte[] greenLUT = new byte[] {2, 20};
    byte[] blueLUT = new byte[] {3, 30};
    byte[] alphaLUT = new byte[] {4, 40};

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createIndexed(
            redLUT, greenLUT, blueLUT, alphaLUT, 1, DataBuffer.TYPE_BYTE);
    ColorModel model = type.getColorModel();

    assertEquals(
        "Failed to return the colorspace", ColorSpace.TYPE_RGB, model.getColorSpace().getType());
    assertEquals(
        "Failed to return the transparency", Transparency.TRANSLUCENT, model.getTransparency());
    assertEquals(
        "Failed to return the tranfer type", DataBuffer.TYPE_BYTE, model.getTransferType());
    assertEquals("Failed to return the red color component", 1, model.getRed(0));
    assertEquals("Failed to return the red color component", 10, model.getRed(1));
    assertEquals("Failed to return the green color component", 2, model.getGreen(0));
    assertEquals("Failed to return the green color component", 20, model.getGreen(1));
    assertEquals("Failed to return the blue color component", 3, model.getBlue(0));
    assertEquals("Failed to return the blue color component", 30, model.getBlue(1));
    assertEquals("Failed to return the alpha color component", 4, model.getAlpha(0));
    assertEquals("Failed to return the alpha color component", 40, model.getAlpha(1));
  }
  /**
   * Gets a specific pixel color from the specified buffered image
   *
   * @param image
   * @param i
   * @param j
   * @param color
   * @return
   */
  protected Color getPixelColor(BufferedImage image, int i, int j) {
    ColorModel cm = image.getColorModel();
    Raster raster = image.getRaster();
    Object pixel = raster.getDataElements(i, j, null);

    Color actual;
    if (cm.hasAlpha()) {
      actual =
          new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), cm.getAlpha(pixel));
    } else {
      actual = new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), 255);
    }
    return actual;
  }
Beispiel #3
0
    public int[] doContrast(int[] pix, int iw, int ih, double contrast)
    {    
		ColorModel cm = ColorModel.getRGBdefault();
		int r, g, b;
		for(int i = 0; i < iw*ih; i++)	
		{			
			r = (int) (contrast>=0?cm.getRed(pix[i]) + (255-cm.getRed(pix[i]))*contrast:
				cm.getRed(pix[i]) + cm.getRed(pix[i])*contrast);
			g = (int) (contrast>=0?cm.getGreen(pix[i]) + (255-cm.getGreen(pix[i]))*contrast:
				cm.getGreen(pix[i]) + cm.getGreen(pix[i])*contrast);
			b = (int) (contrast>=0?cm.getBlue(pix[i]) + (255-cm.getBlue(pix[i]))*contrast:
				cm.getBlue(pix[i]) + cm.getBlue(pix[i])*contrast);
	
			pix[i] = 255 << 24|r << 16|g << 8|b;
			
		}
		System.out.println("contrast is: " + contrast);
		return pix;
	}	
 public int filterRGB(int x, int y, int rgb) {
   int R, G, B, color;
   ColorModel cm = ColorModel.getRGBdefault();
   if (x == -1) ;
   R = cm.getRed(rgb) & 0xff;
   G = cm.getGreen(rgb) & 0xff;
   B = cm.getBlue(rgb) & 0xff;
   if (R >= 100 && G >= 100 && B >= 100) color = 0xffffffff;
   else color = 0xff000000;
   return color;
 }
Beispiel #5
0
  public PixelVector(int[] vector, int resolution) {

    _rgb = new byte[vector.length];

    ColorModel cm = ColorModel.getRGBdefault();

    for (int i = 0; i < vector.length; i++) {
      setRedValue(i, convertColorToByte(cm.getRed(vector[i])));
      setGreenValue(i, convertColorToByte(cm.getGreen(vector[i])));
      setBlueValue(i, convertColorToByte(cm.getBlue(vector[i])));
    }
    _constant = 1;
    setResolution(resolution);
  }
  public static void main(String args[]) {
    ColorModel cm =
        new ColorModel(32) {
          public int getAlpha(int pixel) {
            return 255;
          }

          public int getBlue(int pixel) {
            return 255;
          }

          public int getGreen(int pixel) {
            return 255;
          }

          public int getRed(int pixel) {
            return 255;
          }
        };

    cm.hasAlpha();
    cm.isAlphaPremultiplied();
    cm.getTransferType();
    cm.getPixelSize();
    cm.getComponentSize();
    cm.getComponentSize();
    cm.getTransparency();
    cm.getNumComponents();
    cm.getNumColorComponents();
    cm.getRed(20);
    cm.getGreen(20);
    cm.getBlue(20);
    cm.getAlpha(20);
    cm.getRGB(20);
    cm.isAlphaPremultiplied();
    cm.isAlphaPremultiplied();

    cm = ColorModel.getRGBdefault();
  }
  // ///////////////////////////////////////////////////////////////
  // 双线性内插值算法
  // 参数:img:要缩放的Image对象
  // dstW:目标图像宽
  // dstH:目标图像高
  // comp:组件参数,比如Applet
  //
  // 公式:f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) +
  // uvf(i+1,j+1)
  //
  // ///////////////////////////////////////////////////////////////
  public static Image doubleLinearScale(Image img, int dstW, int dstH) {
    OperateImage OI = new OperateImage();
    Image imgTemp;
    int[] scaled, src;
    int srcW, srcH;
    int R, G, B;
    double widthFactor, heightFactor, tempX, tempY;
    // double srcX_float = 0.0, srcY_float = 0.0;// 坐标的小数部分
    // int srcX_int = 0, srcY_int = 0;// 坐标的整数部分
    src = OI.takeImg(img, img.getWidth(null), img.getHeight(null));
    ColorModel cm = ColorModel.getRGBdefault();
    for (int j = 0; j < src.length; j++) {
      R = cm.getRed(src[j]);
      G = cm.getGreen(src[j]);
      B = cm.getBlue(src[j]);
      if (R >= 200 && G >= 200 && B >= 200) src[j] = 0xffffffff;
      else src[j] = 0xff000000;
    }
    scaled = new int[dstW * dstH]; // 存放缩放后的图片
    srcW = img.getWidth(null);
    srcH = img.getHeight(null);

    widthFactor = srcW / (dstW + 0.0);
    // System.out.println("widthFactor:"+widthFactor);
    heightFactor = srcH / (dstH + 0.0);
    // System.out.println("heightFactor:"+heightFactor);
    for (int a = 0; a < dstH; a++)
      for (int b = 0; b < dstW; b++) {
        tempX = b * widthFactor;
        tempY = a * heightFactor;
        scaled[a * dstW + b] = getDestPixle(src, srcW, srcH, tempX, tempY);
      }
    // System.out.println("双线性内插值算法完成!");
    imgTemp = OI.madeImg(scaled, dstW, dstH);
    ImageFilter filter = new BWFilter();
    return Toolkit.getDefaultToolkit()
        .createImage(new FilteredImageSource(imgTemp.getSource(), filter));
    // return imgTemp;
  }
  /**
   * PS see http://astronomy.swin.edu.au/~pbourke/geomformats/postscript/ Java
   * http://show.docjava.com:8086/book/cgij/doc/ip/graphics/SimpleImageFrame.java.html
   */
  public boolean drawImage(
      Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
    try {
      // get data from image
      int[] pixels = new int[width * height];
      PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
      grabber.grabPixels();
      ColorModel model = ColorModel.getRGBdefault();

      // print data to ps
      m_printstream.println("gsave");
      m_printstream.println(
          xTransform(xScale(x)) + " " + (yTransform(yScale(y)) - yScale(height)) + " translate");
      m_printstream.println(xScale(width) + " " + yScale(height) + " scale");
      m_printstream.println(
          width + " " + height + " " + "8" + " [" + width + " 0 0 " + (-height) + " 0 " + height
              + "]");
      m_printstream.println("{<");

      int index;
      for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
          index = i * width + j;
          m_printstream.print(toHex(model.getRed(pixels[index])));
          m_printstream.print(toHex(model.getGreen(pixels[index])));
          m_printstream.print(toHex(model.getBlue(pixels[index])));
        }
        m_printstream.println();
      }

      m_printstream.println(">}");
      m_printstream.println("false 3 colorimage");
      m_printstream.println("grestore");
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
Beispiel #9
0
  public BufferedImage changeGrey() {
    PixelGrabber pg =
        new PixelGrabber(this.image.getSource(), 0, 0, this.iw, this.ih, this.pixels, 0, this.iw);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    int grey = 123;

    ColorModel cm = ColorModel.getRGBdefault();
    for (int i = 0; i < this.iw * this.ih; i++) {
      int alpha = cm.getAlpha(this.pixels[i]);
      int red;

      if (cm.getRed(this.pixels[i]) > grey) red = 255;
      else red = 0;
      int green;

      if (cm.getGreen(this.pixels[i]) > grey) green = 255;
      else green = 0;
      int blue;

      if (cm.getBlue(this.pixels[i]) > grey) blue = 255;
      else {
        blue = 0;
      }
      this.pixels[i] = (alpha << 24 | red << 16 | green << 8 | blue);
    }

    Image tempImg =
        Toolkit.getDefaultToolkit()
            .createImage(new MemoryImageSource(this.iw, this.ih, this.pixels, 0, this.iw));
    this.image = new BufferedImage(tempImg.getWidth(null), tempImg.getHeight(null), 4);
    this.image.createGraphics().drawImage(tempImg, 0, 0, null);
    return this.image;
  }
Beispiel #10
0
  public BufferedImage getMedian() {
    PixelGrabber pg =
        new PixelGrabber(this.image.getSource(), 0, 0, this.iw, this.ih, this.pixels, 0, this.iw);
    try {
      pg.grabPixels();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    ColorModel cm = ColorModel.getRGBdefault();
    for (int i = 1; i < this.ih - 1; i++) {
      for (int j = 1; j < this.iw - 1; j++) {
        int alpha = cm.getAlpha(this.pixels[(i * this.iw + j)]);

        int red4 = cm.getRed(this.pixels[(i * this.iw + j - 1)]);
        int red5 = cm.getRed(this.pixels[(i * this.iw + j)]);
        int red6 = cm.getRed(this.pixels[(i * this.iw + j + 1)]);
        int red;

        if (red4 >= red5) {

          if (red5 >= red6) {
            red = red5;
          } else {

            if (red4 >= red6) red = red6;
            else red = red4;
          }
        } else {

          if (red4 > red6) {
            red = red4;
          } else {

            if (red5 > red6) red = red6;
            else {
              red = red5;
            }
          }
        }

        int green4 = cm.getGreen(this.pixels[(i * this.iw + j - 1)]);
        int green5 = cm.getGreen(this.pixels[(i * this.iw + j)]);
        int green6 = cm.getGreen(this.pixels[(i * this.iw + j + 1)]);
        int green;

        if (green4 >= green5) {

          if (green5 >= green6) {
            green = green5;
          } else {

            if (green4 >= green6) green = green6;
            else green = green4;
          }
        } else {

          if (green4 > green6) {
            green = green4;
          } else {

            if (green5 > green6) green = green6;
            else {
              green = green5;
            }
          }
        }

        int blue4 = cm.getBlue(this.pixels[(i * this.iw + j - 1)]);
        int blue5 = cm.getBlue(this.pixels[(i * this.iw + j)]);
        int blue6 = cm.getBlue(this.pixels[(i * this.iw + j + 1)]);
        int blue;

        if (blue4 >= blue5) {

          if (blue5 >= blue6) {
            blue = blue5;
          } else {

            if (blue4 >= blue6) blue = blue6;
            else blue = blue4;
          }
        } else {

          if (blue4 > blue6) {
            blue = blue4;
          } else {

            if (blue5 > blue6) blue = blue6;
            else {
              blue = blue5;
            }
          }
        }
        this.pixels[(i * this.iw + j)] = (alpha << 24 | red << 16 | green << 8 | blue);
      }
    }

    Image tempImg =
        Toolkit.getDefaultToolkit()
            .createImage(new MemoryImageSource(this.iw, this.ih, this.pixels, 0, this.iw));
    this.image = new BufferedImage(tempImg.getWidth(null), tempImg.getHeight(null), 4);
    this.image.createGraphics().drawImage(tempImg, 0, 0, null);
    return this.image;
  }