[C++]将多张图片合并成大图
在《批量下载网络图片到本地》里写到了批量下载图片,其实那些图片是电子地图的散片,我想把这些图片合成一张大图,以便做成可以直接打开查看的地图甚至把印成纸质地图。当然这一切可以用图片处理软件一张一张地拼接,但地图散片动不动就是成百上千片,手动处理会很累,于是我选择了C++,代码很简单,同时这也得益于lodepng库的引入:
#include <vector>
#include <urlmon.h>
#include <string>
#include <iostream>
#include "loadpng.h"
using namespace std;
#pragma comment(lib, "urlmon.lib")
#define ROW 10
#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[])
{
decodeOneStep();
system("pause");
}
标签: C++
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: