Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Diff of /panTools.cpp [9be1ae] .. [51520e] Maximize Restore

  Switch to side-by-side view

--- a/panTools.cpp
+++ b/panTools.cpp
@@ -43,10 +43,10 @@
 **/
 static dllEntry getAddr(char *func)
 {
-  dllEntry addr = (dllEntry) GetProcAddress(DLL.hpano13, func); 
+  dllEntry addr = (dllEntry) GetProcAddress(gData->dll.hpano13, func); 
   if (addr == NULL)
   {
-    FreeLibrary(DLL.hpano13);
+    FreeLibrary(gData->gData->dll.hpano13);
     display("Pano13.dll is invalid (%s).", func);
   }
   return addr;
@@ -57,22 +57,12 @@
 * @param[in] x the name of the function to retrieve the address
 **/
 #define SETFUNC(x)\
-  DLL. ## x = (x ## _proc)getAddr(#x);\
-  if (DLL. ## x == NULL) goto err_exit
-
+  gData->dll. ## x = (x ## _proc)getAddr(#x);\
+  if (gData->dll. ## x == NULL) goto err_exit
+
+#else
+extern  HINSTANCE hDllInstance;
 #endif
-
-//extern  HINSTANCE hDllInstance;
-//extern void SetWindowOwner(HWND owner);
-extern "C" BOOL APIENTRY DLLInit(HANDLE hInstance, DWORD fdwReason, LPVOID lpReserved);
-/*
-extern "C" void FourToThreeBPP(Image *im);
-extern "C" void CopyImageData(Image *dest, Image *src);
-extern "C" void SetImageDefaults(Image *im);
-extern "C" void filter_main(TrformStr *TrPtr, struct stitcher_Prefs *spref);
-extern "C" int writePSD(Image *im, fullPath* fname);
-extern "C" void myfree(void **hdl);
-*/
 
 /**
 * Load pano13.dll and initilize funtions needed.
@@ -81,50 +71,50 @@
 static int loadLibrary()
 {
 #if defined pano13DYNAMICLINK
-  {
-    // Get a handle to the DLL module.
-    DLL.hpano13 = LoadLibrary("pano13.dll");
-    if (DLL.hpano13 == NULL)
-    {
-      DWORD  err      = GetLastError();
-      LPVOID lpMsgBuf = NULL;
-      FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
-                     NULL, err, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
-      // This problem is happening sometimes on different systems, but not all the time.
-      // It is not because pano13 is not properly installed.  Hopefully by adding the Last Error 
-      // we can track down the problme.
-      display("Unable to load the pano13.dll file.\n%s", (LPCTSTR)lpMsgBuf);
-      // Free the buffer.
-      LocalFree( lpMsgBuf );
-      return 1;
-    }
-    // initialize all function pointers
-    SETFUNC(FourToThreeBPP);
-    SETFUNC(SetWindowOwner);
-    SETFUNC(CopyImageData);
-    SETFUNC(SetImageDefaults);
-    SETFUNC(filter_main);
-    SETFUNC(writePSD);
-    SETFUNC(myfree);
-
-    return 0;
+  // Get a handle to the DLL module.
+  gData->dll.hpano13 = LoadLibrary("pano13.dll");
+  if (gData->dll.hpano13 == NULL)
+  {
+    DWORD  err      = GetLastError();
+    LPVOID lpMsgBuf = NULL;
+    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
+                    NULL, err, 0, (LPTSTR) &lpMsgBuf, 0, NULL);
+    // This problem is happening sometimes on different systems, but not all the time.
+    // It is not because pano13 is not properly installed.  Hopefully by adding the Last Error 
+    // we can track down the problme.
+    display("Unable to load the pano13.dll file.\n%s", (LPCTSTR)lpMsgBuf);
+    // Free the buffer.
+    LocalFree( lpMsgBuf );
+    return 1;
+  }
+  // initialize all function pointers
+  SETFUNC(FourToThreeBPP);
+  SETFUNC(SetWindowOwner);
+  SETFUNC(CopyImageData);
+  SETFUNC(SetImageDefaults);
+  SETFUNC(filter_main);
+  SETFUNC(writePSD);
+  SETFUNC(writePSDwithLayer);
+  SETFUNC(myfree);
+
+  return 0;
 
 err_exit:
-  FreeLibrary(DLL.hpano13);	
+  FreeLibrary(gData->dll.hpano13);
   return 1;
-  }
+
 #else
-  {
-    DLL.FourToThreeBPP = FourToThreeBPP;
-    DLL.SetWindowOwner = SetWindowOwner;
-    DLL.CopyImageData = CopyImageData;
-    DLL.SetImageDefaults = SetImageDefaults;
-    DLL.filter_main = filter_main;
-    DLL.writePSD = writePSD;
-    DLL.myfree = myfree;
-
-    return 0;
-  }
+  gData->dll.FourToThreeBPP = FourToThreeBPP;
+  gData->dll.SetWindowOwner = SetWindowOwner;
+  gData->dll.CopyImageData = CopyImageData;
+  gData->dll.SetImageDefaults = SetImageDefaults;
+  gData->dll.filter_main = filter_main;
+  gData->dll.writePSD = writePSD;
+  gData->dll.writePSDwithLayer = writePSDwithLayer;
+  gData->dll.myfree = myfree;
+  hDllInstance = GetDLLInstance();
+
+  return 0;
 #endif
 }
 
@@ -162,12 +152,12 @@
   while(1)
   {
 #if _MSC_VER >= 1400
-    sprintf_s(buf, bufSize, "%s%d.%d.psd", PAN.sprefs.sFile.name, pre, i++);
+    sprintf_s(buf, bufSize, "%s%d.%d.psd", gData->pan.sprefs.sFile.name, pre, i++);
 #else
-    sprintf(buf, "%s%d.%d.psd", PAN.sprefs.sFile.name, pre, i++);
+    sprintf(buf, "%s%d.%d.psd", gData->pan.sprefs.sFile.name, pre, i++);
 #endif
     HANDLE hFile = CreateFile(
-        buf,				              // file name
+        buf,                      // file name
         GENERIC_READ,             // access mode
         FILE_SHARE_READ,          // share mode
         NULL,                     // security
@@ -194,152 +184,153 @@
   void         *alpha     = NULL;
   PlatformData *platform  = NULL;
 
-  if (!DLL.initDLL) 
+#ifdef pano13DYNAMICLINK
+  if (!gData->dll.initDLL) 
+  // If linking staticaly then we will call loadLibrary each time to ensure we are always in sync.
+#endif
   {
     if (loadLibrary())
     {
       return 1;
     }
-    DLL.initDLL = true;
+    gData->dll.initDLL = true;
   }
 
   platform = (PlatformData*)(gFr->platformData);
-
-  // initialize PHOtoshop settings
-  PHO.imageWidth = gFr->imageSize.h;
-  PHO.imageHeight = gFr->imageSize.v;	
-  PHO.imageDepth = gFr->depth;
+  gData->dll.SetWindowOwner((HWND)platform->hwnd);
+
+
+  // initialize Photoshop settings
+  gData->pho.imageWidth  = gFr->wholeSize.h;
+  gData->pho.imageHeight = gFr->wholeSize.v;
+  gData->pho.imageDepth  = gFr->depth;
+  gData->pho.imagePlanes = gFr->planes<=4?gFr->planes:4;// At most add just one Alpha Channel(image mask, clip mask)
 
   // PSP sets depth to zero!
-  if (PHO.imageDepth != 16)
-  {
-    PHO.imageDepth = 8;
-  }
-
-  PHO.imageChannelSize = PHO.imageWidth * PHO.imageHeight * (PHO.imageDepth/8);
-  PHO.imageSize = PHO.imageChannelSize * 3;
+  if (gData->pho.imageDepth != 16)
+  {
+    gData->pho.imageDepth = 8;
+  }
+  
+  gData->pho.imageChannelSize = gData->pho.imageWidth * gData->pho.imageHeight * (gData->pho.imageDepth/8);
+  gData->pho.imageSize = gData->pho.imageChannelSize * gData->pho.imagePlanes;
 
   // initialize images
-  DLL.SetImageDefaults(&PAN.image1);
-  DLL.SetImageDefaults(&PAN.image2);
+  gData->dll.SetImageDefaults(&gData->pan.image1);
+  gData->dll.SetImageDefaults(&gData->pan.image2);
 
   // assume no alpha channel needed
-  PAN.image1.width = PHO.imageWidth;
-  PAN.image1.height = PHO.imageHeight;
-  PAN.image1.bitsPerPixel = PHO.imageDepth * 3;
-  PAN.image1.bytesPerLine = PHO.imageWidth * PAN.image1.bitsPerPixel/8;
-  PAN.image1.dataSize = PHO.imageHeight * PAN.image1.bytesPerLine;
-  PAN.image1.dataformat = _RGB;
-  PAN.image1.format = _rectilinear;
-  PAN.image1.data = (unsigned char **)&buf;
+  gData->pan.image1.width         = gData->pho.imageWidth;
+  gData->pan.image1.height        = gData->pho.imageHeight;
+  gData->pan.image1.bitsPerPixel  = gData->pho.imageDepth * gData->pho.imagePlanes;
+  gData->pan.image1.bytesPerLine  = gData->pho.imageWidth * gData->pan.image1.bitsPerPixel/8;
+  gData->pan.image1.dataSize      = gData->pho.imageHeight * gData->pan.image1.bytesPerLine;
+  gData->pan.image1.dataformat    = _RGB;
+  gData->pan.image1.format        = _rectilinear;
+  gData->pan.image1.data          = (unsigned char **)&buf;
 
   // setup tr
-  memset(&PAN.tr, 0, sizeof(TrformStr));
-  PAN.tr.tool = tool;
-  PAN.tr.success = 1;
-  PAN.tr.data = &PAN.prefs;
-  PAN.tr.src = &PAN.image1;
-  PAN.tr.dest = &PAN.image2;
-
-  if (!PAN.initPrefs)
+  memset(&gData->pan.tr, 0, sizeof(TrformStr));
+  gData->pan.tr.tool    = tool;
+  gData->pan.tr.success = 1;
+  gData->pan.tr.data    = &gData->pan.prefs;
+  gData->pan.tr.src     = &gData->pan.image1;
+  gData->pan.tr.dest    = &gData->pan.image2;
+
+  if (!gData->pan.initPrefs)
   {
     // 1st call to filter_main
-    PAN.tr.mode = _setprefs;
-    DLL.SetWindowOwner((HWND)platform->hwnd);
-    #ifndef pano13DYNAMICLINK
-      DLLInit(GetDLLInstance(), DLL_PROCESS_ATTACH, NULL);
-    #endif
-    DLL.filter_main(&PAN.tr, &PAN.sprefs);
-    if (!PAN.tr.success)
+    gData->pan.tr.mode = _setprefs;
+    gData->dll.filter_main(&gData->pan.tr, &gData->pan.sprefs);
+    if (!gData->pan.tr.success)
     {
       *gResult = userCanceledErr;
       return 1;
     }
-    PAN.initPrefs = true;
+    gData->pan.initPrefs = true;
   }
 
   // update tr info
-  PAN.tr.gamma        = PAN.sprefs.gamma;
-  PAN.tr.interpolator = PAN.sprefs.interpolator;
-  PAN.tr.fastStep     = PAN.sprefs.fastStep;
+  gData->pan.tr.gamma        = gData->pan.sprefs.gamma;
+  gData->pan.tr.interpolator = gData->pan.sprefs.interpolator;
+  gData->pan.tr.fastStep     = gData->pan.sprefs.fastStep;
 
   // get image from photoshop
-  buf = memAlloc(memDynamic, PHO.imageSize);
+  buf = memAlloc(memDynamic, gData->pho.imageSize);
   if (buf == NULL) goto err_exit;
   if (cpyPsToBuf(buf)) goto err_exit;
 
-  // convert add alpha channel to image1
-  if (tool == _adjust)
-  {
-    PAN.image2 = PAN.image1;
+  // convert add alpha channel to image1 if it does not have one already
+  if (tool == _adjust && gData->pho.imagePlanes == 3)
+  {
+    gData->pan.image2 = gData->pan.image1;
 
     // add alpha channel to image1
-    PAN.image1.bitsPerPixel = PHO.imageDepth * 4;
-    PAN.image1.bytesPerLine = PHO.imageWidth * PAN.image1.bitsPerPixel/8;
-    PAN.image1.dataSize = PHO.imageHeight * PAN.image1.bytesPerLine;
+    gData->pan.image1.bitsPerPixel = gData->pho.imageDepth * 4;
+    gData->pan.image1.bytesPerLine = gData->pho.imageWidth * gData->pan.image1.bitsPerPixel/8;
+    gData->pan.image1.dataSize = gData->pho.imageHeight * gData->pan.image1.bytesPerLine;
 
     // allocate buffer
-    alpha = memAlloc(memDynamic, PAN.image1.dataSize);
+    alpha = memAlloc(memDynamic, gData->pan.image1.dataSize);
     if (alpha == NULL)
     {
       goto err_exit;
     }
-    PAN.image1.data = (unsigned char **)&alpha;
-
-    DLL.CopyImageData(&PAN.image1, &PAN.image2);
-    memFree(memDynamic, *PAN.image2.data);
+    gData->pan.image1.data = (unsigned char **)&alpha;
+
+    gData->dll.CopyImageData(&gData->pan.image1, &gData->pan.image2);
+    memFree(memDynamic, *gData->pan.image2.data);
   }
 
   // 2nd call to filter_main
   // for compability with 2.6b1, don't use _destSupplied
-  PAN.tr.mode = _useprefs | _show_progress;
-  DLL.SetWindowOwner((HWND)platform->hwnd);
-  DLL.filter_main(&PAN.tr, &PAN.sprefs);
+  gData->pan.tr.mode = _useprefs | _show_progress;
+  gData->dll.filter_main(&gData->pan.tr, &gData->pan.sprefs);
 
   // don't call myfree... PT cleans up after itself
-  if (!PAN.tr.success)
+  if (!gData->pan.tr.success)
   {
     return 1;
   }
 
   // restore image1 for return to PS
-  PAN.image1.bitsPerPixel = PHO.imageDepth * 3;
-  PAN.image1.bytesPerLine = PHO.imageWidth * PAN.image1.bitsPerPixel/8;
-  PAN.image1.dataSize = PHO.imageHeight * PAN.image1.bytesPerLine;
+  gData->pan.image1.bitsPerPixel = gData->pho.imageDepth * gData->pho.imagePlanes;
+  gData->pan.image1.bytesPerLine = gData->pho.imageWidth * gData->pan.image1.bitsPerPixel/8;
+  gData->pan.image1.dataSize = gData->pho.imageHeight * gData->pan.image1.bytesPerLine;
 
   // determine images for file and display
   Image *imageFile, *imageDisplay;
   if (tool == _adjust)
   {
     // resize for display, removing alpha channel
-    DLL.CopyImageData(&PAN.image1, &PAN.image2);
-    if (PAN.sprefs.noAlpha)
-    {
-      DLL.FourToThreeBPP(&PAN.image2);
-    }
-    imageFile = &PAN.image2;
-    imageDisplay = &PAN.image1;
+    gData->dll.CopyImageData(&gData->pan.image1, &gData->pan.image2);
+    if (gData->pan.sprefs.noAlpha)
+    {
+      gData->dll.FourToThreeBPP(&gData->pan.image2);
+    }
+    imageFile = &gData->pan.image2;
+    imageDisplay = &gData->pan.image1;
   }
   else
   {
-    imageFile = &PAN.image2;
-
-    if ( PAN.tr.src->width != PAN.tr.dest->width
-      || PAN.tr.src->height != PAN.tr.dest->height )
+    imageFile = &gData->pan.image2;
+
+    if ( gData->pan.tr.src->width != gData->pan.tr.dest->width
+      || gData->pan.tr.src->height != gData->pan.tr.dest->height )
     {
         // resize for display
-        DLL.CopyImageData(&PAN.image1, &PAN.image2);
-        imageDisplay = &PAN.image1;
+        gData->dll.CopyImageData(&gData->pan.image1, &gData->pan.image2);
+        imageDisplay = &gData->pan.image1;
     }
     else
     {
-      imageDisplay = &PAN.image2;
+      imageDisplay = &gData->pan.image2;
     }
   }
 
   // if change in file size, we've got extra processing to do
-  if ( PAN.tr.src->width == PAN.tr.dest->width
-    && PAN.tr.src->height == PAN.tr.dest->height )
+  if ( gData->pan.tr.src->width == gData->pan.tr.dest->width
+    && gData->pan.tr.src->height == gData->pan.tr.dest->height )
   {
       if (cpyBufToPs(*imageDisplay->data))
       {
@@ -348,22 +339,30 @@
   }
   else
   {
-    if (PAN.sprefs.displayPart)
+    if (gData->pan.sprefs.displayPart)
     {
       if (cpyBufToPs(*imageDisplay->data))
       {
         goto err_exit;
       }
     }
-    if (PAN.sprefs.saveFile)
+    if (gData->pan.sprefs.saveFile)
     {
       int BuffSize = 0;
       fullPath path;
       BuffSize = sizeof(path)/sizeof(char);
       fileName(path.name, BuffSize);
-      DLL.writePSD(imageFile, &path);
-
-      if (PAN.sprefs.launchApp)
+
+      if ( tool==_adjust && gData->pan.sprefs.noAlpha )
+      {
+        gData->dll.writePSD(imageFile, &path);
+      }
+      else
+      {
+        gData->dll.writePSDwithLayer(imageFile, &path);
+      }
+
+      if (gData->pan.sprefs.launchApp)
       {
         char command[300];
         STARTUPINFO si;
@@ -375,9 +374,9 @@
         // build command-line
         BuffSize = sizeof(command)/sizeof(char);
 #if _MSC_VER >= 1400
-        sprintf_s(command, BuffSize, "%s \"%s\"", PAN.sprefs.lApp.name, path.name);
+        sprintf_s(command, BuffSize, "%s \"%s\"", gData->pan.sprefs.lApp.name, path.name);
 #else
-        sprintf(command, "%s \"%s\"", PAN.sprefs.lApp.name, path.name);
+        sprintf(command, "%s \"%s\"", gData->pan.sprefs.lApp.name, path.name);
 #endif
 
         CreateProcess(
@@ -396,17 +395,17 @@
   }
 
   // believe it or not, image2 still has pano13's allocated memory
-  DLL.myfree((void **)PAN.image2.data);
+  gData->dll.myfree((void **)gData->pan.image2.data);
 
   return 0;
 
 err_exit:
-  if (PAN.image2.data)
-  {
-    DLL.myfree((void **)PAN.image2.data);
-  }
-
-  FreeLibrary(DLL.hpano13);
-  DLL.initDLL = false;
+  if (gData->pan.image2.data)
+  {
+    gData->dll.myfree((void **)gData->pan.image2.data);
+  }
+
+  FreeLibrary(gData->dll.hpano13);
+  gData->dll.initDLL = false;
   return 1;
 }