void *FastGaussianBlurValueOperation::initializeTileData(rcti *rect)
{
	lockMutex();
	if (!this->m_iirgaus) {
		MemoryBuffer *newBuf = (MemoryBuffer *)this->m_inputprogram->initializeTileData(rect);
		MemoryBuffer *copy = newBuf->duplicate();
		FastGaussianBlurOperation::IIR_gauss(copy, this->m_sigma, 0, 3);

		if (this->m_overlay == FAST_GAUSS_OVERLAY_MIN) {
			float *src = newBuf->getBuffer();
			float *dst = copy->getBuffer();
			for (int i = copy->getWidth() * copy->getHeight(); i != 0; i--, src += COM_NUM_CHANNELS_VALUE, dst += COM_NUM_CHANNELS_VALUE) {
				if (*src < *dst) {
					*dst = *src;
				}
			}
		}
		else if (this->m_overlay == FAST_GAUSS_OVERLAY_MAX) {
			float *src = newBuf->getBuffer();
			float *dst = copy->getBuffer();
			for (int i = copy->getWidth() * copy->getHeight(); i != 0; i--, src += COM_NUM_CHANNELS_VALUE, dst += COM_NUM_CHANNELS_VALUE) {
				if (*src > *dst) {
					*dst = *src;
				}
			}
		}

//		newBuf->

		this->m_iirgaus = copy;
	}
	unlockMutex();
	return this->m_iirgaus;
}
void *FastGaussianBlurOperation::initializeTileData(rcti *rect)
{
	lockMutex();
	if (!this->m_iirgaus) {
		MemoryBuffer *newBuf = (MemoryBuffer *)this->m_inputProgram->initializeTileData(rect);
		MemoryBuffer *copy = newBuf->duplicate();
		updateSize();

		int c;
		this->m_sx = this->m_data.sizex * this->m_size / 2.0f;
		this->m_sy = this->m_data.sizey * this->m_size / 2.0f;

		if ((this->m_sx == this->m_sy) && (this->m_sx > 0.0f)) {
			for (c = 0; c < COM_NUM_CHANNELS_COLOR; ++c)
				IIR_gauss(copy, this->m_sx, c, 3);
		}
		else {
			if (this->m_sx > 0.0f) {
				for (c = 0; c < COM_NUM_CHANNELS_COLOR; ++c)
					IIR_gauss(copy, this->m_sx, c, 1);
			}
			if (this->m_sy > 0.0f) {
				for (c = 0; c < COM_NUM_CHANNELS_COLOR; ++c)
					IIR_gauss(copy, this->m_sy, c, 2);
			}
		}
		this->m_iirgaus = copy;
	}
	unlockMutex();
	return this->m_iirgaus;
}
void *FastGaussianBlurOperation::initializeTileData(rcti *rect)
{
#if 0
	lockMutex();
	if (!this->m_iirgaus) {
		MemoryBuffer *newBuf = (MemoryBuffer *)this->m_inputProgram->initializeTileData(rect);
		MemoryBuffer *copy = newBuf->duplicate();
		updateSize();

		int c;
		this->m_sx = this->m_data->sizex * this->m_size / 2.0f;
		this->m_sy = this->m_data->sizey * this->m_size / 2.0f;
		
		if ((this->m_sx == this->m_sy) && (this->m_sx > 0.f)) {
			for (c = 0; c < COM_NUMBER_OF_CHANNELS; ++c)
				IIR_gauss(copy, this->m_sx, c, 3);
		}
		else {
			if (this->m_sx > 0.0f) {
				for (c = 0; c < COM_NUMBER_OF_CHANNELS; ++c)
					IIR_gauss(copy, this->m_sx, c, 1);
			}
			if (this->m_sy > 0.0f) {
				for (c = 0; c < COM_NUMBER_OF_CHANNELS; ++c)
					IIR_gauss(copy, this->m_sy, c, 2);
			}
		}
		this->m_iirgaus = copy;
	}
	unlockMutex();
	return this->m_iirgaus;
#else

	lockMutex();
	if (this->m_iirgaus) {
		// if this->m_iirgaus is set, we don't do tile rendering, so
		// we can return the already calculated cache
		unlockMutex();
		return this->m_iirgaus;
	}
	updateSize();
	rcti dai;
	bool use_tiles = getDAI(rect, &dai);
	if (use_tiles) {
		unlockMutex();
	}

	MemoryBuffer *buffer = (MemoryBuffer *)this->m_inputProgram->initializeTileData(NULL);
	rcti *buf_rect = buffer->getRect();

	dai.xmin = max(dai.xmin, buf_rect->xmin);
	dai.xmax = min(dai.xmax, buf_rect->xmax);
	dai.ymin = max(dai.ymin, buf_rect->ymin);
	dai.ymax = min(dai.ymax, buf_rect->ymax);

	MemoryBuffer *tile = new MemoryBuffer(NULL, &dai);
	tile->copyContentFrom(buffer);

	int c;
	float sx = this->m_data->sizex * this->m_size / 2.0f;
	float sy = this->m_data->sizey * this->m_size / 2.0f;

	if ((sx == sy) && (sx > 0.f)) {
		for (c = 0; c < COM_NUMBER_OF_CHANNELS; ++c)
			IIR_gauss(tile, sx, c, 3);
	}
	else {
		if (sx > 0.0f) {
			for (c = 0; c < COM_NUMBER_OF_CHANNELS; ++c)
				IIR_gauss(tile, sx, c, 1);
		}
		if (sy > 0.0f) {
			for (c = 0; c < COM_NUMBER_OF_CHANNELS; ++c)
				IIR_gauss(tile, sy, c, 2);
		}
	}
	if (!use_tiles) {
		this->m_iirgaus = tile;
		unlockMutex();
	}
	return tile;
#endif
}