tahoma2d/toonz/sources/toonzlib/imagebuilders.cpp

405 lines
12 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tcommon.h"
#include "timageinfo.h"
#include "timage_io.h"
#include "tlevel_io.h"
#include "ttoonzimage.h"
#include "tvectorimage.h"
#include "trastercm.h"
#include "tvectorrenderdata.h"
#include "tpalette.h"
#include "tgl.h"
#include "tvectorgl.h"
#include "tofflinegl.h"
#include "timagecache.h"
// TnzLib includes
#include "toonz/txshleveltypes.h"
#include "toonz/levelproperties.h"
#include "toonz/txshsimplelevel.h"
#include "toonz/fill.h"
// Qt includes
2016-04-27 03:18:29 +12:00
#include <QImage>
#include <QThread>
#include <QOpenGLContext>
#include <QOffscreenSurface>
#include <QOpenGLFramebufferObject>
2016-03-19 06:57:51 +13:00
#include "imagebuilders.h"
//***************************************************************************************
// Global stuff
//***************************************************************************************
extern TOfflineGL *currentOfflineGL;
//***************************************************************************************
// ImageLoader implementation
//***************************************************************************************
ImageLoader::ImageLoader(const TFilePath &path, const TFrameId &fid)
2016-06-15 18:43:10 +12:00
: m_path(path), m_fid(fid), m_subsampling(0), m_64bitCompatible(false) {}
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool ImageLoader::getInfo(TImageInfo &info, int imFlags, void *extData) {
try {
TLevelReaderP lr(m_path);
TImageReaderP fr = lr->getFrameReader(m_fid);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// NOTE: Currently not changing imageInfo's bpp stuff...
// Ignoring subsampling too...
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return ImageBuilder::setImageInfo(info, fr.getPointer());
} catch (TException &e) {
QString msg = QString::fromStdWString(e.getMessage());
if (msg == QString("Old 4.1 Palette")) throw;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return false;
}
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
inline int ImageLoader::buildSubsampling(int imFlags, BuildExtData *data) {
return (imFlags & ImageManager::toBeModified)
? 1
: (data->m_subs > 0)
? data->m_subs
: (m_subsampling > 0)
? m_subsampling
: data->m_sl->getProperties()->getSubsampling();
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TImageP ImageLoader::build(int imFlags, void *extData) {
assert(extData);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Extract external data
BuildExtData *data = static_cast<BuildExtData *>(extData);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int subsampling = buildSubsampling(imFlags, data);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
try {
// Initialize level reader
TLevelReaderP lr(m_path);
if (!lr) return TImageP();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Load info in cases where it's required first
lr->doReadPalette(false);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if ((m_path.getType() == "pli") || (m_path.getType() == "svg") ||
(m_path.getType() == "psd"))
lr->loadInfo();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
lr->doReadPalette(true); // Allow palette loading
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TImageReaderP ir = lr->getFrameReader(m_fid);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool enable64bit = (imFlags & ImageManager::is64bitEnabled);
ir->enable16BitRead(enable64bit); // Set 64-bit loading if required
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Load the image
TImageP img;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (data->m_icon && m_path.getType() == "tlv")
img = ir->loadIcon(); // TODO: Why just in the tlv case??
else {
ir->setShrink(subsampling);
img = ir->load();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ir->enable16BitRead(false);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!img) return img; // There was an error loading the image.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TPalette *palette = data->m_sl->getPalette();
if (palette) img->setPalette(palette);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (subsampling > 1) {
// Store the subsampling info in the image
if (TRasterImageP ri = img)
ri->setSubsampling(subsampling);
else if (TToonzImageP ti = img)
ti->setSubsampling(subsampling);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// In case the image will be cached, store its subsampling and 64 bit
// compatibility
if (!(imFlags & ImageManager::dontPutInCache)) {
m_subsampling = subsampling;
m_64bitCompatible =
data->m_sl->is16BitChannelLevel() ? enable64bit : true;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return img;
} catch (...) {
return TImageP();
}
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool ImageLoader::isImageCompatible(int imFlags, void *extData) {
assert(extData);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
BuildExtData *data = static_cast<BuildExtData *>(extData);
const TXshSimpleLevel *sl = data->m_sl;
2016-06-15 18:43:10 +12:00
// NOTE: Vector and Mesh dont care about sub sampling rate and bit depth
// compatibility.
// They are property of Raster.
if (sl->getType() == PLI_XSHLEVEL || sl->getType() == MESH_XSHLEVEL)
return true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int subsampling = buildSubsampling(imFlags, data);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_subsampling <= 0 || subsampling != m_subsampling) return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_64bitCompatible || !(imFlags & ImageManager::is64bitEnabled)) {
return true;
} else {
return false;
}
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ImageLoader::invalidate() {
ImageBuilder::invalidate();
m_subsampling = 0;
m_64bitCompatible = false;
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
/*--
* ImageBuilder仮想関数の実装Load時に全てキャッシュに格納する
* --*/
2016-03-19 06:57:51 +13:00
void ImageLoader::buildAllIconsAndPutInCache(TXshSimpleLevel *level,
2016-06-15 18:43:10 +12:00
std::vector<TFrameId> fids,
std::vector<std::string> iconIds,
bool cacheImagesAsWell) {
if (m_path.getType() != "tlv") return;
if (fids.empty() || iconIds.empty()) return;
/*- fidとアイコンidの数は同じはず -*/
if ((int)fids.size() != (int)iconIds.size()) return;
try {
TLevelReaderP lr(m_path);
if (!lr) return;
for (int i = 0; i < (int)fids.size(); i++) {
lr->doReadPalette(false);
TImageReaderP ir = lr->getFrameReader(fids[i]);
lr->doReadPalette(true);
TImageInfo info;
TPalette *palette = level->getPalette();
std::string fullImgId = level->getImageId(fids[i]);
/*- 画像データも一緒にキャッシュする場合 -*/
if (cacheImagesAsWell) {
ir->enable16BitRead(m_64bitCompatible);
ir->setShrink(1);
TImageP fullImg = ir->load();
if (fullImg) {
if (palette) fullImg->setPalette(palette);
TImageCache::instance()->add(fullImgId, fullImg, true);
setImageInfo(info, fullImg.getPointer());
}
}
/*- アイコンのロード -*/
TImageP img = ir->loadIcon();
ir->enable16BitRead(false);
if (img) {
if (palette) img->setPalette(palette);
TImageCache::instance()->add(iconIds[i], img, true);
}
}
} catch (...) {
return;
}
2016-03-19 06:57:51 +13:00
}
2017-10-02 23:30:28 +13:00
void ImageLoader::setFid(const TFrameId &fid) { m_fid = fid; }
2016-03-19 06:57:51 +13:00
//***************************************************************************************
// ImageRasterizer implementation
//***************************************************************************************
2016-06-15 18:43:10 +12:00
bool ImageRasterizer::getInfo(TImageInfo &info, int imFlags, void *extData) {
assert(false); // None should get these... in case, TODO
return false;
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TImageP ImageRasterizer::build(int imFlags, void *extData) {
assert(!(imFlags &
~(ImageManager::dontPutInCache | ImageManager::forceRebuild)));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TDimension d(10, 10);
TPoint off(0, 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Fetch image
assert(extData);
ImageLoader::BuildExtData *data = (ImageLoader::BuildExtData *)extData;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const std::string &srcImgId = data->m_sl->getImageId(data->m_fid);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TImageP img = ImageManager::instance()->getImage(srcImgId, imFlags, extData);
if (img) {
TVectorImageP vi = img;
if (vi) {
TRectD bbox = vi->getBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
d = TDimension(tceil(bbox.getLx()) + 1, tceil(bbox.getLy()) + 1);
off = TPoint((int)bbox.x0, (int)bbox.y0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TPalette *vpalette = vi->getPalette();
TVectorRenderData rd(TTranslation(-off.x, -off.y), TRect(TPoint(0, 0), d),
vpalette, 0, true, true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// this is too slow.
{
QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CompatibilityProfile);
2016-04-27 03:18:29 +12:00
2016-07-21 13:34:09 +12:00
std::unique_ptr<QOffscreenSurface> surface(new QOffscreenSurface());
surface->setFormat(format);
2017-11-09 21:36:56 +13:00
// Enabling Qt::AA_ShareOpenGLContexts attribute in main()
surface->setScreen(QOpenGLContext::globalShareContext()->screen());
2016-07-21 13:34:09 +12:00
surface->create();
2016-06-15 18:43:10 +12:00
TRaster32P ras(d);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_MODELVIEW), glPushMatrix();
glMatrixMode(GL_PROJECTION), glPushMatrix();
{
std::unique_ptr<QOpenGLFramebufferObject> fb(
new QOpenGLFramebufferObject(d.lx, d.ly));
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
fb->bind();
assert(glGetError() == 0);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
glViewport(0, 0, d.lx, d.ly);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, d.lx, 0, d.ly);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.0);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
assert(glGetError() == 0);
tglDraw(rd, vi.getPointer());
assert(glGetError() == 0);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
assert(glGetError() == 0);
glFlush();
assert(glGetError() == 0);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
QImage img =
fb->toImage().scaled(QSize(d.lx, d.ly), Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
int wrap = ras->getLx() * sizeof(TPixel32);
uchar *srcPix = img.bits();
uchar *dstPix = ras->getRawData() + wrap * (d.ly - 1);
for (int y = 0; y < d.ly; y++) {
memcpy(dstPix, srcPix, wrap);
dstPix -= wrap;
srcPix += wrap;
}
fb->release();
}
glMatrixMode(GL_MODELVIEW), glPopMatrix();
glMatrixMode(GL_PROJECTION), glPopMatrix();
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
glPopAttrib();
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
TRasterImageP ri = TRasterImageP(ras);
ri->setOffset(off + ras->getCenter());
2016-04-27 03:18:29 +12:00
2016-06-15 18:43:10 +12:00
return ri;
}
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Error case: return a dummy image (is it really required?)
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRaster32P ras(d);
ras->fill(TPixel32(127, 0, 127, 127));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return TRasterImageP(ras);
2016-03-19 06:57:51 +13:00
}
//***************************************************************************************
// ImageFiller implementation
//***************************************************************************************
2016-06-15 18:43:10 +12:00
bool ImageFiller::getInfo(TImageInfo &info, int imFlags, void *extData) {
assert(false); // None should get these... in case, TODO
return false;
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TImageP ImageFiller::build(int imFlags, void *extData) {
assert(imFlags == ImageManager::none);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Fetch image
assert(extData);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ImageLoader::BuildExtData *data = (ImageLoader::BuildExtData *)extData;
assert(data->m_subs == 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const std::string &srcImgId = data->m_sl->getImageId(data->m_fid);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TImageP img = ImageManager::instance()->getImage(srcImgId, imFlags, extData);
if (img) {
TRasterImageP ri = img;
if (ri) {
TRaster32P ras = ri->getRaster();
if (ras) {
TRaster32P newRas = ras->clone();
FullColorAreaFiller filler(newRas);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TPaletteP palette = new TPalette();
int styleId = palette->getPage(0)->addStyle(TPixel32::White);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FillParameters params;
params.m_palette = palette.getPointer();
params.m_styleId = styleId;
params.m_minFillDepth = 0;
params.m_maxFillDepth = 15;
filler.rectFill(newRas->getBounds(), params, false);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRasterImageP ri = TRasterImageP(newRas);
return ri;
}
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Error case: return a dummy image (is it really required?)
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRaster32P ras(10, 10);
ras->fill(TPixel32(127, 0, 127, 127));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return TRasterImageP(ras);
2016-03-19 06:57:51 +13:00
}