JP46_Reader_camera.java 47.1 KB
Newer Older
Andrey Filippov's avatar
Andrey Filippov committed
1 2 3 4 5 6 7 8 9
/**
 ** -----------------------------------------------------------------------------**
 ** JP46_Reader_camera.java
 **
 ** Reads Elphel Camera JP46 files into ImageJ, un-applying gamma and gains
 **
 ** Copyright (C) 2010 Elphel, Inc.
 **
 ** -----------------------------------------------------------------------------**
10
 **
Andrey Filippov's avatar
Andrey Filippov committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 **  JP46_Reader.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/>.
 ** -----------------------------------------------------------------------------**
 **
 */

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
Andrey Filippov's avatar
Andrey Filippov committed
45 46 47 48
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

49
import javax.swing.JFileChooser;
Andrey Filippov's avatar
Andrey Filippov committed
50 51 52 53 54 55 56 57 58
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.ImageStack;
import ij.Prefs;
import ij.WindowManager;
import ij.gui.GUI;
import ij.gui.GenericDialog;
import ij.io.FileInfo;
import ij.io.OpenDialog;
import ij.plugin.frame.PlugInFrame;
import ij.process.ImageConverter;
import ij.process.ImageProcessor;
import ij.text.TextWindow;

Andrey Filippov's avatar
Andrey Filippov committed
74 75


Andrey Filippov's avatar
Andrey Filippov committed
76
/* This plugin opens images in Elphel JP4/JP46 format (opens as JPEG, reads MakerNote and converts). */
Andrey Filippov's avatar
Andrey Filippov committed
77 78 79
public class JP46_Reader_camera extends PlugInFrame implements ActionListener {

	/**
80
	 *
Andrey Filippov's avatar
Andrey Filippov committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	 */
	private static final long serialVersionUID = 390855361964415147L;
	Panel panel1;
	Panel confpanel;
	Frame instance;

	String arg;

	static File dir;

	public String camera_url = "http://192.168.0.236:8081/";
	public String camera_img = "bimg";
	public String camera_img_new = "towp/wait/bimg"; // will always wait for the next image (repetitive acquisitions get new images)
	public String camera_jp46settings = "";
	public boolean IS_SILENT=true;
	public boolean ABSOLUTELY_SILENT=false;
        public boolean demux=true;
	public String imageTitle="cameraImage";
	private int ExifOffset=0x0c;

	public JP46_Reader_camera() {
		super("JP46 Reader Camera");
		if (IJ.versionLessThan("1.39t")) return;
		if (instance!=null) {
			instance.toFront();
			return;
		}
		instance = this;
		addKeyListener(IJ.getInstance());

		panel1 = new Panel();

		panel1.setLayout(new GridLayout(6, 1, 50, 5));

115
		addButton("Open JP4/JP46...",panel1);
Andrey Filippov's avatar
Andrey Filippov committed
116 117 118 119 120
		addButton("Open JP4/JP46 from camera",panel1);
		addButton("Configure...",panel1);
		addButton("Show image properties",panel1);
		addButton("Decode image info to properties",panel1);
		addButton("Split Bayer",panel1);
121

Andrey Filippov's avatar
Andrey Filippov committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

		add(panel1);

		pack();
		GUI.center(this);
		setVisible(true);
	}
	public JP46_Reader_camera(boolean showGUI) {
		super("JP46 Reader Camera");
		if (IJ.versionLessThan("1.39t")) return;
		if (instance!=null) {
			instance.toFront();
			return;
		}
		instance = this;
		addKeyListener(IJ.getInstance());

		panel1 = new Panel();

		panel1.setLayout(new GridLayout(6, 1, 50, 5));

143
		addButton("Open JP4/JP46...",panel1);
Andrey Filippov's avatar
Andrey Filippov committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
		addButton("Open JP4/JP46 from camera",panel1);
		addButton("Configure...",panel1);
		addButton("Show image properties",panel1);
		addButton("Decode image info to properties",panel1);
		addButton("Split Bayer",panel1);
		add(panel1);
		pack();
		GUI.center(this);
		setVisible(showGUI);
	}

	void addButton(String label, Panel panel) {
		Button b = new Button(label);
		b.addActionListener(this);
		b.addKeyListener(IJ.getInstance());
		panel.add(b);
	}

162
	@Override
Andrey Filippov's avatar
Andrey Filippov committed
163 164 165
	public void actionPerformed(ActionEvent e) {
		String label = e.getActionCommand();

Andrey Filippov's avatar
Andrey Filippov committed
166
		/* nothing */
Andrey Filippov's avatar
Andrey Filippov committed
167 168
		if (label==null) return;

Andrey Filippov's avatar
Andrey Filippov committed
169
		/* button */
170
		if (label.equals("Open JP4/JP46...")) {
Andrey Filippov's avatar
Andrey Filippov committed
171 172 173 174 175 176
			read_jp46(arg,true);
		}else if (label.equals("Open JP4/JP46 (no scale)...")) {
			read_jp46(arg,false);
		}else if (label.equals("Configure...")) {
			showConfigDialog(); // open configure dialog
		}else if (label.equals("Open JP4/JP46 from camera")) {
177
			openURL(camera_url + camera_img_new + camera_jp46settings, arg, true);
Andrey Filippov's avatar
Andrey Filippov committed
178
		}else if (label.equals("Open JP4/JP46 from camera (no scale)")) {
179
			openURL(camera_url + camera_img_new + camera_jp46settings, arg, false);
Andrey Filippov's avatar
Andrey Filippov committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		}else if (label.equals("Show image properties")) {
			ImagePlus imp_sel = WindowManager.getCurrentImage();
			if (imp_sel==null){
				IJ.showMessage("Error","No images selected");
				return;
			}
			listImageProperties (imp_sel);
		}
		else if (label.equals("Decode image info to properties")) {
			ImagePlus imp_sel = WindowManager.getCurrentImage();
			if (imp_sel==null){
				IJ.showMessage("Error","No images selected");
				return;
			}
			decodeProperiesFromInfo(imp_sel);
			listImageProperties (imp_sel);
		}
		else if (label.equals("Split Bayer")) {
			ImagePlus imp_sel = WindowManager.getCurrentImage();
			if (imp_sel==null){
				IJ.showMessage("Error","No images selected");
				return;
			}
			splitShowBayer(imp_sel);
		}
		IJ.showStatus("DONE");
	}
    public void splitShowBayer(ImagePlus imp){
    	float [] pixels= (float[]) imp.getProcessor().getPixels();
    	int height=imp.getHeight();
    	int width= imp.getWidth();
    	int halfHeight=height/2;
    	int halfWidth=width/2;
        float [][] bayerPixels=new float[4][halfHeight * halfWidth];
        for (int iy=0;iy<halfHeight;iy++) for (int ix=0;ix<halfWidth;ix++){
        	int oIndex=iy*halfWidth+ix;
        	int iIndex=iy*2*width+ix*2;
        	bayerPixels[0][oIndex]=pixels[iIndex];
        	bayerPixels[1][oIndex]=pixels[iIndex+1];
        	bayerPixels[2][oIndex]=pixels[iIndex+width];
        	bayerPixels[3][oIndex]=pixels[iIndex+width+1];
        }
        ImageStack array_stack=new ImageStack(halfWidth,halfHeight);
        for (int i=0;i<4;i++) array_stack.addSlice("chn-"+i, bayerPixels[i]);
        ImagePlus imp_stack = new ImagePlus(imp.getTitle()+"-BAYER", array_stack);
        imp_stack.getProcessor().resetMinAndMax();
        imp_stack.show();
    }
228

Andrey Filippov's avatar
Andrey Filippov committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	public void read_jp46(String arg, boolean scale) {
		JFileChooser fc=null;
		//try {fc = new JFileChooser();}

		fc = new JFileChooser();

		//catch (Throwable e) {IJ.error("This plugin requires Java 2 or Swing."); return;}
		fc.setMultiSelectionEnabled(true);

		if (dir==null) {
			String sdir = OpenDialog.getDefaultDirectory();
			if (sdir!=null)
				dir = new File(sdir);
		}
		if (dir!=null)
			fc.setCurrentDirectory(dir);

		int returnVal = fc.showOpenDialog(IJ.getInstance());
		if (returnVal!=JFileChooser.APPROVE_OPTION)
			return;
		File[] files = fc.getSelectedFiles();
		if (files.length==0) { // getSelectedFiles does not work on some JVMs
			files = new File[1];
			files[0] = fc.getSelectedFile();
		}
		String path = fc.getCurrentDirectory().getPath()+Prefs.getFileSeparator();
		dir = fc.getCurrentDirectory();
		for (int i=0; i<files.length; i++) {
			open(path, files[i].getName(), arg, scale);
		}
	}

	public boolean showConfigDialog() {
		GenericDialog gd = new GenericDialog("Configure");
		gd.addStringField ("Image title:    ", getTitle(), 20);
		gd.addStringField("Camera Address: ", getURL(), 20);
		gd.addStringField("Image: ",       camera_img, 20);
		gd.addStringField("Image (new): ", camera_img_new, 20);
		gd.addStringField("JP46 Parameters: ", camera_jp46settings, 50);
		gd.addCheckbox("Demux composite frame? ", demux);
		gd.addCheckbox("Silent? ", IS_SILENT);
		//      gd.addCheckbox("JP4 (not JP46)? ", IS_JP4);


		confpanel = new Panel();
		gd.addPanel(confpanel);
275
		addButton("Open JP4/JP46 (no scale)...", confpanel);
Andrey Filippov's avatar
Andrey Filippov committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		addButton("Open JP4/JP46 from camera (no scale)", confpanel);

		//Vector textfields = gd.getStringFields();
		//((TextField)fields.elementAt(0)).SetWidth = 20;

		gd.showDialog();
		if (gd.wasCanceled()) return false;
		setTitle(gd.getNextString());
		setURL  (gd.getNextString());
		camera_img = gd.getNextString();
		camera_img_new = gd.getNextString();
		camera_jp46settings = gd.getNextString();
		demux=gd.getNextBoolean();
		IS_SILENT=gd.getNextBoolean();
		return true;
	}
	public ImagePlus open(String directory, String fileName, String arg, boolean scale) {
		return open(directory, fileName, arg, scale, null,true);
	}
	public ImagePlus open(String directory, String fileName, String arg, boolean scale, ImagePlus imp_src) {
		return open(directory, fileName, arg, scale, imp_src,true);
	}


	public ImagePlus open(
			String directory,
			String fileName,
			String arg,
			boolean scale,
			ImagePlus imp_src,
			boolean showImage) {
		long[] ElphelMakerNote=null;
		ImagePlus imp = null;
		boolean reuse_imp=false;
		boolean showDemux=showImage && demux;
		if (demux) showImage=false;
		double [] xtraExif=new double[1]; // ExposureTime
		try {
			imp = openJpegOrGif(directory, fileName);
			if (imp == null) {
				IJ.showMessage("JP46 Reader Error", "Could not open "+directory+"" + fileName + " as JPEG/JP46");
			} else {
Andrey Filippov's avatar
Andrey Filippov committed
318 319 320 321 322
				if ((imp_src==null)&& showImage) imp.show(); /* Shows before re-ordering*/
				ElphelMakerNote = readElphelMakerNote(directory, fileName, 16,xtraExif); /* after or 8.2.2 */
				if (ElphelMakerNote==null) ElphelMakerNote = readElphelMakerNote(directory, fileName, 14,xtraExif); /* after or 8.0.8.32 */
				if (ElphelMakerNote==null) ElphelMakerNote = readElphelMakerNote(directory, fileName, 12,xtraExif); /* after or 8.0.7.3 */
				if (ElphelMakerNote==null) ElphelMakerNote = readElphelMakerNote(directory, fileName, 8 ,xtraExif); /* before 8.0.7.3 */
Andrey Filippov's avatar
Andrey Filippov committed
323 324 325 326 327 328 329 330 331 332 333 334
			}
		} catch (IOException e) {
			IJ.showStatus("");
			String error = e.getMessage();
			if (error==null || error.equals("")) error = ""+e;
			IJ.showMessage("JP46 Reader", ""+error);
			return null;
		}
		if (imp!=null) {
			reuse_imp=jp46Reorder(imp, ElphelMakerNote, scale, imp_src);
			if (reuse_imp) {
				imp=imp_src;
Andrey Filippov's avatar
Andrey Filippov committed
335
			} else if ((imp_src!=null)&& showImage) { /* tried to reuse, but wrong size */
336
				imp.show(); /* never did that before */
Andrey Filippov's avatar
Andrey Filippov committed
337 338 339 340 341
			}
			if ((xtraExif!=null) && !Double.isNaN(xtraExif[0])){
				imp.setProperty("EXPOSURE",  String.format("%f",xtraExif[0]));

			}
Andrey Filippov's avatar
Andrey Filippov committed
342
			if (showImage) imp.updateAndDraw(); /* Redisplays final image*/
Andrey Filippov's avatar
Andrey Filippov committed
343 344 345 346 347 348 349 350 351 352 353

			if (showDemux) {
				if (!this.ABSOLUTELY_SILENT) System.out.println("demuxing...");
				ImagePlus imp_0 = demuxImage(imp, 0); if (imp_0!=null) imp_0.show();
				ImagePlus imp_1 = demuxImage(imp, 1); if (imp_1!=null) imp_1.show();
				ImagePlus imp_2 = demuxImage(imp, 2); if (imp_2!=null) imp_2.show();
				if ((imp_0==null) && (imp_0==null) && (imp_0==null)) imp.show(); // Show original image if demux failed (single original)
			}

			return imp;
		}
354
		return null;
Andrey Filippov's avatar
Andrey Filippov committed
355 356 357 358
	}

	public ImagePlus openURL(ImagePlus imp_src) {
		if (imp_src==null) return openURL(camera_url + camera_img_new + camera_jp46settings, arg, true);
359
		return openURL(camera_url + camera_img_new + camera_jp46settings, arg, true, imp_src,true);
Andrey Filippov's avatar
Andrey Filippov committed
360 361 362
	}

	public ImagePlus openURL() {
363
		return openURL(camera_url + camera_img_new + camera_jp46settings, arg, true);
Andrey Filippov's avatar
Andrey Filippov committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	}

	public ImagePlus openURL(String url, String arg, boolean scale) {
		return openURL(url, arg, scale, null,true);
	}

	public ImagePlus openURL(
			String url,
			String arg,
			boolean scale,
			ImagePlus imp_src,
			boolean showImage) {
		long[] ElphelMakerNote=null;
		ImagePlus imp = null;
		boolean reuse_imp=false;
		boolean showDemux=showImage && demux;
		if (demux) showImage=false;
		double [] xtraExif=new double[1]; // ExposureTime
382

Andrey Filippov's avatar
Andrey Filippov committed
383 384 385 386 387 388 389 390 391

//		System.out.println("imp_src is "+((imp_src!=null)?"not ":"")+"null");
		try {
			imp = openJpegOrGifUsingURL(url);
			if (imp == null) {
				IJ.showMessage("JP46 Reader Error", "Could not open the URL: " + url + " as JPEG/JP46");
			} else {
				if ((imp_src==null) && showImage) {
//					System.out.println("show() 1");
Andrey Filippov's avatar
Andrey Filippov committed
392
					imp.show(); /* Shows before re-ordering*/
Andrey Filippov's avatar
Andrey Filippov committed
393 394
				}
				/// get rid of the "/towp/wait" if any - there is a chance to re-read the same image
Andrey Filippov's avatar
Andrey Filippov committed
395 396 397 398
				ElphelMakerNote = readElphelMakerNoteURL(url.replaceFirst("/towp/wait",""), 16,xtraExif); /* after or 8.2.2 */
				if (ElphelMakerNote==null) ElphelMakerNote = readElphelMakerNoteURL(url.replaceFirst("/towp/wait",""), 14,xtraExif); /* after or 8.0.8.32 */
				if (ElphelMakerNote==null) ElphelMakerNote = readElphelMakerNoteURL(url.replaceFirst("/towp/wait",""), 12,xtraExif); /* after or 8.0.7.3 */
				if (ElphelMakerNote==null) ElphelMakerNote = readElphelMakerNoteURL(url.replaceFirst("/towp/wait",""), 8 ,xtraExif ); /* before 8.0.7.3 */
Andrey Filippov's avatar
Andrey Filippov committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412

			}
		} catch (IOException e) {
			IJ.showStatus("");
			String error = e.getMessage();
			if (error==null || error.equals(""))
				error = ""+e;
			IJ.showMessage("JP46 Reader", ""+error);
			return null;
		}
		if (imp!=null) {
			reuse_imp=jp46Reorder(imp, ElphelMakerNote, scale, imp_src);
			if (reuse_imp) {
				imp=imp_src;
Andrey Filippov's avatar
Andrey Filippov committed
413
			} else if ((imp_src!=null) && showImage) { /* tried to reuse, but wrong size */
Andrey Filippov's avatar
Andrey Filippov committed
414
//				System.out.println("show() 2");
415
				imp.show(); /* never did that before */
Andrey Filippov's avatar
Andrey Filippov committed
416 417 418 419
			}
			if ((xtraExif!=null) && !Double.isNaN(xtraExif[0])){
				imp.setProperty("EXPOSURE",  String.format("%f",xtraExif[0]));
			}
Andrey Filippov's avatar
Andrey Filippov committed
420
			if (showImage) imp.updateAndDraw(); /* Redisplays final image*/
Andrey Filippov's avatar
Andrey Filippov committed
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
			if (showDemux) {
				if (!this.ABSOLUTELY_SILENT) System.out.println("demuxing...");
				ImagePlus imp_0 = demuxImage(imp, 0); if (imp_0!=null) imp_0.show();
				ImagePlus imp_1 = demuxImage(imp, 1); if (imp_1!=null) imp_1.show();
				ImagePlus imp_2 = demuxImage(imp, 2); if (imp_2!=null) imp_2.show();
				if ((imp_0==null) && (imp_0==null) && (imp_0==null)) imp.show(); // Show original image if demux failed (single original)
			}

			return imp;
		}
		return null;
	}

	boolean jp46Reorder(ImagePlus imp, long[] MakerNote, boolean scale) {
		return jp46Reorder(imp, MakerNote, scale, null);
	}

	void swapArrayElements (double[]arr,int i, int j) {
		double tmp=arr[i];
		arr[i]=arr[j];
		arr[j]=tmp;
	}
	void swapArrayElements (long[]arr,int i, int j) {
		long tmp=arr[i];
		arr[i]=arr[j];
		arr[j]=tmp;
	}
	boolean  jp46Reorder(ImagePlus imp, long[] MakerNote, boolean scale, ImagePlus imp_src) {
		//    int MARGIN=2; // 2 pixels in JP4/JP46 mode around WOI
		double[] gains= new double[4];
		double[] blacks= new double[4];
		double[] blacks256= new double[4];
		double[] gammas= new double[4];
454
		long []   gamma_scales= new long[4]; /* now not used, was scale _after_ gamma is applied, 0x400(default) corresponds to 1.0 */
Andrey Filippov's avatar
Andrey Filippov committed
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
		int i;
		double[][] rgammas=new double[4][];
		double min_gain;
		long WOI_LEFT,WOI_WIDTH,WOI_TOP,WOI_HEIGHT,BAYER_MODE,DCM_HOR,DCM_VERT,BIN_HOR,BIN_VERT;
		long COLOR_MODE=0;
		long FLIPH=0;
		long FLIPV=0;
		long  HEIGHT1=0;
		long  HEIGHT2=0;
		long  HEIGHT3=0;
		long  BLANK1=0;
		long  BLANK2=0;
		boolean FLIPH1=false;
		boolean FLIPV1=false;
		boolean FLIPH2=false;
		boolean FLIPV2=false;
		boolean FLIPH3=false;
		boolean FLIPV3=false;
		boolean COMPOSITE=false;
		boolean PORTRAIT=false;
		boolean YTABLEFORC=false;
		long     QUALITY=0;
		long     CQUALITY=0;
		long     CORING_INDEX_Y=0;
		long     CORING_INDEX_C=0;
		double []   satValue={255.0, 255.0, 255.0, 255.0};
		if  (MakerNote !=null) {
Andrey Filippov's avatar
Andrey Filippov committed
482
			for (i=0;i<4;i++) { /* r,g,gb,b */
Andrey Filippov's avatar
Andrey Filippov committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 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 582 583 584 585 586 587 588 589
				gains[i]= MakerNote[i]/65536.0;
				blacks[i]=(MakerNote[i+4]>>24)/256.0;
				gammas[i]=((MakerNote[i+4]>>16)&0xff)/100.0;
				gamma_scales[i]=MakerNote[i+4] & 0xffff;
				imp.setProperty("gains_"+i,String.format("%f",gains[i]));
				imp.setProperty("blacks_"+i,String.format("%f",blacks[i]));
				imp.setProperty("gammas_"+i,String.format("%f",gammas[i]));
				imp.setProperty("gamma_scales_"+i,String.format("%d",gamma_scales[i]));
			}
			IJ.showStatus("R=" +(0.001*((int)(1000*gains[0])))+
					" G=" +IJ.d2s(gains[1],3)+
					" Gb="+IJ.d2s(gains[2],3)+
					" B=" +IJ.d2s(gains[3],3)+
					" Gamma[0]="+IJ.d2s(gammas[0],3)+
					" Black[0]="+((int) (256*blacks[0])));
			String info=new String();
			info+="Gain\t"+ IJ.d2s(gains[0],3) + "\t"+ IJ.d2s(gains[1],3) + "\t"+ IJ.d2s(gains[2],3) + "\t"+ IJ.d2s(gains[3],3) + "\n"+
			"Gamma\t"+IJ.d2s(gammas[0],3) + "\t"+IJ.d2s(gammas[1],3) + "\t"+IJ.d2s(gammas[2],3) + "\t"+IJ.d2s(gammas[3],3) + "\n"+
			"Black\t"+IJ.d2s(blacks[0],3) + "\t"+IJ.d2s(blacks[1],3) + "\t"+IJ.d2s(blacks[2],3) + "\t"+IJ.d2s(blacks[3],3) + "\n";
			if (MakerNote.length>=14) {
				COMPOSITE= ((MakerNote[10] & 0xc0000000)!=0);
				if (COMPOSITE) {
					HEIGHT1= MakerNote[11] & 0xffff;
					BLANK1= (MakerNote[11]>>16) & 0xffff;
					HEIGHT2= MakerNote[12] & 0xffff;
					BLANK2= (MakerNote[12]>>16) & 0xffff;
					//           HEIGHT3=(( MakerNote[9]>>16)+2*MARGIN) - HEIGHT1-BLANK1-HEIGHT2-BLANK2;
					HEIGHT3=( MakerNote[9]>>16) - HEIGHT1-BLANK1-HEIGHT2-BLANK2;

					FLIPH1= (((MakerNote[10] >> 24) & 1)!=0); // Same value as FLIP_H
					FLIPV1= (((MakerNote[10] >> 25) & 1)!=0); // Same value as FLIP_V
					FLIPH2= (((MakerNote[10] >> 26) & 1)!=0);
					FLIPV2= (((MakerNote[10] >> 27) & 1)!=0);
					FLIPH3= (((MakerNote[10] >> 28) & 1)!=0);
					FLIPV3= (((MakerNote[10] >> 29) & 1)!=0);
				}
				PORTRAIT=  (((MakerNote[13] >>  7) & 1)!=0);
				YTABLEFORC=(((MakerNote[13] >> 15) & 1)!=0);
				QUALITY=   (MakerNote[13] & 0x7f);
				CQUALITY=  ((MakerNote[13]>>8) & 0x7f);
				if (CQUALITY==0) CQUALITY=QUALITY;
				CORING_INDEX_Y= ((MakerNote[13]>>16) & 0x7f);
				CORING_INDEX_C= ((MakerNote[13]>>24) & 0x7f);
				if (CORING_INDEX_C==0) CORING_INDEX_C=CORING_INDEX_Y;

			}
			if (MakerNote.length>=12) {
				WOI_LEFT=   MakerNote[8]&0xffff;
				WOI_WIDTH=  MakerNote[8]>>16;
				WOI_TOP=    MakerNote[9]&0xffff;
				WOI_HEIGHT= MakerNote[9]>>16;
				FLIPH=      MakerNote[10]      & 1;
				FLIPV=     (MakerNote[10]>> 1) & 1;
				BAYER_MODE=(MakerNote[10]>> 2) & 3;
				COLOR_MODE=(MakerNote[10]>> 4) & 0x0f;
				DCM_HOR=   (MakerNote[10]>> 8) & 0x0f;
				DCM_VERT=  (MakerNote[10]>>12) & 0x0f;
				BIN_HOR=   (MakerNote[10]>>16) & 0x0f;
				BIN_VERT=  (MakerNote[10]>>20) & 0x0f;
				info+="WOI_LEFT\t" +  WOI_LEFT+"\t\t\t\n"+
				"WOI_WIDTH\t"+  WOI_WIDTH+"\t\t\t\n"+
				"WOI_TOP\t"  +  WOI_TOP+"\t\t\t\n"+
				"WOI_HEIGHT\t"+ WOI_HEIGHT+"\t\t\t\n"+
				"FLIP_HOR\t"+   (FLIPH!=0)+"\t\t\t\n"+
				"FLIP_VERT\t"+  (FLIPV!=0)+"\t\t\t\n"+
				"BAYER_MODE\t"+ BAYER_MODE+"\t\t\t\n"+
				"COLOR_MODE\t"+ COLOR_MODE+"\t"+ ((COLOR_MODE==2)?"JP46":((COLOR_MODE==5)?"JP4":((COLOR_MODE==0)?"MONO":"OTHER")))  +"\t\t\n"+
				"DECIM_HOR\t"+  DCM_HOR+"\t\t\t\n"+
				"DECIM_VERT\t"+ DCM_VERT+"\t\t\t\n"+
				"BIN_HOR\t"+    BIN_HOR+"\t\t\t\n"+
				"BIN_VERT\t"+   BIN_VERT+"\t\t\t\n";
				imp.setProperty("WOI_LEFT",  String.format("%d",WOI_LEFT));
				imp.setProperty("WOI_WIDTH", String.format("%d",WOI_WIDTH));
				imp.setProperty("WOI_TOP",   String.format("%d",WOI_TOP));
				imp.setProperty("WOI_HEIGHT",String.format("%d",WOI_HEIGHT));
				imp.setProperty("FLIPH",     String.format("%d",FLIPH));
				imp.setProperty("FLIPV",     String.format("%d",FLIPV));
				imp.setProperty("BAYER_MODE",String.format("%d",BAYER_MODE));
				imp.setProperty("COLOR_MODE",((COLOR_MODE==2)?"JP46":((COLOR_MODE==5)?"JP4":((COLOR_MODE==0)?"MONO":"OTHER"))));
				imp.setProperty("DCM_HOR",   String.format("%d",DCM_HOR));
				imp.setProperty("DCM_VERT",  String.format("%d",DCM_VERT));
				imp.setProperty("BIN_HOR",   String.format("%d",BIN_HOR));
				imp.setProperty("BIN_VERT",  String.format("%d",BIN_VERT));

			}
			if (MakerNote.length>=14) {
				info+="COMPOSITE\t" +  COMPOSITE+"\t\t\t\n";
				info+="ORIENTATION\t" +  (PORTRAIT?"PORTRAIT":"LANDSCAPE" )+"\t\t\t\n";
				info+="JPEG quality\t" +  QUALITY+"\t"+((CQUALITY!=QUALITY)?("("+CQUALITY+")"):"")+"\t"+(YTABLEFORC? "Color use Y table":"")+"\t\n";
				info+="Coring index\t" +  CORING_INDEX_Y+"\t"+((CORING_INDEX_C!=CORING_INDEX_Y)?("("+CORING_INDEX_C+")"):"")+"\t\t\n";
				imp.setProperty("COMPOSITE",String.format("%d",COMPOSITE?1:0));
				imp.setProperty("ORIENTATION",(PORTRAIT?"PORTRAIT":"LANDSCAPE" ));
				imp.setProperty("QUALITY",String.format("%d",QUALITY)); //not full
				imp.setProperty("CORING_INDEX_Y",String.format("%d",CORING_INDEX_Y));
				imp.setProperty("CORING_INDEX_C",String.format("%d",CORING_INDEX_C));
			}
			if (MakerNote.length>=16) {
				long [] iTemps={
						(MakerNote[14]>> 0) & 0xffff,
						(MakerNote[14]>>16) & 0xffff,
						(MakerNote[15]>> 0) & 0xffff,
						(MakerNote[15]>>16) & 0xffff};
				for (i=0;i<iTemps.length;i++) if (iTemps[i]!=0xffff){
					double temperature=(iTemps[i]&0xfff)/16.0;
					if (i==0) info+="SYSTEM TEMPERATURE\t" +  temperature+"\t\t\t\n";
					else  info+="SFE "+i+" TEMPERATURE\t" +  temperature+"\t\t\t\n";
					imp.setProperty("TEMPERATURE_"+i,""+temperature);
590

Andrey Filippov's avatar
Andrey Filippov committed
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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
				}
			}
			if (COMPOSITE) {
				info+="SUB_FRAMES\t- 1 -\t- 2 -\t- 3 -\t\n";
				info+="HEIGHTS\t"+HEIGHT1+"\t"+HEIGHT2+"\t"+HEIGHT3+"\t\n";
				info+="BLANK_ROWS\t" +BLANK1+"\t"+BLANK2+"\t\t\n";
				info+="FLIPH\t"+FLIPH1+"\t"+FLIPH2+"\t"+FLIPH3+"\t\n";
				info+="FLIPV\t"+FLIPV1+"\t"+FLIPV2+"\t"+FLIPV3+"\t\n";

				imp.setProperty("HEIGHT1",String.format("%d",HEIGHT1));
				imp.setProperty("HEIGHT2",String.format("%d",HEIGHT2));
				imp.setProperty("HEIGHT3",String.format("%d",HEIGHT3));
				imp.setProperty("BLANK_ROWS1",String.format("%d",BLANK1));
				imp.setProperty("BLANK_ROWS2",String.format("%d",BLANK2));
				imp.setProperty("FLIPH1",FLIPH1?"1":"0");
				imp.setProperty("FLIPH2",FLIPH2?"1":"0");
				imp.setProperty("FLIPH3",FLIPH3?"1":"0");
				imp.setProperty("FLIPV1",FLIPV1?"1":"0");
				imp.setProperty("FLIPV2",FLIPV2?"1":"0");
				imp.setProperty("FLIPV3",FLIPV3?"1":"0");
			}
			if (!IS_SILENT) new TextWindow(imp.getTitle()+" info", "Parameter\tRed\tGreen(R)\tGreen(B)\tBlue",info, 400, COMPOSITE?(600):((MakerNote.length>=12)?600:160));

			//tw.setLocation(0,0);
// If there are FLIPH, FLIPV - swap gains, gammas, blacks accordingly. later the images will be also flipped
			if (FLIPV!=0) {
				swapArrayElements (gains,       1, 3);
				swapArrayElements (gains,       0, 2);
				swapArrayElements (blacks,      1, 3);
				swapArrayElements (blacks,      0, 2);
				swapArrayElements (gammas,      1, 3);
				swapArrayElements (gammas,      0, 2);
				swapArrayElements (gamma_scales,1, 3);
				swapArrayElements (gamma_scales,0, 2);
			}
			if (FLIPH!=0) {
				swapArrayElements (gains,       1, 0);
				swapArrayElements (gains,       3, 2);
				swapArrayElements (blacks,      1, 0);
				swapArrayElements (blacks,      3, 2);
				swapArrayElements (gammas,      1, 0);
				swapArrayElements (gammas,      3, 2);
				swapArrayElements (gamma_scales,1, 0);
				swapArrayElements (gamma_scales,3, 2);
			}
			for (i=0;i<4;i++) rgammas[i]=elphel_gamma_calc (gammas[i], blacks[i], gamma_scales[i]);
		} else {
			IJ.showMessage("WARNING", "MakerNote not found");
		}
		/**adjusting gains to have the result picture in the range 0..256 */
		min_gain=2.0*gains[0];
		for (i=0;i<4;i++) {
			if (min_gain > gains[i]*(1.0-blacks[i])) min_gain = gains[i]*(1.0-blacks[i]);
644
			System.out.println("gains["+i+"]="+gains[i]+" min_gain="+min_gain);
Andrey Filippov's avatar
Andrey Filippov committed
645
		}
646 647
		imp.setProperty("GAIN",String.format("%f",min_gain)); // common gain

Andrey Filippov's avatar
Andrey Filippov committed
648 649
		for (i=0;i<4;i++) gains[i]/=min_gain;
		for (i=0;i<4;i++) blacks256[i]=256.0*blacks[i];
650 651 652
		for (i=0;i<4;i++) {
			System.out.println("scaled gains["+i+"]="+gains[i]);
		}
Andrey Filippov's avatar
Andrey Filippov committed
653 654 655 656 657 658 659 660


		for (i=0;i<4;i++) {
			if  (MakerNote !=null) {
				if (scale) satValue[i]=((rgammas[i][255])-blacks256[i])/gains[i];
				else       satValue[i]=((rgammas[i][255])-blacks256[i]);
			}   else       satValue[i]=255.0;
			imp.setProperty("saturation_"+i,String.format("%f",satValue[i]));
661
			System.out.println("scaled gains["+i+"]="+gains[i]+" satValue["+i+"]="+satValue[i]);
Andrey Filippov's avatar
Andrey Filippov committed
662

663
		}
Andrey Filippov's avatar
Andrey Filippov committed
664 665 666 667 668 669 670 671 672
// swap satValue to match FLIPH,FLIPV again
		if (FLIPV!=0) {
			swapArrayElements (satValue,       1, 3);
			swapArrayElements (satValue,       0, 2);
		}
		if (FLIPH!=0) {
			swapArrayElements (satValue,       1, 0);
			swapArrayElements (satValue,       3, 2);
		}
673 674


Andrey Filippov's avatar
Andrey Filippov committed
675 676 677
		for (i=0;i<4;i++) {
			imp.setProperty("saturation_"+i,String.format("%f",satValue[i]));
//System.out.println("saturation_"+i+"\t"+String.format("%f",satValue[i]));
678 679

		}
Andrey Filippov's avatar
Andrey Filippov committed
680 681 682


		ImageProcessor ip = imp.getProcessor();
Andrey Filippov's avatar
Andrey Filippov committed
683 684
		//		if (FLIPH!=0) ip.flipHorizontal(); /* To correct Bayer */
		//		if (FLIPV!=0) ip.flipVertical(); /* To correct Bayer */
Andrey Filippov's avatar
Andrey Filippov committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701

		int width = ip.getWidth();
		int height = ip.getHeight();
		int yb,y,xb,x,offset,nb,xbyr,ybyr;
		float [] pixels = (float[])ip.getPixels();
		float [][] macroblock=new float[16][16];
		float [] pixels1= null;
		boolean IS_JP4=(COLOR_MODE==5);
		boolean IS_JP46=(COLOR_MODE==2);
		if (IS_JP4) pixels1= pixels.clone(); ///JP4 mode

		boolean use_imp_src= (imp_src!=null) && (imp_src.getWidth()==imp.getWidth()) && (imp_src.getHeight()==imp.getHeight());
		ImageProcessor ip_src= use_imp_src? imp_src.getProcessor():ip;
		if (ip_src==null) {
			ip_src=ip;
			use_imp_src=false;
		}
Andrey Filippov's avatar
Andrey Filippov committed
702
		for (yb=0;yb<(height>>4); yb++) for (xb=0;xb<(width>>4); xb++) { /* iterating macroblocks */
Andrey Filippov's avatar
Andrey Filippov committed
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
			if (IS_JP4) {
				for (nb=0; nb<4;nb++) {
					xbyr=nb & 1;
					ybyr=(nb>>1) & 1;
					for (y=0;y<8;y++) {
						offset=((yb<<4)+y)*width+ (nb<<3) +((xb>=(width>>5))?(((xb<<5)-width)+(width<<3)):(xb<<5));
						for (x=0;x<8;x++) {
							macroblock[(y<<1) | ybyr][(x<<1) | xbyr]=pixels1[offset+x];
						}
					}
				}
			} else if (IS_JP46) {
				for (y=0;y<16;y++) {
					offset=((yb<<4)+y)*width+(xb<<4);
					for (x=0;x<16;x++) {
						macroblock[((y<<1)&0xe) | ((y>>3) & 0x1)][((x<<1)&0xe) | ((x>>3) & 0x1)]=pixels[offset+x];
					}
				}
			} else { /// mono and other non-processed
				for (y=0;y<16;y++) {
					offset=((yb<<4)+y)*width+(xb<<4);
					for (x=0;x<16;x++) {
						macroblock[y][x]=pixels[offset+x];
					}
				}
			}
Andrey Filippov's avatar
Andrey Filippov committed
729
			/* apply gammas here */
Andrey Filippov's avatar
Andrey Filippov committed
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
			if  (MakerNote !=null) {

				if (scale) {
					for (y=0;y<16;y+=2) for (x=0;x<16;x+=2) {
						i=(int) macroblock[y  ][x  ];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y  ][x  ]= (float) (((rgammas[1][i])-blacks256[1])/gains[1]);

						i=(int) macroblock[y  ][x+1];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y  ][x+1]= (float) (((rgammas[0][i])-blacks256[0])/gains[0]);

						i=(int) macroblock[y+1][x  ];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y+1][x  ]= (float) (((rgammas[3][i])-blacks256[3])/gains[3]);

						i=(int) macroblock[y+1][x+1];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y+1][x+1]= (float) (((rgammas[2][i])-blacks256[2])/gains[2]);
					}
				} else {
					for (y=0;y<16;y+=2) for (x=0;x<16;x+=2) {
						i=(int) macroblock[y  ][x  ];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y  ][x  ]= (float) ((rgammas[1][i])-blacks256[1]);

						i=(int) macroblock[y  ][x+1];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y  ][x+1]= (float) ((rgammas[0][i])-blacks256[0]);

						i=(int) macroblock[y+1][x  ];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y+1][x  ]= (float) ((rgammas[3][i])-blacks256[3]);

						i=(int) macroblock[y+1][x+1];  if (i<0) i=0 ; else if (i>255) i=255;
						macroblock[y+1][x+1]= (float) ((rgammas[2][i])-blacks256[2]);
					}
				}
			}
			if (ip_src==null)  System.out.println("ip_src is null");
			//   else if (ip_src.setf==null)  System.out.println("ip_src.setf is null");
			for (y=0;y<16;y++) {
				offset=(yb<<4)+y;
				for (x=0;x<16;x++) {
					ip_src.setf((xb<<4)+ x, offset, macroblock[y][x]); // here null pointer if image was closed
				}
			}
		}
Andrey Filippov's avatar
Andrey Filippov committed
771 772
		if (FLIPH!=0) ip_src.flipHorizontal(); /* To correct Bayer */
		if (FLIPV!=0) ip_src.flipVertical(); /* To correct Bayer */
773

Andrey Filippov's avatar
Andrey Filippov committed
774 775
		/* Is it needed here ? */
		/*    imp.draw();
Andrey Filippov's avatar
Andrey Filippov committed
776
    imp.show(); **/
777
		if (use_imp_src) copyProperties (imp, imp_src);
Andrey Filippov's avatar
Andrey Filippov committed
778 779 780
		return use_imp_src;
	}

Andrey Filippov's avatar
Andrey Filippov committed
781
	/* reverses gamma calculations in the camera
Andrey Filippov's avatar
Andrey Filippov committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
      returns double[] table , in the range 0.0..255.996
	 */
	double [] elphel_gamma_calc (double gamma, double black, long gamma_scale) {
		int i;
		double x, black256 ,k;
		int[] gtable = new int[257];
		double[] rgtable =new double[256];
		int ig;
		black256=black*256.0;
		k=1.0/(256.0-black256);
		if (gamma < 0.13) gamma=0.13;
		if (gamma >10.0)  gamma=10.0;
		for (i=0; i<257; i++) {
			x=k*(i-black256);
			if (x < 0.0 ) x=0.0;
			ig= (int) (0.5+65535.0*Math.pow(x,gamma));
			ig=(ig* (int) gamma_scale)/0x400;
			if (ig > 0xffff) ig=0xffff;
			gtable[i]=ig;
		}
Andrey Filippov's avatar
Andrey Filippov committed
802 803
		/* now gtable[] is the same as was used in the camera */
		/* FPGA was using linear interpolation between elements of the gamma table, so now we'll reverse that process */
Andrey Filippov's avatar
Andrey Filippov committed
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
		// double[] rgtable =new  double[256];
		int indx=0;
		double outValue;
		for (i=0; i<256; i++ ) {
			outValue=128+(i<<8);
			while ((gtable[indx+1]<outValue) && (indx<256)) indx++;
			if (indx>=256) rgtable[i]=65535.0/256;
			else if (gtable[indx+1]==gtable[indx]) rgtable[i]=i;
			else           rgtable[i]=indx+(1.0*(outValue-gtable[indx]))/(gtable[indx+1] - gtable[indx]);
		}
		return rgtable;
	}


	long[] readElphelMakerNote(String directory, String fileName, int len, double [] xtraExif) throws IOException  {
Andrey Filippov's avatar
Andrey Filippov committed
819 820 821 822
		byte [] sig=  {(byte) 0x92 ,0x7c, /* MakerNote*/
				0x00 ,0x04, /* type (long)*/
				0x00 ,0x00 ,0x00 ,0x08 }; /* number*/
		/* should always read all MakerNote - verify that format did not change (edit here when it does). */
Andrey Filippov's avatar
Andrey Filippov committed
823 824 825 826 827 828
		sig[7]=(byte) (len & 0xff);
		sig[6]=(byte) ((len>>8) & 0xff);
		sig[5]=(byte) ((len>>16) & 0xff);
		sig[4]=(byte) ((len>>24) & 0xff);

		RandomAccessFile in = new RandomAccessFile(directory + fileName, "r");
Andrey Filippov's avatar
Andrey Filippov committed
829
		byte[] head = new byte[4096]; /* just read the beginning of the file */
Andrey Filippov's avatar
Andrey Filippov committed
830 831 832 833 834 835
		in.readFully(head);
		in.close(); // was no close()! -? "too many open files"
		if ((head[this.ExifOffset]!=0x4d) || (head[this.ExifOffset+1]!=0x4d)) {
			IJ.showMessage("JP46 Reader", "Exif Header not found in " + directory + fileName);
			return null;
		}
Andrey Filippov's avatar
Andrey Filippov committed
836
		/* search for MakerNote */
Andrey Filippov's avatar
Andrey Filippov committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
		long [] note=getExifData (sig, head, len);
		if (xtraExif!=null){
			if (xtraExif.length>0){ // get exposure time
				byte [] exposureTime={
						(byte) 0x82,(byte) 0x9a,0x00,0x05,
						0x00,0x00,0x00,0x01};
				long [] nomDenom=getExifData (exposureTime, head, 2);
				if (nomDenom==null) xtraExif[0]=Double.NaN;
				else {
					xtraExif[0]=(1.0*nomDenom[0])/nomDenom[1];
				}
			}
		}
		return note;
	}

	long[] readElphelMakerNoteURL(String url, int len, double [] xtraExif) throws IOException  {
		URL camURL = null;
		URLConnection urlConn = null;
		byte[] data = new byte[4096];
		//      System.out.println("loading exif from: " + url);

		try {
			camURL  = new URL(url);
			urlConn = camURL.openConnection();
862
			int contentLength = 4096; /* just read the beginning of the file */ //urlConn.getContentLength();
Andrey Filippov's avatar
Andrey Filippov committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
			//inStream = new InputStreamReader(urlConn.getInputStream());

			int bytesRead = 0;
			int offset = 0;
			InputStream raw = urlConn.getInputStream();
			InputStream in = new BufferedInputStream(raw);
			while (offset < contentLength) {
				bytesRead = in.read(data, offset, data.length - offset);
				if (bytesRead == -1)
					break;
				offset += bytesRead;
			}
			in.close();

		} catch(MalformedURLException e){
			System.out.println("Please check the URL:" + e.toString() );
		} catch(IOException  e1){
880
			System.out.println("Can't read  from the Internet: "+ e1.toString() );
Andrey Filippov's avatar
Andrey Filippov committed
881
		}
Andrey Filippov's avatar
Andrey Filippov committed
882 883 884 885
		byte [] sig=  {(byte) 0x92 ,0x7c, /* MakerNote*/
				0x00 ,0x04, /* type (long)*/
				0x00 ,0x00 ,0x00 ,0x08 }; /* number*/
		/* should always read all MakerNote - verify that format did not change (edit here when it does). */
Andrey Filippov's avatar
Andrey Filippov committed
886 887 888 889 890 891 892
		sig[7]=(byte) (len & 0xff);
		sig[6]=(byte) ((len>>8) & 0xff);
		sig[5]=(byte) ((len>>16) & 0xff);
		sig[4]=(byte) ((len>>24) & 0xff);

		//in = new RandomAccess(RandomAccessFactory.createRO(camURL), "r");

Andrey Filippov's avatar
Andrey Filippov committed
893
		byte[] head = new byte[4096]; /* just read the beginning of the file */
Andrey Filippov's avatar
Andrey Filippov committed
894 895 896 897 898 899 900
		head = data;
		//in.readFully(head);

		if ((head[this.ExifOffset] != 0x4d) || (head[this.ExifOffset+1] != 0x4d)) {
			IJ.showMessage("JP46 Reader", "Exif Header not found in " + url);
			return null;
		}
Andrey Filippov's avatar
Andrey Filippov committed
901
		/* search for MakerNote */
Andrey Filippov's avatar
Andrey Filippov committed
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
//		return getExifData (sig, head, len);
		long [] note=getExifData (sig, head, len);
		if (xtraExif!=null){
			if (xtraExif.length>0){ // get exposure time
				byte [] exposureTime={
						(byte) 0x82,(byte) 0x9a,0x00,0x05,
						0x00,0x00,0x00,0x01};
				long [] nomDenom=getExifData (exposureTime, head, 2);
				if (nomDenom==null) xtraExif[0]=Double.NaN;
				else {
					xtraExif[0]=(1.0*nomDenom[0])/nomDenom[1];
				}
			}
		}
		return note;

	}
919 920 921



Andrey Filippov's avatar
Andrey Filippov committed
922
	long [] getExifData (byte [] sig, byte [] head, int len){
Andrey Filippov's avatar
Andrey Filippov committed
923
		/* search for sig array */
Andrey Filippov's avatar
Andrey Filippov committed
924 925 926 927 928 929 930 931 932 933 934 935
		int i = this.ExifOffset + 2;
		boolean match=false;
		for (i = this.ExifOffset + 2; i < (head.length - sig.length); i++ ) {
			match=true;
			for (int j=0;j<sig.length;j++)if (head[i+j]!=sig[j]){
				match=false;
				break;
			}
			if (match) break;
		}
		i += sig.length;
		if (i >= (head.length-4)) {
Andrey Filippov's avatar
Andrey Filippov committed
936
			/* IJ.showMessage("JP46 Reader", "MakerNote tag not found in "+directory + fileName+ ", finished at offset="+i); // re-enable with DEBUG_LEVEL*/
Andrey Filippov's avatar
Andrey Filippov committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
			return null;
		}

		int offs=this.ExifOffset+(((head[i]<<24) & 0xff000000) |((head[i+1]<<16) & 0xff0000)| ((head[i+2]<<8) & 0xff00) | (head[i+3] & 0xff));

		// IJ.showMessage("JP46 Reader Debug", "MakerNote starts at offset "+offs);
		if (offs > (head.length-len) ) {
			IJ.showMessage("JP46 Reader", "Error: data (i.e.MakerNote)  starts too far - at offset "+offs+", while we read only "+ head.length+ "bytes");
			return null;
		}

		long[] note=new long[len];
		for (i=0; i<len; i++) note[i]=((head[offs+(i<<2)]&0xff) << 24) | ((head[offs+(i<<2)+1]&0xff) << 16)  | ((head[offs+(i<<2)+2]&0xff) << 8)  | (head[offs+(i<<2)+3]&0xff);
		return note;
	}

Andrey Filippov's avatar
Andrey Filippov committed
953
	/* Modified from Opener.java */
Andrey Filippov's avatar
Andrey Filippov committed
954 955 956 957 958 959 960 961
	ImagePlus openJpegOrGif(String dir, String name) {
		ImagePlus imp = null;
		Image img = Toolkit.getDefaultToolkit().createImage(dir+name);
		if (img!=null) {
			try {
				imp = new ImagePlus(name, img);
			} catch (IllegalStateException e) {
				return null; // error loading image
962
			}
Andrey Filippov's avatar
Andrey Filippov committed
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978

			if (imp.getType()==ImagePlus.COLOR_RGB) {
				checkGrayJpegTo32Bits(imp);
			}

			IJ.showStatus("Converting to 32-bits");
			new ImageConverter(imp).convertToGray32();

			FileInfo fi = new FileInfo();
			fi.fileFormat = FileInfo.GIF_OR_JPG;
			fi.fileName = name;
			fi.directory = dir;
			imp.setFileInfo(fi);
		}
		return imp;
	}
979
	@Override
Andrey Filippov's avatar
Andrey Filippov committed
980 981 982
	public void setTitle (String title) {
		imageTitle=title;
	}
983
	@Override
Andrey Filippov's avatar
Andrey Filippov committed
984 985 986 987 988 989 990 991 992 993
	public String getTitle () {
		return imageTitle;
	}
	public void setURL (String url) {
		camera_url=url;
	}
	public String getURL () {
		return camera_url;
	}

994 995
	ImagePlus openJpegOrGifUsingURL (String cameraurl) {
		URL url = null;
Andrey Filippov's avatar
Andrey Filippov committed
996 997 998
		ImagePlus imp = null;
		Image img = null;

Andrey Filippov's avatar
Andrey Filippov committed
999
		/* Validate URL */
Andrey Filippov's avatar
Andrey Filippov committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
		try {
			url = new URL(cameraurl);
		} catch (MalformedURLException e) {
			System.out.println("Bad URL: " + cameraurl);
			return null;
		}

		img = Toolkit.getDefaultToolkit().createImage(url);
		if (!this.ABSOLUTELY_SILENT) System.out.println("loading image from: " + url);
		//      imp = new ImagePlus("test", img);
		imp = new ImagePlus(imageTitle, img);


		if (imp.getType() == ImagePlus.COLOR_RGB) {
			checkGrayJpegTo32Bits(imp);
		}

		IJ.showStatus("Converting to 32-bits");
		new ImageConverter(imp).convertToGray32();

		FileInfo fi = new FileInfo();
		fi.fileFormat = FileInfo.GIF_OR_JPG;
		fi.fileName = "aquired from camera";
		fi.directory = cameraurl;
		imp.setFileInfo(fi);

		return imp;
	}

	public static void checkGrayJpegTo32Bits(ImagePlus imp) {
		ImageProcessor ip = imp.getProcessor();
		int width = ip.getWidth();
		int height = ip.getHeight();
		int[] pixels = (int[])ip.getPixels();
		int c,r,g,b,offset;
		for (int y=0; y<(height-8); y++) {
			offset = y*width;
			for (int x=0; x<(width-8); x++) {
				c = pixels[offset+x];
				r = (c&0xff0000)>>16;
			g = (c&0xff00)>>8;
		b = c&0xff;
		if (!((r==g)&&(g==b))) {
			IJ.error("Warning: color image");
			return;
		}
			}
		}
		IJ.showStatus("Converting to 32-bits");
		new ImageConverter(imp).convertToGray32();
	}
Andrey Filippov's avatar
Andrey Filippov committed
1051 1052
	/* =====Other methods =================================================================== */
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	public void listImageProperties (ImagePlus imp) {
		listImageProperties (imp,false);
	}
	public void listImageProperties (ImagePlus imp, boolean toConsole) {
		StringBuffer sb = new StringBuffer();
		Set<Object> jp4_set;
		Properties jp4_prop;
		Iterator<Object> itr;
		String str;
		jp4_prop=imp.getProperties();
		if (jp4_prop!=null) {
			jp4_set=jp4_prop.keySet();
			itr=jp4_set.iterator();
			while(itr.hasNext()) {
				str = (String) itr.next();
				sb.append(str+"\t"+jp4_prop.getProperty(str)+"\n");
				//				System.out.println(str + "=\t" +jp4_prop.getProperty(str));
			}
		}
		if (toConsole){
			System.out.println(imp.getTitle()+" properties\n"+sb.toString());
		} else {
		  new TextWindow(imp.getTitle()+" properties", "name\tvalue", sb.toString(),400,800);
		}
	}

Andrey Filippov's avatar
Andrey Filippov committed
1079
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	public double fracOverExposed(double [] map,   // map of overexposed pixels 0.0 - 0K, >0 (==1.0) - overexposed
			                      int  mapWidth,   // width of the map
			                      int        x0,   // X of the top left corner of the selection
			                      int        y0,   // Y of the top left corner of the selection
			                      int     width,   // selection width
			                      int     height){ // selection height
		int index,i,j;
		int y1=y0+height;
		int over=0;
		for (i=y0;i<y1;i++) {
			index=i*mapWidth+x0;
			for (j=0;j<width;j++) if (map[index++]>0.0) over++;
1092

Andrey Filippov's avatar
Andrey Filippov committed
1093 1094 1095 1096
		}
		return (1.0*over)/width/height;
	}
	// returns 1.0 if there is overexposed pixel, 0.0 - if OK
Andrey Filippov's avatar
Andrey Filippov committed
1097
	/* ======================================================================== */
Andrey Filippov's avatar
Andrey Filippov committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	public double [] overexposedMap (ImagePlus imp) {
		return overexposedMap (imp, 0.999);
	}
	public double [] overexposedMap (ImagePlus imp, double relativeThreshold) {
		double [] satValues=new double[4];
		boolean noProperties=false;
		int i,j,index;
		for (i=0;i<4;i++) {
	//protect from Double.valueOf(null), move to a function
			if (imp.getProperty("saturation_"+i)!=null) satValues[i]= Double.valueOf((String) imp.getProperty("saturation_"+i)).doubleValue();
			else {
				noProperties=true;
				break;
			}
		}
		if (noProperties) return null;
	//0 - red, 1,2 - green (use Math.min()), 3 - blue
		for (i=0;i<4;i++) satValues[i]*=relativeThreshold;
1116

Andrey Filippov's avatar
Andrey Filippov committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
		ImageProcessor ip=imp.getProcessor();
		int width=imp.getWidth();
		float []pixels=(float[]) ip.getPixels();
		double [] overexposed= new double [pixels.length];
	    for (index=0;index<overexposed.length;index++){
	    	i=(index / width) % 2;
	    	j=1-((index % width) % 2);
	    	overexposed[index]=(pixels[index]>=satValues[i*2+j])?1.0:0.0;
	    }
		return overexposed;
	}
1128

Andrey Filippov's avatar
Andrey Filippov committed
1129 1130 1131 1132
	public ImagePlus demuxImageOrClone(ImagePlus imp, int numImg) {
		ImagePlus imp_new=demuxImage(imp, numImg);
		if (imp_new!=null) return imp_new;
		return demuxClone(imp);
1133
	}
Andrey Filippov's avatar
Andrey Filippov committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151

	public ImagePlus demuxClone(ImagePlus imp) {
		ImageProcessor ip=imp.getProcessor().duplicate();
		ImagePlus imp_new=new ImagePlus(imp.getTitle()+"-dup",ip);
		Set<Object> jp4_set;
		Properties jp4_prop;
		Iterator<Object> itr;
		String str;
			jp4_prop=imp.getProperties();
			if (jp4_prop!=null) {
			  jp4_set=jp4_prop.keySet();
			  itr=jp4_set.iterator();
			  while(itr.hasNext()) {
				str = (String) itr.next();
				imp_new.setProperty(str,jp4_prop.getProperty(str));
		  }
		}
		return imp_new;
1152 1153 1154
	}


Andrey Filippov's avatar
Andrey Filippov committed
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	public ImagePlus demuxImage(ImagePlus imp, int numImg) {
		int width= imp.getWidth();
//		int height=imp.getHeight();
		int FLIPGV,FLIPGH;
		int [] FLIPV= new int[3];
		int [] FLIPH= new int[3];
		int [] HEIGHTS=new int[3];
		int [] BLANKS= new int[2];
		Object timestamp=null;
		if (imp.getProperty("FLIPV")!=null) FLIPGV= Integer.valueOf((String) imp.getProperty("FLIPV")).intValue(); else return null;
		if (imp.getProperty("FLIPH")!=null) FLIPGH= Integer.valueOf((String) imp.getProperty("FLIPH")).intValue(); else return null;
		int i;
		for (i=1;i<=3;i++) {
			if (imp.getProperty("FLIPV"+i)!=null)  FLIPV[i-1]=   Integer.valueOf((String) imp.getProperty("FLIPV"+i)).intValue(); else return null;
			if (imp.getProperty("FLIPH"+i)!=null)  FLIPH[i-1]=   Integer.valueOf((String) imp.getProperty("FLIPH"+i)).intValue(); else return null;
			if (imp.getProperty("HEIGHT"+i)!=null) HEIGHTS[i-1]= Integer.valueOf((String) imp.getProperty("HEIGHT"+i)).intValue(); else return null;
		}
		for (i=1;i<=2;i++) {
			if (imp.getProperty("BLANK_ROWS"+i)!=null) BLANKS[i-1]= Integer.valueOf((String) imp.getProperty("BLANK_ROWS"+i)).intValue(); else return null;
		}
		timestamp=imp.getProperty("timestamp");
		if (timestamp!=null);
1177 1178

/*
Andrey Filippov's avatar
Andrey Filippov committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
		System.out.println("FLIPV="+FLIPGV+" FLIPH="+FLIPGH);
		for (i=0;i<3;i++) System.out.println("FLIPV["+i+"]=  "+FLIPV[i]+" FLIPH["+i+"]=  "+FLIPH[i]);
		for (i=0;i<3;i++) System.out.println("HEIGHTS["+i+"]="+HEIGHTS[i]);
		for (i=0;i<2;i++) System.out.println("BLANKS["+i+"]= "+BLANKS[i]);
*/
		Rectangle [] r = new Rectangle[3];
		r[0]=new Rectangle(0, 0,                                        width,HEIGHTS[0]);
		r[1]=new Rectangle(0, HEIGHTS[0]+BLANKS[0],                     width,HEIGHTS[1]);
		r[2]=new Rectangle(0, HEIGHTS[0]+BLANKS[0]+HEIGHTS[1]+BLANKS[1],width,HEIGHTS[2]);
// assuming that 		(HEIGHTS[1]==0) && (HEIGHTS[2]!=0) == false
		if (FLIPGV!=0){
			if (HEIGHTS[1]>0) {
				if (HEIGHTS[2]>0) {
					Rectangle r_tmp=r[0];
					r[0]=r[2];
					r[2]=r_tmp;
				} else {
					Rectangle r_tmp=r[0];
					r[0]=r[1];
					r[1]=r_tmp;
				}
			}
		}
		if (FLIPGV>0) for (i=0;i<3;i++) FLIPV[i]=1-FLIPV[i];
		if (FLIPGH>0) for (i=0;i<3;i++) FLIPH[i]=1-FLIPH[i];

//		for (i=0;i<3;i++) System.out.println("Final: FLIPV["+i+"]=  "+FLIPV[i]+" FLIPH["+i+"]=  "+FLIPH[i]);

// if needed, we'll cut one pixel line. later can modify to add one extra, but then we need to duplicate the pre-last one (same Bayer),
1208
// not just add zeros - later before sliding FHT the two border lines are repeated for 16 times to reduce border effects.
Andrey Filippov's avatar
Andrey Filippov committed
1209
		for (i=0;i<3;i++) {
1210
//			System.out.println("before r["+i+"].x=  "+r[i].x+" r["+i+"].width=  "+r[i].width);
Andrey Filippov's avatar
Andrey Filippov committed
1211 1212 1213 1214 1215 1216 1217 1218 1219
//			System.out.println("before r["+i+"].y=  "+r[i].y+" r["+i+"].height=  "+r[i].height);
			if (((r[i].height & 1)==0 ) & (((r[i].y+FLIPV[i])&1)!=0)) r[i].height-=2;
			r[i].height &=~1;
			if (((r[i].y+FLIPV[i])&1)!=0) r[i].y+=1;

			if (((r[i].width & 1)==0 ) & (((r[i].x+FLIPH[i])&1)!=0)) r[i].width-=2;
			r[i].width &=~1;
			if (((r[i].x+FLIPH[i])&1)!=0) r[i].x+=1;

1220 1221 1222 1223


//			System.out.println("after r["+i+"].x=  "+r[i].x+" r["+i+"].width=  "+r[i].width);
//			System.out.println("after r["+i+"].y=  "+r[i].y+" r["+i+"].height=  "+r[i].height);
Andrey Filippov's avatar
Andrey Filippov committed
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
		}
		if (r[numImg].height<=0) return null;
//		ImageProcessor ip=imp.getProcessor();
//		ip.setRoi(r[numImg]);
//		ImageProcessor ip_individual=ip.crop().duplicate(); //java.lang.NegativeArraySizeException
/*
 * When using in multithreaded with (probably) the same composite image
Exception in thread "Thread-3564" java.lang.ArrayIndexOutOfBoundsException: 8970912
        at ij.process.FloatProcessor.crop(FloatProcessor.java:706)
        at JP46_Reader_camera.demuxImage(JP46_Reader_camera.java:1104)
        at CalibrationHardwareInterface$CamerasInterface$4.run(CalibrationHardwareInterface.java:1101)
1235

Andrey Filippov's avatar
Andrey Filippov committed
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
 */
//		ImageProcessor ip=imp.getProcessor();
//		ip.setRoi(r[numImg]);
		ImageProcessor ip_individual=imp.getProcessor().duplicate(); //java.lang.NegativeArraySizeException
		ip_individual.setRoi(r[numImg]);
		ip_individual=ip_individual.crop();

		if (FLIPH[numImg]!=0) ip_individual.flipHorizontal();
		if (FLIPV[numImg]!=0) ip_individual.flipVertical();
		ImagePlus imp_result=new ImagePlus(imp.getTitle()+"-"+numImg,ip_individual);

		//copy all defined properties of the composite image
		Set<Object> jp4_set;
		Properties jp4_prop;
		Iterator<Object> itr;
		String str;
			jp4_prop=imp.getProperties();
			if (jp4_prop!=null) {
			  jp4_set=jp4_prop.keySet();
			  itr=jp4_set.iterator();
			  while(itr.hasNext()) {
				str = (String) itr.next();
				imp_result.setProperty(str,jp4_prop.getProperty(str));
		  }
		}
// Replaced by copying all properties from the composite image
/*
		for (i=0;i<4;i++) {
			//protect from Double.valueOf(null), move to a function
			if (imp.getProperty("saturation_"+i)!=null) {
				imp_result.setProperty("saturation_"+i, imp.getProperty("saturation_"+i));
			}
		}
		if (timestamp!=null)imp_result.setProperty("timestamp", timestamp);
*/
1271
		// fill in meta data
Andrey Filippov's avatar
Andrey Filippov committed
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
		return imp_result;
	}
	public void copyProperties (ImagePlus imp_src,ImagePlus imp_dst){
		// copy all the properties to the new image
   		Set<Object> set;
		Properties prop;
		Iterator<Object> itr;
		String str;
		prop=imp_src.getProperties();
		if (prop!=null) {
			set=prop.keySet();
			itr=set.iterator();
			while(itr.hasNext()) {
				str = (String) itr.next();
				imp_dst.setProperty(str,prop.getProperty(str));
			}
		}
	}
1290 1291


Andrey Filippov's avatar
Andrey Filippov committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	public ImagePlus encodeProperiesToInfo(ImagePlus imp){
		String info="<?xml version=\"1.0\" encoding=\"UTF-8\"?><properties>";
		Set<Object> jp4_set;
		Properties jp4_prop;
		Iterator<Object> itr;
		String str;
		jp4_prop=imp.getProperties();
		if (jp4_prop!=null) {
			jp4_set=jp4_prop.keySet();
			itr=jp4_set.iterator();
			while(itr.hasNext()) {
				str = (String) itr.next();
				//				if (!str.equals("Info")) info+="<"+str+">\""+jp4_prop.getProperty(str)+"\"</"+str+">";
				if (!str.equals("Info")) info+="<"+str+">"+jp4_prop.getProperty(str)+"</"+str+">";
			}
		}
		info+="</properties>\n";
		imp.setProperty("Info", info);
		return imp;
	}
	public boolean decodeProperiesFromInfo(ImagePlus imp){
		if (imp.getProperty("Info")==null) return false;
		String xml= (String) imp.getProperty("Info");
1315

Andrey Filippov's avatar
Andrey Filippov committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	    DocumentBuilder db=null;
		try {
			db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			return false;
		}
	    InputSource is = new InputSource();
	    is.setCharacterStream(new StringReader(xml));
    	Document doc = null;
	    try {
	    	doc = db.parse(is);
	    } catch (SAXException e) {
	    	return false;
	    } catch (IOException e) {
	    	return false;
	    }
	    NodeList allNodes=doc.getDocumentElement().getElementsByTagName("*");
	    for (int i=0;i<allNodes.getLength();i++) {
	        String name= allNodes.item(i).getNodeName();
            String value=allNodes.item(i).getFirstChild().getNodeValue();
    		imp.setProperty(name, value);
1337

Andrey Filippov's avatar
Andrey Filippov committed
1338
	    }
1339

Andrey Filippov's avatar
Andrey Filippov committed
1340 1341
		return true;
	}
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	/**
	 * Main method for debugging.
	 *
	 * For debugging, it is convenient to have a method that starts ImageJ, loads an
	 * image and calls the plugin, e.g. after setting breakpoints.
	 * Grabbed from https://github.com/imagej/minimal-ij1-plugin
	 * @param args unused
	 */
	public static void main(String[] args) {
		// set the plugins.dir property to make the plugin appear in the Plugins menu
		Class<?> clazz = Aberration_Calibration.class;
		String url = clazz.getResource("/" + clazz.getName().replace('.', '/') + ".class").toString();
		String pluginsDir = url.substring(5, url.length() - clazz.getName().length() - 6);
		System.setProperty("plugins.dir", pluginsDir);
		// start ImageJ
		new ImageJ();
		// run the plugin
		IJ.runPlugIn(clazz.getName(), "");
	}
1361

Andrey Filippov's avatar
Andrey Filippov committed
1362 1363 1364 1365 1366
}