Skip to content

Commit

Permalink
Fast Gzip support
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed May 27, 2021
1 parent eff3350 commit 935d6da
Show file tree
Hide file tree
Showing 45 changed files with 240 additions and 126 deletions.
2 changes: 1 addition & 1 deletion define_types.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface
{$endif}

const
kVers = 'v1.0.20201102+';
kVers = 'v1.0.20201102++a';
NaN : double = 1/0;
kTab = chr(9);
kCR = chr (13);
Expand Down
4 changes: 4 additions & 0 deletions gl_core_3d.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

{$mode objfpc}{$H+}
{$Include opts.inc}
{$IFNDEF CPUAARCH64} //Apple M1 has poor 10_10_10_2 performance
{$DEFINE USE_GL_INT_2_10_10_10}
{$ENDIF}
interface

uses
Expand Down Expand Up @@ -459,6 +462,7 @@ procedure SetTrackUniforms(lineWidth, ScreenPixelX, ScreenPixelY: integer);
X,Y,Z,W: word;
end;


function f32tof16(v: single): word;assembler; nostackframe;
asm
.long 0x1e23c000 //fcvt h0, s0
Expand Down
4 changes: 2 additions & 2 deletions mainunit.lfm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object GLForm1: TGLForm1
Left = 228
Left = 372
Height = 659
Top = 25
Top = 86
Width = 1004
AllowDropFiles = True
Caption = 'Surf Ice'
Expand Down
92 changes: 67 additions & 25 deletions mainunit.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
warning: GTK3 only supports OpenGL core - enable COREGL inn opts.inc
{$ENDIF}{$ENDIF}
{$mode delphi}{$H+}
{$IFDEF Darwin}
{$modeswitch objectivec1}
{$ENDIF}
//{$DEFINE DARKMODE}
interface

uses
{$IFDEF DGL} dglOpenGL, {$ELSE DGL} {$IFDEF COREGL}glcorearb, {$ELSE} gl, {$ENDIF} {$ENDIF DGL}
fphttpclient, strutils,
{$IFDEF MYPY}PythonEngine, {$ENDIF}
//{$IFDEF SCRIPTING}
//{$ENDIF}
{$IFNDEF UNIX} shellapi, {$ELSE} Process, {$ENDIF}
{$IFDEF Linux} LazFileUtils, {$ENDIF}
{$IFDEF COREGL} gl_core_3d, {$ELSE} gl_legacy_3d, {$ENDIF}
{$IFDEF LHRH} meshlhrh, {$ENDIF}
uPSComponent,Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Expand Down Expand Up @@ -539,7 +544,7 @@ implementation

uses
{$IFDEF LCLCocoa}
UserNotification, {$IFDEF DARKMODE}nsappkitext,{$ENDIF} glcocoanscontext,
CocoaAll, UserNotification, {$IFDEF DARKMODE}nsappkitext,{$ENDIF} glcocoanscontext,
{$ENDIF}
commandsu;

Expand Down Expand Up @@ -785,7 +790,7 @@ function InitPyLibraryPath: string;
{$ENDIF}

{$IFDEF Darwin}
function findMacOSLibPython3: string;
function findMacOSLibPython27: string;
const
kPth = '/System/Library/Frameworks/Python.framework/Versions/Current/lib/libpython2.7.dylib';
begin
Expand All @@ -794,6 +799,19 @@ function findMacOSLibPython3: string;
result := kPth;
end;

function findMacOSLibPython3x: string;
var
N: integer;
S: string;
begin
for N:= 5 to 9 do
begin
S:= Format('/Library/Frameworks/Python.framework/Versions/3.%d/lib/libpython3.%d.dylib',[N, N]);
if FileExists(S) then exit(S);
end;
result := '';
end;

//Catalina sandbox restrictions make do not allow this for Notarized apps:
(*function findMacOSLibPython3(pthroot: string = '/Library/Frameworks/Python.framework/'): string;
label
Expand All @@ -819,26 +837,40 @@ function findMacOSLibPython3: string;
{$ENDIF}

{$IFDEF LINUX}
function findLinuxLibPython3(pthroot: string = '/usr/lib/'): string;
label
121;
function findFirstRecursive(const SearchPath: String; SearchMask: String): string;
//example findFirstRecursive('/usr/lib/', 'libpython3*so')
//uses LazFileUtils
var
pths: TStringList;
i: integer;
ret: string;
procedure find1(Dir: string);
var
SR: TSearchRec;
begin
result := '';
if not DirectoryExists(pthroot) then exit;
pths := TStringList.Create;
FindAllFiles(pths, pthroot, 'libpython3*so', true); //find e.g. all pascal sourcefiles
if pths.Count < 1 then goto 121;
for i := pths.Count -1 downto 0 do
if AnsiContainsText(pths[i],'loader') then
pths.Delete(i);
if pths.Count < 1 then goto 121;
result := pths[0];
//printf(pths);
121:
pths.Free;
if (ret <> '') or (FileIsSymlink(Dir)) then exit;
if FindFirst(IncludeTrailingBackslash(Dir) + SearchMask, faAnyFile or faDirectory, SR) = 0 then begin
ret := IncludeTrailingBackslash(Dir) +SR.Name;
FindClose(SR);
exit;
end;
if FindFirst(IncludeTrailingBackslash(Dir) + '*.*', faAnyFile or faDirectory, SR) = 0 then
try
repeat
if ((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..') then
find1(IncludeTrailingBackslash(Dir) + SR.Name); // recursive call!
until FindNext(Sr) <> 0;
finally
FindClose(SR);
end;
end;
begin
ret := '';
find1(SearchPath);
result := ret;
end;

function findLinuxLibPython3(pthroot: string = '/usr/lib/'): string;
begin
result := findFirstRecursive(pthroot, 'libpython3*so');
end;
{$ENDIF}

Expand Down Expand Up @@ -885,7 +917,9 @@ function findLinuxLibPython3(pthroot: string = '/usr/lib/'): string;
if length(result) > 0 then exit;
end;
{$IFDEF Darwin}
result := findMacOSLibPython3();
result := findMacOSLibPython3x();
if length(result) > 0 then exit;
result := findMacOSLibPython27();
if length(result) > 0 then exit;
{$ENDIF}
{$IFDEF LINUX}
Expand Down Expand Up @@ -2918,14 +2952,18 @@ procedure TGLForm1.VolumeToMeshMenuClick(Sender: TObject);
{$ELSE}
kVolFilter = 'NIfTI volume|*.hdr;*.nii;*nii.gz';
{$ENDIF}
var
ok: boolean;
begin
OpenDialog.Filter := kVolFilter;
OpenDialog.Title := 'Select volume to convert';
if not OpenDialog.Execute then exit;
if (sender as TMenuItem).tag = 1 then
Atlas2Mesh(OpenDialog.FileName)
ok := Atlas2Mesh(OpenDialog.FileName)
else
Nii2Mesh(OpenDialog.FileName);
ok := Nii2Mesh(OpenDialog.FileName);
if (not ok) then
showmessage('Unable to convert '+OpenDialog.FileName);
end;

procedure TGLForm1.ShaderForBackgroundOnlyClick(Sender: TObject);
Expand Down Expand Up @@ -5968,7 +6006,7 @@ function GetHardwareVersion: string;
lProcess.Free;
end;

function GetOSVersion: string;
(*function GetOSVersion: string;
//returns number of CPUs for MacOSX computer
//example - will return 4 if the computer has two dual core CPUs
//requires Process in Uses Clause
Expand All @@ -5988,6 +6026,10 @@ function GetOSVersion: string;
result := lStringList.Strings[1];
lStringList.Free;
lProcess.Free;
end; *)
function GetOSVersion: string;
begin
result := NSProcessInfo.ProcessInfo.operatingSystemVersionString.UTF8String;
end;
{$ENDIF}

Expand Down Expand Up @@ -6517,7 +6559,7 @@ procedure TGLForm1.OpenMenuClick(Sender: TObject);

procedure TGLForm1.OverlayBoxCreate;
var
lSearchRec: TSearchRec;
lSearchRec: TSearchRec;
lStr: string;
begin
LUTdropNode.Items.Clear;
Expand Down
5 changes: 3 additions & 2 deletions mesh.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9703,13 +9703,14 @@ function TMesh.LoadVoxel2Vertex(const FileName: string; lOverlayIndex: integer):
//if not readMGHHeader (fnm, hdr, gzFlag, swapEndian, x64) then exit;
if not readVoxHeader (fnm, hdr, gzFlag, swapEndian, x64) then exit;
if (x64 <> length(vertices)) then begin
//showmessageX(format('Expected %d vertices, not %d.', [length(vertices), x64]));
exit;
printf(format('Error loading MGH overlay: Mesh has %d vertices, but MGH file has %d.', [length(vertices), x64]));
exit;
end;
if (gzFlag = K_gzBytes_onlyImageCompressed) then begin
showmessageX('Unable to read files with uncompressed headers and compressed images.');
exit;
end;
printf(format('Loading MGH overlay with %d vertices.', [x64]));
vol := hdr.dim[2];
if (vol < 1) then vol := 1;
overlay[lOverlayIndex].volumes := vol;
Expand Down
14 changes: 13 additions & 1 deletion nifti_loader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,11 @@ function readVoxHeader (var fname: string; var nhdr: TNIFTIhdr; var gzBytes: int
result := true;
end;

procedure printf(s: string);
begin
{$IFDEF UNIX}writeln(s);{$ENDIF}
end;

function TNIFTI.LoadFromFile(const FileName: string; smoothMethod: integer): boolean; //smoothMethod is one of kNiftiSmooth... options
var
ext, hdrName, imgName: string;
Expand Down Expand Up @@ -842,8 +847,15 @@ function TNIFTI.LoadFromFile(const FileName: string; smoothMethod: integer): boo
if not FixDataType (hdr) then exit;
*)
imgName := FileName;
if not readVoxHeader (imgName, hdr, gzFlag, isSwap, xDim64) then
if not readVoxHeader (imgName, hdr, gzFlag, isSwap, xDim64) then begin
printf(format('Unable to load as voxelwise data: %s', [FileName]));
exit(false);
end;
printf(format('voxel dimensions %d %d %d', [hdr.dim[1],hdr.dim[2],hdr.dim[3]]));
if (hdr.dim[1] < 2) or (hdr.dim[2] < 2) or (hdr.dim[3] < 2) then begin
printf(format('Not a 3D voxel-based image: %s', [FileName]));
exit(false);
end;
if not ReadImg(imgName, isSwap, gzFlag) then exit(false);
setMatrix;
if smoothMethod = kNiftiSmoothMaskZero then
Expand Down
3 changes: 2 additions & 1 deletion opts.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
{$DEFINE COREGL}
{$DEFINE LEGACY_INDEXING}
{$DEFINE GEOM_GLEXT}
{$DEFINE FOREIGNVOL}
//{$DEFINE HEMISSAO} //for hemispherical SSAO - requires shaders that save surface normal - see hemiAO3 and hemiAO
{$DEFINE CTM} //support OpenCTM format, requires LZMA decoding
//{$DEFINE TUBES}
{$DEFINE MATCAP}
{$DEFINE MATCAP}
5 changes: 5 additions & 0 deletions surfice.app/Contents/Resources/script/startup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import gl
gl.resetdefaults()
gl.meshload('/Users/chrisrorden/Neuro/Surfice/Surfice/fs/rh.pial')
#gl.overlayload('/Users/chrisrorden/Desktop/cache.th13.neg.sig.masked.mgh')
gl.overlayminmax(1,1,2)
Loading

0 comments on commit 935d6da

Please sign in to comment.