PageRenderTime 224ms CodeModel.GetById 121ms app.highlight 14ms RepoModel.GetById 86ms app.codeStats 1ms

/Source/System/FileIO/OSB/NFIO_Depreciated/OSGNFIOImage.cpp

https://github.com/msteners/OpenSGDevMaster_Toolbox
C++ | 233 lines | 114 code | 45 blank | 74 comment | 17 complexity | e1195a447b8b88bdf2684d41109884fc MD5 | raw file
  1/*---------------------------------------------------------------------------*\
  2 *                                OpenSG                                     * 
  3 *                                                                           * 
  4 *                                                                           * 
  5 *           Copyright (C) 2000,2001,2002 by the OpenSG Forum                * 
  6 *                                                                           * 
  7 *                            www.opensg.org                                 * 
  8 *                                                                           * 
  9 *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          * 
 10 *                                                                           * 
 11\*---------------------------------------------------------------------------*/
 12/*---------------------------------------------------------------------------*\
 13 *                                License                                    * 
 14 *                                                                           * 
 15 * This library is free software; you can redistribute it and/or modify it   * 
 16 * under the terms of the GNU Library General Public License as published    * 
 17 * by the Free Software Foundation, version 2.                               * 
 18 *                                                                           * 
 19 * This library is distributed in the hope that it will be useful, but       * 
 20 * WITHOUT ANY WARRANTY; without even the implied warranty of                * 
 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         * 
 22 * Library General Public License for more details.                          * 
 23 *                                                                           * 
 24 * You should have received a copy of the GNU Library General Public         * 
 25 * License along with this library; if not, write to the Free Software       * 
 26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 * 
 27 *                                                                           * 
 28\*---------------------------------------------------------------------------*/
 29/*---------------------------------------------------------------------------*\
 30 *                                Changes                                    * 
 31 *                                                                           * 
 32 *                                                                           * 
 33 *                                                                           * 
 34 *                                                                           * 
 35 *                                                                           * 
 36 *                                                                           * 
 37\*---------------------------------------------------------------------------*/
 38
 39#include "OSGConfig.h"
 40#include "OSGNFIOImage.h"
 41#include "OSGNFIOFactory.h"
 42
 43#include <vector>
 44
 45#include "OSGLog.h"
 46#include "OSGImageFileHandler.h"
 47//#include "OSGJPGImageFileType.h"
 48
 49OSG_USING_NAMESPACE
 50
 51/***************************************************************************\
 52 *                            Description                                  *
 53\***************************************************************************/
 54
 55/*! \class OSG::NFIOImage
 56           reads and writes a image.
 57 */
 58
 59/***************************************************************************\
 60 *                           Class variables                               *
 61\***************************************************************************/
 62
 63NFIOImage NFIOImage::_the;
 64
 65/*-------------------------------------------------------------------------*\
 66 -  public                                                                 -
 67\*-------------------------------------------------------------------------*/
 68
 69
 70/***************************************************************************\
 71 *                           Instance methods                              *
 72\***************************************************************************/
 73
 74/*----------------------------- constructors  -----------------------------*/
 75
 76NFIOImage::NFIOImage(void) :
 77    NFIOBase("Image")
 78{
 79    _version = 200;
 80}
 81
 82/*------------------------------ destructor -------------------------------*/
 83
 84NFIOImage::~NFIOImage(void)
 85{
 86}
 87
 88/*-------------------------------------------------------------------------*\
 89 -  protected                                                              -
 90\*-------------------------------------------------------------------------*/
 91
 92FieldContainerPtr NFIOImage::readFC(const std::string &/*typeName*/)
 93{
 94    FDEBUG(("NFIOImage::readFC\n"));
 95    
 96    ImagePtr img = Image::create();
 97    
 98    UInt16 version;
 99    _in->getValue(version);
100    
101    std::string fieldName = readFCFields(img, "", "'cpixel'");
102    
103    // compressed pixel
104    if(fieldName == "cpixel")
105    {
106        readCompressedPixel(img);
107        _in->getValue(fieldName); // read end marker
108    }
109    
110    if(img->getPixel().empty())
111    {
112        // read texture
113        //beginEditCP(img);
114            //img->read(img->getName().c_str());
115            img = ImageFileHandler::the()->read(img->getName().c_str());
116        //endEditCP(img);
117    }
118    
119    return img;
120}
121
122void NFIOImage::writeFC(const FieldContainerPtr &fc)
123{
124    FDEBUG(("NFIOImage::witeFC\n"));
125    
126    static bool jpeg = isJPEGSupported();
127    
128    ImagePtr img = dynamic_cast<ImagePtr>(fc);
129    
130    _out->putValue(_version);
131    
132    bool compressTextures = false;
133    std::string exclude;
134    if(!getOptions().inlineTextures())
135    {
136        // only write the name field.
137        exclude = "'dimension' 'width' 'height' 'depth' 'bpp' 'mipMapCount' "
138                  "'frameCount' 'frameDelay' 'pixelFormat' 'pixel' 'frameSize'";
139    }
140    else
141    {
142        // jpeg supports only 1 and 3 byte per pixel images :-(
143        if(jpeg && getOptions().compressTextures() &&
144           img->getDataType() == Image::OSG_UINT8_IMAGEDATA &&
145           (img->getBpp() == 1 || img->getBpp() == 3))
146        {
147            compressTextures = true;
148            exclude += " 'pixel'";
149        }
150    }
151    
152    writeFCFields(img, exclude, false);
153    
154    if(compressTextures)
155        writeCompressedPixel(img);
156
157    writeEndMarker();
158}
159
160void NFIOImage::readCompressedPixel(ImagePtr &img)
161{
162    static bool jpeg = isJPEGSupported();
163    
164    std::string fieldType;
165    _in->getValue(fieldType);
166    
167    UInt32 size;
168    UInt32 noe;
169    
170    _in->getValue(size);
171    
172    // skip data if jpeg is not supported.
173    if(!jpeg)
174    {
175        FWARNING(("NFIOImage::readCompressedPixel : skipping compressed texture"
176                  ", jpeg is not supported\n"));
177        _in->skip(size);
178        return;
179    }
180    
181    _in->getValue(noe);
182    
183    std::vector<UInt8> buffer;
184    buffer.reserve(noe);
185    UInt8 p;
186    for(UInt32 i=0;i<noe;++i)
187    {
188        _in->getValue(p);
189        buffer.push_back(p);
190    }
191    
192    //beginEditCP(img);
193    // FIXME missing in Image.cpp
194    //UInt64 msize = img->restore(&buffer[0], buffer.size());
195    //endEditCP(img);
196}
197
198void NFIOImage::writeCompressedPixel(const ImagePtr &img)
199{
200    _out->putValue(std::string("cpixel"));
201    _out->putValue(std::string("MFUInt8"));
202    
203    std::vector<UInt8> buffer;
204    buffer.resize(img->getSize(false, false));
205    // FIXME need to add a option inteface like in the SceneFileHandler.
206    //JPGImageFileType::the().setQuality(getOptions().texturesCompressionQuality());
207    // FIXME missing in Image.cpp
208    //UInt64 msize = img->store("jpeg", &buffer[0]);
209    UInt64 msize = 0;
210    
211    UInt32 noe = (UInt32) msize;
212    UInt32 size = sizeof(UInt32) + sizeof(UInt8) * noe;
213
214    _out->putValue(size);
215    _out->putValue(noe);
216    
217    for(UInt32 i=0;i<noe;++i)
218        _out->putValue(buffer[i]);
219}
220
221bool NFIOImage::isJPEGSupported(void)
222{
223    std::list<const Char8*> sl;
224    if(ImageFileHandler::the()->getSuffixList(sl) > 0)
225    {
226        for(std::list<const Char8*>::iterator i=sl.begin();i!=sl.end();++i)
227        {
228            if(!strcmp(*i, "jpeg"))
229                return true;
230        }
231    }
232    return false;
233}