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 
45 // Use Bullet's Quaternion object to create one from Euler angles
46 #include <LinearMath/btQuaternion.h>
47 
49 
50 // compute linear index for given map coords
51 #define MAP_IDX(sx, i, j) ((sx) * (j) + (i))
52 
53 namespace map_server
54 {
55 
56 void
57 loadMapFromFile(nav_msgs::GetMap::Response* resp,
58  const char* fname, double res, bool negate,
59  double occ_th, double free_th, double* origin,
60  MapMode mode)
61 {
62  SDL_Surface* img;
63 
64  unsigned char* pixels;
65  unsigned char* p;
66  unsigned char value;
67  int rowstride, n_channels, avg_channels;
68  unsigned int i,j;
69  int k;
70  double occ;
71  int alpha;
72  int color_sum;
73  double color_avg;
74 
75  // Load the image using SDL. If we get NULL back, the image load failed.
76  if(!(img = IMG_Load(fname)))
77  {
78  std::string errmsg = std::string("failed to open image file \"") +
79  std::string(fname) + std::string("\": ") + IMG_GetError();
80  throw std::runtime_error(errmsg);
81  }
82 
83  // Copy the image data into the map structure
84  resp->map.info.width = img->w;
85  resp->map.info.height = img->h;
86  resp->map.info.resolution = res;
87  resp->map.info.origin.position.x = *(origin);
88  resp->map.info.origin.position.y = *(origin+1);
89  resp->map.info.origin.position.z = 0.0;
90  btQuaternion q;
91  // setEulerZYX(yaw, pitch, roll)
92  q.setEulerZYX(*(origin+2), 0, 0);
93  resp->map.info.origin.orientation.x = q.x();
94  resp->map.info.origin.orientation.y = q.y();
95  resp->map.info.origin.orientation.z = q.z();
96  resp->map.info.origin.orientation.w = q.w();
97 
98  // Allocate space to hold the data
99  resp->map.data.resize(resp->map.info.width * resp->map.info.height);
100 
101  // Get values that we'll need to iterate through the pixels
102  rowstride = img->pitch;
103  n_channels = img->format->BytesPerPixel;
104 
105  // NOTE: Trinary mode still overrides here to preserve existing behavior.
106  // Alpha will be averaged in with color channels when using trinary mode.
107  if (mode==TRINARY || !img->format->Amask)
108  avg_channels = n_channels;
109  else
110  avg_channels = n_channels - 1;
111 
112  // Copy pixel data into the map structure
113  pixels = (unsigned char*)(img->pixels);
114  for(j = 0; j < resp->map.info.height; j++)
115  {
116  for (i = 0; i < resp->map.info.width; i++)
117  {
118  // Compute mean of RGB for this pixel
119  p = pixels + j*rowstride + i*n_channels;
120  color_sum = 0;
121  for(k=0;k<avg_channels;k++)
122  color_sum += *(p + (k));
123  color_avg = color_sum / (double)avg_channels;
124 
125  if (n_channels == 1)
126  alpha = 1;
127  else
128  alpha = *(p+n_channels-1);
129 
130  if(negate)
131  color_avg = 255 - color_avg;
132 
133  if(mode==RAW){
134  value = color_avg;
135  resp->map.data[MAP_IDX(resp->map.info.width,i,resp->map.info.height - j - 1)] = value;
136  continue;
137  }
138 
139 
140  // If negate is true, we consider blacker pixels free, and whiter
141  // pixels free. Otherwise, it's vice versa.
142  occ = (255 - color_avg) / 255.0;
143 
144  // Apply thresholds to RGB means to determine occupancy values for
145  // map. Note that we invert the graphics-ordering of the pixels to
146  // produce a map with cell (0,0) in the lower-left corner.
147  if(occ > occ_th)
148  value = +100;
149  else if(occ < free_th)
150  value = 0;
151  else if(mode==TRINARY || alpha < 1.0)
152  value = -1;
153  else {
154  double ratio = (occ - free_th) / (occ_th - free_th);
155  value = 99 * ratio;
156  }
157 
158  resp->map.data[MAP_IDX(resp->map.info.width,i,resp->map.info.height - j - 1)] = value;
159  }
160  }
161 
162  SDL_FreeSurface(img);
163 }
164 
165 }
MapMode
Definition: image_loader.h:52
#define MAP_IDX(sx, i, j)
void loadMapFromFile(nav_msgs::GetMap::Response *resp, const char *fname, double res, bool negate, double occ_th, double free_th, double *origin, MapMode mode=TRINARY)


map_server
Author(s): Brian Gerkey, Tony Pratkanis, contradict@gmail.com
autogenerated on Thu Jan 21 2021 04:05:35