NCBI C++ ToolKit
phylo_image_grabber.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: phylo_image_grabber.cpp 33050 2015-05-19 16:06:18Z falkrb $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Bob Falk
27  *
28  * File Description:
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
35 
36 #include <util/image/image.hpp>
37 
38 
39 
41 
43 : m_Pane(pane)
44 , m_TileViewportWidth(0)
45 , m_TileViewportHeight(0)
46 {
48 }
49 
51 {
52  // Save some current rendering values we want to change
57 
58  // Update rendering options. It would be better if these margins explicitly
59  // included any boundary widths from subtree boundaries that are included
60  // in the graph. But normally those boundaries will be pretty narrow and
61  // this should handle it.
62  s.SetMargins(20.0, 20.0, 20.0, 20.0);
63 
64  m_ModelViewScaler = 1.0f;
65  m_GuideWidthX = 0;
66  m_GuideWidthY = 0;
67 
68  int viewport_x, viewport_y;
69 
70  // If the user doesn't ask for a specific aspect ratio, pick an optimal one.
71  if (m_TileAspectRatio < 0.0f) {
72  float partitions_ratio = ((float)(m_ImagePartitions.Y()+1))/
73  (float)(m_ImagePartitions.X()+1);
74  float tile_aspect = m_ImageAspectRatio*partitions_ratio;
75 
76  // Set the size of the target-image to which we will render.
77  // If we are not scaling in x or y, ignore aspect ratio (tile into squre images)
81  }
82  else {
83  if (tile_aspect > 1.0f) {
85  m_TileViewportHeight = (int)(((float)m_TileViewportWidth)/tile_aspect);
86  }
87  else {
89  m_TileViewportWidth = (int)(((float)m_TileViewportHeight)*tile_aspect);
90  }
91  }
92 
93  if (m_GuidesEnabled) {
98  }
99 
100  // Set the size of the full viewport (sum of all tiles).
101  viewport_x = m_TileViewportWidth * (m_ImagePartitions.X()+1);
102  viewport_y = m_TileViewportHeight * (m_ImagePartitions.Y()+1);
103  }
104  else {
105  // Since tiles have a fixed ratio in this section of code,
106  // the partitioned aspect is the ratio of the tiles summed
107  // together (ratio of tiled window)
108  float partition_ratio = ((float)(m_ImagePartitions.X()+1))/
109  (float)(m_ImagePartitions.Y()+1);
110  float partitioned_aspect = m_TileAspectRatio*partition_ratio;
111 
112  // Get the size of the individual tiles (this is the size of the
113  // target image to be saved). One dimension is the target image
114  // size, and the other (smaller) dimension is sized according to
115  // aspect ratio.
116  if (m_TileAspectRatio > 1.0f) {
119 
120  // Subtract space, if requested, for print guidelines. to keep imag
121  // proportional, we will subtract the base amount from the shorter
122  // dimension and a proportional (larger) amount from the long dimension.
123  if (m_GuidesEnabled) {
126  }
127  }
128  else {
131 
132  // Subtract space, if requested, for print guidelines. to keep imag
133  // proportional, we will subtract the base amount from the shorter
134  // dimension and a proportional (larger) amount from the long dimension.
135  if (m_GuidesEnabled) {
137  m_GuideWidthY = (int)(((float)m_GuideWidth)*(1.0f/m_TileAspectRatio));
138  }
139  }
140 
141  if (m_GuidesEnabled) {
144  }
145 
146  // Get size of viewport containing all tiles (sum of tiles)
147  viewport_y = m_TileViewportHeight * (m_ImagePartitions.Y()+1);
148  viewport_x = m_TileViewportWidth * (m_ImagePartitions.X()+1);
149 
150  // for a fixed aspect ratio, one of the dimensions is going
151  // to be longer than it needs to be (will have blank space
152  // at the end). The scaler is the ratio of the actual (extended)
153  // viewport size to the size it would be if it matched the aspect
154  // ratio of the on-screen tree image.
155 
156  // If we are not scaling in x or y, though, just tile the images evenly (given the fixed per-image
157  // aspect ration) along the x or y direction (so do nothing here in that case)
159  if (partitioned_aspect > m_ImageAspectRatio) {
160  m_ModelViewScaler.X() = ((float)viewport_x)/
161  (((float)m_ImageAspectRatio)*(float)viewport_y);
162  viewport_x = (int)(m_ImageAspectRatio*(float)viewport_y);
163  }
164  else {
165  m_ModelViewScaler.Y() = ((float)viewport_y)/
166  (((float)viewport_x)/m_ImageAspectRatio);
167  viewport_y = (int)(((float)viewport_x)/m_ImageAspectRatio);
168  }
169  }
170  }
171 
172  // We need the correct (sum of all tiles) viewport size in order
173  // to correctly compute the size of the model limits rect
174  CGlRect<int> vp(0, 0, viewport_x, viewport_y);
175  m_Pane.GetPane().SetViewport(vp);
176 
177  // Compute the size of the model limits rectangle
179 }
180 
182 {
184 
185  /// Reset rendering options we may have updated.
189 }
190 
193  CVect2<int> index,
194  CVect2<size_t>& capture_size)
195 {
196  CGlRect< TModelUnit> pane_rect, full_rect;
197 
198  full_rect = m_Pane.GetPane().GetModelLimitsRect();
199 
200  int vp_width;
201  int vp_height;
202 
203  double width, height, left, bottom;
204 
205  vp_width = m_TileViewportWidth;
206  vp_height = m_TileViewportHeight;
207 
208  width = ceil(((double)full_rect.Width())/(double)(partitions.X()+1)) *
210  height = ceil(((double)full_rect.Height())/(double)(partitions.Y()+1)) *
212 
213 
214  // We add one pixel-equivalent in each direction here
215  // to ensure overlap.
216  double one_pix_x = width/(double)vp_width;
217  double one_pix_y = height/(double)vp_height;
218 
219  left = full_rect.Left() + ((double)(index.X()))*width;
220  bottom = full_rect.Top() - (index.Y() + 1)*height;
221  pane_rect.Init(left - one_pix_x,
222  bottom - one_pix_y,
223  left + width + one_pix_x,
224  bottom + height + one_pix_y);
225 
226  // Set viewport size to image size and render image
229  vp_width + m_GuideWidthX,
230  vp_height + m_GuideWidthY);
231 
232  m_Pane.GetPane().SetViewport(vp);
233  m_Pane.GetPane().SetVisibleRect(pane_rect);
234 
235  // Only render the scale marker on last image of tree. Determine
236  // this using index and number of partitions.
238  if (index.Y() == partitions.Y() && index.X() == partitions.X())
240  else
242 
244 
245  // Don't change renderstate for tree object.
247 
248  capture_size.X() = vp_width + 2*m_GuideWidthX;
249  capture_size.Y() = vp_height + 2*m_GuideWidthY;
250 
251  return eSuccess;
252 }
253 
254 
CGlRect reprsents rectangle in the coordinate system with origin located in the left bottom corner.
Definition: glrect.hpp:52
int m_TileViewportWidth
Width and height of individual images being saved.
CPhyloTreePane & m_Pane
Pane display canvas for the tree we are saving.
CPhyloTreeScheme::TLabelTruncation m_LabelsTruncated
eCaptureResult x_CaptureImage(CVect2< int > partitions, CVect2< int > index, CVect2< size_t > &capture_size)
Capture the image at tile position 'index'.
CPhyloImageGrabber(CPhyloTreePane &pane)
void x_BeginCaptures(int buffer_size)
Called to setup values before image capturing starts.
CGlPane m_PreviousGlPane
We change some rendering options.
void x_EndCaptures()
Called to restore values (in pane) when capturing stops.
CVect2< float > m_ModelViewScaler
Ratio scalers for the drawing area that maps to the images.
class CPhyloTreePane
CGlPane & GetPane()
IPhyloTreeRender * GetCurrRenderer(void)
TLabelTruncation & GetLabelTruncation()
void GetMargins(GLdouble &left, GLdouble &top, GLdouble &right, GLdouble &bottom) const
TZoomBehavior GetZoomBehavior(const string &renderer)
void SetMargins(const GLdouble &left, const GLdouble &top, const GLdouble &right, const GLdouble &bottom)
void SetLabelTruncation(TLabelTruncation lt)
bool m_GuidesEnabled
If true, printing guides (like where to cut paper) will be added to image edges.
int m_GuideWidthX
If we are making space for printing guides (margins) on the sides, those margins may have different s...
CVect2< int > m_ImagePartitions
Number of partitions (tilings) in x and 1.
float m_ImageAspectRatio
Aspect ratio of image from which we are generating tiles.
static const int m_GuideWidth
Width of printing guides, if enabled.
float m_TileAspectRatio
Ratio of width to height (w/h) in captured images.
void SetRenderScale(bool s)
virtual string GetDescription(void)=0
bool GetRenderScale() const
CPhyloTreeScheme & GetScheme(void)
T & X()
Definition: vect2.hpp:107
T & Y()
Definition: vect2.hpp:109
T Height() const
Definition: glrect.hpp:90
void SetViewport(const TVPRect &R)
Definition: glpane.cpp:96
void Init()
Definition: glrect.hpp:62
T Top() const
Definition: glrect.hpp:84
T Width() const
Definition: glrect.hpp:86
TModelRect & GetModelLimitsRect(void)
Definition: glpane.hpp:347
T Left() const
Definition: glrect.hpp:81
void SetVisibleRect(const TModelRect &R)
Definition: glpane.cpp:113
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
static int buffer_size
Definition: pcretest.c:1050
Modified on Thu Feb 22 17:08:46 2024 by modify_doxy.py rev. 669887