| 1 |
/* |
| 2 |
* imgUtil.tcl |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "imgInt.h" |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#ifndef _LANG |
| 9 |
#ifdef MAC_TCL |
| 10 |
# include "dlfcn.h" |
| 11 |
#else |
| 12 |
# ifdef HAVE_DLFCN_H |
| 13 |
# include <dlfcn.h> |
| 14 |
# else |
| 15 |
# include "compat/dlfcn.h" |
| 16 |
# endif |
| 17 |
#endif |
| 18 |
/* |
| 19 |
* In some systems, like SunOS 4.1.3, the RTLD_NOW flag isn't defined |
| 20 |
* and this argument to dlopen must always be 1. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef RTLD_NOW |
| 24 |
# define RTLD_NOW 1 |
| 25 |
#endif |
| 26 |
#endif /* _LANG */ |
| 27 |
|
| 28 |
|
| 29 |
/* |
| 30 |
*---------------------------------------------------------------------- |
| 31 |
* |
| 32 |
* ImgPhotoPutBlock -- |
| 33 |
* |
| 34 |
* This procedure is called to put image data into a photo image. |
| 35 |
* The difference with Tk_PhotoPutBlock is that it handles the |
| 36 |
* transparency information as well. |
| 37 |
* |
| 38 |
* Results: |
| 39 |
* None. |
| 40 |
* |
| 41 |
* Side effects: |
| 42 |
* The image data is stored. The image may be expanded. |
| 43 |
* The Tk image code is informed that the image has changed. |
| 44 |
* |
| 45 |
*---------------------------------------------------------------------- |
| 46 |
*/ |
| 47 |
|
| 48 |
int |
| 49 |
ImgPhotoPutBlock(handle, blockPtr, x, y, width, height) |
| 50 |
Tk_PhotoHandle handle; /* Opaque handle for the photo image |
| 51 |
* to be updated. */ |
| 52 |
Tk_PhotoImageBlock *blockPtr; |
| 53 |
/* Pointer to a structure describing the |
| 54 |
* pixel data to be copied into the image. */ |
| 55 |
int x, y; /* Coordinates of the top-left pixel to |
| 56 |
* be updated in the image. */ |
| 57 |
int width, height; /* Dimensions of the area of the image |
| 58 |
* to be updated. */ |
| 59 |
{ |
| 60 |
int alphaOffset; |
| 61 |
|
| 62 |
alphaOffset = blockPtr->offset[3]; |
| 63 |
if ((alphaOffset< 0) || (alphaOffset>= blockPtr->pixelSize)) { |
| 64 |
alphaOffset = blockPtr->offset[0]; |
| 65 |
if (alphaOffset < blockPtr->offset[1]) { |
| 66 |
alphaOffset = blockPtr->offset[1]; |
| 67 |
} |
| 68 |
if (alphaOffset < blockPtr->offset[2]) { |
| 69 |
alphaOffset = blockPtr->offset[2]; |
| 70 |
} |
| 71 |
if (++alphaOffset >= blockPtr->pixelSize) { |
| 72 |
alphaOffset = blockPtr->offset[0]; |
| 73 |
} |
| 74 |
} else { |
| 75 |
if ((alphaOffset == blockPtr->offset[1]) || |
| 76 |
(alphaOffset == blockPtr->offset[2])) { |
| 77 |
alphaOffset = blockPtr->offset[0]; |
| 78 |
} |
| 79 |
} |
| 80 |
if (alphaOffset != blockPtr->offset[0]) { |
| 81 |
int X, Y, end; |
| 82 |
unsigned char *pixelPtr, *imagePtr, *rowPtr; |
| 83 |
rowPtr = imagePtr = blockPtr->pixelPtr; |
| 84 |
for (Y = 0; Y < height; Y++) { |
| 85 |
X = 0; |
| 86 |
pixelPtr = rowPtr + alphaOffset; |
| 87 |
while(X < width) { |
| 88 |
/* search for first non-transparent pixel */ |
| 89 |
while ((X < width) && !(*pixelPtr)) { |
| 90 |
X++; pixelPtr += blockPtr->pixelSize; |
| 91 |
} |
| 92 |
end = X; |
| 93 |
/* search for first transparent pixel */ |
| 94 |
while ((end < width) && *pixelPtr) { |
| 95 |
end++; pixelPtr += blockPtr->pixelSize; |
| 96 |
} |
| 97 |
if (end > X) { |
| 98 |
blockPtr->pixelPtr = rowPtr + blockPtr->pixelSize * X; |
| 99 |
Tk_PhotoPutBlock(handle, blockPtr, x+X, y+Y, end-X, 1, TK_PHOTO_COMPOSITE_SET); |
| 100 |
} |
| 101 |
X = end; |
| 102 |
} |
| 103 |
rowPtr += blockPtr->pitch; |
| 104 |
} |
| 105 |
blockPtr->pixelPtr = imagePtr; |
| 106 |
} else { |
| 107 |
Tk_PhotoPutBlock(handle,blockPtr,x,y,width,height, TK_PHOTO_COMPOSITE_SET); |
| 108 |
} |
| 109 |
return TCL_OK; |
| 110 |
} |
| 111 |
|
| 112 |
#ifndef _LANG |
| 113 |
|
| 114 |
/* |
| 115 |
*---------------------------------------------------------------------- |
| 116 |
* |
| 117 |
* ImgLoadLib -- |
| 118 |
* |
| 119 |
* This procedure is called to load a shared library into memory. |
| 120 |
* If the extension is ".so" (e.g. Solaris, Linux) or ".sl" (HP-UX) |
| 121 |
* it is possible that the extension is appended or replaced with |
| 122 |
* a major version number. If the file cannot be found, the version |
| 123 |
* numbers will be stripped off one by one. e.g. |
| 124 |
* |
| 125 |
* HP-UX: libtiff.3.4 Linux,Solaris: libtiff.so.3.4 |
| 126 |
* libtiff.3 libtiff.so.3 |
| 127 |
* libtiff.sl libtiff.so |
| 128 |
* |
| 129 |
* Results: |
| 130 |
* TCL_OK if function succeeds. Otherwise TCL_ERROR while the |
| 131 |
* interpreter will contain an error-message. The last parameter |
| 132 |
* "num" contains the minimum number of symbols that is required |
| 133 |
* by the application to succeed. Only the first <num> symbols |
| 134 |
* will produce an error if they cannot be found. |
| 135 |
* |
| 136 |
* Side effects: |
| 137 |
* At least <num> Library functions become available by the |
| 138 |
* application. |
| 139 |
* |
| 140 |
*---------------------------------------------------------------------- |
| 141 |
*/ |
| 142 |
|
| 143 |
typedef struct Functions { |
| 144 |
VOID *handle; |
| 145 |
int (* first) _ANSI_ARGS_((void)); |
| 146 |
int (* next) _ANSI_ARGS_((void)); |
| 147 |
} Functions; |
| 148 |
|
| 149 |
#define IMG_FAILED ((VOID *) -114) |
| 150 |
|
| 151 |
int |
| 152 |
ImgLoadLib(interp, libName, handlePtr, symbols, num) |
| 153 |
Tcl_Interp *interp; |
| 154 |
CONST char *libName; |
| 155 |
VOID **handlePtr; |
| 156 |
char **symbols; |
| 157 |
int num; |
| 158 |
{ |
| 159 |
VOID *handle = (VOID *) NULL; |
| 160 |
Functions *lib = (Functions *) handlePtr; |
| 161 |
char **p = (char **) &(lib->first); |
| 162 |
char **q = symbols; |
| 163 |
char buf[256]; |
| 164 |
char *r; |
| 165 |
int length; |
| 166 |
|
| 167 |
if (lib->handle != NULL) { |
| 168 |
return (lib->handle != IMG_FAILED) ? TCL_OK : TCL_ERROR; |
| 169 |
} |
| 170 |
|
| 171 |
length = strlen(libName); |
| 172 |
strcpy(buf,libName); |
| 173 |
handle = dlopen(buf, RTLD_NOW); |
| 174 |
|
| 175 |
while (handle == NULL) { |
| 176 |
if ((r = strrchr(buf,'.')) != NULL) { |
| 177 |
if ((r[1] < '0') || (r[1] > '9')) { |
| 178 |
if (interp) { |
| 179 |
Tcl_AppendResult(interp,"cannot open ",libName, |
| 180 |
": ", dlerror(), (char *) NULL); |
| 181 |
} else { |
| 182 |
printf("cannot open %s: %s\n",libName,dlerror()); |
| 183 |
} |
| 184 |
lib->handle = IMG_FAILED; |
| 185 |
return TCL_ERROR; |
| 186 |
} |
| 187 |
length = r - buf; |
| 188 |
*r = 0; |
| 189 |
} |
| 190 |
if (strchr(buf,'.') == NULL) { |
| 191 |
strcpy(buf+length,".sl"); |
| 192 |
length += 3; |
| 193 |
} |
| 194 |
dlerror(); |
| 195 |
handle = dlopen(buf, RTLD_NOW); |
| 196 |
} |
| 197 |
|
| 198 |
buf[0] = '_'; |
| 199 |
while (*q) { |
| 200 |
*p = (char *) dlsym(handle,*q); |
| 201 |
if (*p == (char *)NULL) { |
| 202 |
strcpy(buf+1,*q); |
| 203 |
*p = (char *) dlsym(handle,buf); |
| 204 |
if ((num > 0) && (*p == (char *)NULL)) { |
| 205 |
if (interp) { |
| 206 |
Tcl_AppendResult(interp,"cannot open ",libName, |
| 207 |
": symbol \"",*q,"\" not found", (char *) NULL); |
| 208 |
} else { |
| 209 |
printf("cannot open %s: symbol \"%s\" not found", |
| 210 |
libName, *q); |
| 211 |
} |
| 212 |
dlclose(handle); |
| 213 |
lib->handle = IMG_FAILED; |
| 214 |
return TCL_ERROR; |
| 215 |
} |
| 216 |
} |
| 217 |
q++; num--; |
| 218 |
p += (Tk_Offset(Functions, next) - Tk_Offset(Functions, first)) / |
| 219 |
sizeof(char *); |
| 220 |
} |
| 221 |
lib->handle = handle; |
| 222 |
|
| 223 |
return TCL_OK; |
| 224 |
} |
| 225 |
|
| 226 |
/* |
| 227 |
*---------------------------------------------------------------------- |
| 228 |
* |
| 229 |
* ImgLoadFailed -- |
| 230 |
* |
| 231 |
* Mark the loaded library as invalid. Remove it from memory |
| 232 |
* if possible. It will no longer be used in the future. |
| 233 |
* |
| 234 |
* Results: |
| 235 |
* None. |
| 236 |
* |
| 237 |
* Side effects: |
| 238 |
* Next time the same handle is used by ImgLoadLib, it will |
| 239 |
* fail immediately, without trying to load it. |
| 240 |
* |
| 241 |
*---------------------------------------------------------------------- |
| 242 |
*/ |
| 243 |
|
| 244 |
void |
| 245 |
ImgLoadFailed(handlePtr) |
| 246 |
VOID **handlePtr; |
| 247 |
{ |
| 248 |
if ((*handlePtr != NULL) && (*handlePtr != IMG_FAILED)) { |
| 249 |
/* Oops, still loaded. First remove it from menory */ |
| 250 |
dlclose(*handlePtr); |
| 251 |
} |
| 252 |
*handlePtr = IMG_FAILED; |
| 253 |
} |
| 254 |
|
| 255 |
#endif |
| 256 |
|