14 #include <QtCore/QRect>
15 #include <QApplication>
16 #include <QDesktopWidget>
25 #include <ApplicationServices/ApplicationServices.h>
26 #include <IOKit/graphics/IOGraphicsLib.h>
31 using namespace Okular;
38 switch ( orientation )
41 ret = QRect( width - source.y() - source.height(), source.x(),
42 source.height(), source.width() );
45 ret = QRect( width - source.x() - source.width(), height - source.y() - source.height(),
46 source.width(), source.height() );
49 ret = QRect( source.y(), height - source.x() - source.width(),
50 source.height(), source.width() );
64 return QX11Info::appDpiX();
69 return QX11Info::appDpiY();
74 const QDesktopWidget* w = QApplication::desktop();
75 return (
double(w->width()) * 25.4) / double(w->widthMM());
80 const QDesktopWidget* w = QApplication::desktop();
81 return (
double(w->height()) * 25.4) / double(w->heightMM());
84 #elif defined(Q_WS_MAC)
89 static int GetIntFromDictionaryForKey( CFDictionaryRef desc, CFStringRef key )
93 if ( (value = (CFNumberRef)CFDictionaryGetValue(desc, key)) == NULL || CFGetTypeID(value) != CFNumberGetTypeID())
95 CFNumberGetValue(value, kCFNumberIntType, &num);
99 static CGDisplayErr GetDisplayDPI( CFDictionaryRef displayModeDict, CGDirectDisplayID displayID,
100 double *horizontalDPI,
double *verticalDPI )
102 CGDisplayErr err = kCGErrorFailure;
103 io_connect_t displayPort;
104 CFDictionaryRef displayDict;
107 displayPort = CGDisplayIOServicePort( displayID );
108 if ( displayPort != MACH_PORT_NULL )
111 displayDict = IODisplayCreateInfoDictionary(displayPort, 0);
112 if ( displayDict != NULL )
114 const double mmPerInch = 25.4;
115 double horizontalSizeInInches =
116 (double)GetIntFromDictionaryForKey(displayDict,
117 CFSTR(kDisplayHorizontalImageSize)) / mmPerInch;
118 double verticalSizeInInches =
119 (double)GetIntFromDictionaryForKey(displayDict,
120 CFSTR(kDisplayVerticalImageSize)) / mmPerInch;
123 CFRelease(displayDict);
128 (double)GetIntFromDictionaryForKey( displayModeDict, kCGDisplayWidth )
129 / horizontalSizeInInches;
130 *verticalDPI = (double)GetIntFromDictionaryForKey( displayModeDict,
131 kCGDisplayHeight ) / verticalSizeInInches;
132 err = CGDisplayNoErr;
141 CGDisplayErr err = GetDisplayDPI( CGDisplayCurrentMode(kCGDirectMainDisplay),
142 kCGDirectMainDisplay,
145 return err == CGDisplayNoErr ? x : 72.0;
151 CGDisplayErr err = GetDisplayDPI( CGDisplayCurrentMode(kCGDirectMainDisplay),
152 kCGDirectMainDisplay,
155 return err == CGDisplayNoErr ? y : 72.0;
171 return QDesktopWidget().physicalDpiX();
176 return QDesktopWidget().physicalDpiY();
191 return ( argb & 0xFFFFFF ) == 0xFFFFFF;
199 int width = image->width();
200 int height = image->height();
201 int left, top, bottom, right, x, y;
209 for ( top = 0; top < height; ++top )
210 for ( x = 0; x < width; ++x )
211 if ( !
isWhite( image->pixel( x, top ) ) )
218 for ( bottom = height-1; bottom >= top; --bottom )
219 for ( x = width-1; x >= 0; --x )
220 if ( !
isWhite( image->pixel( x, bottom ) ) )
230 for ( y = top; y <= bottom && ( left > 0 || right < width-1 ); ++y )
232 for ( x = 0; x < left; ++x )
233 if ( !
isWhite( image->pixel( x, y ) ) )
235 for ( x = width-1; x > right+1; --x )
236 if ( !
isWhite( image->pixel( x, y ) ) )
240 NormalizedRect bbox( QRect( left, top, ( right - left + 1), ( bottom - top + 1 ) ),
241 image->width(), image->height() );
244 kDebug() <<
"Computed bounding box" << bbox <<
"in" << time.elapsed() <<
"ms";
252 QByteArray buffer( 65536,
'\0' );
255 while ( ( read = from->read( buffer.data(), buffer.size() ) ) > 0 )
257 written = to->write( buffer.constData(), read );
258 if ( read != written )
static double dpiY()
Return the vertical DPI of the main display.
static double realDpiX()
Return the real horizontal DPI of the main display.
NormalizedRect is a helper class which stores the coordinates of a normalized rect, which is a rectangle of.
static QRect rotateRect(const QRect &source, int width, int height, int orientation)
Rotate the rect source in the area width x height with the specified orientation .
static double dpiX()
Return the horizontal DPI of the main display.
static NormalizedRect imageBoundingBox(const QImage *image)
Compute the smallest rectangle that contains all non-white pixels in image), in normalized [0...
void copyQIODevice(QIODevice *from, QIODevice *to)
static bool isWhite(QRgb argb)
static double realDpiY()
Return the real vertical DPI of the main display.