bool CBaseTexture::LoadFromFileInternal(const std::string& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels, const std::string& strMimeType) { if (URIUtils::HasExtension(texturePath, ".dds")) { // special case for DDS images CDDSImage image; if (image.ReadFile(texturePath)) { Update(image.GetWidth(), image.GetHeight(), 0, image.GetFormat(), image.GetData(), false); return true; } return false; } unsigned int width = maxWidth ? std::min(maxWidth, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); unsigned int height = maxHeight ? std::min(maxHeight, g_Windowing.GetMaxTextureSize()) : g_Windowing.GetMaxTextureSize(); // Read image into memory to use our vfs XFILE::CFile file; XFILE::auto_buffer buf; if (file.LoadFile(texturePath, buf) <= 0) return false; CURL url(texturePath); // make sure resource:// paths are properly resolved if (url.IsProtocol("resource")) { std::string translatedPath; if (XFILE::CResourceFile::TranslatePath(url, translatedPath)) url.Parse(translatedPath); } // handle xbt:// paths differently because it allows loading the texture directly from memory if (url.IsProtocol("xbt")) { XFILE::CXbtFile xbtFile; if (!xbtFile.Open(url)) return false; return LoadFromMemory(xbtFile.GetImageWidth(), xbtFile.GetImageHeight(), 0, xbtFile.GetImageFormat(), xbtFile.HasImageAlpha(), reinterpret_cast<unsigned char*>(buf.get())); } IImage* pImage; if(strMimeType.empty()) pImage = ImageFactory::CreateLoader(texturePath); else pImage = ImageFactory::CreateLoaderFromMimeType(strMimeType); if (!LoadIImage(pImage, (unsigned char *)buf.get(), buf.size(), width, height, autoRotate)) { CLog::Log(LOGDEBUG, "%s - Load of %s failed.", __FUNCTION__, texturePath.c_str()); delete pImage; return false; } delete pImage; return true; }
LRESULT SSkinGif::OnAttrSrc( const SStringW &strValue,BOOL bLoading ) { SStringTList strLst; size_t nSegs=ParseResID(S_CW2T(strValue),strLst); LPBYTE pBuf=NULL; size_t szBuf=0; if(nSegs == 2) { szBuf=GETRESPROVIDER->GetRawBufferSize(strLst[0],strLst[1]); if(szBuf) { pBuf=new BYTE[szBuf]; GETRESPROVIDER->GetRawBuffer(strLst[0],strLst[1],pBuf,szBuf); } }else {//自动从GIF资源类型里查找资源 szBuf=GETRESPROVIDER->GetRawBufferSize(_T("gif"),strLst[0]); if(szBuf) { pBuf=new BYTE[szBuf]; GETRESPROVIDER->GetRawBuffer(_T("gif"),strLst[0],pBuf,szBuf); } } if(pBuf) { LoadFromMemory(pBuf,szBuf); delete []pBuf; } return S_OK; }
int main(int argc, char* argv[]) { LoadFromFile(); printf("\n\n"); LoadFromMemory(); return 0; }
bool Font::LoadFromResource(LPCTSTR id,LPCTSTR group) { HRSRC resource = FindResource(NULL,id,group); if(!resource) return false; HGLOBAL global = LoadResource(NULL,resource); if(!global) return false; LPBYTE data = (LPBYTE)LockResource(global); if(!data) return false; if(!LoadFromMemory(data,SizeofResource(NULL,resource))) { UnlockResource(global); FreeResource(global); return false; } UnlockResource(global); FreeResource(global); return true; }
BOOL CMime::Load(LPCTSTR pFile) {_STT(); // Out with the old Destroy(); CWinFile file; // Open the file if ( !file.OpenExisting( pFile, GENERIC_READ ) ) return FALSE; // Get file size DWORD size = file.Size(); if ( size == 0 ) return FALSE; // Allocate memory TMem< BYTE > buf; if ( !buf.allocate( size + 1 ) ) return FALSE; // Read in the data into ram DWORD read; if ( !file.Read( buf, size, &read ) || read != size ) return FALSE; buf[ size ] = 0; // Load the file if ( !LoadFromMemory( buf, size ) ) return FALSE; return TRUE; }
// // Load BTM surface from file // bool CBTMSurface::LoadFromFile ( const char *pFileName ) { void *pFile; //FILE *fp; HXFSFile fp; int size; //fp = fopen( pFileName, "rb" ); fp = xfs->Open( pFileName ); if ( !fp ) return false; //fseek( fp, 0, SEEK_END ); //size = ftell( fp ); //fseek( fp, 0, SEEK_SET ); size = xfs->SizeOf( fp ); pFile = ALLOCATE( size ); //fread( pFile, 1, size, fp ); //fclose( fp ); xfs->Read( fp, pFile, size ); xfs->Close( fp ); LoadFromMemory( pFile ); FREE( pFile ); return true; }
Image::Image(const Image & other) : m_format(ImageFormat::NONE) , m_width(0) , m_height(0) , m_pixels(nullptr) { LoadFromMemory(other.m_width, other.m_height, other.m_format, other.m_pixels); }
Image::Image(u32 width, u32 height, ImageFormat format, const u08 * pixels) : m_format(ImageFormat::NONE) , m_width(0) , m_height(0) , m_pixels(nullptr) { LoadFromMemory(width, height, format, pixels); }
sfMod::Mod::Mod(const void* data, unsigned int size) : SoundStream() { file_ = NULL; name_ = ""; length_ = 0; buffer_.resize(SFMOD_BUFFERSIZE / 2); LoadFromMemory(data, size); }
bool cTextureFont::LoadFromPack( const Uint32& TexId, cPack* Pack, const std::string& FilePackPath ) { if ( NULL != Pack && Pack->IsOpen() && -1 != Pack->Exists( FilePackPath ) ) { SafeDataPointer PData; Pack->ExtractFileToMemory( FilePackPath, PData ); return LoadFromMemory( TexId, reinterpret_cast<const char*> ( PData.Data ), PData.DataSize ); } return false; }
int main(int argc, char* argv[]) { if (argc < 2) { fprintf(stderr, "USAGE: %s <filename.dll>\n", argv[0]); return 1; } if (!LoadFromMemory(argv[1])) { return 2; } return 0; }
void cTextureFontLoader::LoadFont() { mFont = cTextureFont::New( mFontName ); if ( TEF_LT_PATH == mLoadType ) LoadFromPath(); else if ( TEF_LT_MEM == mLoadType ) LoadFromMemory(); else if ( TEF_LT_PACK == mLoadType ) LoadFromPack(); else if ( TEF_LT_TEX == mLoadType ) LoadFromTex(); mFontLoaded = true; }
const CTexture* CGraphicsComponent::LoadTexture(const TResourceFileId aId, const std::string& aFilename) { TFilePtr memFile = iCabinets.GetFile(aId); if (!memFile) { // Try loading by filename if by index did not work memFile = iCabinets.GetFile(aFilename); } if (memFile) { TTexturePtr texture = LoadFromMemory(memFile, aFilename.c_str()); if (texture) { iTextures[aId] = std::move(texture); return &*iTextures[aId]; } } iTextures[aId] = TTexturePtr(); // Store the failure to load return nullptr; }
void tSoundLoader<T>::Start() { if ( NULL != mSndMngr ) { cObjectLoader::Start(); if ( SND_LT_PATH == mLoadType ) LoadFromPath(); else if ( SND_LT_MEM == mLoadType ) LoadFromMemory(); else if ( SND_LT_PACK == mLoadType ) LoadFromPack(); else if ( SND_LT_SAMPLES == mLoadType ) LoadFromSamples(); SetLoaded(); } }
HBITMAP CImageFile::LoadFromFile() { if ( ! IsFile() ) return NULL; HANDLE hMap = CreateFileMapping( m_hFile, NULL, PAGE_READONLY, 0, 0, NULL ); DWORD nPosition = SetFilePointer( m_hFile, 0, NULL, FILE_CURRENT ); DWORD nLength = GetFileSize( m_hFile, NULL ); HBITMAP hBitmap = NULL; if ( LPCVOID pBuffer = MapViewOfFile( hMap, FILE_MAP_READ, 0, nPosition, nLength ) ) { hBitmap = LoadFromMemory( pBuffer, nLength ); UnmapViewOfFile( pBuffer ); } CloseHandle( hMap ); return hBitmap; }
b08 Image::LoadFromFile(const char * filename) { int width, height, format; u08 * pixels = stbi_load(filename, &width, &height, &format, 0); if (!pixels) { std::cerr << stbi_failure_reason() << std::endl; return false; } LoadFromMemory(width, height, static_cast<ImageFormat>(format), pixels); stbi_image_free(pixels); if (m_pixels) return true; return false; }
i32 CCfgFile::Load(xst_castring& strFileName, xst_castring& strGroupName) { m_pFileMgr = CFileManager::GetSingletonPtr(); m_strFileName = strFileName; Resources::FilePtr pFile = m_pFileMgr->LoadFile( strFileName, strGroupName ); if( !pFile.IsNull() ) { m_eFileEncoding = ITextFile::GetEncoding( pFile->GetData().GetPointer(), pFile->GetSize() ); if( ITextFile::IsUTF16( m_eFileEncoding ) ) { ch16* pData = (ch16*)pFile->GetData().GetPointer(); pData[ pFile->GetSize() / sizeof( u16 ) ] = 0; } return LoadFromMemory( pFile->GetData().GetPointer(), pFile->GetData().GetSize(), m_eFileEncoding ); } return RESULT::FAILED; }
WError WSound::LoadFromWS(basic_filebuf<char>* buff, uint pos, bool bSaveData) { //use the given stream fstream file; if (!buff) return WError(W_INVALIDPARAM); file.set_rdbuf(buff); file.seekg(pos); float temp[3]; //read pitch & volume file.read((char*)temp, 2 * sizeof(float)); alSourcef(m_source, AL_PITCH, temp[0]); alSourcef(m_source, AL_GAIN, temp[1]); //read position & direction file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_POSITION, temp[0], temp[1], temp[2]); file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_DIRECTION, temp[0], temp[1], temp[2]); char numBuffers = 0; file.read((char*)&numBuffers, 1); for (uint i = 0; i < numBuffers; i++) { __SAVEDATA data; file.read((char*)&data.buffer, 4); //buffer index file.read((char*)&data.format, sizeof ALenum); //buffer format file.read((char*)&temp[0], 4); //frequency file.read((char*)&data.dataSize, 4); //size of data data.data = W_SAFE_ALLOC(data.dataSize); file.read((char*)&data.data, m_dataV[i].dataSize); //data WError err = LoadFromMemory(data.buffer, data.data, data.dataSize, data.format, temp[0], bSaveData); W_SAFE_FREE(data.data); return WError(err); } return WError(W_SUCCEEDED); }
bool LWOFile::LoadFromFile(const char *strFile) { // try to open file HANDLE hFile=CreateFileA(strFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile==INVALID_HANDLE_VALUE) { AddError("Could not open file: " + std::string(strFile)); return false; } // get file size LARGE_INTEGER LI_Size; if(!GetFileSizeEx(hFile,&LI_Size)) { AddError("Could not get file size!"); return false; } unsigned int iFileSize=(unsigned int)LI_Size.QuadPart; // allocate memory to hold the file char *pData=new char[iFileSize]; // read file to memory DWORD iBytesRead=0; if(!ReadFile(hFile,pData,iFileSize,&iBytesRead,NULL) || iBytesRead!=iFileSize) { AddError("Reading file failed!"); delete[] pData; CloseHandle(hFile); return false; } // load if(!LoadFromMemory(pData, iFileSize)) return false; delete[] pData; CloseHandle(hFile); return true; }
WError WSound::LoadFromWS(std::string filename, bool bSaveData) { fstream file; file.open(filename, ios::in | ios::binary); if (file.fail()) return WError(W_FILENOTFOUND); float temp[3]; //read pitch & volume file.read((char*)temp, 2 * sizeof(float)); alSourcef(m_source, AL_PITCH, temp[0]); alSourcef(m_source, AL_GAIN, temp[1]); //read position & direction file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_POSITION, temp[0], temp[1], temp[2]); file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_DIRECTION, temp[0], temp[1], temp[2]); char numBuffers = 0; file.read((char*)&numBuffers, 1); for (uint i = 0; i < numBuffers; i++) { __SAVEDATA data; file.read((char*)&data.buffer, 4); //buffer index file.read((char*)&data.format, sizeof ALenum); //buffer format file.read((char*)&temp[0], 4); //frequency file.read((char*)&data.dataSize, 4); //size of data data.data = W_SAFE_ALLOC(data.dataSize); file.read((char*)&data.data, m_dataV[i].dataSize); //data LoadFromMemory(data.buffer, data.data, data.dataSize, data.format, temp[0], bSaveData); W_SAFE_FREE(data.data); } file.close(); return WError(W_SUCCEEDED); }
bool Targa::LoadFromFile(LPCTSTR path) { HANDLE hFile = CreateFile(path,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL); if(hFile == INVALID_HANDLE_VALUE) return false; unsigned char* buffer = new unsigned char[GetFileSize(hFile,NULL)]; if(!buffer) return false; unsigned long read; ReadFile(hFile,buffer,GetFileSize(hFile,NULL),&read,NULL); if(!LoadFromMemory(buffer,read)) { delete buffer; return false; } delete buffer; return true; }
Image& Image::operator=(const Image & other) { LoadFromMemory(other.m_width, other.m_height, other.m_format, other.m_pixels); return *this; }
void Shader::LoadFromFile(const std::string& path) { m_ShaderPath = FileSystem::GetPath(path); m_ShaderFileName = FileSystem::GetFilename(path); LoadFromMemory(FileSystem::LoadTextFile(path)); }
STDMETHODIMP CLTDMLoader::GetObject( LPDMUS_OBJECTDESC pDESC, // Description of the requested object in <t DMUS_OBJECTDESC> structure. REFIID riid, // The interface type to return in <p ppv> LPVOID FAR *ppv) // Receives the interface on success. { HRESULT hr = E_NOTIMPL; EnterCriticalSection(&m_CriticalSection); IDirectMusicObject * pIObject; // At this point, the loader should check with all the objects it already // has loaded. It should look for file name, object guid, and name. // In this case, we are being cheap and looking for only the object // guid, which should be guaranteed to be unique. If the files are all // created with guids, (and producer supports that) this should work // well. // If it sees that the object is already loaded, it should // return a pointer to that one and increment the reference. // It is very important to keep the previously loaded objects // "cached" in this way. Otherwise, objects, like DLS collections, will get loaded // multiple times with a very great expense in memory and efficiency! // This is primarily an issue when object reference each other. For // example, segments reference style and collection objects. if (pDESC->dwValidData & DMUS_OBJ_OBJECT) { CLTDMObjectRef * pObject = NULL; for (pObject = m_pObjectList;pObject;pObject = pObject->m_pNext) { if (pDESC->guidObject == pObject->m_guidObject) { break; } } if (pObject) { hr = E_FAIL; pIObject = pObject->m_pObject; if (pObject->m_pObject) { hr = pObject->m_pObject->QueryInterface( riid, ppv ); } LeaveCriticalSection(&m_CriticalSection); return hr; } } if (pDESC->dwValidData & DMUS_OBJ_FILENAME) { hr = LoadFromFile(pDESC,&pIObject); } else if (pDESC->dwValidData & DMUS_OBJ_MEMORY) { hr = LoadFromMemory(pDESC,&pIObject); } else hr = DMUS_E_LOADER_NOFILENAME; if (SUCCEEDED(hr)) { // If we succeeded in loading it, keep a pointer to it, // addref it, and keep the guid for finding it next time. // To get the GUID, call ParseDescriptor on the object and // it will fill in the fields it knows about, including the // guid. DMUS_OBJECTDESC DESC; memset((void *)&DESC,0,sizeof(DESC)); DESC.dwSize = sizeof (DMUS_OBJECTDESC); pIObject->GetDescriptor(&DESC); if (DESC.dwValidData & DMUS_OBJ_OBJECT) { CLTDMObjectRef * pObject = new CLTDMObjectRef; if (pObject) { pObject->m_guidObject = DESC.guidObject; pObject->m_pNext = m_pObjectList; m_pObjectList = pObject; pObject->m_pObject = pIObject; pIObject->AddRef(); } } hr = pIObject->QueryInterface( riid, ppv ); pIObject->Release(); } LeaveCriticalSection(&m_CriticalSection); return hr; }