Exemplo n.º 1
0
void ObjectScene::syncQuad(Kite::KQuadCom *Quad) {
	auto pixItem = (QGraphicsPixmapItem *)Quad->getSceneItem();

	Kite::KRectF32 brect;
	Quad->getBoundingRect(brect);

	if (!Quad->getAtlasTextureArray().str.empty()) {
		Kite::KAtlasTextureArray *tarray = nullptr;
		emit(tarray = (Kite::KAtlasTextureArray *)requestResource(Quad->getAtlasTextureArray().str.c_str()));

		if (tarray) {
			auto atex = tarray->getItem(Quad->getTextureArrayIndex());
			if (atex) {
				Kite::KTexture *tex = atex->getTexture();
				if (tex) {
					Kite::KImage image;
					tex->getImage(image);
					QImage qimage(image.getPixelsData(), image.getWidth(), image.getHeight(), QImage::Format::Format_RGBA8888);
					auto procimgae = qimage.copy(Quad->getAtlasItem().xpos, Quad->getAtlasItem().ypos,
																	  Quad->getAtlasItem().width, Quad->getAtlasItem().height)
														  .mirrored(Quad->getAtlasItem().getFlipH(), !Quad->getAtlasItem().getFlipV())
														  .scaled(Quad->getWidth(), Quad->getHeight());

					//blend
					if (Quad->getBlendColor() != Kite::KColor(Kite::Colors::WHITE)) {
						QColor col(Quad->getBlendColor().getR(), Quad->getBlendColor().getG(),
								   Quad->getBlendColor().getB(), Quad->getBlendColor().getA());

						auto alpha = procimgae.alphaChannel();
						for (int x = 0; x != procimgae.width(); ++x) {
							for (int y(0); y != procimgae.height(); ++y) {
								if (qAlpha(procimgae.pixel(x, y)) == 0) continue; // transparrent pixels

								QColor icol(procimgae.pixel(x, y));
								icol.setRed(BLEND_Multiply(icol.red(), col.red()));
								icol.setBlue(BLEND_Multiply(icol.blue(), col.blue()));
								icol.setGreen(BLEND_Multiply(icol.green(), col.green()));
								procimgae.setPixel(x, y, icol.rgb());
							}
						}
						procimgae.setAlphaChannel(alpha);
					}

					pixItem->setPixmap(QPixmap::fromImage(procimgae));
					pixItem->setOpacity(Quad->getBlendColor().getGLA());
					return;
				}
			}
		}
	}
	
	QPixmap pm(Quad->getWidth(), Quad->getHeight());
	pm.fill(QColor(Quad->getBlendColor().getR(), Quad->getBlendColor().getG(), Quad->getBlendColor().getB(), Quad->getBlendColor().getA()));
	pixItem->setPixmap(pm);
	pixItem->setOpacity(Quad->getBlendColor().getGLA());
}
Exemplo n.º 2
0
/**
 * Sets the alpha value of all pixels of the specified color to transparent. This is know as "color key". Returns 0 if
 * there is no image loaded.
 * If you call this method on an image not having 32 bpp and ::IND_RGBA format, it will be converted first to that format.
 * It is recommended that you use this method with IND_RGBA color format images and 32 bpp.
 * The method returns true if image could be converted and alpha channel set correctly, false otherwise.
 * @param pR						Byte R (Red).
 * @param pG						Byte G (Green).
 * @param pB						Byte B (Blue).
 */
bool IND_Image::setAlpha(BYTE pR, BYTE pG, BYTE pB) {
	// No image loaded
	if (!isImageLoaded() || !FreeImage_HasPixels(getFreeImageHandle()))  return false;
	
	bool success = true;

	// We add alpha channel if the image hasn't.
	if (getFormatInt() != IND_RGBA) {
		int targetbpp (32);
		if (IND_RGB == getFormatInt()) {
			targetbpp = (getBpp()* 4 )/ 3;
	}

		success = convert(IND_RGBA,targetbpp);
	} 

	if (success) {
	setAlphaChannel(pR, pG, pB);
	}

	return success;
}