PixelColor* VisualTextureContainer::createARGBImagePixels(bool debug) {
	
	PixelColor* pixelBuffer = NULL;
	
	VisualGraphics* theVisualGraphics = VisualGraphics::getInstance();
	
	if (debug == true) {
		pixelBuffer = VisualColorTools::createARGBCheckPixels(this->textureRect.width, this->textureRect.height);
		return pixelBuffer;
	}
	
	theVisualGraphics->enableTexturing(this->useRectExtension);
	theVisualGraphics->bindTexture(this->textureName, this->useRectExtension);
	theVisualGraphics->setPixelStorageParams();
	
	uint16 format = kGL_BGRA;
	uint16 type;
	
	// GL_BGRA/GL_UNSIGNED_BYTE and GL_BGRA/GL_UNSIGNED_INT_8_8_8_8_REV are equivalent on little endian machines
	
	if (this->useRectExtension == false) {
		
		pixelBuffer = (PixelColor*)malloc(this->textureRect.width * this->textureRect.height * sizeof(PixelColor));
		
		type = kGL_UNSIGNED_BYTE;
		theVisualGraphics->getPixelsOfCurrentTexture(this->useRectExtension, format, type, &(pixelBuffer));
		
#if __BIG_ENDIAN__
		VisualColorTools::convertInterleavedPixels1234To4321(pixelBuffer, this->textureRect.width * this->textureRect.height);
#endif

		if ((this->textureRect.width != this->imageRect.width) || (this->textureRect.height != this->imageRect.height)) {
			// copy actual image pixel data out of texture pixel data
			Pixel topLeftPosition;
			topLeftPosition.x = 0;
			topLeftPosition.y = 0;
			TopLeftPositionedPixelRect clipRect;
			clipRect.topLeftPixel = topLeftPosition;
			clipRect.pixelRect = this->imageRect;
			PixelColor* clippedPixelData = theVisualGraphics->clipPixelData(pixelBuffer, this->textureRect, clipRect);
			free(pixelBuffer);
			pixelBuffer = clippedPixelData;
		}

	} else {
		
		pixelBuffer = (PixelColor*)malloc(this->imageRect.width * this->imageRect.height * sizeof(PixelColor));
		
		type = kGL_UNSIGNED_INT_8_8_8_8_REV;
		theVisualGraphics->getPixelsOfCurrentTexture(this->useRectExtension, format, type, &pixelBuffer);
	}
	
	theVisualGraphics->disableTexturing(this->useRectExtension);
	
	return pixelBuffer;
}
UInt32* VisualTextureContainer::getTexturePixels(const UInt16 format, const UInt16 type) {

	bool debug = false;

	UInt8* pixelBuffer8Bit = NULL;
		
	char errStr[256];

	VisualGraphics* theVisualGraphics = VisualGraphics::getInstance();
	
	if (debug == true) {
		if (this->pixelBuffer != NULL) {
			free(this->pixelBuffer);
			this->pixelBuffer = NULL;
		}
		this->pixelBuffer = theVisualGraphics->createARGBCheckPixels(this->textureWidth, this->textureHeight);
		return this->pixelBuffer;
	}
	
	if ((debug == false) && (this->pixelBuffer != NULL)) {
		//return this->pixelBuffer;
	}
	
	UInt8 numberOfBytesPerChannel = 0;
	UInt8 numberOfChannels = 0; // channel == color resp. alpha channel
	UInt8 numberOfBytesPerPixel = 0;
	UInt32 numberOfBytesPerRow = 0;
	if ((format == kGL_RGBA) || (format == kGL_BGRA)) {
		numberOfChannels = 4;
	} else {
		sprintf(errStr, "unknown format %d in file: %s (line: %d) [%s])", format, __FILE__, __LINE__, __FUNCTION__);
		writeLog(errStr);
		return this->pixelBuffer;
	}
	
	if ((type == kGL_UNSIGNED_INT_8_8_8_8_REV) || (type == kGL_UNSIGNED_INT_8_8_8_8) || (type == kGL_UNSIGNED_BYTE)) {
		numberOfBytesPerChannel = 1; // 1 byte (== 8 bits) per color/channel
	} else {
		sprintf(errStr, "unknown type %d in file: %s (line: %d) [%s])", type, __FILE__, __LINE__, __FUNCTION__);
		writeLog(errStr);
		return this->pixelBuffer;
	}

	if (this->pixelBuffer != NULL) {
		free(this->pixelBuffer);
		this->pixelBuffer = NULL;
	}

	if (this->useRectExtension == false) {
		numberOfBytesPerPixel = numberOfBytesPerChannel * numberOfChannels;
		numberOfBytesPerRow = numberOfBytesPerPixel * this->textureWidth;
		if ((format == kGL_RGBA) || (format == kGL_BGRA)) {
			if ((type == kGL_UNSIGNED_INT_8_8_8_8_REV) || (type == kGL_UNSIGNED_INT_8_8_8_8)) {
				this->pixelBuffer = (UInt32*)calloc((numberOfBytesPerRow / numberOfBytesPerPixel) * this->textureHeight, numberOfBytesPerPixel);
			} else if (type == kGL_UNSIGNED_BYTE) {
				this->pixelBuffer = (UInt32*)calloc((numberOfBytesPerRow / numberOfBytesPerPixel) * this->textureHeight, numberOfBytesPerPixel);
				pixelBuffer8Bit = (UInt8*)malloc(this->textureWidth * this->textureHeight * 4);
			}
		}
		
		theVisualGraphics->enableTexturing(this->useRectExtension);
		theVisualGraphics->bindTexture(this->textureName, this->useRectExtension);
		theVisualGraphics->setPixelStorageParams();
		if ((type == kGL_UNSIGNED_INT_8_8_8_8_REV) || (type == kGL_UNSIGNED_INT_8_8_8_8)) {
			theVisualGraphics->get32BitPixelsOfCurrentTexture(this->useRectExtension, format, type, &(this->pixelBuffer));
		} else if (type == kGL_UNSIGNED_BYTE) {
			theVisualGraphics->get8BitPixelsOfCurrentTexture(this->useRectExtension, format, type, &pixelBuffer8Bit);
		}
		theVisualGraphics->disableTexturing(this->useRectExtension);

	} else {
#if TARGET_OS_MAC
		// glGetTexImage() does not always reliably return the pixelBuffer
		// of npot (non-power-of-two, GL_TEXTURE_RECTANGLE_EXT) textures
		// because of inconsistencies with Nvidia's GeForce4 MX card (1.4.18) [only with some not all textures the pixel data was returned]
		// we grab the pixels with glReadPixels()
		// (HW, 20070208)
		this->pixelBuffer = this->getRectPixels(format, type);
#endif
	}

	if (type == kGL_UNSIGNED_BYTE) {
		UInt32 b, g, r, a, color32bit;
		UInt32 pixelBufferIdx = 0;
		UInt32 pixelBuffer8BitIdx = 0;
		for (UInt32 i = 0; i < this->textureHeight; i++) {
			for (UInt32 k = 0; k < this->textureWidth; k++) {
				b = pixelBuffer8Bit[pixelBuffer8BitIdx + 0] << 24;
				g = pixelBuffer8Bit[pixelBuffer8BitIdx + 1] << 16;
				r = pixelBuffer8Bit[pixelBuffer8BitIdx + 2] << 8;
				a = pixelBuffer8Bit[pixelBuffer8BitIdx + 3];
				color32bit = b | g | r | a;
				this->pixelBuffer[pixelBufferIdx] = color32bit;
				pixelBufferIdx++;
				pixelBuffer8BitIdx += 4;
			}
		}
		free(pixelBuffer8Bit);
	}

	return this->pixelBuffer;
}