Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "timageinfo.h"
Toshihiro Shimizu 890ddd
#include "tmeshimage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tiio_mesh.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TODO: Eccezioni. Vedi come funziona in toonzscene.cpp
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TImageWriterMesh  definition
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TImageWriterMesh final : public TImageWriter {
Shinya Kitaoka 120a6e
  TFrameId m_fid;  //!< The frame id
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TImageWriterMesh(const TFilePath &fp, const TFrameId &frameId);
Shinya Kitaoka 120a6e
  ~TImageWriterMesh() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 473e70
  void save(const TImageP &) override;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  TImageWriterMesh(const TImageWriterMesh &);
Shinya Kitaoka 120a6e
  TImageWriterMesh &operator=(const TImageWriterMesh &src);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageWriterMesh::TImageWriterMesh(const TFilePath &fp, const TFrameId &frameId)
Shinya Kitaoka 120a6e
    : TImageWriter(fp), m_fid(frameId) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageWriterMesh::save(const TImageP &img) {
Shinya Kitaoka 120a6e
  TFilePath imagePath(this->m_path.withFrame(m_fid));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TOStream ostream(imagePath, true);  // Use compression
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TMeshImageP mi = img;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Save Header
Shinya Kitaoka 120a6e
  ostream.openChild("header");
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    // Save version
Shinya Kitaoka 120a6e
    ostream.openChild("version");
Shinya Kitaoka 120a6e
    ostream << 1 << 19;
Shinya Kitaoka 120a6e
    ostream.closeChild();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Save dpi
Shinya Kitaoka 120a6e
    ostream.openChild("dpi");
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    double dpiX, dpiY;
Shinya Kitaoka 120a6e
    mi->getDpi(dpiX, dpiY);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    ostream << dpiX << dpiY;
Shinya Kitaoka 120a6e
    ostream.closeChild();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ostream.closeChild();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Save meshes
Shinya Kitaoka 120a6e
  const std::vector<ttexturemeshp> &meshes = mi->meshes();</ttexturemeshp>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int m, mCount = meshes.size();
Shinya Kitaoka 120a6e
  for (m = 0; m < mCount; ++m) {
Shinya Kitaoka 120a6e
    ostream.openChild("mesh");
Shinya Kitaoka 120a6e
    ostream << *meshes[m];
Shinya Kitaoka 120a6e
    ostream.closeChild();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TImageReaderMesh  definition
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TImageReaderMesh final : public TImageReader {
Shinya Kitaoka 120a6e
  TFrameId m_fid;             //
Shinya Kitaoka 120a6e
  mutable TImageInfo m_info;  //!< The image's infos
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TImageReaderMesh(const TFilePath &fp, const TFrameId &frameId);
Shinya Kitaoka 120a6e
  ~TImageReaderMesh() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  const TImageInfo *getImageInfo() const override;
Shinya Kitaoka 473e70
  TImageP load() override;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  //! Reference to level reader
Shinya Kitaoka 120a6e
  TLevelReaderMesh *m_lrp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  void readHeader(TIStream &is) const;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // not implemented
Shinya Kitaoka 120a6e
  TImageReaderMesh(const TImageReaderMesh &);
Shinya Kitaoka 120a6e
  TImageReaderMesh &operator=(const TImageReaderMesh &src);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageReaderMesh::TImageReaderMesh(const TFilePath &fp, const TFrameId &frameId)
Shinya Kitaoka 120a6e
    : TImageReader(fp), m_fid(frameId) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const TImageInfo *TImageReaderMesh::getImageInfo() const {
Shinya Kitaoka 120a6e
  if (!m_info.m_valid) {
Shinya Kitaoka 120a6e
    // Load info from file
Shinya Kitaoka 120a6e
    TFilePath imagePath(this->m_path.withFrame(m_fid));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    TIStream is(imagePath);
Shinya Kitaoka 120a6e
    readHeader(is);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return &m_info;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TImageReaderMesh::readHeader(TIStream &is) const {
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Open header tag
Shinya Kitaoka 120a6e
  is.openChild(tagName);
Shinya Kitaoka 120a6e
  assert(tagName == "header");
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    // Read header entries
Shinya Kitaoka 120a6e
    while (is.openChild(tagName)) {
Shinya Kitaoka 120a6e
      if (tagName == "version") {
Shinya Kitaoka 120a6e
        int major, minor;
Shinya Kitaoka 120a6e
        is >> major >> minor;
Shinya Kitaoka 120a6e
        is.setVersion(VersionNumber(major, minor));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        is.closeChild();
Shinya Kitaoka 120a6e
      } else if (tagName == "dpi") {
Shinya Kitaoka 120a6e
        is >> m_info.m_dpix >> m_info.m_dpiy;
Shinya Kitaoka 120a6e
        assert(m_info.m_dpix > 0.0 && m_info.m_dpiy > 0.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        is.closeChild();
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        is.skipCurrentTag();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  is.closeChild();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_info.m_valid = true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageP TImageReaderMesh::load() {
Shinya Kitaoka 120a6e
  TMeshImageP mi(new TMeshImage);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TFilePath imagePath(this->m_path.withFrame(m_fid));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TIStream is(imagePath);
Shinya Kitaoka 120a6e
  readHeader(is);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  mi->setDpi(m_info.m_dpix, m_info.m_dpiy);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Meshes
Shinya Kitaoka 120a6e
  std::vector<ttexturemeshp> &meshes = mi->meshes();</ttexturemeshp>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
  while (is.openChild(tagName)) {
Shinya Kitaoka 120a6e
    if (tagName == "mesh") {
Shinya Kitaoka 120a6e
      meshes.push_back(new TTextureMesh);
Shinya Kitaoka 120a6e
      is >> *meshes.back();
Shinya Kitaoka 120a6e
      is.closeChild();
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      is.skipCurrentTag();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return mi;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TLevelWriterMesh  implementation
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelWriterMesh::TLevelWriterMesh(const TFilePath &path, TPropertyGroup *winfo)
Shinya Kitaoka 120a6e
    : TLevelWriter(path, winfo) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TLevelWriterMesh::~TLevelWriterMesh() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageWriterP TLevelWriterMesh::getFrameWriter(TFrameId fid) {
Shinya Kitaoka 120a6e
  return new TImageWriterMesh(this->m_path, fid);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    TLevelReaderMesh  implementation
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TLevelReaderMesh::TLevelReaderMesh(const TFilePath &path)
Shinya Kitaoka 120a6e
    : TLevelReader(path) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TLevelReaderMesh::~TLevelReaderMesh() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TImageReaderP TLevelReaderMesh::getFrameReader(TFrameId fid) {
Shinya Kitaoka 120a6e
  return new TImageReaderMesh(this->m_path, fid);
Toshihiro Shimizu 890ddd
}