示例#1
0
void TileRenderer::Compose(std::string compositionLayerPath, std::string compositionMode, double compositionAlpha, int width, int height, mapnik::image_32* buf,int zz, int xx, int yy)
#endif
{
	std::string compositionTilePath = "";
	 // COMPOSITION MODE
	if(compositionLayerPath != "")
	{
		//std::stringstream str;
		//str<< compositionLayerPath << "/tiles/" << zoom << "/" << x << "/" << y << ".png";
		ImageObject compImg;

		// find possible composition
		  int compositionLevel = 1;
		  double modX = 1;
		  double modY = 1;
		  if(compositionLayerPath != "")
		  {
			for(size_t h = zz; h > 0; h--)
			{
				std::stringstream ss;
				ss << compositionLayerPath <<  h << "/" << (math::Floor((double)xx/(compositionLevel > 0? compositionLevel:1))) << "/" << (math::Floor((double)yy/(compositionLevel > 0? compositionLevel:1))) << ".png";
				if(FileSystem::FileExists(ss.str()))
				{
					compositionTilePath = ss.str();
					double dx = ((double)xx/compositionLevel);
					double dy = ((double)yy/compositionLevel);
					int ix = int(math::Floor(dx));
					int iy = int(math::Floor(dy));
					modX =  dx - ix;
					modY =  dy - iy;
					break;
				}
				compositionLevel *= 2;
			}
		  }
		
		if(ImageLoader::LoadFromDisk(Img::Format_PNG, compositionTilePath, Img::PixelFormat_RGBA,compImg))
		{
			if(compositionMode == "unify" || compositionMode == "unifyopaque")
			{
				for(size_t y = 0; y < height; y++)
				{
					for(size_t x = 0; x < width; x++)
					{
						unsigned char comp[4];
						int adr = 4*y*width+4*x;
						if(compositionLevel > 0)
						{
							double pX = ((double)x/compositionLevel)+(modX*width);
							double pY = ((double)y/compositionLevel)+(modY*height);
							compImg.ReadPixelBilinear4(pX,pY,comp[0],comp[1],comp[2],comp[3]);
						}
						else
						{
							compImg.ReadPixel4(x,y,comp[0],comp[1],comp[2],comp[3]);
						}
						
						unsigned int compInt = 0x00000000;
						compInt = compInt | comp[3];
						compInt = (compInt << 8) | comp[2];
						compInt = (compInt << 8) | comp[1];
						compInt = (compInt << 8) | comp[0];
						unsigned char alpha;
						if(compositionMode == "unifyopaque")
						{
							alpha = buf->raw_data()[adr+3] > 0 ? 255 : 0;
						}
						else
						{
							alpha = buf->raw_data()[adr+3];
						}
						//buf.setPixel(x,y,compInt);
						buf->blendPixel(x,y,compInt,(int)alpha);
					}
				}
			}
			else if(compositionMode == "overlay")
			{
				for(size_t y = 0; y < height; y++)
				{
					for(size_t x = 0; x < width; x++)
					{
						unsigned char comp[4];
						int adr = 4*y*width+4*x;
						if(compositionLevel > 0)
						{
							int pX = (x/compositionLevel)+(modX*width);
							int pY = (y/compositionLevel)+(modY*height);
							compImg.ReadPixelBilinear4(pX,pY,comp[0],comp[1],comp[2],comp[3]);
						}
						else
						{
							compImg.ReadPixel4(x,y,comp[0],comp[1],comp[2],comp[3]);
						}
						unsigned int compInt = 0x00000000;
						compInt = compInt | comp[3];
						compInt = (compInt << 8) | comp[2];
						compInt = (compInt << 8) | comp[1];
						compInt = (compInt << 8) | comp[0];
						unsigned char alpha = buf->raw_data()[adr+3];
						//buf.setPixel(x,y,compInt);
						buf->blendPixel(x,y,compInt,(int)comp[3]);
					}
				}
			}
		}
	}
}