OpenSGDevMaster_Toolbox /Source/System/Image/FileIO/OSGDATImageFileType.cpp

Language C++ Lines 668
MD5 Hash 6b90ed3da19fad9391badee111e99414
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*---------------------------------------------------------------------------*\
 *                                OpenSG                                     *
 *                                                                           *
 *                                                                           *
 *             Copyright (C) 2000-2002 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                                    *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
\*---------------------------------------------------------------------------*/

//-------------------------------
//      Includes
//-------------------------------

#include <cstdlib>
#include <cstdio>

#include "OSGConfig.h"

#include <iostream>
#include <fstream>

#include "OSGLog.h"
#include "OSGImageFileHandler.h"
#include "OSGPathHandler.h"
#include "OSGFileSystem.h"
#include "OSGZStream.h"

#include "OSGDATImageFileType.h"

OSG_USING_NAMESPACE

/*! \class OSG::DATImageFileType
    \ingroup GrpSystemImage

  Image File Type to read/write and store/restore Image objects as
  PNM/RAW data.

  All the type specific code is included in the class. Does
  not depend on external libs.

 */


// Static Class Varible implementations:

static const Char8 *suffixArray[] =
{
    "dat"
};

DATImageFileType DATImageFileType::_the("image/x-dat",
                                        suffixArray,
                                        sizeof(suffixArray) );

std::map<std::string,
         DATImageFileType::KeyType   > DATImageFileType::_keyStrMap;

std::map<std::string,
         DATImageFileType::FormatDesc> DATImageFileType::_formatStrMap;



//-------------------------------------------------------------------------

/*! Tries to fill the image object with the data read from
    the given fileName. Returns true on success.
 */

bool DATImageFileType::read(      Image *image,
                            const Char8 *fileName)
{
    bool retCode = false;

    std::ifstream inDat(fileName), inVolS;
    std::istream *inVol;
    std::string keyStr, objectFileName;
    const UInt32 lineBufferSize = 1024;
    Char8 *value, *keySepPos, lineBuffer[lineBufferSize];
    const Char8 keySep = ':';
    int fileOffset, keyL, valueL;
    std::map<std::string, KeyType>::iterator keyI;
    std::map<std::string, FormatDesc>::iterator formatI;
    KeyType key;
    Image::Type formatType;
    UInt32 channel = 1;
    UInt32 res[3];
    UInt32 dataSize = 0;
    Image::PixelFormat pixelFormat = Image::OSG_L_PF;
    char *dataBuffer = 0;
    bool needConversion = false;
    // default endian type is big endian
    bool big_endian = true;

    res[0] = res[1] = res[2] = 0;
    fileOffset = 0;
    formatType = Image::OSG_INVALID_IMAGEDATATYPE;
    dataSize = 0;
    dataBuffer = 0;

    initTypeMap();

    // read the data file
    for(lineBuffer[0] = 0;
        inDat.getline(lineBuffer, lineBufferSize);
        lineBuffer[0] = 0)
    {
        if((keySepPos = strchr(lineBuffer,keySep)))
        {
            keyL = keySepPos - lineBuffer;
            keyStr.assign( lineBuffer, keyL );
            keyI = _keyStrMap.find(keyStr);
            key = ((keyI == _keyStrMap.end()) ? UNKNOWN_KT : keyI->second);
            value = keySepPos + 1;

            while (value && isspace(*value))
                value++;

            valueL = strlen(value);

            while (isspace(value[valueL-1]))
                value[--valueL] = 0;

            switch (key)
            {
                case OBJECT_FILE_NAME_KT:
                    objectFileName = value;
                    image->setAttachmentField ( keyStr, value );
                    break;
                case CHANNEL_KT:
                    sscanf ( value, "%d", &(channel) );
                    image->setAttachmentField ( keyStr, value );
                    break;
                case RESOLUTION_KT:
                    sscanf ( value, "%d %d %d",
                             &(res[0]), &(res[1]), &(res[2]));
                    image->setAttachmentField ( keyStr, value );
                    break;
                case FORMAT_KT:
                    formatI = _formatStrMap.find(value);
                    if (formatI != _formatStrMap.end())
                    {
                        formatType = formatI->second.type;
                    }
                    else
                    {
                        formatType = Image::OSG_INVALID_IMAGEDATATYPE;
                    }
                    image->setAttachmentField ( keyStr, value );
                    break;
                case ENDIAN_KT:
                    if(!strcmp(value, "LITTLE"))
                        big_endian = false;
                    image->setAttachmentField ( keyStr, value );
                    break;
                case FILE_OFFSET_KT:
                    sscanf ( value, "%d", &fileOffset );
                    image->setAttachmentField ( keyStr, value );
                    break;
                case UNKNOWN_KT:
                    FNOTICE (( "Uknown DAT file key: >%s<\n",
                                 keyStr.c_str() ));
                    image->setAttachmentField ( keyStr, value );
                    break;
                case SLICE_THICKNESS_KT:
                default:
                    image->setAttachmentField ( keyStr, value );
                    break;
            }
        }
        else
        {
            FINFO (("Skip DAT line\n"));
        }
    }

    // set pixelformat
    switch (channel) 
    {
        case 4:
            pixelFormat = Image::OSG_RGBA_PF;
            break;
        case 3:
            pixelFormat = Image::OSG_RGB_PF;
            break;
        case 2:
            pixelFormat = Image::OSG_LA_PF;
            break;
        default:
            pixelFormat = Image::OSG_L_PF;
            break;
    }

    // check the setting and read the raw vol data
    if (objectFileName.empty() == false)
    {
        if((res[0] > 0) && (res[1] > 0) && (res[2] > 0))
        {
            if(formatType != Image::OSG_INVALID_IMAGEDATATYPE)
            {
                inVolS.open(objectFileName.c_str(),
                            std::ios::in | std::ios::binary);

                if (inVolS.fail() && ImageFileHandler::the()->getPathHandler())
                {
                    // Try to find the file in the search path
                    inVolS.clear(); // reset the error state

                    PathHandler *ph =
                        ImageFileHandler::the()->getPathHandler();

                    inVolS.open(ph->findFile(objectFileName.c_str()).c_str(),
                                std::ios::in | std::ios::binary );
                }

                if(inVolS.fail())
                {
                    // Maybe compressed and name not changed?
                    std::string gzname = objectFileName + ".gz";

                    inVolS.clear(); // reset the error state

                    inVolS.open(gzname.c_str(),
                                std::ios::in | std::ios::binary );

                    if(inVolS.fail() &&
                       ImageFileHandler::the()->getPathHandler())
                    {
                        // Try to find the file in the search path
                        inVolS.clear(); // reset the error state

                        PathHandler *ph =
                            ImageFileHandler::the()->getPathHandler();

                        inVolS.open(ph->findFile(gzname.c_str()).c_str(),
                                    std::ios::in | std::ios::binary );
                    }
                }

                if(inVolS.good())
                {
#ifdef OSG_WITH_ZLIB
                    zip_istream *unzipper = NULL;
#endif

                    image->set(pixelFormat,
                               res[0], res[1], res[2],
                               1, 1, 0.0, 0,
                               formatType);

                    image->clear();

                    dataSize = image->getSize();

                    UInt32 fileDataSize = dataSize;

                    if(isGZip(inVolS))
                    {
#ifdef OSG_WITH_ZLIB
                        unzipper = new zip_istream(inVolS);
                        inVol = unzipper;
#else
                        SFATAL << "Compressed streams are not supported! "
                               << "Configure with --enable-png "
                               << "--with-png=DIR options." << std::endl;
#endif
                    }
                    else
                    {
                        inVol = &inVolS;

                        // get length of the stream.
                        inVol->seekg(0, std::ios::end);
                        UInt64 length = inVol->tellg();
                        inVol->seekg(0, std::ios::beg);

                        if(length < dataSize - fileOffset)
                        {
                            // correct dataSize.
                            fileDataSize = length;
                            FWARNING (( "RAW file length to small!\n" ));
                        }
                        else if(length > dataSize - fileOffset)
                        {
                            FWARNING (( "RAW file length to big!\n" ));
                        }
                    }

                    if(needConversion)
                    {
                        dataBuffer = new char [ dataSize ];
                    }
                    else
                    {
                        dataBuffer = 
                            reinterpret_cast<char *>(image->editData());
                    }

                    if(fileOffset != 0)
                        inVol->ignore (fileOffset);

                    inVol->read ( dataBuffer, fileDataSize );

#ifdef OSG_WITH_ZLIB
                    if(unzipper != NULL)
                        delete unzipper;
#endif
                }
                else
                {
                    FWARNING (( "Can not open %s image data\n",
                                objectFileName.c_str() ));
                }
            }
            else
            {
                FWARNING (( "Invalid/Missing DAT Format\n" ));
            }
        }
        else
        {
            FWARNING (( "Invalid/Missing DAT Resolution\n" ));
        }
    }
    else
    {
        FWARNING (( "Invalid/Missing DAT ObjectFileName\n" ));
    }

    // check/reformat vol data
    if (dataSize && dataBuffer)
    {
        // check host endian type
        UInt16 word = 0x0001;
        UInt8 *byte = reinterpret_cast<UInt8 *>(&word);
        bool host_big_endian = byte[0] ? false : true;

        if(big_endian != host_big_endian)
            image->swapDataEndian();

        if (needConversion)
        {
            FLOG (("DAT-Data convert not impl. yet !\n"));
            {
                switch (formatType)
                {
                    case Image::OSG_UINT8_IMAGEDATA:
                        break;
                    case Image::OSG_UINT16_IMAGEDATA:
                        break;
                    case Image::OSG_UINT32_IMAGEDATA:
                        break;
                    case Image::OSG_FLOAT32_IMAGEDATA:
                        break;
                    default:
                        ;
                }
            }
        }
        else
        {
            retCode = true;
        }
    }


    /* TODO
       std::ifstream in(fileName);
       Head head;
       void *headData = (void*)(&head);
       unsigned dataSize, headSize = sizeof(Head);

       if ( in &&
       in.read(static_cast<char *>(headData),
       headSize) && head.netToHost() &&
       image.set ( Image::PixelFormat(head.pixelFormat),
       head.width, head.height, head.depth, head.mipmapCount,
       head.frameCount, float(head.frameDelay) / 1000.0) &&
       (dataSize = image.getSize()) &&
       in.read((char *)(image.getData()), dataSize ))
       retCode = true;
       else
       retCode = false;
    */

    return retCode;
}

//-------------------------------------------------------------------------
/*! Tries to write the image object to the given fileName.
    Returns true on success.
*/
bool DATImageFileType::write(const Image *image,
                             const Char8 *fileName)
{
    initTypeMap();

    // ok we write always in big endian.
#if BYTE_ORDER == LITTLE_ENDIAN
    Image *pSwapImage = const_cast<Image *>(&(*image));

    pSwapImage->swapDataEndian();
#endif

    std::ofstream dat(fileName, std::ios::binary);

    if(!dat)
    {
        SWARNING << "DATImageFileType::write : Can not open output stream "
                 << "for file '"
                 << fileName
                 << "'!"
                 << std::endl;

        return false;
    }

    Real64 sT[3];
    sT[0] = sT[1] = sT[2] = 1.0;

    const std::string *attr = image->findAttachmentField("SliceThickness");

    if(attr != NULL)
        sscanf(attr->c_str(), "%lf %lf %lf", &sT[0], &sT[1], &sT[2]);

    std::string format = "UCHAR";

    for(std::map<std::string, FormatDesc>::iterator it = _formatStrMap.begin();
        it != _formatStrMap.end();
        ++it)
    {
        if((*it).second.type == image->getDataType())
        {
            format = (*it).first;
            break;
        }
    }

    std::string basename = fileName;
    std::string::size_type i = basename.rfind(".");

    if(i != std::string::npos)
        basename = basename.substr(0, i);

    basename += ".raw";

    std::string name = basename;

    i = name.rfind("/");

    // on windows also a / is possible!
#if defined(WIN32)
    if(i == std::string::npos)
        i = name.rfind("\\");
#endif

    if(i != std::string::npos)
        name = name.substr(i+1);

    dat << "ObjectFileName: " << name << "\n";
    dat << "TaggedFileName: ---\n";
    dat << "Resolution:     " << image->getWidth() << " " << image->getHeight()
        << " "                << image->getDepth() << "\n";
    dat << "SliceThickness: " << sT[0] << " " << sT[1] << " " << sT[2] << "\n";
    dat << "Format:         " << format << "\n";
    dat << "NbrTags:        0\n";
    dat << "ObjectType:     TEXTURE_VOLUME_OBJECT\n";
    dat << "ObjectModel:    DENSITY\n";
    dat << "GridType:       EQUIDISTANT\n";

    if(image->getBpp() > 1)
      dat << "Channel: " << image->getBpp() << "\n";

    dat.close();

    std::ofstream raw(basename.c_str(), std::ios::binary);

    if(!raw)
    {
        SWARNING << "DATImageFileType::write : Can not open output stream "
                 << "for file '"
                 << basename
                 << "'!"
                 << std::endl;

        return false;
    }

    raw.write (reinterpret_cast<const char *>(image->getData()), 
               image->getSize());
    raw.close();

    // restore to original endian
#if BYTE_ORDER == LITTLE_ENDIAN
    pSwapImage->swapDataEndian();
#endif

    /*
    ofstream out(fileName);
    Head head;
    const void *headData = (void*)(&head);
    unsigned dataSize = image.getSize(), headSize = sizeof(Head);

    head.pixelFormat  = image.getPixelFormat();
    head.width        = image.getWidth();
    head.height       = image.getHeight();
    head.depth        = image.getDepth();
    head.mipmapCount  = image.getMipMapCount();
    head.frameCount   = image.getFrameCount();
    head.frameDelay   = short(image.getFrameDelay() * 1000.0);
    head.hostToNet();

    if ( out && out.write(static_cast<const char *>(headData), headSize) &&
         dataSize &&
             out.write((char *)(image.getData()), dataSize) )
            retCode = true;
    else
        retCode = false;
    */

    return true;
}


//-------------------------------------------------------------------------
/*! Tries to restore the image data from the given memblock.
    Returns the amount of data read.
*/

UInt64 DATImageFileType::restoreData(      Image  *image,
                                     const UChar8 *buffer,
                                           Int32   OSG_CHECK_ARG(memSize))
{
    image->setData(buffer);

    return image->getSize();
}

//-------------------------------------------------------------------------
/*! Tries to store the image data to the given memblock.
    Returns the amount of data written.
*/

UInt64 DATImageFileType::storeData(const Image  *image,
                                         UChar8 *buffer,
                                         Int32   OSG_CHECK_ARG(memSize))
{
          UInt32  dataSize = image->getSize();
    const UChar8 *src      = image->getData();

    if(dataSize && src && buffer)
        memcpy( buffer, src, dataSize);

    return dataSize;
}


//-------------------------------------------------------------------------
/*! Constructor used for the singleton object
 */

DATImageFileType::DATImageFileType(const Char8  *mimeType,
                                   const Char8  *suffixArray[],
                                         UInt16  suffixByteCount) :
    Inherited(mimeType,
              suffixArray,
              suffixByteCount)
{
}


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

DATImageFileType::~DATImageFileType(void)
{
}

//-------------------------------------------------------------------------
/*! Helper to initialize the type map;
 */

void DATImageFileType::initTypeMap(void)
{
    FormatDesc *desc;

    if(_keyStrMap.empty())
    {
        _keyStrMap["ObjectFileName"]  = OBJECT_FILE_NAME_KT;
        _keyStrMap["Resolution"]      = RESOLUTION_KT;
        _keyStrMap["Channel"]         = CHANNEL_KT;
        _keyStrMap["SliceThickness"]  = SLICE_THICKNESS_KT;
        _keyStrMap["Format"]          = FORMAT_KT;
        _keyStrMap["Endian"]          = ENDIAN_KT;
        _keyStrMap["FileOffset"]      = FILE_OFFSET_KT;
    }

    if(_formatStrMap.empty())
    {
        desc = &(_formatStrMap["UCHAR"]);
        desc->type = Image::OSG_UINT8_IMAGEDATA;
        desc->bpv  = 1;
        desc->pixelFormat = Image::OSG_L_PF;
        desc->needConversion = false;

        desc = &(_formatStrMap["USHORT"]);
        desc->type = Image::OSG_UINT16_IMAGEDATA;
        desc->bpv  = 2;
        desc->pixelFormat = Image::OSG_L_PF;
        desc->needConversion = false;

        desc = &(_formatStrMap["UINT"]);
        desc->type = Image::OSG_UINT32_IMAGEDATA;
        desc->bpv  = 4; // TODO; is this right ?
        desc->pixelFormat = Image::OSG_L_PF;
        desc->needConversion = false;

        desc = &(_formatStrMap["ULONG"]);
        desc->type = Image::OSG_UINT32_IMAGEDATA;
        desc->bpv  = 4;
        desc->pixelFormat = Image::OSG_L_PF;
        desc->needConversion = false;

        desc = &(_formatStrMap["FLOAT"]);
        desc->type = Image::OSG_FLOAT32_IMAGEDATA;
        desc->bpv  = 4;
        desc->pixelFormat = Image::OSG_L_PF;
        desc->needConversion = false;

        desc = &(_formatStrMap["DOUBLE"]);
        // we have no OSG_FLOAT64_IMAGEDATA
        desc->type = Image::OSG_FLOAT32_IMAGEDATA;
        desc->bpv  = 8;
        desc->pixelFormat = Image::OSG_L_PF;
        desc->needConversion = false;
    }
}
Back to Top