/**
 * Hide a layer.
 *
 * @param[in] [1]
 *            The name of the layer to hide.
 * @param[in] WINDOW @opt
 *            The name of the window. Defaults to the active window.
 * @rsof
 * @usage print,hide_layer("raster.tif")
 * @endusage
 */
IDL_VPTR hide_layer(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   IDL_VPTR idlPtr;
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int windowExists;
      IDL_STRING windowName;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"WINDOW", IDL_TYP_STRING, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(windowName))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);

   std::string windowName;
   std::string layerName;

   if (kw->windowExists)
   {
      windowName = IDL_STRING_STR(&kw->windowName);
   }
   bool bSuccess = false;
   if (argc < 1)
   {
      IDL_Message(IDL_M_GENERIC, IDL_MSG_RET, "function takes a layer name as a parameter with "
         "'window' as an optional keyword.");
      return IDL_StrToSTRING("failure");
   }
   //the layer name as a parameter
   layerName = IDL_VarGetString(pArgv[0]);
   SpatialDataView* pView = dynamic_cast<SpatialDataView*>(IdlFunctions::getViewByWindowName(windowName));
   if (pView != NULL)
   {
      Layer* pLayer = IdlFunctions::getLayerByName(windowName, layerName, false);
      if (pLayer != NULL)
      {
         pView->hideLayer(pLayer);
         bSuccess = true;
      }
   }
   if (bSuccess)
   {
      idlPtr = IDL_StrToSTRING("success");
   }
   else
   {
      idlPtr = IDL_StrToSTRING("failure");
   }
   return idlPtr;
}
/**
 * Get the current position of a layer in the layer list.
 *
 * @param[in] [1]
 *            The name of the layer.
 * @param[in] WINDOW @opt
 *            The name of the window. Defaults to the active window.
 * @return The 0 based position of the layer in the layer list.
 * @usage idx = get_layer_position("data.tif")
 * @endusage
 */
IDL_VPTR get_layer_position(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int windowExists;
      IDL_STRING windowName;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"WINDOW", IDL_TYP_STRING, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(windowName))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);

   std::string windowName;
   std::string name;
   int index = -1;

   if (kw->windowExists)
   {
      windowName = IDL_STRING_STR(&kw->windowName);
   }

   if (argc < 1)
   {
      IDL_Message(IDL_M_GENERIC, IDL_MSG_RET, "get_layer_position takes a layer name as a parameter with a "
         "window as an optional keyword to specify a non current window.");
   }
   else
   {
      //layer name passed in as a parameter
      name = IDL_VarGetString(pArgv[0]);
      SpatialDataView* pView = dynamic_cast<SpatialDataView*>(IdlFunctions::getViewByWindowName(windowName));
      if (pView != NULL)
      {
         Layer* pLayer = IdlFunctions::getLayerByName(windowName, name, false);
         if (pLayer != NULL)
         {
            index = pView->getLayerDisplayIndex(pLayer);
         }
      }
   }
   return IDL_GettmpInt(index);
}
/**
 * Get the name of the layer in a given position in the layer list.
 *
 * @param[in] INDEX @opt
 *            The 0 based index of the layer. Defaults to the top most layer.
 * @param[in] WINDOW @opt
 *            The name of the window. Defaults to the active window.
 * @return The name of the layer.
 * @usage print,get_layer_name(2, WINDOW="Window 1")
 * @endusage
 */
IDL_VPTR get_layer_name(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int windowExists;
      IDL_STRING windowName;
      int indexExists;
      IDL_LONG index;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"INDEX", IDL_TYP_LONG, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(indexExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(index))},
      {"WINDOW", IDL_TYP_STRING, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(windowName))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);

   std::string windowName;
   int index = -1;
   std::string name;

   if (kw->indexExists)
   {
      index = kw->index;
   }
   if (kw->windowExists)
   {
      windowName = IDL_STRING_STR(&kw->windowName);
   }

   if (argc < 0)
   {
      IDL_Message(IDL_M_GENERIC, IDL_MSG_RET, "Invalid arguments");
      return IDL_StrToSTRING("");
   }
   Layer* pLayer = IdlFunctions::getLayerByIndex(windowName, index);
   if (pLayer != NULL)
   {
      name =  pLayer->getName();
   }
   return IDL_StrToSTRING(const_cast<char*>(name.c_str()));
}
/**
 * Get the number of layers in a window.
 *
 * @param[in] WINDOW @opt
 *            The name of the window. Defaults to the active window.
 * @return The number of layers in the window.
 * @usage num_layers = get_num_layers()
 * @endusage
 */
IDL_VPTR get_num_layers(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int windowExists;
      IDL_STRING windowName;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"WINDOW", IDL_TYP_STRING, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(windowName))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);

   std::string windowName;
   int layers = 0;

   if (kw->windowExists)
   {
      windowName = IDL_STRING_STR(&kw->windowName);
   }

   SpatialDataView* pView = dynamic_cast<SpatialDataView*>(IdlFunctions::getViewByWindowName(windowName));
   if (pView != NULL)
   {
      LayerList* pList = pView->getLayerList();
      if (pList != NULL)
      {
         layers = pList->getNumLayers();
      }
   }
   return IDL_GettmpInt(layers);
}
Example #5
0
static void IDL_CDECL IDL_mg_print(int argc, IDL_VPTR *argv, char *argk) {
  int nargs;
  char *format, *cformat;
  IDL_VPTR origFormat, vcformat;

  typedef struct {
    IDL_KW_RESULT_FIRST_FIELD;
    IDL_VPTR format;
    int format_present;
  } KW_RESULT;

  static IDL_KW_PAR kw_pars[] = {
    { "FORMAT", IDL_TYP_STRING, 1, IDL_KW_VIN,
      IDL_KW_OFFSETOF(format_present), IDL_KW_OFFSETOF(format) },
    { NULL }
  };

  KW_RESULT kw;

  nargs = IDL_KWProcessByOffset(argc, argv, argk, kw_pars, NULL, 1, &kw);

  if (kw.format_present) {
    origFormat = argv[argc - 1];
    format = IDL_VarGetString(origFormat);
    cformat = (char *) calloc(strlen(format) + 5 + 1, sizeof(char));
    sprintf(cformat, "(%%\"%s\")", format);
    vcformat = IDL_StrToSTRING(cformat);
    argv[argc - 1] = vcformat;
  }

  IDL_Print(argc, argv, argk);

  if (kw.format_present) {
    argv[argc - 1] = origFormat;
    IDL_Deltmp(vcformat);
    free(cformat);
  }

  IDL_KW_FREE;
}
/**
 * Get the name of the current data element or window.
 *
 * @param[in] DATASET @opt
 *            This flag gets the primary data element name of the currently active window.
 *            This is the default.
 * @param[in] FILE @opt
 *            This flag gets the currently active window's primary data element's file name.
 * @param[in] WINDOW @opt
 *            This flag gets the name of the currently active window.
 * @return The name of the element.
 * @usage print,get_current_name(/FILE)
 * @endusage
 */
IDL_VPTR get_current_name(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int datasetExists;
      IDL_LONG dataset;
      int fileExists;
      IDL_LONG file;
      int windowExists;
      IDL_LONG window;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"DATASET", IDL_TYP_INT, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(datasetExists)),
            reinterpret_cast<char*>(IDL_KW_OFFSETOF(dataset))},
      {"FILE", IDL_TYP_INT, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(fileExists)),
            reinterpret_cast<char*>(IDL_KW_OFFSETOF(file))},
      {"WINDOW", IDL_TYP_INT, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
            reinterpret_cast<char*>(IDL_KW_OFFSETOF(window))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);

   std::string filename;
   std::string wizardName;
   int file = 0;
   int dataset = 0;
   int window = 0;

   if (kw->datasetExists)
   {
      if (kw->dataset != 0)
      {
         dataset = 1;
      }
   }
   else if (kw->fileExists)
   {
      if (kw->file != 0)
      {
         file = 1;
      }
   }
   else if (kw->windowExists)
   {
      if (kw->window != 0)
      {
         window = 1;
      }
   }
   else
   {
      dataset = 1;
   }

   RasterElement* pElement = IdlFunctions::getDataset("");
   if (pElement == NULL)
   {
      IDL_Message(IDL_M_GENERIC, IDL_MSG_RET, "Opticks was unable to determine the current in memory dataset.");
      return IDL_StrToSTRING("");
   }
   std::string name;
   if (dataset != 0)
   {
      // get the name of the RasterElement if dataset
      name = pElement->getName();
   }
   else if (file)
   {
      // get the filename associated with the RasterElement
      name = pElement->getFilename();
   }
   else
   {
      // get the window name
      Service<DesktopServices>()->getCurrentWorkspaceWindowName(name);
   }
   return IDL_StrToSTRING(const_cast<char*>(name.c_str()));
}
/**
 * Get the names of all the data elements which are children of the primary raster element.
 *
 * @param[in] WINDOW @opt
 *            The name of the window. Defaults to the active window.
 * @return An array of data element names or the string "failure" if an error occurred.
 * @usage names = get_data_element_names()
 * @endusage
 */
IDL_VPTR get_data_element_names(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int windowExists;
      IDL_STRING windowName;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"WINDOW", IDL_TYP_STRING, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(windowName))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);
   std::string windowName;
   std::string name;
   bool bSuccess = false;
   IDL_VPTR idlPtr;
   unsigned int total = 0;
   IDL_STRING* pStrarr = NULL;

   if (kw->windowExists)
   {
      windowName = IDL_STRING_STR(&kw->windowName);
   }

   SpatialDataWindow* pWindow = NULL;
   if (windowName.empty())
   {
      pWindow = dynamic_cast<SpatialDataWindow*>(Service<DesktopServices>()->getCurrentWorkspaceWindow());
   }
   else
   {
      pWindow = dynamic_cast<SpatialDataWindow*>(
         Service<DesktopServices>()->getWindow(windowName, SPATIAL_DATA_WINDOW));
   }
   if (pWindow != NULL)
   {
      SpatialDataView* pView = pWindow->getSpatialDataView();
      if (pView != NULL)
      {
         LayerList* pList = pView->getLayerList();
         if (pList != NULL)
         {
            RasterElement* pElement = pList->getPrimaryRasterElement();
            if (pElement != NULL)
            {
               std::vector<std::string> names = Service<ModelServices>()->getElementNames(pElement, "");
               total = names.size();
               if (total > 0)
               {
                  pStrarr = reinterpret_cast<IDL_STRING*>(malloc(total * sizeof(IDL_STRING)));
                  for (unsigned int i=0; i < total; ++i)
                  {
                     IDL_StrStore(&(pStrarr[i]), const_cast<char*>(names[i].c_str()));
                  }
                  bSuccess = true;
               }
            }
         }
      }
   }
   else if (windowName == "all")
   {
      std::vector<std::string> names = Service<ModelServices>()->getElementNames("RasterElement");
      total = names.size();
      if (total > 0)
      {
         pStrarr = reinterpret_cast<IDL_STRING*>(malloc(total* sizeof(IDL_STRING)));
         for (unsigned int i=0; i < total; ++i)
         {
            IDL_StrStore(&(pStrarr[i]), const_cast<char*>(names[i].c_str()));
         }
         bSuccess = true;
      }
   }
   if (!bSuccess)
   {
      IDL_Message(IDL_M_GENERIC, IDL_MSG_RET, "No elements matched.");
      return IDL_StrToSTRING("failure");
   }
   IDL_MEMINT dims[] = {total};
   idlPtr = IDL_ImportArray(1, dims, IDL_TYP_STRING, reinterpret_cast<UCHAR*>(pStrarr),
      reinterpret_cast<IDL_ARRAY_FREE_CB>(free), NULL);
   return idlPtr;
}
/**
 * Get the data element name of a specified layer.
 *
 * @param[in] [1] @opt
 *            The name of the layer. Defaults to the top most layer.
 * @param[in] WINDOW @opt
 *            The name of the window. Defaults to the active window.
 * @param[in] DATASET @opt
 *            If \p [1] is not specified and this flag it set, get the
 *            data set name of the top most raster layer.
 * @return The name of the data element.
 * @usage print,get_data_name(/DATASET)
 * @endusage
 */
IDL_VPTR get_data_name(int argc, IDL_VPTR pArgv[], char* pArgk)
{
   typedef struct
   {
      IDL_KW_RESULT_FIRST_FIELD;
      int windowExists;
      IDL_STRING windowName;
      int datasetExists;
      IDL_LONG dataset;
   } KW_RESULT;

   //IDL_KW_FAST_SCAN is the type of scan we are using, following it is the
   //name of the keyword, followed by the type, the mask(which should be 1),
   //flags, a boolean whether the value was populated and finally the value itself
   static IDL_KW_PAR kw_pars[] = {
      IDL_KW_FAST_SCAN,
      {"DATASET", IDL_TYP_INT, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(datasetExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(dataset))},
      {"WINDOW", IDL_TYP_STRING, 1, 0, reinterpret_cast<int*>(IDL_KW_OFFSETOF(windowExists)),
         reinterpret_cast<char*>(IDL_KW_OFFSETOF(windowName))},
      {NULL}
   };

   IdlFunctions::IdlKwResource<KW_RESULT> kw(argc, pArgv, pArgk, kw_pars, 0, 1);
   std::string windowName;
   std::string layerName;
   std::string name;

   if (kw->windowExists)
   {
      windowName = IDL_STRING_STR(&kw->windowName);
   }

   //retrieve the layer name passed in as a parameter
   if (argc >= 1)
   {
      layerName = IDL_VarGetString(pArgv[0]);
   }
   //get the layer
   bool datasets = false;
   if (kw->datasetExists)
   {
      if (kw->dataset != 0)
      {
         datasets = true;
      }
   }
   Layer* pLayer = IdlFunctions::getLayerByName(windowName, layerName, datasets);
   if (pLayer != NULL)
   {
      //get the spectral element of the layer and return its name
      DataElement* pElement = pLayer->getDataElement();
      if (pElement != NULL)
      {
         name = pElement->getName();
      }
   }
   else
   {
      IDL_Message(IDL_M_GENERIC, IDL_MSG_RET, "the layer name passed into get_data_name "
         "was invalid.");
      return IDL_StrToSTRING("");
   }

   return IDL_StrToSTRING(const_cast<char*>(name.c_str()));
}
IDL_VPTR p3d_idlGetMaxVolumeBlob(int argc, IDL_VPTR argv[], char* argk) {

    typedef struct {
        IDL_KW_RESULT_FIRST_FIELD; // Must be first entry in structure
        IDL_LONG conn;
        int cn_there;
    } KW_RESULT;

    // Alphabetical order is crucial:
    static IDL_KW_PAR kw_pars[] = {
        IDL_KW_FAST_SCAN,
        { "CONN", IDL_TYP_LONG, 1, 0, (int*) IDL_KW_OFFSETOF(cn_there), (char*) IDL_KW_OFFSETOF(conn)},
        { NULL}
    };

    KW_RESULT kw;

    IDL_VPTR idl_out_rev, idl_in_rev;
    unsigned char *in_rev8, *out_rev8;
    int keywords_ct = 0;

    int conn2D = CONN4;
    int conn3D = CONN6;

    int err_code;

    // Process keywords:
    IDL_KWProcessByOffset(argc, argv, argk, kw_pars, NULL, 1, &kw);


    // Get input data in IDL format:
    idl_in_rev = argv[0];

    IDL_ENSURE_SIMPLE(idl_in_rev);
    IDL_ENSURE_ARRAY(idl_in_rev);


    // Get the CONN input argument:
    if (kw.cn_there) {
        if (idl_in_rev->value.arr->n_dim == 2) {
            // Check values:
            if ((kw.conn != 4) && (kw.conn != 8))
                _p3d_idlPrintNamedError("CONN must be a value of the set {4,8}.");

            // Get values:
            if (kw.conn == 4)
                conn2D = CONN4;
            else if (kw.conn == 8)
                conn2D = CONN8;
        } else if (idl_in_rev->value.arr->n_dim == 3) {
            // Check values:
            if ((kw.conn != 6) && (kw.conn != 18) && (kw.conn != 26))
                _p3d_idlPrintNamedError("CONN must be a value of the set {6,18,26}.");

            // Get values:
            if (kw.conn == 6)
                conn3D = CONN6;
            else if (kw.conn == 18)
                conn3D = CONN18;
            else if (kw.conn == 26)
                conn3D = CONN26;
        }
        // else: error on input arguments with further handling.

        keywords_ct++;
    }


    // Call Pore3D depending on input arguments:
	if (idl_in_rev->value.arr->n_dim == 3) {
        // Extract first input (volume to filter) in C format:
        if (idl_in_rev->type == IDL_TYP_BYTE) {
            in_rev8 = (unsigned char *) idl_in_rev->value.arr->data;

            // Allocate memory for output:
            if (!(idl_in_rev->flags & IDL_V_TEMP))
                out_rev8 = (unsigned char *) IDL_MakeTempArray(
                    IDL_TYP_BYTE,
                    idl_in_rev->value.arr->n_dim,
                    idl_in_rev->value.arr->dim,
                    IDL_ARR_INI_NOP,
                    &idl_out_rev
                    );


            // Call Pore3D:
            err_code = p3dGetMaxVolumeBlob3D(
                    in_rev8,
                    out_rev8,
                    (unsigned int) idl_in_rev->value.arr->dim[0],
                    (unsigned int) idl_in_rev->value.arr->dim[1],
                    (unsigned int) idl_in_rev->value.arr->dim[2],
                    conn3D,
                    _p3d_idlPrintInfo
                    );

            // On exception print error:
            if (err_code == P3D_ERROR)
                _p3d_idlPrintNamedError("Error on code execution.");
        }
        else {
            _p3d_idlPrintNamedError("Input argument IMAGE must be of type BYTE.");
        }
    } else {
        _p3d_idlPrintNamedError("Input argument IMAGE must be a 2D or 3D matrix.");
    }


    // Free resources:
    IDL_KW_FREE;

    // Return output in IDL Format
    return (idl_out_rev);
}
IDL_VPTR p3d_idlGaussianFilter(int argc, IDL_VPTR argv[], char* argk) {

	typedef struct {
		IDL_KW_RESULT_FIRST_FIELD; // Must be first entry in structure
		double sigma;
		int si_there;
		IDL_LONG nsize;
		int ns_there;
	} KW_RESULT;

	// Alphabetical order is crucial:
	static IDL_KW_PAR kw_pars[] = {
		IDL_KW_FAST_SCAN,
		{ "SIGMA", IDL_TYP_DOUBLE, 1, 0, (int*) IDL_KW_OFFSETOF(si_there), (char*) IDL_KW_OFFSETOF(sigma)},
		{ "WIDTH", IDL_TYP_LONG, 1, 0, (int*) IDL_KW_OFFSETOF(ns_there), (char*) IDL_KW_OFFSETOF(nsize)},
		{ NULL}
	};

	KW_RESULT kw;

	IDL_VPTR idl_out_rev, idl_in_rev;
	unsigned char *in_rev8, *out_rev8;
	unsigned short *in_rev16, *out_rev16;
	int keywords_ct = 0;

	int    width = 3;   // default
	double sigma = 1.0; // default

	int err_code;

	// Process keywords:
	IDL_KWProcessByOffset(argc, argv, argk, kw_pars, NULL, 1, &kw);


	// Get input data in IDL format:
	idl_in_rev = argv[0];

	IDL_ENSURE_SIMPLE(idl_in_rev);
	IDL_ENSURE_ARRAY(idl_in_rev);


	// Get the WIDTH input argument:
	if (kw.ns_there) {
		// Check values:
		if ((kw.nsize < 3) || (kw.nsize > 51))
			_p3d_idlPrintNamedError("WIDTH must be an odd value within the range [3,51].");

		if ((kw.nsize % 2) == 0)
			_p3d_idlPrintNamedError("WIDTH must be an odd value within the range [3,51].");

		// Get values:
		width = (int) kw.nsize;

		keywords_ct++;
	}

	// Get the SIGMA input argument:
	if (kw.si_there) {
		// Check values:
		if (kw.sigma < 0)
			_p3d_idlPrintNamedError("SIGMA must be greater than zero.");

		// Get values:
		sigma = (double) kw.sigma;

		keywords_ct++;
	}


	// Call Pore3D depending on input arguments:
	if (idl_in_rev->value.arr->n_dim == 3) {
		// Extract first input (volume to filter) in C format:
		if (idl_in_rev->type == IDL_TYP_BYTE) {
			in_rev8 = (unsigned char *) idl_in_rev->value.arr->data;

			// Allocate memory for output:
			if (!(idl_in_rev->flags & IDL_V_TEMP))
				out_rev8 = (unsigned char *) IDL_MakeTempArray(
				IDL_TYP_BYTE,
				idl_in_rev->value.arr->n_dim,
				idl_in_rev->value.arr->dim,
				IDL_ARR_INI_NOP,
				&idl_out_rev
				);



			// Call Pore3D:
			err_code = p3dGaussianFilter3D_8(
				in_rev8,
				out_rev8,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				(int) idl_in_rev->value.arr->dim[2],
				width,
				sigma,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");

		} else if (idl_in_rev->type == IDL_TYP_UINT) {
			in_rev16 = (unsigned short *) idl_in_rev->value.arr->data;

			// Allocate memory for output:
			if (!(idl_in_rev->flags & IDL_V_TEMP))
				out_rev16 = (unsigned short *) IDL_MakeTempArray(
				IDL_TYP_UINT,
				idl_in_rev->value.arr->n_dim,
				idl_in_rev->value.arr->dim,
				IDL_ARR_INI_NOP,
				&idl_out_rev
				);


			// Call Pore3D:
			err_code = p3dGaussianFilter3D_16(
				in_rev16,
				out_rev16,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				(int) idl_in_rev->value.arr->dim[2],
				width,
				sigma,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");

		} else {
			_p3d_idlPrintNamedError("Input argument IMAGE must be of type BYTE or UINT.");
		}
	} else {
		_p3d_idlPrintNamedError("Input argument IMAGE must be a 2D or 3D matrix.");
	}


	// Free resources:
	IDL_KW_FREE;

	// Return output in IDL Format
	return (idl_out_rev);
}
Example #11
0
void p3d_idlWriteRaw(int argc, IDL_VPTR argv[], char* argk) {

	typedef struct {
		IDL_KW_RESULT_FIRST_FIELD; // Must be first entry in structure
		IDL_LONG endian;
		IDL_LONG sign;
	} KW_RESULT;

	// Alphabetical order is crucial:
	static IDL_KW_PAR kw_pars[] = {
		IDL_KW_FAST_SCAN,
		{ "BIG_ENDIAN", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, (char*) IDL_KW_OFFSETOF(endian)},
		{ "UNSIGNED", IDL_TYP_LONG, 1, IDL_KW_ZERO, 0, (char*) IDL_KW_OFFSETOF(sign)},
		{ NULL}
	};

	KW_RESULT kw;
	IDL_VPTR idl_in_rev;

	char* filename;
	int err_code;

	int little_endian;
	int is_signed;

	unsigned char* in_rev8;
	unsigned short* in_rev16;
	unsigned int* in_rev32;
	float* in_rev32f;


	// Process keywords:
	IDL_KWProcessByOffset(argc, argv, argk, kw_pars, (IDL_VPTR *) 0, 1, &kw);

	little_endian = (kw.endian == 0) ? P3D_TRUE : P3D_FALSE;
	is_signed = (kw.sign == 0) ? P3D_TRUE : P3D_FALSE;

	// Get input data in IDL format:
	idl_in_rev = argv[0];

	IDL_ENSURE_SIMPLE(idl_in_rev);
	IDL_ENSURE_ARRAY(idl_in_rev);


	if (argv[1]->type == IDL_TYP_STRING)
		filename = IDL_VarGetString(argv[1]);
	else
		_p3d_idlPrintNamedError("Input argument FILENAME must be a string.");


	// Check if user wants to write a 8-bit or 16-bit format image:
	if (idl_in_rev->type == IDL_TYP_BYTE) {
		// Extract input in C format
		in_rev8 = (unsigned char *) idl_in_rev->value.arr->data;


		// Check if user wants to write a 2D image or a 3D volume:
		if (idl_in_rev->value.arr->n_dim == 2) {
			// Call Pore3D:
			err_code = p3dWriteRaw8(
				in_rev8,
				filename,
				(int) (idl_in_rev->value.arr->dim[0]),
				(int) (idl_in_rev->value.arr->dim[1]),
				1,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		} else if (idl_in_rev->value.arr->n_dim == 3) {


			// Call Pore3D:
			err_code = p3dWriteRaw8(
				in_rev8,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				(int) idl_in_rev->value.arr->dim[2],
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		} else {
			_p3d_idlPrintNamedError("Input argument IMAGE must be a 2D or 3D matrix.");
		}
	} else if (idl_in_rev->type == IDL_TYP_UINT) {
		// Extract input in C format
		in_rev16 = (unsigned short *) idl_in_rev->value.arr->data;

		// Check if user wants to write a 2D image or a 3D volume:
		if (idl_in_rev->value.arr->n_dim == 2) {


			// Call Pore3D:
			err_code = p3dWriteRaw16(
				in_rev16,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				1,
				little_endian,
				is_signed,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		} else if (idl_in_rev->value.arr->n_dim == 3) {


			// Call Pore3D:
			err_code = p3dWriteRaw16(
				in_rev16,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				(int) idl_in_rev->value.arr->dim[2],
				little_endian,
				is_signed,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");

		} else {
			_p3d_idlPrintNamedError("Input argument IMAGE must be a 2D or 3D matrix."); 
		}
	} else if (idl_in_rev->type == IDL_TYP_ULONG) {
		// Extract input in C format
		in_rev32 = (unsigned int *) idl_in_rev->value.arr->data;

		// Check if user wants to write a 2D image or a 3D volume:
		if (idl_in_rev->value.arr->n_dim == 2) {


			// Call Pore3D:
			err_code = p3dWriteRaw32(
				in_rev32,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				1,
				little_endian,
				is_signed,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		} else if (idl_in_rev->value.arr->n_dim == 3) {


			// Call Pore3D:
			err_code = p3dWriteRaw32(
				in_rev32,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				(int) idl_in_rev->value.arr->dim[2],
				little_endian,
				is_signed,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		}
		else {
			_p3d_idlPrintNamedError("Input argument IMAGE must be a 2D or 3D matrix.");
		}	
	} else if (idl_in_rev->type == IDL_TYP_FLOAT) {
		// Extract input in C format
		in_rev32f = (float *) idl_in_rev->value.arr->data;

		// Check if user wants to write a 2D image or a 3D volume:
		if (idl_in_rev->value.arr->n_dim == 2) {


			// Call Pore3D:
			err_code = p3dWriteRaw32f(
				in_rev32f,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				1,
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		} else if (idl_in_rev->value.arr->n_dim == 3) {


			// Call Pore3D:
			err_code = p3dWriteRaw32f(
				in_rev32f,
				filename,
				(int) idl_in_rev->value.arr->dim[0],
				(int) idl_in_rev->value.arr->dim[1],
				(int) idl_in_rev->value.arr->dim[2],
				_p3d_idlPrintInfo,
				NULL
				);

			// On exception print error:
			if ((err_code == P3D_IO_ERROR) || (err_code == P3D_ERROR))
				_p3d_idlPrintNamedError("Error on code execution.");
		}
		else {
			_p3d_idlPrintNamedError("Input argument IMAGE must be a 2D or 3D matrix.");
		}

	} else {
		_p3d_idlPrintNamedError("Input argument IMAGE must be a BYTE, UINT, ULONG or FLOAT matrix.");
	}


	// Free keywords resources:
	IDL_KW_FREE;
}