TextureDataLoadStore.hh 3.63 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
35
36
37
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                               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
bool saveScreenshot( const std::string &_prefix = "screenshot", const std::string _fileEnding = "png" );

///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
Robert Menzel's avatar
Robert Menzel committed
40
41
42
43

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

44
45
46
47
48
49
50
51
52
53
54
55
#ifdef ACGL_COMPILE_WITH_QT
//! loads from the QT library
SharedTextureData loadTextureDataFromQT(const std::string &_filename);
#endif

///////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                           library specific save
///////////////////////////////////////////////////////////////////////////////////////////////////

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

Robert Menzel's avatar
Robert Menzel committed
56
//! save to a PNG file with lodepng
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
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)
{
    unsigned int channelCount = getNumberOfChannels( _data->getFormat() );

    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;
}
85
86
87
88

} // OpenGL
} // ACGL

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