Exemple #1
0
bool Image::scaleBilinear(int newWidth, int newHeight)
{
	if (newWidth == _width && newHeight == _height)
	{
		return true;
	}

	if (_pixels.size() == 0)
	{
		return false;
	}

	std::vector<Color> newPixels(newWidth * newHeight);

	float x_ratio = ((float)(_width - 1) / newWidth);
	float y_ratio = ((float)(_height - 1) / newHeight);
	Color a, b, c, d;
	int x, y;
	float w, h;

	for (int i = 0; i < newWidth; ++i)
	{
		for (int j = 0; j < newHeight; ++j)
		{
			x = (int)(x_ratio * i);
			y = (int)(y_ratio * j);

			w = (x_ratio * i) - x;
			h = (y_ratio * j) - y;

			a = getPixel(x, y);
			b = getPixel(x + 1, y);
			c = getPixel(x, y + 1);
			d = getPixel(x + 1, y + 1);

			byte cr = (byte)(a.r * (1 - w) * (1 - h) + b.r * (w)* (1 - h) + c.r * (h)* (1 - w) + d.r * (w * h));
			byte cg = (byte)(a.g * (1 - w) * (1 - h) + b.g * (w)* (1 - h) + c.g * (h)* (1 - w) + d.g * (w * h));
			byte cb = (byte)(a.b * (1 - w) * (1 - h) + b.b * (w)* (1 - h) + c.b * (h)* (1 - w) + d.b * (w * h));

			newPixels[i + j * newWidth] = Color(cr, cg, cb);
		}
	}

	_pixels.resize(newWidth * newHeight);
	std::copy(newPixels.begin(), newPixels.end(), _pixels.begin());
	_width = newWidth;
	_height = newHeight;

	return true;
}
Exemple #2
0
bool Image::rotate(float angle)
{
	if (_width == 0 || _height == 0)
	{
		return false;
	}

	float radians = util::deg_to_rad(angle);
	float cosine = std::cos(radians);
	float sine = std::sin(radians);

	float x1 = -_height * sine;
	float y1 = _height * cosine;
	float x2 = _width * cosine - _height * sine;
	float y2 = _height * cosine + _width * sine;
	float x3 = _width * cosine;
	float y3 = _width * sine;

	float minX = std::min({ 0.0f, x1, x2, x3 });
	float minY = std::min({ 0.0f, y1, y2, y3 });
	float maxX = std::max({ x1, x2, x3 });
	float maxY = std::max({ x1, y2, y3 });

	int newWidth = (int)std::ceil(std::fabs(maxX) - minX);
	int newHeight = (int)std::ceil(std::fabs(maxY) - minY);

	std::vector<Color> newPixels(newWidth * newHeight);

	for (int x = 0; x < newWidth; ++x)
	{
		for (int y = 0; y < newHeight; ++y)
		{
			int srcX = (int)((x + minX) * cosine + (y + minY) * sine);
			int srcY = (int)((y + minY) * cosine - (x + minX) * sine);

			newPixels[x + y * newWidth] = getPixel(srcX, srcY);
		}
	}

	_pixels.resize(newPixels.size());
	std::copy(newPixels.begin(), newPixels.end(), _pixels.begin());
	_width = newWidth;
	_height = newHeight;

	return true;
}
Exemple #3
0
bool Image::scaleHq4x(void)
{
	if (_pixels.size() == 0)
	{
		return false;
	}

	int newWidth = _width * 4;
	int newHeight = _height * 4;

	std::vector<Color> newPixels(newWidth * newHeight);

	hq4x_32((uint32_t*)&_pixels.front(), (uint32_t*)&newPixels.front(), _width, _height);

	_pixels.resize(newWidth * newHeight);
	std::copy(newPixels.begin(), newPixels.end(), _pixels.begin());
	_width = newWidth;
	_height = newHeight;

    return true;
}
    void Texture::resize(const glm::ivec2& newSize)
    {
        FEA_ASSERT(newSize.x > 0 && newSize.y > 0, "Size must be bigger than zero in both dimensions! Given size is " << newSize.x << " " << newSize.y);

        std::vector<uint8_t> newPixels(newSize.x * newSize.y * 4, 0);

        int32_t newWidth = newSize.x;
        int32_t currentWidth = mSize.x;

        for(uint32_t x = 0; x < mSize.x && x < newSize.x; x++)
        {   
            for(uint32_t y = 0; y < mSize.y < newSize.y; y++)
            {   
                newPixels[(x + y * newWidth) * 4 + 0] = mPixelData[(x + y * currentWidth) * 4 + 0]; 
                newPixels[(x + y * newWidth) * 4 + 1] = mPixelData[(x + y * currentWidth) * 4 + 1]; 
                newPixels[(x + y * newWidth) * 4 + 2] = mPixelData[(x + y * currentWidth) * 4 + 2]; 
                newPixels[(x + y * newWidth) * 4 + 3] = mPixelData[(x + y * currentWidth) * 4 + 3]; 
            }   
        }   

        create(newSize, newPixels.data(), mSmooth);
    }
Exemple #5
0
bool Image::scaleNearest(int newWidth, int newHeight)
{
    if(newWidth == 0 || newHeight == 0 || _pixels.size() == 0)
    {
        return false;
    }

    if(newWidth == _width && newHeight == _height)
    {
        return true;
    }

    std::vector<Color> newPixels(newWidth * newHeight);

    float x_ratio = _width / (float)newWidth;
    float y_ratio = _height / (float)newHeight;
    float px, py;

    for(int y = 0; y < newHeight; ++y)
    {
        for(int x = 0; x < newWidth; ++x)
        {
            px = std::floor(x * x_ratio);
            py = std::floor(y * y_ratio);

            newPixels[x + y * newWidth] = _pixels[int(px + py * _width)];
        }
    }

    _pixels.resize(newWidth * newHeight);
    std::copy(newPixels.begin(), newPixels.end(), _pixels.begin());

    _width = newWidth;
    _height = newHeight;

    return true;
}
Exemple #6
0
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
	OID ID;
	
	/* OMEIS data structures */
	omeis* is;
	pixHeader* head = (pixHeader*) mxMalloc(sizeof(pixHeader));
	
	/* MATLAB data structueres */
	mxArray *m_dx, *m_dy, *m_dz, *m_dc, *m_dt, *m_bp, *m_isSigned, *m_isFloat;
	mxArray *m_url, *m_sessionkey;
	char* url, *sessionkey;
	
	if (nrhs != 2)
		mexErrMsgTxt("\n [ID] = newPixels (is, ph)");
		
	if (!mxIsStruct(prhs[0]))
		mexErrMsgTxt("newPixels requires the first input to be the struct outputed"
					 " from openConnectionOMEIS\n");
					 
	if (!(m_url = mxGetField(prhs[0], 0, "url")))
		mexErrMsgTxt("newPixels requires the first input, OMEIS struct, to have field: url");
	if (!(m_sessionkey = mxGetField(prhs[0], 0, "sessionkey")))
		mexErrMsgTxt("newPixels requires the first input, OMEIS struct, to have field: sessionkey");
		
	if (!mxIsChar(m_url) || !mxIsChar(m_sessionkey))
		mexErrMsgTxt("OMEIS field aren't character array.\n");		
	
	if (!mxIsStruct(prhs[0]))
		mexErrMsgTxt("newPixels requires the first input to be the struct outputed"
					 " from openConnectionOMEIS\n");
					 
	if (!mxIsStruct(prhs[1]))
		mexErrMsgTxt("newPixels requires the second input to be a pixHeader struct.");
		
	if (!(m_dx = mxGetField(prhs[1], 0, "dx")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: dx");
	if (!(m_dy = mxGetField(prhs[1], 0, "dy")))
			mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: dy");
	if (!(m_dz = mxGetField(prhs[1], 0, "dz")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: dz");
	if (!(m_dc = mxGetField(prhs[1], 0, "dc")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: dc");
	if (!(m_dt = mxGetField(prhs[1], 0, "dt")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: dt");
	if (!(m_bp = mxGetField(prhs[1], 0, "bp")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: bp");
	if (!(m_isSigned = mxGetField(prhs[1], 0, "isSigned")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: isSigned");
	if (!(m_isFloat  = mxGetField(prhs[1], 0, "isFloat")))
		mexErrMsgTxt("newPixels requires the second input, pixHeader struct, to have field: isFloat");

	head->dx = (ome_dim) mxGetScalar(m_dx);
 	head->dy = (ome_dim) mxGetScalar(m_dy);
 	head->dz = (ome_dim) mxGetScalar(m_dz);
 	head->dc = (ome_dim) mxGetScalar(m_dc);
 	head->dt = (ome_dim) mxGetScalar(m_dt);
 	head->bp = (u_int8_t) mxGetScalar(m_bp);
 	head->isSigned = (u_int8_t) mxGetScalar(m_isSigned);
 	head->isFloat  = (u_int8_t) mxGetScalar(m_isFloat);
	
	url = mxArrayToString(m_url);
	sessionkey = mxArrayToString(m_sessionkey);
	
	is = openConnectionOMEIS (url, sessionkey);

	if ( !(ID = newPixels (is, head)) ) {	
		/* clean up */
		mxFree(url);
		mxFree(sessionkey);
		mxFree(head);
		mxFree(is);
		
		mexErrMsgTxt("newPixels OMEIS method failed.\n");
	}

	plhs[0] = mxCreateScalarDouble((double) ID);

	/* clean up */
	mxFree(url);
	mxFree(sessionkey);
	mxFree(head);
	mxFree(is);
}
Exemple #7
0
void FogOfWar::initializeVertexsGiven(const sf::VertexArray& vertices,
	const sf::Texture* tileSet)
{
	//const sf::VertexArray& vertices = vertexNode->getVertices();
	Utility::CircleGeometry circle(mCenterPoint, mSightRadius);
	for (int i = 0; i < vertices.getVertexCount(); i += 4){
		const sf::Vertex& topLeft = vertices[i];
		const sf::Vertex& topRight = vertices[i + 1];
		const sf::Vertex& bottomRight = vertices[i + 2];
		const sf::Vertex& bottomLeft = vertices[i + 3];

		sf::FloatRect floatRect(topLeft.position.x, topLeft.position.y, 
			topRight.position.x - topLeft.position.x, bottomLeft.position.y - topLeft.position.y);

		/*if (!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, topLeft.position) &&
			!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, topRight.position) &&
			!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, bottomRight.position) &&
			!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, bottomLeft.position))
			continue;*/
		
		if (!Utility::isCircleIntersectsWithFloatRect(circle, floatRect))
			continue;

		//get the texture that the vertex is using
		//const sf::Texture* tileSet = vertexNode->getTileSet();
		//get the corresponding img also
		sf::Image* tileImg = mTexturesInt.textureKeyToImg(tileSet);
		
		if (!tileImg)
			continue;

		sf::Vector2u imgSize = tileImg->getSize();

		//is the size of the current vertex's texture size
		sf::Vector2f textureRectSize;
		textureRectSize.x = topRight.texCoords.x - topLeft.texCoords.x;
		textureRectSize.y = bottomLeft.texCoords.y - topLeft.texCoords.y;

		//the top left position of the 4 vertexs combined together
		sf::Vector2f vertexPosInGame = topLeft.position;
		

		const sf::Uint8* tilePixels = tileImg->getPixelsPtr();

		//img we use to put into our mMapTexture
		/*sf::Image finalImageToTxt;
		finalImageToTxt.create(textureRectSize.x, textureRectSize.y);

		

	

		//for (float txCoord = 0.f; txCoord < textureRectSize.x; txCoord++){

		for (float tyCoord = 0.f; tyCoord < textureRectSize.y; tyCoord++){
			for (float txCoord = 0.f; txCoord < textureRectSize.x; txCoord++){

				int a = (topLeft.texCoords.x + txCoord) +
					(imgSize.x *  (topLeft.texCoords.y + tyCoord));

				a *= 4;

				finalImageToTxt.setPixel(txCoord, tyCoord,
					sf::Color(
					tilePixels[a],
					tilePixels[a + 1],
					tilePixels[a + 2],
					20));
			}
		}
		mMapTexture.update(finalImageToTxt, vertexPosInGame.x, vertexPosInGame.y);*/

		float finalSize = textureRectSize.x * textureRectSize.y * 4.f;
		int totalIndex = static_cast<int>(finalSize);
		std::unique_ptr<sf::Uint8> newPixels(new sf::Uint8[totalIndex]);
		sf::Uint8* ptrToPixel = newPixels.get();

		int newI = 0;
		for (float tyCoord = 0.f; tyCoord < textureRectSize.y; tyCoord++){
			for (float txCoord = 0.f; txCoord < textureRectSize.x; txCoord++){

				int a = (topLeft.texCoords.x + txCoord) +
					(imgSize.x *  (topLeft.texCoords.y + tyCoord));

				a *= 4;
				ptrToPixel[newI] = tilePixels[a];
				ptrToPixel[newI + 1] = tilePixels[a + 1];
				ptrToPixel[newI + 2] = tilePixels[a + 2];
				ptrToPixel[newI + 3] = 20;

				newI += 4;
			}
		}
		mMapTexture.update(ptrToPixel, textureRectSize.x, textureRectSize.y, vertexPosInGame.x, vertexPosInGame.y);
		
	}
}