前面写了《批量下载网络图片到本地》及《将多张图片合并成大图》,所以综合一下,完整的从电子地图网站把电子地图生成到本地的程序:
#include <vector>
#include <urlmon.h>
#include <string>
#include <iostream>
#include "loadpng.h"
using namespace std;
#pragma comment(lib, "urlmon.lib")
#define ROW 12
#define COL 9
#define PIC_WIDTH 256
#define PIC_HEIGHT 256
void decodeOneStep()
{
std::vector<unsigned char> image_ret(ROW*COL*PIC_WIDTH*PIC_HEIGHT*4);
std::vector<unsigned char> image[ROW][COL];
unsigned width = 0;
unsigned height = 0;
unsigned error = 0;
unsigned int idx_row;
unsigned int idx_col;
char filename[100];
unsigned int idx_row_tmp;
unsigned int idx_col_tmp;
unsigned int x;
unsigned int y;
unsigned int pos;
const char *out = "out.png";
for (idx_row = 0; idx_row < ROW; idx_row++) {
for (idx_col = 0; idx_col < COL; idx_col++) {
sprintf(filename, "map/%d_%d.png", idx_col, (ROW-idx_row-1));
error = lodepng::decode(image[idx_row][idx_col], width, height, filename);
printf("file:%s w:%d h:%d\n", filename, width, height);
if (error) {
printf("decoder error:%d %s\n", error, lodepng_error_text(error));
}
if (width != PIC_WIDTH || height != PIC_HEIGHT) {
printf(" error size of pic \n");
}
pos = 0;
for (idx_row_tmp = 0; idx_row_tmp < height; idx_row_tmp++) {
for (idx_col_tmp = 0; idx_col_tmp < width; idx_col_tmp++) {
x = idx_col * PIC_WIDTH + idx_col_tmp;
y = idx_row * PIC_HEIGHT + idx_row_tmp;
image_ret[(y * PIC_WIDTH * COL + x) * 4] = image[idx_row][idx_col][(pos*4)];
image_ret[(y * PIC_WIDTH * COL + x) * 4 + 1] = image[idx_row][idx_col][(pos * 4 + 1)];
image_ret[(y * PIC_WIDTH * COL + x) * 4 + 2] = image[idx_row][idx_col][(pos * 4 + 2)];
image_ret[(y * PIC_WIDTH * COL + x) * 4 + 3] = image[idx_row][idx_col][(pos * 4 + 3)];
pos++;
}
}
}
}
printf("image ******* len:%u\n", image_ret.size());
lodepng::encode(out, image_ret, PIC_WIDTH * COL, PIC_HEIGHT * ROW);
}
int main(int argc, char* argv[])
{
int i, j;
string url;
string file;
char tmp_url[1024];
char tmp_file[100];
wchar_t *buffer_url;
wchar_t *buffer_file;
size_t len;
int nmlen;
for (i = 0; i < COL; i++) {
for (j = 0; j < ROW; j++) {
sprintf(tmp_url, "http://img.wikia.nocookie.net/intmap_tile_set_5360/4/%d/%d.png", i,j);
sprintf(tmp_file, "map/%d_%d.png", i, j);
url = tmp_url;
file = tmp_file;
len = url.length();
nmlen = MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, NULL, 0);
buffer_url = new wchar_t[nmlen];
MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, buffer_url, nmlen);
len = file.length();
nmlen = MultiByteToWideChar(CP_ACP, 0, file.c_str(), len + 1, NULL, 0);
buffer_file = new wchar_t[nmlen];
MultiByteToWideChar(CP_ACP, 0, file.c_str(), len + 1, buffer_file, nmlen);
URLDownloadToFile(NULL, buffer_url, buffer_file, 0, 0);
delete[]buffer_url;
delete[]buffer_file;
cout << url << " ->>>" << file << endl;
}
}
decodeOneStep();
system("pause");
return 0;
}