TextureDataLoadStore.hh 4.4 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
6

Robert Menzel's avatar
Robert Menzel committed
7
8
#ifndef ACGL_OPENGL_DATA_TEXTUREDATALOADSTORE_HH
#define ACGL_OPENGL_DATA_TEXTUREDATALOADSTORE_HH
9
10

/**
Robert Menzel's avatar
Robert Menzel committed
11
12
 * Helper function for writing the contents of a TextureData object into a file
 * and loading them from a file.
13
14
15
16
 */

#include <ACGL/ACGL.hh>
#include <ACGL/OpenGL/Data/TextureData.hh>
17
#include <ACGL/OpenGL/Tools.hh>
18
19
20
21
22
23

#include <string>

namespace ACGL{
namespace OpenGL{

24
25
26
27
28
29
30
31
32
33
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                               generic load/save
///////////////////////////////////////////////////////////////////////////////////////////////////

//! generic load function that will use one of the loading functions below based on the file ending
SharedTextureData loadTextureData(const std::string &_filename);

//! generic save function that will use one of the saving functions below based on the file ending
bool saveTextureData(const SharedTextureData &_textureData, const std::string &_filename);

34
35
36
//! saves the viewport visible part of the framebuffer 0 to a file named _fileName. The file ending determines the file type
bool saveScreenshot( const std::string& _fileName );

37
//! saves the viewport visible part of the framebuffer 0 to a file named _prefix_DATE-TIME._fileEnding
38
bool saveScreenshotWithDate( const std::string& _prefix, const std::string& _fileEnding);
Robert Menzel's avatar
Robert Menzel committed
39
40

//! saves the viewport visible part of the framebuffer 0 to a file named screenshot_DATE-TIME._fileEnding
41
42
inline bool saveScreenshotWithDate( const std::string& _fileEnding = "png" ) {
    return saveScreenshotWithDate( "screenshot", _fileEnding );
Robert Menzel's avatar
Robert Menzel committed
43
}
44
45
46
47

///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Menzel's avatar
Robert Menzel committed
48
49
50
51

//! loads from a PNG using the simple lodepng library
SharedTextureData loadTextureDataFromLodepng(const std::string &_filename);

52
#ifdef ACGL_COMPILE_WITH_QT
53
//! loads various formats from the QT library
54
55
56
SharedTextureData loadTextureDataFromQT(const std::string &_filename);
#endif

57
//! loads RGBE aka Radiance files
58
59
SharedTextureData loadTextureDataFromRGBE(const std::string &_filename);

60
//! loads EXR / OpenEXR files iff the library is present AT RUNTIME (linux only)
61
62
SharedTextureData loadTextureDataFromEXR(const std::string &_filename);

63
64
65
//! loads PNM / PPM files:
SharedTextureData loadTextureDataFromPNM(const std::string &_filename);

66
67
68
69
70
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific save
///////////////////////////////////////////////////////////////////////////////////////////////////

//! saves to a PPM file
bool saveTextureDataToPPM(const SharedTextureData &_textureData, const std::string &_filename);

Robert Menzel's avatar
Robert Menzel committed
73
//! save to a PNG file with lodepng
74
75
76
77
78
79
80
81
82
83
84
bool saveTextureDataToLodepng( const SharedTextureData &_data, const std::string &_filename );

#ifdef ACGL_COMPILE_WITH_QT
bool saveTextureDataToQT( const SharedTextureData &_data, const std::string &_filename );
#endif


// helper for saveTextureDataToLodepng
template< typename T>
unsigned char *preProcess( const SharedTextureData &_data)
{
Robert Menzel's avatar
Robert Menzel committed
85
    unsigned int channelCount = _data->getNumberOfChannels();
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

    unsigned int pixelCount = _data->getWidth() * _data->getHeight();
    T *processedrawdata = new T[pixelCount * channelCount];

    // copy & flip the image:
    T *originalrawdata  = (T*) _data->getData();

    for (int i = 0; i < _data->getHeight(); ++i) {
        size_t srcOffset = _data->getWidth() * i * channelCount;
        size_t dstOffset = _data->getWidth() * (_data->getHeight()-i-1) * channelCount;

        memcpy( processedrawdata + dstOffset, originalrawdata + srcOffset, _data->getWidth()*channelCount * sizeof(T) );
    }

    return (unsigned char *) processedrawdata;
}
102
103
104
105

} // OpenGL
} // ACGL

Robert Menzel's avatar
Robert Menzel committed
106
#endif // ACGL_OPENGL_DATA_TEXTUREDATALOADSTORE_HH