int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QCoreApplication::setApplicationName("Vincent Tim"); QCoreApplication::setApplicationVersion("1.2"); #ifdef Q_OS_WIN QTextCodec::setCodecForLocale(QTextCodec::codecForName("IBM 850")); #endif #ifdef TESTS_ENABLED qDebug() << "Running tests..."; Collect c("tests/tim/files"); c.textureData("tim"); #endif Arguments args; if (args.help() || args.paths().isEmpty()) { args.showHelp(); } else { foreach (const QString &path, args.paths()) { TextureFile *texture; if (args.inputFormat(path) == args.outputFormat()) { qWarning() << "Error: input and output formats are not different"; a.exit(1); } QFile f(path); if (f.open(QIODevice::ReadOnly)) { if (!args.analysis()) { texture = TextureFile::factory(args.inputFormat(path)); if (texture->open(f.readAll())) { if (TextureFile::supportedTextureFormats().contains(args.outputFormat(), Qt::CaseInsensitive)) { if (!toTexture(texture, path, args)) { break; } } else if (TextureFile::supportedTextureFormats().contains(args.inputFormat(path), Qt::CaseInsensitive)) { fromTexture(texture, path, args); } else { qWarning() << "Error: input format or output format must be a supported texture format" << TextureFile::supportedTextureFormats(); a.exit(1); } } else { qWarning() << "Error: Cannot open Texture file"; a.exit(1); } f.close(); delete texture; } else { // Search tim files QList<PosSize> positions = TimFile::findTims(&f); int num = 0; foreach (const PosSize &pos, positions) { texture = new TimFile(); f.seek(pos.first); if (texture->open(f.read(pos.second))) { if (args.outputFormat().compare("tim", Qt::CaseInsensitive) == 0) { if (!texture->saveToFile(args.destination(path, num))) { qWarning() << "Error: Cannot save Texture file from" << QDir::toNativeSeparators(path) << "to" << args.destination(path, num); continue; } else { printf("%s\n", qPrintable(QDir::toNativeSeparators(args.destination(path, num)))); } } else { fromTexture(texture, path, args, num); } num++; } else { qWarning() << "Error: Cannot open Texture file from" << QDir::toNativeSeparators(path); a.exit(1); } delete texture; } } } else { qWarning() << "Error: cannot open file" << QDir::toNativeSeparators(path) << f.errorString(); a.exit(1); } }
void fromTexture(TextureFile *texture, const QString &path, const Arguments &args, int num = -1) { QString destPathTexture; bool error = false; if (args.palette() < 0 || args.palette() >= texture->colorTableCount()) { if (texture->colorTableCount() <= 0) { destPathTexture = args.destination(path, num); if (!saveTextureTo(texture, destPathTexture)) { error = true; } } for (int paletteID=0; paletteID<texture->colorTableCount(); ++paletteID) { texture->setCurrentColorTable(paletteID); destPathTexture = args.destination(path, num, paletteID); if (!saveTextureTo(texture, destPathTexture)) { error = true; } } } else { destPathTexture = args.destination(path, num); if (!saveTextureTo(texture, destPathTexture)) { error = true; } } if (!error) { if (args.exportMeta()) { ExtraData meta = texture->extraData(); if (!meta.fields().isEmpty()) { QString destPathMeta = args.destinationMeta(path, num); if (!meta.save(destPathMeta)) { qWarning() << "Error: Cannot save extra data"; return; } else { printf("%s\n", qPrintable(QDir::toNativeSeparators(destPathMeta))); } } } if (args.exportPalettes() && texture->depth() < 16) { // Do not use isPaletted for that! QImage palette = texture->palette(); if (!palette.isNull()) { QString destPathPalette = args.destinationPalette(path, num); if (!palette.save(destPathPalette)) { qWarning() << "Error: Cannot save palette"; return; } printf("%s\n", qPrintable(QDir::toNativeSeparators(destPathPalette))); } else { qWarning() << "Warning: No palette to export"; return; } } } if (error) { qWarning() << "Error: Cannot save image"; } }
bool toTexture(TextureFile *texture, const QString &path, const Arguments &args, int num = -1) { QString pathMeta = args.inputPathMeta(path), pathPalette = args.inputPathPalette(path); if (pathMeta.isEmpty()) { qWarning() << "Error: Please set the input path meta"; return false; } TextureFile *tex; QString destPath; if (args.outputFormat().compare("tex", Qt::CaseInsensitive) == 0) { tex = new TexFile(*texture); } else if (args.outputFormat().compare("tim", Qt::CaseInsensitive) == 0) { tex = new TimFile(*texture); } else { qWarning() << "toTexture: output format not supported"; return false; } ExtraData meta; if (!meta.open(pathMeta)) { qWarning() << "Meta data not found!" << QDir::toNativeSeparators(pathMeta); goto toTextureError; } tex->setExtraData(meta); // Not texture to texture if (args.outputFormat().compare(args.inputFormat(path), Qt::CaseInsensitive) != 0 && tex->depth() < 16) { // Do not use isPaletted for that! if (pathPalette.isEmpty()) { qWarning() << "Error: Please set the input path palette"; goto toTextureError; } QImage paletteImage; if (paletteImage.load(pathPalette)) { if (!tex->setPalette(paletteImage)) { qWarning() << "Error: Please set the depth in the meta file"; goto toTextureError; } if (args.palette() < 0 || args.palette() >= tex->colorTableCount()) { qWarning() << "Error: Please set a valid number of palette"; goto toTextureError; } tex->convertToIndexedFormat(args.palette()); } else { qWarning() << "Error: Cannot open the input palette"; goto toTextureError; } } destPath = args.destination(path, num); if (!tex->saveToFile(destPath)) { goto toTextureError; } printf("%s\n", qPrintable(QDir::toNativeSeparators(destPath))); delete tex; return true; toTextureError: delete tex; return false; }