unit realUtils;

interface
	uses
		Types, Memory, QuickDraw, Packages, Menus, Events, Fonts, ToolUtils, globals, Utilities, Graphics;

function NewRealWindow (name: str255; width, height: LongInt): boolean;
procedure DisplayRealImage(rData: rImagePtr; min, max: real; BlackIsZero: boolean);
function ConvertToReal:boolean;


implementation


function NewRealWindow (name: str255; width, height: LongInt): boolean;
var
	TempH: handle;
begin
	tempH := GetBigHandle(width * height * SizeOf(real));
	if TempH = nil then begin
		PutMemoryAlert;
		NewRealWindow := false;
		exit(NewRealWindow);
	end;
	if not NewPicWindow(name, width, height) then begin
		DisposeHandle(TempH);
		exit(NewRealWindow);
	end;
	info^.DataH := tempH;
	UpdateTitleBar;
	UpdateWindowsMenuItem;
	NewRealWindow := true;
end;


	procedure DisplayRealImage(rData: rImagePtr; min, max: real; BlackIsZero: boolean);
		var
			row, col, i, base, width, height: LongInt;
			r, scale: real;
			line: lineType;
	begin
		with info^ do begin
			width := pixelsPerLine;
			height := nLines;
		end;
		scale := 255.0 / (max - min);
		for row := 0 to height - 1 do begin
				base := row * width;
				for col := 0 to width - 1 do begin
						r := rData^[base + col];
						line[col] := round((r - min) * scale);
				end;
				PutLine(0, row, width, line);
			end;
		if BlackIsZero then
			InvertPic;
		with info^ do begin
			Changes := true;
			fit:=StraightLine;
			nCoefficients := 2;
			if BlackIsZero then begin
				coefficient[1] := max;
				coefficient[2] := -1.0/scale;
			end else begin
				coefficient[1] := min;
				coefficient[2] := 1.0/scale;
			end;
			nKnownValues := 0;
			ZeroClip := false;
			GenerateValues;
			UnitOfMeasure := '';
			UpdateTitleBar;
		end;
	end;
	
	
function ConvertToReal:boolean;
var
	row, col, i, sum, base: LongInt;
	width, height, NeededSize, CurrentSize: LongInt;
	line: LineType;
	rData: rImagePtr;
	TempH: handle;
begin
	with info^ do begin
		width := pixelsPerLine;
		height := nLines;
		NeededSize := width * height * SizeOf(real);
		CurrentSize := 0;
		if dataH <> nil then
			CurrentSize := GetHandleSize(dataH);
		if CurrentSize <> NeededSize then begin
			tempH := GetBigHandle(NeededSize);
			if TempH = nil then begin
				PutMemoryAlert;
				ConvertToReal := false;
				exit(ConvertToReal);
			end;
			dataH := tempH;
		end;
		hlock(dataH);
		rData := rImagePtr(dataH^);
	end;
	for row:= 0 to height - 1 do begin
		GetLine(0, row, width, line);
		base := row * width;
		for col := 0 to width - 1 do
			rData^[base + col] := line[col];
	end;
	hunlock(info^.dataH);
	UpdateTitleBar;
	UpdateWindowsMenuItem;
	ConvertToReal := true;
end;



end. {realUtils Unit}
