root/OpenSceneGraph/trunk/examples/osgphotoalbum/PhotoArchive.cpp @ 12577

Revision 12577, 12.0 kB (checked in by robert, 4 years ago)

Fixed a range of issues reported by cppcheck:

[examples/osgphotoalbum/PhotoArchive.cpp:56]: (error) Memory leak: fileIndentifier
[examples/osgphotoalbum/PhotoArchive.cpp:257]: (error) Deallocating a deallocated pointer: newData
[examples/osgphotoalbum/PhotoArchive.cpp:318]: (error) Deallocating a deallocated pointer: newData
[src/osg/ImageUtils.cpp:116]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:307]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:312]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:367]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:399]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:400]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:482]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:483]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:484]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:519]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:536]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:71]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:74]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:77]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:82]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:102]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:107]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:599]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:600]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:601]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:602]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:603]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:604]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:605]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:606]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:134]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:135]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:136]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:137]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:139]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:177]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:178]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:195]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:198]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:203]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:205]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:253]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/InputStream.cpp:553]: (error) Memory leak: data
[src/osgDB/OutputStream.cpp:393]: (error) Memory leak: data
[src/osgPlugins/Inventor/ConvertToInventor.cpp:656]: (error) Mismatching allocation and deallocation: tmpArray
[src/osgPlugins/Inventor/ReaderWriterIV.cpp:237]: (error) Common realloc mistake: 'buf' nulled but not freed upon failure
[src/osgPlugins/OpenFlight/expGeometryRecords.cpp:167]: (portability) Extra qualification 'flt::' unnecessary and considered an error by many compilers.
[src/osgPlugins/OpenFlight/expGeometryRecords.cpp:373]: (portability) Extra qualification 'flt::' unnecessary and considered an error by many compilers.
[src/osgPlugins/cfg/CameraConfig.cpp:635]: (error) Unusual pointer arithmetic
[src/osgPlugins/freetype/FreeTypeLibrary.cpp:122]: (error) Memory leak: buffer
[src/osgPlugins/geo/ReaderWriterGEO.cpp:210]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 211
[src/osgPlugins/geo/ReaderWriterGEO.cpp:227]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 228
[src/osgPlugins/geo/ReaderWriterGEO.cpp:903]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 904
[src/osgPlugins/geo/osgGeoNodes.h:180]: (error) Memory leak: geoHeaderGeo::intVars
[src/osgPlugins/geo/osgGeoNodes.h:181]: (error) Memory leak: geoHeaderGeo::useVars
[src/osgPlugins/geo/osgGeoNodes.h:182]: (error) Memory leak: geoHeaderGeo::extVars
[src/osgPlugins/md2/ReaderWriterMD2.cpp:180]: (error) Memory leak: mapbase
[src/osgPlugins/md2/ReaderWriterMD2.cpp:166]: (error) Resource leak: file_fd
[src/osgPlugins/pic/ReaderWriterPIC.cpp:152]: (error) Mismatching allocation and deallocation: tmpbuf
[src/osgPlugins/pic/ReaderWriterPIC.cpp:153]: (error) Mismatching allocation and deallocation: buffer
[src/osgPlugins/ply/plyfile.cpp:843]: (error) Memory leak: plyfile
[src/osgPlugins/pvr/ReaderWriterPVR.cpp:179]: (error) Memory leak: imageData
[src/osgPlugins/shp/ESRIShapeParser.cpp:29]: (error) Resource leak: fd
[src/osgPlugins/shp/XBaseParser.cpp:96]: (error) Resource leak: fd
[src/osgPlugins/zip/unzip.cpp:3158]: (error) Possible null pointer dereference: s - otherwise it is redundant to check if s is null at line 3159
[src/osgPlugins/zip/unzip.cpp:4155]: (error) Dangerous usage of 'rd' (strncpy doesn't always 0-terminate it)
[src/osgShadow/MinimalCullBoundsShadowMap.cpp:334]: (error) Possible null pointer dereference: rl - otherwise it is redundant to check if rl is null at line 331
[src/osgViewer/ScreenCaptureHandler.cpp:617]: (error) Possible null pointer dereference: camera - otherwise it is redundant to check if camera is null at line 611
[src/osgViewer/ScreenCaptureHandler.cpp:632]: (error) Possible null pointer dereference: camera - otherwise it is redundant to check if camera is null at line 626
[src/osgVolume/Locator.cpp:209]: (error) Dangerous iterator usage after erase()-method.
[src/osgVolume/RayTracedTechnique.cpp:274]: (error) Possible null pointer dereference: imageLayer - otherwise it is redundant to check if imageLayer is null at line 259
[src/osgVolume/RayTracedTechnique.cpp:275]: (error) Possible null pointer dereference: imageLayer - otherwise it is redundant to check if imageLayer is null at line 259
[src/osgWrappers/serializers/osg/ShaderBinary.cpp:28]: (error) Mismatching allocation and deallocation: data

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgphotoalbum.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
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
31const std::string FILE_IDENTIFER("osgphotoalbum photo archive");
32
33PhotoArchive::PhotoArchive(const std::string& filename)
34{
35    readPhotoIndex(filename);
36}
37
38bool 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    // success record filename.
59    _archiveFileName = filename;
60   
61    return true;
62}
63
64void 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
75osg::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                // find image
94                in.seekg(photoHeader.thumbnail_position);
95               
96                // read image header
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                // find image
120                in.seekg(photoHeader.fullsize_position);
121               
122                // read image header
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
145void PhotoArchive::buildArchive(const std::string& filename, const FileNameList& imageList, unsigned int thumbnailSize, unsigned int maximumSize, bool /*compressed*/)
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        // set name
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    // open up the archive for writing to
175    osgDB::ofstream out(filename.c_str(), std::ios::out | std::ios::binary);
176
177    // write out file indentifier.
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    // write the photo index to ensure we can the correct amount of space
184    // available.
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            // first need to rescale image to the require thumbnail size
210            unsigned int newTotalSize =
211                image->computeRowWidthInBytes(thumbnailSize,image->getPixelFormat(),image->getDataType(),image->getPacking())*
212                thumbnailSize;
213
214            // need to sort out what size to really use...
215            unsigned char* newData = new unsigned char [newTotalSize];
216            if (!newData)
217            {
218                // should we throw an exception???  Just return for time being.
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            // now set up the photo header.
245            photoHeader.thumbnail_s = thumbnailSize;
246            photoHeader.thumbnail_t = thumbnailSize;
247            photoHeader.thumbnail_position = (unsigned int)out.tellp();
248
249            // set up image header
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            // write out image header and image data.
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            // first need to rescale image to the require thumbnail size
277            unsigned int newTotalSize =
278                image->computeRowWidthInBytes(photoHeader.fullsize_s,image->getPixelFormat(),image->getDataType(),image->getPacking())*
279                photoHeader.fullsize_t;
280
281            // need to sort out what size to really use...
282            unsigned char* newData = new unsigned char [newTotalSize];
283            if (!newData)
284            {
285                // should we throw an exception???  Just return for time being.
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            //out.write((char*)image->data(),imageHeader.size);
322
323            delete [] newData;
324
325            std::cout<<"done."<< std::endl;
326        }
327       
328    }
329
330    // rewrite photo index now it has the correct sizes set
331    out.seekp(startOfPhotoIndex);
332    out.write((char*)&photoIndex.front(),sizeof(PhotoHeader)*photoIndex.size());
333
334}
Note: See TracBrowser for help on using the browser.