RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
image_loader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * This file contains helper functions for loading images as maps.
32  *
33  * Author: Brian Gerkey
34  */
35 
36 #include <cstring>
37 #include <stdexcept>
38 
39 #include <stdlib.h>
40 #include <stdio.h>
41 
42 // We use SDL_image to load the image from disk
43 #include <SDL/SDL_image.h>
44 
46 #include <tf/tf.h>
47 
48 // compute linear index for given map coords
49 #define MAP_IDX(sx, i, j) ((sx) * (j) + (i))
50 
51 namespace map_server
52 {
53 
54 void
55 loadMapFromFile(nav_msgs::GetMap::Response* resp,
56  const char* fname, double res, bool negate,
57  double occ_th, double free_th, double* origin,
58  bool trinary)
59 {
60  SDL_Surface* img;
61 
62  unsigned char* pixels;
63  unsigned char* p;
64  unsigned char value;
65  int rowstride, n_channels, avg_channels;
66  unsigned int i,j;
67  int k;
68  double occ;
69  int alpha;
70  int color_sum;
71  double color_avg;
72 
73  // Load the image using SDL. If we get NULL back, the image load failed.
74  if(!(img = IMG_Load(fname)))
75  {
76  std::string errmsg = std::string("failed to open image file \"") +
77  std::string(fname) + std::string("\"");
78  throw std::runtime_error(errmsg);
79  }
80 
81  // Copy the image data into the map structure
82  resp->map.info.width = img->w;
83  resp->map.info.height = img->h;
84  resp->map.info.resolution = res;
85  resp->map.info.origin.position.x = *(origin);
86  resp->map.info.origin.position.y = *(origin+1);
87  resp->map.info.origin.position.z = 0.0;
88  tf::Quaternion q;
89  q.setRPY(0,0, *(origin+2));
90  resp->map.info.origin.orientation.x = q.x();
91  resp->map.info.origin.orientation.y = q.y();
92  resp->map.info.origin.orientation.z = q.z();
93  resp->map.info.origin.orientation.w = q.w();
94 
95  // Allocate space to hold the data
96  resp->map.data.resize(resp->map.info.width * resp->map.info.height);
97 
98  // Get values that we'll need to iterate through the pixels
99  rowstride = img->pitch;
100  n_channels = img->format->BytesPerPixel;
101 
102  if (trinary || n_channels == 1)
103  avg_channels = n_channels;
104  else
105  avg_channels = n_channels - 1;
106 
107  // Copy pixel data into the map structure
108  pixels = (unsigned char*)(img->pixels);
109  for(j = 0; j < resp->map.info.height; j++)
110  {
111  for (i = 0; i < resp->map.info.width; i++)
112  {
113  // Compute mean of RGB for this pixel
114  p = pixels + j*rowstride + i*n_channels;
115  color_sum = 0;
116  for(k=0;k<avg_channels;k++)
117  color_sum += *(p + (k));
118  color_avg = color_sum / (double)avg_channels;
119 
120  if (n_channels == 1)
121  alpha = 1;
122  else
123  alpha = *(p+n_channels-1);
124 
125  // If negate is true, we consider blacker pixels free, and whiter
126  // pixels free. Otherwise, it's vice versa.
127  if(negate)
128  occ = color_avg / 255.0;
129  else
130  occ = (255 - color_avg) / 255.0;
131 
132  // Apply thresholds to RGB means to determine occupancy values for
133  // map. Note that we invert the graphics-ordering of the pixels to
134  // produce a map with cell (0,0) in the lower-left corner.
135  if(occ > occ_th)
136  value = +100;
137  else if(occ < free_th)
138  value = 0;
139  else if(trinary || alpha < 1.0)
140  value = -1;
141  else {
142  double ratio = (occ - free_th) / (occ_th - free_th);
143  value = 99 * ratio;
144  }
145 
146  resp->map.data[MAP_IDX(resp->map.info.width,i,resp->map.info.height - j - 1)] = value;
147  }
148  }
149 
150  SDL_FreeSurface(img);
151 }
152 
153 }
void loadMapFromFile(nav_msgs::GetMap::Response *resp, const char *fname, double res, bool negate, double occ_th, double free_th, double *origin, bool trinary=true)
#define MAP_IDX(sx, i, j)
list origin
Definition: crop_map.py:62