TextureDataLoadStore.hh 3.97 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
34
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                               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);

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

//! saves the viewport visible part of the framebuffer 0 to a file named screenshot_DATE-TIME._fileEnding
inline bool saveScreenshot( const std::string _fileEnding = "png" ) {
    return saveScreenshot( "screenshot", _fileEnding );
}
41
42
43
44

///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Menzel's avatar
Robert Menzel committed
45
46
47
48

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

49
50
51
52
53
#ifdef ACGL_COMPILE_WITH_QT
//! loads from the QT library
SharedTextureData loadTextureDataFromQT(const std::string &_filename);
#endif

54
55
SharedTextureData loadTextureDataFromRGBE(const std::string &_filename);

56
57
SharedTextureData loadTextureDataFromEXR(const std::string &_filename);

58
59
60
61
62
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific save
///////////////////////////////////////////////////////////////////////////////////////////////////

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

Robert Menzel's avatar
Robert Menzel committed
65
//! save to a PNG file with lodepng
66
67
68
69
70
71
72
73
74
75
76
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
77
    unsigned int channelCount = _data->getNumberOfChannels();
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    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;
}
94
95
96
97

} // OpenGL
} // ACGL

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