Exemplo n.º 1
0
bool
HdrInput::read_native_scanline (int y, int z, void *data)
{
    if (m_next_scanline > y) {
        // User is trying to read an earlier scanline than the one we're
        // up to.  Easy fix: close the file and re-open.
        ImageSpec dummyspec;
        int subimage = current_subimage();
        int miplevel = current_miplevel();
        if (! close ()  ||
            ! open (m_filename, dummyspec)  ||
            ! seek_subimage (subimage, miplevel, dummyspec))
            return false;    // Somehow, the re-open failed
        assert (m_next_scanline == 0 && current_subimage() == subimage &&
                current_miplevel() == miplevel);
    }
    while (m_next_scanline <= y) {
        // Keep reading until we're read the scanline we really need
        int r = RGBE_ReadPixels_RLE (m_fd, (float *)data, m_spec.width, 1, rgbe_error);
        ++m_next_scanline;
        if (r != RGBE_RETURN_SUCCESS) {
            error ("%s", rgbe_error);
            return false;
        }
    }
    return true;
}
Exemplo n.º 2
0
void HDRTextureCube::Load(const string fileName) {
	string path = string(HDR_RELATIVE_PATH) + fileName + string(HDR_EXT);	

	cout << "Loading hdr texture " << path << ".." << endl;

	FILE* file = fopen(path.c_str(), "rb");
	
	RGBE_ReadHeader(file, &width, &height, NULL);
	data = new float[3 * width * height];
	RGBE_ReadPixels_RLE(file, data, width, height);
	
	fclose(file);

	LoadFaces();

	cout << " - width : " << width << "px" << endl; 
	cout << " - height : " << height << "px" << endl;
	cout << " - memory size : " << (3 * width * height * sizeof(float)) / 8 << " bytes" << endl;
	cout << "Generating texture cube.." << endl;
}
Exemplo n.º 3
0
bool readHDR(const char* path, cv::Mat& hdr)
{
  FILE* fp = std::fopen(path, "rb");
  if (!fp) {
    std::fprintf(stderr, "cannot load %s\n", path);
    return false;
  }
  
  rgbe_header_info info;
  int rgbeStatus, width, height;
  
  rgbeStatus = RGBE_ReadHeader(fp, &width, &height, &info);
  if (rgbeStatus != RGBE_RETURN_SUCCESS) {
    std::fclose(fp);
    return false;
  }
  
  cv::Mat_<cv::Vec3f> tmp(height, width);
  float* data = (float*)static_cast<void*>(tmp.ptr());
  
  rgbeStatus = RGBE_ReadPixels_RLE(fp, data, width, height);
  if (rgbeStatus != RGBE_RETURN_SUCCESS) {
    std::fclose(fp);
    return false;
  }
  
  for (int y = 0; y < height; ++y) {
    for (int x = 0; x < width; ++x) {
      cv::Vec3f& c = tmp.at<cv::Vec3f>(y, x);
      std::swap(c[0], c[2]);
    }
  }
  
  std::fclose(fp);
  hdr = tmp;
  
  return true;
}
Exemplo n.º 4
0
void Image::loadFromHdr(const std::string &filename)
{
	// Try to open the file
	FILE* f = fopen(filename.c_str(), "rb");
	if (f == 0)
		throw Exception("File missing : " + filename);
	
	// Read image header
	int res = RGBE_ReadHeader(f, &sizeX, &sizeY, 0);
	if (res < 0)
		throw Exception("Incorrect .hdr file : " + filename);

	// Read image data
	data = new uchar[sizeof(float)*3*sizeX*sizeY];
	res = RGBE_ReadPixels_RLE(f, reinterpret_cast<float*>(data), sizeX, sizeY);

	// Set format and type
	format = GL_RGB;
	type = GL_FLOAT;

	// Close the file
	fclose(f);
}
Exemplo n.º 5
0
int main(int argc, const char **argv)
{
	FILE *fp;
	int width, height;
	struct MipOutput *mip;
	struct FrameBuffer *hdr;
	rgbe_header_info info;

	if (argc == 2 && strcmp(argv[1], "--help") == 0) {
		printf("%s", USAGE);
		return 0;
	}

	if (argc != 3) {
		fprintf(stderr, "error: invalid number of arguments.\n");
		fprintf(stderr, "%s", USAGE);
		return -1;
	}

	errno = 0;
	if ((fp = fopen(argv[1], "rb")) == NULL) {
		fprintf(stderr, "error: %s: %s\n", argv[1], strerror(errno));
		return -1;
	}

	RGBE_ReadHeader(fp, &width, &height, &info);
	if (width <= 0 || height <= 0) {
		fprintf(stderr, "error: invalid image size detected: %d x %d\n", width, height);
		return -1;
	}

	hdr = FbNew();
	if (hdr == NULL) {
		fprintf(stderr, "error: could not allocate framebuffer itself\n");
		return -1;
	}

	if (FbResize(hdr, width, height, 3) == NULL) {
		fprintf(stderr, "error: could not allocate framebuffer: %d x %d\n", width, height);
		return -1;
	}
	RGBE_ReadPixels_RLE(fp, FbGetWritable(hdr, 0, 0, 0), width, height);

	if ((mip = MipOpenOutputFile(argv[2])) == NULL) {
		fprintf(stderr, "error: couldn't open output file\n");
		FbFree(hdr);
		return -1;
	}

	MipGenerateFromSourceData(mip, FbGetReadOnly(hdr, 0, 0, 0), width, height, 3);
	printf("input res: %d, %d\n", width, height);
	printf("output res: %d, %d\n", mip->width, mip->height);

	MipWriteFile(mip);

	MipCloseOutputFile(mip);
	FbFree(hdr);
	fclose(fp);

	return 0;
}
bool CDX9TextureObject::LoadHDRFile( TCHAR * file )
{
	LPDIRECT3DDEVICE9 pDevice;
	if( !m_Renderer )
	{
		return false;
	}
	pDevice = (LPDIRECT3DDEVICE9)m_Renderer->GetAPIDevice();
	if( !pDevice )
	{
		return false;
	}
	
	int iWidth;
	int iHeight;
	// Read in the HDR light probe.
	FILE* fp = fopen( file, "rb" );

	if( fp )
	{
		rgbe_header_info info;
		RGBE_ReadHeader( fp, &iWidth, &iHeight, &info );

		// We really don't need this
		float fExposure = info.exposure;
		float fGamma  = info.gamma;

		// Create a float array to read in the RGB components
		float* m_fHDRPixels = new float[3 * iWidth * iHeight];
		memset( m_fHDRPixels, 0, 3 * iWidth * iHeight * sizeof( float ) );

		RGBE_ReadPixels_RLE( fp, m_fHDRPixels, iWidth, iHeight );

		if( FAILED( D3DXCreateTexture( pDevice, iWidth, iHeight, 1, 0, 
						D3DFMT_A16B16G16R16F, D3DPOOL_MANAGED, &m_Texture ) ) )
		{
			m_ToolBox->Log( LOGERROR, _T("Could not load radience(.hdr) file\n") );
			return false;
		}

		// Convert the 32-bit floats into 16-bit floats and include the alpha component
		D3DXFLOAT16* m_fHDR = new D3DXFLOAT16[4 * iWidth * iHeight];

		int j = 0;
		for( int i = 0; i < 4 * iWidth * iHeight; i += 4 )
		{
			m_fHDR[i] = m_fHDRPixels[i - j];
			m_fHDR[i + 1] = m_fHDRPixels[i + 1 - j];
			m_fHDR[i + 2] = m_fHDRPixels[i + 2 - j];
			m_fHDR[i + 3] = 1.0f;
			j++;
		}

		// Lock the texture and copy the pixel data into it
		D3DLOCKED_RECT lr;
		if(FAILED( m_Texture->LockRect( 0, &lr, NULL, 0 ) ) )
		{
			m_ToolBox->Log( LOGERROR, _T("Could not lock radience(.hdr) file for writing\n") );
			return false;
		}

		memcpy( (D3DXFLOAT16*)lr.pBits, m_fHDR, 4 * iWidth * iHeight
				* sizeof( D3DXFLOAT16 ) );

		if(FAILED( m_Texture->UnlockRect( 0 ) ) )
		{
			m_ToolBox->Log( LOGERROR, _T("Could not unlock radience(.hdr) file for writing\n") );
			return false;
		}

		delete[] m_fHDRPixels;
		delete[] m_fHDR;

		fclose( fp );

		if( m_Texture )
		{
			m_Texture = m_Texture;
			m_Filename = file;
			m_bRenderTarget = false;		
			D3DSURFACE_DESC tempDesc;
			m_Texture->GetLevelDesc(0, &tempDesc);
			if( m_AutoGenMips )
			{
				m_Texture->GenerateMipSubLevels();
			}
			m_Width = tempDesc.Width;
			m_Height = tempDesc.Height;
			m_ColorDepth = 64;
			return true;
		}
	}
	return false;
}
Exemplo n.º 7
0
int RD(FILE* fp, float *data, int width, int height, char *errbuf)
{
    return RGBE_ReadPixels_RLE(fp, data, width, height, errbuf);
}