summaryrefslogtreecommitdiff
path: root/gen_mipmaps_man.c
blob: f29aed0160a7d23434c25855b5a775d5f2605f36 (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
#define KTX_OPENGL 1
#include <ktx.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

#include <waffle-1/waffle.h>

int
no_dot_output_filter (const struct dirent * dir_entity)
{
	return strlen(dir_entity->d_name) > 2 &&
		strstr(dir_entity->d_name, "simple.ktx") == NULL &&
		strstr(dir_entity->d_name, "swp") == NULL;
}

void make_context()
{
	/* Generate a context */
	struct waffle_display *dpy;
	struct waffle_config *config;
	struct waffle_window *window;
	struct waffle_context *ctx;

	const int32_t init_attrs[] = {
	WAFFLE_PLATFORM, WAFFLE_PLATFORM_X11_EGL,
	0,
	};

	const int32_t config_attrs[] = {
	WAFFLE_CONTEXT_API,         WAFFLE_CONTEXT_OPENGL_ES2,
	WAFFLE_RED_SIZE,            8,
	WAFFLE_BLUE_SIZE,           8,
	WAFFLE_GREEN_SIZE,          8,

	0,
	};

	const int32_t window_width = 320;
	const int32_t window_height = 240;

	waffle_init(init_attrs);
	dpy = waffle_display_connect(NULL);

	// Exit if OpenGL ES2 is unsupported.
	if (!waffle_display_supports_context_api(dpy, WAFFLE_CONTEXT_OPENGL_ES2)
		|| !waffle_dl_can_open(WAFFLE_DL_OPENGL_ES2))
	{
		exit(EXIT_FAILURE);
	}

	config = waffle_config_choose(dpy, config_attrs);
	window = waffle_window_create(config, window_width, window_height);
	ctx = waffle_context_create(config, NULL);
	waffle_make_current(dpy, window, ctx);
}

/* Accepts the mipmap dir, the output filename */
int
main(int argc, char *argv[])
{
	/* Get arguments */
	char * output_dir = argv[1];
	char * filename = argv[2];

	/* Create GLES2 context */
	make_context();

	/* List directories (adapted from scandir manpage) */
	struct dirent **namelist;
	int levels = scandir(output_dir, &namelist, no_dot_output_filter, alphasort);
        if (levels < 0)
            perror("scandir");

	/* Structs used to write the final output */
	KTX_texture_info tex_info;
	KTX_image_info *img_info = (KTX_image_info*)malloc(levels * sizeof(KTX_image_info));

	int cur_lev;
	char cur_path[4096];

	/* Write a file from Memory */
	/* Write memory object to file */
	strcpy(cur_path, output_dir);
	strcat(cur_path, filename);
	FILE* fileo = fopen(cur_path, "wb");
	if (fileo == NULL)
		printf("File not opened\n");



        for (cur_lev = 0; cur_lev < levels; ++cur_lev) {

		/* Create full file path */
		char * cur_file = namelist[cur_lev]->d_name;
		strcpy(cur_path, output_dir);
		strcat(cur_path, cur_file);
		//printf("%s\n", cur_path);

		/* Read in the file from memory */
		FILE* file = fopen(cur_path, "rb");
		if (file == NULL)
			printf("File not opened\n");


		/* Read the identifier and endianness */
		uint32_t header_top[4];
		size_t size_read = fread(header_top, 1, 16, file);
		if (size_read < 16)
			printf("bad read 0\n");

		/* Read the appropriate header bits */
		KTX_texture_info ltex_info;
		size_read = fread(&ltex_info, 1, sizeof(ltex_info), file);
		if (size_read < sizeof(ltex_info))
			printf("bad read 1\n");

		/* Skip the bytes of Key Value Data */
		int error = fseek(file, 4, SEEK_CUR);
		if (error)
			printf("File seek bad\n");

		/* Get the image size */
		GLsizei *lsize = &img_info[cur_lev].size;
		GLubyte **ldata = &img_info[cur_lev].data;
		size_read = fread(lsize, 1, 4, file);
		if (size_read < 4)
			printf("bad read 2\n");

		/* Get the image data */
		*ldata = (GLubyte*) malloc(*lsize);
		size_read = fread(*ldata, 1, *lsize, file);
		if (size_read < *lsize)
			printf("bad read 2\n");

		/* Clean up */
		free(namelist[cur_lev]);

		/* Close the file */
		if (file != NULL)
			fclose(file);

		/* Fill out final struct */
		if (cur_lev == 0) {
			memcpy(&tex_info, &ltex_info, sizeof(ltex_info));

			/* Update the mipmap section */
			tex_info.numberOfMipmapLevels = levels;

			/* Update the final file with this data */
			uint32_t nokvdat = 0;
			fwrite(header_top, 16, 1, fileo);
			fwrite(&tex_info, sizeof(tex_info), 1, fileo);
			fwrite(&nokvdat, sizeof(nokvdat), 1, fileo);
		}

		/* Update the final file with this data */
		fwrite(lsize, sizeof(GLubyte), 1, fileo);
		fwrite(*ldata, *lsize, 1, fileo);
		
		/* mip padding */
		error = fseek(fileo, 3-((*lsize+3) % 4), SEEK_CUR);
		if (error)
			printf("File seek bad\n");

        }

	///KTX_error_code kec;
	///kec = ktxWriteKTXN(cur_path, &tex_info, 0, NULL, levels, img_info);
	///printf("%s - %s\n", cur_path, ktxErrorString(kec));


	/* Close the file */
	if (fileo != NULL)
		fclose(fileo);

	/* Cleanup */
        free(namelist);
        for (cur_lev = 0; cur_lev < levels; ++cur_lev)
		free(img_info[cur_lev].data);
        free(img_info);
	return 0;
}