www.pudn.com > dstile-0.2.rar > tilepack.cpp


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef deque TTileList;
typedef map TBlockMap;
TBlockMap bm;

void MakeDirs(char *pszPath) {
    char *psz, *pszTemp;
    
    pszTemp = new char[strlen(pszPath) + 1];
    strcpy(pszTemp, pszPath);
    for (psz = pszTemp; *psz; ++psz) if (*psz == '/') {
        *psz = 0;
        mkdir(pszTemp, 0755);
        *psz = '/';
    }
    delete pszTemp;
}

void ScanDatasetR(const char *path) {
    DIR *d;
    struct dirent *de;
    int l, x, y;
    string s;

    d = opendir(path);
    if (!d) return;
    while (de = readdir(d)) {
        if ((de->d_type == DT_DIR) && (de->d_name[0] != '.')) ScanDatasetR((string(path) + string(de->d_name) + "/").c_str());
        if (de->d_type == DT_REG) {
            s = de->d_name;
            if (sscanf(s.substr(0, 2).c_str(), "%x", &l) && sscanf(s.substr(2, 8).c_str(), "%x", &x) && sscanf(s.substr(10, 8).c_str(), "%x", &y)) {
		bm[(static_cast(l) << 50) + (static_cast(y >> 7) << 25) + (x >> 7)].push_back((x & 0x7f) + ((y & 0x7f) << 7));
            }
        }
    }
    closedir(d);
}

struct THeader {
    char id[8];
    int ver;
    char fill[4084];
} header;

unsigned int tab[32768];
unsigned char buf[1024 * 1024];

int main(int argc, char *argv[]) {
    TBlockMap::iterator it;
    TTileList::iterator i;
    TTileList *tl;
    unsigned long long ll;
    int l, bx, by, x, y;
    FILE *f, *ff;
    char fn[256];
    unsigned int pos;
    int i1, i2, i3, i4;
    int s;

    if (argc < 3) {
	fprintf(stderr, "RUN: %s src_path/ dst_path/\n", argv[0]);
	return 1;
    }
    memcpy(header.id, "TilePack", 8);
    header.ver = 0;
    ScanDatasetR(argv[1]);
    for (it = bm.begin(); it != bm.end(); ++it) {
	ll = it->first;
	l = it->first >> 50;
	bx = it->first & 0x1ffffff;
	by = (it->first >> 25) & 0x1ffffff;
	tl = &it->second;
	sort(tl->begin(), tl->end());
	memset(tab, 0, sizeof(tab));
	snprintf(fn, sizeof(fn), "%s%02x%07x%07x.tpk", argv[2], l, bx, by);
	printf("%s ", fn); fflush(stdout);
	f = fopen(fn, "rb");
	if (f) {
	    fclose(f);
	} else {
	f = fopen(fn, "wb");
	fwrite(&header, sizeof(header), 1, f);
	pos = 0;
	for (i = tl->begin(); i != tl->end(); ++i) {
	    tab[(*i) * 2] = pos;
	    fseek(f, pos * 512 + 4096 + 131072, SEEK_SET);
	    x = (bx << 7) + ((*i) & 0x7f);
	    y = (by << 7) + (((*i) >> 7) & 0x7f);
	    i1 = ((x >> 5) & 0x1F) | (((y >> 5) & 0x1F) << 5);
    	    i2 = ((x >> 10) & 0x1F) | (((y >> 10) & 0x1F) << 5);
	    i3 = ((x >> 15) & 0x1F) | (((y >> 15) & 0x1F) << 5);
    	    i4 = ((x >> 20) & 0x1F) | (((y >> 20) & 0x1F) << 5);
	    snprintf(fn, sizeof(fn), "%s%02x/%03x/%03x/%03x/%03x/%02x%08x%08x.jpg", argv[1], l, i4, i3, i2, i1, l, x, y);
	    printf("."); fflush(stdout);
	    ff = fopen(fn, "rb");
	    s = fread(buf, 1, sizeof(buf), ff);
	    fwrite(buf, 1, s, f);
	    tab[(*i) * 2 + 1] = s;
	    pos += ((s + 511) / 512);
	    fclose(ff);
	}
	fseek(f, 4096, SEEK_SET);
	fwrite(tab, sizeof(tab), 1, f);
	fclose(f);
	}
	printf("\n");
    }

    return 0;
}