summaryrefslogtreecommitdiffstats
path: root/tkimg/base/tkimgUtils.c
blob: 64c725159e6587e5533ccde4662c836ec1678f90 (plain)
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
/*

 *  tkimgUtils.tcl

 */

#include <string.h>

#include <stdlib.h>

#include "tkimg.h"


/*

 * The variable "tkimg_initialized" contains flags indicating which

 * version of Tcl or Perl we are running:

 *

 *  IMG_PERL        perl

 *  IMG_COMPOSITE   Photo image type proc signatures are 8.4 or higher.

 *  IMG_NOPANIC     Photo image type proc signatures are 8.5 or higher.

 *

 * These flags will be determined at runtime (except the IMG_PERL

 * flag, for now), so we can use the same dynamic library for all

 * Tcl/Tk versions (and for Perl/Tk in the future).

 *

 * The existence of the CPP macro _LANG implies usage in Perl/Tk.

 *

 * Img 1.4: Support for Tcl 8.2 and lower is dropped.

 */

int tkimg_initialized = 0;

int TkimgInitUtilities(
	Tcl_Interp *interp
) {
#ifdef _LANG

	tkimg_initialized = IMG_PERL;
#else


	int major, minor, patchlevel, type;
	tkimg_initialized = IMG_TCL;

	Tcl_GetVersion(&major, &minor, &patchlevel, &type);

	if ((major > 8) || ((major == 8) && (minor > 3))) {
		tkimg_initialized |= IMG_COMPOSITE;
	}
	if ((major > 8) || ((major == 8) && (minor > 4))) {
		tkimg_initialized |= IMG_NOPANIC;
	}

#endif

	return tkimg_initialized;
}


/*

 *----------------------------------------------------------------------

 *

 * tkimg_GetStringFromObj --

 *

 *  Returns the string representation's byte array pointer and length

 *  for an object.

 *

 * Results:

 *  Returns a pointer to the string representation of objPtr.  If

 *  lengthPtr isn't NULL, the length of the string representation is

 *  stored at *lengthPtr. The byte array referenced by the returned

 *  pointer must not be modified by the caller. Furthermore, the

 *  caller must copy the bytes if they need to retain them since the

 *  object's string rep can change as a result of other operations.

 *      REMARK: This function reacts a little bit different than

 *  Tcl_GetStringFromObj():

 *  - objPtr is allowed to be NULL. In that case the NULL pointer

 *    will be returned, and the length will be reported to be 0;

 *  In the tkimg_ code there is never a distinction between en empty

 *  string and a NULL pointer, while the latter is easier to check

 *  for. That's the reason for this difference.

 *

 * Side effects:

 *  May call the object's updateStringProc to update the string

 *  representation from the internal representation.

 *

 *----------------------------------------------------------------------

 */

const char *tkimg_GetStringFromObj(
	register Tcl_Obj *objPtr, /* Object whose string rep byte pointer

	 * should be returned, or NULL */
	register int *lengthPtr /* If non-NULL, the location where the

	 * string rep's byte array length should be

	 * stored. If NULL, no length is stored. */
) {
	if (!objPtr) {
		if (lengthPtr) {
			*lengthPtr = 0;
		}
		return NULL;
	}
#ifdef _LANG

	{
		char *string = LangString((Arg) objPtr);
		if (lengthPtr) {
			*lengthPtr = string? strlen(string): 0;
		}
		return string;
	}
#else /* _LANG */

	return Tcl_GetStringFromObj(objPtr, lengthPtr);
#endif /* _LANG */

}


/*

 *----------------------------------------------------------------------

 *

 * tkimg_GetByteArrayFromObj --

 *

 *  Returns the binary representation and length

 *  for a byte array object.

 *

 * Results:

 *  Returns a pointer to the byte representation of objPtr.  If

 *  lengthPtr isn't NULL, the length of the string representation is

 *  stored at *lengthPtr. The byte array referenced by the returned

 *  pointer must not be modified by the caller. Furthermore, the

 *  caller must copy the bytes if they need to retain them since the

 *  objects representation can change as a result of other operations.

 *

 * Side effects:

 *  May call the object's updateStringProc to update the string

 *  representation from the internal representation.

 *

 *----------------------------------------------------------------------

 */
unsigned char *tkimg_GetByteArrayFromObj(
	register Tcl_Obj *objPtr, /**< Object whose string rep byte pointer

	 * should be returned, or NULL */
	register int *lengthPtr /**< If non-NULL, the location where the

	 * string rep's byte array length should be

	 * stored. If NULL, no length is stored. */
) {
#ifdef _LANG

	char *string = LangString((Arg) objPtr);
	if (lengthPtr) {
		*lengthPtr = string? strlen(string): 0;
	}
	return (unsigned char *) string;
#else /* _LANG */


	return Tcl_GetByteArrayFromObj(objPtr, lengthPtr);
#endif /* _LANG */

}


/*

 *----------------------------------------------------------------------

 *

 * tkimg_ListObjGetElements --

 *

 *  Splits an object into its components.

 *

 * Results:

 *  If objPtr is a valid list (or can be converted to one),

 *  TCL_OK will be returned. The object will be split in

 *  its components.

 *  Otherwise TCL_ERROR is returned. If interp is not a NULL

 *  pointer, an error message will be left in it as well.

 *

 * Side effects:

 *  May call the object's updateStringProc to update the string

 *  representation from the internal representation.

 *

 *----------------------------------------------------------------------

 */

int tkimg_ListObjGetElements(
	Tcl_Interp *interp,
	Tcl_Obj *objPtr,
	int *objc,
	Tcl_Obj ***objv
) {
	if (!objPtr) {
		*objc = 0;
		return TCL_OK;
	}
	return Tcl_ListObjGetElements(interp, objPtr, objc, objv);
}

/*

 *----------------------------------------------------------------------

 *

 * tkimg_Fix*Proc --

 *

 *  Various Compatibility functions

 *

 * Results:

 *  The functions below allow an image type to distinguish

 *  between a call made by Tk 8.3.2 or earlier (4 arguments), versus 8.3

 *  or later (3 arguments) and adapt at runtime. This adaption is done

 *  by shuffling the incoming arguments around to their correct positions.

 *

 * Side effects:

 *  S.a.

 *

 *----------------------------------------------------------------------

 */

void tkimg_FixChanMatchProc(
	Tcl_Interp **interp,
	Tcl_Channel *chan,
	const char **file,
	Tcl_Obj **format,
	int **width,
	int **height
) {
	Tcl_Interp *tmp;

	if (tkimg_initialized & IMG_PERL) {
		return;
	}
	tmp = (Tcl_Interp *) *height;

	*height = *width;
	*width = (int *) *format;
	*format = (Tcl_Obj *) *file;
	*file = (const char *) *chan;
	*chan = (Tcl_Channel) *interp;
	*interp = tmp;
}

void tkimg_FixObjMatchProc(
	Tcl_Interp **interp,
	Tcl_Obj **data,
	Tcl_Obj **format,
	int **width,
	int **height
) {
	Tcl_Interp *tmp;

	if (tkimg_initialized & IMG_PERL) {
		return;
	}
	tmp = (Tcl_Interp *) *height;

	*height = *width;
	*width = (int *) *format;
	*format = (Tcl_Obj *) *data;
	*data = (Tcl_Obj *) *interp;
	*interp = tmp;
}

void tkimg_FixStringWriteProc(
	Tcl_DString *data,
	Tcl_Interp **interp,
	Tcl_DString **dataPtr,
	Tcl_Obj **format,
	Tk_PhotoImageBlock **blockPtr
) {
	if (!*blockPtr) {
		*blockPtr = (Tk_PhotoImageBlock *) *format;
		*format = (Tcl_Obj *) *dataPtr;
		*dataPtr = data;
		Tcl_DStringInit(data);
	}
}