turtletooth 04d8fd
turtletooth 04d8fd
#include "tsystem.h"
turtletooth 04d8fd
#include "tiio_gif.h"
turtletooth 04d8fd
#include "trasterimage.h"
turtletooth 04d8fd
#include "timageinfo.h"
Jeremy Bullock 2b9d8f
#include "toonz/stage.h"
turtletooth 04d8fd
#include <qstringlist></qstringlist>
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TImageWriterGif
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
class TImageWriterGif : public TImageWriter {
turtletooth 04d8fd
public:
turtletooth 04d8fd
  int m_frameIndex;
turtletooth 04d8fd
turtletooth 04d8fd
  TImageWriterGif(const TFilePath &path, int frameIndex, TLevelWriterGif *lwg)
turtletooth 04d8fd
      : TImageWriter(path), m_frameIndex(frameIndex), m_lwg(lwg) {
turtletooth 04d8fd
    m_lwg->addRef();
turtletooth 04d8fd
  }
turtletooth 04d8fd
  ~TImageWriterGif() { m_lwg->release(); }
turtletooth 04d8fd
turtletooth 04d8fd
  bool is64bitOutputSupported() override { return false; }
turtletooth 04d8fd
  void save(const TImageP &img) override { m_lwg->save(img, m_frameIndex); }
turtletooth 04d8fd
turtletooth 04d8fd
private:
turtletooth 04d8fd
  TLevelWriterGif *m_lwg;
turtletooth 04d8fd
};
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TLevelWriterGif;
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
TLevelWriterGif::TLevelWriterGif(const TFilePath &path, TPropertyGroup *winfo)
turtletooth 04d8fd
    : TLevelWriter(path, winfo) {
turtletooth 04d8fd
  if (!m_properties) m_properties = new Tiio::GifWriterProperties();
shun-iwasawa 27b0cf
  std::string scale = m_properties->getProperty("Scale")->getValueAsString();
shun-iwasawa 27b0cf
  m_scale           = QString::fromStdString(scale).toInt();
turtletooth 04d8fd
  TBoolProperty *looping =
turtletooth 04d8fd
      (TBoolProperty *)m_properties->getProperty("Looping");
turtletooth 04d8fd
  m_looping = looping->getValue();
turtletooth 04d8fd
  TBoolProperty *palette =
turtletooth 04d8fd
      (TBoolProperty *)m_properties->getProperty("Generate Palette");
turtletooth 04d8fd
  m_palette    = palette->getValue();
turtletooth 04d8fd
  ffmpegWriter = new Ffmpeg();
turtletooth 04d8fd
  ffmpegWriter->setPath(m_path);
turtletooth 04d8fd
  // m_frameCount = 0;
turtletooth 04d8fd
  if (TSystem::doesExistFileOrLevel(m_path)) TSystem::deleteFile(m_path);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TLevelWriterGif::~TLevelWriterGif() {
turtletooth 04d8fd
  QStringList preIArgs;
turtletooth 04d8fd
  QStringList postIArgs;
turtletooth 04d8fd
  QStringList palettePreIArgs;
turtletooth 04d8fd
  QStringList palettePostIArgs;
turtletooth 04d8fd
turtletooth 04d8fd
  int outLx = m_lx;
turtletooth 04d8fd
  int outLy = m_ly;
turtletooth 04d8fd
turtletooth 04d8fd
  // set scaling
turtletooth 04d8fd
  outLx = m_lx * m_scale / 100;
turtletooth 04d8fd
  outLy = m_ly * m_scale / 100;
shun-iwasawa 27b0cf
  // ffmpeg doesn't like resolutions that aren't divisible by 2.
turtletooth 04d8fd
  if (outLx % 2 != 0) outLx++;
turtletooth 04d8fd
  if (outLy % 2 != 0) outLy++;
turtletooth 04d8fd
turtletooth 04d8fd
  QString palette;
turtletooth 04d8fd
  QString filters = "scale=" + QString::number(outLx) + ":-1:flags=lanczos";
turtletooth 04d8fd
  QString paletteFilters = filters + " [x]; [x][1:v] paletteuse";
turtletooth 04d8fd
  if (m_palette) {
Jeremy Bullock edaab7
    palette = ffmpegWriter->getFfmpegCache().getQString() + "//" +
Jeremy Bullock edaab7
              QString::fromStdString(m_path.getName()) + "palette.png";
turtletooth 04d8fd
    palettePreIArgs << "-v";
turtletooth 04d8fd
    palettePreIArgs << "warning";
turtletooth 04d8fd
turtletooth 04d8fd
    palettePostIArgs << "-vf";
turtletooth 04d8fd
    palettePostIArgs << filters + ",palettegen";
turtletooth 04d8fd
    palettePostIArgs << palette;
turtletooth 04d8fd
turtletooth 04d8fd
    // write the palette
turtletooth 04d8fd
    ffmpegWriter->runFfmpeg(palettePreIArgs, palettePostIArgs, false, true,
turtletooth 04d8fd
                            true);
turtletooth 04d8fd
    ffmpegWriter->addToCleanUp(palette);
turtletooth 04d8fd
  }
turtletooth 04d8fd
turtletooth 04d8fd
  preIArgs << "-v";
turtletooth 04d8fd
  preIArgs << "warning";
turtletooth 04d8fd
  preIArgs << "-r";
turtletooth 04d8fd
  preIArgs << QString::number(m_frameRate);
turtletooth 04d8fd
  if (m_palette) {
turtletooth 04d8fd
    postIArgs << "-i";
turtletooth 04d8fd
    postIArgs << palette;
turtletooth 04d8fd
    postIArgs << "-lavfi";
turtletooth 04d8fd
    postIArgs << paletteFilters;
shun-iwasawa 27b0cf
  } else {
shun-iwasawa 27b0cf
    postIArgs << "-lavfi";
shun-iwasawa 27b0cf
    postIArgs << filters;
turtletooth 04d8fd
  }
turtletooth 04d8fd
turtletooth 04d8fd
  if (!m_looping) {
turtletooth 04d8fd
    postIArgs << "-loop";
turtletooth 04d8fd
    postIArgs << "-1";
turtletooth 04d8fd
  }
turtletooth 04d8fd
turtletooth 04d8fd
  std::string outPath = m_path.getQString().toStdString();
turtletooth 04d8fd
turtletooth 04d8fd
  ffmpegWriter->runFfmpeg(preIArgs, postIArgs, false, false, true);
turtletooth 04d8fd
  ffmpegWriter->cleanUpFiles();
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TImageWriterP TLevelWriterGif::getFrameWriter(TFrameId fid) {
turtletooth 04d8fd
  // if (IOError != 0)
turtletooth 04d8fd
  //	throw TImageException(m_path, buildGifExceptionString(IOError));
turtletooth 04d8fd
  if (fid.getLetter() != 0) return TImageWriterP(0);
turtletooth 04d8fd
  int index            = fid.getNumber();
turtletooth 04d8fd
  TImageWriterGif *iwg = new TImageWriterGif(m_path, index, this);
turtletooth 04d8fd
  return TImageWriterP(iwg);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
void TLevelWriterGif::setFrameRate(double fps) {
turtletooth 04d8fd
  // m_fps = fps;
turtletooth 04d8fd
  m_frameRate = fps;
turtletooth 04d8fd
  ffmpegWriter->setFrameRate(fps);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
void TLevelWriterGif::saveSoundTrack(TSoundTrack *st) { return; }
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
void TLevelWriterGif::save(const TImageP &img, int frameIndex) {
turtletooth 04d8fd
  TRasterImageP image(img);
turtletooth 04d8fd
  m_lx = image->getRaster()->getLx();
turtletooth 04d8fd
  m_ly = image->getRaster()->getLy();
turtletooth 04d8fd
  ffmpegWriter->createIntermediateImage(img, frameIndex);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TImageReaderGif
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
class TImageReaderGif final : public TImageReader {
turtletooth 04d8fd
public:
turtletooth 04d8fd
  int m_frameIndex;
turtletooth 04d8fd
Jeremy Bullock 2b9d8f
  TImageReaderGif(const TFilePath &path, int index, TLevelReaderGif *lra,
Jeremy Bullock 2b9d8f
                  TImageInfo *info)
Jeremy Bullock 2b9d8f
      : TImageReader(path), m_lra(lra), m_frameIndex(index), m_info(info) {
turtletooth 04d8fd
    m_lra->addRef();
turtletooth 04d8fd
  }
turtletooth 04d8fd
  ~TImageReaderGif() { m_lra->release(); }
turtletooth 04d8fd
turtletooth 04d8fd
  TImageP load() override { return m_lra->load(m_frameIndex); }
turtletooth 04d8fd
  TDimension getSize() const { return m_lra->getSize(); }
turtletooth 04d8fd
  TRect getBBox() const { return TRect(); }
Jeremy Bullock 2b9d8f
  const TImageInfo *getImageInfo() const override { return m_info; }
turtletooth 04d8fd
turtletooth 04d8fd
private:
turtletooth 04d8fd
  TLevelReaderGif *m_lra;
Jeremy Bullock 2b9d8f
  TImageInfo *m_info;
turtletooth 04d8fd
turtletooth 04d8fd
  // not implemented
turtletooth 04d8fd
  TImageReaderGif(const TImageReaderGif &);
turtletooth 04d8fd
  TImageReaderGif &operator=(const TImageReaderGif &src);
turtletooth 04d8fd
};
turtletooth 04d8fd
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
//
turtletooth 04d8fd
//  TLevelReaderGif
turtletooth 04d8fd
//
turtletooth 04d8fd
//===========================================================
turtletooth 04d8fd
turtletooth 04d8fd
TLevelReaderGif::TLevelReaderGif(const TFilePath &path)
turtletooth 04d8fd
    : TLevelReader(path)
turtletooth 04d8fd
turtletooth 04d8fd
{
turtletooth 04d8fd
  ffmpegReader = new Ffmpeg();
turtletooth 04d8fd
  ffmpegReader->setPath(m_path);
turtletooth 04d8fd
  ffmpegReader->disablePrecompute();
turtletooth 04d8fd
  ffmpegFileInfo tempInfo = ffmpegReader->getInfo();
turtletooth 04d8fd
  double fps              = tempInfo.m_frameRate;
turtletooth 04d8fd
  m_frameCount            = tempInfo.m_frameCount;
turtletooth 04d8fd
  m_size                  = TDimension(tempInfo.m_lx, tempInfo.m_ly);
turtletooth 04d8fd
  m_lx                    = m_size.lx;
turtletooth 04d8fd
  m_ly                    = m_size.ly;
turtletooth 04d8fd
turtletooth 04d8fd
  ffmpegReader->getFramesFromMovie();
Jeremy Bullock 779cdf
  m_frameCount = ffmpegReader->getGifFrameCount();
turtletooth 04d8fd
  // set values
turtletooth 04d8fd
  m_info                   = new TImageInfo();
turtletooth 04d8fd
  m_info->m_frameRate      = fps;
turtletooth 04d8fd
  m_info->m_lx             = m_lx;
turtletooth 04d8fd
  m_info->m_ly             = m_ly;
turtletooth 04d8fd
  m_info->m_bitsPerSample  = 8;
turtletooth 04d8fd
  m_info->m_samplePerPixel = 4;
Jeremy Bullock 2b9d8f
  m_info->m_dpix           = Stage::standardDpi;
Jeremy Bullock 2b9d8f
  m_info->m_dpiy           = Stage::standardDpi;
turtletooth 04d8fd
}
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TLevelReaderGif::~TLevelReaderGif() {
turtletooth 04d8fd
  // ffmpegReader->cleanUpFiles();
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TLevelP TLevelReaderGif::loadInfo() {
turtletooth 04d8fd
  if (m_frameCount == -1) return TLevelP();
turtletooth 04d8fd
  TLevelP level;
turtletooth 04d8fd
  for (int i = 1; i <= m_frameCount; i++) level->setFrame(i, TImageP());
turtletooth 04d8fd
  return level;
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//-----------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TImageReaderP TLevelReaderGif::getFrameReader(TFrameId fid) {
turtletooth 04d8fd
  // if (IOError != 0)
turtletooth 04d8fd
  //	throw TImageException(m_path, buildAVIExceptionString(IOError));
turtletooth 04d8fd
  if (fid.getLetter() != 0) return TImageReaderP(0);
Jeremy Bullock 2b9d8f
  int index            = fid.getNumber();
Jeremy Bullock 2b9d8f
  TImageReaderGif *irm = new TImageReaderGif(m_path, index, this, m_info);
turtletooth 04d8fd
  return TImageReaderP(irm);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
//------------------------------------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TDimension TLevelReaderGif::getSize() { return m_size; }
turtletooth 04d8fd
turtletooth 04d8fd
//------------------------------------------------
turtletooth 04d8fd
turtletooth 04d8fd
TImageP TLevelReaderGif::load(int frameIndex) {
turtletooth 04d8fd
  return ffmpegReader->getImage(frameIndex);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
Tiio::GifWriterProperties::GifWriterProperties()
shun-iwasawa 27b0cf
    : m_scale("Scale", 1, 100, 100)
turtletooth 04d8fd
    , m_looping("Looping", true)
turtletooth 04d8fd
    , m_palette("Generate Palette", true) {
turtletooth 04d8fd
  bind(m_scale);
turtletooth 04d8fd
  bind(m_looping);
turtletooth 04d8fd
  bind(m_palette);
turtletooth 04d8fd
}
turtletooth 04d8fd
turtletooth 04d8fd
// Tiio::Reader* Tiio::makeGifReader(){ return nullptr; }
turtletooth 04d8fd
// Tiio::Writer* Tiio::makeGifWriter(){ return nullptr; }