85
85
import net .imglib2 .view .Views ;
86
86
87
87
/**
88
- *
89
88
* Apply a bigwarp transform to a 2d or 3d ImagePlus
90
- *
91
89
*/
92
90
public class ApplyBigwarpPlugin implements PlugIn
93
91
{
@@ -825,18 +823,12 @@ public static <T> List<ImagePlus> apply(
825
823
final boolean wait ,
826
824
final WriteDestinationOptions writeOpts ) {
827
825
828
- // int numChannels = bwData.movingSourceIndexList.size();
829
826
final int numChannels = bwData .numMovingSources ();
830
- // final List< SourceAndConverter< T >> sourcesxfm = BigWarp.wrapSourcesAsTransformed(
831
- // bwData.sourceInfos,
832
- // landmarks.getNumdims(),
833
- // bwData );
834
827
835
828
final InvertibleRealTransform invXfm = new BigWarpTransform ( landmarks , tranformTypeOption ).getTransformation ();
836
829
for ( int i = 0 ; i < numChannels ; i ++ )
837
830
{
838
831
final SourceAndConverter < T > movingSource = bwData .getMovingSource ( i );
839
- // final int originalIdx = bwData.sources.indexOf(movingSource);
840
832
((WarpedSource <?>)(movingSource .getSpimSource ())).updateTransform (invXfm );
841
833
((WarpedSource <?>)(movingSource .getSpimSource ())).setIsTransformed (true );
842
834
}
@@ -858,12 +850,13 @@ public static <T> List<ImagePlus> apply(
858
850
859
851
if ( writeOpts != null && writeOpts .n5Dataset != null && !writeOpts .n5Dataset .isEmpty ())
860
852
{
853
+ final SourceAndConverter <T > src = bwData .getMovingSource (0 );
861
854
final String unit = ApplyBigwarpPlugin .getUnit ( bwData , resolutionOption );
862
- runN5Export ( bwData , bwData . sources , fieldOfViewOption ,
855
+ runN5Export ( bwData , src , fieldOfViewOption ,
863
856
outputIntervalList .get ( 0 ), interp ,
864
857
offset , res , unit ,
865
858
progressWriter , writeOpts ,
866
- Executors .newFixedThreadPool ( nThreads ) );
859
+ Executors .newFixedThreadPool (nThreads ) );
867
860
return null ;
868
861
}
869
862
else
@@ -959,6 +952,7 @@ private static double[] physicalOffsetFromPixelInterval(final RealInterval inter
959
952
return out ;
960
953
}
961
954
955
+ @ Deprecated
962
956
public static <S , T extends NativeType <T > & NumericType <T >> void runN5Export (
963
957
final BigWarpData <S > data ,
964
958
final List < SourceAndConverter < S >> sources ,
@@ -975,8 +969,12 @@ public static <S, T extends NativeType<T> & NumericType<T>> void runN5Export(
975
969
final int nd = BigWarp .detectNumDims ( data .sources );
976
970
final double [] resolution = limit (nd ,resolutionArg );
977
971
978
- // final double[] offset = limit(nd,offsetArg);
979
- final double [] offset = physicalOffsetFromPixelInterval (outputInterval , resolution );
972
+ final double [] offset = ApplyBigwarpPlugin .getPixelOffset ( fieldOfViewOption , offsetArg , resolution ,
973
+ outputInterval );
974
+
975
+ System .out .println ("resolution: " + Arrays .toString (resolution ));
976
+ System .out .println ("offset : " + Arrays .toString (offset ));
977
+ System .out .println ("interval : " + Intervals .toString (outputInterval ));
980
978
981
979
// setup n5 parameters
982
980
final String dataset = writeOpts .n5Dataset ;
@@ -1074,6 +1072,121 @@ public static <S, T extends NativeType<T> & NumericType<T>> void runN5Export(
1074
1072
progressWriter .setProgress ( 1.0 );
1075
1073
}
1076
1074
1075
+ public static <S ,T extends NativeType <T > & NumericType <T >> void runN5Export (
1076
+ final BigWarpData <S > data ,
1077
+ final SourceAndConverter <S > sourceAndConverter ,
1078
+ final String fieldOfViewOption ,
1079
+ final Interval outputInterval ,
1080
+ final Interpolation interp ,
1081
+ final double [] offsetArg ,
1082
+ final double [] resolutionArg ,
1083
+ final String unit ,
1084
+ final ProgressWriter progressWriter ,
1085
+ final WriteDestinationOptions writeOpts ,
1086
+ final ExecutorService exec )
1087
+ {
1088
+
1089
+ final int nd = BigWarp .detectNumDims ( data .sources );
1090
+ final double [] resolution = limit (nd ,resolutionArg );
1091
+
1092
+ // pixel offset
1093
+ final double [] offsetPixel = ApplyBigwarpPlugin .getPixelOffset ( fieldOfViewOption , offsetArg , resolution ,
1094
+ outputInterval );
1095
+
1096
+ // setup n5 parameters
1097
+ final String dataset = writeOpts .n5Dataset ;
1098
+ final int [] blockSize = writeOpts .blockSize ;
1099
+ final Compression compression = writeOpts .compression ;
1100
+ if ( dataset == null || dataset .isEmpty () )
1101
+ {
1102
+ System .err .println ("Problem with n5 dataset path: " + dataset );
1103
+ return ;
1104
+ }
1105
+ N5Writer n5 ;
1106
+ try
1107
+ {
1108
+ n5 = new N5Factory ().openWriter ( writeOpts .pathOrN5Root );
1109
+ }
1110
+ catch ( final RuntimeException e1 )
1111
+ {
1112
+ System .err .println ("Could not create n5 writer for: " + writeOpts .pathOrN5Root );
1113
+ e1 .printStackTrace ();
1114
+ return ;
1115
+ }
1116
+
1117
+ // build metadata
1118
+ final OmeNgffMetadataParser parser = new OmeNgffMetadataParser ();
1119
+ final String [] axesLabels = nd == 2 ? new String []{"x" , "y" } : new String []{"x" , "y" , "z" };
1120
+ final Axis [] axes = new Axis [nd ];
1121
+ for (int i = 0 ; i < nd ; i ++)
1122
+ axes [i ] = new Axis (Axis .SPACE , axesLabels [i ], unit );
1123
+
1124
+ // setup physical to pixel transform
1125
+ final AffineTransform3D resolutionTransform = new AffineTransform3D ();
1126
+ resolutionTransform .set ( resolution [ 0 ], 0 , 0 );
1127
+ resolutionTransform .set ( resolution [ 1 ], 1 , 1 );
1128
+
1129
+ if ( resolution .length > 2 )
1130
+ resolutionTransform .set ( resolution [ 2 ], 2 , 2 );
1131
+
1132
+ final double [] offsetPhysical = new double [resolution .length ];
1133
+ offsetPhysical [0 ] = resolution [0 ] * offsetPixel [0 ];
1134
+ offsetPhysical [1 ] = resolution [1 ] * offsetPixel [1 ];
1135
+
1136
+ if ( resolution .length > 2 )
1137
+ offsetPhysical [2 ] = resolution [2 ] * offsetPixel [2 ];
1138
+
1139
+ final AffineTransform3D offsetTransform = new AffineTransform3D ();
1140
+ offsetTransform .set ( offsetPhysical [ 0 ], 0 , 3 );
1141
+ offsetTransform .set ( offsetPhysical [ 1 ], 1 , 3 );
1142
+
1143
+ if ( resolution .length > 2 )
1144
+ offsetTransform .set ( offsetPhysical [ 2 ], 2 , 3 );
1145
+
1146
+ final AffineTransform3D pixelRenderToPhysical = new AffineTransform3D ();
1147
+ pixelRenderToPhysical .concatenate ( resolutionTransform );
1148
+ pixelRenderToPhysical .concatenate ( offsetTransform );
1149
+
1150
+ // render and write
1151
+ final String srcName = sourceAndConverter .getSpimSource ().getName ();
1152
+ final BigWarpExporter <?> exporter = BigWarpExporter .getExporter ( data ,
1153
+ sourceAndConverter , interp , progressWriter );
1154
+ exporter .setRenderResolution ( resolution );
1155
+ exporter .setOffset ( offsetPixel );
1156
+ exporter .setInterval (Intervals .zeroMin (outputInterval ));
1157
+ exporter .setSingleChannelNoStack (true );
1158
+ final RandomAccessibleInterval <T > imgExp = (RandomAccessibleInterval <T >)exporter .exportRai ((Source <T >)sourceAndConverter .getSpimSource ());
1159
+ final IntervalView <T > img = Views .translateInverse ( imgExp , Intervals .minAsLongArray ( imgExp ));
1160
+
1161
+ RandomAccessibleInterval <T > imgToWrite ;
1162
+ if ( nd == 2 )
1163
+ imgToWrite = Views .hyperSlice ( img , 2 , 0 );
1164
+ else
1165
+ imgToWrite = img ;
1166
+
1167
+ final String destDataset = dataset ;
1168
+
1169
+ final OmeNgffMetadata metadata = OmeNgffMetadata .buildForWriting (nd , srcName , axes , new String []{"s0" },
1170
+ new double [][]{resolution }, new double [][]{offsetPhysical });
1171
+
1172
+ try
1173
+ {
1174
+ N5Utils .save ( imgToWrite , n5 , destDataset + "/s0" , blockSize , compression , exec );
1175
+ if ( parser != null && metadata != null )
1176
+ parser .writeMetadata ( metadata , n5 , destDataset );
1177
+
1178
+ n5 .close ();
1179
+ }
1180
+ catch ( final Exception e )
1181
+ {
1182
+ e .printStackTrace ();
1183
+ }
1184
+
1185
+
1186
+ progressWriter .setProgress ( 1.0 );
1187
+ System .out .println ("done" );
1188
+ }
1189
+
1077
1190
@ Override
1078
1191
public void run ( final String arg )
1079
1192
{
0 commit comments