图像运算(八)——逻辑异或(XOR)

描述:

As XOR

input1 input2output

0             0               0

0             1               1

1             0               1

1             1               0


As XNOR

input1 input2output

0              0              1

0              1              0

1              0              0

1             1               1


Code:


Start,

  /**
   *Applies the image XOR operator on the specified image arrays, with the specified offset and scale value
   *@param src1_1d The first source image as a pixel array
   *@param src2_1d The second source image as a pixel array
   *@param XNOR Boolean to indicate wether we should XNOR rather than XOR
   *@param width width of the destination image in pixels
   *@param height height of the destination image in pixels
   *@param oset The offset value
   *@param scale The scale value
   *@return A pixel array containing the X(N)OR of the two input images
   */


 

  public int [] doXor(int [] src1_1d, int [] src2_1d, boolean XNOR, 
		      int width, int height, float oset, float scale){
    
    int place1 = -1;
    int place2 = -1;
    int src1rgb = 0;
    int src2rgb = 0;
    int result = 0;
    //Get size of image and make 1d_arrays
    d_w = width;
    d_h = height;
    
    dest_1d = new int[d_w*d_h];
    
    boolean firstwider = false;
    boolean secondwider = false;
    int wrap;
    
    if (i1_w > d_w){
      wrap =   ((i1_w + 1) - d_w);
      firstwider = true;
    } else if (i2_w > d_w){
      wrap =    ((i2_w + 1) - d_w);
      secondwider = true;
      
    } else {
      wrap = 0;
    }
    
    //if you know there is no wrap around, you can save yourself some time
    
    if (wrap == 0) {
      for (int i=0; i< dest_1d. length ; i++){
	src2rgb = src2_1d[i] & 0x000000ff;
	src1rgb = src1_1d[i] & 0x000000ff;
	
	/* Due to Java not having unsigned ints, the ~ will always give a
	   negative result. As we a dealing with only the bottom 8 bits of
	   the int, I will just mask out the bottom bits. Neil.
	   */
	if (XNOR) {
	  result = (int) ((scale * (float)(~(src1rgb ^ src2rgb) & 0xff))+oset);
	} else {
	  result = (int) ((scale * (float) ( src1rgb ^ src2rgb )) + oset);
	}

	if (result < 0){
	  result = 0;
	} else if  (result > 255){
	  result = 255;
	}
	
	//create an int value for dest_1d
	dest_1d[i ] =  0xff000000 | (result + (result << 16) + (result << 8));
	
      }
      
      return dest_1d;
      
    }
    else {
      
      for (int i=0; i< dest_1d. length ; i++){
	
	//we might need to skip out some pixels which aren't in the overlap area
	
	if ((i %d_w  ) == 0 ) {
	  if ( i == 0 ){
	    place1 = 0;
	    place2 = 0;
	  } else if (secondwider) {
	    place2 = place2 + wrap;
	    place1 ++;
	  } else {
	    place1 = place1 + wrap;
	    place2 ++;
	  }
	} else{
	  place2 ++;
	  place1 ++;
	}
	
	src2rgb = src2_1d[place2] & 0x000000ff;
	src1rgb = src1_1d[place1] & 0x000000ff;
	
	/* Due to Java not having unsigned ints, the ~ will always give a
	   negative result. As we a dealing with only the bottom 8 bits of
	   the int, I will just mask out the bottom bits. Neil.
	   */
	if (XNOR) {
	  result = (int) ((scale * (float) (~(src1rgb ^ src2rgb) & 0xff))+ oset);
	} else {
	  result = (int) ((scale * (float) ( src1rgb ^ src2rgb )) + oset);
	}

	if (result < 0){
	  result = 0;
	} else if  (result > 255){
	  result = 255;
	}
	
	//create an int value for dest_1d
	dest_1d[i ] =  0xff000000 | (result + (result << 16) + (result << 8));
      }
      return dest_1d;
    }
  }

Input Image

图像运算(八)——逻辑异或(XOR)_第1张图片图像运算(八)——逻辑异或(XOR)_第2张图片


Output Image;

图像运算(八)——逻辑异或(XOR)_第3张图片


总结:个人感觉,此操作有点“非有族类,其心必异”之势

你可能感兴趣的:(image,float,arrays,input,output,java,Image,Processing-Base)