Ejemplo n.º 1
0
int main(int argc, char** argv)
{
	unsigned char dx = 0;
	unsigned char dy = 0;

	const char* arg_format = NULL;
	const char* arg_src    = NULL;
	const char* arg_dst    = NULL;

	if(argc == 6)
	{
		dx = (unsigned char) strtoul(argv[2], (char**) NULL, 10);
		dy = (unsigned char) strtoul(argv[3], (char**) NULL, 10);

		arg_format = argv[1];
		arg_src    = argv[4];
		arg_dst    = argv[5];
	}
	else if(argc == 4)
	{
		arg_format = argv[1];
		arg_src    = argv[2];
		arg_dst    = argv[3];

		if(check_ext(arg_src, "mgm"))
		{
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}
	else
	{
		usage(argv[0]);
		return EXIT_FAILURE;
	}

	// parse format
	int type;
	int format;
	if(strcmp(arg_format, "RGBA-8888") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_RGBA;
	}
	else if(strcmp(arg_format, "BGRA-8888") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_BGRA;
	}
	else if(strcmp(arg_format, "RGB-565") == 0)
	{
		type  = TEXGZ_UNSIGNED_SHORT_5_6_5;
		format = TEXGZ_RGB;
	}
	else if(strcmp(arg_format, "RGBA-4444") == 0)
	{
		type  = TEXGZ_UNSIGNED_SHORT_4_4_4_4;
		format = TEXGZ_RGBA;
	}
	else if(strcmp(arg_format, "RGB-888") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_RGB;
	}
	else if(strcmp(arg_format, "RGBA-5551") == 0)
	{
		type  = TEXGZ_UNSIGNED_SHORT_5_5_5_1;
		format = TEXGZ_RGBA;
	}
	else if(strcmp(arg_format, "LUMINANCE") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_LUMINANCE;
	}
	else if(strcmp(arg_format, "LUMINANCE-F") == 0)
	{
		type  = TEXGZ_FLOAT;
		format = TEXGZ_LUMINANCE;
	}
	else
	{
		LOGE("invalid format=%s", arg_format);
		return EXIT_FAILURE;
	}

	// import src
	texgz_tex_t* tex = NULL;
	if(check_ext(arg_src, "texgz"))
	{
		tex = texgz_tex_import(arg_src);
	}
	else if(check_ext(arg_src, "jpg"))
	{
		tex = texgz_jpeg_import(arg_src);
	}
	else if(check_ext(arg_src, "png"))
	{
		tex = texgz_png_import(arg_src);
	}
	else if(check_ext(arg_src, "mgm"))
	{
		tex = texgz_mgm_import(arg_src, dx, dy);
	}
	else
	{
		LOGE("invalid src=%s", arg_src);
		return EXIT_FAILURE;
	}

	if(tex == NULL)
	{
		return EXIT_FAILURE;
	}

	// convert to format
	if(texgz_tex_convert(tex, type, format) == 0)
	{
		goto fail_convert;
	}

	// export dst
	int ret = 0;
	if(check_ext(arg_dst, "texgz"))
	{
		ret = texgz_tex_export(tex, arg_dst);
	}
	else if(check_ext(arg_dst, "jpg"))
	{
		ret = texgz_jpeg_export(tex, arg_dst);
	}
	else if(check_ext(arg_dst, "png"))
	{
		ret = texgz_png_export(tex, arg_dst);
	}
	else
	{
		LOGE("invalid dst=%s", arg_dst);
		goto fail_dst;
	}

	if(ret == 0)
	{
		goto fail_export;
	}

	texgz_tex_delete(&tex);

	// success
	return EXIT_SUCCESS;

	// failure
	fail_export:
	fail_dst:
	fail_convert:
		texgz_tex_delete(&tex);
	return EXIT_FAILURE;
}
Ejemplo n.º 2
0
a3d_spriteTex_t* a3d_spriteTex_new(const char* fname,
                                   const char* resource)
{
	assert(fname);
	assert(resource);
	LOGD("debug fname=%s, resource=%s", fname, resource);

	a3d_spriteTex_t* self = (a3d_spriteTex_t*) malloc(sizeof(a3d_spriteTex_t));
	if(self == NULL)
	{
		LOGE("malloc failed");
		return NULL;
	}

	pak_file_t*  pak = NULL;
	texgz_tex_t* tex = NULL;
	if(fname[0] == '$')
	{
		pak = pak_file_open(resource, PAK_FLAG_READ);
		if(pak)
		{
			const char* key = &(fname[1]);
			int size = pak_file_seek(pak, key);
			if(size > 0)
			{
				tex = texgz_tex_importf(pak->f, size);
			}
			else
			{
				LOGE("invalid fname=%s", fname);
			}
			pak_file_close(&pak);
		}
	}
	else
	{
		tex = texgz_tex_import(fname);
	}

	if(tex == NULL)
	{
		goto fail_tex;
	}

	// load tex
	glGenTextures(1, &self->id_tex);
	glBindTexture(GL_TEXTURE_2D, self->id_tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexImage2D(GL_TEXTURE_2D, 0, tex->format,
	             tex->stride, tex->vstride,
	             0, tex->format, tex->type,
	             tex->pixels);

	// no longer needed
	texgz_tex_delete(&tex);

	strncpy(self->fname, fname, 256);
	self->fname[255] = '\0';
	self->ref_count  = 0;

	// success
	return self;

	// failure
	fail_tex:
		free(self);
	return NULL;
}