Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FileSystemImageSource.cc

Go to the documentation of this file.
00001 #include "FileSystemImageSource.h"
00002 #include "Shared/ImageUtil.h"
00003 #include "Shared/debuget.h"
00004 #include <sys/types.h>
00005 #include <sys/mman.h>
00006 
00007 //better to put this here instead of the header
00008 using namespace std; 
00009 
00010 void FileSystemImageSource::ImageInfo::prepare() {
00011   if(prepared)
00012     return;
00013   if(data==NULL) {
00014     // load the file from disk
00015     FileInfo::prepare();
00016     if(data==NULL)
00017       return;
00018     
00019     // get the image info
00020     width=height=components=0;
00021     size_t totalSize=0;
00022     char * imgbuf = ((char*)NULL)-1;
00023     
00024     image_util::decodeImage(data,size,width,height,components,imgbuf,totalSize); // this call fails (no allocation), but sets image info
00025 
00026     totalSize = width*height*components;
00027     // using mmap to allocate space so we can still use mlock/munlock on it later
00028     imgbuf = static_cast<char*>(mmap(NULL,totalSize,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0));
00029     if(imgbuf==MAP_FAILED) {
00030       std::string err="FileSystemImageSource::ImageInfo::prepare() unable to mmap allocation for image decompression of ";
00031       err+=filename;
00032       perror(err.c_str());
00033       FileInfo::release();
00034       return;
00035     }
00036     
00037     // decompress the image for real this time:
00038     if(!image_util::decodeImage(data,size,width,height,components,imgbuf,totalSize)) {
00039       cerr << "Image decompression failed for " << filename << endl;
00040       munmap(imgbuf,totalSize); // error, give up and clear our memory usage
00041       FileInfo::release();
00042       return; // don't fall through to the prepare() below!
00043     }
00044     
00045     // replace the raw compressed data with the uncompressed image
00046     FileInfo::release();
00047     setData(imgbuf,totalSize);
00048   }
00049   FileInfo::prepare();
00050 }
00051 
00052 /*! @file
00053  * @brief 
00054  * @author Ethan Tira-Thompson (ejt) (Creator)
00055  */

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:38 2016 by Doxygen 1.6.3