Getting and setting a non-linear VOI LUT Example for C#

//LEADDICOM1 is a DICOM Dataset defined outside this method
private void GettingSettingNonlinearVOILUT()
{
   // This example will add a new VOI LUT to the dataset or
   // replace the existing one(s)
   LTDICLib.LVOILUTAttributes VOILUTAttributes = null;
   short iRet = 0;
   int lVOILUTCount = 0;
   int lLUTIndex = 0;
   int lNewLUTIndex = 0;
   int lDataSize = 0;
   byte[] LUTData = null ;
   bool bAddVOILUT = false;
   bAddVOILUT = true;
   lVOILUTCount = 0;
   lNewLUTIndex = 0;
   // Get the number of VOI LUT(s) in the file
   lVOILUTCount = LEADDICOM1.VOILUTCount;
   if (lVOILUTCount > 0)
   {
      object objLUTData = null ;
      // Get he attributes of the first VOI LUT
      iRet = LEADDICOM1.GetVOILUTAttributes(0, 0);
      if (iRet != 0)
      {
         MessageBox.Show("error");
         return;
      }
      VOILUTAttributes = LEADDICOM1.VOILUTAttributes;
      lDataSize = VOILUTAttributes.LUTDescriptorNumberOfEntries;
      // Get the LUT data
      LUTData = new byte [lDataSize];
      objLUTData = LUTData ;
      iRet = LEADDICOM1.GetVOILUTData(0, ref objLUTData, 0);
      if (iRet != 0)
      {
         MessageBox.Show("error");
         return;
      }
      // Remap the data
      for (lLUTIndex = 0; lLUTIndex < lDataSize; lLUTIndex++)
      {
         LUTData[lLUTIndex] = (byte)(LUTData[lLUTIndex] / 2);
      }
   }
   else
   {
      VOILUTAttributes = LEADDICOM1.VOILUTAttributes;
      // Define our own LUT
      VOILUTAttributes.LUTDescriptorFirstMapped = 0;
      VOILUTAttributes.LUTDescriptorEntryBits = 16;
      VOILUTAttributes.LUTDescriptorNumberOfEntries = 0X10000;
      lDataSize = VOILUTAttributes.LUTDescriptorNumberOfEntries;
      LUTData = new byte [lDataSize];
      for (lLUTIndex = 0; lLUTIndex < lDataSize; lLUTIndex++)
      {
         LUTData[lLUTIndex] = (byte)lLUTIndex;
      }
   }
   lNewLUTIndex = lVOILUTCount;
   if (bAddVOILUT == false)
   {
      // Delete existing LUT
      iRet = LEADDICOM1.DeleteVOILUT(0);
      if (iRet != 0)
      {
         MessageBox.Show("error");
         return;
      }
      lNewLUTIndex = 0;
   }
   // Set the new LUT
   iRet = LEADDICOM1.SetVOILUT(lNewLUTIndex, LUTData, 0);
   if (iRet != 0)
   {
      MessageBox.Show("error");
      return;
   }
}