-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapAccessor.cpp
208 lines (153 loc) · 5.8 KB
/
MapAccessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <math.h>
#include <iostream>
#include <QImage>
#include "MapAccessor.h"
#include "ScreenshotCollectionMapDB.h"
#define CHECK_MMA auto mDb2 = dynamic_cast<ScreenshotCollectionMapDB*>(&mDb); if(!mDb2) return
MapAccessor::MapAccessor(MapDB& m) : mDb(m)
{
CHECK_MMA;
if(!mDb2->imagesMaskFilename().isNull())
mImageMask = QImage(mDb2->rootDirectory() + "/" + mDb2->imagesMaskFilename());//.createAlphaMask(Qt::AutoColor);
}
void MapAccessor::getImagesToDraw(const MapDB::ImageSpaceCoord& mBottomLeftViewCorner, const MapDB::ImageSpaceCoord& mTopRightViewCorner, std::vector<ImageData> &images_to_draw) const
{
// For now, just dont be subtle: return all known images.
// To do:
// * only return the images that actually cross the supplied rectangle of coordinates
// * add a cache and load images n demand from the disk
const std::map<MapDB::ImageHandle,MapDB::RegisteredImage>& images_map = mDb.getFullListOfImages();
images_to_draw.clear();
for(auto it(images_map.begin());it!=images_map.end();++it)
{
ImageData id ;
id.W = it->second.W;
id.H = it->second.H;
id.bottom_left_corner = it->second.bottom_left_corner;
id.handle = it->first;
//id.directory = mDb.rootDirectory() ;
//id.filename = it->first ;
int tmp_W,tmp_H;
id.texture_data = getPixelDataForTextureUsage(it->first,tmp_W,tmp_H);
id.descriptors = it->second.descriptors;
images_to_draw.push_back(id);
}
}
bool MapAccessor::getImageParams(MapDB::ImageHandle h, MapDB::RegisteredImage& img)
{
return mDb.getImageParams(h,img) ;
}
QImage MapAccessor::getImageData(MapDB::ImageHandle h) const
{
return mDb.getImageData(h);
}
bool MapAccessor::findImagePixel(const MapDB::ImageSpaceCoord& is,const std::vector<MapAccessor::ImageData>& images,float& img_x,float& img_y,MapDB::ImageHandle & h)
{
for(int i=images.size()-1;i>=0;--i)
if( images[i].bottom_left_corner.x <= is.x
&& images[i].bottom_left_corner.x + images[i].W > is.x
&& images[i].bottom_left_corner.y <= is.y
&& images[i].bottom_left_corner.y + images[i].H > is.y )
{
h = images[i].handle;
img_x = is.x - images[i].bottom_left_corner.x;
img_y = images[i].H - 1 - (is.y - images[i].bottom_left_corner.y);
int X = (int)floor(img_x) ;
int Y = (int)floor(img_y) ;
if(X < 0 || X >= images[i].W || Y < 0 || Y >= images[i].H)
continue;
if(mImageMask.width() == images[i].W && mImageMask.height() == images[i].H && mImageMask.pixel(X,Y) == 0)
continue;
return true;
}
return false;
}
QImage MapAccessor::extractTile(const MapDB::ImageSpaceCoord& bottom_left, const MapDB::ImageSpaceCoord& top_right, int W, int H)
{
std::vector<ImageData> images ;
getImagesToDraw(bottom_left,top_right,images);
QImage img(W,H,QImage::Format_RGB32);
float img_x,img_y;
for(int i=0;i<W;++i)
for(int j=0;j<H;++j)
{
MapDB::ImageHandle handle ;
MapDB::ImageSpaceCoord c ;
c.x = bottom_left.x + i/(float)W*(top_right.x - bottom_left.x);
c.y = bottom_left.y + j/(float)H*(top_right.y - bottom_left.y);
if(! findImagePixel(c,images,img_x,img_y,handle))
{
img.setPixelColor(i,H-1-j,QRgb(0));
continue;
}
int tmp_W,tmp_H;
const unsigned char *data = getPixelData(handle,tmp_W,tmp_H);
img.setPixelColor(i,H-1-j,MapRegistration::interpolated_image_color_ABGR(data,tmp_W,tmp_H,img_x,img_y));
}
return img;
}
const unsigned char *MapAccessor::getPixelData(MapDB::ImageHandle h,int& W,int& H) const
{
auto it = mImageCache.find(h) ;
if(mImageCache.end() != it)
{
W = it->second.width();
H = it->second.height();
return it->second.bits() ;
}
QImage img = getImageData(h);
std::cerr << "Loading/caching image data for image handle " << uint32_t(h) << ", format=" << img.format() << std::endl;
W=img.width();
H=img.height();
mImageCache[h] = img;
return mImageCache[h].bits();
}
const unsigned char *MapAccessor::getPixelDataForTextureUsage(MapDB::ImageHandle h, int& W, int& H) const
{
auto it = mImageTextureCache.find(h) ;
if(mImageTextureCache.end() != it)
return it->second.bits() ;
std::cerr << "Loading/caching image data for image handle " << h << std::endl;
QImage image = mDb.getImageData(h);
if(mImageMask.width() == image.width() || mImageMask.height() == image.height())
image.setAlphaChannel(mImageMask);
// static bool toto=false;
// if(!toto)
// {
// image.save("toto.png");
// toto=true;
// }
mImageTextureCache[h] = image.scaled(1024,1024,Qt::IgnoreAspectRatio,Qt::SmoothTransformation).rgbSwapped() ;
W = H = 1024;
return mImageTextureCache[h].bits();
}
void MapAccessor::moveImage(MapDB::ImageHandle h,float delta_lon,float delta_lat)
{
CHECK_MMA;
mDb2->moveImage(h,delta_lon,delta_lat);
}
void MapAccessor::recomputeDescriptors(MapDB::ImageHandle h)
{
CHECK_MMA;
mDb2->recomputeDescriptors(h);
}
void MapAccessor::saveMap()
{
CHECK_MMA;
mDb2->save();
}
QString MapAccessor::fullPath(MapDB::ImageHandle h)
{
CHECK_MMA QString();
return mDb2->getImagePath(h);
}
void MapAccessor::placeImage(MapDB::ImageHandle h,const MapDB::ImageSpaceCoord& new_corner)
{
CHECK_MMA;
return mDb2->placeImage(h,new_corner);
}
void MapAccessor::setReferencePoint(MapDB::ImageHandle h,int point_x,int point_y)
{
CHECK_MMA;
mDb2->setReferencePoint(h,point_x,point_y);
}