| 1 |
/* |
| 2 |
* imgObj.tcl |
| 3 |
*/ |
| 4 |
#include "tk.h" |
| 5 |
#include "tkVMacro.h" |
| 6 |
|
| 7 |
#include "imgInt.h" |
| 8 |
#include <string.h> |
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
/* |
| 12 |
* The variable "initialized" contains flags indicating which |
| 13 |
* version of Tcl or Perl we are running: |
| 14 |
* |
| 15 |
* IMG_PERL perl |
| 16 |
* IMG_TCL Tcl |
| 17 |
* IMG_OBJS using (Tcl_Obj *) in stead of (char *) |
| 18 |
* IMG_UTF Tcl supports UTF-8 |
| 19 |
* |
| 20 |
* These flags will be determined at runtime (except the IMG_PERL |
| 21 |
* flag, for now), so we can use the same dynamic library for all |
| 22 |
* Tcl/Tk versions (and for Perl/Tk in the future). |
| 23 |
*/ |
| 24 |
|
| 25 |
static int initialized = 0; |
| 26 |
static Tcl_ObjType* byteArrayType = 0; |
| 27 |
|
| 28 |
int |
| 29 |
ImgObjInit(interp) |
| 30 |
Tcl_Interp *interp; |
| 31 |
{ |
| 32 |
Tcl_CmdInfo cmdInfo; |
| 33 |
#ifdef _LANG |
| 34 |
return (initialized = IMG_PERL|IMG_OBJS); |
| 35 |
#else |
| 36 |
char *version; |
| 37 |
initialized = IMG_TCL; |
| 38 |
if (!Tcl_GetCommandInfo(interp,"image", &cmdInfo)) { |
| 39 |
Tcl_AppendResult(interp, "cannot find the \"image\" command", |
| 40 |
(char *) NULL); |
| 41 |
initialized = 0; |
| 42 |
return TCL_ERROR; |
| 43 |
} |
| 44 |
if (cmdInfo.isNativeObjectProc == 1) { |
| 45 |
initialized |= IMG_OBJS; /* we use objects */ |
| 46 |
} |
| 47 |
version = Tcl_PkgRequire(interp, "Tcl", "8.0", 0); |
| 48 |
if (version && (version[2] > '0')) { |
| 49 |
initialized |= IMG_UTF; |
| 50 |
} |
| 51 |
return initialized; |
| 52 |
#endif |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
* The following structure is the internal rep for a ByteArray object. |
| 57 |
* Keeps track of how much memory has been used and how much has been |
| 58 |
* allocated for the byte array to enable growing and shrinking of the |
| 59 |
* ByteArray object with fewer mallocs. The ByteArray is also guaranteed |
| 60 |
* to have a terminating 0 byte at the end of the used length. |
| 61 |
*/ |
| 62 |
|
| 63 |
typedef struct ByteArray { |
| 64 |
int used; /* The number of bytes used in the byte |
| 65 |
* array. */ |
| 66 |
int allocated; /* The amount of space actually allocated |
| 67 |
* minus 1 byte. */ |
| 68 |
unsigned char bytes[4]; /* The array of bytes. The actual size of |
| 69 |
* this field depends on the 'allocated' field |
| 70 |
* above. */ |
| 71 |
} ByteArray; |
| 72 |
|
| 73 |
/* |
| 74 |
*---------------------------------------------------------------------- |
| 75 |
* |
| 76 |
* ImgGetStringFromObj -- |
| 77 |
* |
| 78 |
* Returns the string representation's byte array pointer and length |
| 79 |
* for an object. |
| 80 |
* |
| 81 |
* Results: |
| 82 |
* Returns a pointer to the string representation of objPtr. If |
| 83 |
* lengthPtr isn't NULL, the length of the string representation is |
| 84 |
* stored at *lengthPtr. The byte array referenced by the returned |
| 85 |
* pointer must not be modified by the caller. Furthermore, the |
| 86 |
* caller must copy the bytes if they need to retain them since the |
| 87 |
* object's string rep can change as a result of other operations. |
| 88 |
* REMARK: This function reacts a little bit different than |
| 89 |
* Tcl_GetStringFromObj(): |
| 90 |
* - objPtr is allowed to be NULL. In that case the NULL pointer |
| 91 |
* will be returned, and the length will be reported to be 0; |
| 92 |
* In the Img code there is never a distinction between en empty |
| 93 |
* string and a NULL pointer, while the latter is easier to check |
| 94 |
* for. That's the reason for this difference. |
| 95 |
* |
| 96 |
* Side effects: |
| 97 |
* May call the object's updateStringProc to update the string |
| 98 |
* representation from the internal representation. |
| 99 |
* |
| 100 |
*---------------------------------------------------------------------- |
| 101 |
*/ |
| 102 |
|
| 103 |
char * |
| 104 |
ImgGetStringFromObj(objPtr, lengthPtr) |
| 105 |
register Tcl_Obj *objPtr; /* Object whose string rep byte pointer |
| 106 |
* should be returned, or NULL */ |
| 107 |
register int *lengthPtr; /* If non-NULL, the location where the |
| 108 |
* string rep's byte array length should be |
| 109 |
* stored. If NULL, no length is stored. */ |
| 110 |
{ |
| 111 |
if (!objPtr) { |
| 112 |
if (lengthPtr != NULL) { |
| 113 |
*lengthPtr = 0; |
| 114 |
} |
| 115 |
return (char *) NULL; |
| 116 |
} else |
| 117 |
#ifdef _LANG |
| 118 |
{ |
| 119 |
char *string = Tcl_GetString(objPtr); |
| 120 |
if (lengthPtr != NULL) { |
| 121 |
*lengthPtr = string ? strlen(string) : 0; |
| 122 |
} |
| 123 |
return string; |
| 124 |
} |
| 125 |
#else /* _LANG */ |
| 126 |
if (initialized & IMG_OBJS) { |
| 127 |
return Tcl_GetStringFromObj(objPtr, lengthPtr); |
| 128 |
} else { |
| 129 |
char *string = (char *) objPtr; |
| 130 |
if (lengthPtr != NULL) { |
| 131 |
*lengthPtr = string ? strlen(string) : 0; |
| 132 |
} |
| 133 |
return string; |
| 134 |
} |
| 135 |
#endif /* _LANG */ |
| 136 |
} |
| 137 |
/* |
| 138 |
*---------------------------------------------------------------------- |
| 139 |
* |
| 140 |
* ImgGetByteArrayFromObj -- |
| 141 |
* |
| 142 |
* Returns the binary representation and length |
| 143 |
* for a byte array object. |
| 144 |
* |
| 145 |
* Results: |
| 146 |
* Returns a pointer to the byte representation of objPtr. If |
| 147 |
* lengthPtr isn't NULL, the length of the string representation is |
| 148 |
* stored at *lengthPtr. The byte array referenced by the returned |
| 149 |
* pointer must not be modified by the caller. Furthermore, the |
| 150 |
* caller must copy the bytes if they need to retain them since the |
| 151 |
* object's representation can change as a result of other operations. |
| 152 |
* |
| 153 |
* Side effects: |
| 154 |
* May call the object's updateStringProc to update the string |
| 155 |
* representation from the internal representation. |
| 156 |
* |
| 157 |
*---------------------------------------------------------------------- |
| 158 |
*/ |
| 159 |
char * |
| 160 |
ImgGetByteArrayFromObj(objPtr, lengthPtr) |
| 161 |
register Tcl_Obj *objPtr; /* Object whose string rep byte pointer |
| 162 |
* should be returned, or NULL */ |
| 163 |
register int *lengthPtr; /* If non-NULL, the location where the |
| 164 |
* string rep's byte array length should be |
| 165 |
* stored. If NULL, no length is stored. */ |
| 166 |
{ |
| 167 |
#ifdef _LANG |
| 168 |
char *string = Tcl_GetString(objPtr); |
| 169 |
if (lengthPtr != NULL) { |
| 170 |
*lengthPtr = string ? strlen(string) : 0; |
| 171 |
} |
| 172 |
return string; |
| 173 |
#else /* _LANG */ |
| 174 |
if (initialized & IMG_OBJS) { |
| 175 |
ByteArray *baPtr; |
| 176 |
if (byteArrayType) { |
| 177 |
if (objPtr->typePtr != byteArrayType) { |
| 178 |
byteArrayType->setFromAnyProc(NULL, objPtr); |
| 179 |
} |
| 180 |
} else if (objPtr->typePtr && !strcmp(objPtr->typePtr->name, "bytearray")) { |
| 181 |
byteArrayType = objPtr->typePtr; |
| 182 |
} else { |
| 183 |
return Tcl_GetStringFromObj(objPtr, lengthPtr); |
| 184 |
} |
| 185 |
baPtr = (ByteArray *) (objPtr)->internalRep.otherValuePtr; |
| 186 |
if (lengthPtr != NULL) { |
| 187 |
*lengthPtr = baPtr->used; |
| 188 |
} |
| 189 |
return (unsigned char *) baPtr->bytes; |
| 190 |
} else { |
| 191 |
char *string = (char *) objPtr; |
| 192 |
if (lengthPtr != NULL) { |
| 193 |
*lengthPtr = string ? strlen(string) : 0; |
| 194 |
} |
| 195 |
return string; |
| 196 |
} |
| 197 |
#endif /* _LANG */ |
| 198 |
} |
| 199 |
|
| 200 |
/* |
| 201 |
*---------------------------------------------------------------------- |
| 202 |
* |
| 203 |
* ImgListObjGetElements -- |
| 204 |
* |
| 205 |
* Splits an object into its compoments. |
| 206 |
* |
| 207 |
* Results: |
| 208 |
* If objPtr is a valid list (or can be converted to one), |
| 209 |
* TCL_OK will be returned. The object will be split in |
| 210 |
* its components. |
| 211 |
* Otherwise TCL_ERROR is returned. If interp is not a NULL |
| 212 |
* pointer, an error message will be left in it as well. |
| 213 |
* |
| 214 |
* Side effects: |
| 215 |
* May call the object's updateStringProc to update the string |
| 216 |
* representation from the internal representation. |
| 217 |
* |
| 218 |
*---------------------------------------------------------------------- |
| 219 |
*/ |
| 220 |
|
| 221 |
int |
| 222 |
ImgListObjGetElements(interp, objPtr, objc, objv) |
| 223 |
Tcl_Interp *interp; |
| 224 |
Tcl_Obj *objPtr; |
| 225 |
int *objc; |
| 226 |
Tcl_Obj ***objv; |
| 227 |
{ |
| 228 |
static Tcl_Obj *staticObj = (Tcl_Obj *) NULL; |
| 229 |
|
| 230 |
if (objPtr == NULL) { |
| 231 |
*objc = 0; |
| 232 |
return TCL_OK; |
| 233 |
} |
| 234 |
#ifndef _LANG |
| 235 |
if (!(initialized & IMG_OBJS)) { |
| 236 |
if (staticObj != (Tcl_Obj *) NULL) { |
| 237 |
Tcl_DecrRefCount(staticObj); |
| 238 |
} |
| 239 |
objPtr = staticObj = Tcl_NewStringObj((char *) objPtr, -1); |
| 240 |
Tcl_IncrRefCount(staticObj); |
| 241 |
} |
| 242 |
#endif |
| 243 |
return Tcl_ListObjGetElements(interp, objPtr, objc, objv); |
| 244 |
} |
| 245 |
/* |
| 246 |
*---------------------------------------------------------------------- |
| 247 |
* |
| 248 |
* ImgOpenFileChannel -- |
| 249 |
* |
| 250 |
* Open a file channel in binary mode. If permissions is 0, the |
| 251 |
* file will be opened in read mode, otherwise in write mode. |
| 252 |
* |
| 253 |
* Results: |
| 254 |
* The same as Tcl_OpenFileChannel, only the file will |
| 255 |
* always be opened in binary mode without encoding. |
| 256 |
* |
| 257 |
* Side effects: |
| 258 |
* If function fails, an error message will be left in the |
| 259 |
* interpreter. |
| 260 |
* |
| 261 |
*---------------------------------------------------------------------- |
| 262 |
*/ |
| 263 |
|
| 264 |
Tcl_Channel |
| 265 |
ImgOpenFileChannel(interp, fileName, permissions) |
| 266 |
Tcl_Interp *interp; |
| 267 |
CONST char *fileName; |
| 268 |
int permissions; |
| 269 |
{ |
| 270 |
Tcl_Channel chan = Tcl_OpenFileChannel(interp, (char *) fileName, |
| 271 |
permissions?"w":"r", permissions); |
| 272 |
if (!chan) { |
| 273 |
return (Tcl_Channel) NULL; |
| 274 |
} |
| 275 |
if (Tcl_SetChannelOption(interp, chan, "-translation", "binary") != TCL_OK) { |
| 276 |
Tcl_Close(interp, chan); |
| 277 |
return (Tcl_Channel) NULL; |
| 278 |
} |
| 279 |
return chan; |
| 280 |
} |
| 281 |
|
| 282 |
/* |
| 283 |
* Various Compatibility functions |
| 284 |
*/ |
| 285 |
|
| 286 |
void |
| 287 |
ImgFixChanMatchProc(interp, chan, file, format, width, height) |
| 288 |
Tcl_Interp **interp; |
| 289 |
Tcl_Channel *chan; |
| 290 |
Tcl_Obj **file; |
| 291 |
Tcl_Obj **format; |
| 292 |
int **width; |
| 293 |
int **height; |
| 294 |
{ |
| 295 |
#ifndef _LANG |
| 296 |
Tcl_Interp *tmp; |
| 297 |
|
| 298 |
if (initialized & IMG_PERL) { |
| 299 |
return; |
| 300 |
} |
| 301 |
if (initialized & IMG_OBJS) { |
| 302 |
tmp = (Tcl_Interp *) *height; |
| 303 |
if (tmp->result != ((Interp *) tmp)->resultSpace) { |
| 304 |
return; |
| 305 |
} |
| 306 |
} else { |
| 307 |
tmp = (Tcl_Interp *) NULL; |
| 308 |
} |
| 309 |
|
| 310 |
*height = *width; |
| 311 |
*width = (int *) *format; |
| 312 |
*format = (Tcl_Obj *) *file; |
| 313 |
*file = (CONST char *) *chan; |
| 314 |
*chan = (Tcl_Channel) *interp; |
| 315 |
*interp = tmp; |
| 316 |
#endif |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
void |
| 321 |
ImgFixObjMatchProc(interp, data, format, width, height) |
| 322 |
Tcl_Interp **interp; |
| 323 |
Tcl_Obj **data; |
| 324 |
Tcl_Obj **format; |
| 325 |
int **width; |
| 326 |
int **height; |
| 327 |
{ |
| 328 |
#ifndef _LANG |
| 329 |
Tcl_Interp *tmp; |
| 330 |
|
| 331 |
if (initialized & IMG_PERL) { |
| 332 |
return; |
| 333 |
} |
| 334 |
if (initialized & IMG_OBJS) { |
| 335 |
tmp = (Tcl_Interp *) *height; |
| 336 |
if (tmp->result != ((Interp *) tmp)->resultSpace) { |
| 337 |
return; |
| 338 |
} |
| 339 |
} else { |
| 340 |
tmp = (Tcl_Interp *) NULL; |
| 341 |
} |
| 342 |
*height = *width; |
| 343 |
*width = (int *) *format; |
| 344 |
*format = (Tcl_Obj *) *data; |
| 345 |
*data = (Tcl_Obj *) *interp; |
| 346 |
*interp = tmp; |
| 347 |
#endif |
| 348 |
} |
| 349 |
|
| 350 |
void |
| 351 |
ImgFixStringWriteProc(data, interp, dataPtr, format, blockPtr) |
| 352 |
Tcl_DString *data; |
| 353 |
Tcl_Interp **interp; |
| 354 |
Tcl_DString **dataPtr; |
| 355 |
Tcl_Obj **format; |
| 356 |
Tk_PhotoImageBlock **blockPtr; |
| 357 |
{ |
| 358 |
*dataPtr = data; |
| 359 |
Tcl_DStringInit(data); |
| 360 |
} |