Example #1
0
static void sample_subtile(texgz_tex_t* src,
                           pak_file_t* pak,
                           int x, int y,
                           int i, int j,
                           double latT, double lonL,
                           double latB, double lonR)
{
	assert(src);
	assert(pak);
	LOGD("debug x=%i, y=%i, i=%i, j=%i, latT=%lf, lonL=%lf, latB=%lf, lonR=%lf",
	     x, y, i, j, latT, lonL, latB, lonR);

	// create dst
	texgz_tex_t* dst = texgz_tex_new(SUBTILE_SIZE, SUBTILE_SIZE,
	                                 SUBTILE_SIZE, SUBTILE_SIZE,
	                                 TEXGZ_UNSIGNED_BYTE,
	                                 TEXGZ_RGB, NULL);
	if(dst == NULL)
	{
		return;
	}

	int m;
	int n;
	for(m = 0; m < SUBTILE_SIZE; ++m)
	{
		for(n = 0; n < SUBTILE_SIZE; ++n)
		{
			sample_data(src, dst, x, y, i, j, m, n, latT, lonL, latB, lonR);
		}
	}

	// convert dst to 565
	texgz_tex_convert(dst, TEXGZ_UNSIGNED_SHORT_5_6_5, TEXGZ_RGB);

	char key[256];
	snprintf(key, 256, "%i_%i", j, i);
	pak_file_writek(pak, key);
	texgz_tex_exportf(dst, pak->f);
	texgz_tex_delete(&dst);
}
Example #2
0
static void sample_sector(texgz_tex_t* dst,
                          int month, int r, int c,
                          double latT, double lonL,
                          double latB, double lonR,
                          const char* fname)
{
	assert(dst);
	assert(fname);

	LOGI("%s", fname);

	texgz_tex_t* src = texgz_png_import(fname);
	if(src == NULL)
	{
		return;
	}

	// convert src to 888 (should be already)
	texgz_tex_convert(src, TEXGZ_UNSIGNED_BYTE, TEXGZ_RGB);

	// 2^9 gives 512 tiles at zoom=9
	int t = 512;
	int x;
	int y;
	int xmin = (t/4)*c;
	int xmax = (t/4)*(c + 1);
	int ymin = (t/2)*r;
	int ymax = (t/2)*(r + 1);
	for(y = ymin; y < ymax; ++y)
	{
		for(x = xmin; x < xmax; ++x)
		{
			sample_tile(src, dst, month, x, y,
			            latT, lonL, latB, lonR);
		}
	}

	texgz_tex_delete(&src);
}
Example #3
0
static void sample_sector(int month, int r, int c,
                          double latT, double lonL,
                          double latB, double lonR,
                          const char* fname)
{
	assert(fname);
	LOGD("debug month=%i, r=%i, c=%i, latT=%lf, lonL=%ls, latB=%lf, lonR=%lf, fname=%s",
         month, r, c, latT, lonL, latB, lonR, fname);

	LOGI("%s", fname);

	texgz_tex_t* src = texgz_png_import(fname);
	if(src == NULL)
	{
		return;
	}

	// convert src to 888 (should be already)
	texgz_tex_convert(src, TEXGZ_UNSIGNED_BYTE, TEXGZ_RGB);

	// 2^9*256/2048 gives 64 tiles at zoom=9
	int t = 64;
	int x;
	int y;
	int xmin = (t/4)*c;
	int xmax = (t/4)*(c + 1);
	int ymin = (t/2)*r;
	int ymax = (t/2)*(r + 1);
	for(y = ymin; y < ymax; ++y)
	{
		for(x = xmin; x < xmax; ++x)
		{
			sample_tile(src, month, x, y, latT, lonL, latB, lonR);
		}
	}

	texgz_tex_delete(&src);
}
Example #4
0
int main(int argc, char** argv)
{
	unsigned char dx = 0;
	unsigned char dy = 0;

	const char* arg_format = NULL;
	const char* arg_src    = NULL;
	const char* arg_dst    = NULL;

	if(argc == 6)
	{
		dx = (unsigned char) strtoul(argv[2], (char**) NULL, 10);
		dy = (unsigned char) strtoul(argv[3], (char**) NULL, 10);

		arg_format = argv[1];
		arg_src    = argv[4];
		arg_dst    = argv[5];
	}
	else if(argc == 4)
	{
		arg_format = argv[1];
		arg_src    = argv[2];
		arg_dst    = argv[3];

		if(check_ext(arg_src, "mgm"))
		{
			usage(argv[0]);
			return EXIT_FAILURE;
		}
	}
	else
	{
		usage(argv[0]);
		return EXIT_FAILURE;
	}

	// parse format
	int type;
	int format;
	if(strcmp(arg_format, "RGBA-8888") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_RGBA;
	}
	else if(strcmp(arg_format, "BGRA-8888") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_BGRA;
	}
	else if(strcmp(arg_format, "RGB-565") == 0)
	{
		type  = TEXGZ_UNSIGNED_SHORT_5_6_5;
		format = TEXGZ_RGB;
	}
	else if(strcmp(arg_format, "RGBA-4444") == 0)
	{
		type  = TEXGZ_UNSIGNED_SHORT_4_4_4_4;
		format = TEXGZ_RGBA;
	}
	else if(strcmp(arg_format, "RGB-888") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_RGB;
	}
	else if(strcmp(arg_format, "RGBA-5551") == 0)
	{
		type  = TEXGZ_UNSIGNED_SHORT_5_5_5_1;
		format = TEXGZ_RGBA;
	}
	else if(strcmp(arg_format, "LUMINANCE") == 0)
	{
		type  = TEXGZ_UNSIGNED_BYTE;
		format = TEXGZ_LUMINANCE;
	}
	else if(strcmp(arg_format, "LUMINANCE-F") == 0)
	{
		type  = TEXGZ_FLOAT;
		format = TEXGZ_LUMINANCE;
	}
	else
	{
		LOGE("invalid format=%s", arg_format);
		return EXIT_FAILURE;
	}

	// import src
	texgz_tex_t* tex = NULL;
	if(check_ext(arg_src, "texgz"))
	{
		tex = texgz_tex_import(arg_src);
	}
	else if(check_ext(arg_src, "jpg"))
	{
		tex = texgz_jpeg_import(arg_src);
	}
	else if(check_ext(arg_src, "png"))
	{
		tex = texgz_png_import(arg_src);
	}
	else if(check_ext(arg_src, "mgm"))
	{
		tex = texgz_mgm_import(arg_src, dx, dy);
	}
	else
	{
		LOGE("invalid src=%s", arg_src);
		return EXIT_FAILURE;
	}

	if(tex == NULL)
	{
		return EXIT_FAILURE;
	}

	// convert to format
	if(texgz_tex_convert(tex, type, format) == 0)
	{
		goto fail_convert;
	}

	// export dst
	int ret = 0;
	if(check_ext(arg_dst, "texgz"))
	{
		ret = texgz_tex_export(tex, arg_dst);
	}
	else if(check_ext(arg_dst, "jpg"))
	{
		ret = texgz_jpeg_export(tex, arg_dst);
	}
	else if(check_ext(arg_dst, "png"))
	{
		ret = texgz_png_export(tex, arg_dst);
	}
	else
	{
		LOGE("invalid dst=%s", arg_dst);
		goto fail_dst;
	}

	if(ret == 0)
	{
		goto fail_export;
	}

	texgz_tex_delete(&tex);

	// success
	return EXIT_SUCCESS;

	// failure
	fail_export:
	fail_dst:
	fail_convert:
		texgz_tex_delete(&tex);
	return EXIT_FAILURE;
}
Example #5
0
int main(int argc, char** argv)
{
	// osm.list
	// zoom x y
	if(argc != 2)
	{
		LOGE("usage: %s [osm.list]", argv[0]);
		return EXIT_FAILURE;
	}

	// create directories if necessary
	char dname[256];
	snprintf(dname, 256, "%s", "osm");
	if(mkdir(dname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
	{
		if(errno == EEXIST)
		{
			// already exists
		}
		else
		{
			LOGE("mkdir %s failed", dname);
			return EXIT_FAILURE;
		}
	}

	// open the list
	FILE* f = fopen(argv[1], "r");
	if(f == NULL)
	{
		LOGE("failed to open %s", argv[1]);
		return EXIT_FAILURE;
	}

	// iteratively pak osm images
	char* line = NULL;
	size_t n   = 0;
	int index = 0;
	while(getline(&line, &n, f) > 0)
	{
		int x;
		int y;
		int zoom;
		if(sscanf(line, "%i %i %i", &zoom, &x, &y) != 3)
		{
			LOGE("invalid line=%s", line);
			continue;
		}

		LOGI("%i: zoom=%i, x=%i, y=%i", index++, zoom, x, y);

		// create directories if necessary
		char dname[256];
		snprintf(dname, 256, "osm/%i", zoom);
		if(mkdir(dname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
		{
			if(errno == EEXIST)
			{
				// already exists
			}
			else
			{
				LOGE("mkdir %s failed", dname);
				continue;
			}
		}

		char fname[256];
		snprintf(fname, 256, "osm/%i/%i_%i.pak", zoom, x, y);
		pak_file_t* pak = pak_file_open(fname, PAK_FLAG_WRITE);
		if(pak == NULL)
		{
			continue;
		}

		int i;
		int j;
		for(i = 0; i < 8; ++i)
		{
			for(j = 0; j < 8; ++j)
			{
				int xj = 8*x + j;
				int yi = 8*y + i;

				snprintf(fname, 256, "localhost/osm/%i/%i/%i.png", zoom, xj, yi);
				texgz_tex_t* tex = texgz_png_import(fname);
				if(tex == NULL)
				{
					continue;
				}

				if(texgz_tex_convert(tex,
				                     TEXGZ_UNSIGNED_SHORT_4_4_4_4,
				                     TEXGZ_RGBA) == 0)
				{
					texgz_tex_delete(&tex);
					continue;
				}

				// j=dx, i=dy
				snprintf(fname, 256, "%i_%i", j, i);
				pak_file_writek(pak, fname);
				texgz_tex_exportf(tex, pak->f);
				texgz_tex_delete(&tex);
			}
		}

		pak_file_close(&pak);
	}
	free(line);

	return EXIT_SUCCESS;
}
Example #6
0
void naip_cache(naip_node_t* node)
{
	assert(node);

	// update LRU if already cached
	if(node->tex)
	{
		if(node == glru)
		{
			return;
		}

		naip_node_t* prev = node->prev;
		naip_node_t* next = node->next;
		if(prev)
		{
			prev->next = next;
		}

		if(next)
		{
			next->prev = prev;
		}

		node->prev = NULL;
		node->next = glru;
		glru->prev = node;
		glru       = node;

		return;
	}

	LOGI("import %s", node->fname);
	node->tex = texgz_jp2_import(node->fname);
	if(node->tex == NULL)
	{
		LOGE("fatal error importing %s", node->fname);
		exit(EXIT_FAILURE);
	}

	// force the jp2 to RGBA
	if(texgz_tex_convert(node->tex,
	                     TEXGZ_UNSIGNED_BYTE,
	                     TEXGZ_RGBA) == 0)
	{
		LOGE("fatal error converting %s", node->fname);
		texgz_tex_delete(&node->tex);
		exit(EXIT_FAILURE);
	}

	// insert node into cache
	node->prev = NULL;
	node->next = glru;

	if(glru)
	{
		glru->prev = node;
	}
	glru = node;

	// evict excess nodes
	int count = 0;
	while(node)
	{
		++count;

		if(count > NAIP_CACHE_SIZE)
		{
			naip_node_t* prev = node->prev;
			naip_node_t* next = node->next;
			if(prev)
			{
				prev->next = next;
			}

			if(next)
			{
				next->prev = prev;
			}

			texgz_tex_delete(&node->tex);
			node->prev = NULL;
			node->next = NULL;
			node = next;
		}
		else
		{
			node = node->next;
		}
	}
}