Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/dpiscale.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshcell.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshleveltypes.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "timagecache.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TAffine getDpiAffine(TXshSimpleLevel *level, const TFrameId &fid,
Shinya Kitaoka 120a6e
                     bool forceFullSampling) {
Shinya Kitaoka 120a6e
  const double factor = Stage::inch;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (level->getType() == PLI_XSHLEVEL) return TAffine();
Shinya Kitaoka 120a6e
  LevelProperties *prop = level->getProperties();
Shinya Kitaoka 120a6e
  TAffine aff;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string imageId = level->getImageId(fid);
Shinya Kitaoka 120a6e
  int frameStatus     = level->getFrameStatus(fid);
Shinya Kitaoka 120a6e
  if (frameStatus & TXshSimpleLevel::CleanupPreview) {
Shinya Kitaoka 120a6e
    TRasterImageP ri = TImageCache::instance()->get(imageId, false);
Shinya Kitaoka 120a6e
    if (ri) {
Shinya Kitaoka 120a6e
      double dpix = 0, dpiy = 0;
Shinya Kitaoka 120a6e
      ri->getDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
      if (dpix != 0.0 && dpiy != 0.0)
Shinya Kitaoka 120a6e
        return TScale(factor / dpix, factor / dpiy);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int subs = prop->getSubsampling();
Shinya Kitaoka 120a6e
  if (level->getType() != PLI_XSHLEVEL) {
Shinya Kitaoka 120a6e
    TPointD dpi                       = level->getDpi(fid);
Shinya Kitaoka 120a6e
    if (dpi.x != 0 && dpi.y != 0) aff = TScale(factor / dpi.x, factor / dpi.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (!forceFullSampling)
Shinya Kitaoka 120a6e
      TImageCache::instance()->getSubsampling(imageId, subs);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (subs != 1 && !forceFullSampling) aff = TScale(subs) * aff;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return aff;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TAffine getDpiAffine(TCamera *camera) {
Shinya Kitaoka 120a6e
  const double factor = Stage::inch;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TDimensionD size = camera->getSize();
Shinya Kitaoka 120a6e
  TDimension res   = camera->getRes();
Shinya Kitaoka 120a6e
  double sx        = factor * size.lx / res.lx;
Shinya Kitaoka 120a6e
  double sy        = factor * size.ly / res.ly;
Shinya Kitaoka 120a6e
  return TScale(sx, sy);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPointD getCurrentDpiScale(TXshSimpleLevel *sl, const TFrameId &fid) {
Shinya Kitaoka 120a6e
  TAffine aff = getDpiAffine(sl, fid);
Shinya Kitaoka 120a6e
  if ((sl->getType() == TZP_XSHLEVEL || sl->getType() == OVL_XSHLEVEL) &&
Shinya Kitaoka 120a6e
      sl->getProperties()->getSubsampling() > 1) {
Shinya Kitaoka 120a6e
    int subs    = sl->getProperties()->getSubsampling();
Shinya Kitaoka 120a6e
    TImageP img = TImageCache::instance()->get(sl->getImageId(fid), false);
Shinya Kitaoka 120a6e
    TToonzImageP ti(img);
Shinya Kitaoka 120a6e
    TRasterImageP ri(img);
Shinya Kitaoka 120a6e
    if (ti)
Shinya Kitaoka 120a6e
      subs = ti->getSubsampling();
Shinya Kitaoka 120a6e
    else if (ri)
Shinya Kitaoka 120a6e
      subs   = ri->getSubsampling();
Shinya Kitaoka 120a6e
    double f = 1.0 / subs;
Shinya Kitaoka 120a6e
    return aff * TPointD(f, f);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    return aff * TPointD(1, 1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef FORSE_OBSOLETO
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double getCurrentCameraStandDpi() {
Shinya Kitaoka 120a6e
  const double defaultDpi = 100;
Shinya Kitaoka 120a6e
  TApplication *app       = TApplication::instance();
Shinya Kitaoka 120a6e
  if (app->isEditingLevelFrame()) {
Shinya Kitaoka 120a6e
    TXshSimpleLevel *sl =
Shinya Kitaoka 120a6e
        app->getCurrentLevel() ? app->getCurrentLevel()->getSimpleLevel() : 0;
Shinya Kitaoka 120a6e
    if (sl) {
Shinya Kitaoka 120a6e
      double dpi = sl->getDpi().x;
Shinya Kitaoka 120a6e
      return dpi > 0.0 ? dpi : defaultDpi;
Shinya Kitaoka 120a6e
      return sl->getDpi().x;
Shinya Kitaoka 120a6e
    } else
Shinya Kitaoka 120a6e
      return defaultDpi;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    ToonzScene *scene = app->getCurrentScene();
Shinya Kitaoka 120a6e
    TCamera *camera   = scene->getCurrentCamera();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TDimensionD size = camera->getSize();
Shinya Kitaoka 120a6e
    TDimension res   = camera->getRes();
Shinya Kitaoka 120a6e
    assert(size.lx > 0);
Shinya Kitaoka 120a6e
    if (size.lx > 0)
Shinya Kitaoka 120a6e
      return res.lx / size.lx;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      return defaultDpi;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif