/* * The Npic library and tools * * Copyright (C) 2003 Edouard Thiel * * This program is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* * npic-slice.c - 14/01/2009 * */ #include void ShowUsage () { printf ( "npic-slice - Extract a slice from a volume or hypervolume.\n" "Usage:\n" " npic-slice -h | -help | --help : print help\n" " npic-slice in1 out1 [options] : cut in1 to out1\n" "\n" "Options:\n" " -c|-l|-d : convert pixel type to char, long, double\n" " -x|-y|-z|-t|-s|-r k : cut slice k for given coordinate.\n" " different slices can be achieved at the same time. Each slice decreases\n" " the dimension; the result must have at least dimension 2.\n" "\n" "in1, out1 : image in " NPIC_KNOWN_IMAGE_EXT " format\n" "\n" "Example: SEDT on a volume, shown slice by slice (please open a wide terminal!)\n" " ./npic-sedt -dt -hirata ../images/klette_3c.pan.gz tmp1.npz\n" " for ((i = 24; i < 46 ; i++)); do\n" " echo \"Slice x=$i\"\n" " if ./npic-slice tmp1.npz tmp2.npz -x $i\n" " then ./npic-print tmp2.npz | sed -e 's/ 0/ ./g'\n" " else break ; fi ; sleep 1\n" " done\n" "\n" ); } void print_size (Npic_image *np1) { if (np1 == NULL || np1->gen.ok != NPIC_SUCCESS) return; printf ("Image size: "); if (np1->gen.dim >= 6) printf ("%d x ", np1->gen.rmax); if (np1->gen.dim >= 5) printf ("%d x ", np1->gen.smax); if (np1->gen.dim >= 4) printf ("%d x ", np1->gen.tmax); if (np1->gen.dim >= 3) printf ("%d x ", np1->gen.zmax); printf ("%d x %d\n", np1->gen.ymax, np1->gen.xmax); } void ArgcExit (int argc, int n) { if (argc < n) { fprintf (stderr, "ERROR: %d argument(s) missing, " "type \"npic-slice -h\" to get help.\n", n-argc); exit (1); } } void ArgShift (int *argc, char **argv[], int n) { *argc -= n; *argv += n; } int main (int argc, char *argv[]) { char *in1, *out1; Npic_image *np1, *np2 = NULL; int kx = -1, ky = -1, kz = -1, kt = -1, ks = -1, kr = -1, conv = NPIC_NONE; ArgcExit (argc, 2); ArgcExit (argc, 1+1); if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "-help") == 0 || strcmp (argv[1], "--help") == 0) { ShowUsage (); exit (0); } ArgcExit (argc, 2+1); in1 = argv[1]; out1 = argv[2]; ArgShift (&argc, &argv, 2); while (argc > 1) { if (strcmp (argv[1], "-x") == 0) { ArgcExit (argc, 2+1); kx = atoi(argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-y") == 0) { ArgcExit (argc, 2+1); ky = atoi(argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-z") == 0) { ArgcExit (argc, 2+1); kz = atoi(argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-t") == 0) { ArgcExit (argc, 2+1); kt = atoi(argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-s") == 0) { ArgcExit (argc, 2+1); ks = atoi(argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-r") == 0) { ArgcExit (argc, 2+1); kr = atoi(argv[2]); ArgShift (&argc, &argv, 2); } else if (strcmp (argv[1], "-c") == 0) { conv = NPIC_C; ArgShift (&argc, &argv, 1); } else if (strcmp (argv[1], "-l") == 0) { conv = NPIC_L; ArgShift (&argc, &argv, 1); } else if (strcmp (argv[1], "-d") == 0) { conv = NPIC_D; ArgShift (&argc, &argv, 1); } else { fprintf (stderr, "ERROR: unknown argument \"%s\", " "type \"npic-slice -h\" to get help.\n", argv[1]); exit (1); } } printf ("Loading \"%s\"\n", in1); np1 = NpicReadImage (in1); if (np1 == NULL) exit (1); switch (conv) { case NPIC_C : NpicConvertImage_c (np1); break; case NPIC_L : NpicConvertImage_l (np1); break; case NPIC_D : NpicConvertImage_d (np1); break; } print_size (np1); /* Cut slices in order r,s,t,z,y,x to preserve coordinates meaning */ if (np1 != NULL && kr != -1) { np2 = NpicCreateSlice_r (np1, kr); NpicDestroyImage (np1); np1 = np2; np2 = NULL; print_size (np1); } if (np1 != NULL && ks != -1) { np2 = NpicCreateSlice_s (np1, ks); NpicDestroyImage (np1); np1 = np2; np2 = NULL; print_size (np1); } if (np1 != NULL && kt != -1) { np2 = NpicCreateSlice_t (np1, kt); NpicDestroyImage (np1); np1 = np2; np2 = NULL; print_size (np1); } if (np1 != NULL && kz != -1) { np2 = NpicCreateSlice_z (np1, kz); NpicDestroyImage (np1); np1 = np2; np2 = NULL; print_size (np1); } if (np1 != NULL && ky != -1) { np2 = NpicCreateSlice_y (np1, ky); NpicDestroyImage (np1); np1 = np2; np2 = NULL; print_size (np1); } if (np1 != NULL && kx != -1) { np2 = NpicCreateSlice_x (np1, kx); NpicDestroyImage (np1); np1 = np2; np2 = NULL; print_size (np1); } if (np1 == NULL) exit (1); printf ("Saving \"%s\"\n", out1); if (NpicWriteImage (np1, out1) != NPIC_SUCCESS) exit(1); NpicDestroyImage (np1); exit (0); }