| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 DLOG(ERROR) << "Failed to load image \"" << filename | 212 DLOG(ERROR) << "Failed to load image \"" << filename |
| 213 << "\": unknown file type"; | 213 << "\": unknown file type"; |
| 214 return false; | 214 return false; |
| 215 } | 215 } |
| 216 | 216 |
| 217 // Given an arbitrary bitmap file, load it all into memory and then call our | 217 // Given an arbitrary bitmap file, load it all into memory and then call our |
| 218 // stream loader | 218 // stream loader |
| 219 bool Bitmap::LoadFromFile(const FilePath &filepath, | 219 bool Bitmap::LoadFromFile(const FilePath &filepath, |
| 220 ImageFileType file_type, | 220 ImageFileType file_type, |
| 221 bool generate_mipmaps) { | 221 bool generate_mipmaps) { |
| 222 // Open the file | 222 // Open the file. |
| 223 bool result = false; |
| 223 String filename = FilePathToUTF8(filepath); | 224 String filename = FilePathToUTF8(filepath); |
| 224 FILE *file = OpenFile(filepath, "rb"); | 225 FILE *file = OpenFile(filepath, "rb"); |
| 225 | 226 |
| 226 if (!file) { | 227 if (!file) { |
| 227 DLOG(ERROR) << "bitmap file not found \"" << filename << "\""; | 228 DLOG(ERROR) << "bitmap file not found \"" << filename << "\""; |
| 228 return false; | 229 } else { |
| 230 // Determine the file's length |
| 231 int64 file_size64; |
| 232 if (!GetFileSize(filepath, &file_size64)) { |
| 233 DLOG(ERROR) << "error getting bitmap file size \"" << filename << "\""; |
| 234 } else { |
| 235 if (file_size64 > 0xffffffffLL) { |
| 236 DLOG(ERROR) << "bitmap file is too large \"" << filename << "\""; |
| 237 } else { |
| 238 size_t file_length = static_cast<size_t>(file_size64); |
| 239 |
| 240 // Load the compressed image data into memory |
| 241 MemoryBuffer<uint8> file_contents(file_length); |
| 242 uint8 *p = file_contents; |
| 243 if (fread(p, file_length, 1, file) != 1) { |
| 244 DLOG(ERROR) << "error reading bitmap file \"" << filename << "\""; |
| 245 } else { |
| 246 // And create the bitmap from a memory stream |
| 247 MemoryReadStream stream(file_contents, file_length); |
| 248 result = LoadFromStream(&stream, filename, file_type, |
| 249 generate_mipmaps); |
| 250 } |
| 251 } |
| 252 } |
| 253 CloseFile(file); |
| 229 } | 254 } |
| 230 | 255 |
| 231 // Determine the file's length | |
| 232 int64 file_size64; | |
| 233 if (!GetFileSize(filepath, &file_size64)) { | |
| 234 DLOG(ERROR) << "error getting bitmap file size \"" << filename << "\""; | |
| 235 CloseFile(file); | |
| 236 return false; | |
| 237 } | |
| 238 if (file_size64 > 0xffffffffLL) { | |
| 239 DLOG(ERROR) << "bitmap file is too large \"" << filename << "\""; | |
| 240 return false; | |
| 241 } | |
| 242 size_t file_length = static_cast<size_t>(file_size64); | |
| 243 | |
| 244 // Load the compressed image data into memory | |
| 245 MemoryBuffer<uint8> file_contents(file_length); | |
| 246 uint8 *p = file_contents; | |
| 247 if (fread(p, file_length, 1, file) != 1) { | |
| 248 DLOG(ERROR) << "error reading bitmap file \"" << filename << "\""; | |
| 249 return false; | |
| 250 } | |
| 251 | |
| 252 // And create the bitmap from a memory stream | |
| 253 MemoryReadStream stream(file_contents, file_length); | |
| 254 bool result = LoadFromStream(&stream, filename, file_type, generate_mipmaps); | |
| 255 CloseFile(file); | |
| 256 | |
| 257 return result; | 256 return result; |
| 258 } | 257 } |
| 259 | 258 |
| 260 // Given a RawData object containing image data in one of our known formats, | 259 // Given a RawData object containing image data in one of our known formats, |
| 261 // decide which image format it is and call the correct loading function. | 260 // decide which image format it is and call the correct loading function. |
| 262 bool Bitmap::LoadFromRawData(RawData *raw_data, | 261 bool Bitmap::LoadFromRawData(RawData *raw_data, |
| 263 ImageFileType file_type, | 262 ImageFileType file_type, |
| 264 bool generate_mipmaps) { | 263 bool generate_mipmaps) { |
| 265 String filename = raw_data->uri(); | 264 String filename = raw_data->uri(); |
| 266 | 265 |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 967 return false; | 966 return false; |
| 968 } | 967 } |
| 969 return true; | 968 return true; |
| 970 } | 969 } |
| 971 | 970 |
| 972 ObjectBase::Ref Bitmap::Create(ServiceLocator* service_locator) { | 971 ObjectBase::Ref Bitmap::Create(ServiceLocator* service_locator) { |
| 973 return ObjectBase::Ref(new Bitmap(service_locator)); | 972 return ObjectBase::Ref(new Bitmap(service_locator)); |
| 974 } | 973 } |
| 975 | 974 |
| 976 } // namespace o3d | 975 } // namespace o3d |
| OLD | NEW |