Forráskód Böngészése

Add a pandatool for converting various image formats to the TXO format. (#1740)

Drew Rogers 2 hete
szülő
commit
7ef2f0616f

+ 13 - 0
makepanda/makepanda.py

@@ -5284,6 +5284,19 @@ if not PkgSkip("PANDATOOL"):
         TargetAdd('egg2bam.exe', input=COMMON_EGG2X_LIBS)
         TargetAdd('egg2bam.exe', opts=['ADVAPI', 'FFTW'])
 
+#
+# DIRECTORY: pandatool/src/converter/
+#
+
+if not PkgSkip("PANDATOOL"):
+    OPTS=['DIR:pandatool/src/converter']
+    TargetAdd('txo-converter_txoConverter.obj', opts=OPTS, input='txoConverter.cxx')
+    TargetAdd('txo-converter.exe', input='txo-converter_txoConverter.obj')
+    TargetAdd('txo-converter.exe', input='libp3progbase.lib')
+    TargetAdd('txo-converter.exe', input='libp3pandatoolbase.lib')
+    TargetAdd('txo-converter.exe', input=COMMON_PANDA_LIBS)
+    TargetAdd('txo-converter.exe', opts=['ADVAPI', 'FFTW'])
+
 #
 # DIRECTORY: pandatool/src/daeegg/
 #

+ 4 - 0
pandatool/src/converter/CMakeLists.txt

@@ -21,3 +21,7 @@ install(TARGETS p3converter
   INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/panda3d
   ARCHIVE COMPONENT ToolsDevel)
 install(FILES ${P3CONVERTER_HEADERS} COMPONENT ToolsDevel DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/panda3d)
+
+add_executable(txo-converter txoConverter.cxx txoConverter.h)
+target_link_libraries(txo-converter p3progbase panda)
+install(TARGETS txo-converter EXPORT Tools COMPONENT Tools DESTINATION ${CMAKE_INSTALL_BINDIR})

+ 148 - 0
pandatool/src/converter/txoConverter.cxx

@@ -0,0 +1,148 @@
+/**
+ * PANDA 3D SOFTWARE
+ * Copyright (c) Carnegie Mellon University.  All rights reserved.
+ *
+ * All use of this software is subject to the terms of the revised BSD
+ * license.  You should have received a copy of this license along
+ * with this source code in a file named "LICENSE."
+ *
+ * @file txoConverter.cxx
+ * @author RegDogg
+ * @date 2025-11-10
+ */
+
+#include "txoConverter.h"
+#include "pnmFileTypeRegistry.h"
+#include "pnmFileType.h"
+
+/**
+ *
+ */
+TxoConverter::
+TxoConverter() : WithOutputFile(true, false, true) {
+  set_program_brief("convert various image formats to .txo file format");
+  set_program_description
+    ("This program reads the image date from the input file and "
+     "outputs a txo files, suitable for viewing in Panda.");
+
+  clear_runlines();
+  add_runline("[opts] input output");
+  add_runline("[opts] -o output input");
+
+  add_option
+    ("o", "filename", 0,
+     "Specify the filename to which the resulting .bam file will be written.  "
+     "If this option is omitted, the last parameter name is taken to be the "
+     "name of the output file.",
+     &TxoConverter::dispatch_filename, &_got_output_filename, &_output_filename);
+
+  add_option
+    ("alpha", "filename", 0,
+     "Apply an RGB alpha file for image types such as JPEG.",
+     &TxoConverter::dispatch_filename, &_got_rgb_filename, &_rgb_filename);
+}
+
+/**
+ *
+ */
+void TxoConverter::
+run() {
+  nassertv(has_output_filename());
+  Filename fullpath = Filename(_image_filename.get_fullpath());
+
+  nout << "Reading " << fullpath << "...\n";
+
+  PNMFileType *type = PNMFileTypeRegistry::get_global_ptr()->get_type_from_extension(fullpath);
+  if (type == nullptr) {
+    nout << "Cannot determine type of image file " << fullpath << ".\n";
+    nout << "Currently supported image types:\n";
+    PNMFileTypeRegistry::get_global_ptr()->write(nout, 2);
+    nout << "\n";
+    return;
+  }
+
+  PT(Texture) tex = new Texture("original image");
+
+  if (_got_rgb_filename) {
+    PNMFileType *type = PNMFileTypeRegistry::get_global_ptr()->get_type_from_extension(_rgb_filename);
+    if (type == nullptr) {
+      nout << "Image file type '" << _rgb_filename << "' is unknown.\n";
+      return;
+    }
+    tex->read(_image_filename, _rgb_filename.get_fullpath(), 0, 0, LoaderOptions());
+  }
+  else {
+    tex->read(_image_filename, LoaderOptions());
+  }
+  tex->get_ram_image();
+
+  convert_txo(tex);
+
+}
+
+
+/**
+ * If the indicated Texture was not already loaded from a txo file, writes it
+ * to a txo file and updates the Texture object to reference the new file.
+ */
+void TxoConverter::
+convert_txo(Texture *tex) {
+  if (!tex->get_loaded_from_txo()) {
+
+    Filename output = get_output_filename();
+    output.make_dir();
+
+    if (tex->write(output)) {
+      nout << "Writing " << output << "...\n";
+      tex->set_loaded_from_txo();
+      tex->set_fullpath(output);
+      tex->clear_alpha_fullpath();
+
+      tex->set_filename(output);
+      tex->clear_alpha_filename();
+    }
+  }
+}
+
+/**
+ *
+ */
+bool TxoConverter::
+handle_args(ProgramBase::Args &args) {
+  if (!_got_output_filename && args.size() > 1) {
+    _got_output_filename = true;
+    _output_filename = Filename::from_os_specific(args.back());
+    args.pop_back();
+  }
+
+  if (!_got_output_filename) {
+    nout << "You must specify an output path.";
+    return false;
+  }
+
+  if ((_output_filename.get_extension() != "txo")) {
+    nout << "Output filename " << _output_filename << " must end in .txo\n";
+    return false;
+  }
+
+  if (args.empty()) {
+    nout << "You must specify the image file to read on the command line.\n";
+    return false;
+  }
+
+  if (args.size() != 1) {
+    nout << "Specify only one image on the command line.\n";
+    return false;
+  }
+
+  _image_filename = Filename::from_os_specific(args[0]);
+
+  return true;
+}
+
+int main(int argc, char *argv[]) {
+  TxoConverter prog;
+  prog.parse_command_line(argc, argv);
+  prog.run();
+  return 0;
+}

+ 43 - 0
pandatool/src/converter/txoConverter.h

@@ -0,0 +1,43 @@
+/**
+ * PANDA 3D SOFTWARE
+ * Copyright (c) Carnegie Mellon University.  All rights reserved.
+ *
+ * All use of this software is subject to the terms of the revised BSD
+ * license.  You should have received a copy of this license along
+ * with this source code in a file named "LICENSE."
+ *
+ * @file txoConverter.h
+ * @author RegDogg
+ * @date 2025-11-10
+ */
+
+#ifndef TXOCONVERTER_H
+#define TXOCONVERTER_H
+
+#include "pandatoolbase.h"
+#include "programBase.h"
+#include "filename.h"
+#include "withOutputFile.h"
+#include "textureAttrib.h"
+
+/**
+ *
+ */
+class TxoConverter : public ProgramBase, public WithOutputFile {
+public:
+  TxoConverter();
+    
+  void run();
+
+  protected:
+  virtual bool handle_args(Args &args);
+
+  private:
+  void convert_txo(Texture *tex);
+
+  Filename _image_filename;
+  bool _got_rgb_filename;
+  Filename _rgb_filename;
+};
+
+#endif