optimize: tiff method refactor

This commit is contained in:
xiaoyifang 2022-04-05 21:25:07 +08:00
parent 64d989f9c5
commit 67c74136e8
9 changed files with 39 additions and 193 deletions

29
dsl.cc
View file

@ -1919,33 +1919,8 @@ void DslResourceRequest::run()
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( (unsigned char *) &data.front(),
data.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if ( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "BMP" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
Mutex::Lock _( dataMutex );

29
gls.cc
View file

@ -1287,33 +1287,8 @@ void GlsResourceRequest::run()
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( (unsigned char *) &data.front(),
data.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if ( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "BMP" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
if( Filetype::isNameOfCSS( resourceName ) )

31
mdx.cc
View file

@ -975,33 +975,8 @@ void MddResourceRequest::run()
if( Filetype::isNameOfTiff( u8ResourceName ) )
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( (unsigned char *)&data.front(), data.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "webp" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
}
break;
@ -1096,8 +1071,6 @@ QString & MdxDictionary::filterResource( QString const & articleId, QString & ar
QString id = QString::fromStdString( getId() );
QString uniquePrefix = QString::fromLatin1( "g" ) + id + "_" + articleId + "_";
QString articleNewText;
int linkPos = 0;
QRegularExpressionMatchIterator it = mdxRx.allLinksRe.globalMatch( article );

28
slob.cc
View file

@ -1589,32 +1589,8 @@ void SlobResourceRequest::run()
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( reinterpret_cast< const uchar * >( resource.data() ), resource.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if ( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "BMP" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
else
{

View file

@ -1731,33 +1731,8 @@ void StardictResourceRequest::run()
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( (unsigned char *) &data.front(),
data.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if ( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "BMP" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
if( Filetype::isNameOfCSS( resourceName ) )

22
tiff.cc
View file

@ -139,6 +139,28 @@ bool tiffToQImage( const char * data, int size, QImage & image )
return false;
}
void tiff2img( vector< char > & data, const char * format )
{
QImage img = QImage::fromData( (unsigned char *)&data.front(), data.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
tiffToQImage( &data.front(), data.size(), img );
#endif
if( !img.isNull() )
{
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, format );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
buffer.close();
}
}
} // namespace
#endif

View file

@ -4,7 +4,8 @@
#ifdef MAKE_EXTRA_TIFF_HANDLER
#include <QImage>
#include <vector>
using std::vector;
namespace GdTiff
{
@ -12,7 +13,7 @@ namespace GdTiff
// We will handle such 1-bit b/w images with default photometric
bool tiffToQImage( const char * data, int size, QImage & image );
void tiff2img( vector< char > & data, const char * format = "webp" );
}
#endif

30
xdxf.cc
View file

@ -1091,34 +1091,8 @@ void XdxfResourceRequest::run()
if ( Filetype::isNameOfTiff( resourceName ) )
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( (unsigned char *) &data.front(),
data.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if ( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "BMP" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
Mutex::Lock _( dataMutex );

29
zim.cc
View file

@ -1424,33 +1424,8 @@ void ZimResourceRequest::run()
if ( Filetype::isNameOfTiff( resourceName ) )
{
// Convert it
dataMutex.lock();
QImage img = QImage::fromData( reinterpret_cast< const uchar * >( resource.data() ), resource.size() );
#ifdef MAKE_EXTRA_TIFF_HANDLER
if( img.isNull() )
GdTiff::tiffToQImage( &data.front(), data.size(), img );
#endif
dataMutex.unlock();
if ( !img.isNull() )
{
// Managed to load -- now store it back as BMP
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img.save( &buffer, "BMP" );
Mutex::Lock _( dataMutex );
data.resize( buffer.size() );
memcpy( &data.front(), buffer.data(), data.size() );
}
Mutex::Lock _( dataMutex );
GdTiff::tiff2img( data );
}
else
{