Diff of /panTools.cpp [68cdf8] .. [ed05d7]  Maximize  Restore

  Switch to side-by-side view

--- a/panTools.cpp
+++ b/panTools.cpp
@@ -1,7 +1,3 @@
-
-// PTLens, Copyright (C) 2004, Thomas Niemann
-// email: thomasn@epaperpress.com, subject=ptlens
-// 
 // This program is free software; you can redistribute 
 // it and/or modify it under the terms of the GNU
 // General Public License as published by the Free 
@@ -19,258 +15,396 @@
 // write to the Free Software Foundation, Inc., 675 
 // Mass Ave, Cambridge, MA 02139, USA.
 
+/**
+* @file panTools.cpp
+* This file is included in all four plug-ins (PTAdjust, PTCorrect, PTPerspect, and PTRemap)
+* It contains the main functions to invoke panotools to preform the selected tasks
+* @date January 2007
+*/
+
 #include "main.h"
+
+/**
+* Used to switch between how the plugins use panotools core libreary
+* if not defined then staticly linking to pano13, using source code (for debuffing, ect... )
+* if defined then dynamicly linking to pano13
+**/
+//#define pano13DYNAMICLINK  
 
 typedef void (*dllEntry)();
 extern int tool;
 
-static dllEntry getAddr(char *func) {
-	dllEntry addr = (dllEntry) GetProcAddress(DLL.hPano12, func); 
-    if (addr == NULL) {
-        FreeLibrary(DLL.hPano12);
-        display("Pano12.dll is invalid (%s).", func);
-    }
-	return addr;
+#if defined pano13DYNAMICLINK
+
+/**
+* Retrieve the address to a function in pano13.dll
+* @param[in] func the name of the function to retrieve the address
+* @return the funtion if it exist
+**/
+static dllEntry getAddr(char *func)
+{
+  dllEntry addr = (dllEntry) GetProcAddress(DLL.hpano13, func); 
+  if (addr == NULL)
+  {
+    FreeLibrary(DLL.hpano13);
+    display("Pano13.dll is invalid (%s).", func);
+  }
+  return addr;
 }
 
+/**
+* macro used to locate the funtions in pano13.dll libreary
+* @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
-    
-
-static int loadLibrary() {
-	// Get a handle to the DLL module.
-    DLL.hPano12 = LoadLibrary("pano12.dll");
-    if (DLL.hPano12 == NULL)
-        return display("File pano12.dll is not correctly installed.");
-
-	// initialize all function pointers
-#if 0  // unused functions
-	SETFUNC(ConvFileName);
-	SETFUNC(PrintError);
-	SETFUNC(SetAdjustDefaults);
-	SETFUNC(addLayerToFile);
-	SETFUNC(mymalloc);
-	SETFUNC(readPrefs);
-	SETFUNC(writePSDwithLayer);
-	SETFUNC(writeTIFF);
+  DLL. ## x = (x ## _proc)getAddr(#x);\
+  if (DLL. ## x == NULL) goto err_exit
+
 #endif
 
-	SETFUNC(FourToThreeBPP);	
-	SETFUNC(SetWindowOwner);
-	SETFUNC(CopyImageData);
-	SETFUNC(SetImageDefaults);
-	SETFUNC(filter_main);
-	SETFUNC(writePSD);
-	SETFUNC(myfree);
-
-	return 0;
+//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 size_Prefs *spref);
+extern "C" int writePSD(Image *im, fullPath* fname);
+extern "C" void myfree(void **hdl);
+*/
+
+/**
+* Load pano13.dll and initilize funtions needed.
+* @return 0 if successful, 1 on error
+**/
+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;
 
 err_exit:
-	FreeLibrary(DLL.hPano12);	
-	return 1;
+  FreeLibrary(DLL.hpano13);	
+  return 1;
+  }
+#else
+  {
+    //hDllInstance = GetDLLInstance();
+    DLLInit(GetDLLInstance(), DLL_PROCESS_ATTACH, NULL);
+    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;
+  }
+#endif
 }
 
-static void fileName(char *buf) {
-	int pre;
-
-	switch(tool) {
-	case _perspective:	pre = 0; break;
-	case _correct:      pre = 1; break;
-	case _remap:        pre = 2; break;
-	case _adjust:       pre = 3; break;
-	}
-    
-	// find filename
-	int i = 0;
-	while(1) {
-		sprintf(buf, "%s%d.%d.psd", PAN.sprefs.sFile.name, pre, i++);
-
-		HANDLE hFile = CreateFile(
-			buf,				      // file name
-			GENERIC_READ,             // access mode
-			FILE_SHARE_READ,          // share mode
-			NULL,                     // security
-			OPEN_EXISTING,            // how to create
-			0,                        // flags & attributes
-			NULL);                    // template file
-
-		if (hFile == INVALID_HANDLE_VALUE) return;
-		CloseHandle(hFile);
-	}
+/**
+* Retrieve the name of the next tempory file
+* @param[out] buf the name of the file to use
+* @param[in] bufSize the size of the buffer to contain the file name.
+* @return none
+**/
+static void fileName(char *buf, int bufSize)
+{
+  int pre;
+
+  switch(tool)
+  {
+    case _perspective:
+      pre = 0; 
+      break;
+
+    case _correct:
+      pre = 1;
+      break;
+
+    case _remap:
+      pre = 2;
+      break;
+
+    case _adjust:
+      pre = 3;
+      break;
+  }
+
+  // find filename
+  int i = 0;
+  while(1)
+  {
+#if _MSC_VER >= 1400
+    sprintf_s(buf, bufSize, "%s%d.%d.psd", PAN.sprefs.sFile.name, pre, i++);
+#else
+    sprintf(buf, "%s%d.%d.psd", PAN.sprefs.sFile.name, pre, i++);
+#endif
+    HANDLE hFile = CreateFile(
+        buf,				              // file name
+        GENERIC_READ,             // access mode
+        FILE_SHARE_READ,          // share mode
+        NULL,                     // security
+        OPEN_EXISTING,            // how to create
+        0,                        // flags & attributes
+        NULL);                    // template file
+
+    if (hFile == INVALID_HANDLE_VALUE)
+    {
+      return;
+    }
+
+    CloseHandle(hFile);
+  }
 }
 
-int panTools() {
-	void *buf, *alpha;
-	PlatformData* platform = NULL;
-
-	if (!DLL.initDLL) {
-		if (loadLibrary()) return 1;
-		DLL.initDLL = true;
-	}
-	
-	platform = (PlatformData*)(gFr->platformData);
-
-	// initialize PHOtoshop settings
-	PHO.imageWidth = gFr->imageSize.h;
-	PHO.imageHeight = gFr->imageSize.v;	
-	PHO.imageDepth = gFr->depth;
-	
-	// 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;
-
-	// initialize images
-	DLL.SetImageDefaults(&PAN.image1);
-	DLL.SetImageDefaults(&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;
-
-	// 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) {
-		// 1st call to filter_main
-		PAN.tr.mode = _setprefs;
-		DLL.SetWindowOwner((HWND)platform->hwnd);
-		DLL.filter_main(&PAN.tr, &PAN.sprefs);
-		if (!PAN.tr.success) {
-			*gResult = userCanceledErr;
-			return 1;
-		}
-		PAN.initPrefs = true;
-	}
-
-	// update tr info
-	PAN.tr.gamma = PAN.sprefs.gamma;
-	PAN.tr.interpolator = PAN.sprefs.interpolator;
-
-	// get image from photoshop
-	buf = memAlloc(memDynamic, 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;
-
-		// 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;
-
-		// allocate buffer
-		alpha = memAlloc(memDynamic, PAN.image1.dataSize);
-		if (alpha == NULL) goto err_exit;
-		PAN.image1.data = (unsigned char **)α
-
-		DLL.CopyImageData(&PAN.image1, &PAN.image2);
-		memFree(memDynamic, *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);
+/**
+* The main plugin loop.
+* @return 0 on success, 1 on error
+**/
+int panTools()
+{
+  void         *buf       = NULL;
+  void         *alpha     = NULL;
+  PlatformData *platform  = NULL;
+
+  if (!DLL.initDLL) 
+  {
+    if (loadLibrary())
+    {
+      return 1;
+    }
+    DLL.initDLL = true;
+  }
+
+  platform = (PlatformData*)(gFr->platformData);
+
+  // initialize PHOtoshop settings
+  PHO.imageWidth = gFr->imageSize.h;
+  PHO.imageHeight = gFr->imageSize.v;	
+  PHO.imageDepth = gFr->depth;
+
+  // 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;
+
+  // initialize images
+  DLL.SetImageDefaults(&PAN.image1);
+  DLL.SetImageDefaults(&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;
+
+  // 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)
+  {
+    // 1st call to filter_main
+    PAN.tr.mode = _setprefs;
+    DLL.SetWindowOwner((HWND)platform->hwnd);
     DLL.filter_main(&PAN.tr, &PAN.sprefs);
-
-	// don't call myfree... PT cleans up after itself
-	if (!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;
-	
-	// 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;
+    if (!PAN.tr.success)
+    {
+      *gResult = userCanceledErr;
+      return 1;
+    }
+    PAN.initPrefs = true;
+  }
+
+  // update tr info
+  PAN.tr.gamma = PAN.sprefs.gamma;
+  PAN.tr.interpolator = PAN.sprefs.interpolator;
+
+  // get image from photoshop
+  buf = memAlloc(memDynamic, 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;
+
+    // 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;
+
+    // allocate buffer
+    alpha = memAlloc(memDynamic, PAN.image1.dataSize);
+    if (alpha == NULL)
+    {
+      goto err_exit;
+    }
+    PAN.image1.data = (unsigned char **)α
+
+    DLL.CopyImageData(&PAN.image1, &PAN.image2);
+    memFree(memDynamic, *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);
+
+  // don't call myfree... PT cleans up after itself
+  if (!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;
+
+  // 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;
+  }
+  else
+  {
+    imageFile = &PAN.image2;
+
+    if ( PAN.tr.src->width != PAN.tr.dest->width
+      || PAN.tr.src->height != PAN.tr.dest->height )
+    {
+        // resize for display
+        DLL.CopyImageData(&PAN.image1, &PAN.image2);
         imageDisplay = &PAN.image1;
-
-	} else {
-		imageFile = &PAN.image2;
-		
-		if (PAN.tr.src->width != PAN.tr.dest->width ||
-			PAN.tr.src->height != PAN.tr.dest->height) {
-
-			// resize for display
-			DLL.CopyImageData(&PAN.image1, &PAN.image2);
-			imageDisplay = &PAN.image1;
-		} else {
-			imageDisplay = &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 (cpyBufToPs(*imageDisplay->data)) goto err_exit;
-	} else {
-		if (PAN.sprefs.displayPart) {
-			if (cpyBufToPs(*imageDisplay->data)) goto err_exit;
-		}
-		if (PAN.sprefs.saveFile) {
-			fullPath path;
-			fileName(path.name);
-			DLL.writePSD(imageFile, &path);
-
-			if (PAN.sprefs.launchApp) {
-				char command[300];
-				STARTUPINFO si;
-				PROCESS_INFORMATION pi;
-
-				memset(&si, 0, sizeof(STARTUPINFO));
-				si.cb = sizeof(STARTUPINFO);
-
-				// build command-line
-				sprintf(command, "%s \"%s\"", PAN.sprefs.lApp.name, path.name);
-
-				CreateProcess(
-					NULL,					// name of executable module
-					command,                // command line string
-					NULL,					// process security attributes
-					NULL,					// thread security attributes
-					FALSE,					// handle inheritance
-					0,						// creation flags
-					NULL,					// new environment block
-					NULL,					// current directory name
-					&si,					// startup information
-					&pi);					// process information
-			}
-		}	
-	}
-
-	// believe it or not, image2 still has pano12's allocated memory
-	DLL.myfree((void **)PAN.image2.data);
-
-	return 0;
+    }
+    else
+    {
+      imageDisplay = &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 (cpyBufToPs(*imageDisplay->data))
+      {
+        goto err_exit;
+      }
+  }
+  else
+  {
+    if (PAN.sprefs.displayPart)
+    {
+      if (cpyBufToPs(*imageDisplay->data))
+      {
+        goto err_exit;
+      }
+    }
+    if (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)
+      {
+        char command[300];
+        STARTUPINFO si;
+        PROCESS_INFORMATION pi;
+
+        memset(&si, 0, sizeof(STARTUPINFO));
+        si.cb = sizeof(STARTUPINFO);
+
+        // build command-line
+        BuffSize = sizeof(command)/sizeof(char);
+#if _MSC_VER >= 1400
+        sprintf_s(command, BuffSize, "%s \"%s\"", PAN.sprefs.lApp.name, path.name);
+#else
+        sprintf(command, "%s \"%s\"", PAN.sprefs.lApp.name, path.name);
+#endif
+
+        CreateProcess(
+          NULL,           // name of executable module
+          command,        // command line string
+          NULL,           // process security attributes
+          NULL,           // thread security attributes
+          FALSE,          // handle inheritance
+          0,              // creation flags
+          NULL,           // new environment block
+          NULL,           // current directory name
+          &si,            // startup information
+          &pi);           // process information
+      }
+    }
+  }
+
+  // believe it or not, image2 still has pano13's allocated memory
+  DLL.myfree((void **)PAN.image2.data);
+
+  return 0;
 
 err_exit:
-	if (PAN.image2.data)
-		DLL.myfree((void **)PAN.image2.data);
-	
-	FreeLibrary(DLL.hPano12);
-	DLL.initDLL = false;
-    return 1;
+  if (PAN.image2.data)
+  {
+    DLL.myfree((void **)PAN.image2.data);
+  }
+
+  FreeLibrary(DLL.hpano13);
+  DLL.initDLL = false;
+  return 1;
 }

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks