/ src / map-tools.c
map-tools.c
  1  /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2  /*
  3    Gpredict: Real-time satellite tracking and orbit prediction program
  4  
  5    Copyright (C)  2013  Alexandru Csete, OZ9AEC.
  6  
  7    Authors: Alexandru Csete <oz9aec@gmail.com>
  8  
  9    Comments, questions and bugreports should be submitted via
 10    http://sourceforge.net/projects/gpredict/
 11    More details can be found at the project home page:
 12  
 13    http://gpredict.oz9aec.net/
 14  
 15    This program is free software; you can redistribute it and/or modify
 16    it under the terms of the GNU General Public License as published by
 17    the Free Software Foundation; either version 2 of the License, or
 18    (at your option) any later version.
 19    
 20    This program is distributed in the hope that it will be useful,
 21    but WITHOUT ANY WARRANTY; without even the implied warranty of
 22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 23    GNU General Public License for more details.
 24    
 25    You should have received a copy of the GNU General Public License
 26    along with this program; if not, visit http://www.fsf.org/
 27  */
 28  #include <math.h>
 29  #include <gdk-pixbuf/gdk-pixbuf.h>
 30  #ifdef HAVE_CONFIG_H
 31  #  include <build-config.h>
 32  #endif
 33  
 34  
 35  /*! \brief Rotate map to be centered around a specific longitude.
 36   *  \param in Input map centered around 0 degrees longitude.
 37   *  \param out Output map that will be centered around clon. Must have the same
 38   *             dimension and color space as the input map.
 39   *  \param clon The longitude that should be at center between -180 and 180 degrees (East positive).
 40   * 
 41   * This function will rotate the input map to be centered around the specified longitude.
 42   * The output map must be pre-allocated and have the sae width, height and colorspace as the input
 43   * map. The longitude parameter must be between -180 and 180 degrees with negative values meaning
 44   * West of Greenwich,positive values meaning East of Greenwich.
 45   */
 46  void map_tools_shift_center(GdkPixbuf *in, GdkPixbuf *out, float clon)
 47  {
 48      if (clon > 180.0)
 49          clon = 180.0;
 50      else if (clon < -180.0)
 51          clon = -180.0;
 52  
 53      int lon_x = round((clon+180.0) * gdk_pixbuf_get_width(in)/360.0);
 54      int img_w = gdk_pixbuf_get_width(in);
 55      int img_h = gdk_pixbuf_get_height(in);
 56      
 57      if (clon < 0.0) {
 58          /* copy left side to right */
 59          gdk_pixbuf_copy_area(in,
 60                               0,
 61                               0,
 62                               lon_x + img_w/2,
 63                               img_h,
 64                               out,
 65                               img_w - (lon_x + img_w/2),
 66                               0);
 67          /* copy right side to left */
 68          gdk_pixbuf_copy_area(in,
 69                               lon_x + img_w/2,
 70                               0,
 71                               img_w - (lon_x + img_w/2),
 72                               img_h,
 73                               out,
 74                               0,
 75                               0);
 76      }
 77      else if (clon > 0.0) {
 78          /* copy left side to right */
 79          gdk_pixbuf_copy_area(in,
 80                               0,
 81                               0,
 82                               lon_x - img_w/2,
 83                               img_h,
 84                               out,
 85                               img_w - (lon_x - img_w/2),
 86                               0);
 87          /* copy right side to left */
 88          gdk_pixbuf_copy_area(in,
 89                               lon_x - img_w/2,
 90                               0,
 91                               img_w - (lon_x - img_w/2),
 92                               img_h,
 93                               out,
 94                               0,
 95                               0);
 96      }
 97      else {
 98          /* no shifting of center */
 99          gdk_pixbuf_copy_area(in,
100                               0,
101                               0,
102                               img_w,
103                               img_h,
104                               out,
105                               0,
106                               0);
107      }   
108  }
109