TextureDataLoadStore.hh 5.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
 */

#include <ACGL/ACGL.hh>
16
#include <ACGL/OpenGL/Tools.hh>
17
18
#include <ACGL/OpenGL/Data/ColorSpace.hh>
#include <ACGL/OpenGL/Data/TextureData.hh>
19
20

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

namespace ACGL{
namespace OpenGL{

27
28
29
30
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                               generic load/save
///////////////////////////////////////////////////////////////////////////////////////////////////

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

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

40
//! generic load function that will use one of the loading functions below based on the file ending
Janis Born's avatar
Janis Born committed
41
SharedTextureData loadTextureData(const std::string &_filename, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT);
42
43
44
45

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

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

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

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

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

//! loads from a PNG using the simple lodepng library
Janis Born's avatar
Janis Born committed
62
SharedTextureData loadTextureDataFromLodepng(const std::string& _filename, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT);
Robert Menzel's avatar
Robert Menzel committed
63

64
#ifdef ACGL_COMPILE_WITH_QT
65
//! loads various formats from the QT library
Janis Born's avatar
Janis Born committed
66
SharedTextureData loadTextureDataFromQT(const std::string& _filename, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT);
67
68
#endif

69
//! loads RGBE aka Radiance files
Janis Born's avatar
Janis Born committed
70
SharedTextureData loadTextureDataFromRGBE(const std::string& _filename, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT);
71

72
//! loads EXR / OpenEXR files iff the library is present AT RUNTIME (linux only)
Janis Born's avatar
Janis Born committed
73
SharedTextureData loadTextureDataFromEXR(const std::string& _filename, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT);
74

75
//! loads PNM / PPM files:
Janis Born's avatar
Janis Born committed
76
SharedTextureData loadTextureDataFromPNM(const std::string& _filename, ColorSpace _colorSpace = ColorSpace::AUTO_DETECT);
77

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

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

Robert Menzel's avatar
Robert Menzel committed
85
//! save to a PNG file with lodepng
86
bool saveTextureDataToLodepng( const SharedTextureData &_data, const std::string &_filename );
87
88
89

//! save the imagedata raw to a file
bool saveTextureDataToRAW( const SharedTextureData &_data, const std::string &_filename );
90
91
92
93
94
95
96
97
98
99

#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
100
    unsigned int channelCount = _data->getNumberOfChannels();
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

    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;
}
117
118
119
120

} // OpenGL
} // ACGL

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