2 * \file GraphicsImage.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Baruch Even <baruch.even@writeme.com>
7 * \author Angus Leeming <leeming@lyx.org>
8 * \author Herbert Voss <voss@lyx.org>
14 #pragma implementation
17 #include "GraphicsImage.h"
18 #include "GraphicsParams.h"
26 // This is to be connected to a function that will return a new
27 // instance of a viable derived class.
28 boost::function0<Image::ImagePtr> Image::newImage;
30 /// Return the list of loadable formats.
31 boost::function0<Image::FormatList> Image::loadableFormats;
34 std::pair<unsigned int, unsigned int>
35 Image::getScaledDimensions(Params const & params) const
37 lyxerr[Debug::GRAPHICS]
38 << "GraphicsImage::getScaledDImensions()"
39 << "\n\tparams.scale : " << params.scale
40 << "\n\tparams.width : " << params.width
41 << "\n\tparams.height : " << params.height
42 << "\n\tkeepLyXAspectRatio : " << params.keepLyXAspectRatio
44 if (params.width == 0 && params.height == 0 && params.scale == 0) {
45 // original size or scale/custom without any input
46 lyxerr[Debug::GRAPHICS]
47 << "\treturn with the original values!\n";
48 return std::make_pair(getWidth(), getHeight());
51 typedef unsigned int dimension;
54 if (params.scale != 0) {
55 // GraphicsParams::Scale
56 width = dimension(double(getWidth()) * params.scale / 100.0);
57 height = dimension(getHeight() * params.scale / 100.0);
58 return std::make_pair(width, height);
61 width = (params.width > 0) ? params.width : getWidth();
62 height = (params.height > 0) ? params.height : getHeight();
63 if (!params.keepLyXAspectRatio)
64 return std::make_pair(width, height);
66 // calculate aspect ratio
67 float const rw = getWidth();
68 float const rh = getHeight();
69 // there must be a width for the division
70 float const ratio = (rw > 0.001) ? rh/rw : 1.0;
71 lyxerr[Debug::GRAPHICS]
72 << "\tValue of LyXAspectRatio: " << ratio << std::endl;
73 // there are now four different cases
74 // w=0 & h=0 -> see above, no more possible at this place
75 // w>0 & h=0 -> calculate h
76 // w=0 & h>0 -> calculate w
77 // w>0 & h>0 -> the greatest difference to the original
78 // value becomes the same
79 if (params.width > 0 && params.height > 0) {
80 // both widths are given and keepAspectRatio, too
81 int const diff_width = abs(int(getWidth() - params.width));
82 int const diff_height= abs(int(getHeight() - params.height));
83 if (diff_width > diff_height)
84 height = int(ratio * params.width);
86 width = int(ratio * params.height);
87 return std::make_pair(width, height);
89 if (params.width > 0) {
91 height = int(ratio * params.width);
92 return std::make_pair(width, height);
94 if (params.height > 0) {
95 height = params.height;
96 width = int(ratio * params.height);
97 return std::make_pair(width, height);
99 // all other cases ... kind of paranoia :-)
100 return std::make_pair(getWidth(), getHeight());