void MHmgphy::WriteJython(
	const vector<double>	&X,
	double					trim,
	int						Ntr )
{
	FILE	*f = FileOpenOrDie( "JythonTransforms.txt", "w" );

	fprintf( f, "transforms = {\n" );

	int	nr = vRgn.size();

	for( int i = 0, itrf = 0; i < nr; ++i ) {

		const RGN&	I = vRgn[(*zs)[i].i];

		// skip unused tiles
		if( I.itr < 0 )
			continue;

		++itrf;

		const char	*path;
		DisplayStrings( NULL, path, I );

		// fix origin : undo trimming
		int		j = I.itr * NX;
		THmgphy	T( &X[j] );
		double	x_orig;
		double	y_orig;

		TopLeft( y_orig, x_orig, T, gW, gH, trim );

		fprintf( f,
		"\"%s\" : [%f, %f, %f, %f, %f, %f, %.12g, %.12g]%s\n",
		path,
		X[j+0], X[j+3], X[j+6],
		X[j+1], X[j+4], X[j+7],
		x_orig, y_orig,
		(itrf == Ntr ? "" : ",") );
	}

	fprintf( f, "}\n" );
	fclose( f );

	IDBT2ICacheClear();
}
int MyFlush() {
	if (!myBmpBits) return 0;
	GdiFlush();
	//BitBlt(hMemDC, 0, 0, dcRect.right, dcRect.bottom, hDC, 0, 0, SRCCOPY);
	/*
	BITMAPINFO info;
	info.bmiHeader.biSize = sizeof(info.bmiHeader);
	info.bmiHeader.biWidth = dcRect.right;
	info.bmiHeader.biHeight = -dcRect.bottom;
	info.bmiHeader.biPlanes = 1;
	info.bmiHeader.biBitCount = 32;
	info.bmiHeader.biCompression = BI_RGB;
	info.bmiHeader.biSizeImage = dcRect.right * dcRect.bottom * 4;
	info.bmiHeader.biClrImportant = 0;
	info.bmiHeader.biClrUsed = 0;
	unsigned int *buffer = (unsigned int*) malloc(dcRect.right * dcRect.bottom * sizeof(unsigned int));
	DWORD res = GetDIBits(hMemDCTheirs, hMemBmpTheirs, 0, dcRect.bottom, buffer, &info, DIB_RGB_COLORS);
	//*/
	int res = BitBlt(hMemDCMine, 0, 0, dcRect.right, dcRect.bottom, hMemDCTheirs, 0, 0, SRCCOPY);
	int numStrings = 0;
	if (!res) return 0;
	GdiFlush();
	if (AtlasIsLoaded()) {
		StringInfo* strings = LocateStrings(myBmpBits, dcRect.right, dcRect.bottom, numStrings);
		if (strings) {
			TransStrings(strings, numStrings);
			//BitBlt(hMemDCMine, 0, 0, dcRect.right, dcRect.bottom, hMemDCTheirs, 0, 0, SRCCOPY);
			DisplayStrings(hMemDCMine, strings, numStrings, dcRect.right, dcRect.bottom);
			for (int i=0; i<numStrings; i++) {
				free(strings[i].string);
			}
			free(strings);
		}
	}
	BitBlt(activeDC, 0, 0, dcRect.right, dcRect.bottom, hMemDCMine, 0, 0, SRCCOPY);
	return res;
	//if (res)
	//	res = SetDIBits(hMemDC, hMemBmp, 0, dcRect.bottom, buffer, &info, DIB_RGB_COLORS);
}
void MHmgphy::WriteTrakEM(
	double					xmax,
	double					ymax,
	const vector<double>	&X,
	double					trim,
	int						xml_type,
	int						xml_min,
	int						xml_max )
{
	FILE	*f = FileOpenOrDie( "MultLayHmgphy.xml", "w" );

	int	oid = 3;

	fprintf( f, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" );

	TrakEM2WriteDTD( f );

	fprintf( f, "<trakem2>\n" );

	fprintf( f,
	"\t<project\n"
	"\t\tid=\"0\"\n"
	"\t\ttitle=\"Project\"\n"
	"\t\tmipmaps_folder=\"trakem2.mipmaps/\"\n"
	"\t\tn_mipmap_threads=\"8\"\n"
	"\t/>\n" );

	fprintf( f,
	"\t<t2_layer_set\n"
	"\t\toid=\"%d\"\n"
	"\t\ttransform=\"matrix(1,0,0,1,0,0)\"\n"
	"\t\ttitle=\"Top level\"\n"
	"\t\tlayer_width=\"%.2f\"\n"
	"\t\tlayer_height=\"%.2f\"\n"
	"\t>\n",
	oid++, xmax, ymax );

	int	prev	= -1;	// will be previously written layer
	int	offset	= int(2 * trim + 0.5);
	int	nr		= vRgn.size();

	for( int i = 0; i < nr; ++i ) {

		const RGN&	I = vRgn[(*zs)[i].i];

		// skip unused tiles
		if( I.itr < 0 )
			continue;

		// changed layer
		if( (*zs)[i].z != prev ) {

			if( prev != -1 )
				fprintf( f, "\t\t</t2_layer>\n" );

			fprintf( f,
			"\t\t<t2_layer\n"
			"\t\t\toid=\"%d\"\n"
			"\t\t\tthickness=\"0\"\n"
			"\t\t\tz=\"%d\"\n"
			"\t\t>\n",
			oid++, (*zs)[i].z );

			prev = (*zs)[i].z;
		}

		const char	*path;
		char		title[128];
		DisplayStrings( title, path, I );

		// fix origin : undo trimming
		int		j = I.itr * NX;
		THmgphy	T( &X[j] );
		double	x_orig;
		double	y_orig;

		TopLeft( y_orig, x_orig, T, gW, gH, trim );

		fprintf( f,
		"\t\t\t<t2_patch\n"
		"\t\t\t\toid=\"%d\"\n"
		"\t\t\t\twidth=\"%d\"\n"
		"\t\t\t\theight=\"%d\"\n"
		"\t\t\t\ttransform=\"matrix(1,0,0,1,%f,%f)\"\n"
		"\t\t\t\ttitle=\"%s\"\n"
		"\t\t\t\ttype=\"%d\"\n"
		"\t\t\t\tfile_path=\"%s\"\n"
		"\t\t\t\to_width=\"%d\"\n"
		"\t\t\t\to_height=\"%d\"\n",
		oid++, gW - offset, gH - offset,
		x_orig, y_orig,
		title, xml_type, path, gW - offset, gH - offset );

		if( xml_min < xml_max ) {

			fprintf( f,
			"\t\t\t\tmin=\"%d\"\n"
			"\t\t\t\tmax=\"%d\"\n"
			"\t\t\t>\n",
			xml_min, xml_max );
		}
		else
			fprintf( f, "\t\t\t>\n" );

		fprintf( f,
		"\t\t\t<ict_transform"
		" class=\"mpicbg.trakem2.transform.HomographyModel2D\""
		" data=\"%f %f %f %f %f %f %.12g %.12g 1\"/>\n"
		"\t\t\t</t2_patch>\n",
		X[j  ], X[j+1], X[j+2], X[j+3],
		X[j+4], X[j+5], X[j+6], X[j+7] );
	}

	if( nr > 0 )
		fprintf( f, "\t\t</t2_layer>\n" );

	fprintf( f, "\t</t2_layer_set>\n" );
	fprintf( f, "</trakem2>\n" );
	fclose( f );

	IDBT2ICacheClear();
}