bool HdrInput::seek_subimage(int subimage, int miplevel) { // HDR doesn't support multiple subimages or mipmaps if (subimage != 0 || miplevel != 0) return false; // Skip the hard work if we're already on the requested subimage if (subimage == current_subimage()) { return true; } close(); // Check that file exists and can be opened m_fd = Filesystem::fopen(m_filename, "rb"); if (m_fd == NULL) { error("Could not open file \"%s\"", m_filename.c_str()); return false; } rgbe_header_info h; int width, height; int r = RGBE_ReadHeader(m_fd, &width, &height, &h, rgbe_error); if (r != RGBE_RETURN_SUCCESS) { error("%s", rgbe_error); close(); return false; } m_spec = ImageSpec(width, height, 3, TypeDesc::FLOAT); if (h.valid & RGBE_VALID_GAMMA) { // Round gamma to the nearest hundredth to prevent stupid // precision choices and make it easier for apps to make // decisions based on known gamma values. For example, you want // 2.2, not 2.19998. float g = float(1.0 / h.gamma); g = roundf(100.0 * g) / 100.0f; m_spec.attribute("oiio:Gamma", g); if (g == 1.0f) m_spec.attribute("oiio:ColorSpace", "linear"); else m_spec.attribute("oiio:ColorSpace", Strutil::sprintf("GammaCorrected%.2g", g)); } else { // Presume linear color space m_spec.attribute("oiio:ColorSpace", "linear"); } if (h.valid & RGBE_VALID_ORIENTATION) m_spec.attribute("Orientation", h.orientation); // FIXME -- should we do anything about exposure, software, // pixaspect, primaries? (N.B. rgbe.c doesn't even handle most of them) m_subimage = subimage; m_next_scanline = 0; return true; }
bool HdrInput::seek_subimage (int subimage, int miplevel, ImageSpec &newspec) { // HDR doesn't support multiple subimages or mipmaps if (subimage != 0 || miplevel != 0) return false; // Skip the hard work if we're already on the requested subimage if (subimage == current_subimage()) { newspec = spec(); return true; } close(); // Check that file exists and can be opened m_fd = Filesystem::fopen (m_filename, "rb"); if (m_fd == NULL) { error ("Could not open file \"%s\"", m_filename.c_str()); return false; } rgbe_header_info h; int width, height; int r = RGBE_ReadHeader (m_fd, &width, &height, &h, rgbe_error); if (r != RGBE_RETURN_SUCCESS) { error ("%s", rgbe_error); close (); return false; } m_spec = ImageSpec (width, height, 3, TypeDesc::FLOAT); if (h.valid & RGBE_VALID_GAMMA) m_spec.attribute ("oiio:Gamma", h.gamma); if (h.valid & RGBE_VALID_ORIENTATION) m_spec.attribute ("Orientation", h.orientation); // FIXME -- should we do anything about exposure, software, // pixaspect, primaries? (N.B. rgbe.c doesn't even handle most of them) m_subimage = subimage; m_next_scanline = 0; newspec = m_spec; return true; }
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; }
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; }
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); }
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; }
int RH(FILE* fp, int *width, int *height, rgbe_header_info *info, char *errbuf) { return RGBE_ReadHeader(fp, width, height, info, errbuf); }