OpenSGDevMaster_Toolbox /Source/System/FileIO/OSB/OSGOSBImageElement.cpp

Language C++ Lines 307
MD5 Hash 0d2384d05550541a3a860cd146dad9b8
Repository https://github.com/msteners/OpenSGDevMaster_Toolbox.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*---------------------------------------------------------------------------*\
 *                                OpenSG                                     *
 *                                                                           *
 *                                                                           *
 *                   Copyright (C) 2007 by the OpenSG Forum                  *
 *                                                                           *
 *                            www.opensg.org                                 *
 *                                                                           *
 *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                License                                    *
 *                                                                           *
 * This library is free software; you can redistribute it and/or modify it   *
 * under the terms of the GNU Library General Public License as published    *
 * by the Free Software Foundation, version 2.                               *
 *                                                                           *
 * 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 GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public         *
 * License along with this library; if not, write to the Free Software       *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                Changes                                    *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
\*---------------------------------------------------------------------------*/

#include "OSGOSBImageElement.h"

#include "OSGOSBRootElement.h"
#include "OSGImage.h"
#include "OSGImageFileHandler.h"

OSG_USING_NAMESPACE

/*-------------------------------------------------------------------------*/
/* OSBImageElement                                                         */
/*-------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------*/
/* Static members                                                          */

OSBElementRegistrationHelper<OSBImageElement>
    OSBImageElement::_regHelper =
        OSBElementRegistrationHelper<OSBImageElement>("Image");

const UInt8 OSBImageElement::FlagPixelDataCompressedMask;
const UInt8 OSBImageElement::FlagPixelDataCompressed;
const UInt8 OSBImageElement::FlagPixelDataOutOfLineMask;
const UInt8 OSBImageElement::FlagPixelDataOutOfLine;

/*-------------------------------------------------------------------------*/
/* Constructor                                                             */

OSBImageElement::OSBImageElement(OSBRootElement *root)
    : Inherited      (root, OSGOSBHeaderVersion200),
      _hasJPEGSupport(false                        ),
      _version       (OSGOSBHeaderVersion200      )
{
}

/*-------------------------------------------------------------------------*/
/* Destructor                                                              */

OSBImageElement::~OSBImageElement(void)
{
}

/*-------------------------------------------------------------------------*/
/* Reading                                                                 */

void
OSBImageElement::read(const std::string &typeName)
{
    OSG_OSB_LOG(("OSBImageElement::read [%s]\n", typeName.c_str()));

    const OSBRootElement *root      = getRoot();
    BinaryReadHandler     *rh        = editRoot()->getReadHandler();
    UInt8                  fcPtrType;

    if(root->getHeaderVersion() >= OSGOSBHeaderVersion200)
    {
        if(root->getHeaderVersion() > OSGOSBHeaderVersion200)
        {
            FINFO(("OSBImageElement::read: "
                   "Unknown version, trying to process as latest.\n"));
        }

        rh->getValue(fcPtrType);
    }

    ImageUnrecPtr img = Image::create();
    setContainer(img);

    rh->getValue(_version);

    if(_version >= OSGOSBHeaderVersion200)
    {
        UInt8 flags;
        rh->getValue(flags);

        if(flags & FlagPixelDataCompressedMask)
        {
            // compressed inline texture
            std::string endMarker = "'pixel'";
            std::string fieldName = readFields("", endMarker);

            if(fieldName == "pixel")
            {
                readCompressedPixelData();
            }
        }
        else
        {
            // read fields stored in file
            readFields("", "");
            
            if(flags & FlagPixelDataOutOfLineMask)
            {
                // read out-of-line image data
                const std::string &fileName = img->getName();
                img = ImageFileHandler::the()->read(fileName.c_str());
                setContainer(img);
            }
        }
    }
    else if(_version >= OSGOSBHeaderVersion100)
    {
        std::string endMarker = "'cpixel'";
        std::string fieldName = readFields("", endMarker);

        // read compressed pixel data - should be the last field
        if(fieldName == "cpixel")
        {
            readCompressedPixelData();
        }

        // read any remaining fields - should be none, but this is safe to do.
        readFieldsContinue(fieldName, "", "");

        // read out-of-line image data
        if(img->getMFPixel()->empty())
        {
            const std::string &fileName = img->getName();
            img = ImageFileHandler::the()->read(fileName.c_str());
            setContainer(img);
        }
    }
}

void
OSBImageElement::postRead(void)
{
    // nothing to do
}

/*-------------------------------------------------------------------------*/
/* Writing                                                                 */

void
OSBImageElement::preWrite(FieldContainer * const fc)
{
    OSG_OSB_LOG(("OSBImageElement::preWrite\n"));

    preWriteFieldContainer(fc, "");
}

void
OSBImageElement::write(void)
{
    OSG_OSB_LOG(("OSBImageElement::write\n"));

    if(getContainer() == NULL)
    {
        FWARNING(("OSBImageElement::write: Attempt to write NULL.\n"));
        return;
    }

    Image                *img   = dynamic_cast<Image *>(getContainer());
    BinaryWriteHandler   *wh    = editRoot()->getWriteHandler();
    const OSBRootElement *root  = getRoot();
    UInt8                 flags = 0;

    wh->putValue(getFCPtrType(getContainer()));
    wh->putValue(getVersion()                );

    std::string excludeFields    = "";
    bool        compressTextures = false;

    if(getRoot()->getOptions().inlineTextures() == false)
    {
        // only write "name" field
        flags |= FlagPixelDataOutOfLine;
        excludeFields.append("'dimension' 'width' 'height' 'depth' 'bpp' "
                             "'mipMapCount' 'frameCount' 'frameDelay' "
                             "'pixelFormat' 'pixel' 'frameSize' "         );
    }
    else
    {
        if(_hasJPEGSupport && root->getOptions().compressTextures() &&
           (img->getDataType() == Image::OSG_UINT8_IMAGEDATA)       &&
           ((img->getBpp() == 1) || (img->getBpp() == 3))             )
        {
            compressTextures =  true;
            flags            |= FlagPixelDataCompressed;
            excludeFields.append("'pixel'");
        }
    }

    // write flags
    wh->putValue(flags);

    // write all fields that do not require special handling
    writeFields(excludeFields, false);

    if(compressTextures)
        writeCompressedPixelData();

    writeEndMarker();
}

/*-------------------------------------------------------------------------*/
/* Reading Helper Functions                                                */

void
OSBImageElement::readCompressedPixelData(void)
{
    BinaryReadHandler *rh  = editRoot()->getReadHandler();
    Image             *img = dynamic_cast<Image *>(getContainer());

    std::string        fieldTypeName;
    UInt32             fieldSize;
    UInt32             byteSize;
    std::vector<UInt8> buffer;

    rh->getValue(fieldTypeName);
    rh->getValue(fieldSize    );

    if(!_hasJPEGSupport)
    {
        FWARNING(("OSBImageElement::readCompressedPixelData: "
                  "JPEG Support not available, skipping compressed "
                  "texture data.\n"));
        rh->skip(fieldSize);
        return;
    }

    rh->getValue(byteSize);

    // allocate and fill buffer
    buffer.resize(byteSize);
    rh->getValues(&buffer.front(), byteSize);

    img->restore(&buffer.front(), byteSize);
}

/*-------------------------------------------------------------------------*/
/* Writing Helper Functions                                                */

void
OSBImageElement::writeCompressedPixelData(void)
{
    // FIXME Inline Images are disabled right now. A more recent versin of
    //       the ImageFileHandler from CVS needs to be ported to OpenSG 2.

    const OSBRootElement *root = getRoot();
    BinaryWriteHandler   *wh   = editRoot()->getWriteHandler();

    Image       *img       = dynamic_cast<Image *>(getContainer());
    std::string  imageType = root->getOptions().texturesImageType();
//     std::string imageType = "jpeg";

    std::vector<UInt8> buffer;
    UInt32             factor = 1;

    // JPEG always uses RGB, so single channel images need additional memory
    if((imageType == "jpeg") && (img->getBpp() == 1))
        factor = 3;

    // some extra space is needed for the image header
    UInt32 bufferSize =
       ImageFileHandler::the()->getDefaultType()->maxBufferSize(img) * factor +
           16384;

    buffer.resize(bufferSize);
    UInt64 compressedSize = img->store(imageType.c_str(), &buffer.front());
//     UInt64 compressedSize = 0;

    UInt32 byteSize  = static_cast<UInt32>(compressedSize);
    UInt32 fieldSize = sizeof(UInt32) + sizeof(UInt8) * byteSize;

    wh->putValue (fieldSize                );
    wh->putValue (byteSize                 );
    wh->putValues(&buffer.front(), byteSize);
}
Back to Top