Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[63eab1]: panTools.cpp Maximize Restore History

Download this file

panTools.cpp    412 lines (358 with data), 11.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// 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
// Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// Public License along with this program; if not,
// 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;
#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(gData->dll.hpano13, func);
if (addr == NULL)
{
FreeLibrary(gData->gData->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)\
gData->dll. ## x = (x ## _proc)getAddr(#x);\
if (gData->dll. ## x == NULL) goto err_exit
#else
extern HINSTANCE hDllInstance;
#endif
/**
* 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.
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(gData->dll.hpano13);
return 1;
#else
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
}
/**
* 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", gData->pan.sprefs.sFile.name, pre, i++);
#else
sprintf(buf, "%s%d.%d.psd", gData->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);
}
}
/**
* The main plugin loop.
* @return 0 on success, 1 on error
**/
int panTools()
{
void *buf = NULL;
void *alpha = NULL;
PlatformData *platform = NULL;
#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;
}
gData->dll.initDLL = true;
}
platform = (PlatformData*)(gFr->platformData);
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 (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
gData->dll.SetImageDefaults(&gData->pan.image1);
gData->dll.SetImageDefaults(&gData->pan.image2);
// assume no alpha channel needed
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(&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
gData->pan.tr.mode = _setprefs;
gData->dll.filter_main(&gData->pan.tr, &gData->pan.sprefs);
if (!gData->pan.tr.success)
{
*gResult = userCanceledErr;
return 1;
}
gData->pan.initPrefs = true;
}
// update tr info
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, gData->pho.imageSize);
if (buf == NULL) goto err_exit;
if (cpyPsToBuf(buf)) goto err_exit;
// 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
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, gData->pan.image1.dataSize);
if (alpha == NULL)
{
goto err_exit;
}
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
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 (!gData->pan.tr.success)
{
return 1;
}
// restore image1 for return to PS
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
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 = &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
gData->dll.CopyImageData(&gData->pan.image1, &gData->pan.image2);
imageDisplay = &gData->pan.image1;
}
else
{
imageDisplay = &gData->pan.image2;
}
}
// if change in file size, we've got extra processing to do
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))
{
goto err_exit;
}
}
else
{
if (gData->pan.sprefs.displayPart)
{
if (cpyBufToPs(*imageDisplay->data))
{
goto err_exit;
}
}
if (gData->pan.sprefs.saveFile)
{
int BuffSize = 0;
fullPath path;
BuffSize = sizeof(path)/sizeof(char);
fileName(path.name, BuffSize);
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;
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\"", gData->pan.sprefs.lApp.name, path.name);
#else
sprintf(command, "%s \"%s\"", gData->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
gData->dll.myfree((void **)gData->pan.image2.data);
return 0;
err_exit:
if (gData->pan.image2.data)
{
gData->dll.myfree((void **)gData->pan.image2.data);
}
FreeLibrary(gData->dll.hpano13);
gData->dll.initDLL = false;
return 1;
}