r2130: *** empty log message ***
[ctsim.git] / libctsim / ctndicom.cpp
index 02a4ed2da10073da89d6ea2a24df288e66ffc580..4a96d02bd1470bc097dcb2c9bf57e000b52a48f6 100644 (file)
@@ -9,7 +9,7 @@
 **  This is part of the CTSim program
 **  Copyright (c) 1983-2001 Kevin Rosenberg
 **
-**  $Id: ctndicom.cpp,v 1.8 2001/03/07 16:52:52 kevin Exp $
+**  $Id: ctndicom.cpp,v 1.14 2002/05/08 08:55:45 kevin Exp $
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License (version 2) as
 
 
 DicomImporter::DicomImporter (const char* const pszFile)
-  : m_strFilename(pszFile), m_bFail(false), m_iContents(DICOM_CONTENTS_INVALID), 
+  : m_pFile(NULL), m_strFilename(pszFile), m_bFail(false), m_iContents(DICOM_CONTENTS_INVALID), 
   m_pImageFile(NULL), m_pProjections(NULL)
 {
-  unsigned long lOptions = DCM_ORDERLITTLEENDIAN;
+  unsigned long lOptions = DCM_ORDERLITTLEENDIAN | DCM_FORMATCONVERSION;
   DCM_Debug (FALSE);
-  if (DCM_OpenFile (pszFile, lOptions, &m_pFile) != DCM_NORMAL) {
-    m_bFail = true;;
-    m_strFailMessage = "Can't open file ";
-    m_strFailMessage += m_strFilename;
+
+  CONDITION cond = DCM_OpenFile (pszFile, lOptions, &m_pFile);
+  if (cond != DCM_NORMAL) {
+    m_bFail = true;
+    char textbuf [2048]; 
+    CONDITION cond2 = COND_TopCondition (&cond, textbuf, sizeof(textbuf));
+    cond2 = DCM_NORMAL; // testing
+    if (cond2 != DCM_NORMAL) {
+      m_strFailMessage = "DCM_OpenFile failure: ";
+      m_strFailMessage += m_strFilename;
+    } else
+      m_strFailMessage = textbuf;
+
     return;
   }
 
@@ -154,7 +163,7 @@ DicomImporter::loadImage(unsigned short iNRows, unsigned short iNCols, unsigned
   if (iMaskLength > 8)
     iMaskLength -= 8;
   unsigned int iMask = (1 << iMaskLength) - 1;
-  for (int iy = iNRows - 1; iy >= 0; iy--) {
+  for (int iy = 0; iy < iNRows; iy++) {
     for (int ix = 0; ix < iNCols; ix++) {
       if (iBitsAllocated == 8)  {
         unsigned char cV = pRawPixels[iy * iNRows + ix];
@@ -164,7 +173,7 @@ DicomImporter::loadImage(unsigned short iNRows, unsigned short iNCols, unsigned
         unsigned char cV1 = pRawPixels[lBase];
         unsigned char cV2 = pRawPixels[lBase+1] & iMask;
         int iV = cV1 + (cV2 << 8);
-        v[ix][iy] = iV * dRescaleSlope + dRescaleIntercept;
+        v[ix][iNRows - 1 - iy] = iV * dRescaleSlope + dRescaleIntercept;
       }
     }
   }
@@ -191,7 +200,7 @@ DicomImporter::loadProjections()
   }
 
   DCM_TAG somatomTag = DCM_MAKETAG(TAG_GROUP_SOMATOM, TAG_MEMBER_SOMATOM_DATA);
-  DCM_ELEMENT elemProjections = {somatomTag, DCM_UNKNOWN, "", 1, 0, NULL};
+  DCM_ELEMENT elemProjections = {somatomTag, DCM_UN, "", 1, 0, NULL};
   if (DCM_GetElementSize (&m_pFile, elemProjections.tag, &lRtnLength) != DCM_NORMAL) {
     m_bFail = true;
     m_strFailMessage = "Can't find projection data";
@@ -284,7 +293,7 @@ DicomExporter::createDicomObject()
     dWidth = 1E-7;
   double dScale = 65535. / dWidth;
 
-  double dRescaleIntercept = -dMin;
+  double dRescaleIntercept = dMin;
   double dRescaleSlope = 1 / dScale;
   char szRescaleIntercept[17];
   char szRescaleSlope[17];
@@ -319,6 +328,12 @@ DicomExporter::createDicomObject()
   char szIDImageType[] = "ORIGINAL";
   char szIDManufacturerModel[65] = "";
 
+  std::ostringstream osPatComments;
+  m_pImageFile->printLabelsBrief (osPatComments);
+  size_t sizePatComments = osPatComments.str().length();
+  char* pszPatComments = new char [sizePatComments+1];
+  strncpy (pszPatComments, osPatComments.str().c_str(), sizePatComments);
+
   snprintf (szIDSOPInstanceUID, sizeof(szIDSOPInstanceUID), "%s.2.1.6.1", szCTSimRoot);
   snprintf (szRelStudyInstanceUID, sizeof(szRelStudyInstanceUID), "%s.2.1.6.1.1", szCTSimRoot);
   snprintf (szRelFrameOfReferenceUID, sizeof(szRelFrameOfReferenceUID), "%s.99", szCTSimRoot);
@@ -378,13 +393,13 @@ DicomExporter::createDicomObject()
     {DCM_IDMODALITY, DCM_CS, "", 1, strlen(szModality), szModality},
     {DCM_IDSOPCLASSUID, DCM_UI, "", 1, strlen(szSOPClassUID), szSOPClassUID},
     {DCM_IDMANUFACTURERMODEL, DCM_LO, "", 1, strlen(szIDManufacturerModel), szIDManufacturerModel},
+    {DCM_PATCOMMENTS, DCM_LT, "", 1, strlen(pszPatComments), pszPatComments},
   };
   int nElemRequired = sizeof (aElemRequired) / sizeof(DCM_ELEMENT);
 
   int iUpdateCount;
   cond = DCM_ModifyElements (&m_pObject, aElemRequired, nElemRequired, NULL, 0, &iUpdateCount);
 
-
   DCM_ELEMENT elemPixelData = {DCM_PXLPIXELDATA, DCM_OT, "", 1, 0, NULL};
 
   unsigned long lRealLength = 2 * m_pImageFile->nx() * m_pImageFile->ny();
@@ -394,10 +409,10 @@ DicomExporter::createDicomObject()
   elemPixelData.d.ot = pRawPixels;
   
   ImageFileArray v = m_pImageFile->getArray();
-  for (int iy = iNRows - 1; iy >= 0; iy--) {
+  for (int iy = 0; iy < iNRows; iy++) {
     for (int ix = 0; ix < iNCols; ix++) {
         unsigned long lBase = (iy * iNRows + ix) * 2;
-        unsigned int iValue = nearest<int>(dScale * (v[ix][iy] - dMin));
+        unsigned int iValue = nearest<int>(dScale * (v[ix][iNRows - 1 - iy] - dMin));
         pRawPixels[lBase] = iValue & 0xFF;
         pRawPixels[lBase+1] = (iValue & 0xFF00) >> 8;
     }
@@ -405,6 +420,8 @@ DicomExporter::createDicomObject()
   cond = DCM_ModifyElements (&m_pObject, &elemPixelData, 1, NULL, 0, &iUpdateCount);
   delete pRawPixels;
 
+  delete pszPatComments;
+
   if (cond != DCM_NORMAL || iUpdateCount != 1) {
     m_bFail = true;
     m_strFailMessage = "Error modifying pixel data";