Exemple #1
0
CircleDrawable::CircleDrawable(float radius, MessageBus& m)
    : Component     (m),
    m_circleShape   (radius),
    m_shader        (nullptr),
    m_normalMap     (nullptr)
{
    m_circleShape.setOrigin(radius, radius);
    m_circleShape.setOutlineThickness(1.4f);

    float colour = std::min(radius / 46.f, 1.f);
    sf::Uint8 colourByte = static_cast<sf::Uint8>(colour * baseColour);
    sf::Color finalColour(colourByte, colourByte, colourByte, static_cast<sf::Uint8>(colour * 240.f));
    m_circleShape.setOutlineColor(finalColour);
    finalColour.a = static_cast<sf::Uint8>(colour * alpha);
    m_circleShape.setFillColor(finalColour);
}
Image* ImageReaderPNG::readGreyscaleImage(const std::string& filePath, unsigned int requiredTypeFlags)
{
	PNGInfra infra;
	ImageType actualType = readData(filePath, infra, requiredTypeFlags & Image::IMAGE_FLAGS_ALPHA);

	if (actualType == eInvalid)
		return NULL;

	const bool makeFloat = !(requiredTypeFlags & Image::IMAGE_FORMAT_NATIVE);

	Image1f* pImage1f = NULL;
	Image1b* pImage1b = NULL;

	if (makeFloat)
	{
		pImage1f = new Image1f(infra.width, infra.height, false);
	}
	else
	{
		pImage1b = new Image1b(infra.width, infra.height, false);
	}

	if (pImage1f || pImage1b)
	{
		if (makeFloat)
		{
			if (actualType == eRGBA)
			{
				if (requiredTypeFlags & Image::IMAGE_FLAGS_ALPHA)
				{
					// we just want the alpha channel from the RGBA
					for (unsigned int i = 0; i < infra.height; i++)
					{
						png_byte* pLineData = infra.pRows[i];

						// need to flip the height round...
						unsigned int y = infra.height - i - 1;

						float* pFloatRow = pImage1f->floatRowPtr(y);

						for (unsigned int x = 0; x < infra.width; x++)
						{
							pLineData++;
							pLineData++;
							pLineData++;
							unsigned char alpha = *pLineData++;

							*pFloatRow = (alpha == 0) ? 0 : alpha / 255.0f;

							pFloatRow++;
						}
					}
				}
				else if (requiredTypeFlags & Image::IMAGE_FLAGS_BRIGHTNESS)
				{
					// otherwise, get the brightness of the RGB
					// copy data across...
					for (unsigned int i = 0; i < infra.height; i++)
					{
						png_byte* pLineData = infra.pRows[i];

						// need to flip the height round...
						unsigned int y = infra.height - i - 1;

						float* pFloatRow = pImage1f->floatRowPtr(y);

						for (unsigned int x = 0; x < infra.width; x++)
						{
							unsigned char red = *pLineData++;
							unsigned char green = *pLineData++;
							unsigned char blue = *pLineData++;
							pLineData++;

							float r = ColourSpace::convertSRGBToLinearLUT(red);
							float g = ColourSpace::convertSRGBToLinearLUT(green);
							float b = ColourSpace::convertSRGBToLinearLUT(blue);

							Colour3f finalColour(r, g, b);

							*pFloatRow = finalColour.brightness();

							pFloatRow++;
						}
					}
				}
				else // exact - assumes greyscale for displacement
				{
					for (unsigned int i = 0; i < infra.height; i++)
					{
						png_byte* pLineData = infra.pRows[i];

						// need to flip the height round...
						unsigned int y = infra.height - i - 1;

						float* pFloatRow = pImage1f->floatRowPtr(y);

						for (unsigned int x = 0; x < infra.width; x++)
						{
							unsigned char red = *pLineData++;
							unsigned char green = *pLineData++;
							unsigned char blue = *pLineData++;
							pLineData++;

							float average = (float)red + (float)green + (float)blue;
							average *= 0.3333333333f;

							average /= 255.0f;

							*pFloatRow = average;

							pFloatRow++;
						}
					}
				}
			}
			else
			{
				// it's only got one channel, so just bring that in as raw value...
				for (unsigned int i = 0; i < infra.height; i++)
				{
					png_byte* pLineData = infra.pRows[i];

					// need to flip the height round...
					unsigned int y = infra.height - i - 1;

					float* pFloatRow = pImage1f->floatRowPtr(y);

					for (unsigned int x = 0; x < infra.width; x++)
					{
						unsigned char value = *pLineData++;

						*pFloatRow = (value == 0) ? 0 : (value * (1.0f / 255.0f));

						pFloatRow++;
					}
				}
			}
		}
		else
		{
			// copy over directly
			if (actualType == eRGBA)
			{
				if (requiredTypeFlags & Image::IMAGE_FLAGS_ALPHA)
				{
					// we just want the alpha channel from the RGBA
					for (unsigned int i = 0; i < infra.height; i++)
					{
						png_byte* pLineData = infra.pRows[i];

						// need to flip the height round...
						unsigned int y = infra.height - i - 1;

						unsigned char* pUCharRow = pImage1b->uCharRowPtr(y);

						for (unsigned int x = 0; x < infra.width; x++)
						{
							pLineData++;
							pLineData++;
							pLineData++;
							unsigned char alpha = *pLineData++;

							*pUCharRow = alpha;

							pUCharRow++;
						}
					}
				}
				else if (requiredTypeFlags & Image::IMAGE_FLAGS_BRIGHTNESS)
				{
					// otherwise, get the brightness of the RGB
					// copy data across...
					for (unsigned int i = 0; i < infra.height; i++)
					{
						png_byte* pLineData = infra.pRows[i];

						// need to flip the height round...
						unsigned int y = infra.height - i - 1;

						unsigned char* pUCharRow = pImage1b->uCharRowPtr(y);

						for (unsigned int x = 0; x < infra.width; x++)
						{
							unsigned char red = *pLineData++;
							unsigned char green = *pLineData++;
							unsigned char blue = *pLineData++;
							pLineData++;

							Colour3b finalColour(red, green, blue);

							*pUCharRow = finalColour.brightness();

							pUCharRow++;
						}
					}
				}
				else // exact - assumes greyscale for displacement
				{
					for (unsigned int i = 0; i < infra.height; i++)
					{
						png_byte* pLineData = infra.pRows[i];

						// need to flip the height round...
						unsigned int y = infra.height - i - 1;

						unsigned char* pUCharRow = pImage1b->uCharRowPtr(y);

						for (unsigned int x = 0; x < infra.width; x++)
						{
							unsigned char red = *pLineData++;
							unsigned char green = *pLineData++;
							unsigned char blue = *pLineData++;
							pLineData++;

							float average = (float)red + (float)green + (float)blue;
							average *= 0.3333333333f;

							*pUCharRow = (unsigned char)average;

							pUCharRow++;
						}
					}
				}
			}
			else
			{
				// it's only got one channel, so just bring that in as raw value...
				for (unsigned int i = 0; i < infra.height; i++)
				{
					png_byte* pLineData = infra.pRows[i];

					// need to flip the height round...
					unsigned int y = infra.height - i - 1;

					unsigned char* pUCharRow = pImage1b->uCharRowPtr(y);

					for (unsigned int x = 0; x < infra.width; x++)
					{
						unsigned char value = *pLineData++;

						*pUCharRow = value;

						pUCharRow++;
					}
				}
			}
		}
	}

	png_destroy_read_struct(&infra.pPNG, &infra.pInfo, (png_infopp)NULL);

	for (unsigned int y = 0; y < infra.height; y++)
	{
		delete [] infra.pRows[y];
	}
	delete [] infra.pRows;

	fclose(infra.pFile);

	Image* pFinalImage = (makeFloat) ? static_cast<Image*>(pImage1f) : static_cast<Image*>(pImage1b);

	return pFinalImage;
}