void Application::onKey(int key, int scancode, int action, int mods) {
    InputEvent& e = inputEvent;
    e.type = InputType::Key;
    e.action = action;
    e.key = key;
    e.scancode = scancode;
    e.mods = mods;
    e.cursor = context->cursorPosition;
    if (key == GLFW_KEY_F11 && e.action == GLFW_PRESS) {
        for (int i = 0; i < 1000; i++) {
            std::string screenShot = MakeString() << GetDesktopDirectory()
                                     << ALY_PATH_SEPARATOR<<"screenshot"<<std::setw(4)<<std::setfill('0')<<i<<".png";
            if(!FileExists(screenShot)) {
                std::cout<<"Saving "<<screenShot<<std::endl;
                ImageRGBA img;
                getScreenShot(img);
                FlipVertical(img);
                WriteImageToFile(screenShot,img);
                break;
            }
        }
    }

    fireEvent(e);
}
void Application::runOnce(const std::string& fileName) {
    close();
    run(0);
    glfwSwapBuffers(context->window);
    ImageRGBA img;
    getScreenShot(img);
    FlipVertical(img);
    WriteImageToFile(fileName, img);
}
void ac::FlipMirrorAlphaBlend(cv::Mat &frame) {
    cv::Mat copies[2];
    copies[0] = frame.clone();
    copies[1] = frame.clone();
    FlipHorizontal(copies[0]);
    FlipVertical(copies[1]);
    cv::Mat output[2];
    AlphaBlendDouble(copies[0], frame, output[0], 0.5, 0.5);
    AlphaBlendDouble(copies[1], frame, output[1], 0.5, 0.5);
    AlphaBlend(output[0], output[1], frame, 0.5);
    AddInvert(frame);
}
Beispiel #4
0
void Image::FlipVertical()
{
	switch(format)
	{
	case FORMAT_A8:
		FlipVertical((uint8 *)data, width, height);
		break;

	case FORMAT_A16:
	case FORMAT_RGBA5551:
	case FORMAT_RGBA4444:
	case FORMAT_RGB565:
		FlipVertical((uint16 *)data, width, height);
		break;

	case FORMAT_RGBA8888:
		FlipVertical((uint32 *)data, width, height);
		break;

	default:
		DVASSERT(false && "Not implemented");
		break;
	}
}
Beispiel #5
0
void HDRTextureCube::LoadFaces() {
	faces = new Face*[6];
	
	int faceWidth = width / 3;
	int faceHeight = height / 4;
	
	for(int i = 0; i < 6; ++i) {
		faces[i] = new Face();
		faces[i]->data = new float[3 * faceWidth * faceHeight];
		faces[i]->width = faceWidth;
		faces[i]->height = faceHeight;
		faces[i]->currentOffset = 0;
	}
	
	for(int l = 0; l < height; ++l) {
		int jFace = (l - (l % faceHeight)) / faceHeight;
		
		for(int iFace = 0; iFace < 3; ++iFace) {
			Face *face = NULL;			
			int offset = 3 * (faceWidth * iFace + l * width);

			if(iFace == 2 && jFace == 1) face = faces[0]; // POS_Y
			if(iFace == 0 && jFace == 1) face = faces[1]; // NEG_Y
			if(iFace == 1 && jFace == 0) face = faces[2]; // POS_X
			if(iFace == 1 && jFace == 2) face = faces[3]; // NEG_X
			if(iFace == 1 && jFace == 1) face = faces[4]; // POS_Z
			if(iFace == 1 && jFace == 3) face = faces[5]; // NEG_Z
            
			if(face) {
				// the number of components to copy
				int n = sizeof(float) * faceWidth * 3;
				
				memcpy(face->data + face->currentOffset, data + offset, n); 	
				face->currentOffset += (3 * faceWidth);
			}
		}
	}

    // adjust NEG_Z face
    FlipHorizontal(faces[5]); 
    FlipVertical(faces[5]);   
}
Beispiel #6
0
//////////////////////////////////////////////////////////////////////////
//  This is the routine where we create the data being output by the Virtual
//  Camera device.
//	Modified as per red5 to allow for dropped frames and reset of time stamps
//
//  http://comSender.googlecode.com/svn/trunk/
//
//////////////////////////////////////////////////////////////////////////
HRESULT CVCamStream::FillBuffer(IMediaSample *pms)
{
	unsigned char *buffer;
	unsigned char *src;
	unsigned char *dst;
	unsigned char red, grn, blu;

	unsigned int i, imagesize, width, height;
	long l, lDataLen;
	HRESULT hr=S_OK;;
    BYTE *pData;
	HGLRC glCtx;
	float dim[4]; // for saving the viewport dimensions
	VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) m_mt.Format();

	// If graph is inactive stop cueing samples
	if(!m_pParent->IsActive()) {
		return S_FALSE;
	}

	// first get the timing right
	// create some working info
	REFERENCE_TIME rtNow, rtDelta, rtDelta2=0; // delta for dropped, delta 2 for sleep.
	REFERENCE_TIME avgFrameTime = ((VIDEOINFOHEADER*)m_mt.pbFormat)->AvgTimePerFrame;
	
	// Simple method - avoids "stuttering" in yawcam but VLC fails !
	/*
	rtNow = m_rtLastTime;
    m_rtLastTime += avgFrameTime;
    pms->SetTime(&rtNow, &m_rtLastTime);
    pms->SetSyncPoint(TRUE);
	*/

	// What Time is it REALLY ???
	m_pParent->GetSyncSource(&m_pClock);
	
	m_pClock->GetTime(&refSync1);
	
	if(m_pClock) m_pClock->Release();

	if(NumFrames <= 1) {
		// initiate values
		refStart = refSync1; // FirstFrame No Drop.
		refSync2 = 0;
 	}

	// Set the timestamps that will govern playback frame rate.
    // The current time is the sample's start
	rtNow = m_rtLastTime;
	m_rtLastTime = avgFrameTime + m_rtLastTime;
	
	// IAMDropppedFrame. We only have avgFrameTime to generate image.
	// Find generated stream time and compare to real elapsed time
	rtDelta=((refSync1-refStart)-(((NumFrames)*avgFrameTime)-avgFrameTime));

	if(rtDelta-refSync2 < 0) { 
		//we are early
		rtDelta2=rtDelta-refSync2;
		if( abs(rtDelta2/10000)>=1)
			Sleep(abs(rtDelta2/10000));
	} // endif (rtDelta-refSync2 < 0)
	else if(rtDelta/avgFrameTime>NumDroppedFrames) {
		// new dropped frame
		NumDroppedFrames = rtDelta/avgFrameTime;
		// Figure new RT for sleeping
		refSync2 = NumDroppedFrames*avgFrameTime;
		// Our time stamping needs adjustment.
		// Find total real stream time from start time
		rtNow = refSync1-refStart;
		m_rtLastTime = rtNow+avgFrameTime;
		pms->SetDiscontinuity(true);
	} // end else if(rtDelta/avgFrameTime>NumDroppedFrames)

	// The SetTime method sets the stream times when this sample should begin and finish.
    hr = pms->SetTime(&rtNow, &m_rtLastTime);
	// Set true on every sample for uncompressed frames
    hr = pms->SetSyncPoint(true);
	
	// ============== END OF INITIAL TIMING ============

	// Check access to the sample's data buffer
    pms->GetPointer(&pData);
	if(pData == NULL) {
		return NOERROR;
	}

	// Get the current frame size for texture transfers
    imagesize = (unsigned int)pvi->bmiHeader.biSizeImage;
	width = (unsigned int)pvi->bmiHeader.biWidth;
	height = (unsigned int)pvi->bmiHeader.biHeight;
	if(width == 0 || height == 0) {
		return NOERROR;
	}


	// don't do anything if disconnected because it will already have connected
	// previously and something has changed. It can only disconnect after it has connected.
	if(!bDisconnected) {
		
		// This also initialises OpenGL and Glew - bInitialized is set if all is OK
		if(!bInitialized) {
			if(OpenReceiver() == NOERROR) {
				bInitialized = true;
				bDisconnected = false;
				if(!bMemoryMode) {
					InitTexture(g_Width, g_Height); // the size of the camera window
				}
			}
			else {
				bInitialized = false;
				bDisconnected = true;
			}
			return NOERROR;
		}

		// check gl context again
		glCtx = wglGetCurrentContext();
		if(glCtx == NULL) {
			receiver.ReleaseReceiver();
			bInitialized = false;
			bDisconnected = true; // don't try again
			return NOERROR;
		}

		// Check that a texture Sender has not changed size
		// If connected, sizes should be OK, but check again
		unsigned int size = (unsigned int)pms->GetSize();
		imagesize = width*height*3; // also retrieved above
		if(size != imagesize) {
			receiver.ReleaseReceiver();
			bInitialized = false;
			bDisconnected = true; // don't try again
			return NOERROR;
		}
		//
		// everything matches so go ahead with the shared texture read
		if(!bDisconnected) {
			if(bMemoryMode) {
				//
				// Memoryshare instead of interop texture share
				//
				// A memoryshare sender cannot change from startup like a texture sender
				// so the global width and height are always the same as the camera width and height
				//
				// Read the bitmap from shared memory into a local buffer
				buffer = (unsigned char *)malloc(g_Width*g_Height*3);
				if(buffer) {
					// Format for Receiveimage in memoryshare mode is GL_RGB
					// because there is no texture to receive
					if(receiver.ReceiveImage(SharedMemoryName, senderWidth, senderHeight, buffer)) {
						// first check that the image size has not changed
						if(senderWidth == g_Width && senderHeight == g_Height) {
							// all is OK - flip the data for correct orientation and change pixel format
							// VertFlipBuf(buffer, (long)g_Width, (long)g_Height);
							FlipVertical(buffer, g_Width, g_Height);
							dst = (unsigned char *)pData;
							src = buffer;
							for(i=0; i<g_Width*g_Height; i++) {
								red = *src++;
								grn = *src++;
								blu = *src++;
								*dst++ = blu;
								*dst++ = grn;
								*dst++ = red;
							}
						}
						else {
							// Sender has changed the image size
							// no way presently to deal with it so deinit
							receiver.ReleaseReceiver();
							bMemoryMode = false; // it will go to a static image from now on
							bDisconnected = true; // and not try again
						} // end image size check
					} // received OK
					free((void *)buffer);
				} // endif buffer OK
				NumFrames++;
				return NOERROR;
			}
			else if(receiver.ReceiveTexture(SharedMemoryName, senderWidth, senderHeight)) {
				//
				// ======= RENDER THE SHARED TEXTURE INVERTED TO A LOCAL TEXTURE VIA FBO ==========
				//
				// The shared texture can be a different size to the local texture because this is 
				// rendering and not copying. The local texture is always the same size as the filter.
				//
				glMatrixMode(GL_TEXTURE);
				glPushMatrix();
				glLoadIdentity();
				glPushAttrib(GL_TRANSFORM_BIT);
				glGetFloatv(GL_VIEWPORT, dim);
				glViewport(0, 0, g_Width, g_Height); // size of the camera window
				glMatrixMode(GL_PROJECTION);
				glPushMatrix();
				glLoadIdentity(); // reset the current matrix back to its default state
				glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0f, 1.0f);
				glMatrixMode(GL_MODELVIEW);
				glPushMatrix();
				glLoadIdentity();

				// Attach the local texture (desination) to the color buffer in our frame buffer  
				// and draw into it with the shared texture
				glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_fbo); 
				glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_fbo_texture, 0);
				receiver.DrawSharedTexture();
				glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); 
					
				glMatrixMode(GL_MODELVIEW);
				glPopMatrix();
				glMatrixMode(GL_PROJECTION);
				glPopMatrix();
				glPopAttrib();
				glViewport(dim[0], dim[1], dim[2], dim[3]);
				glMatrixMode(GL_TEXTURE);
				glPopMatrix();

				// now read the local texture into the sample's data buffer because the sizes match
				glBindTexture(GL_TEXTURE_2D, g_fbo_texture);
				glEnable(GL_TEXTURE_2D);
				glGetTexImage(GL_TEXTURE_2D, 0,  GL_BGR,  GL_UNSIGNED_BYTE, (void *)pData);
				glBindTexture(GL_TEXTURE_2D, 0);
				glDisable(GL_TEXTURE_2D);

				NumFrames++;
				return NOERROR;

			} // endif received OK
			else {

				receiver.ReleaseReceiver();
				bInitialized = false;
				bDisconnected = true; // don't try again
			} // endif received texture OK
		} // endif texture mode
	} // endif not disconnected

	// drop through to default static image if it did not work
	pms->GetPointer(&pData);
	lDataLen = pms->GetSize();
	for(l = 0; l <lDataLen; ++l) 
		pData[l] = rand();

	NumFrames++;

	return NOERROR;

} // FillBuffer
Beispiel #7
0
bool TgaTexture::load(const char *filename)
{
	FILE *pFile = fopen(filename, "rb");

	if (!pFile)
		return false;

	tgaheader_t tgaHeader;

	// read the TGA header
	fread(&tgaHeader, 1, sizeof(tgaheader_t), pFile);

	// see if the image type is one that we support (RGB, RGB RLE, GRAYSCALe, GRAYSCALe RLE)
	if ( ((tgaHeader.imageTypeCode != TGA_RGB) && (tgaHeader.imageTypeCode != TGA_GRAYSCALE) && 
		 (tgaHeader.imageTypeCode != TGA_RGB_RLE) && (tgaHeader.imageTypeCode != TGA_GRAYSCALE_RLE)) ||
		 tgaHeader.colorMapType != 0)
	{
		fclose(pFile);
		return false;
	}

	// get image width and height
	m_width = tgaHeader.width;
	m_height = tgaHeader.height;

	// colormode -> 3 = bGR, 4 = bGRA
	int colorMode = tgaHeader.bpp / 8;

	// we don't handle less than 24 bit
	if (colorMode < 3)
	{
		fclose(pFile);
		return false;
	}

	m_imageSize = m_width * m_height * colorMode;

	// allocate memory for TGA image data
	m_pImageData = new unsigned char[m_imageSize];

	// skip past the id if there is one
	if (tgaHeader.idLength > 0)
		fseek(pFile, SEEK_CUR, tgaHeader.idLength);

	// read image data
	if (tgaHeader.imageTypeCode == TGA_RGB || tgaHeader.imageTypeCode == TGA_GRAYSCALE)
	{
		fread(m_pImageData, 1, m_imageSize, pFile);
	}
	else 
	{
		// this is an RLE compressed image
		unsigned char id;
		unsigned char length;
		rgba_t color = { 0, 0, 0, 0 };
		unsigned int i = 0;

		while (i < m_imageSize)
		{
			id = fgetc(pFile);

			// see if this is run length data
			if (id >= 128)// & 0x80)
			{
				// find the run length
				length = (unsigned char)(id - 127);

				// next 3 (or 4) bytes are the repeated values
				color.b = (unsigned char)fgetc(pFile);
				color.g = (unsigned char)fgetc(pFile);
				color.r = (unsigned char)fgetc(pFile);

				if (colorMode == 4)
					color.a = (unsigned char)fgetc(pFile);

				// save everything in this run
				while (length > 0)
				{
					m_pImageData[i++] = color.b;
					m_pImageData[i++] = color.g;
					m_pImageData[i++] = color.r;

					if (colorMode == 4)
						m_pImageData[i++] = color.a;

					--length;
				}
			}
			else
			{
				// the number of non RLE pixels
				length = unsigned char(id + 1);

				while (length > 0)
				{
					color.b = (unsigned char)fgetc(pFile);
					color.g = (unsigned char)fgetc(pFile);
					color.r = (unsigned char)fgetc(pFile);

					if (colorMode == 4)
						color.a = (unsigned char)fgetc(pFile);

					m_pImageData[i++] = color.b;
					m_pImageData[i++] = color.g;
					m_pImageData[i++] = color.r;

					if (colorMode == 4)
						m_pImageData[i++] = color.a;

					--length;
				}
			}
		}
	}

	fclose(pFile);

	switch(tgaHeader.imageTypeCode)
	{
	case TGA_RGB:
	case TGA_RGB_RLE:
		if (3 == colorMode)
		{
			m_imageDataFormat = IMAGE_RGB;
			m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE;
			m_colorDepth = 24;
		}
		else
		{
			m_imageDataFormat = IMAGE_RGBA;
			m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE;
			m_colorDepth = 32;
		}
		break;

	case TGA_GRAYSCALE:
	case TGA_GRAYSCALE_RLE:
		m_imageDataFormat = IMAGE_LUMINANCe;
		m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE;
		m_colorDepth = 8;
		break;
	}

	if ((tgaHeader.imageDesc & TOP_LEFT) == TOP_LEFT)
		FlipVertical();

	// swap the red and blue components in the image data
	SwapRedblue();

	if(m_pImageData != NULL){
		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		if (colorMode==3)
			gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_width, m_height, GL_RGB, GL_UNSIGNED_BYTE, m_pImageData);
		else
			gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, m_pImageData);
	}

	return (m_pImageData != NULL);
}
Beispiel #8
0
bool CTargaImage::Load(const char *filename)
{
	FILE *pFile = fopen(filename, "rb");

	if (!pFile)
	{
	        perror("Error opening file");
		return false;
		
	}

	tgaheader_t tgaHeader;

	// read the TGA header
	fread(&tgaHeader, 1, sizeof(tgaheader_t), pFile);

	// see if the image type is one that we support (RGB, RGB RLE, GRAYSCALE, GRAYSCALE RLE)
	if ( ((tgaHeader.imageTypeCode != TGA_RGB) && (tgaHeader.imageTypeCode != TGA_GRAYSCALE) && 
		 (tgaHeader.imageTypeCode != TGA_RGB_RLE) && (tgaHeader.imageTypeCode != TGA_GRAYSCALE_RLE)) ||
		 tgaHeader.colorMapType != 0)
	{
	        perror("File header type was incorrect");
		fclose(pFile);
		return false;
	}

	// get image width and height
	m_width = tgaHeader.width;
	m_height = tgaHeader.height;

	// colormode -> 3 = BGR, 4 = BGRA
	int colorMode = tgaHeader.bpp / 8;

	// we don't handle less than 24 bit
	if (colorMode < 3)
	{
	        perror("Color mode was less than 24");
		fclose(pFile);
		return false;
	}

	m_imageSize = m_width * m_height * colorMode;

	// allocate memory for TGA image data
	m_pImageData = new unsigned char[m_imageSize];

	// skip past the id if there is one
	if (tgaHeader.idLength > 0)
		fseek(pFile, SEEK_CUR, tgaHeader.idLength);

	// read image data
	if (tgaHeader.imageTypeCode == TGA_RGB || tgaHeader.imageTypeCode == TGA_GRAYSCALE)
	{
		fread(m_pImageData, 1, m_imageSize, pFile);
	}
	else 
	{
		// this is an RLE compressed image
		unsigned char id;
		unsigned char length;
		rgba_t color = { 0, 0, 0, 0 };
		unsigned int i = 0;

		while (i < m_imageSize)
		{
			id = fgetc(pFile);

			// see if this is run length data
			if (id >= 128)// & 0x80)
			{
				// find the run length
				length = (unsigned char)(id - 127);

				// next 3 (or 4) bytes are the repeated values
				color.b = (unsigned char)fgetc(pFile);
				color.g = (unsigned char)fgetc(pFile);
				color.r = (unsigned char)fgetc(pFile);

				if (colorMode == 4)
					color.a = (unsigned char)fgetc(pFile);

				// save everything in this run
				while (length > 0)
				{
					m_pImageData[i++] = color.b;
					m_pImageData[i++] = color.g;
					m_pImageData[i++] = color.r;

					if (colorMode == 4)
						m_pImageData[i++] = color.a;

					--length;
				}
			}
			else
			{
				// the number of non RLE pixels
				length = (unsigned char)(id + 1);

				while (length > 0)
				{
					color.b = (unsigned char)fgetc(pFile);
					color.g = (unsigned char)fgetc(pFile);
					color.r = (unsigned char)fgetc(pFile);

					if (colorMode == 4)
						color.a = (unsigned char)fgetc(pFile);

					m_pImageData[i++] = color.b;
					m_pImageData[i++] = color.g;
					m_pImageData[i++] = color.r;

					if (colorMode == 4)
						m_pImageData[i++] = color.a;

					--length;
				}
			}
		}
	}

	fclose(pFile);

	switch(tgaHeader.imageTypeCode)
	{
	case TGA_RGB:
	case TGA_RGB_RLE:
		if (3 == colorMode)
		{
			m_imageDataFormat = IMAGE_RGB;
			m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE;
			m_colorDepth = 24;
		}
		else
		{
			m_imageDataFormat = IMAGE_RGBA;
			m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE;
			m_colorDepth = 32;
		}
		break;

	case TGA_GRAYSCALE:
	case TGA_GRAYSCALE_RLE:
		m_imageDataFormat = IMAGE_LUMINANCE;
		m_imageDataType = IMAGE_DATA_UNSIGNED_BYTE;
		m_colorDepth = 8;
		break;
	}

	if ((tgaHeader.imageDesc & TOP_LEFT) == TOP_LEFT)
		FlipVertical();

	// swap the red and blue components in the image data
	SwapRedBlue();

	return (m_pImageData != NULL);
}