bool Image::SaveJPG(const String & fileName, int quality) { FileSystem* fileSystem = GetSubsystem<FileSystem>(); if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName))) { LOGERROR("Access denied to " + fileName); return false; } if (IsCompressed()) { LOGERROR("Can not save compressed image to JPG"); return false; } if (data_) return jo_write_jpg(fileName.CString(), data_.Get(), width_, height_, components_, quality) != 0; else return false; }
bool File::Open(const String& fileName, FileMode mode) { Close(); FileSystem* fileSystem = GetSubsystem<FileSystem>(); if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName))) { LOGERROR("Access denied to " + fileName); return false; } #ifdef ANDROID if (fileName.StartsWith("/apk/")) { if (mode != FILE_READ) { LOGERROR("Only read mode is supported for asset files"); return false; } assetHandle_ = SDL_RWFromFile(fileName.Substring(5).CString(), "rb"); if (!assetHandle_) { LOGERROR("Could not open asset file " + fileName); return false; } else { fileName_ = fileName; mode_ = mode; position_ = 0; offset_ = 0; checksum_ = 0; size_ = assetHandle_->hidden.androidio.size; readBuffer_ = new unsigned char[READ_BUFFER_SIZE]; readBufferOffset_ = 0; readBufferSize_ = 0; return true; } } #endif if (fileName.Empty()) { LOGERROR("Could not open file with empty name"); return false; } #ifdef WIN32 handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode]); #else handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode]); #endif if (!handle_) { LOGERROR("Could not open file " + fileName); return false; } fileName_ = fileName; mode_ = mode; position_ = 0; offset_ = 0; checksum_ = 0; compressed_ = false; fseek((FILE*)handle_, 0, SEEK_END); size_ = ftell((FILE*)handle_); fseek((FILE*)handle_, 0, SEEK_SET); return true; }
bool File::OpenInternal(const String& fileName, FileMode mode, bool fromPackage) { Close(); compressed_ = false; readSyncNeeded_ = false; writeSyncNeeded_ = false; FileSystem* fileSystem = GetSubsystem<FileSystem>(); if (fileSystem && !fileSystem->CheckAccess(GetPath(fileName))) { ATOMIC_LOGERRORF("Access denied to %s", fileName.CString()); return false; } if (fileName.Empty()) { ATOMIC_LOGERROR("Could not open file with empty name"); return false; } #ifdef __ANDROID__ if (ATOMIC_IS_ASSET(fileName)) { if (mode != FILE_READ) { ATOMIC_LOGERROR("Only read mode is supported for Android asset files"); return false; } assetHandle_ = SDL_RWFromFile(ATOMIC_ASSET(fileName), "rb"); if (!assetHandle_) { ATOMIC_LOGERRORF("Could not open Android asset file %s", fileName.CString()); return false; } else { fileName_ = fileName; mode_ = mode; position_ = 0; if (!fromPackage) { size_ = SDL_RWsize(assetHandle_); offset_ = 0; } checksum_ = 0; return true; } } #endif #ifdef _WIN32 handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode]); #else handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode]); #endif // If file did not exist in readwrite mode, retry with write-update mode if (mode == FILE_READWRITE && !handle_) { #ifdef _WIN32 handle_ = _wfopen(GetWideNativePath(fileName).CString(), openMode[mode + 1]); #else handle_ = fopen(GetNativePath(fileName).CString(), openMode[mode + 1]); #endif } if (!handle_) { ATOMIC_LOGERRORF("Could not open file %s", fileName.CString()); return false; } if (!fromPackage) { fseek((FILE*)handle_, 0, SEEK_END); long size = ftell((FILE*)handle_); fseek((FILE*)handle_, 0, SEEK_SET); if (size > M_MAX_UNSIGNED) { ATOMIC_LOGERRORF("Could not open file %s which is larger than 4GB", fileName.CString()); Close(); size_ = 0; return false; } size_ = (unsigned)size; offset_ = 0; } fileName_ = fileName; mode_ = mode; position_ = 0; checksum_ = 0; return true; }