10 #define TPL_FILE_TYPE_DISC 0
11 #define TPL_FILE_TYPE_MEM 1
13 #define TPL_HDR_VERSION_FIELD 0
14 #define TPL_HDR_NTEXTURE_FIELD 4
15 #define TPL_HDR_HDRSIZE_FIELD 8
16 #define TPL_HDR_DESCR_FIELD 12
19 typedef struct _tplimgheader TPLImgHeader
;
21 struct _tplimgheader
{
37 // texture palette header
38 typedef struct _tplpalheader TPLPalHeader
;
40 struct _tplpalheader
{
49 typedef struct _tpldesc TPLDescHeader
;
52 TPLImgHeader
*imghead
;
53 TPLPalHeader
*palhead
;
56 static u32
TPL_GetTextureSize(u32 width
,u32 height
,u32 fmt
)
64 size
= ((width
+7)>>3)*((height
+7)>>3)*32;
69 size
= ((width
+7)>>3)*((height
+7)>>2)*32;
75 size
= ((width
+3)>>2)*((height
+3)>>2)*32;
78 size
= ((width
+3)>>2)*((height
+3)>>2)*32*2;
86 s32
TPL_OpenTPLFromFile(TPLFile
* tdf
, const char* file_name
)
91 TPLDescHeader
*deschead
= NULL
;
92 TPLImgHeader
*imghead
= NULL
;
93 TPLPalHeader
*palhead
= NULL
;
95 if(!file_name
) return 0;
97 f
= fopen(file_name
,"rb");
100 tdf
->type
= TPL_FILE_TYPE_DISC
;
101 tdf
->tpl_file
= (FHANDLE
)f
;
103 fread(&version
,sizeof(u32
),1,f
);
104 fread(&tdf
->ntextures
,sizeof(u32
),1,f
);
106 fseek(f
,TPL_HDR_DESCR_FIELD
,SEEK_SET
);
108 deschead
= malloc(tdf
->ntextures
*sizeof(TPLDescHeader
));
110 fread(deschead
,sizeof(TPLDescHeader
),tdf
->ntextures
,f
);
112 for(c
=0;c
<tdf
->ntextures
;c
++) {
113 imghead
= deschead
[c
].imghead
;
114 palhead
= deschead
[c
].palhead
;
116 //now read in the image data.
117 fseek(f
,(s32
)imghead
,SEEK_SET
);
118 imghead
= malloc(sizeof(TPLImgHeader
));
119 if(!imghead
) goto error_open
;
121 fread(imghead
,sizeof(TPLImgHeader
),1,f
);
122 deschead
[c
].imghead
= imghead
;
125 fseek(f
,(s32
)palhead
,SEEK_SET
);
127 palhead
= malloc(sizeof(TPLPalHeader
));
128 if(!palhead
) goto error_open
;
130 fread(palhead
,sizeof(TPLPalHeader
),1,f
);
131 deschead
[c
].palhead
= palhead
;
134 tdf
->texdesc
= deschead
;
140 if(deschead
) free(deschead
);
141 if(palhead
) free(palhead
);
147 s32
TPL_OpenTPLFromMemory(TPLFile
* tdf
, const void *memory
,u32 len
)
151 const char *p
= memory
;
152 TPLDescHeader
*deschead
= NULL
;
153 TPLImgHeader
*imghead
= NULL
;
154 TPLPalHeader
*palhead
= NULL
;
156 if(!memory
|| !len
) return -1; //TPL_ERR_INVALID
158 tdf
->type
= TPL_FILE_TYPE_MEM
;
159 tdf
->tpl_file
= (FHANDLE
)NULL
;
161 version
= *(u32
*)(p
+ TPL_HDR_VERSION_FIELD
);
162 tdf
->ntextures
= *(u32
*)(p
+ TPL_HDR_NTEXTURE_FIELD
);
164 deschead
= (TPLDescHeader
*)(p
+ TPL_HDR_DESCR_FIELD
);
165 for(c
=0;c
<tdf
->ntextures
;c
++) {
169 pos
= (u32
)deschead
[c
].imghead
;
170 imghead
= (TPLImgHeader
*)(p
+ pos
);
172 pos
= (u32
)imghead
->data
;
173 imghead
->data
= (char*)(p
+ pos
);
175 pos
= (u32
)deschead
[c
].palhead
;
177 palhead
= (TPLPalHeader
*)(p
+ pos
);
179 pos
= (u32
)palhead
->data
;
180 palhead
->data
= (char*)(p
+ pos
);
182 deschead
[c
].imghead
= imghead
;
183 deschead
[c
].palhead
= palhead
;
185 tdf
->texdesc
= deschead
;
190 s32
TPL_GetTextureInfo(TPLFile
*tdf
,s32 id
,u32
*fmt
,u16
*width
,u16
*height
)
192 TPLDescHeader
*deschead
= NULL
;
193 TPLImgHeader
*imghead
= NULL
;
196 if(id
<0 || id
>=tdf
->ntextures
) return -1;
198 deschead
= (TPLDescHeader
*)tdf
->texdesc
;
199 if(!deschead
) return -1;
201 imghead
= deschead
[id
].imghead
;
202 if(!imghead
) return -1;
204 if(fmt
) *fmt
= imghead
->fmt
;
205 if(width
) *width
= imghead
->width
;
206 if(height
) *height
= imghead
->height
;
211 s32
TPL_GetTexture(TPLFile
*tdf
,s32 id
,GXTexObj
*texObj
)
216 TPLDescHeader
*deschead
= NULL
;
217 TPLImgHeader
*imghead
= NULL
;
219 u8 biasclamp
= GX_DISABLE
;
222 if(!texObj
) return -1;
223 if(id
<0 || id
>=tdf
->ntextures
) return -1;
225 deschead
= (TPLDescHeader
*)tdf
->texdesc
;
226 if(!deschead
) return -1;
228 imghead
= deschead
[id
].imghead
;
229 if(!imghead
) return -1;
231 size
= TPL_GetTextureSize(imghead
->width
,imghead
->height
,imghead
->fmt
);
232 if(tdf
->type
==TPL_FILE_TYPE_DISC
) {
233 f
= (FILE*)tdf
->tpl_file
;
234 pos
= (s32
)imghead
->data
;
235 imghead
->data
= memalign(PPC_CACHE_ALIGNMENT
,size
);
236 if(!imghead
->data
) return -1;
238 fseek(f
,pos
,SEEK_SET
);
239 fread(imghead
->data
,1,size
,f
);
242 if(imghead
->maxlod
>0) bMipMap
= 1;
243 if(imghead
->lodbias
>0.0f
) biasclamp
= GX_ENABLE
;
245 DCFlushRange(imghead
->data
,size
);
246 GX_InitTexObj(texObj
,imghead
->data
,imghead
->width
,imghead
->height
,imghead
->fmt
,imghead
->wraps
,imghead
->wrapt
,bMipMap
);
247 if(bMipMap
) GX_InitTexObjLOD(texObj
,imghead
->minfilter
,imghead
->magfilter
,imghead
->minlod
,imghead
->maxlod
,
248 imghead
->lodbias
,biasclamp
,biasclamp
,imghead
->edgelod
);
253 s32
TPL_GetTextureCI(TPLFile
*tdf
,s32 id
,GXTexObj
*texObj
,GXTlutObj
*tlutObj
,u8 tluts
)
258 TPLDescHeader
*deschead
= NULL
;
259 TPLImgHeader
*imghead
= NULL
;
260 TPLPalHeader
*palhead
= NULL
;
262 u8 biasclamp
= GX_DISABLE
;
265 if(!texObj
) return -1;
266 if(!tlutObj
) return -1;
267 if(id
<0 || id
>=tdf
->ntextures
) return -1;
269 deschead
= (TPLDescHeader
*)tdf
->texdesc
;
270 if(!deschead
) return -1;
272 imghead
= deschead
[id
].imghead
;
273 if(!imghead
) return -1;
275 palhead
= deschead
[id
].palhead
;
276 if(!palhead
) return -1;
278 size
= TPL_GetTextureSize(imghead
->width
,imghead
->height
,imghead
->fmt
);
279 if(tdf
->type
==TPL_FILE_TYPE_DISC
) {
280 f
= (FILE*)tdf
->tpl_file
;
281 pos
= (s32
)imghead
->data
;
282 imghead
->data
= memalign(PPC_CACHE_ALIGNMENT
,size
);
283 if(!imghead
->data
) return -1;
285 fseek(f
,pos
,SEEK_SET
);
286 fread(imghead
->data
,1,size
,f
);
288 pos
= (s32
)palhead
->data
;
289 palhead
->data
= memalign(PPC_CACHE_ALIGNMENT
,(palhead
->nitems
*sizeof(u16
)));
295 fseek(f
,pos
,SEEK_SET
);
296 fread(palhead
->data
,1,(palhead
->nitems
*sizeof(u16
)),f
);
299 if(imghead
->maxlod
>0) bMipMap
= 1;
300 if(imghead
->lodbias
>0.0f
) biasclamp
= GX_ENABLE
;
302 DCFlushRange(imghead
->data
,size
);
303 DCFlushRange(palhead
->data
,(palhead
->nitems
*sizeof(u16
)));
304 GX_InitTlutObj(tlutObj
,palhead
->data
,palhead
->fmt
,palhead
->nitems
);
305 GX_InitTexObjCI(texObj
,imghead
->data
,imghead
->width
,imghead
->height
,imghead
->fmt
,imghead
->wraps
,imghead
->wrapt
,bMipMap
,tluts
);
306 if(bMipMap
) GX_InitTexObjLOD(texObj
,imghead
->minfilter
,imghead
->magfilter
,imghead
->minlod
,imghead
->maxlod
,
307 imghead
->lodbias
,biasclamp
,biasclamp
,imghead
->edgelod
);
312 void TPL_CloseTPLFile(TPLFile
*tdf
)
316 TPLPalHeader
*palhead
;
317 TPLImgHeader
*imghead
;
318 TPLDescHeader
*deschead
;
322 if(tdf
->type
==TPL_FILE_TYPE_DISC
) {
323 f
= (FILE*)tdf
->tpl_file
;
326 deschead
= (TPLDescHeader
*)tdf
->texdesc
;
327 if(!deschead
) return;
329 for(i
=0;i
<tdf
->ntextures
;i
++) {
330 imghead
= deschead
[i
].imghead
;
331 palhead
= deschead
[i
].palhead
;
333 if(imghead
->data
) free(imghead
->data
);
337 if(palhead
->data
) free(palhead
->data
);
346 tdf
->tpl_file
= NULL
;