コード例 #1
0
ファイル: ImageExtractor.cpp プロジェクト: 3dcgarts/assimp
// -----------------------------------------------------------------------------------
// Implementation of the assimp extract utility
int Assimp_Extract (const char* const* params, unsigned int num)
{
	const char* const invalid = "assimp extract: Invalid number of arguments. See \'assimp extract --help\'\n"; 
	if (num < 1) {
		printf(invalid);
		return 1;
	}

	// --help
	if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
		printf("%s",AICMD_MSG_DUMP_HELP_E);
		return 0;
	}

	// asssimp extract in out [options]
	if (num < 1) {
		printf(invalid);
		return 1;
	}

	std::string in  = std::string(params[0]);
	std::string out = (num > 1 ? std::string(params[1]) : "-");

	// get import flags
	ImportData import;
	ProcessStandardArguments(import,params+1,num-1);

	bool nosuffix = false;
	unsigned int texIdx  = 0xffffffff, flags = 0;

	// process other flags
	std::string extension = "bmp";
	for (unsigned int i = (out[0] == '-' ? 1 : 2); i < num;++i)		{
		if (!params[i]) {
			continue;
		}

		if (!strncmp( params[i], "-f",2)) {
			extension = std::string(params[i]+2);
		}
		else if ( !strncmp( params[i], "--format=",9)) {
			extension = std::string(params[i]+9);
		}
		else if ( !strcmp( params[i], "--nosuffix") || !strcmp(params[i],"-s")) {
			nosuffix = true;
		}
		else if ( !strncmp( params[i], "--texture=",10)) {
			texIdx = Assimp::strtoul10(params[i]+10);
		}
		else if ( !strncmp( params[i], "-t",2)) {
			texIdx = Assimp::strtoul10(params[i]+2);
		}
		else if ( !strcmp( params[i], "-ba") ||  !strcmp( params[i], "--bmp-with-alpha")) {
			flags |= AI_EXTRACT_WRITE_BMP_ALPHA;
		}
#if 0
		else {
			printf("Unknown parameter: %s\n",params[i]);
			return 10;
		}
#endif
	}

	std::transform(extension.begin(),extension.end(),extension.begin(),::tolower);
	
	if (out[0] == '-') {
		// take file name from input file
		std::string::size_type s = in.find_last_of('.');
		if (s == std::string::npos)
			s = in.length();

		out = in.substr(0,s);
	}

	// take file extension from file name, if given
	std::string::size_type s = out.find_last_of('.');
	if (s != std::string::npos) {
		extension = out.substr(s+1,in.length()-(s+1));
		out = out.substr(0,s);
	}

	// import the main model
	const aiScene* scene = ImportModel(import,in);
	if (!scene) {
		printf("assimp extract: Unable to load input file %s\n",in.c_str());
		return 5;
	}

	// get the texture(s) to be exported
	if (texIdx != 0xffffffff) {

		// check whether the requested texture is existing
		if (texIdx >= scene->mNumTextures) {
			::printf("assimp extract: Texture %i requested, but there are just %i textures\n",
				texIdx, scene->mNumTextures);
			return 6;
		}
	}
	else {
		::printf("assimp extract: Exporting %i textures\n",scene->mNumTextures);
	}

	// now write all output textures
	for (unsigned int i = 0; i < scene->mNumTextures;++i)	{
		if (texIdx != 0xffffffff && texIdx != i) {
			continue;
		}

		const aiTexture* tex = scene->mTextures[i];
		std::string out_cpy = out, out_ext = extension;

		// append suffix if necessary - always if all textures are exported
		if (!nosuffix || (texIdx == 0xffffffff)) {
			out_cpy.append  ("_img");
			char tmp[10];
			Assimp::ASSIMP_itoa10(tmp,i);

			out_cpy.append(std::string(tmp));
		}

		// if the texture is a compressed one, we'll export
		// it to its native file format
		if (!tex->mHeight) {
			printf("assimp extract: Texture %i is compressed (%s). Writing native file format.\n",
				i,tex->achFormatHint);

			// modify file extension
			out_ext = std::string(tex->achFormatHint);
		}
		out_cpy.append("."+out_ext);

		// open output file
		FILE* p = ::fopen(out_cpy.c_str(),"wb");
		if (!p)  {
			printf("assimp extract: Unable to open output file %s\n",out_cpy.c_str());
			return 7;
		}
		int m;

		if (!tex->mHeight) {
			m = (1 != fwrite(tex->pcData,tex->mWidth,1,p));
		}
		else m = DoExport(tex,p,extension,flags);
		::fclose(p);

		printf("assimp extract: Wrote texture %i to %s\n",i, out_cpy.c_str());
		if (texIdx != 0xffffffff)
			return m;
	}
	return 0;
}
コード例 #2
0
ファイル: WriteDumb.cpp プロジェクト: tonttu/assimp
// -----------------------------------------------------------------------------------
int Assimp_Dump (const char* const* params, unsigned int num)
{
	const char* fail = "assimp dump: Invalid number of arguments. "
			"See \'assimp dump --help\'\r\n";
	if (num < 1) {
		printf(fail);
		return 1;
	}

	// --help
	if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
		printf("%s",AICMD_MSG_DUMP_HELP);
		return 0;
	}

	// asssimp dump in out [options]
	if (num < 1) {
		printf(fail);
		return 1;
	}

	std::string in  = std::string(params[0]);
	std::string out = (num > 1 ? std::string(params[1]) : std::string("-"));

	// store full command line
	std::string cmd;
	for (unsigned int i = (out[0] == '-' ? 1 : 2); i < num;++i)	{
		if (!params[i])continue;
		cmd.append(params[i]);
		cmd.append(" ");
	}

	// get import flags
	ImportData import;
	ProcessStandardArguments(import,params+1,num-1);

	bool binary = false, shortened = false,compressed=false;
	
	// process other flags
	for (unsigned int i = 1; i < num;++i)		{
		if (!params[i])continue;
		if (!strcmp( params[i], "-b") || !strcmp( params[i], "--binary")) {
			binary = true;
		}
		else if (!strcmp( params[i], "-s") || !strcmp( params[i], "--short")) {
			shortened = true;
		}
		else if (!strcmp( params[i], "-z") || !strcmp( params[i], "--compressed")) {
			compressed = true;
		}
#if 0
		else if (i > 2 || params[i][0] == '-') {
			::printf("Unknown parameter: %s\n",params[i]);
			return 10;
		}
#endif
	}

	if (out[0] == '-') {
		// take file name from input file
		std::string::size_type s = in.find_last_of('.');
		if (s == std::string::npos) {
			s = in.length();
		}

		out = in.substr(0,s);
		out.append((binary ? ".assbin" : ".assxml"));
		if (shortened && binary) {
			out.append(".regress");
		}
	}

	// import the main model
	const aiScene* scene = ImportModel(import,in);
	if (!scene) {
		printf("assimp dump: Unable to load input file %s\n",in.c_str());
		return 5;
	}

	// open the output file and build the dump
	FILE* o = ::fopen(out.c_str(),(binary ? "wb" : "wt"));
	if (!o) {
		printf("assimp dump: Unable to open output file %s\n",out.c_str());
		return 12;
	}

	if (binary) {
		WriteBinaryDump (scene,o,in.c_str(),cmd.c_str(),shortened,compressed,import);
	}
	else WriteDump (scene,o,in.c_str(),cmd.c_str(),shortened);
	fclose(o);

	if (compressed && binary) {
		CompressBinaryDump(out.c_str(),ASSBIN_HEADER_LENGTH);
	}

	printf("assimp dump: Wrote output dump %s\n",out.c_str());
	return 0;
}