| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include "PhotoArchive.h" |
|---|
| 20 | |
|---|
| 21 | #include <osg/GLU> |
|---|
| 22 | #include <osg/Notify> |
|---|
| 23 | #include <osgDB/ReadFile> |
|---|
| 24 | #include <osgDB/fstream> |
|---|
| 25 | |
|---|
| 26 | #include <osg/GraphicsContext> |
|---|
| 27 | |
|---|
| 28 | #include <iostream> |
|---|
| 29 | #include <string.h> |
|---|
| 30 | |
|---|
| 31 | const std::string FILE_IDENTIFER("osgphotoalbum photo archive"); |
|---|
| 32 | |
|---|
| 33 | PhotoArchive::PhotoArchive(const std::string& filename) |
|---|
| 34 | { |
|---|
| 35 | readPhotoIndex(filename); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | bool PhotoArchive::readPhotoIndex(const std::string& filename) |
|---|
| 39 | { |
|---|
| 40 | osgDB::ifstream in(filename.c_str()); |
|---|
| 41 | |
|---|
| 42 | char* fileIndentifier = new char [FILE_IDENTIFER.size()]; |
|---|
| 43 | in.read(fileIndentifier,FILE_IDENTIFER.size()); |
|---|
| 44 | if (FILE_IDENTIFER!=fileIndentifier) |
|---|
| 45 | { |
|---|
| 46 | delete [] fileIndentifier; |
|---|
| 47 | return false; |
|---|
| 48 | } |
|---|
| 49 | delete [] fileIndentifier; |
|---|
| 50 | |
|---|
| 51 | unsigned int numPhotos; |
|---|
| 52 | in.read((char*)&numPhotos,sizeof(numPhotos)); |
|---|
| 53 | |
|---|
| 54 | _photoIndex.resize(numPhotos); |
|---|
| 55 | |
|---|
| 56 | in.read((char*)&_photoIndex.front(),sizeof(PhotoHeader)*numPhotos); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | _archiveFileName = filename; |
|---|
| 60 | |
|---|
| 61 | return true; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void PhotoArchive::getImageFileNameList(FileNameList& filenameList) |
|---|
| 65 | { |
|---|
| 66 | for(PhotoIndexList::const_iterator itr=_photoIndex.begin(); |
|---|
| 67 | itr!=_photoIndex.end(); |
|---|
| 68 | ++itr) |
|---|
| 69 | { |
|---|
| 70 | filenameList.push_back(std::string(itr->filename)); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | osg::Image* PhotoArchive::readImage(const std::string& filename, |
|---|
| 76 | unsigned int target_s, unsigned target_t, |
|---|
| 77 | float& original_s, float& original_t) |
|---|
| 78 | { |
|---|
| 79 | for(PhotoIndexList::const_iterator itr=_photoIndex.begin(); |
|---|
| 80 | itr!=_photoIndex.end(); |
|---|
| 81 | ++itr) |
|---|
| 82 | { |
|---|
| 83 | if (filename==itr->filename) |
|---|
| 84 | { |
|---|
| 85 | const PhotoHeader& photoHeader = *itr; |
|---|
| 86 | |
|---|
| 87 | if (target_s <= photoHeader.thumbnail_s && |
|---|
| 88 | target_t <= photoHeader.thumbnail_t && |
|---|
| 89 | photoHeader.thumbnail_position != 0) |
|---|
| 90 | { |
|---|
| 91 | osgDB::ifstream in(_archiveFileName.c_str(),std::ios::in | std::ios::binary); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | in.seekg(photoHeader.thumbnail_position); |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | ImageHeader imageHeader; |
|---|
| 98 | in.read((char*)&imageHeader,sizeof(ImageHeader)); |
|---|
| 99 | unsigned char* data = new unsigned char[imageHeader.size]; |
|---|
| 100 | in.read((char*)data,imageHeader.size); |
|---|
| 101 | |
|---|
| 102 | osg::Image* image = new osg::Image; |
|---|
| 103 | image->setImage(photoHeader.thumbnail_s,photoHeader.thumbnail_t,1, |
|---|
| 104 | imageHeader.pixelFormat,imageHeader.pixelFormat,imageHeader.type, |
|---|
| 105 | data,osg::Image::USE_NEW_DELETE); |
|---|
| 106 | |
|---|
| 107 | original_s = photoHeader.original_s; |
|---|
| 108 | original_t = photoHeader.original_t; |
|---|
| 109 | |
|---|
| 110 | return image; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | if (photoHeader.fullsize_s && |
|---|
| 114 | photoHeader.fullsize_t && |
|---|
| 115 | photoHeader.fullsize_position != 0) |
|---|
| 116 | { |
|---|
| 117 | osgDB::ifstream in(_archiveFileName.c_str(),std::ios::in | std::ios::binary); |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | in.seekg(photoHeader.fullsize_position); |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | ImageHeader imageHeader; |
|---|
| 124 | in.read((char*)&imageHeader,sizeof(ImageHeader)); |
|---|
| 125 | unsigned char* data = new unsigned char[imageHeader.size]; |
|---|
| 126 | in.read((char*)data,imageHeader.size); |
|---|
| 127 | |
|---|
| 128 | osg::Image* image = new osg::Image; |
|---|
| 129 | image->setImage(photoHeader.fullsize_s,photoHeader.fullsize_t,1, |
|---|
| 130 | imageHeader.pixelFormat,imageHeader.pixelFormat,imageHeader.type, |
|---|
| 131 | data,osg::Image::USE_NEW_DELETE); |
|---|
| 132 | |
|---|
| 133 | original_s = photoHeader.original_s; |
|---|
| 134 | original_t = photoHeader.original_t; |
|---|
| 135 | |
|---|
| 136 | return image; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | return NULL; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | void PhotoArchive::buildArchive(const std::string& filename, const FileNameList& imageList, unsigned int thumbnailSize, unsigned int maximumSize, bool ) |
|---|
| 146 | { |
|---|
| 147 | |
|---|
| 148 | PhotoIndexList photoIndex; |
|---|
| 149 | photoIndex.reserve(imageList.size()); |
|---|
| 150 | for(FileNameList::const_iterator fitr=imageList.begin(); |
|---|
| 151 | fitr!=imageList.end(); |
|---|
| 152 | ++fitr) |
|---|
| 153 | { |
|---|
| 154 | PhotoHeader header; |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | strncpy(header.filename,fitr->c_str(),255); |
|---|
| 158 | header.filename[255]=0; |
|---|
| 159 | |
|---|
| 160 | header.thumbnail_s = thumbnailSize; |
|---|
| 161 | header.thumbnail_t = thumbnailSize; |
|---|
| 162 | header.thumbnail_position = 0; |
|---|
| 163 | |
|---|
| 164 | header.fullsize_s = thumbnailSize; |
|---|
| 165 | header.fullsize_t = thumbnailSize; |
|---|
| 166 | header.fullsize_position = 0; |
|---|
| 167 | |
|---|
| 168 | photoIndex.push_back(header); |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | std::cout<<"Building photo archive containing "<<photoIndex.size()<<" pictures"<<std::endl; |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | osgDB::ofstream out(filename.c_str(), std::ios::out | std::ios::binary); |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | out.write(FILE_IDENTIFER.c_str(),FILE_IDENTIFER.size()); |
|---|
| 179 | |
|---|
| 180 | unsigned int numPhotos = photoIndex.size(); |
|---|
| 181 | out.write((char*)&numPhotos,sizeof(unsigned int)); |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | unsigned int startOfPhotoIndex = out.tellp(); |
|---|
| 186 | out.write((char*)&photoIndex.front(),sizeof(PhotoHeader)*photoIndex.size()); |
|---|
| 187 | |
|---|
| 188 | unsigned int photoCount=1; |
|---|
| 189 | for(PhotoIndexList::iterator pitr=photoIndex.begin(); |
|---|
| 190 | pitr!=photoIndex.end(); |
|---|
| 191 | ++pitr,++photoCount) |
|---|
| 192 | { |
|---|
| 193 | PhotoHeader& photoHeader = *pitr; |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | std::cout<<"Processing image "<<photoCount<<" of "<< photoIndex.size()<<" filename="<< photoHeader.filename << std::endl; |
|---|
| 197 | std::cout<<" reading image...";std::cout.flush(); |
|---|
| 198 | |
|---|
| 199 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(photoHeader.filename); |
|---|
| 200 | |
|---|
| 201 | std::cout<<"done."<< std::endl; |
|---|
| 202 | |
|---|
| 203 | photoHeader.original_s = image->s(); |
|---|
| 204 | photoHeader.original_t = image->t(); |
|---|
| 205 | |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | std::cout<<" creating thumbnail image..."; |
|---|
| 209 | |
|---|
| 210 | unsigned int newTotalSize = |
|---|
| 211 | image->computeRowWidthInBytes(thumbnailSize,image->getPixelFormat(),image->getDataType(),image->getPacking())* |
|---|
| 212 | thumbnailSize; |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | unsigned char* newData = new unsigned char [newTotalSize]; |
|---|
| 216 | if (!newData) |
|---|
| 217 | { |
|---|
| 218 | |
|---|
| 219 | osg::notify(osg::FATAL) << "Error scaleImage() did not succeed : out of memory."<<newTotalSize<<std::endl; |
|---|
| 220 | return; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | osg::PixelStorageModes psm; |
|---|
| 224 | psm.pack_alignment = image->getPacking(); |
|---|
| 225 | psm.unpack_alignment = image->getPacking(); |
|---|
| 226 | |
|---|
| 227 | GLint status = osg::gluScaleImage(&psm, image->getPixelFormat(), |
|---|
| 228 | image->s(), |
|---|
| 229 | image->t(), |
|---|
| 230 | image->getDataType(), |
|---|
| 231 | image->data(), |
|---|
| 232 | thumbnailSize, |
|---|
| 233 | thumbnailSize, |
|---|
| 234 | image->getDataType(), |
|---|
| 235 | newData); |
|---|
| 236 | |
|---|
| 237 | if (status!=0) |
|---|
| 238 | { |
|---|
| 239 | delete [] newData; |
|---|
| 240 | osg::notify(osg::WARN) << "Error scaleImage() did not succeed : errorString = "<<osg::gluErrorString((GLenum)status)<<std::endl; |
|---|
| 241 | return; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | photoHeader.thumbnail_s = thumbnailSize; |
|---|
| 246 | photoHeader.thumbnail_t = thumbnailSize; |
|---|
| 247 | photoHeader.thumbnail_position = (unsigned int)out.tellp(); |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | ImageHeader imageHeader; |
|---|
| 251 | imageHeader.s = thumbnailSize; |
|---|
| 252 | imageHeader.t = thumbnailSize; |
|---|
| 253 | imageHeader.internalTextureformat = image->getInternalTextureFormat(); |
|---|
| 254 | imageHeader.pixelFormat = image->getPixelFormat(); |
|---|
| 255 | imageHeader.type = image->getDataType(); |
|---|
| 256 | imageHeader.size = newTotalSize; |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | out.write((char*)&imageHeader,sizeof(ImageHeader)); |
|---|
| 260 | out.write((char*)newData,imageHeader.size); |
|---|
| 261 | |
|---|
| 262 | delete [] newData; |
|---|
| 263 | |
|---|
| 264 | std::cout<<"done."<< std::endl; |
|---|
| 265 | |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | { |
|---|
| 269 | std::cout<<" creating fullsize image...";std::cout.flush(); |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | photoHeader.fullsize_s = osg::minimum((unsigned int)image->s(),maximumSize); |
|---|
| 273 | photoHeader.fullsize_t = osg::minimum((unsigned int)image->t(),maximumSize); |
|---|
| 274 | photoHeader.fullsize_position = (unsigned int)out.tellp(); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | unsigned int newTotalSize = |
|---|
| 278 | image->computeRowWidthInBytes(photoHeader.fullsize_s,image->getPixelFormat(),image->getDataType(),image->getPacking())* |
|---|
| 279 | photoHeader.fullsize_t; |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | unsigned char* newData = new unsigned char [newTotalSize]; |
|---|
| 283 | if (!newData) |
|---|
| 284 | { |
|---|
| 285 | |
|---|
| 286 | osg::notify(osg::FATAL) << "Error scaleImage() did not succeed : out of memory."<<newTotalSize<<std::endl; |
|---|
| 287 | return; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | osg::PixelStorageModes psm; |
|---|
| 291 | psm.pack_alignment = image->getPacking(); |
|---|
| 292 | psm.unpack_alignment = image->getPacking(); |
|---|
| 293 | |
|---|
| 294 | GLint status = osg::gluScaleImage(&psm, image->getPixelFormat(), |
|---|
| 295 | image->s(), |
|---|
| 296 | image->t(), |
|---|
| 297 | image->getDataType(), |
|---|
| 298 | image->data(), |
|---|
| 299 | photoHeader.fullsize_s, |
|---|
| 300 | photoHeader.fullsize_t, |
|---|
| 301 | image->getDataType(), |
|---|
| 302 | newData); |
|---|
| 303 | |
|---|
| 304 | if (status!=0) |
|---|
| 305 | { |
|---|
| 306 | delete [] newData; |
|---|
| 307 | osg::notify(osg::WARN) << "Error scaleImage() did not succeed : errorString = "<<osg::gluErrorString((GLenum)status)<<std::endl; |
|---|
| 308 | return; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | ImageHeader imageHeader; |
|---|
| 312 | imageHeader.s = photoHeader.fullsize_s; |
|---|
| 313 | imageHeader.t = photoHeader.fullsize_t; |
|---|
| 314 | imageHeader.internalTextureformat = image->getInternalTextureFormat(); |
|---|
| 315 | imageHeader.pixelFormat = image->getPixelFormat(); |
|---|
| 316 | imageHeader.type = image->getDataType(); |
|---|
| 317 | imageHeader.size = newTotalSize; |
|---|
| 318 | |
|---|
| 319 | out.write((char*)&imageHeader,sizeof(ImageHeader)); |
|---|
| 320 | out.write((char*)newData,imageHeader.size); |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | delete [] newData; |
|---|
| 324 | |
|---|
| 325 | std::cout<<"done."<< std::endl; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | } |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | out.seekp(startOfPhotoIndex); |
|---|
| 332 | out.write((char*)&photoIndex.front(),sizeof(PhotoHeader)*photoIndex.size()); |
|---|
| 333 | |
|---|
| 334 | } |
|---|