Welcome to mirror list, hosted at ThFree Co, Russian Federation.

nv_image.h « Images « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f00ea74a3e2ee005f498a2fef5db0024b2b50d5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//-----------------------------------------------------------------------------
//           Name: nv_image.h
//      Developer: Wolfire Games LLC
//    Description: This is a file from an Nvidia image library, with some 
//                 modification made by Wolfire.
//        License: Read below
//-----------------------------------------------------------------------------
//
// nvImage.h - Image support class
//
// The nvImage class implements an interface for a multipurpose image
// object. This class is useful for loading and formating images
// for use as textures. The class supports dds, png, and hdr formats.
//
// Author: Evan Hart
// Email: sdkfeedback@nvidia.com
//
// Copyright (c) NVIDIA Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////
#pragma once

#include <opengl.h>

#include <vector>
#include <cassert>

namespace nv2 {

    class Image {
    public:

        Image();
        virtual ~Image();

        // return the width of the image
        int getWidth() const { return _width; }

        //return the height of the image
        int getHeight() const { return _height; }

        //return the dpeth of the image (0 for images with no depth)
        int getDepth() const { return _depth; }

        //return the number of mipmap levels available for the image
        int getMipLevels() const { return _levelCount; }

        //return the number of cubemap faces available for the image (0 for non-cubemap images)
        int getFaces() const { return _faces; }

        //return the format of the image data (GL_RGB, GL_BGR, etc)
        GLenum getFormat() const { return _format; }

        //return the suggested internal format for the data
        GLenum getInternalFormat() const { return _internalFormat; }

        //return the type of the image data
        GLenum getType() const { return _type; }

        //return the Size in bytes of a level of the image 
        int getImageSize(int level = 0) const;

        //return whether the data is a crompressed format
        bool isCompressed() const {
            switch(_format) {
                case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
                case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
                case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
                case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
                case GL_COMPRESSED_LUMINANCE_LATC1_EXT:
                case GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT:
                case GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT:
                case GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT:
                    return true;
            }
            return false;
        }

        //return whether the image represents a cubemap
        bool isCubeMap() const { return _faces > 0; }

        //return whether the image represents a volume
        bool isVolume() const { return _depth > 0; }

        //get a pointer to level data
        const void* getLevel( int level, GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X) const;
        void* getLevel( int level, GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X);

        //initialize an image from a file
        bool loadImageFromFile( const char* file);

        //convert a suitable image from a cubemap cross to a cubemap (returns false for unsuitable images)
        bool convertCrossToCubemap();

        //load an image from memory, for the purposes of saving
        bool setImage( int width, int height, GLenum format, GLenum type, const void* data);

        //save an image to a file
        bool saveImageToFile( const char* file);

    protected:
        int _width;
        int _height;
        int _depth;
        int _levelCount;
        int _faces;
        GLenum _format;
        GLenum _internalFormat;
        GLenum _type;
        int _elementSize;

        //pointers to the levels
        std::vector<GLubyte*> _data;

        void freeData();
        void flipSurface(GLubyte *surf, int width, int height, int depth);

        //
        // Static elements used to dispatch to proper sub-readers
        //
        //////////////////////////////////////////////////////////////
        struct FormatInfo {
            const char* extension;
            bool (*reader)( const char* file, Image& i);
            bool (*writer)( const char* file, Image& i);
        };

        static FormatInfo formatTable[]; 

        static bool readDDS( const char *file, Image& i);

        static void flip_blocks_dxtc1(GLubyte *ptr, unsigned int numBlocks);
        static void flip_blocks_dxtc3(GLubyte *ptr, unsigned int numBlocks);
        static void flip_blocks_dxtc5(GLubyte *ptr, unsigned int numBlocks);
    };
}