├── CMakeLists.txt ├── README ├── code ├── dds_types.h ├── libdds.c ├── libdds.h ├── libdds_opengl.c └── libdds_opengl.h ├── images └── fungus.dds └── viewer └── main.c /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 2.6) 2 | 3 | project (libdds) 4 | 5 | set (CMAKE_LIBRARY_OUTPUT_DIRECTORY lib) 6 | include_directories (code) 7 | 8 | add_definitions (-Wall -fomit-frame-pointer) 9 | 10 | add_library (dds SHARED code/libdds.c) 11 | 12 | add_library (dds_opengl SHARED code/libdds_opengl.c) 13 | target_link_libraries (dds_opengl dds GL) 14 | 15 | add_executable (Viewer viewer/main.c) 16 | target_link_libraries (Viewer dds dds_opengl glut) 17 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | libdds 2 | =========== 3 | To see how to use it, take a look at viewer/main.c 4 | 5 | Everything else could be found on our wiki at https://github.com/bazhenovc/libdds 6 | 7 | Sincerelly yours, 8 | bazhenovc -------------------------------------------------------------------------------- /code/dds_types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * libdds 3 | * Copyright (c) 2010 Cyril Bazhenov bazhenovc@gmail.com 4 | * 5 | * Permission is hereby granted, free of charge, to any person 6 | * obtaining a copy of this software and associated documentation 7 | * files (the "Software"), to deal in the Software without 8 | * restriction, including without limitation the rights to use, 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following 12 | * conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | * OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef _DDS_TYPES_H 27 | #define _DDS_TYPES_H 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | typedef int dds_int; 34 | typedef unsigned int dds_uint; 35 | typedef char dds_byte; 36 | typedef unsigned char dds_ubyte; 37 | typedef long dds_long; 38 | typedef unsigned long dds_ulong; 39 | typedef void dds_void; 40 | typedef void* dds_ptr; 41 | typedef enum _dds_bool { 42 | DDS_FALSE = 0, 43 | DDS_TRUE 44 | } dds_bool; 45 | 46 | #ifdef _WIN32 47 | #define DDS_API __declspec(dllexport) 48 | #define DDS_APIENTRY __cdecl 49 | #else 50 | #define DDS_API extern 51 | #define DDS_APIENTRY 52 | #endif 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif /* _DDS_TYPES_H */ 59 | 60 | -------------------------------------------------------------------------------- /code/libdds.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libdds 3 | * Copyright (c) 2010 Cyril Bazhenov bazhenovc@gmail.com 4 | * 5 | * Permission is hereby granted, free of charge, to any person 6 | * obtaining a copy of this software and associated documentation 7 | * files (the "Software"), to deal in the Software without 8 | * restriction, including without limitation the rights to use, 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following 12 | * conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | * OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | 34 | /* SunOS 4.1.3_U1 has no SEEK_* defines */ 35 | #ifndef SEEK_CUR 36 | #define SEEK_CUR 1 37 | #endif 38 | 39 | #ifndef SEEK_END 40 | #define SEEK_END 2 41 | #endif 42 | 43 | #ifndef SEEK_SET 44 | #define SEEK_SET 0 45 | #endif 46 | 47 | /* Load DDS file 48 | */ 49 | dds_uint dds_load (const char* filename, DDSTextureInfo* texture) { 50 | if (texture == NULL) { 51 | printf ("dds_load: invalid surface!\n"); 52 | } 53 | FILE* file; 54 | char magic[4]; 55 | dds_long bufferSize, curr, end; 56 | 57 | file = fopen (filename, "rb"); 58 | if (!file) { 59 | return DDS_CANNOT_OPEN; 60 | } 61 | 62 | /* Read magic number */ 63 | fread (&magic, sizeof (char), 4, file); 64 | 65 | if (strncmp (magic, "DDS ", 4) != 0) { 66 | texture->ok = DDS_FALSE; 67 | fclose (file); 68 | return DDS_INVALID_FILE; 69 | } 70 | 71 | /* Get surface descriptor */ 72 | fread (&texture->surface, sizeof (DDSurfaceDesc), 1, file); 73 | 74 | /* Calculate pixel data size */ 75 | curr = ftell (file); 76 | fseek (file, 0, SEEK_END); 77 | end = ftell (file); 78 | fseek (file, curr, SEEK_SET); 79 | bufferSize = end - curr; 80 | texture->buffer_size = bufferSize; 81 | 82 | /* Read pixel data (with mipmaps) */ 83 | texture->texels = (dds_ubyte*)malloc (bufferSize * sizeof (dds_ubyte)); 84 | fread (texture->texels, sizeof (dds_ubyte), bufferSize, file); 85 | 86 | texture->ok = DDS_TRUE; 87 | 88 | fclose (file); 89 | return DDS_OK; 90 | } 91 | 92 | /* Free DDS texture 93 | */ 94 | void dds_free (DDSTextureInfo* texinfo) { 95 | if (texinfo->ok) { 96 | free (texinfo->texels); 97 | texinfo->texels = NULL; 98 | texinfo->ok = DDS_FALSE; 99 | } 100 | } 101 | 102 | /* Get error description 103 | */ 104 | const char* dds_error_string (int err) { 105 | switch (err) { 106 | case DDS_OK: 107 | return "No error"; 108 | case DDS_CANNOT_OPEN: 109 | return "Cannot open image file"; 110 | case DDS_INVALID_FILE: 111 | return "Given file does not appear to be a valid DDS file"; 112 | case DDS_BAD_COMPRESSION: 113 | return "Given file does not appear to be compressed with DXT1, DXT3 or DXT5"; 114 | case DDS_NO_GL_SUPPORT: 115 | return "Seems that OpenGL has no \"glCompressedTexImage2D\" extension"; 116 | case DDS_CANNOT_WRITE_FILE: 117 | return "Cannot write file"; 118 | case DDS_BAD_TEXINFO: 119 | return "Bad texture given"; 120 | default: 121 | return "Unknown error"; 122 | } 123 | } 124 | 125 | /* Free raw pixel data 126 | */ 127 | void dds_free_raw (DDSRawPixelData* data) { 128 | if (data->ok) { 129 | free (data->texels); 130 | data->ok = DDS_FALSE; 131 | } 132 | } 133 | 134 | /* Write DDS file 135 | */ 136 | dds_uint dds_write (const char* filename, const DDSTextureInfo* texinfo) { 137 | if (!texinfo->ok) { 138 | return DDS_BAD_TEXINFO; 139 | } 140 | 141 | FILE* file = fopen (filename, "wb"); 142 | if (!file) { 143 | return DDS_CANNOT_WRITE_FILE; 144 | } 145 | 146 | /* Write magic number */ 147 | fwrite ("DDS ", sizeof (char), 4, file); 148 | 149 | /* Write surface desc */ 150 | fwrite (&texinfo->surface, sizeof (DDSurfaceDesc), 1, file); 151 | 152 | /* Write compressed texels */ 153 | fwrite (texinfo->texels, sizeof (dds_ubyte), texinfo->buffer_size, file); 154 | 155 | fclose (file); 156 | return DDS_OK; 157 | } 158 | -------------------------------------------------------------------------------- /code/libdds.h: -------------------------------------------------------------------------------- 1 | /* 2 | * libdds 3 | * Copyright (c) 2010 Cyril Bazhenov bazhenovc@gmail.com 4 | * 5 | * Permission is hereby granted, free of charge, to any person 6 | * obtaining a copy of this software and associated documentation 7 | * files (the "Software"), to deal in the Software without 8 | * restriction, including without limitation the rights to use, 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following 12 | * conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | * OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef _LIBDDS_H 27 | #define _LIBDDS_H 28 | 29 | #include 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /** DirectDraw pixel format */ 36 | typedef struct _DDPixelFormat { 37 | dds_uint size; 38 | dds_uint flags; 39 | dds_uint fourcc; 40 | dds_uint bpp; 41 | dds_uint red_mask; 42 | dds_uint green_mask; 43 | dds_uint blue_mask; 44 | dds_uint alpha_mask; 45 | } DDPixelFormat; 46 | 47 | /** DDS caps */ 48 | typedef dds_uint DDSCaps[4]; 49 | 50 | /** DirectDraw color key */ 51 | typedef struct _DDColorKey { 52 | dds_uint low; 53 | dds_uint high; 54 | } DDColorKey; 55 | 56 | /** DirectDraw surface descriptor */ 57 | typedef struct _DDSurfaceDesc { 58 | dds_uint size; 59 | dds_uint flags; 60 | dds_uint height; 61 | dds_uint width; 62 | dds_uint pitch; 63 | dds_uint depth; 64 | dds_uint mip_level; 65 | dds_uint alpha_bit_depth; 66 | dds_uint __reserved; 67 | dds_uint surface; 68 | DDColorKey dest_overlay; 69 | DDColorKey dest_blit; 70 | DDColorKey src_overlay; 71 | DDColorKey src_blit; 72 | DDPixelFormat format; 73 | DDSCaps caps; 74 | dds_uint texture_stage; 75 | } DDSurfaceDesc; 76 | 77 | /** DDS texture info */ 78 | typedef struct _DDSTextureInfo { 79 | DDSurfaceDesc surface; 80 | dds_ubyte* texels; 81 | dds_long buffer_size; 82 | dds_bool ok; 83 | } DDSTextureInfo; 84 | 85 | /** Raw pixels */ 86 | typedef struct _DDSRawPixelData { 87 | dds_uint height; 88 | dds_uint width; 89 | dds_ubyte* texels; 90 | dds_bool ok; 91 | } DDSRawPixelData; 92 | 93 | #define DDS_MAKE_FOURCC(ch0, ch1, ch2, ch3) \ 94 | ((dds_uint)( \ 95 | (((dds_uint)(dds_ubyte)(ch3) << 24) & 0xFF000000) | \ 96 | (((dds_uint)(dds_ubyte)(ch2) << 16) & 0x00FF0000) | \ 97 | (((dds_uint)(dds_ubyte)(ch1) << 8) & 0x0000FF00) | \ 98 | ((dds_uint)(dds_ubyte)(ch0) & 0x000000FF) )) 99 | 100 | #define DDS_FOURCC_DXT1 DDS_MAKE_FOURCC('D', 'X', 'T', '1') 101 | #define DDS_FOURCC_DXT3 DDS_MAKE_FOURCC('D', 'X', 'T', '3') 102 | #define DDS_FOURCC_DXT5 DDS_MAKE_FOURCC('D', 'X', 'T', '5') 103 | #define DDS_FOURCC_BC4U DDS_MAKE_FOURCC('B', 'C', '4', 'U') 104 | #define DDS_FOURCC_BC4S DDS_MAKE_FOURCC('B', 'C', '4', 'S') 105 | #define DDS_FOURCC_BC5S DDS_MAKE_FOURCC('B', 'C', '5', 'S') 106 | 107 | /** Errors */ 108 | #define DDS_OK 0 109 | #define DDS_CANNOT_OPEN 1 110 | #define DDS_INVALID_FILE 2 111 | #define DDS_BAD_COMPRESSION 3 112 | #define DDS_NO_GL_SUPPORT 4 113 | #define DDS_CANNOT_WRITE_FILE 5 114 | #define DDS_BAD_TEXINFO 6 115 | 116 | /** Utils */ 117 | #define DDS_MAX(a, b) (a > b ? a : b) 118 | #define DDS_MIN(a, b) (a > b ? b : a) 119 | 120 | /* =====================Load/Free========================= */ 121 | 122 | /** Load DDS file 123 | * @par filename File name 124 | * @par texture Texture to load to 125 | * @return Error code 126 | */ 127 | DDS_API dds_uint DDS_APIENTRY dds_load (const char* filename, 128 | DDSTextureInfo* texture); 129 | 130 | /** Free DDS file 131 | * @par texinfo DDS texture info 132 | */ 133 | DDS_API void DDS_APIENTRY dds_free (DDSTextureInfo* texinfo); 134 | 135 | /** Get error string description 136 | * @par err Error code 137 | * @return Error description 138 | */ 139 | DDS_API const char* DDS_APIENTRY dds_error_string (int err); 140 | 141 | /** Free raw pixel data 142 | * @par data Data to free 143 | */ 144 | DDS_API void DDS_APIENTRY dds_free_raw (DDSRawPixelData* data); 145 | 146 | /* =====================Pack/Unpack======================= */ 147 | 148 | /** Unpack DXT1 compressed texture to raw data 149 | * @par texinfo Packed texture info 150 | * @par pixels Pixel data to unpack to 151 | * @return Error code 152 | * TODO: implement 153 | */ 154 | DDS_API dds_uint DDS_APIENTRY dds_dxt1_unpack (DDSTextureInfo* texinfo, 155 | DDSRawPixelData* pixels); 156 | 157 | /** Unpack DXT3 compressed texture to raw data 158 | * @par texinfo Packed texture info 159 | * @par pixels Pixel data to unpack to 160 | * @return Error code 161 | * TODO: implement 162 | */ 163 | DDS_API dds_uint DDS_APIENTRY dds_dxt3_unpack (DDSTextureInfo* texinfo, 164 | DDSRawPixelData* pixels); 165 | 166 | /** Unpack DXT5 compressed texture to raw data 167 | * @par texinfo Packed texture info 168 | * @par pixels Pixel data to unpack to 169 | * @return Error code 170 | * TODO: implement 171 | */ 172 | DDS_API dds_uint DDS_APIENTRY dds_dxt5_unpack (DDSTextureInfo* texinfo, 173 | DDSRawPixelData* pixels); 174 | 175 | /** Pack raw pixel data with DXT1 176 | * @par pixels Raw pixel data 177 | * @par texinfo Texture info 178 | * @return Error code 179 | * TODO: implement 180 | */ 181 | DDS_API dds_uint DDS_APIENTRY dds_dxt1_pack (DDSRawPixelData* pixels, 182 | DDSTextureInfo* texinfo); 183 | 184 | /** Pack raw pixel data with DXT3 185 | * @par pixels Raw pixel data 186 | * @par texinfo Texture info 187 | * @return Error code 188 | * TODO: implement 189 | */ 190 | DDS_API dds_uint DDS_APIENTRY dds_dxt3_pack (DDSRawPixelData* pixels, 191 | DDSTextureInfo* texinfo); 192 | 193 | /** Pack raw pixel data with DXT5 194 | * @par pixels Raw pixel data 195 | * @par texinfo Texture info 196 | * @return Error code 197 | * TODO: implement 198 | */ 199 | DDS_API dds_uint DDS_APIENTRY dds_dxt5_pack (DDSRawPixelData* pixels, 200 | DDSTextureInfo* texinfo); 201 | 202 | /* =====================Write============================= */ 203 | 204 | /** Write DDS texture file 205 | * @par filename File name 206 | * @par texinfo Texture info to write 207 | * @return Error code 208 | */ 209 | DDS_API dds_uint DDS_APIENTRY dds_write (const char* filename, 210 | const DDSTextureInfo* texinfo); 211 | 212 | #ifdef __cplusplus 213 | } 214 | #endif 215 | 216 | #endif 217 | 218 | -------------------------------------------------------------------------------- /code/libdds_opengl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libdds 3 | * Copyright (c) 2010 Cyril Bazhenov bazhenovc@gmail.com 4 | * 5 | * Permission is hereby granted, free of charge, to any person 6 | * obtaining a copy of this software and associated documentation 7 | * files (the "Software"), to deal in the Software without 8 | * restriction, including without limitation the rights to use, 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following 12 | * conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | * OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #ifdef _WIN32 34 | #include 35 | #else 36 | #include 37 | #endif 38 | 39 | PFNGLCOMPRESSEDTEXIMAGE2DARBPROC ddsglCompressedTexImage2D = NULL; 40 | 41 | static int ddsgl_init () { 42 | if (ddsglCompressedTexImage2D) { 43 | return (1); 44 | } 45 | #ifdef _WIN32 46 | ddsglCompressedTexImage2D = (PFNGLCOMPRESSEDTEXIMAGE2DARBPROC) wglGetProcAddress ("glCompressedTexImage2DARB"); 47 | #else 48 | ddsglCompressedTexImage2D = (PFNGLCOMPRESSEDTEXIMAGE2DARBPROC) glXGetProcAddress ((const dds_ubyte*) "glCompressedTexImage2DARB"); 49 | #endif 50 | return (ddsglCompressedTexImage2D != NULL); 51 | } 52 | 53 | dds_uint ddsGL_load (const char* filename, DDS_GL_TextureInfo* texture) { 54 | if (!ddsgl_init ()) { 55 | return DDS_NO_GL_SUPPORT; 56 | } 57 | DDSTextureInfo textureInfo; 58 | dds_int error = dds_load (filename, &textureInfo); 59 | if (error != DDS_OK) { 60 | return error; 61 | } 62 | texture->width = textureInfo.surface.width; 63 | texture->height = textureInfo.surface.height; 64 | texture->num_mipmaps = textureInfo.surface.mip_level; 65 | switch (textureInfo.surface.format.fourcc) { 66 | case DDS_FOURCC_DXT1: 67 | texture->format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; 68 | texture->internal_format = 3; 69 | break; 70 | case DDS_FOURCC_DXT3: 71 | texture->format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; 72 | texture->internal_format = 4; 73 | break; 74 | case DDS_FOURCC_DXT5: 75 | texture->format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; 76 | texture->internal_format = 4; 77 | break; 78 | case DDS_FOURCC_BC4U: 79 | texture->format = GL_COMPRESSED_RED_RGTC1_EXT; 80 | texture->internal_format = 1; 81 | break; 82 | case DDS_FOURCC_BC4S: 83 | texture->format = GL_COMPRESSED_SIGNED_RED_RGTC1_EXT; 84 | texture->internal_format = 1; 85 | break; 86 | case DDS_FOURCC_BC5S: 87 | texture->format = GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT; 88 | texture->internal_format = 2; 89 | break; 90 | default: 91 | dds_free (&textureInfo); 92 | return DDS_BAD_COMPRESSION; 93 | } 94 | /* Generate new texture */ 95 | glGenTextures (1, &texture->id); 96 | glEnable (GL_TEXTURE_2D); 97 | glBindTexture (GL_TEXTURE_2D, texture->id); 98 | /* Filtering */ 99 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 100 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 101 | size_t blockSize = texture->format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ? 8 : 16; 102 | size_t offset = 0; 103 | dds_uint mipWidth = texture->width, 104 | mipHeight = texture->height, 105 | mipSize; 106 | /* Upload mipmaps to video memory */ 107 | size_t mip; 108 | for (mip = 0; mip < texture->num_mipmaps; mip++) { 109 | mipSize = ((mipWidth + 3) / 4) * ((mipHeight + 3) / 4) * blockSize; 110 | 111 | ddsglCompressedTexImage2D (GL_TEXTURE_2D, mip, texture->format, 112 | mipWidth, mipHeight, 0, mipSize, 113 | textureInfo.texels + offset); 114 | 115 | mipWidth = DDS_MAX (mipWidth >> 1, 1); 116 | mipHeight = DDS_MAX (mipHeight >> 1, 1); 117 | 118 | offset += mipSize; 119 | } 120 | dds_free (&textureInfo); 121 | glDisable (GL_TEXTURE_2D); 122 | return DDS_OK; 123 | } 124 | 125 | void ddsGL_free (DDS_GL_TextureInfo* texture) { 126 | if (texture->id) { 127 | glDeleteTextures (1, &texture->id); 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /code/libdds_opengl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * libdds 3 | * Copyright (c) 2010 Cyril Bazhenov bazhenovc@gmail.com 4 | * 5 | * Permission is hereby granted, free of charge, to any person 6 | * obtaining a copy of this software and associated documentation 7 | * files (the "Software"), to deal in the Software without 8 | * restriction, including without limitation the rights to use, 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following 12 | * conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | * OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | #ifndef _LIBDDS_OPENGL_H 27 | #define _LIBDDS_OPENGL_H 28 | 29 | #include 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /** OpenGL texture info */ 36 | typedef struct _DDS_GL_TextureInfo { 37 | dds_int width; 38 | dds_int height; 39 | dds_uint format; 40 | dds_int internal_format; 41 | dds_uint id; 42 | dds_uint num_mipmaps; 43 | } DDS_GL_TextureInfo; 44 | 45 | /** Load OpenGL texture 46 | * @par filename Texture filename 47 | * @par texture Texture to load to 48 | * @return Error code 49 | */ 50 | DDS_API dds_uint DDS_APIENTRY ddsGL_load (const char* filename, 51 | DDS_GL_TextureInfo* texture); 52 | 53 | /** Release OpenGL texture 54 | * @par texture Texture to release 55 | */ 56 | DDS_API void DDS_APIENTRY ddsGL_free (DDS_GL_TextureInfo* texture); 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | #endif /* _LIBDDS_OPENGL_H */ 63 | 64 | -------------------------------------------------------------------------------- /images/fungus.dds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bazhenovc/libdds/5faee029a18e709aa014e8932a6a7412a02d9776/images/fungus.dds -------------------------------------------------------------------------------- /viewer/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libdds 3 | * Copyright (c) 2010 Cyril Bazhenov bazhenovc@gmail.com 4 | * 5 | * Permission is hereby granted, free of charge, to any person 6 | * obtaining a copy of this software and associated documentation 7 | * files (the "Software"), to deal in the Software without 8 | * restriction, including without limitation the rights to use, 9 | * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the 11 | * Software is furnished to do so, subject to the following 12 | * conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | * OTHER DEALINGS IN THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | /* Our texture */ 36 | static DDS_GL_TextureInfo texture; 37 | 38 | /* Cleanup */ 39 | static void cleanup () { 40 | ddsGL_free (&texture); 41 | } 42 | 43 | /* Draw our texture */ 44 | static void display () { 45 | glClear (GL_COLOR_BUFFER_BIT); 46 | 47 | glEnable (GL_TEXTURE_2D); 48 | glBindTexture (GL_TEXTURE_2D, texture.id); 49 | glBegin (GL_QUADS); 50 | glTexCoord2f (0.0f, 0.0f); 51 | glVertex3f (-0.5f, -0.5f, 0.0f); 52 | 53 | glTexCoord2f (1.0f, 0.0f); 54 | glVertex3f (0.5f, -0.5f, 0.0f); 55 | 56 | glTexCoord2f (1.0f, 1.0f); 57 | glVertex3f (0.5f, 0.5f, 0.0f); 58 | 59 | glTexCoord2f (0.0f, 1.0f); 60 | glVertex3f (-0.5f, 0.5f, 0.0f); 61 | glEnd (); 62 | glutSwapBuffers (); 63 | } 64 | 65 | static void key (unsigned char key, int x, int y) { 66 | if (key == 27) { 67 | exit (0); 68 | } 69 | } 70 | 71 | int main (int argc, char** argv) { 72 | const char* imagename = argc < 2 ? "images/fungus.dds" : argv[1]; 73 | 74 | glutInit (&argc, argv); 75 | glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGBA); 76 | glutInitWindowSize (800, 600); 77 | 78 | glutCreateWindow (imagename); 79 | 80 | glutDisplayFunc (display); 81 | glutKeyboardFunc (key); 82 | 83 | /* Load texture */ 84 | int err = ddsGL_load (imagename, &texture); 85 | if (err != DDS_OK) { 86 | printf ("DDS load error: %s\n", dds_error_string (err)); 87 | return (1); 88 | } 89 | atexit (cleanup); 90 | 91 | glutMainLoop (); 92 | 93 | return (0); 94 | } 95 | --------------------------------------------------------------------------------