1 /*
2  * init.c --
3  *
4  *  Generic photo image type initialization, Tcl/Tk package
5  *
6  * Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net>
7  *
8  */
9 
10 #include "tkimg.h"
11 
12 #ifndef MORE_INITIALIZATION
13 #define MORE_INITIALIZATION /* Nothing */
14 #endif
15 
16 /*
17  * Functions exported for package management.
18  */
19 
20 
21 extern DLLEXPORT int @CPACKAGE@_Init(Tcl_Interp *interp);
22 extern DLLEXPORT int @CPACKAGE@_SafeInit(Tcl_Interp *interp);
23 
24 /*
25  * Declarations of internal functions.
26  */
27 
28 static int ChnMatch(Tcl_Channel chan, const char *fileName,
29 	Tcl_Obj *format, int *widthPtr, int *heightPtr,	Tcl_Interp *interp);
30 
31 static int ObjMatch(Tcl_Obj *dataObj, Tcl_Obj *format,
32 	int *widthPtr, int *heightPtr, Tcl_Interp *interp);
33 
34 static int ChnRead(Tcl_Interp *interp, Tcl_Channel chan,
35 	const char *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle,
36 	int destX, int destY, int width, int height, int srcX, int srcY);
37 
38 static int ObjRead(Tcl_Interp *interp, Tcl_Obj *dataObj,
39 	Tcl_Obj *format, Tk_PhotoHandle imageHandle,
40 	int destX, int destY, int width, int height, int srcX, int srcY);
41 
42 static int ChnWrite(Tcl_Interp *interp, const char *filename,
43 	Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr);
44 
45 static int StringWrite(Tcl_Interp *interp, Tcl_Obj *format,
46 	Tk_PhotoImageBlock *blockPtr);
47 
48 static Tk_PhotoImageFormat sImageFormat = {
49 	(char *) "%PHIMGTYPE%", /* name */
50 	ChnMatch, /* fileMatchProc */
51 	ObjMatch, /* stringMatchProc */
52 	ChnRead, /* fileReadProc */
53 	ObjRead, /* stringReadProc */
54 	ChnWrite, /* fileWriteProc */
55 	StringWrite /* stringWriteProc */
56 };
57 
58 #ifdef SECOND_FORMAT
59 /*
60  * Declare procedures of the second format as needed. The macro we
61  * check for allow us to share code between first and second
62  * format. Current user of this feature: The PS/PDF combo handler
63  */
64 
65 #ifndef SECOND_CHNMATCH
66 #define SECOND_CHNMATCH ChnMatchBeta
67 static int ChnMatchBeta(Tcl_Channel chan, const char *fileName,
68 	Tcl_Obj *format, int *widthPtr, int *heightPtr, Tcl_Interp *interp);
69 #endif
70 #ifndef SECOND_OBJMATCH
71 #define SECOND_OBJMATCH ObjMatchBeta
72 static int ObjMatchBeta(Tcl_Obj *dataObj, Tcl_Obj *format,
73 	int *widthPtr, int *heightPtr, Tcl_Interp *interp);
74 #endif
75 #ifndef SECOND_CHNREAD
76 #define SECOND_CHNREAD ChnReadBeta
77 static int ChnReadBeta(Tcl_Interp *interp, Tcl_Channel chan,
78 	const char *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle,
79 	int destX, int destY, int width, int height, int srcX, int srcY);
80 #endif
81 #ifndef SECOND_OBJREAD
82 #define SECOND_OBJREAD ChnObjReadBeta
83 static int ObjReadBeta(Tcl_Interp *interp, Tcl_Obj *dataObj,
84 	Tcl_Obj *format, Tk_PhotoHandle imageHandle,
85 	int destX, int destY, int width, int height, int srcX, int srcY);
86 #endif
87 #ifndef SECOND_CHNWRITE
88 #define SECOND_CHNWRITE ChnWriteBeta
89 static int ChnWriteBeta(Tcl_Interp *interp, const char *filename,
90 	Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr);
91 #endif
92 #ifndef SECOND_STRWRITE
93 #define SECOND_STRWRITE StringWriteBeta
94 static int StringWriteBeta(Tcl_Interp *interp, Tcl_Obj *format,
95 	Tk_PhotoImageBlock *blockPtr);
96 #endif
97 
98 static Tk_PhotoImageFormat sImageFormatBeta = {
99 	(char *) "%PHIMGTYPE_BETA%", /* name */
100 	SECOND_CHNMATCH, /* fileMatchProc */
101 	SECOND_OBJMATCH, /* stringMatchProc */
102 	SECOND_CHNREAD, /* fileReadProc */
103 	SECOND_OBJREAD, /* stringReadProc */
104 	SECOND_CHNWRITE, /* fileWriteProc */
105 	SECOND_STRWRITE /* stringWriteProc */
106 };
107 
108 #endif /* SECOND_FORMAT */
109 
110 
111 /*
112  *----------------------------------------------------------------------------
113  *
114  * @CPACKAGE@_Init --
115  *
116  *  Initialisation routine for loadable module
117  *
118  * Results:
119  *  None.
120  *
121  * Side effects:
122  *  Creates commands in the interpreter, loads package.
123  *
124  *----------------------------------------------------------------------------
125  */
126 
127 int
_Init(Tcl_Interp * interp)128 @CPACKAGE@_Init(
129 	Tcl_Interp *interp /* Interpreter to initialise. */
130 ) {
131 	if (!Tcl_InitStubs(interp, "8.3", 0)) {
132 		return TCL_ERROR;
133 	}
134 	if (!Tk_InitStubs(interp, "8.3", 0)) {
135 		return TCL_ERROR;
136 	}
137 	if (!Tkimg_InitStubs(interp, TKIMG_VERSION, 0)) {
138 		return TCL_ERROR;
139 	}
140 
141 	MORE_INITIALIZATION;
142 
143 	/*
144 	 * Register the new photo image type.
145 	 */
146 
147 	Tk_CreatePhotoImageFormat(&sImageFormat);
148 #ifdef SECOND_FORMAT
149 	Tk_CreatePhotoImageFormat(&sImageFormatBeta);
150 #endif /* SECOND_FORMAT */
151 
152 	/*
153 	 * At last provide the package ...
154 	 */
155 
156 	if (Tcl_PkgProvide(interp, PACKAGE_TCLNAME, TKIMG_VERSION) != TCL_OK) {
157 		return TCL_ERROR;
158 	}
159 	return TCL_OK;
160 }
161 
162 /*
163  *----------------------------------------------------------------------------
164  *
165  * @CPACKAGE@_SafeInit --
166  *
167  *  Initialisation routine for loadable module in a safe interpreter.
168  *
169  * Results:
170  *  None.
171  *
172  * Side effects:
173  *  Creates commands in the interpreter, loads package.
174  *
175  *----------------------------------------------------------------------------
176  */
177 
178 int
_SafeInit(Tcl_Interp * interp)179 @CPACKAGE@_SafeInit(
180 	Tcl_Interp *interp /* Interpreter to initialise. */
181 ) {
182 	return @CPACKAGE@_Init(interp);
183 }
184 
185