• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegraphics API Reference
  • KDE Home
  • Contact Us
 

okular

  • sources
  • kde-4.12
  • kdegraphics
  • okular
  • core
utils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Luigi Toscano <luigi.toscano@tiscali.it> *
3  * Copyright (C) 2008 by Pino Toscano <pino@kde.org> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ***************************************************************************/
10 
11 #include "utils.h"
12 #include "utils_p.h"
13 
14 #include <QtCore/QRect>
15 #include <QApplication>
16 #include <QDesktopWidget>
17 #include <QImage>
18 #include <QIODevice>
19 
20 #ifdef Q_WS_X11
21 #include <QX11Info>
22 #endif
23 
24 #ifdef Q_WS_MAC
25 #include <ApplicationServices/ApplicationServices.h>
26 #include <IOKit/graphics/IOGraphicsLib.h>
27 #endif
28 
29 
30 
31 using namespace Okular;
32 
33 QRect Utils::rotateRect( const QRect & source, int width, int height, int orientation )
34 {
35  QRect ret;
36 
37  // adapt the coordinates of the boxes to the rotation
38  switch ( orientation )
39  {
40  case 1:
41  ret = QRect( width - source.y() - source.height(), source.x(),
42  source.height(), source.width() );
43  break;
44  case 2:
45  ret = QRect( width - source.x() - source.width(), height - source.y() - source.height(),
46  source.width(), source.height() );
47  break;
48  case 3:
49  ret = QRect( source.y(), height - source.x() - source.width(),
50  source.height(), source.width() );
51  break;
52  case 0: // no modifications
53  default: // other cases
54  ret = source;
55  }
56 
57  return ret;
58 }
59 
60 #if defined(Q_WS_X11)
61 
62 double Utils::dpiX()
63 {
64  return QX11Info::appDpiX();
65 }
66 
67 double Utils::dpiY()
68 {
69  return QX11Info::appDpiY();
70 }
71 
72 double Utils::realDpiX()
73 {
74  const QDesktopWidget* w = QApplication::desktop();
75  return (double(w->width()) * 25.4) / double(w->widthMM());
76 }
77 
78 double Utils::realDpiY()
79 {
80  const QDesktopWidget* w = QApplication::desktop();
81  return (double(w->height()) * 25.4) / double(w->heightMM());
82 }
83 
84 #elif defined(Q_WS_MAC)
85  /*
86  * Code copied from http://developer.apple.com/qa/qa2001/qa1217.html
87  */
88  // Handy utility function for retrieving an int from a CFDictionaryRef
89  static int GetIntFromDictionaryForKey( CFDictionaryRef desc, CFStringRef key )
90  {
91  CFNumberRef value;
92  int num = 0;
93  if ( (value = (CFNumberRef)CFDictionaryGetValue(desc, key)) == NULL || CFGetTypeID(value) != CFNumberGetTypeID())
94  return 0;
95  CFNumberGetValue(value, kCFNumberIntType, &num);
96  return num;
97  }
98 
99  static CGDisplayErr GetDisplayDPI( CFDictionaryRef displayModeDict, CGDirectDisplayID displayID,
100  double *horizontalDPI, double *verticalDPI )
101  {
102  CGDisplayErr err = kCGErrorFailure;
103  io_connect_t displayPort;
104  CFDictionaryRef displayDict;
105 
106  // Grab a connection to IOKit for the requested display
107  displayPort = CGDisplayIOServicePort( displayID );
108  if ( displayPort != MACH_PORT_NULL )
109  {
110  // Find out what IOKit knows about this display
111  displayDict = IODisplayCreateInfoDictionary(displayPort, 0);
112  if ( displayDict != NULL )
113  {
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;
121 
122  // Make sure to release the dictionary we got from IOKit
123  CFRelease(displayDict);
124 
125  // Now we can calculate the actual DPI
126  // with information from the displayModeDict
127  *horizontalDPI =
128  (double)GetIntFromDictionaryForKey( displayModeDict, kCGDisplayWidth )
129  / horizontalSizeInInches;
130  *verticalDPI = (double)GetIntFromDictionaryForKey( displayModeDict,
131  kCGDisplayHeight ) / verticalSizeInInches;
132  err = CGDisplayNoErr;
133  }
134  }
135  return err;
136  }
137 
138 double Utils::dpiX()
139 {
140  double x,y;
141  CGDisplayErr err = GetDisplayDPI( CGDisplayCurrentMode(kCGDirectMainDisplay),
142  kCGDirectMainDisplay,
143  &x, &y );
144 
145  return err == CGDisplayNoErr ? x : 72.0;
146 }
147 
148 double Utils::dpiY()
149 {
150  double x,y;
151  CGDisplayErr err = GetDisplayDPI( CGDisplayCurrentMode(kCGDirectMainDisplay),
152  kCGDirectMainDisplay,
153  &x, &y );
154 
155  return err == CGDisplayNoErr ? y : 72.0;
156 }
157 
158 double Utils::realDpiX()
159 {
160  return dpiX();
161 }
162 
163 double Utils::realDpiY()
164 {
165  return dpiY();
166 }
167 #else
168 
169 double Utils::dpiX()
170 {
171  return QDesktopWidget().physicalDpiX();
172 }
173 
174 double Utils::dpiY()
175 {
176  return QDesktopWidget().physicalDpiY();
177 }
178 
179 double Utils::realDpiX()
180 {
181  return dpiX();
182 }
183 
184 double Utils::realDpiY()
185 {
186  return dpiY();
187 }
188 #endif
189 
190 inline static bool isWhite( QRgb argb ) {
191  return ( argb & 0xFFFFFF ) == 0xFFFFFF; // ignore alpha
192 }
193 
194 NormalizedRect Utils::imageBoundingBox( const QImage * image )
195 {
196  if ( !image )
197  return NormalizedRect();
198 
199  int width = image->width();
200  int height = image->height();
201  int left, top, bottom, right, x, y;
202 
203 #ifdef BBOX_DEBUG
204  QTime time;
205  time.start();
206 #endif
207 
208  // Scan pixels for top non-white
209  for ( top = 0; top < height; ++top )
210  for ( x = 0; x < width; ++x )
211  if ( !isWhite( image->pixel( x, top ) ) )
212  goto got_top;
213  return NormalizedRect( 0, 0, 0, 0 ); // the image is blank
214 got_top:
215  left = right = x;
216 
217  // Scan pixels for bottom non-white
218  for ( bottom = height-1; bottom >= top; --bottom )
219  for ( x = width-1; x >= 0; --x )
220  if ( !isWhite( image->pixel( x, bottom ) ) )
221  goto got_bottom;
222  Q_ASSERT( 0 ); // image changed?!
223 got_bottom:
224  if ( x < left )
225  left = x;
226  if ( x > right )
227  right = x;
228 
229  // Scan for leftmost and rightmost (we already found some bounds on these):
230  for ( y = top; y <= bottom && ( left > 0 || right < width-1 ); ++y )
231  {
232  for ( x = 0; x < left; ++x )
233  if ( !isWhite( image->pixel( x, y ) ) )
234  left = x;
235  for ( x = width-1; x > right+1; --x )
236  if ( !isWhite( image->pixel( x, y ) ) )
237  right = x;
238  }
239 
240  NormalizedRect bbox( QRect( left, top, ( right - left + 1), ( bottom - top + 1 ) ),
241  image->width(), image->height() );
242 
243 #ifdef BBOX_DEBUG
244  kDebug() << "Computed bounding box" << bbox << "in" << time.elapsed() << "ms";
245 #endif
246 
247  return bbox;
248 }
249 
250 void Okular::copyQIODevice( QIODevice *from, QIODevice *to )
251 {
252  QByteArray buffer( 65536, '\0' );
253  qint64 read = 0;
254  qint64 written = 0;
255  while ( ( read = from->read( buffer.data(), buffer.size() ) ) > 0 )
256  {
257  written = to->write( buffer.constData(), read );
258  if ( read != written )
259  break;
260  }
261 }
Okular::Utils::dpiY
static double dpiY()
Return the vertical DPI of the main display.
Definition: utils.cpp:174
Okular::Utils::realDpiX
static double realDpiX()
Return the real horizontal DPI of the main display.
Definition: utils.cpp:179
Okular::NormalizedRect
NormalizedRect is a helper class which stores the coordinates of a normalized rect, which is a rectangle of.
Definition: area.h:105
Okular::Utils::rotateRect
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 .
Definition: utils.cpp:33
Okular::Utils::dpiX
static double dpiX()
Return the horizontal DPI of the main display.
Definition: utils.cpp:169
utils.h
Okular::Utils::imageBoundingBox
static NormalizedRect imageBoundingBox(const QImage *image)
Compute the smallest rectangle that contains all non-white pixels in image), in normalized [0...
Definition: utils.cpp:194
Okular::copyQIODevice
void copyQIODevice(QIODevice *from, QIODevice *to)
Definition: utils.cpp:250
utils_p.h
isWhite
static bool isWhite(QRgb argb)
Definition: utils.cpp:190
Okular::Utils::realDpiY
static double realDpiY()
Return the real vertical DPI of the main display.
Definition: utils.cpp:184
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:45:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okular

Skip menu "okular"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegraphics API Reference

Skip menu "kdegraphics API Reference"
  •     libkdcraw
  •     libkexiv2
  •     libkipi
  •     libksane
  • okular

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal