DeBayerScissors.java 37.5 KB
Newer Older
Andrey Filippov's avatar
Andrey Filippov committed
1
package com.elphel.imagej.calibration;
Andrey Filippov's avatar
Andrey Filippov committed
2
/**
Andrey Filippov's avatar
Andrey Filippov committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 ** -----------------------------------------------------------------------------**
 ** deBayerScissors.java
 **
 ** Frequency-domain de-mosoaic filters generation
 ** 
 **
 ** Copyright (C) 2010 Elphel, Inc.
 **
 ** -----------------------------------------------------------------------------**
 **  
 **  focus_tuning.java is free software: you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation, either version 3 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ** -----------------------------------------------------------------------------**
 **
 */
Andrey Filippov's avatar
Andrey Filippov committed
28 29 30 31

import ij.process.*;
import ij.plugin.filter.GaussianBlur;
import java.util.HashSet;
Andrey Filippov's avatar
Andrey Filippov committed
32 33

import com.elphel.imagej.common.DoubleFHT;
34
import com.elphel.imagej.common.ShowDoubleFloatArrays;
Andrey Filippov's avatar
Andrey Filippov committed
35
public class DeBayerScissors {
Andrey Filippov's avatar
Andrey Filippov committed
36 37 38 39
	private PolarSpectrums        pol_instace=null;
	private double [][][]         lopass=null;
	private int                   size;
	private double                lastMidEnergy; // last midrange spectral energy
40
//	private ShowDoubleFloatArrays SDFA_instance; // just for debugging?
Andrey Filippov's avatar
Andrey Filippov committed
41
	private DoubleFHT             fht_instance;
42 43 44 45 46 47
	private int [][] aliasMapRedBlue={
			{-2,-2},{-2,-1},{-2,0},{-2,1},
			{-1,-2},{-1,-1},{-1,0},{-1,1},
			{ 0,-2},{ 0,-1},       { 0,1},
			{ 1,-2},{ 1,-1},{ 1,0},{ 1,1}};
    private int [][][][] speedTable = null;
Andrey Filippov's avatar
Andrey Filippov committed
48 49

	public double getMidEnergy() {return lastMidEnergy; } // instead of the  DOUBLE_DEBUG_RESULT
Andrey Filippov's avatar
Andrey Filippov committed
50
	public DeBayerScissors(
Andrey Filippov's avatar
Andrey Filippov committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
			int isize, // size of the square array, centar is at size/2, size/2, only top half+line will be used
			double polarStep, // maximal step in pixels on the maxRadius for 1 angular step (i.e. 0.5)
			double debayer_width_green, // result green mask mpy by scaled default (diamond)
			double debayer_width_redblue, // result red/blue mask mpy by scaled default (square)
			double debayer_width_redblue_main, // green mask when applied to red/blue, main (center)
			double debayer_width_redblue_clones){// green mask when applied to red/blue, clones 
		size=isize;
		fht_instance=       new DoubleFHT();
		pol_instace=new PolarSpectrums(size, // size of the square array, centar is at size/2, size/2, only top half+line will be used
				Math.PI,
				size/2-2, // width of the polar array - should be <= size/2-2
				polarStep, //0.5, //0.75, //2.0, //0.5, // maximal step in pixels on the maxRadius for 1 angular step (i.e. 0.5)
				4);// angular symmetry - 0- none,1 - pi corresponds to integer, 2 - pi/2 corresponds to integer, n - pi/n corresponds to integer angular step 

		lopass=  createAliasFilters (debayer_width_green, // result green mask mpy by scaled default (diamond)
				debayer_width_redblue, // result red/blue mask mpy by scaled default (square)
				debayer_width_redblue_main, // green mask when applied to red/blue, main (center)
				debayer_width_redblue_clones, // green mask when applied to red/blue, clones 
				size, // side of the square
				4); // should be 4 now
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

		int hsize=size/2;
		int subpixel=4; // hardwired - when changing it will need to change alias maps
		int aliasX=size/subpixel;
	    speedTable = new int [hsize+1][size][aliasMapRedBlue.length][3];
        int i,j,nAlias,x,y;
		for (i = 0;i <= hsize;i++) for (j = 0;j < size;j++) {
			speedTable[i][j][0][0] = i*size+j;
			speedTable[i][j][0][1] = ((size-i) % size) * size + ((size-j) % size);
			for(nAlias=0;nAlias<aliasMapRedBlue.length; nAlias++) {
				y = (i-aliasX*aliasMapRedBlue[nAlias][0]+size) % size;
				x = (j-aliasX*aliasMapRedBlue[nAlias][1]+size) % size;
				if (y > hsize) {
					y = size - y;
					x = (size - x) % size;
				}
				if (y>hsize) {
					y=size-y;
					x=(size-x)%size;
				}
				speedTable[i][j][nAlias][2]=y*size+x;
			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
94 95 96 97 98
	}

	/* returns 2 masks (0:0 in the top left corner, match fht) [0] - for greens, [1] - for red/blue */
	/* Possible improvements: - 1 make the initial green mask (or actually "fan"-like image) to have sharper ends.
                             2. detect periodic (line of spots) on the spectrum amplitudes (corresponds to thin lines) and use this
Andrey Filippov's avatar
Andrey Filippov committed
99 100
                                info to confirm this area to belong to the main spectrum */

Andrey Filippov's avatar
Andrey Filippov committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	public double [][] aliasScissors(double [] green_fht, // fht array for green, will be masked in-place
			double debayer_threshold, // no high frequencies - use default uniform filter
			double debayer_gamma, // power function applied to the amplitudes before generating spectral masks
			double debayer_bonus, // scale far pixels as (1.0+bonus*r/rmax)
			double mainToAlias,// relative main/alias amplitudes to enable pixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out)
			double debayer_mask_blur, // for both masks  sigma for Gaussian blur of the binary masks (<0 -do not use "scissors")
			boolean debayer_use_scissors, // use "scissors", if false - just apply "diamond" ands "square" with DEBAYER_WIDTH_GREEN and DEBAYER_WIDTH_REDBLUE
			int this_debug){ // internal debug level
		int length=green_fht.length;
		int size=(int) Math.sqrt(length);
		double [] green_mask;
		double [] red_blue_mask;
		double [] green_amp=fht_instance.calculateAmplitude(green_fht);
		int i,j;
		/**normalize amplitudes, apply gamma */
		double dmax=0.0;
		for (i=0;i<green_amp.length;i++) if (green_amp[i]>dmax) dmax=green_amp[i];
		dmax=1.0/dmax;
		for (i=0;i<green_amp.length;i++) green_amp[i]= Math.pow(green_amp[i]*dmax,debayer_gamma);
120
		if (this_debug>2)   ShowDoubleFloatArrays.showArrays(green_amp,  "DT-gam"); // only top half+1 will be used
Andrey Filippov's avatar
Andrey Filippov committed
121 122 123 124 125 126 127 128 129 130 131
		double midRangeSpectral=pol_instace.maxAmpInRing (green_amp);
		boolean useFancyDebayer=(midRangeSpectral>=debayer_threshold);

		lastMidEnergy= midRangeSpectral; // for optional monitoring outside of this class

		if (useFancyDebayer && debayer_use_scissors) { /* calculate and apply "scissors" masks */
			green_mask= calcGreensAliasMaskRays (green_amp, // normalized amplitude spectrum, (0,0) in the center
					pol_instace, // initialized instance
					debayer_bonus, // hack - here it is "bonus"
					this_debug);//

132
			if (this_debug>3) ShowDoubleFloatArrays.showArrays(green_mask,  "G-raw");
Andrey Filippov's avatar
Andrey Filippov committed
133 134
			if (debayer_mask_blur>0) {
				blurDouble(green_mask,   size, debayer_mask_blur, debayer_mask_blur, 0.01);
135
				if (this_debug>3) ShowDoubleFloatArrays.showArrays(green_mask,  "G-blurred");
Andrey Filippov's avatar
Andrey Filippov committed
136 137 138 139 140 141 142 143 144
			}
			double [] green_mask_for_redblue_main=  green_mask.clone();
			double [] green_mask_for_redblue_clones=green_mask.clone();
			for (i=0;i<green_mask.length;i++) {
				green_mask_for_redblue_main[i]*=  lopass[2][0][i];
				green_mask_for_redblue_clones[i]*=lopass[2][1][i];
			}

			if (this_debug>2) {
145 146
				ShowDoubleFloatArrays.showArrays(green_mask_for_redblue_main,  "MAIN");
				ShowDoubleFloatArrays.showArrays(green_mask_for_redblue_main,  "CLONES");
Andrey Filippov's avatar
Andrey Filippov committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160
			}

			/* Maybe here we need to unmasked (wide bandwidth) green_amp? */
			red_blue_mask= calcRedBlueAliasMaskRays (
					green_amp, // both halves are needed ??
					green_mask_for_redblue_main, // may be null if amp_pixels is already masked
					green_mask_for_redblue_clones,
					pol_instace, // initialized instance (if null - skip rays processing)
					mainToAlias,// relative main/alias amplitudes to enable pixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out)
					debayer_bonus, // scale far pixels as (1.0+bonus*r/rmax)
					this_debug);// relative main/alias amplitudes to enable pixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out)

			/* add    double mainToAlias){// relative main/alias amplitudes to enable pixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out) */

161
			if (this_debug>3) ShowDoubleFloatArrays.showArrays(red_blue_mask,  "RB-raw");
Andrey Filippov's avatar
Andrey Filippov committed
162 163
			if (debayer_mask_blur>0) {
				blurDouble(red_blue_mask, size,debayer_mask_blur, debayer_mask_blur, 0.01);
164
				if (this_debug>3) ShowDoubleFloatArrays.showArrays(red_blue_mask,  "RB-blurred");
Andrey Filippov's avatar
Andrey Filippov committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
			}
			for (i=0;i<red_blue_mask.length;i++) red_blue_mask[i]*=lopass[1][1][i]; //  scaled, red-blue - was red_blue_lopass[i];
		} else { // debayer_mask_blur<0 : use default masks
			green_mask=lopass[1][0].clone(); //green_lopass.clone(); variable (wide) filter here)
			red_blue_mask=lopass[1][1].clone(); //red_blue_lopass.clone();
			if (!useFancyDebayer) for (i=0;i<green_mask.length;i++) { // no high-frequency componets detected - reduce noise by extra (narrow) filtering
				green_mask[i]*=   lopass[0][0][i]; // *=   green_lopass[i];
				red_blue_mask[i]*=lopass[0][1][i]; // *=red_blue_lopass[i];
			}
		}
		/* Swap quadrants in the masks to match FHT arrays (0:0 in the top left corner) */
		fht_instance.swapQuadrants(green_mask);
		fht_instance.swapQuadrants(red_blue_mask);
		/* return both masks */
		double [][] result =new double [2][];
		result[0]= green_mask;
		result[1]= red_blue_mask;
182
		//    if (this_debug>3) ShowDoubleFloatArrays.showArrays(result,  "before_norm_masks");
Andrey Filippov's avatar
Andrey Filippov committed
183 184 185 186 187 188 189


		/* normalize masks to have exactly 1.0 at 0:0 - it can be reduced by blurring */
		for (i=0;i<result.length;i++) {
			dmax=1.0/result[i][0];
			for (j=0;j<result[i].length;j++) result[i][j]*=dmax;
		}
190
		//    if (this_debug>3) ShowDoubleFloatArrays.showArrays(result,  "masks");
Andrey Filippov's avatar
Andrey Filippov committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		return result;
	}

	public double [] calcRedBlueAliasMaskRays (
			double [] green_amp, // both halves are needed ??
			double [] green_mask, // may be null if amp_pixels is already masked
			double [] green_mask_clones, // mask (more inclusive than just green_mask) to be used with clones
			PolarSpectrums pol_instace, // initialized instance (if null - skip rays processing)
			double mainToAlias,// relative main/alias amplitudes to enable lixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out)
			double bonus, // scale far pixels as (1.0+bonus*r/rmax)
			int this_debug){// relative main/alias amplitudes to enable lixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out)

		int length=green_amp.length;
		int size = (int) Math.sqrt(length);
		int hsize=size/2;
206 207 208
//		int subpixel=4; // hardwired - when changing it will need to change alias maps
//		int aliasX=size/subpixel;
		int i,j,index,index_back; //,x,y;
Andrey Filippov's avatar
Andrey Filippov committed
209 210 211 212 213 214 215 216 217
		double [] amp=       green_amp.clone();
		double [] amp_clones=green_amp.clone();
		if (green_mask!=null)        for (i=0;i<amp.length;i++)        amp[i]*=green_mask[i];
		if (green_mask_clones!=null) for (i=0;i<amp_clones.length;i++) amp_clones[i]*=green_mask_clones[i];
		double [] mask= new double [length];
		for (i=0;i<length;i++) mask[i]=0.0;
		/* Combine into mask by comparing pixels[] from the zero and 7 aliases */
		double d;
		int nAlias;
218
		/*
Andrey Filippov's avatar
Andrey Filippov committed
219 220 221 222 223 224
		int [][] aliasMapRedBlue={
				{-2,-2},{-2,-1},{-2,0},{-2,1},
				{-1,-2},{-1,-1},{-1,0},{-1,1},
				{ 0,-2},{ 0,-1},       { 0,1},
				{ 1,-2},{ 1,-1},{ 1,0},{ 1,1}};

225
		// First step - mask out all the pixels where at least one of the alias amplitude is above the main one
226 227
		if (this_debug>2) ShowDoubleFloatArrays.showArrays(amp.clone(),  "amp");
		if (this_debug>2) ShowDoubleFloatArrays.showArrays(amp_clones,  "amp_clones");
Andrey Filippov's avatar
Andrey Filippov committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

		for (i=0;i<=hsize;i++) for (j=0;j<size;j++) {
			index=i*size+j;
			index_back=((size-i) % size) * size + ((size-j) % size);
			d=amp[index]*mainToAlias;
			if (d>0.0) {
				mask[index]=1.0;
				mask[index_back]=1.0;
				//        isGreater=true;
				for(nAlias=0;nAlias<aliasMapRedBlue.length; nAlias++) {
					y=(i-aliasX*aliasMapRedBlue[nAlias][0]+size) % size;
					x=(j-aliasX*aliasMapRedBlue[nAlias][1]+size) % size;
					if (y>hsize) {
						y=size-y;
						x=(size-x)%size;
					}
					if (amp_clones[y*size+x]>d) {
						mask[index]=-1.0;
						mask[index_back]=-1.0;
						break;
					}
					
				}
			}
		}
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
		*/
		/*
	    speedTable = new int [hsize][size][aliasMapRedBlue.length][3];
        int i,j,nAlias,x,y;
		for (i=0;i<=hsize;i++) for (j=0;j<size;j++) {
			speedTable[i][j][0][0] = i*size+j;
			speedTable[i][j][0][1] = ((size-i) % size) * size + ((size-j) % size);
			for(nAlias=0;nAlias<aliasMapRedBlue.length; nAlias++) {
				y = (i-aliasX*aliasMapRedBlue[nAlias][0]+size) % size;
				x = (j-aliasX*aliasMapRedBlue[nAlias][1]+size) % size;
				if (y > hsize) {
					y = size - y;
					x = (size - x) % size;
				}
				if (y>hsize) {
					y=size-y;
					x=(size-x)%size;
				}
				speedTable[i][j][nAlias][2]=y*size+x;
			}
		}
		*/

		// First step - mask out all the pixels where at least one of the alias amplitude is above the main one
277 278
		if (this_debug>2) ShowDoubleFloatArrays.showArrays(amp.clone(),  "amp");
		if (this_debug>2) ShowDoubleFloatArrays.showArrays(amp_clones,  "amp_clones");
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

		for (i=0;i<=hsize;i++) for (j=0;j<size;j++) {
			index =      speedTable[i][j][0][0]; // i*size+j;
			index_back = speedTable[i][j][0][1]; //((size-i) % size) * size + ((size-j) % size);
			d=amp[index]*mainToAlias;
			if (d>0.0) {
				mask[index]=1.0;
				mask[index_back]=1.0;
				for(nAlias=0;nAlias<aliasMapRedBlue.length; nAlias++) {
					if (amp_clones[speedTable[i][j][nAlias][2]]>d) {
						mask[index]=-1.0;
						mask[index_back]=-1.0;
						break;
					}
				}
			}
		}
		
// End of replacement code
		
299
		if (this_debug>2)  ShowDoubleFloatArrays.showArrays(mask,  "mask");
Andrey Filippov's avatar
Andrey Filippov committed
300 301 302 303

		if (pol_instace==null) return mask;
		/* Now apply mask to amplitudes and use ray processing (same as with greens)*/
		for (i=0;i<amp.length;i++) amp[i]*=mask[i];
304
		if (this_debug>2) ShowDoubleFloatArrays.showArrays(amp,  "amp-mask");
Andrey Filippov's avatar
Andrey Filippov committed
305
		double [] polar_amp=pol_instace.cartesianToPolar(amp);
306 307
		int width = pol_instace.getWidth();
		int height = pol_instace.getHeight();
308
		if (this_debug>2)   ShowDoubleFloatArrays.showArrays(polar_amp.clone(),width, height,  "RB-polar-amp");
309 310
		double k= bonus/width;
		for (i=0;i<pol_instace.getHeight();i++) for (j = 0; j < width; j++) polar_amp[i * width + j]*=1.0+k*j;
Andrey Filippov's avatar
Andrey Filippov committed
311 312 313
		double [] polar_mask_pixels=pol_instace.genPolarRedBlueMask(polar_amp,0); // 0 - just 1.0/0.0, 1 - "analog"
		double [] cart_mask_pixels= pol_instace.polarToCartesian (polar_mask_pixels,size,0.0);
		if (this_debug>2) {
314 315 316
			ShowDoubleFloatArrays.showArrays(polar_amp,         width, height,     "RB-amp-bonus");
			ShowDoubleFloatArrays.showArrays(polar_mask_pixels, width, height, "pRBm");
			ShowDoubleFloatArrays.showArrays(cart_mask_pixels,  size,  size,   "cRBm");
Andrey Filippov's avatar
Andrey Filippov committed
317 318 319 320
		}
		if (this_debug>2) {
			double [] polar_mask_pixels1=pol_instace.genPolarRedBlueMask(polar_amp,1);
			double [] cart_mask_pixels1= pol_instace.polarToCartesian (polar_mask_pixels1,size,0.0);
321 322
			ShowDoubleFloatArrays.showArrays(polar_mask_pixels1, width, height, "pRBm1");
			ShowDoubleFloatArrays.showArrays(cart_mask_pixels1,  size,  size,   "cRBm1");
Andrey Filippov's avatar
Andrey Filippov committed
323 324 325 326 327 328 329 330 331 332 333 334 335
		}
		return cart_mask_pixels;

	}


	public double [] calcGreensAliasMaskRays (double [] amp_pixels, // normalized amplitude spectrum, (0,0) in the center
			PolarSpectrums pol_instace, // initialized instance
			double bonus, // scale far pixels as (1.0+bonus*r/rmax)
			int this_debug){// relative main/alias amplitudes to enable lixels (i.e. 0.5 means that if alias is >0.5*main, the pixel will be masked out)
		int length=amp_pixels.length;
		int size = (int) Math.sqrt(length);
		double [] polar_amp_pixels=pol_instace.cartesianToPolar(amp_pixels);
336
		if (this_debug>2)   ShowDoubleFloatArrays.showArrays(polar_amp_pixels.clone(),pol_instace.getWidth(),pol_instace.getHeight(),  "polar-amp");
Andrey Filippov's avatar
Andrey Filippov committed
337 338 339 340 341 342

		double k= bonus/pol_instace.getWidth();
		for (int i=0;i<pol_instace.getHeight();i++) for (int j=0;j<pol_instace.getWidth();j++) polar_amp_pixels[i*pol_instace.getWidth()+j]*=1.0+k*j;
		double [] polar_green_mask_pixels=pol_instace.genPolarGreenMask(polar_amp_pixels,0); // 0 - just 1.0/0.0, 1 - "analog"
		double [] cart_green_mask_pixels= pol_instace.polarToCartesian (polar_green_mask_pixels,size,0.0);
		if (this_debug>2) {
343 344 345
			ShowDoubleFloatArrays.showArrays(polar_amp_pixels,  pol_instace.getWidth(),pol_instace.getHeight(),     "amp-bonus");
			ShowDoubleFloatArrays.showArrays(polar_green_mask_pixels,pol_instace.getWidth(),pol_instace.getHeight(), "pgm");
			ShowDoubleFloatArrays.showArrays(cart_green_mask_pixels,size,size,   "cgm");
Andrey Filippov's avatar
Andrey Filippov committed
346 347 348 349 350
		}

		if (this_debug>2) {
			double [] polar_green_mask_pixels1=pol_instace.genPolarGreenMask(polar_amp_pixels,1);
			double [] cart_green_mask_pixels1= pol_instace.polarToCartesian (polar_green_mask_pixels1,size,0.0);
351 352
			ShowDoubleFloatArrays.showArrays(polar_green_mask_pixels1,pol_instace.getWidth(),pol_instace.getHeight(), "PGM1");
			ShowDoubleFloatArrays.showArrays(cart_green_mask_pixels1,size,size,   "CGM1");
Andrey Filippov's avatar
Andrey Filippov committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
		}
		return cart_green_mask_pixels;

	}


	double [][][] createAliasFilters (double debayer_width_green, // result green mask mpy by scaled default (diamond)
			double debayer_width_redblue, // result red/blue mask mpy by scaled default (square)
			double debayer_width_redblue_main, // green mask when applied to red/blue, main (center), square
			double debayer_width_redblue_clones, // green mask when applied to red/blue, clones , square
			int size, // side of the square
			int subpixel){ // should be 4 now
		int i;
		double [] cosMask= createCosMask (size,  subpixel);   // oversampling
		double [][] [] lopass =new double [3][2][];
		lopass[0][0]=new double [size*size];
		for (i=0;i<lopass[0][0].length;i++) lopass[0][0][i]=1.0;
		lopass[0][1]=lopass[0][0].clone();
		lopass[1][0]=lopass[0][0].clone();
		lopass[1][1]=lopass[0][0].clone();
		lopass[2][0]=lopass[0][0].clone();
		lopass[2][1]=lopass[0][0].clone();
		maskBayerAliases (lopass[0][0],   // FHT array to be filtered
				cosMask,   // cosine mask array
				true); // this fht array is for the checkerboard greens
		maskBayerAliases (lopass[0][1],   // FHT array to be filtered
				cosMask,   // cosine mask array
				false); // this fht array is for the checkerboard greens

		cosMask= createCosMask ((int) Math.round(size*debayer_width_green),  subpixel);   // oversampling
		maskBayerAliases (lopass[1][0],   // FHT array to be filtered
				cosMask,   // cosine mask array
				true); // this fht array is for the checkerboard greens
		cosMask= createCosMask ((int) Math.round(size*debayer_width_redblue),  subpixel);   // oversampling
		maskBayerAliases (lopass[1][1],   // FHT array to be filtered
				cosMask,   // cosine mask array
				false); // this fht array is for the checkerboard greens

		cosMask= createCosMask ((int) Math.round(size*debayer_width_redblue_main),  subpixel);   // oversampling
		maskBayerAliases (lopass[2][0],   // FHT array to be filtered
				cosMask,   // cosine mask array
				false); // this fht array is for the checkerboard greens
		cosMask= createCosMask ((int) Math.round(size*debayer_width_redblue_clones),  subpixel);   // oversampling
		maskBayerAliases (lopass[2][1],   // FHT array to be filtered
				cosMask,   // cosine mask array
				false); // this fht array is for the checkerboard greens


		fht_instance.swapQuadrants(lopass[0][0]);
		fht_instance.swapQuadrants(lopass[0][1]);
		fht_instance.swapQuadrants(lopass[1][0]);
		fht_instance.swapQuadrants(lopass[1][1]);
		fht_instance.swapQuadrants(lopass[2][0]);
		fht_instance.swapQuadrants(lopass[2][1]);
		return lopass;
	}

	void maskBayerAliases (double [] fht,   // FHT array to be filtered
			double [] cosMask,   // cosine mask array
			boolean isChecker) { // this fht array is for the checkerboard greens
		int size= (int) Math.sqrt(fht.length);
		int iy,ix, ix1,iy1;
		int tsize= (cosMask.length-1)/(isChecker?1:2);
		int index=0;
		int hsizeM1=(size/2)-1;
		if (isChecker) {

			for (iy=0;iy<size;iy++) {
				iy1=(iy+hsizeM1)%size -hsizeM1;
				for (ix=0;ix<size;ix++) {
					ix1=(ix+hsizeM1)%size -hsizeM1;
					if (((ix1+iy1)>-tsize) &&
							((ix1-iy1)>-tsize) &&
							((ix1+iy1)<=tsize) &&
							((ix1-iy1)<=tsize)) fht[index++]*=cosMask[Math.abs(ix1+iy1)]*cosMask[Math.abs(ix1-iy1)];
					else fht[index++]=0.0;
				}
			}

		} else {
			for (iy=0;iy<size;iy++) {
				iy1=(iy+hsizeM1)%size -hsizeM1;
				for (ix=0;ix<size;ix++) {
					ix1=(ix+hsizeM1)%size -hsizeM1;
					if ((iy1>-tsize) && (iy1<=tsize) && (ix1>-tsize) && (ix1<=tsize)) fht[index++]*=cosMask[2*Math.abs(iy1)]*cosMask[2*Math.abs(ix1)];
					else fht[index++]=0.0;
				}
			}
		}
	}


	double [] createCosMask (int fftsize,   // FHT array to be filtered - just length is used
			int subdiv   // oversampling
			) { // this fht array is for the checkerboard greens
		int size= 2*fftsize/subdiv;
		double [] cosMask=new double [size+1];
		for (int i=0;i<=size;i++) cosMask[i]=0.5*(1.0+Math.cos(i*Math.PI/size));
		return cosMask; 
	}

	// temporary using float implementation in ImageJ - re-write to directly use double [] arrays
	public void  blurDouble(double[] pixels,
			int width,
			double sigmaX,
			double sigmaY,
			double precision) {
		//  public void  blurFloat(red_blue_mask, DEBAYER_MASK_BLUR, DEBAYER_MASK_BLUR, 0.01);
		int i;
		int height = pixels.length/width;
		float [] fpixels=new float [pixels.length];
		for (i=0;i<pixels.length;i++) fpixels[i]= (float) pixels[i];
		FloatProcessor fp = new FloatProcessor(width, height, fpixels, null);
		GaussianBlur gb = new GaussianBlur();
		gb.blurFloat(fp, sigmaX, sigmaY, precision);
		for (i=0;i<pixels.length;i++) pixels[i]=fpixels[i];
	}
	/* ====================================================== */
	public class PolarSpectrums  {
		public int radius=0;
		public int iRadiusPlus1; // number of radius steps
		public int iAngle;
		public double aStep;
		public double rStep;
		public int size;
		public int length;
		// Make them private later, after debugging
		private int    [][] polar2CartesianIndices;   // for each polar angle/radius (angle*iRadiusPlus1+radius) - 4 interpolation corners (0:0, dx:0, 0:dy, dx:dy), the first (0:0) being the closest to the polar point
		private double [][] polar2CartesianFractions; //  a pair of dx, dy for interpolations (used with ) polar2CartesianIndices[][]]
		private int    [][]   cartesian2PolarIndices;   // each per-pixel array is a list of indices in polar array pointing to this cell (may be empty)
		private int    []     cartesian2PolarIndex;     // Cartesian->polar array index (cell closest to the center). Is it possible that cartesian2PolarIndices does not include this one?
		private int    [][]   polarGreenMap=null ;     // each element is a variable length integer array with a list of the alias indices
		private int    [][]   polarRedBlueMap=null ;    // each element is a variable length integer array with a list of the alias indices
		private int    [][]   sameCartesian=null ;     // each element is a variable length integer array with a list of indices of the other polar cells that belong (point to) the same cartesian cell
		private int    []     cartAmpList = null;      // list of indices of the elements of the cartesian array (symmetrical around the center) so the distance is between ampRMinMax[0] and ampRMinMax[1]
		private double []     ampRMinMax  ={0.0,0.0};
		public PolarSpectrums() { }  // so "Compile and Run" will be happy
		/* Convert cartesian to polar array, dimensions are set in the class constructor. Uses bi-linear interpolation */
		public double [] cartesianToPolar (double [] cartPixels ) {
			double [] polPixels=new double[iRadiusPlus1*(iAngle+1)];
			int i;
			for (i=0;i<polPixels.length;i++) {
495 496 497 498
				polPixels[i]=(1-polar2CartesianFractions[i][1])*( (1-polar2CartesianFractions[i][0])*cartPixels[polar2CartesianIndices[i][0]]  +
						polar2CartesianFractions[i][0]*cartPixels[polar2CartesianIndices[i][1]])+
						polar2CartesianFractions[i][1] *( (1-polar2CartesianFractions[i][0])*cartPixels[polar2CartesianIndices[i][2]]  +
								polar2CartesianFractions[i][0]*cartPixels[polar2CartesianIndices[i][3]]) ;
Andrey Filippov's avatar
Andrey Filippov committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
			}
			return polPixels;
		}
		public double [] polarToCartesian (double [] polPixels , int height, double undefined ) { return polarToCartesian (polPixels , height, undefined, height==size); }
		public double [] polarToCartesian (double [] polPixels , double undefined)              { return polarToCartesian (polPixels ,size, undefined,false); }
		public double [] polarToCartesian (double [] polPixels , int height )                   { return polarToCartesian (polPixels , height, Double.NaN,height==size); }
		public double [] polarToCartesian (double [] polPixels )                                { return polarToCartesian (polPixels , size, Double.NaN,false);  }
		public double [] polarToCartesian (double [] polPixels,
				int height, // for partial arrays
				double undefined, // use this value in the undefined areas
				boolean   symmHalf){ // add center-symmetrical top to the bottom(spectrums of real signals)
			int length=size*height;
			double [] cartPixels=new double[length];
			int i,j;
			int [] sameCartCell;
			double d;
			int l=symmHalf?((size+1)*size/2+1) :(size*height);
			int l2=(size+1)*size;
			for (i=0;i<l;i++) {
				sameCartCell=cartesian2PolarIndices[i];
				if (sameCartCell==null) {
					if (cartesian2PolarIndex[i]>=0) cartPixels[i]=polPixels[cartesian2PolarIndex[i]];
					else cartPixels[i]=undefined;
				} else {
					d=0;
					for (j=0;j<sameCartCell.length;j++) d+=polPixels[sameCartCell[j]];
					cartPixels[i]=d/sameCartCell.length;
				}
				if (symmHalf) {
					j=l2-i;
					if (j<length) cartPixels[j] = cartPixels[i];
				}
			}
			return cartPixels;
		}
		/* Caculates maximal value of a center-symmetrical array of the amplitudes in a ring. Uses cached table of indices, recalculates if it changed */
		public double maxAmpInRing ( double []amps ){ return  maxAmpInRing (amps,size*0.118,size*0.236);} // ~=1/3* (Math.sqrt(2)/4), 2/3* (Math.sqrt(2)/4) (center 1/3 ring between center and the closest alias for greens)
		public double maxAmpInRing ( double []amps,
				double rMin,
				double rMax
				){
			int i,j,x,y;
			if ((cartAmpList==null) || (rMin!=ampRMinMax[0]) || (rMax!=ampRMinMax[1])) {
				ampRMinMax[0]=rMin;
				ampRMinMax[1]=rMax;
				double rMin2=rMin*rMin;
				double rMax2=rMax*rMax;
				double r2;
				// pass 1 - count number of elements
				int numMembers=0;
				for (i=0;i<=size/2;i++) {
					y=i-(size/2);
					for (j=0;j<size;j++) {
						x=j-(size/2);
						r2=x*x+y*y;
						if ((r2>=rMin2) && (r2<=rMax2)) numMembers++;
					}
				}
				cartAmpList=new int [numMembers];
				// pass 2 - count number of elements fill in the array
				numMembers=0;
				for (i=0;i<=size/2;i++) {
					y=i-(size/2);
					for (j=0;j<size;j++) {
						x=j-(size/2);
						r2=x*x+y*y;
						if ((r2>=rMin2) && (r2<=rMax2)) cartAmpList[numMembers++]=i*size+j;
					}
				}
			}
			if (cartAmpList.length<1) return Double.NaN;
			double max=amps[cartAmpList[0]];
			for (i=1;i<cartAmpList.length;i++) if (max<amps[cartAmpList[i]]) max=amps[cartAmpList[i]];
			return max;
		}
		/* return polar array width (== radius+1) */
		public int getWidth() { return iRadiusPlus1; } 
		public int getHeight() { return iAngle+1; } 
		public double [] genPolarGreenMask(double [] polarAmps, // polar array of amplitude values, <0 - stop
				int mode){ // result mode - 0: output mask as 0/1, 1 -output proportional, positive - pass, negative - rejected
			return genPolarMask(polarAmps,0,mode);
		}

582 583 584 585 586
		public double [] genPolarRedBlueMask(
				double [] polarAmps, // polar array of amplitude values, <0 - stop
				int mode)
		{ // result mode - 0: output mask as 0/1, 1 -output proportional, positive - pass, negative - rejected
			return genPolarMask(polarAmps, 1, mode);
Andrey Filippov's avatar
Andrey Filippov committed
587 588
		}

589
// **** Seems to be most time-critical ****
Andrey Filippov's avatar
Andrey Filippov committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
		public double [] genPolarMask(double [] polarAmps, // polar array of amplitude values, <0 - stop
				int type, // 0 - green, 1 red/blue
				int mode){ // result mode - 0: output mask as 0/1, 1 -output proportional, positive - pass, negative - rejected
			int [][] polarMap=(type>0)?polarRedBlueMap: polarGreenMap;
			int length=iRadiusPlus1*(iAngle+1);
			int [] intMap= new int[length];
			int i,ia;
			for (i=0;i<intMap.length;i++) intMap[i]=0;
			int []    rayLength=new int[iAngle+1]; // index (radius)) of the current ray end for this angle
			boolean [] rayOpen= new boolean[iAngle+1]; // this ray can grow (not blocked)
			for (i=0;i<iAngle;i++) {
				rayLength[i]=0;
				rayOpen[i]=true;
			}
			int lastIndex;
			int base;
			int l;
			double max;
			int newVal;
			int step=0;
			int iMax=0; // index of the best ray
			int index=0;
			boolean good=true;
			while (iMax>=0) {
				step++;
				/* add polar point index */
				newVal=good?step:-step;
				intMap[index]=newVal;
618 619 620
				if (sameCartesian[index]!=null)
					for (i=0;i<sameCartesian[index].length;i++)
						intMap[sameCartesian[index][i]]=newVal;
Andrey Filippov's avatar
Andrey Filippov committed
621
				/* add aliases of point index (as negative values) */
622 623 624
				if ((good) &&(polarMap[index]!=null))
					for (i=0;i<polarMap[index].length;i++)
						intMap[polarMap[index][i]]=-step;
Andrey Filippov's avatar
Andrey Filippov committed
625 626 627 628 629 630 631 632 633
				/* update ray lengths and status */
				max=-1.0;
				iMax=-1;
				for  (ia=0;ia<=iAngle;ia++) if (rayOpen[ia]) {
					base=ia*iRadiusPlus1+1;  // second for this angle
					l=base+rayLength[ia]; // first after the pointer
					lastIndex=base+iRadiusPlus1; // first in the next row
					while ((l<lastIndex) && (intMap[l]>0)) l++;
					rayLength[ia]=l-base; // last "good" ( >0 and in the same row)
634 635 636 637 638
					if ((l==lastIndex) || (intMap[l]<0) || (polarAmps[l]<0.0) )
						rayOpen[ia]=false;
					else if (polarAmps[l]>max) {
						max=polarAmps[l];
						iMax=ia;
Andrey Filippov's avatar
Andrey Filippov committed
639
					}
640
					
Andrey Filippov's avatar
Andrey Filippov committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
				}
				if (iMax>=0) {
					rayLength[iMax]++;
					index=iMax*iRadiusPlus1+rayLength[iMax];
					/* See if any of the aliases of the new point  hit the positive value, then this point is prohibited (good=false). Otherwise add it with good=true */
					good=true;
					if (polarMap[index]!=null) for (i=0;i<polarMap[index].length;i++) {
						if (intMap[polarMap[index][i]]>0) {
							good=false;
							break;
						}
					}
				}
				/* index is set if (iMax>=0) */
			}
			double [] result=new double [intMap.length];
			if (mode==0) {
				for (i=0;i<length;i++) result[i]=(intMap[i]>0)?1.0:0.0;
			} else {
				for (i=0;i<length;i++) result[i]=(intMap[i]>0)?(step-intMap[i]):-(step+intMap[i]);
			}
			return result;
		}
664
		
Andrey Filippov's avatar
Andrey Filippov committed
665
		public PolarSpectrums(
666
				int isize, // size of the square array, center is at size/2, size/2, only top half+line will be used
Andrey Filippov's avatar
Andrey Filippov committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
				double fullAngle, // i.e. Math.PI, 2*Math.PI
				int    maxRadius, // width of the polar array - should be <= size/2-2
				double outerStep, // maximal step in pixels on the maxRadius for 1 angular step (i.e. 0.5)
				int         symm // angular symmetry - 0- none,1 - pi corresponds to integer, 2 - pi/2 corresponds to integer, n - pi/n corresponds to integer angular step     
				) {
			size= isize;
			length=size*size;
			if (maxRadius>(size/2-2)) maxRadius=(size/2-2);
			radius=maxRadius;
			if (symm==0) aStep=fullAngle/Math.ceil(fullAngle*radius/outerStep);
			else        aStep=Math.PI/symm/Math.ceil(Math.PI*radius/outerStep/symm);
			iRadiusPlus1=(int) Math.ceil(radius/outerStep)+1;
			rStep=radius/(iRadiusPlus1-1.0);
			iAngle=(int) Math.round(fullAngle/aStep);
			polar2CartesianIndices=  new int    [(iAngle+1)*iRadiusPlus1][4]; // [0] - closest one
			polar2CartesianFractions=new double [(iAngle+1)*iRadiusPlus1][2];
			int ia,ir,y,x,i,j; //,PolarIndex;
			double a,r,cos,sin,dy,dx;
			cartesian2PolarIndex=  new int[length];
			cartesian2PolarIndices=new int[length][];
			@SuppressWarnings("unchecked")
			HashSet  <Integer> [] polarList=  (HashSet  <Integer> []) new HashSet[length];
			for (i=0;i<length;i++) {
				polarList[i]=new HashSet <Integer>(); // 16, 0.75
			}
			Integer PolarIndex,CartesianIndex;
			for (ia=0;ia<=iAngle;ia++) {
				a=ia*aStep;
				cos=Math.cos(a);
				sin=Math.sin(a);
				for (ir=0;ir<iRadiusPlus1;ir++) {
					PolarIndex=ia*iRadiusPlus1+ir;
					r=ir*rStep;
					dy=r*sin;
					y=(int) Math.round(dy);
					dy-=y;
					i=size/2-y;
					dx=r*cos;
					x=(int) Math.round(dx);
					dx-=x;
					j=x+size/2;
					CartesianIndex=i*size+j;
					polar2CartesianIndices[PolarIndex][0]=CartesianIndex;
					polarList[CartesianIndex].add(PolarIndex);
					if (dx<0) {
						polar2CartesianIndices[PolarIndex][1]=polar2CartesianIndices[PolarIndex][0]-1;
						dx=-dx;
					} else {
						polar2CartesianIndices[PolarIndex][1]=polar2CartesianIndices[PolarIndex][0]+1;
					}
					if (dy<0) {
						polar2CartesianIndices[PolarIndex][2]=polar2CartesianIndices[PolarIndex][0]+size;
						polar2CartesianIndices[PolarIndex][3]=polar2CartesianIndices[PolarIndex][1]+size;
						dy=-dy;
					} else {
						polar2CartesianIndices[PolarIndex][2]=polar2CartesianIndices[PolarIndex][0]-size;
						polar2CartesianIndices[PolarIndex][3]=polar2CartesianIndices[PolarIndex][1]-size;
					}
					polar2CartesianFractions[PolarIndex][0]=dx;
					polar2CartesianFractions[PolarIndex][1]=dy;
				}
			}
			for (i=0;i<length;i++) {
				y=size/2-(i/size);
				x=(i % size)- size/2;
				r=Math.sqrt(x*x+y*y);
				a=Math.atan2(y,x);
				if (a<0) a+=2*Math.PI;
				ir =(int) Math.round(r/rStep);
				ia= (int) Math.round(a/aStep);
				if ((ir>=0) && (ir<iRadiusPlus1) && (ia>=0) && (ia<=iAngle)) {
					cartesian2PolarIndex[i]=ia*iRadiusPlus1+ir;  
					if (polarList[i].size()==0) cartesian2PolarIndices[i]=null;
					else {
						cartesian2PolarIndices[i]=new int[polarList[i].size()];
						j=0;
						for (Integer val : polarList[i]) cartesian2PolarIndices[i][j++]=val;
					}
				} else {
					cartesian2PolarIndex[i]=-1; // invalid  
					cartesian2PolarIndices[i]=null;
				}
			}
			initSameCartesian();
			polarGreenMap=  new int [iRadiusPlus1*(iAngle+1)][];
			initAliasMaps(0);
			polarRedBlueMap=new int [iRadiusPlus1*(iAngle+1)][];
			initAliasMaps(1);
		}

		public double [] testMapsLengths(int mode) { // 0 - return lengths of "sameCartesian[]", 1 - same for polarGreenMap
			int [][] map= (mode==0)?sameCartesian:((mode==1)?polarGreenMap:polarRedBlueMap);
			double [] result = new double [map.length];
			for (int i=0; i<map.length;i++) {
				result[i]=(map[i]==null)?0.0:map[i].length;
			}
			return result;
		}

		public double [] testGreenMap(int ia, int ir) {
			double [] result = new double [polarGreenMap.length];
			int i;
			for ( i=0; i<result.length;i++) result[i]=0.0;
			int index=ia*iRadiusPlus1+ir;
			if (polarGreenMap[index]!=null){
				for (i=0;i<polarGreenMap[index].length;i++) result [polarGreenMap[index][i]]+=1.0;
				System.out.println("testGreenMap("+ia+","+ir+"): polarGreenMap["+index+"].length="+polarGreenMap[index].length);
			} else {
				System.out.println("testGreenMap("+ia+","+ir+"): polarGreenMap["+index+"]=null");
			}
			result [index]=-1.0;
			return result;
		}

		public double [] testRedBlueMap(int ia, int ir) {
			double [] result = new double [polarRedBlueMap.length];
			int i;
			for ( i=0; i<result.length;i++) result[i]=0.0;
			int index=ia*iRadiusPlus1+ir;
			if (polarRedBlueMap[index]!=null){
				for (i=0;i<polarRedBlueMap[index].length;i++) result [polarRedBlueMap[index][i]]+=1.0;
				System.out.println("testRedBlueMap("+ia+","+ir+"): polarRedBlueMap["+index+"].length="+polarRedBlueMap[index].length);
			} else {
				System.out.println("testRedBlueMap("+ia+","+ir+"): polarRedBlueMap["+index+"]=null");
			}
			result [index]=-1.0;
			return result;
		}


		/* Create per-polar pixel list of aliases for green Bayer. For each polar point it shows the polar coordinates of the same (and rotated by pi) point of aliases */
		/* current implementation - us cartesian (original) pixels as all/nothing, maybe it makes sense to go directly polar-polar, but then it may leave gaps */
		public void initAliasMaps (int type) { // 0 - green, 1 - Red/Blue
			int [][] aliasMapGreen=  {{-2,-2},{-2,0},            // using rollover, so only unique aliases are needed
					{-1,-1},{-1,1},
					{ 0,-2},
					{ 1,-1},{ 1,1}};
			int [][] aliasMapRedBlue={{-2,-2},{-2,-1},{-2,0},{-2,1},
					{-1,-2},{-1,-1},{-1,0},{-1,1},
					{ 0,-2},{ 0,-1},       { 0,1},
					{ 1,-2},{ 1,-1},{ 1,0},{ 1,1}};
			int [][] aliasMap=(type>0)?aliasMapRedBlue:aliasMapGreen;
			int [][] polarMap=(type>0)?polarRedBlueMap: polarGreenMap;
			HashSet  <Integer> aliasList=new HashSet <Integer>();
			int j,ix,iy, nAlias, dirAlias,ixa,iya, index, polarIndex;
			for (polarIndex=0;polarIndex<polarMap.length;polarIndex++) {
				iy= size/2- (polar2CartesianIndices[polarIndex][0] / size);
				ix= (polar2CartesianIndices[polarIndex][0] % size)-size/2 ;
				aliasList.clear();
				for (nAlias=0;nAlias<aliasMap.length;nAlias++) for (dirAlias=-1;dirAlias<2;dirAlias+=2) {
					ixa=(size+ size/2+ aliasMap[nAlias][0]*size/4+ dirAlias*ix) % size;
					iya=(size+ size/2- aliasMap[nAlias][1]*size/4- dirAlias*iy) % size;
					index=iya*size + ixa;
					if (cartesian2PolarIndices[index]==null) {
						if (cartesian2PolarIndex[index]>=0) {
							aliasList.add (new Integer(cartesian2PolarIndex[index]));
						}
					} else {
						for (j=0;j<cartesian2PolarIndices[index].length;j++) {
							aliasList.add (new Integer(cartesian2PolarIndices[index][j]));
						}
					}
				}
				/*  convert set to int[] */
				if (aliasList.size()==0) polarMap[polarIndex]=null;
				else {
					polarMap[polarIndex]=new int[aliasList.size()];
					j=0;
					for (Integer val : aliasList) polarMap[polarIndex][j++]=val;
				}
			}
		}

		public void initSameCartesian () {
			int i,j, polarIndex, cartesianIndex;
			sameCartesian=new int [iRadiusPlus1*(iAngle+1)][];
			for (polarIndex=0;polarIndex<sameCartesian.length;polarIndex++) {
				cartesianIndex=polar2CartesianIndices[polarIndex][0];
				if ((cartesian2PolarIndices[cartesianIndex]==null) || (cartesian2PolarIndices[cartesianIndex].length<=1)) sameCartesian[polarIndex]=null;
				else {
					sameCartesian[polarIndex]=new int [cartesian2PolarIndices[cartesianIndex].length-1];
					j=0;
					/* copy all elements but this one - out of bounds may mean that it was not included - bug */
					for (i=0;i<cartesian2PolarIndices[cartesianIndex].length;i++) if (cartesian2PolarIndices[cartesianIndex][i]!=polarIndex) sameCartesian[polarIndex][j++]=cartesian2PolarIndices[cartesianIndex][i];
				}
			}
		}
	}
Andrey Filippov's avatar
Andrey Filippov committed
855 856 857

}