43 #include <SDL/SDL_image.h>
49 #define MAP_IDX(sx, i, j) ((sx) * (j) + (i))
56 const char* fname,
double res,
bool negate,
57 double occ_th,
double free_th,
double*
origin,
62 unsigned char* pixels;
65 int rowstride, n_channels, avg_channels;
74 if(!(img = IMG_Load(fname)))
76 std::string errmsg = std::string(
"failed to open image file \"") +
77 std::string(fname) + std::string(
"\"");
78 throw std::runtime_error(errmsg);
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;
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();
96 resp->map.data.resize(resp->map.info.width * resp->map.info.height);
99 rowstride = img->pitch;
100 n_channels = img->format->BytesPerPixel;
102 if (trinary || n_channels == 1)
103 avg_channels = n_channels;
105 avg_channels = n_channels - 1;
108 pixels = (
unsigned char*)(img->pixels);
109 for(j = 0; j < resp->map.info.height; j++)
111 for (i = 0; i < resp->map.info.width; i++)
114 p = pixels + j*rowstride + i*n_channels;
116 for(k=0;k<avg_channels;k++)
117 color_sum += *(p + (k));
118 color_avg = color_sum / (double)avg_channels;
123 alpha = *(p+n_channels-1);
128 occ = color_avg / 255.0;
130 occ = (255 - color_avg) / 255.0;
137 else if(occ < free_th)
139 else if(trinary || alpha < 1.0)
142 double ratio = (occ - free_th) / (occ_th - free_th);
146 resp->map.data[
MAP_IDX(resp->map.info.width,i,resp->map.info.height - j - 1)] = value;
150 SDL_FreeSurface(img);
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)