Kaydet (Commit) f8355221 authored tarafından Chris Sherlock's avatar Chris Sherlock Kaydeden (comit) Tomaž Vajngerl

tdf#85761 vcl: JPEG export does not save PPI values correctly

JPEG values are currently hardcoded to 96PPI when we export JPEGs. The
Graphic class doesn't have an easy way to get the PPI, but this can
actually be calculated from the pref size and pref map mode (no idea
why it is called "Pref").

Interestingly, you need to get a multiplier to work this out, relative
to units of 100th mm. The EPS filter code had a function that does
exactly this, but it's entirely based on MapMode units so it was really
implemented in the wrong class IMO. I have thus moved it out of PSWriter
and into MapMode.

This also fixes tdf#65695, which was partially fixed, but had the JPEG
PPI hardcoded to 96dpi.

Also fixes tdf#97481.

Change-Id: Iedb674141dd4e22fcbfb7be357dc777f732aa3aa
Reviewed-on: https://gerrit.libreoffice.org/22339Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarTomaž Vajngerl <quikee@gmail.com>
üst 19fb09dc
...@@ -215,7 +215,6 @@ private: ...@@ -215,7 +215,6 @@ private:
inline void ImplWriteTextColor( sal_uLong nMode = PS_RET ); inline void ImplWriteTextColor( sal_uLong nMode = PS_RET );
void ImplWriteColor( sal_uLong nMode ); void ImplWriteColor( sal_uLong nMode );
static double ImplGetScaling( const MapMode& );
void ImplGetMapMode( const MapMode& ); void ImplGetMapMode( const MapMode& );
static bool ImplGetBoundingBox( double* nNumb, sal_uInt8* pSource, sal_uLong nSize ); static bool ImplGetBoundingBox( double* nNumb, sal_uInt8* pSource, sal_uLong nSize );
static sal_uInt8* ImplSearchEntry( sal_uInt8* pSource, sal_uInt8 const * pDest, sal_uLong nComp, sal_uLong nSize ); static sal_uInt8* ImplSearchEntry( sal_uInt8* pSource, sal_uInt8 const * pDest, sal_uLong nComp, sal_uLong nSize );
...@@ -2197,56 +2196,10 @@ void PSWriter::ImplWriteColor( sal_uLong nMode ) ...@@ -2197,56 +2196,10 @@ void PSWriter::ImplWriteColor( sal_uLong nMode )
ImplExecMode( nMode ); ImplExecMode( nMode );
} }
double PSWriter::ImplGetScaling( const MapMode& rMapMode )
{
double nMul;
switch ( rMapMode.GetMapUnit() )
{
case MAP_PIXEL :
case MAP_SYSFONT :
case MAP_APPFONT :
case MAP_100TH_MM :
nMul = 1;
break;
case MAP_10TH_MM :
nMul = 10;
break;
case MAP_MM :
nMul = 100;
break;
case MAP_CM :
nMul = 1000;
break;
case MAP_1000TH_INCH :
nMul = 2.54;
break;
case MAP_100TH_INCH :
nMul = 25.4;
break;
case MAP_10TH_INCH :
nMul = 254;
break;
case MAP_INCH :
nMul = 2540;
break;
case MAP_TWIP :
nMul = 1.76388889;
break;
case MAP_POINT :
nMul = 35.27777778;
break;
default:
nMul = 1.0;
break;
}
return nMul;
}
void PSWriter::ImplGetMapMode( const MapMode& rMapMode ) void PSWriter::ImplGetMapMode( const MapMode& rMapMode )
{ {
ImplWriteLine( "tm setmatrix" ); ImplWriteLine( "tm setmatrix" );
double fMul = ImplGetScaling( rMapMode ); double fMul = rMapMode.GetUnitMultiplier();
double fScaleX = (double)rMapMode.GetScaleX() * fMul; double fScaleX = (double)rMapMode.GetScaleX() * fMul;
double fScaleY = (double)rMapMode.GetScaleY() * fMul; double fScaleY = (double)rMapMode.GetScaleY() * fMul;
ImplTranslate( rMapMode.GetOrigin().X() * fScaleX, rMapMode.GetOrigin().Y() * fScaleY ); ImplTranslate( rMapMode.GetOrigin().X() * fScaleX, rMapMode.GetOrigin().Y() * fScaleY );
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <vcl/gfxlink.hxx> #include <vcl/gfxlink.hxx>
#include <com/sun/star/uno/Reference.hxx> #include <com/sun/star/uno/Reference.hxx>
#include <vcl/svgdata.hxx> #include <vcl/svgdata.hxx>
#include <basegfx/vector/b2dsize.hxx>
enum GraphicType enum GraphicType
...@@ -163,6 +164,8 @@ public: ...@@ -163,6 +164,8 @@ public:
MapMode GetPrefMapMode() const; MapMode GetPrefMapMode() const;
void SetPrefMapMode( const MapMode& rPrefMapMode ); void SetPrefMapMode( const MapMode& rPrefMapMode );
basegfx::B2DSize GetPPI() const;
Size GetSizePixel( const OutputDevice* pRefDevice = nullptr ) const; Size GetSizePixel( const OutputDevice* pRefDevice = nullptr ) const;
sal_uLong GetSizeBytes() const; sal_uLong GetSizeBytes() const;
......
...@@ -61,6 +61,9 @@ public: ...@@ -61,6 +61,9 @@ public:
void SetScaleY( const Fraction& rScaleY ); void SetScaleY( const Fraction& rScaleY );
const Fraction& GetScaleY() const; const Fraction& GetScaleY() const;
/// Gets the multiplier, which is relative to 1/100 mm units
double GetUnitMultiplier() const;
MapMode& operator=( const MapMode& rMapMode ); MapMode& operator=( const MapMode& rMapMode );
bool operator==( const MapMode& rMapMode ) const; bool operator==( const MapMode& rMapMode ) const;
bool operator!=( const MapMode& rMapMode ) const bool operator!=( const MapMode& rMapMode ) const
......
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_CppunitTest_CppunitTest,vcl_mapmode))
$(eval $(call gb_CppunitTest_set_include,vcl_mapmode,\
$$(INCLUDE) \
-I$(SRCDIR)/vcl/inc \
))
$(eval $(call gb_CppunitTest_add_exception_objects,vcl_mapmode, \
vcl/qa/cppunit/mapmode \
))
$(eval $(call gb_CppunitTest_use_externals,vcl_mapmode,boost_headers))
$(eval $(call gb_CppunitTest_use_libraries,vcl_mapmode, \
comphelper \
cppu \
cppuhelper \
sal \
svt \
test \
tl \
tk \
unotest \
vcl \
$(gb_UWINAPI) \
))
$(eval $(call gb_CppunitTest_use_api,vcl_mapmode,\
udkapi \
offapi \
))
$(eval $(call gb_CppunitTest_use_ure,vcl_mapmode))
$(eval $(call gb_CppunitTest_use_vcl,vcl_mapmode))
$(eval $(call gb_CppunitTest_use_components,vcl_mapmode,\
configmgr/source/configmgr \
i18npool/util/i18npool \
ucb/source/core/ucb1 \
))
$(eval $(call gb_CppunitTest_use_configuration,vcl_mapmode))
# vim: set noet sw=4 ts=4:
...@@ -101,6 +101,7 @@ $(eval $(call gb_Module_add_check_targets,vcl,\ ...@@ -101,6 +101,7 @@ $(eval $(call gb_Module_add_check_targets,vcl,\
CppunitTest_vcl_fontmetric \ CppunitTest_vcl_fontmetric \
CppunitTest_vcl_complextext \ CppunitTest_vcl_complextext \
CppunitTest_vcl_filters_test \ CppunitTest_vcl_filters_test \
CppunitTest_vcl_mapmode \
CppunitTest_vcl_outdev \ CppunitTest_vcl_outdev \
CppunitTest_vcl_app_test \ CppunitTest_vcl_app_test \
CppunitTest_vcl_wmf_test \ CppunitTest_vcl_wmf_test \
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <test/bootstrapfixture.hxx>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <osl/file.hxx>
#include <osl/process.h>
#include <vcl/mapmod.hxx>
class VclMapModeTest : public test::BootstrapFixture
{
public:
VclMapModeTest() : BootstrapFixture(true, false) {}
void testMultiplier();
CPPUNIT_TEST_SUITE(VclMapModeTest);
CPPUNIT_TEST(testMultiplier);
CPPUNIT_TEST_SUITE_END();
};
void VclMapModeTest::testMultiplier()
{
MapMode aMapMode;
CPPUNIT_ASSERT_MESSAGE( "Default map mode is MAP_PIXEL, multiplier should be 1", aMapMode.GetUnitMultiplier() == 1 );
aMapMode.SetMapUnit( MAP_SYSFONT );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_SYSFONT, multiplier should be 1", aMapMode.GetUnitMultiplier() == 1 );
aMapMode.SetMapUnit( MAP_APPFONT );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_APPFONT, multiplier should be 1", aMapMode.GetUnitMultiplier() == 1 );
aMapMode.SetMapUnit( MAP_100TH_MM );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_100TH_MM, multiplier should be 1", aMapMode.GetUnitMultiplier() == 1 );
aMapMode.SetMapUnit( MAP_10TH_MM );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_10TH_MM, multiplier should be 10", aMapMode.GetUnitMultiplier() == 10 );
aMapMode.SetMapUnit( MAP_MM );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_MM, multiplier should be 100", aMapMode.GetUnitMultiplier() == 100 );
aMapMode.SetMapUnit( MAP_CM );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_CM, multiplier should be 1000", aMapMode.GetUnitMultiplier() == 1000 );
aMapMode.SetMapUnit( MAP_1000TH_INCH );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_1000TH_INCH, multiplier should be 2.54", aMapMode.GetUnitMultiplier() == 2.54 );
aMapMode.SetMapUnit( MAP_100TH_INCH );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_100TH_INCH, multiplier should be 2.54", aMapMode.GetUnitMultiplier() == 25.4 );
aMapMode.SetMapUnit( MAP_10TH_INCH );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_10TH_INCH, multiplier should be 254", aMapMode.GetUnitMultiplier() == 254 );
aMapMode.SetMapUnit( MAP_INCH );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_INCH, multiplier should be 2540", aMapMode.GetUnitMultiplier() == 2540 );
aMapMode.SetMapUnit( MAP_TWIP );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_TWIP, multiplier should be 1.76388889", aMapMode.GetUnitMultiplier() == 1.76388889 );
aMapMode.SetMapUnit( MAP_POINT );
CPPUNIT_ASSERT_MESSAGE( "Map mode is MAP_POINT, multiplier should be 35.27777778", aMapMode.GetUnitMultiplier() == 35.27777778 );
}
CPPUNIT_TEST_SUITE_REGISTRATION(VclMapModeTest);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -227,7 +227,11 @@ bool JPEGWriter::Write( const Graphic& rGraphic ) ...@@ -227,7 +227,11 @@ bool JPEGWriter::Write( const Graphic& rGraphic )
if( !mbNative ) if( !mbNative )
mpBuffer = new sal_uInt8[ AlignedWidth4Bytes( mbGreys ? mpReadAccess->Width() * 8L : mpReadAccess->Width() * 24L ) ]; mpBuffer = new sal_uInt8[ AlignedWidth4Bytes( mbGreys ? mpReadAccess->Width() * 8L : mpReadAccess->Width() * 24L ) ];
bRet = WriteJPEG( this, &mrStream, mpReadAccess->Width(), mpReadAccess->Height(), mbGreys, mnQuality, maChromaSubsampling, mxStatusIndicator ); SAL_INFO("vcl", "\nJPEG Export - DPI X: " << rGraphic.GetPPI().getX() << "\nJPEG Export - DPI Y: " << rGraphic.GetPPI().getY());
bRet = WriteJPEG( this, &mrStream, mpReadAccess->Width(),
mpReadAccess->Height(), rGraphic.GetPPI(), mbGreys,
mnQuality, maChromaSubsampling, mxStatusIndicator );
delete[] mpBuffer; delete[] mpBuffer;
mpBuffer = nullptr; mpBuffer = nullptr;
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <com/sun/star/uno/Reference.hxx> #include <com/sun/star/uno/Reference.hxx>
#include <sal/types.h> #include <sal/types.h>
#include <basegfx/vector/b2dsize.hxx>
#include <jpeglib.h> #include <jpeglib.h>
...@@ -38,7 +39,8 @@ void jpeg_svstream_src (j_decompress_ptr cinfo, void* infile); ...@@ -38,7 +39,8 @@ void jpeg_svstream_src (j_decompress_ptr cinfo, void* infile);
void jpeg_svstream_dest (j_compress_ptr cinfo, void* outfile); void jpeg_svstream_dest (j_compress_ptr cinfo, void* outfile);
bool WriteJPEG( JPEGWriter* pJPEGWriter, void* pOutputStream, long nWidth, long nHeight, bool bGreyScale, bool WriteJPEG( JPEGWriter* pJPEGWriter, void* pOutputStream,
long nWidth, long nHeight, basegfx::B2DSize aPPI, bool bGreyScale,
long nQualityPercent, long aChromaSubsampling, long nQualityPercent, long aChromaSubsampling,
css::uno::Reference<css::task::XStatusIndicator> const & status); css::uno::Reference<css::task::XStatusIndicator> const & status);
......
...@@ -231,7 +231,7 @@ void ReadJPEG( JPEGReader* pJPEGReader, void* pInputStream, long* pLines, ...@@ -231,7 +231,7 @@ void ReadJPEG( JPEGReader* pJPEGReader, void* pInputStream, long* pLines,
} }
bool WriteJPEG( JPEGWriter* pJPEGWriter, void* pOutputStream, bool WriteJPEG( JPEGWriter* pJPEGWriter, void* pOutputStream,
long nWidth, long nHeight, bool bGreys, long nWidth, long nHeight, basegfx::B2DSize aPPI, bool bGreys,
long nQualityPercent, long aChromaSubsampling, long nQualityPercent, long aChromaSubsampling,
css::uno::Reference<css::task::XStatusIndicator> const & status ) css::uno::Reference<css::task::XStatusIndicator> const & status )
{ {
...@@ -270,8 +270,8 @@ bool WriteJPEG( JPEGWriter* pJPEGWriter, void* pOutputStream, ...@@ -270,8 +270,8 @@ bool WriteJPEG( JPEGWriter* pJPEGWriter, void* pOutputStream,
jpeg_set_quality( &cinfo, (int) nQualityPercent, FALSE ); jpeg_set_quality( &cinfo, (int) nQualityPercent, FALSE );
cinfo.density_unit = 1; cinfo.density_unit = 1;
cinfo.X_density = 96; cinfo.X_density = aPPI.getX();
cinfo.Y_density = 96; cinfo.Y_density = aPPI.getY();
if ( ( nWidth > 128 ) || ( nHeight > 128 ) ) if ( ( nWidth > 128 ) || ( nHeight > 128 ) )
jpeg_simple_progression( &cinfo ); jpeg_simple_progression( &cinfo );
......
...@@ -37,8 +37,8 @@ static void ImplDrawDefault( OutputDevice* pOutDev, const OUString* pText, ...@@ -37,8 +37,8 @@ static void ImplDrawDefault( OutputDevice* pOutDev, const OUString* pText,
vcl::Font* pFont, const Bitmap* pBitmap, const BitmapEx* pBitmapEx, vcl::Font* pFont, const Bitmap* pBitmap, const BitmapEx* pBitmapEx,
const Point& rDestPt, const Size& rDestSize ) const Point& rDestPt, const Size& rDestSize )
{ {
sal_uInt16 nPixel = (sal_uInt16) pOutDev->PixelToLogic( Size( 1, 1 ) ).Width(); sal_uInt16 nPixel = (sal_uInt16) pOutDev->PixelToLogic( Size( 1, 1 ) ).Width();
sal_uInt16 nPixelWidth = nPixel; sal_uInt16 nPixelWidth = nPixel;
Point aPoint( rDestPt.X() + nPixelWidth, rDestPt.Y() + nPixelWidth ); Point aPoint( rDestPt.X() + nPixelWidth, rDestPt.Y() + nPixelWidth );
Size aSize( rDestSize.Width() - ( nPixelWidth << 1 ), rDestSize.Height() - ( nPixelWidth << 1 ) ); Size aSize( rDestSize.Width() - ( nPixelWidth << 1 ), rDestSize.Height() - ( nPixelWidth << 1 ) );
bool bFilled = ( pBitmap != nullptr || pBitmapEx != nullptr || pFont != nullptr ); bool bFilled = ( pBitmap != nullptr || pBitmapEx != nullptr || pFont != nullptr );
...@@ -407,6 +407,28 @@ void Graphic::SetPrefMapMode( const MapMode& rPrefMapMode ) ...@@ -407,6 +407,28 @@ void Graphic::SetPrefMapMode( const MapMode& rPrefMapMode )
mpImpGraphic->ImplSetPrefMapMode( rPrefMapMode ); mpImpGraphic->ImplSetPrefMapMode( rPrefMapMode );
} }
basegfx::B2DSize Graphic::GetPPI() const
{
MapMode aMapMode = GetPrefMapMode();
double fWidthInches = ( GetPrefSize().Width() * aMapMode.GetUnitMultiplier() ) / 2540;
double fHeightInches = ( GetPrefSize().Height() * aMapMode.GetUnitMultiplier() ) / 2540;
double fPpiX = 0;
double fPpiY = 0;
if ( fWidthInches > 0 || fHeightInches > 0 ) // we don't want a divide by 0 situation
{
fPpiX = GetSizePixel().Width() / fWidthInches;
fPpiY = GetSizePixel().Height() / fHeightInches;
}
else
{
SAL_WARN("vcl", "PPI X is " << fPpiX << " and PPI Y is " << fPpiY << ": thus we are making this 0 DPI. This is unlikely.");
}
return basegfx::B2DSize( fPpiX, fPpiY );
}
Size Graphic::GetSizePixel( const OutputDevice* pRefDevice ) const Size Graphic::GetSizePixel( const OutputDevice* pRefDevice ) const
{ {
Size aRet; Size aRet;
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#include <tools/vcompat.hxx> #include <tools/vcompat.hxx>
#include <tools/debug.hxx> #include <tools/debug.hxx>
struct MapMode::ImplMapMode struct MapMode::ImplMapMode
{ {
sal_uLong mnRefCount; sal_uLong mnRefCount;
...@@ -208,6 +207,52 @@ void MapMode::SetScaleY( const Fraction& rScaleY ) ...@@ -208,6 +207,52 @@ void MapMode::SetScaleY( const Fraction& rScaleY )
mpImplMapMode->maScaleY.ReduceInaccurate(32); mpImplMapMode->maScaleY.ReduceInaccurate(32);
} }
double MapMode::GetUnitMultiplier() const
{
double nMul;
switch ( GetMapUnit() )
{
case MAP_PIXEL :
case MAP_SYSFONT :
case MAP_APPFONT :
case MAP_100TH_MM :
nMul = 1;
break;
case MAP_10TH_MM :
nMul = 10;
break;
case MAP_MM :
nMul = 100;
break;
case MAP_CM :
nMul = 1000;
break;
case MAP_1000TH_INCH :
nMul = 2.54;
break;
case MAP_100TH_INCH :
nMul = 25.4;
break;
case MAP_10TH_INCH :
nMul = 254;
break;
case MAP_INCH :
nMul = 2540;
break;
case MAP_TWIP :
nMul = 1.76388889;
break;
case MAP_POINT :
nMul = 35.27777778;
break;
default:
nMul = 1.0;
break;
}
return nMul;
}
MapMode& MapMode::operator=( const MapMode& rMapMode ) MapMode& MapMode::operator=( const MapMode& rMapMode )
{ {
DBG_ASSERT( rMapMode.mpImplMapMode->mnRefCount < 0xFFFFFFFE, "MapMode: RefCount overflow" ); DBG_ASSERT( rMapMode.mpImplMapMode->mnRefCount < 0xFFFFFFFE, "MapMode: RefCount overflow" );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment