Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "Headers/ImageTools.h"

#include "Core/Headers/Console.h"
#include "Core/Headers/StringHelper.h"
#include "Utility/Headers/Localization.h"
#include "Platform/Video/Headers/RenderAPIEnums.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "nv_dds.h"

#define IL_STATIC_LIB
#undef _UNICODE
#include <IL/ilu.h>

namespace Divide {
namespace ImageTools {

std::mutex ImageDataInterface::_loadingMutex;

ImageData::ImageData() : _compressed(false),
                         _flip(false),
                         _alpha(false),
                         _bpp(0)

{
    _format = GFXImageFormat::COUNT;
    _compressedTextureType = TextureType::COUNT;
}

ImageData::~ImageData()
{
}

bool ImageData::create(const stringImpl& filename) {
    _name = filename;

    if (Util::CompareIgnoreCase(_name.substr(_name.find_last_of('.') + 1), "DDS")) {
        return loadDDS_IL(filename);
    }

    stbi_set_flip_vertically_on_load(_flip ? TRUE : FALSE);

    I32 width = 0, height = 0, comp = 0;
    U8* data = nullptr;
    F32* dataf = nullptr;
    bool isHDR = stbi_is_hdr(_name.c_str()) == TRUE;
    if (isHDR) {
        dataf = stbi_loadf(_name.c_str(), &width, &height, &comp, 0);
    } else {
        data = stbi_load(_name.c_str(), &width, &height, &comp, 0);
    }

    if ((isHDR && dataf == NULL) || (!isHDR && data == NULL)) {
        Console::errorfn(Locale::get(_ID("ERROR_IMAGETOOLS_INVALID_IMAGE_FILE")), _name.c_str());
        return false;
    }

    _data.resize(1);
    ImageLayer& image = _data.front();

    switch (comp) {
        case 1 : {
            _format = GFXImageFormat::LUMINANCE;
            _bpp = 8;
        } break;
        case 2: {
            _format = GFXImageFormat::LUMINANCE_ALPHA;
            _bpp = 16;
        } break;
        case 3: {
            _format = GFXImageFormat::RGB;
            _bpp = 24;
        } break;
        case 4: {
            _format = GFXImageFormat::RGBA;
            _bpp = 32;
        } break;
        default:
            DIVIDE_UNEXPECTED_CALL("Invalid file format!");
            break;
    };

    if (isHDR) {
        _bpp *= 4;
    }

    image._dimensions.set(width, height, 1);
    // most formats do not have an alpha channel
    _alpha = comp % 2 == 0;
    _compressed = false;
    image._size = width * height * _bpp / 8;

    if (isHDR) {
        image.setData(dataf, to_uint(image._size / 4));
        stbi_image_free(dataf);
    } else {
        image.setData(data, to_uint(image._size));
        stbi_image_free(data);
    }

    return true;
}

bool ImageData::loadDDS_IL(const stringImpl& filename) {
    U32 ilTexture = 0;
    ilInit();
    ilGenImages(1, &ilTexture);
    ilBindImage(ilTexture);


    ilEnable(IL_TYPE_SET);
    ilEnable(IL_ORIGIN_SET);
    ilOriginFunc(_flip ? IL_ORIGIN_LOWER_LEFT : IL_ORIGIN_UPPER_LEFT);
    ilSetInteger(IL_KEEP_DXTC_DATA, IL_TRUE);
    ilTypeFunc(IL_UNSIGNED_BYTE);

    if (ilLoadImage(filename.c_str()) == IL_FALSE) {
        Console::errorfn(Locale::get(_ID("ERROR_IMAGETOOLS_INVALID_IMAGE_FILE")), _name.c_str());
        ilDeleteImage(ilTexture);
        return false;
    }

    I32 numMips = ilGetInteger(IL_NUM_MIPMAPS) + 1;
    assert(ilGetInteger(IL_IMAGE_TYPE) == IL_UNSIGNED_BYTE);

    I32 dxtc = ilGetInteger(IL_DXTC_DATA_FORMAT);
    _bpp = to_const_ubyte(ilGetInteger(IL_IMAGE_BPP));

    if (ilGetInteger(IL_IMAGE_CUBEFLAGS) > 0) {
        _compressedTextureType = TextureType::TEXTURE_CUBE_MAP;
    } else {
        _compressedTextureType = ilGetInteger(IL_IMAGE_DEPTH) > 1
                                     ? TextureType::TEXTURE_3D
                                     : TextureType::TEXTURE_2D;
    }

    bool compressed = ((dxtc == IL_DXT1) || 
                       (dxtc == IL_DXT2) ||
                       (dxtc == IL_DXT3) ||
                       (dxtc == IL_DXT4) ||
                       (dxtc == IL_DXT5));

    if (compressed) {
        switch (dxtc) {
            case IL_DXT1: {
                _format = 
                    ilGetInteger(IL_IMAGE_CHANNELS) == 3
                                                    ? GFXImageFormat::COMPRESSED_RGB_DXT1
                                                    : GFXImageFormat::COMPRESSED_RGBA_DXT1;
                 
            }  break;
            case IL_DXT3: {
                _format = GFXImageFormat::COMPRESSED_RGBA_DXT3;
            } break;
            case IL_DXT5: {
                _format = GFXImageFormat::COMPRESSED_RGBA_DXT5;
            } break;
            default: {
                DIVIDE_UNEXPECTED_CALL("Unsupported compressed format!");
                ilDeleteImage(ilTexture);
                return false;
            }
        };
    } else {
        switch (ilGetInteger(IL_IMAGE_FORMAT)) {
            case IL_BGR: {
                _format = GFXImageFormat::BGR;
            } break;
            case IL_RGB: {
                _format = GFXImageFormat::RGB;
            } break;
            case IL_BGRA: {
                _format = GFXImageFormat::BGRA;
            } break;
            case IL_RGBA: {
                _format = GFXImageFormat::RGBA;
            } break;
            case IL_LUMINANCE: {
                _format = GFXImageFormat::LUMINANCE;
            } break;
            case IL_LUMINANCE_ALPHA: {
                _format = GFXImageFormat::LUMINANCE_ALPHA;
            } break;
            default: {
                DIVIDE_UNEXPECTED_CALL("Unsupported image format!");
                ilDeleteImage(ilTexture);
                return false;
            }
        };
    }

    _data.resize(numMips);
    for (U8 i = 0; i < numMips; ++i) {
        ilBindImage(ilTexture);
        ilActiveMipmap(i);

        ILint width = ilGetInteger(IL_IMAGE_WIDTH);
        ILint height = ilGetInteger(IL_IMAGE_HEIGHT);
        ILint depth = ilGetInteger(IL_IMAGE_DEPTH);

        ImageLayer& layer = _data[i];
        layer._dimensions.set(width, height, depth);

        I32 size = compressed ? ilGetDXTCData(NULL, 0, dxtc)
                              : width * height * depth * _bpp;

        I32 numImagePasses = _compressedTextureType == TextureType::TEXTURE_CUBE_MAP ? 6 : 1;
        layer._size = size * numImagePasses;
        layer._data.resize(layer._size);

        for (I32 j = 0, offset = 0; j < numImagePasses; ++j, offset += size) {
            if (_compressedTextureType == TextureType::TEXTURE_CUBE_MAP) {
                ilBindImage(ilTexture);
                ilActiveImage(j);
                ilActiveMipmap(i);
            }
            if (compressed) {
                ilGetDXTCData(&layer._data[0] + offset, size, dxtc);
            } else {
                memcpy(&layer._data[0] + offset, ilGetData(), size);
            }
        }
    }
    
    ilDeleteImage(ilTexture);
    return true;
}

bool ImageData::loadDDS_NV(const stringImpl& filename) {
    nv_dds::CDDSImage image;
    image.load(filename.c_str(), _flip);
    _alpha = image.get_components() == 4;
    switch(image.get_type()) {
        case nv_dds::TextureFlat:
            _compressedTextureType = image.get_height() == 0 ? TextureType::TEXTURE_1D
                                                             : TextureType::TEXTURE_2D;
            break;
        case nv_dds::Texture3D:
            _compressedTextureType = TextureType::TEXTURE_3D;
            break;
        case nv_dds::TextureCubemap:
            _compressedTextureType = TextureType::TEXTURE_CUBE_MAP;
            break;
        default:
            DIVIDE_UNEXPECTED_CALL("Unsupported texture type!");
            break;
    };

    _compressed = image.is_compressed();
    if (_compressed) {
        _bpp = image.get_format() == nv_dds::Format::DXT1 ? 8 : 16;
        switch(image.get_format()) {
            case nv_dds::Format::DXT1:
                _format = GFXImageFormat::COMPRESSED_RGB_DXT1;
                break;
            case nv_dds::Format::DXT3:
                _format = GFXImageFormat::COMPRESSED_RGBA_DXT3;
                break;
            case nv_dds::Format::DXT5:
                _format = GFXImageFormat::COMPRESSED_RGBA_DXT5;
                break;
        };
    } else {
        switch(image.get_format()) {
            case nv_dds::Format::BGR: {
                _bpp = 24;   
                _format = GFXImageFormat::BGR;
            } break;
            case nv_dds::Format::RGB: {
                _bpp = 24;
                _format = GFXImageFormat::RGB;
            } break;
            case nv_dds::Format::BGRA: {
                _bpp = 32;
                _format = GFXImageFormat::BGRA;
            } break;
            case nv_dds::Format::RGBA: {
                _format = GFXImageFormat::RGBA;
                _bpp = 32;
            } break;
            case nv_dds::Format::LUMINANCE: {
                _format = GFXImageFormat::LUMINANCE;
                _bpp = 8;
            } break;
            default:
                DIVIDE_UNEXPECTED_CALL("Unsupported image format!");
                break;
        };
    }

    U32 numMips = image.get_num_mipmaps();
    _data.resize(numMips + 1);
    ImageLayer& base = _data[0];
    base._dimensions.set(image.get_width(),
                         image.get_height(),
                         image.get_depth());
    base._size = image.get_size();
    base.setData(image, base._dimensions.width * base._dimensions.height);

    for (U8 i = 0; i < numMips; ++i) {
        ImageLayer& layer = _data[i + 1];
        const nv_dds::CSurface& mipMap = image.get_mipmap(i);

        layer._dimensions.set(mipMap.get_width(),
                              mipMap.get_height(),
                              mipMap.get_depth());
        layer._size = mipMap.get_size();
        layer.setData(mipMap, layer._dimensions.width * layer._dimensions.height);
    }

    image.clear();
    return true;
}

vec4<U8> ImageData::getColour(I32 x, I32 y, U32 mipLevel) const {
    vec4<U8> returnColour;
    getColour(x, y, returnColour.r, returnColour.g, returnColour.b, returnColour.a, mipLevel);
    return returnColour;
}

void ImageData::getColour(I32 x, I32 y, U8& r, U8& g, U8& b, U8& a, U32 mipLevel) const {
    I32 idx = (y * _data[mipLevel]._dimensions.width + x) * (_bpp / 8);
    r = _data[mipLevel]._data[idx + 0];
    g = _data[mipLevel]._data[idx + 1];
    b = _data[mipLevel]._data[idx + 2];
    a = _alpha ? _data[mipLevel]._data[idx + 3] : 255;
}


void ImageDataInterface::CreateImageData(const stringImpl& filename, ImageData& imgOut) {
    if (fileExists(filename.c_str())) {
        std::lock_guard<std::mutex> lock(_loadingMutex);
        imgOut.create(filename);
    }
}

I8 SaveToTGA(const stringImpl& filename, const vec2<U16>& dimensions, U8 pixelDepth, U8* imageData) {
    U8 cGarbage = 0, type, mode, aux;
    I16 iGarbage = 0;
    U16 width = dimensions.width;
    U16 height = dimensions.height;

    // open file and check for errors
    FILE* file = fopen(filename.c_str(), "wb");
    if (file == nullptr) {
        return (-1);
    }

    // compute image type: 2 for RGB(A), 3 for greyscale
    mode = pixelDepth / 8;
    type = ((pixelDepth == 24) || (pixelDepth == 32)) ? 2 : 3;

    // write the header
    fwrite(&cGarbage, sizeof(U8), 1, file);
    fwrite(&cGarbage, sizeof(U8), 1, file);
    fwrite(&type, sizeof(U8), 1, file);

    fwrite(&iGarbage, sizeof(I16), 1, file);
    fwrite(&iGarbage, sizeof(I16), 1, file);
    fwrite(&cGarbage, sizeof(U8), 1, file);
    fwrite(&iGarbage, sizeof(I16), 1, file);
    fwrite(&iGarbage, sizeof(I16), 1, file);

    fwrite(&width, sizeof(U16), 1, file);
    fwrite(&height, sizeof(U16), 1, file);
    fwrite(&pixelDepth, sizeof(U8), 1, file);
    fwrite(&cGarbage, sizeof(U8), 1, file);

    // convert the image data from RGB(a) to BGR(A)
    if (mode >= 3)
        for (I32 i = 0; i < width * height * mode; i += mode) {
            aux = imageData[i];
            imageData[i] = imageData[i + 2];
            imageData[i + 2] = aux;
        }

    // save the image data
    fwrite(imageData, sizeof(U8), width * height * mode, file);
    fclose(file);
    return 0;
}

/// saves a series of files with names "filenameX.tga"
I8 SaveSeries(const stringImpl& filename, const vec2<U16>& dimensions, U8 pixelDepth,
              U8* imageData) {
    static I32 savedImages = 0;
    // compute the new filename by adding the
    // series number and the extension
    stringImpl newFilename(Util::StringFormat("%s_%d.tga", filename.c_str(), savedImages));

    // save the image
    I8 status = SaveToTGA(newFilename, dimensions, pixelDepth, imageData);

    // increase the counter
    if (status == 0) {
        savedImages++;
    }

    return status;
}
};  // namespace ImageTools
};  // namespace Divide

Commits for Divide-Framework/trunk/Source Code/Utility/ImageTools.cpp

Diff revisions: vs.
Revision Author Commited Message
832 Diff Diff IonutCava picture IonutCava Thu 26 Jan, 2017 17:23:11 +0000

[IonutCava]
- More path related work
- Initial code to centralize file I/O (does not compile yet)

831 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 23:52:34 +0000

[IonutCava]
- More path related updates

830 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper

760 Diff Diff IonutCava picture IonutCava Tue 09 Aug, 2016 16:30:12 +0000

[IonutCava]
- Complete rewrite of RenderPass system:
— Allow every rendering pass (reflection, environment, etc) to use the same rendering system: occlusion culling, light culling, etc
— Define render passes by a param based token similar to modern API systems (DX12/Vulkan)
— W.I.P.: Depth rendering slightly wrong; Environment Mapping ,reflection and refraction rendering currently disabled
- Add compile time variables for DEBUG/PROFILE/RELEASE builds to validate all code for every build

759 Diff Diff IonutCava picture IonutCava Sun 07 Aug, 2016 18:34:32 +0000

[IonutCava]
- Add workaround for singlethreaded texture loading
- Small corrections to ImageTools image loading

727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

721 Diff Diff IonutCava picture IonutCava Wed 08 Jun, 2016 15:47:49 +0000

[IonutCava]
- Spelling: change all references from our code of “color” to the PROPER British version of “colour” because it sounds 100x better

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

660 Diff Diff IonutCava picture IonutCava Mon 21 Mar, 2016 17:11:19 +0000

[IonutCava]
- Finish Parent<->Children thread pool system
- Use new thread pool system for most multithreaded tasks in the engine: culling, sorting, light preparation, etc
- Add platform specific init/close calls at the start and end of the main function for future use

653 IonutCava picture IonutCava Fri 26 Feb, 2016 13:37:24 +0000

[IonutCava]
- Finished DXT texture loading system using DevIL
— NV_DDS implementation not finished yet