TextureDataLoadStore.hh 4.93 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

#include <string>
20
21
#include <map>
#include <vector>
22
23
24
25

namespace ACGL{
namespace OpenGL{

26
27
28
29
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                               generic load/save
///////////////////////////////////////////////////////////////////////////////////////////////////

30
31
32
33
34
35
36
37
38
//! functionpointer which can be used for generic Texture loading
typedef SharedTextureData (*TextureLoadFuncPtr)(const std::string&);

//! registers a functionpointer to be used for loading specified file types (E.G. provide a load function which loads compressed)
void registerTextureLoadFunction(std::vector<std::string> _endings, TextureLoadFuncPtr _function);

//! remove a registered functionpointer (if you want to use the stock ones again
void unregisterTextureLoadFunction(TextureLoadFuncPtr _function);

39
40
41
42
43
44
//! 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);

45
46
47
//! 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 );

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

//! saves the viewport visible part of the framebuffer 0 to a file named screenshot_DATE-TIME._fileEnding
52
53
inline bool saveScreenshotWithDate( const std::string& _fileEnding = "png" ) {
    return saveScreenshotWithDate( "screenshot", _fileEnding );
Robert Menzel's avatar
Robert Menzel committed
54
}
55
56
57
58

///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Menzel's avatar
Robert Menzel committed
59
60
61
62

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

63
#ifdef ACGL_COMPILE_WITH_QT
64
//! loads various formats from the QT library
65
66
67
SharedTextureData loadTextureDataFromQT(const std::string &_filename);
#endif

68
//! loads RGBE aka Radiance files
69
70
SharedTextureData loadTextureDataFromRGBE(const std::string &_filename);

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

74
75
76
//! loads PNM / PPM files:
SharedTextureData loadTextureDataFromPNM(const std::string &_filename);

77
78
79
80
81
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific save
///////////////////////////////////////////////////////////////////////////////////////////////////

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

Robert Menzel's avatar
Robert Menzel committed
84
//! save to a PNG file with lodepng
85
86
87
88
89
90
91
92
93
94
95
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
96
    unsigned int channelCount = _data->getNumberOfChannels();
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

    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;
}
113
114
115
116

} // OpenGL
} // ACGL

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