From 756f28cd417710498e8fe0174c5ed2eb871cae16 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 01:36:28 -0700 Subject: [PATCH 01/16] Added texture pack headers correctly read in --- Makefile | 5 ++++- include/devil1tex.h | 50 +++++++++++++++++++++++++++++++++++++++++ src/devil1tex.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ test/main.c | 19 ++++++++++++++-- 4 files changed, 125 insertions(+), 3 deletions(-) create mode 100644 include/devil1tex.h create mode 100644 src/devil1tex.c diff --git a/Makefile b/Makefile index bec6c8c..23ac41f 100644 --- a/Makefile +++ b/Makefile @@ -4,11 +4,14 @@ CFLAGS= -I"include" all: main -main: devil1pld.o +main: devil1pld.o devil1tex.o $(CC) $^ test/main.c $(CFLAGS) -o $(EX) devil1pld.o: src/devil1pld.c $(CC) -c $^ $(CFLAGS) +devil1tex.o: src/devil1tex.c + $(CC) -c $^ $(CFLAGS) + clean: rm *.o $(EX) diff --git a/include/devil1tex.h b/include/devil1tex.h new file mode 100644 index 0000000..df279c0 --- /dev/null +++ b/include/devil1tex.h @@ -0,0 +1,50 @@ +#ifndef DEVIL1TEX_H +#define DEVIL1TEX_H + +#include + +#pragma pack(push, 1) +// disable struct padding +// to easily impose struct on plain data. + +struct TexturePack { + char id[4]; + int32_t batchNumber; + uint32_t firstBatchOffset; // + uint32_t unknownA; +}; + +struct TextureBatchDescriptor{ + int32_t batchIdx; + uint32_t hash; // + uint32_t texNumber; + uint32_t unknownA[8]; // + uint32_t textureSize; // + uint32_t unknownB[30]; +}; + +struct Texture { + // size of array is defined by descriptor + // textureSize + unsigned char *data; +}; + +struct TextureBatch { + // quantity of textures are defined by descriptor + // texNumber + struct Texture *batch; +}; + +#pragma pack(pop) +// re-enable stuct padding for whatever other reason. + +void gettph(struct TexturePack*, const char*); + +void printtph(struct TexturePack*); + +void gettbd(struct TextureBatchDescriptor*, + struct TexturePack *tph, + const char*, + unsigned int); + +#endif diff --git a/src/devil1tex.c b/src/devil1tex.c new file mode 100644 index 0000000..8bd8ab3 --- /dev/null +++ b/src/devil1tex.c @@ -0,0 +1,54 @@ +#include "devil1tex.h" +#include +#include + +// get texture pack header +void gettph (struct TexturePack *tp, const char *filedata) { +// Assume the Texture Pack Header is already allocated memory. + if (tp != NULL && filedata != NULL) { + memcpy(tp, filedata, sizeof(struct TexturePack)); + } + return; +} + +void printtph (struct TexturePack *tp) { + if (tp != NULL) { + printf("id: \t %c %c %c %c \n", tp -> id[3], + tp -> id[2], + tp -> id[1], + tp -> id[0]); + printf("batch#: \t %i \n", tp -> batchNumber); + printf("1st batch: \t %i \n", tp -> firstBatchOffset); + printf("unknownA: \t %i\n", tp -> unknownA); + } + return; +} + +// get texture pack descriptors +void gettbd (struct TextureBatchDescriptor *tbd, + struct TexturePack* tph, + const char *filedata, + unsigned int filesize) { + if (tbd == NULL || tph == NULL || filedata == NULL) { + // any of these being null will cause segmentation fault. + return; + } + // do some checks regarding the header + if (tph -> firstBatchOffset >= filesize || + tph -> firstBatchOffset <= 0) { + return; + } + unsigned int index = tph -> firstBatchOffset; + // Batch Descriptors are structured one after another. + // therefore the next one is at + // current index + sizeof(TextureBatchDescriptor) + unsigned int i = 0; + do { + index += (sizeof(struct TextureBatchDescriptor) * i); + printf("batch descriptor start: %x\n", index); + tbd = (struct TextureBatchDescriptor*)(filedata + index); + // + } while(i < (tph -> batchNumber)); + return; +} + diff --git a/test/main.c b/test/main.c index e5879e6..84e1a7d 100644 --- a/test/main.c +++ b/test/main.c @@ -1,9 +1,11 @@ -#include "devil1pld.h" #include #include #include #include +#include "devil1pld.h" +#include "devil1tex.h" + bool writedata(const char *basename, unsigned int id, const char *data, @@ -88,11 +90,24 @@ bool unpackpld (const char *filedata, return status; } + + int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); - unpackpld(buffer, bufsize, f); +// unpackpld(buffer, bufsize, f); + struct TexturePack *tp = (struct TexturePack*)malloc + (sizeof(struct TexturePack)); + printf("%d %d %d %d \n", buffer[0], + buffer[1], + buffer[2], + buffer[3]); + gettph(tp, buffer); + printf("%x %x \n", buffer + 0, tp -> id[0]); + printtph(tp); + + free(tp); free(buffer); return 0; } From a446dd87417fe6802032e8726fcc56da8daefd7f Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 10:12:29 -0700 Subject: [PATCH 02/16] Implemented function that correctly acquires TextureBatchDescriptors --- include/devil1tex.h | 7 +++---- src/devil1tex.c | 49 ++++++++++++++++++++++++--------------------- test/main.c | 16 +++++++++------ 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index df279c0..1dc4b89 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -38,12 +38,11 @@ struct TextureBatch { #pragma pack(pop) // re-enable stuct padding for whatever other reason. -void gettph(struct TexturePack*, const char*); - void printtph(struct TexturePack*); -void gettbd(struct TextureBatchDescriptor*, - struct TexturePack *tph, +void printtbd(struct TextureBatchDescriptor*); + +void gettbd(struct TextureBatchDescriptor**, const char*, unsigned int); diff --git a/src/devil1tex.c b/src/devil1tex.c index 8bd8ab3..8e5ccc4 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -2,15 +2,6 @@ #include #include -// get texture pack header -void gettph (struct TexturePack *tp, const char *filedata) { -// Assume the Texture Pack Header is already allocated memory. - if (tp != NULL && filedata != NULL) { - memcpy(tp, filedata, sizeof(struct TexturePack)); - } - return; -} - void printtph (struct TexturePack *tp) { if (tp != NULL) { printf("id: \t %c %c %c %c \n", tp -> id[3], @@ -24,31 +15,43 @@ void printtph (struct TexturePack *tp) { return; } +void printtbd(struct TextureBatchDescriptor *bd) { + if (bd != NULL) { + printf("batch index: \t %i \n", bd -> batchIdx); + printf("hash: \t %u \n", bd -> hash); + printf("textures: \t %i\n", bd -> texNumber); + } +} + // get texture pack descriptors -void gettbd (struct TextureBatchDescriptor *tbd, - struct TexturePack* tph, - const char *filedata, - unsigned int filesize) { - if (tbd == NULL || tph == NULL || filedata == NULL) { - // any of these being null will cause segmentation fault. +void gettbd (struct TextureBatchDescriptor **descriptors, + const char *filedata, + unsigned int filesize) { + if (descriptors == NULL || filedata == NULL || filesize <= 0) { return; } - // do some checks regarding the header - if (tph -> firstBatchOffset >= filesize || - tph -> firstBatchOffset <= 0) { + struct TexturePack *tp = NULL; + tp = (struct TexturePack*)filedata; + if (filesize <= (sizeof(struct TextureBatchDescriptor) + * tp -> batchNumber + + sizeof(struct TexturePack))){ + fprintf(stderr, + "File Error: file size to struct sizes ratio.\n"); return; } - unsigned int index = tph -> firstBatchOffset; + unsigned int index = sizeof(struct TexturePack); // Batch Descriptors are structured one after another. // therefore the next one is at // current index + sizeof(TextureBatchDescriptor) unsigned int i = 0; do { - index += (sizeof(struct TextureBatchDescriptor) * i); + index = sizeof(struct TextureBatchDescriptor) * i + + sizeof(struct TexturePack); printf("batch descriptor start: %x\n", index); - tbd = (struct TextureBatchDescriptor*)(filedata + index); - // - } while(i < (tph -> batchNumber)); + descriptors[i] = (struct TextureBatchDescriptor*) + (filedata + index); + i++; + } while(i < (tp -> batchNumber)); return; } diff --git a/test/main.c b/test/main.c index 84e1a7d..25a8061 100644 --- a/test/main.c +++ b/test/main.c @@ -97,17 +97,21 @@ int main(int argc, char ** argv) { unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); - struct TexturePack *tp = (struct TexturePack*)malloc - (sizeof(struct TexturePack)); + struct TexturePack *tp = NULL; printf("%d %d %d %d \n", buffer[0], buffer[1], buffer[2], buffer[3]); - gettph(tp, buffer); - printf("%x %x \n", buffer + 0, tp -> id[0]); + tp = (struct TexturePack*)buffer; + struct TextureBatchDescriptor **bds = NULL; + bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); + gettbd(bds, buffer, bufsize); printtph(tp); - - free(tp); + int i; + for (i = 0; i < tp -> batchNumber; i++) { + printtbd(bds[i]); + } + free(bds); free(buffer); return 0; } From cff2209971b5ff658f403ac0059a48b2b9b89338 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 19:38:30 -0700 Subject: [PATCH 03/16] Implemented getTextureBatch() --- include/devil1tex.h | 3 +++ src/devil1tex.c | 21 +++++++++++++++++++++ test/main.c | 30 ++++++++++++++++-------------- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 1dc4b89..7b1678a 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -46,4 +46,7 @@ void gettbd(struct TextureBatchDescriptor**, const char*, unsigned int); +void gettexturebatch(struct TextureBatch**, + const char*, + unsigned int); #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index 8e5ccc4..002b372 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -1,5 +1,6 @@ #include "devil1tex.h" #include +#include #include void printtph (struct TexturePack *tp) { @@ -55,3 +56,23 @@ void gettbd (struct TextureBatchDescriptor **descriptors, return; } +void gettexturebatch(struct TextureBatch **tb, + const char *filedata, + unsigned int filesize) { + struct TexturePack *tp = (struct TexturePack*)filedata; + struct TextureBatchDescriptor **bds = NULL; + bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); + gettbd(bds, filedata, filesize); + unsigned int offset = tp -> firstBatchOffset; + unsigned int i; + unsigned int j; + // TextureBatch shares a starting address with the first texture + // and shares the ending address with the last texture. + for (i = 0; i < tp -> batchNumber; i++) { + printf("%x \n", offset); + tb[i] = (struct TextureBatch*)filedata + offset; + // the next texture batch is TextureSize * number of textures away. + offset += bds[i] -> textureSize * (bds[i] -> texNumber); + } + free(bds); +} diff --git a/test/main.c b/test/main.c index 25a8061..2f70e06 100644 --- a/test/main.c +++ b/test/main.c @@ -90,28 +90,30 @@ bool unpackpld (const char *filedata, return status; } - - -int main(int argc, char ** argv) { - char *f = argv[1]; - unsigned int bufsize = 0; - char *buffer = loadfile(f, &bufsize); -// unpackpld(buffer, bufsize, f); - struct TexturePack *tp = NULL; - printf("%d %d %d %d \n", buffer[0], - buffer[1], - buffer[2], - buffer[3]); - tp = (struct TexturePack*)buffer; +/* void unpacktextures(const char *filedata, unsigned int filesize) { + struct TexturePack *tp = (struct TexturePack*)filedata; struct TextureBatchDescriptor **bds = NULL; bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); - gettbd(bds, buffer, bufsize); + gettbd(bds, filedata, filesize); printtph(tp); int i; for (i = 0; i < tp -> batchNumber; i++) { printtbd(bds[i]); } free(bds); + return; +} */ + +int main(int argc, char ** argv) { + char *f = argv[1]; + unsigned int bufsize = 0; + char *buffer = loadfile(f, &bufsize); +// unpackpld(buffer, bufsize, f); +// unpacktextures(buffer, bufsize); + struct TexturePack *tp = (struct TexturePack*)buffer; + struct TextureBatch **batches = NULL; + batches = (struct TextureBatch**)malloc(tp -> batchNumber); + gettexturebatch(batches, buffer, bufsize); free(buffer); return 0; } From 24d4f920dcdc343e874131bd5a5044d0756f19ed Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 21:49:32 -0700 Subject: [PATCH 04/16] Unpack textures has correct offsets for textures --- include/devil1tex.h | 4 +++- src/devil1tex.c | 19 +++++++++++++------ test/main.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 7b1678a..46183ef 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -46,7 +46,9 @@ void gettbd(struct TextureBatchDescriptor**, const char*, unsigned int); -void gettexturebatch(struct TextureBatch**, +unsigned int gettexturebatch( + struct TextureBatch**, + struct TextureBatchDescriptor**, const char*, unsigned int); #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index 002b372..0b5f91e 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -56,23 +56,30 @@ void gettbd (struct TextureBatchDescriptor **descriptors, return; } -void gettexturebatch(struct TextureBatch **tb, +// returns total count of textures in the file. +unsigned int gettexturebatch( + struct TextureBatch **tb, + struct TextureBatchDescriptor **bds, const char *filedata, unsigned int filesize) { struct TexturePack *tp = (struct TexturePack*)filedata; - struct TextureBatchDescriptor **bds = NULL; - bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); - gettbd(bds, filedata, filesize); +// struct TextureBatchDescriptor **bds = NULL; +// bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); +// gettbd(bds, filedata, filesize); unsigned int offset = tp -> firstBatchOffset; unsigned int i; unsigned int j; + unsigned int totaltextures = 0; // TextureBatch shares a starting address with the first texture // and shares the ending address with the last texture. for (i = 0; i < tp -> batchNumber; i++) { printf("%x \n", offset); tb[i] = (struct TextureBatch*)filedata + offset; // the next texture batch is TextureSize * number of textures away. - offset += bds[i] -> textureSize * (bds[i] -> texNumber); + offset += bds[i] -> textureSize * (bds[i] -> texNumber); + totaltextures += bds[i] -> texNumber; } - free(bds); +// free(bds); + return totaltextures; } + diff --git a/test/main.c b/test/main.c index 2f70e06..ba8f4a9 100644 --- a/test/main.c +++ b/test/main.c @@ -102,18 +102,55 @@ bool unpackpld (const char *filedata, } free(bds); return; + + const char *fmt = (const char*)malloc(strlen(filename) + 3 + 3); + strcat(fmt, filename); + strcat(fmt, "%d.dds"); } */ +void unpacktextures(const char *filedata, + unsigned int filesize) { + struct TexturePack *tp = (struct TexturePack*)filedata; + + struct TextureBatch **batches = NULL; + batches = (struct TextureBatch**)malloc(tp -> batchNumber); + + struct TextureBatchDescriptor **descripts = NULL; + descripts = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); + gettbd(descripts, filedata, filesize); + // array of pointers - texture data exists in memory in filedata. + unsigned int texturecount = gettexturebatch(batches, + descripts, + filedata, + filesize); + struct Texture **textures = NULL; + textures = (struct Texture**)malloc(texturecount); + + unsigned int i; + unsigned int j; + // k is access to index of textures. + unsigned int k = 0; + unsigned int offset = 0; + for (i = 0; i < tp -> batchNumber; i++) { + for (j = 0; j < descripts[i] -> texNumber; j++) { + printf("texture offsets %x\n", offset + tp -> firstBatchOffset); + textures[k] = (struct Texture*)batches + offset; + offset += descripts[i] -> textureSize; + k++; + } + } + free(textures); + free(descripts); + free(batches); +} + int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); // unpacktextures(buffer, bufsize); - struct TexturePack *tp = (struct TexturePack*)buffer; - struct TextureBatch **batches = NULL; - batches = (struct TextureBatch**)malloc(tp -> batchNumber); - gettexturebatch(batches, buffer, bufsize); + unpacktextures(buffer, bufsize); free(buffer); return 0; } From 91103da4105e40c040cf64327fb649e43d5f01d3 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 22:05:13 -0700 Subject: [PATCH 05/16] Fixed crash after getting textures list --- test/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/main.c b/test/main.c index ba8f4a9..2279796 100644 --- a/test/main.c +++ b/test/main.c @@ -124,8 +124,8 @@ void unpacktextures(const char *filedata, filedata, filesize); struct Texture **textures = NULL; - textures = (struct Texture**)malloc(texturecount); - + textures = (struct Texture**)malloc( + sizeof(struct Texture**) * texturecount); unsigned int i; unsigned int j; // k is access to index of textures. From 6f915237b4e750cdffbc0d758ffc3d9618668c3a Mon Sep 17 00:00:00 2001 From: _ <_> Date: Wed, 4 Apr 2018 22:14:45 -0700 Subject: [PATCH 06/16] Moved loop to its own function - gettextures() --- include/devil1tex.h | 6 ++++++ src/devil1tex.c | 19 +++++++++++++++++++ test/main.c | 14 +------------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 46183ef..1ae8377 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -51,4 +51,10 @@ unsigned int gettexturebatch( struct TextureBatchDescriptor**, const char*, unsigned int); + +void gettextures(struct Texture **, + struct TexturePack*, + struct TextureBatch**, + struct TextureBatchDescriptor**); + #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index 0b5f91e..1729cce 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -83,3 +83,22 @@ unsigned int gettexturebatch( return totaltextures; } +void gettextures(struct Texture **t, + struct TexturePack *tp, + struct TextureBatch **tb, + struct TextureBatchDescriptor **bds) { + unsigned int i; + unsigned int j; + // k is access to index of textures. + unsigned int k = 0; + unsigned int offset = 0; + for (i = 0; i < tp -> batchNumber; i++) { + for (j = 0; j < bds[i] -> texNumber; j++) { + printf("texture offsets %x\n", offset + tp -> firstBatchOffset); + t[k] = (struct Texture*)tb + offset; + offset += bds[i] -> textureSize; + k++; + } + } +} + diff --git a/test/main.c b/test/main.c index 2279796..b86d540 100644 --- a/test/main.c +++ b/test/main.c @@ -126,19 +126,7 @@ void unpacktextures(const char *filedata, struct Texture **textures = NULL; textures = (struct Texture**)malloc( sizeof(struct Texture**) * texturecount); - unsigned int i; - unsigned int j; - // k is access to index of textures. - unsigned int k = 0; - unsigned int offset = 0; - for (i = 0; i < tp -> batchNumber; i++) { - for (j = 0; j < descripts[i] -> texNumber; j++) { - printf("texture offsets %x\n", offset + tp -> firstBatchOffset); - textures[k] = (struct Texture*)batches + offset; - offset += descripts[i] -> textureSize; - k++; - } - } + gettextures(textures, tp, batches, descripts); free(textures); free(descripts); free(batches); From d3f6a20bece4d77c4bcccd04bc5e4dd7d3c3969c Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 01:10:06 -0700 Subject: [PATCH 07/16] Cleaned up scrapped statements --- src/devil1tex.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/devil1tex.c b/src/devil1tex.c index 1729cce..b7285d7 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -63,9 +63,6 @@ unsigned int gettexturebatch( const char *filedata, unsigned int filesize) { struct TexturePack *tp = (struct TexturePack*)filedata; -// struct TextureBatchDescriptor **bds = NULL; -// bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); -// gettbd(bds, filedata, filesize); unsigned int offset = tp -> firstBatchOffset; unsigned int i; unsigned int j; @@ -79,7 +76,6 @@ unsigned int gettexturebatch( offset += bds[i] -> textureSize * (bds[i] -> texNumber); totaltextures += bds[i] -> texNumber; } -// free(bds); return totaltextures; } From 1b06741be4381366e2a0712171a341476db4823e Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 01:10:52 -0700 Subject: [PATCH 08/16] Implemented texture unpacking - needs testing --- test/main.c | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/test/main.c b/test/main.c index b86d540..2e0f017 100644 --- a/test/main.c +++ b/test/main.c @@ -108,14 +108,31 @@ bool unpackpld (const char *filedata, strcat(fmt, "%d.dds"); } */ +void write(const char *filename, + unsigned int id, + struct Texture *t, + unsigned int size) { + if (filename == NULL) { + return; + } + unsigned int written = 0; + FILE *out = fopen(filename, "wb"); + if (out != NULL) { + written = fwrite(t, sizeof(unsigned char), size, out); + fclose(out); + if (written == 0) { + perror("texture write error"); + } + } +} + void unpacktextures(const char *filedata, - unsigned int filesize) { + unsigned int filesize, + const char *filename) { struct TexturePack *tp = (struct TexturePack*)filedata; - - struct TextureBatch **batches = NULL; - batches = (struct TextureBatch**)malloc(tp -> batchNumber); - + struct TextureBatch **batches = NULL; struct TextureBatchDescriptor **descripts = NULL; + batches = (struct TextureBatch**)malloc(tp -> batchNumber); descripts = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); gettbd(descripts, filedata, filesize); // array of pointers - texture data exists in memory in filedata. @@ -124,9 +141,19 @@ void unpacktextures(const char *filedata, filedata, filesize); struct Texture **textures = NULL; - textures = (struct Texture**)malloc( - sizeof(struct Texture**) * texturecount); + textures = (struct Texture**) + malloc(sizeof(struct Texture**) * texturecount); gettextures(textures, tp, batches, descripts); + // add 3 to buffer for digits, & 4 for file extension + char *fmt = (char*)malloc(strlen(filename) + 3 + 4); + unsigned int texsize = 0; + unsigned int i; + for (i = 0; i < texturecount; i++) { + texsize = descripts[i/(tp -> batchNumber)] -> textureSize; + sprintf(fmt, "test_%d.dds", i); + write(fmt, i, textures[i], texsize); + } + free(fmt); free(textures); free(descripts); free(batches); @@ -137,8 +164,7 @@ int main(int argc, char ** argv) { unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); -// unpacktextures(buffer, bufsize); - unpacktextures(buffer, bufsize); + unpacktextures(buffer, bufsize, f); free(buffer); return 0; } From c8126966121868db418e9a94f9882638798f9cf1 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 14:27:29 -0700 Subject: [PATCH 09/16] detour-implementation of texture unpacking --- include/devil1tex.h | 2 +- src/devil1tex.c | 10 +++++----- test/main.c | 17 ++++++++++------- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 1ae8377..71ad66e 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -52,7 +52,7 @@ unsigned int gettexturebatch( const char*, unsigned int); -void gettextures(struct Texture **, +void gettextures(unsigned int*, struct TexturePack*, struct TextureBatch**, struct TextureBatchDescriptor**); diff --git a/src/devil1tex.c b/src/devil1tex.c index b7285d7..4d06be2 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -78,8 +78,8 @@ unsigned int gettexturebatch( } return totaltextures; } - -void gettextures(struct Texture **t, + +void gettextures(unsigned int *t, struct TexturePack *tp, struct TextureBatch **tb, struct TextureBatchDescriptor **bds) { @@ -87,11 +87,11 @@ void gettextures(struct Texture **t, unsigned int j; // k is access to index of textures. unsigned int k = 0; - unsigned int offset = 0; + unsigned int offset = tp -> firstBatchOffset; for (i = 0; i < tp -> batchNumber; i++) { for (j = 0; j < bds[i] -> texNumber; j++) { - printf("texture offsets %x\n", offset + tp -> firstBatchOffset); - t[k] = (struct Texture*)tb + offset; + printf("texture offsets %x\n", offset); + t[k] = offset; offset += bds[i] -> textureSize; k++; } diff --git a/test/main.c b/test/main.c index 2e0f017..24f8841 100644 --- a/test/main.c +++ b/test/main.c @@ -110,7 +110,7 @@ bool unpackpld (const char *filedata, void write(const char *filename, unsigned int id, - struct Texture *t, + const char* t, unsigned int size) { if (filename == NULL) { return; @@ -120,6 +120,10 @@ void write(const char *filename, if (out != NULL) { written = fwrite(t, sizeof(unsigned char), size, out); fclose(out); +/* unsigned int i; + for (i = 0; i < 10; i++) { + printf("w %u \n", *(t + i)); + } */ if (written == 0) { perror("texture write error"); } @@ -140,10 +144,9 @@ void unpacktextures(const char *filedata, descripts, filedata, filesize); - struct Texture **textures = NULL; - textures = (struct Texture**) - malloc(sizeof(struct Texture**) * texturecount); - gettextures(textures, tp, batches, descripts); + unsigned int *tloc = (unsigned int*) + malloc(sizeof(unsigned int) * texturecount); + gettextures(tloc, tp, batches, descripts); // add 3 to buffer for digits, & 4 for file extension char *fmt = (char*)malloc(strlen(filename) + 3 + 4); unsigned int texsize = 0; @@ -151,10 +154,10 @@ void unpacktextures(const char *filedata, for (i = 0; i < texturecount; i++) { texsize = descripts[i/(tp -> batchNumber)] -> textureSize; sprintf(fmt, "test_%d.dds", i); - write(fmt, i, textures[i], texsize); + write(fmt, i, filedata + tloc[i], texsize); } free(fmt); - free(textures); + free(tloc); free(descripts); free(batches); } From aec8b64fd6be7be08238b005cf89e94079b960b0 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 14:57:06 -0700 Subject: [PATCH 10/16] Deleted scrap code --- src/devil1tex.c | 2 -- test/main.c | 22 ---------------------- 2 files changed, 24 deletions(-) diff --git a/src/devil1tex.c b/src/devil1tex.c index 4d06be2..91316bb 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -70,7 +70,6 @@ unsigned int gettexturebatch( // TextureBatch shares a starting address with the first texture // and shares the ending address with the last texture. for (i = 0; i < tp -> batchNumber; i++) { - printf("%x \n", offset); tb[i] = (struct TextureBatch*)filedata + offset; // the next texture batch is TextureSize * number of textures away. offset += bds[i] -> textureSize * (bds[i] -> texNumber); @@ -90,7 +89,6 @@ void gettextures(unsigned int *t, unsigned int offset = tp -> firstBatchOffset; for (i = 0; i < tp -> batchNumber; i++) { for (j = 0; j < bds[i] -> texNumber; j++) { - printf("texture offsets %x\n", offset); t[k] = offset; offset += bds[i] -> textureSize; k++; diff --git a/test/main.c b/test/main.c index 24f8841..d249ebe 100644 --- a/test/main.c +++ b/test/main.c @@ -90,24 +90,6 @@ bool unpackpld (const char *filedata, return status; } -/* void unpacktextures(const char *filedata, unsigned int filesize) { - struct TexturePack *tp = (struct TexturePack*)filedata; - struct TextureBatchDescriptor **bds = NULL; - bds = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); - gettbd(bds, filedata, filesize); - printtph(tp); - int i; - for (i = 0; i < tp -> batchNumber; i++) { - printtbd(bds[i]); - } - free(bds); - return; - - const char *fmt = (const char*)malloc(strlen(filename) + 3 + 3); - strcat(fmt, filename); - strcat(fmt, "%d.dds"); -} */ - void write(const char *filename, unsigned int id, const char* t, @@ -120,10 +102,6 @@ void write(const char *filename, if (out != NULL) { written = fwrite(t, sizeof(unsigned char), size, out); fclose(out); -/* unsigned int i; - for (i = 0; i < 10; i++) { - printf("w %u \n", *(t + i)); - } */ if (written == 0) { perror("texture write error"); } From dce4180d5d06b38ced1f526142667b48fb2ce1ed Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 15:16:07 -0700 Subject: [PATCH 11/16] Replaced placeholder texture file name --- test/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.c b/test/main.c index d249ebe..0ee9247 100644 --- a/test/main.c +++ b/test/main.c @@ -131,7 +131,7 @@ void unpacktextures(const char *filedata, unsigned int i; for (i = 0; i < texturecount; i++) { texsize = descripts[i/(tp -> batchNumber)] -> textureSize; - sprintf(fmt, "test_%d.dds", i); + sprintf(fmt, "%s_%d.dds", filename, i); write(fmt, i, filedata + tloc[i], texsize); } free(fmt); From 6d0deefe43bf2abdb3c6849fd55d9a99513cfe30 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 17:51:22 -0700 Subject: [PATCH 12/16] Eliminated unneeded parameter --- include/devil1tex.h | 7 +++---- src/devil1tex.c | 7 +++---- test/main.c | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 71ad66e..117ee29 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -52,9 +52,8 @@ unsigned int gettexturebatch( const char*, unsigned int); -void gettextures(unsigned int*, - struct TexturePack*, - struct TextureBatch**, - struct TextureBatchDescriptor**); +void locatetextures(unsigned int*, + struct TexturePack*, + struct TextureBatchDescriptor**); #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index 91316bb..dd22cd5 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -78,10 +78,9 @@ unsigned int gettexturebatch( return totaltextures; } -void gettextures(unsigned int *t, - struct TexturePack *tp, - struct TextureBatch **tb, - struct TextureBatchDescriptor **bds) { +void locatetextures(unsigned int *t, + struct TexturePack *tp, + struct TextureBatchDescriptor **bds) { unsigned int i; unsigned int j; // k is access to index of textures. diff --git a/test/main.c b/test/main.c index 0ee9247..bb45c5a 100644 --- a/test/main.c +++ b/test/main.c @@ -124,7 +124,7 @@ void unpacktextures(const char *filedata, filesize); unsigned int *tloc = (unsigned int*) malloc(sizeof(unsigned int) * texturecount); - gettextures(tloc, tp, batches, descripts); + locatetextures(tloc, tp, descripts); // add 3 to buffer for digits, & 4 for file extension char *fmt = (char*)malloc(strlen(filename) + 3 + 4); unsigned int texsize = 0; From 81af655f35b6944c0bd50fab3fe0136ab246e9cb Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 21:52:56 -0700 Subject: [PATCH 13/16] Added revised functions for handling texture files --- include/devil1tex.h | 17 +++++++++- src/devil1tex.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ test/main.c | 36 +++++++++++++++++++-- 3 files changed, 126 insertions(+), 4 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 117ee29..3dddbdd 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -2,7 +2,7 @@ #define DEVIL1TEX_H #include - +#include #pragma pack(push, 1) // disable struct padding // to easily impose struct on plain data. @@ -56,4 +56,19 @@ void locatetextures(unsigned int*, struct TexturePack*, struct TextureBatchDescriptor**); +bool gettexdescriptor(struct TextureBatchDescriptor**, + unsigned int, + const char *, + unsigned int); + +bool gettexbatch(struct TextureBatch**, + unsigned int, + const char*, + unsigned int); + +bool unpacktexbatch(struct Texture*, + unsigned int, + const char*, + const unsigned int); + #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index dd22cd5..d9d0db6 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -2,6 +2,7 @@ #include #include #include +#include void printtph (struct TexturePack *tp) { if (tp != NULL) { @@ -94,4 +95,80 @@ void locatetextures(unsigned int *t, } } } +// -------------------------------------------------------+ +// Revision Functions +// -------------------------------------------------------+ + +// ** = 'pass by reference' of a pointer to struct +bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, + unsigned int i, + const char *filedata, + unsigned int filesize) { + // starts after the pack header. + bool done = false; + unsigned int offset = sizeof(struct TexturePack); + offset += sizeof(struct TextureBatchDescriptor) * i; + if (filedata != NULL) { + *descriptor = (struct TextureBatchDescriptor*)(filedata + offset); + done = true; + } +// printf("%x %x", *descriptor, filedata); + return done; +} + +// ** = 'pass by reference' of a pointer to struct +bool gettexbatch(struct TextureBatch **batch, + unsigned int i, + const char *filedata, + unsigned int filesize) { + struct TexturePack *p = NULL; + if (filedata == NULL) { // no data to get batch from. + return false; + } + p = (struct TexturePack*)filedata; + if (i > p -> batchNumber) { // no such batch in texture pack + return false; + } + if (i == 0) { + *batch = (struct TextureBatch*)(filedata + (p -> firstBatchOffset)); + return true; + } + struct TextureBatchDescriptor *d = NULL; + // find how large are previous batches in total + // in order to jump to specified texture batch + unsigned int pastbatchsize = p -> firstBatchOffset; + unsigned int j; + for (j = 0; j < i; j++) { + gettexdescriptor(&d, j, filedata, filesize); + pastbatchsize += (d -> textureSize * d -> texNumber); + } + *batch = (struct TextureBatch*)(filedata + pastbatchsize); + //printf("batch offset: %x i: %d\n" , (p -> firstBatchOffset) + pastbatchsize, i); + return true; +} + +bool unpacktexbatch(struct Texture *t, + unsigned int i, + const char *filedata, + const unsigned int filesize) { + if (filedata == NULL) { + return false; + } + struct TextureBatch *b = NULL; + struct TextureBatchDescriptor *d = NULL; + gettexbatch(&b, i, filedata, filesize); + gettexdescriptor(&d, i, filedata, filesize); + + // first texture is at the start of the batch + // second texture is right after. + unsigned int j; + for (j = 0; j < d -> texNumber; j++) { + t[j].data = (unsigned char*)b + (d -> textureSize * j); + //printf("unpack relative batch offset %x i= %i t.data= %x\n" , ((d -> textureSize * j)), i, t[j].data); + } + return true; +} + + + diff --git a/test/main.c b/test/main.c index bb45c5a..6aa2cc0 100644 --- a/test/main.c +++ b/test/main.c @@ -91,7 +91,6 @@ bool unpackpld (const char *filedata, } void write(const char *filename, - unsigned int id, const char* t, unsigned int size) { if (filename == NULL) { @@ -132,7 +131,7 @@ void unpacktextures(const char *filedata, for (i = 0; i < texturecount; i++) { texsize = descripts[i/(tp -> batchNumber)] -> textureSize; sprintf(fmt, "%s_%d.dds", filename, i); - write(fmt, i, filedata + tloc[i], texsize); + write(fmt, filedata + tloc[i], texsize); } free(fmt); free(tloc); @@ -140,12 +139,43 @@ void unpacktextures(const char *filedata, free(batches); } +void exporttextures(const char *filedata, + unsigned int filesize, + const char *filename) { + struct TexturePack *p = NULL; + struct Texture *t = NULL; + struct TextureBatchDescriptor *d = NULL; + char * fmt = NULL; + if (filename == NULL || filesize == 0) { + return; + } + p = (struct TexturePack*)filedata; + fmt = (char*)malloc(strlen(filename) + 3 + 4); + unsigned int i; + unsigned int j; + unsigned int id = 0; + for (i = 0; i < p -> batchNumber; i++) { + gettexdescriptor(&d, i, filedata, filesize); + t = (struct Texture*) + malloc(sizeof(struct Texture) * (d -> texNumber)); + unpacktexbatch(t, i, filedata, filesize); + for (j = 0; j < d -> texNumber; j++) { + sprintf(fmt, "%s_%d.dds", filename, id); + write(fmt, t[j].data, d -> textureSize); + id++; + } + free(t); + } + free(fmt); + return; +} + int main(int argc, char ** argv) { char *f = argv[1]; unsigned int bufsize = 0; char *buffer = loadfile(f, &bufsize); // unpackpld(buffer, bufsize, f); - unpacktextures(buffer, bufsize, f); + exporttextures(buffer, bufsize, f); free(buffer); return 0; } From 5462ec025e9b62fb1291426f19e239e80bc6c2f2 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Thu, 5 Apr 2018 22:01:12 -0700 Subject: [PATCH 14/16] Clean up and reordering of devil1tex component --- include/devil1tex.h | 50 ++++++++++------ src/devil1tex.c | 143 ++++++++++++++++++++++---------------------- 2 files changed, 103 insertions(+), 90 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index 3dddbdd..ffcc2b5 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -3,9 +3,10 @@ #include #include -#pragma pack(push, 1) + // disable struct padding // to easily impose struct on plain data. +#pragma pack(push, 1) struct TexturePack { char id[4]; @@ -36,12 +37,42 @@ struct TextureBatch { }; #pragma pack(pop) -// re-enable stuct padding for whatever other reason. +// -------------------------------------------------------+ +// Functions +// -------------------------------------------------------+ + +// Print Texture Pack Header. void printtph(struct TexturePack*); +// Print Texture Batch Descriptor. void printtbd(struct TextureBatchDescriptor*); +// Get Texture Batch Descriptor. +// ** = 'pass by reference' of a pointer to struct +bool gettexdescriptor(struct TextureBatchDescriptor**, + unsigned int, + const char *, + unsigned int); + +// Get Texture Batch. +// ** = 'pass by reference' of a pointer to struct +bool gettexbatch(struct TextureBatch**, + unsigned int, + const char*, + unsigned int); + +// Unpack Texture Batch +bool unpacktexbatch(struct Texture*, + unsigned int, + const char*, + const unsigned int); + +// -------------------------------------------------------+ +// Retired Functions +// These require the client allocate more on the heap. +// -------------------------------------------------------+ + void gettbd(struct TextureBatchDescriptor**, const char*, unsigned int); @@ -56,19 +87,4 @@ void locatetextures(unsigned int*, struct TexturePack*, struct TextureBatchDescriptor**); -bool gettexdescriptor(struct TextureBatchDescriptor**, - unsigned int, - const char *, - unsigned int); - -bool gettexbatch(struct TextureBatch**, - unsigned int, - const char*, - unsigned int); - -bool unpacktexbatch(struct Texture*, - unsigned int, - const char*, - const unsigned int); - #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index d9d0db6..9305e6b 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -25,6 +25,76 @@ void printtbd(struct TextureBatchDescriptor *bd) { } } +bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, + unsigned int i, + const char *filedata, + unsigned int filesize) { + // starts after the pack header. + bool done = false; + unsigned int offset = sizeof(struct TexturePack); + offset += sizeof(struct TextureBatchDescriptor) * i; + if (filedata != NULL) { + *descriptor = (struct TextureBatchDescriptor*)(filedata + offset); + done = true; + } + return done; +} + +bool gettexbatch(struct TextureBatch **batch, + unsigned int i, + const char *filedata, + unsigned int filesize) { + struct TexturePack *p = NULL; + if (filedata == NULL) { // no data to get batch from. + return false; + } + p = (struct TexturePack*)filedata; + if (i > p -> batchNumber) { // no such batch in texture pack + return false; + } + if (i == 0) { + *batch = (struct TextureBatch*)(filedata + (p -> firstBatchOffset)); + return true; + } + struct TextureBatchDescriptor *d = NULL; + // find how large are previous batches in total + // in order to jump to specified texture batch + unsigned int pastbatchsize = p -> firstBatchOffset; + unsigned int j; + for (j = 0; j < i; j++) { + gettexdescriptor(&d, j, filedata, filesize); + pastbatchsize += (d -> textureSize * d -> texNumber); + } + *batch = (struct TextureBatch*)(filedata + pastbatchsize); + return true; +} + +bool unpacktexbatch(struct Texture *t, + unsigned int i, + const char *filedata, + const unsigned int filesize) { + if (filedata == NULL) { + return false; + } + struct TextureBatch *b = NULL; + struct TextureBatchDescriptor *d = NULL; + gettexbatch(&b, i, filedata, filesize); + gettexdescriptor(&d, i, filedata, filesize); + + // first texture is at the start of the batch + // second texture is right after. + unsigned int j; + for (j = 0; j < d -> texNumber; j++) { + t[j].data = (unsigned char*)b + (d -> textureSize * j); + } + return true; +} + +// -------------------------------------------------------+ +// Retired Functions +// -------------------------------------------------------+ + + // get texture pack descriptors void gettbd (struct TextureBatchDescriptor **descriptors, const char *filedata, @@ -95,79 +165,6 @@ void locatetextures(unsigned int *t, } } } -// -------------------------------------------------------+ -// Revision Functions -// -------------------------------------------------------+ - -// ** = 'pass by reference' of a pointer to struct -bool gettexdescriptor(struct TextureBatchDescriptor **descriptor, - unsigned int i, - const char *filedata, - unsigned int filesize) { - // starts after the pack header. - bool done = false; - unsigned int offset = sizeof(struct TexturePack); - offset += sizeof(struct TextureBatchDescriptor) * i; - if (filedata != NULL) { - *descriptor = (struct TextureBatchDescriptor*)(filedata + offset); - done = true; - } -// printf("%x %x", *descriptor, filedata); - return done; -} - -// ** = 'pass by reference' of a pointer to struct -bool gettexbatch(struct TextureBatch **batch, - unsigned int i, - const char *filedata, - unsigned int filesize) { - struct TexturePack *p = NULL; - if (filedata == NULL) { // no data to get batch from. - return false; - } - p = (struct TexturePack*)filedata; - if (i > p -> batchNumber) { // no such batch in texture pack - return false; - } - if (i == 0) { - *batch = (struct TextureBatch*)(filedata + (p -> firstBatchOffset)); - return true; - } - struct TextureBatchDescriptor *d = NULL; - // find how large are previous batches in total - // in order to jump to specified texture batch - unsigned int pastbatchsize = p -> firstBatchOffset; - unsigned int j; - for (j = 0; j < i; j++) { - gettexdescriptor(&d, j, filedata, filesize); - pastbatchsize += (d -> textureSize * d -> texNumber); - } - *batch = (struct TextureBatch*)(filedata + pastbatchsize); - //printf("batch offset: %x i: %d\n" , (p -> firstBatchOffset) + pastbatchsize, i); - return true; -} - -bool unpacktexbatch(struct Texture *t, - unsigned int i, - const char *filedata, - const unsigned int filesize) { - if (filedata == NULL) { - return false; - } - struct TextureBatch *b = NULL; - struct TextureBatchDescriptor *d = NULL; - gettexbatch(&b, i, filedata, filesize); - gettexdescriptor(&d, i, filedata, filesize); - - // first texture is at the start of the batch - // second texture is right after. - unsigned int j; - for (j = 0; j < d -> texNumber; j++) { - t[j].data = (unsigned char*)b + (d -> textureSize * j); - //printf("unpack relative batch offset %x i= %i t.data= %x\n" , ((d -> textureSize * j)), i, t[j].data); - } - return true; -} From 0dabc2568efe10ab69cfd3be35f6debbdf594ecf Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 01:22:52 -0700 Subject: [PATCH 15/16] Deleted scrapped functions. --- include/devil1tex.h | 19 ------------ src/devil1tex.c | 76 --------------------------------------------- test/main.c | 32 ------------------- 3 files changed, 127 deletions(-) diff --git a/include/devil1tex.h b/include/devil1tex.h index ffcc2b5..db19345 100644 --- a/include/devil1tex.h +++ b/include/devil1tex.h @@ -68,23 +68,4 @@ bool unpacktexbatch(struct Texture*, const char*, const unsigned int); -// -------------------------------------------------------+ -// Retired Functions -// These require the client allocate more on the heap. -// -------------------------------------------------------+ - -void gettbd(struct TextureBatchDescriptor**, - const char*, - unsigned int); - -unsigned int gettexturebatch( - struct TextureBatch**, - struct TextureBatchDescriptor**, - const char*, - unsigned int); - -void locatetextures(unsigned int*, - struct TexturePack*, - struct TextureBatchDescriptor**); - #endif diff --git a/src/devil1tex.c b/src/devil1tex.c index 9305e6b..567aff4 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -90,82 +90,6 @@ bool unpacktexbatch(struct Texture *t, return true; } -// -------------------------------------------------------+ -// Retired Functions -// -------------------------------------------------------+ - - -// get texture pack descriptors -void gettbd (struct TextureBatchDescriptor **descriptors, - const char *filedata, - unsigned int filesize) { - if (descriptors == NULL || filedata == NULL || filesize <= 0) { - return; - } - struct TexturePack *tp = NULL; - tp = (struct TexturePack*)filedata; - if (filesize <= (sizeof(struct TextureBatchDescriptor) - * tp -> batchNumber - + sizeof(struct TexturePack))){ - fprintf(stderr, - "File Error: file size to struct sizes ratio.\n"); - return; - } - unsigned int index = sizeof(struct TexturePack); - // Batch Descriptors are structured one after another. - // therefore the next one is at - // current index + sizeof(TextureBatchDescriptor) - unsigned int i = 0; - do { - index = sizeof(struct TextureBatchDescriptor) * i - + sizeof(struct TexturePack); - printf("batch descriptor start: %x\n", index); - descriptors[i] = (struct TextureBatchDescriptor*) - (filedata + index); - i++; - } while(i < (tp -> batchNumber)); - return; -} - -// returns total count of textures in the file. -unsigned int gettexturebatch( - struct TextureBatch **tb, - struct TextureBatchDescriptor **bds, - const char *filedata, - unsigned int filesize) { - struct TexturePack *tp = (struct TexturePack*)filedata; - unsigned int offset = tp -> firstBatchOffset; - unsigned int i; - unsigned int j; - unsigned int totaltextures = 0; - // TextureBatch shares a starting address with the first texture - // and shares the ending address with the last texture. - for (i = 0; i < tp -> batchNumber; i++) { - tb[i] = (struct TextureBatch*)filedata + offset; - // the next texture batch is TextureSize * number of textures away. - offset += bds[i] -> textureSize * (bds[i] -> texNumber); - totaltextures += bds[i] -> texNumber; - } - return totaltextures; -} - -void locatetextures(unsigned int *t, - struct TexturePack *tp, - struct TextureBatchDescriptor **bds) { - unsigned int i; - unsigned int j; - // k is access to index of textures. - unsigned int k = 0; - unsigned int offset = tp -> firstBatchOffset; - for (i = 0; i < tp -> batchNumber; i++) { - for (j = 0; j < bds[i] -> texNumber; j++) { - t[k] = offset; - offset += bds[i] -> textureSize; - k++; - } - } -} - diff --git a/test/main.c b/test/main.c index 6aa2cc0..25e8c21 100644 --- a/test/main.c +++ b/test/main.c @@ -107,38 +107,6 @@ void write(const char *filename, } } -void unpacktextures(const char *filedata, - unsigned int filesize, - const char *filename) { - struct TexturePack *tp = (struct TexturePack*)filedata; - struct TextureBatch **batches = NULL; - struct TextureBatchDescriptor **descripts = NULL; - batches = (struct TextureBatch**)malloc(tp -> batchNumber); - descripts = (struct TextureBatchDescriptor**)malloc(tp -> batchNumber); - gettbd(descripts, filedata, filesize); - // array of pointers - texture data exists in memory in filedata. - unsigned int texturecount = gettexturebatch(batches, - descripts, - filedata, - filesize); - unsigned int *tloc = (unsigned int*) - malloc(sizeof(unsigned int) * texturecount); - locatetextures(tloc, tp, descripts); - // add 3 to buffer for digits, & 4 for file extension - char *fmt = (char*)malloc(strlen(filename) + 3 + 4); - unsigned int texsize = 0; - unsigned int i; - for (i = 0; i < texturecount; i++) { - texsize = descripts[i/(tp -> batchNumber)] -> textureSize; - sprintf(fmt, "%s_%d.dds", filename, i); - write(fmt, filedata + tloc[i], texsize); - } - free(fmt); - free(tloc); - free(descripts); - free(batches); -} - void exporttextures(const char *filedata, unsigned int filesize, const char *filename) { From f257d1be4b1a6a623f8c7119d35193dda5083017 Mon Sep 17 00:00:00 2001 From: _ <_> Date: Fri, 6 Apr 2018 01:31:21 -0700 Subject: [PATCH 16/16] Removed stdlib.h and string.h as includes --- src/devil1tex.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/devil1tex.c b/src/devil1tex.c index 567aff4..486f068 100644 --- a/src/devil1tex.c +++ b/src/devil1tex.c @@ -1,7 +1,5 @@ #include "devil1tex.h" #include -#include -#include #include void printtph (struct TexturePack *tp) {