/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This application is open source and may be redistributed and/or modified under * the terms of the GNU Public License (GPL) version 1.0 or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ #include "PhotoArchive.h" #include #include #include #include #include #include const std::string FILE_IDENTIFER("osgphotoalbum photo archive"); class MyGraphicsContext { public: MyGraphicsContext() { osg::ref_ptr traits = new osg::GraphicsContext::Traits; traits->x = 0; traits->y = 0; traits->width = 1; traits->height = 1; traits->windowDecoration = false; traits->doubleBuffer = false; traits->sharedContext = 0; traits->pbuffer = true; _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); if (!_gc) { osg::notify(osg::NOTICE)<<"Failed to create pbuffer, failing back to normal graphics window."<pbuffer = false; _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); } if (_gc.valid()) { _gc->realize(); _gc->makeCurrent(); std::cout<<"Realized window"<isRealized(); } private: osg::ref_ptr _gc; }; PhotoArchive::PhotoArchive(const std::string& filename) { readPhotoIndex(filename); } bool PhotoArchive::readPhotoIndex(const std::string& filename) { std::ifstream in(filename.c_str()); char* fileIndentifier = new char [FILE_IDENTIFER.size()]; in.read(fileIndentifier,FILE_IDENTIFER.size()); if (FILE_IDENTIFER!=fileIndentifier) return false; unsigned int numPhotos; in.read((char*)&numPhotos,sizeof(numPhotos)); _photoIndex.resize(numPhotos); in.read((char*)&_photoIndex.front(),sizeof(PhotoHeader)*numPhotos); // success record filename. _archiveFileName = filename; return true; } void PhotoArchive::getImageFileNameList(FileNameList& filenameList) { for(PhotoIndexList::const_iterator itr=_photoIndex.begin(); itr!=_photoIndex.end(); ++itr) { filenameList.push_back(std::string(itr->filename)); } } osg::Image* PhotoArchive::readImage(const std::string& filename, unsigned int target_s, unsigned target_t, float& original_s, float& original_t) { for(PhotoIndexList::const_iterator itr=_photoIndex.begin(); itr!=_photoIndex.end(); ++itr) { if (filename==itr->filename) { const PhotoHeader& photoHeader = *itr; if (target_s <= photoHeader.thumbnail_s && target_t <= photoHeader.thumbnail_t && photoHeader.thumbnail_position != 0) { std::ifstream in(_archiveFileName.c_str(),std::ios::in | std::ios::binary); // find image in.seekg(photoHeader.thumbnail_position); // read image header ImageHeader imageHeader; in.read((char*)&imageHeader,sizeof(ImageHeader)); unsigned char* data = new unsigned char[imageHeader.size]; in.read((char*)data,imageHeader.size); osg::Image* image = new osg::Image; image->setImage(photoHeader.thumbnail_s,photoHeader.thumbnail_t,1, imageHeader.pixelFormat,imageHeader.pixelFormat,imageHeader.type, data,osg::Image::USE_NEW_DELETE); original_s = photoHeader.original_s; original_t = photoHeader.original_t; return image; } if (photoHeader.fullsize_s && photoHeader.fullsize_t && photoHeader.fullsize_position != 0) { std::ifstream in(_archiveFileName.c_str(),std::ios::in | std::ios::binary); // find image in.seekg(photoHeader.fullsize_position); // read image header ImageHeader imageHeader; in.read((char*)&imageHeader,sizeof(ImageHeader)); unsigned char* data = new unsigned char[imageHeader.size]; in.read((char*)data,imageHeader.size); osg::Image* image = new osg::Image; image->setImage(photoHeader.fullsize_s,photoHeader.fullsize_t,1, imageHeader.pixelFormat,imageHeader.pixelFormat,imageHeader.type, data,osg::Image::USE_NEW_DELETE); original_s = photoHeader.original_s; original_t = photoHeader.original_t; return image; } } } return NULL; } void PhotoArchive::buildArchive(const std::string& filename, const FileNameList& imageList, unsigned int thumbnailSize, unsigned int maximumSize, bool /*compressed*/) { PhotoIndexList photoIndex; photoIndex.reserve(imageList.size()); for(FileNameList::const_iterator fitr=imageList.begin(); fitr!=imageList.end(); ++fitr) { PhotoHeader header; // set name strncpy(header.filename,fitr->c_str(),255); header.filename[255]=0; header.thumbnail_s = thumbnailSize; header.thumbnail_t = thumbnailSize; header.thumbnail_position = 0; header.fullsize_s = thumbnailSize; header.fullsize_t = thumbnailSize; header.fullsize_position = 0; photoIndex.push_back(header); } std::cout<<"Building photo archive containing "< image = osgDB::readImageFile(photoHeader.filename); std::cout<<"done."<< std::endl; photoHeader.original_s = image->s(); photoHeader.original_t = image->t(); { std::cout<<" creating thumbnail image..."; // first need to rescale image to the require thumbnail size unsigned int newTotalSize = image->computeRowWidthInBytes(thumbnailSize,image->getPixelFormat(),image->getDataType(),image->getPacking())* thumbnailSize; // need to sort out what size to really use... unsigned char* newData = new unsigned char [newTotalSize]; if (!newData) { // should we throw an exception??? Just return for time being. osg::notify(osg::FATAL) << "Error scaleImage() did not succeed : out of memory."<getPacking()); glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking()); GLint status = gluScaleImage(image->getPixelFormat(), image->s(), image->t(), image->getDataType(), image->data(), thumbnailSize, thumbnailSize, image->getDataType(), newData); if (status!=0) { delete [] newData; osg::notify(osg::WARN) << "Error scaleImage() did not succeed : errorString = "<getInternalTextureFormat(); imageHeader.pixelFormat = image->getPixelFormat(); imageHeader.type = image->getDataType(); imageHeader.size = newTotalSize; // write out image header and image data. out.write((char*)&imageHeader,sizeof(ImageHeader)); out.write((char*)newData,imageHeader.size); delete [] newData; std::cout<<"done."<< std::endl; } { std::cout<<" creating fullsize image...";std::cout.flush(); photoHeader.fullsize_s = osg::minimum((unsigned int)image->s(),maximumSize); photoHeader.fullsize_t = osg::minimum((unsigned int)image->t(),maximumSize); photoHeader.fullsize_position = (unsigned int)out.tellp(); // first need to rescale image to the require thumbnail size unsigned int newTotalSize = image->computeRowWidthInBytes(photoHeader.fullsize_s,image->getPixelFormat(),image->getDataType(),image->getPacking())* photoHeader.fullsize_t; // need to sort out what size to really use... unsigned char* newData = new unsigned char [newTotalSize]; if (!newData) { // should we throw an exception??? Just return for time being. osg::notify(osg::FATAL) << "Error scaleImage() did not succeed : out of memory."<getPacking()); glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking()); GLint status = gluScaleImage(image->getPixelFormat(), image->s(), image->t(), image->getDataType(), image->data(), photoHeader.fullsize_s, photoHeader.fullsize_t, image->getDataType(), newData); if (status!=0) { delete [] newData; osg::notify(osg::WARN) << "Error scaleImage() did not succeed : errorString = "<getInternalTextureFormat(); imageHeader.pixelFormat = image->getPixelFormat(); imageHeader.type = image->getDataType(); imageHeader.size = newTotalSize; out.write((char*)&imageHeader,sizeof(ImageHeader)); out.write((char*)newData,imageHeader.size); //out.write((char*)image->data(),imageHeader.size); delete [] newData; std::cout<<"done."<< std::endl; } } // rewrite photo index now it has the correct sizes set out.seekp(startOfPhotoIndex); out.write((char*)&photoIndex.front(),sizeof(PhotoHeader)*photoIndex.size()); }