4
4
#include < utility>
5
5
#include < ostream>
6
6
#include < fstream>
7
- #include < memory>
8
7
#include < cstring>
9
8
10
9
#include < IL/il.h>
@@ -234,8 +233,8 @@ bool CBitmap::Load(std::string const& filename, unsigned char defaultAlpha)
234
233
return false ;
235
234
}
236
235
237
- unsigned char * buffer = new unsigned char [ file.FileSize () + 2 ] ;
238
- file.Read (buffer, file.FileSize ());
236
+ std::vector< unsigned char > buffer ( file.FileSize () + 2 ) ;
237
+ file.Read (buffer. data () , file.FileSize ());
239
238
240
239
std::lock_guard<spring::mutex> lck (devilMutex);
241
240
ilOriginFunc (IL_ORIGIN_UPPER_LEFT);
@@ -249,13 +248,12 @@ bool CBitmap::Load(std::string const& filename, unsigned char defaultAlpha)
249
248
// do not signal floating point exceptions in devil library
250
249
ScopedDisableFpuExceptions fe;
251
250
252
- const bool success = !!ilLoadL (IL_TYPE_UNKNOWN, buffer, file.FileSize ());
251
+ const bool success = !!ilLoadL (IL_TYPE_UNKNOWN, buffer. data () , file.FileSize ());
253
252
254
253
// FPU control word has to be restored as well
255
254
streflop::streflop_init<streflop::Simple>();
256
255
257
256
ilDisable (IL_ORIGIN_SET);
258
- delete[] buffer;
259
257
260
258
if (success == false ) {
261
259
AllocDummy ();
@@ -300,12 +298,11 @@ bool CBitmap::LoadGrayscale(const std::string& filename)
300
298
channels = 1 ;
301
299
302
300
CFileHandler file (filename);
303
- if (!file.FileExists ()) {
301
+ if (!file.FileExists ())
304
302
return false ;
305
- }
306
303
307
- unsigned char * buffer = new unsigned char [ file.FileSize () + 1 ] ;
308
- file.Read (buffer, file.FileSize ());
304
+ std::vector< unsigned char > buffer ( file.FileSize () + 1 ) ;
305
+ file.Read (buffer. data () , file.FileSize ());
309
306
310
307
std::lock_guard<spring::mutex> lck (devilMutex);
311
308
ilOriginFunc (IL_ORIGIN_UPPER_LEFT);
@@ -315,13 +312,11 @@ bool CBitmap::LoadGrayscale(const std::string& filename)
315
312
ilGenImages (1 , &ImageName);
316
313
ilBindImage (ImageName);
317
314
318
- const bool success = !!ilLoadL (IL_TYPE_UNKNOWN, buffer, file.FileSize ());
315
+ const bool success = !!ilLoadL (IL_TYPE_UNKNOWN, buffer. data () , file.FileSize ());
319
316
ilDisable (IL_ORIGIN_SET);
320
- delete[] buffer;
321
317
322
- if (success == false ) {
318
+ if (! success)
323
319
return false ;
324
- }
325
320
326
321
ilConvertImage (IL_LUMINANCE, IL_UNSIGNED_BYTE);
327
322
xsize = ilGetInteger (IL_IMAGE_WIDTH);
@@ -350,7 +345,7 @@ bool CBitmap::Save(std::string const& filename, bool opaque, bool logged) const
350
345
if (mem.empty () || channels != 4 )
351
346
return false ;
352
347
353
- std::unique_ptr <unsigned char [] > buf (new unsigned char [ xsize * ysize * 4 ] );
348
+ std::vector <unsigned char > buf (xsize * ysize * 4 );
354
349
355
350
const int ymax = ysize - 1 ;
356
351
@@ -383,32 +378,39 @@ bool CBitmap::Save(std::string const& filename, bool opaque, bool logged) const
383
378
ilGenImages (1 , &ImageName); assert (ilGetError () == IL_NO_ERROR);
384
379
ilBindImage (ImageName); assert (ilGetError () == IL_NO_ERROR);
385
380
386
- ilTexImage (xsize, ysize, 1 , 4 , IL_RGBA, IL_UNSIGNED_BYTE, buf.get ()); assert (ilGetError () == IL_NO_ERROR);
381
+ ilTexImage (xsize, ysize, 1 , 4 , IL_RGBA, IL_UNSIGNED_BYTE, buf.data ()); assert (ilGetError () == IL_NO_ERROR);
382
+
383
+
384
+ // const std::string& imageExt = FileSystem::GetExtension(filename);
385
+ const std::string& fsFullPath = dataDirsAccess.LocateFile (filename, FileQueryFlags::WRITE);
386
+
387
+ // IL might be unicode-aware in which case it uses wchar_t{*}
388
+ const std::vector<ILchar> ilFullPath (fsFullPath.begin (), fsFullPath.end ());
387
389
388
- const std::string& fullPath = dataDirsAccess.LocateFile (filename, FileQueryFlags::WRITE);
389
- const std::string& imageExt = FileSystem::GetExtension (filename);
390
390
391
- bool success = ilSaveImage (fullPath. c_str ());
391
+ const bool success = ilSaveImage (ilFullPath. data ());
392
392
393
+ #if 0
393
394
if (!success) {
394
395
if (logged)
395
- LOG (" [CBitmap::%s] error 0x%x saving \" %s\" (ext. \" %s\" ) to \" %s\" " , __func__, ilGetError (), filename.c_str (), imageExt.c_str (), fullPath .c_str ());
396
+ LOG("[CBitmap::%s] error 0x%x saving \"%s\" (ext. \"%s\"; sizeof(ILchar)=%lu) to \"%s\"", __func__, ilGetError(), filename.c_str(), imageExt.c_str(), sizeof(ILchar), fsFullPath .c_str());
396
397
397
398
// manual fallbacks
398
399
switch (int(imageExt[0])) {
399
- case ' b' : case ' B' : { success = ilSave (IL_BMP, fullPath .c_str ()); } break ;
400
- case ' j' : case ' J' : { success = ilSave (IL_JPG, fullPath .c_str ()); } break ;
401
- case ' p' : case ' P' : { success = ilSave (IL_PNG, fullPath .c_str ()); } break ;
402
- case ' t' : case ' T' : { success = ilSave (IL_TGA, fullPath .c_str ()); } break ;
403
- case ' d' : case ' D' : { success = ilSave (IL_DDS, fullPath .c_str ()); } break ;
400
+ case 'b': case 'B': { success = ilSave(IL_BMP, fsFullPath .c_str()); } break;
401
+ case 'j': case 'J': { success = ilSave(IL_JPG, fsFullPath .c_str()); } break;
402
+ case 'p': case 'P': { success = ilSave(IL_PNG, fsFullPath .c_str()); } break;
403
+ case 't': case 'T': { success = ilSave(IL_TGA, fsFullPath .c_str()); } break;
404
+ case 'd': case 'D': { success = ilSave(IL_DDS, fsFullPath .c_str()); } break;
404
405
}
405
406
}
407
+ #endif
406
408
407
409
if (logged) {
408
410
if (success) {
409
- LOG (" [CBitmap::%s] saved \" %s\" to \" %s\" " , __func__, filename.c_str (), fullPath .c_str ());
411
+ LOG (" [CBitmap::%s] saved \" %s\" to \" %s\" " , __func__, filename.c_str (), fsFullPath .c_str ());
410
412
} else {
411
- LOG (" [CBitmap::%s] error 0x%x (re-)saving \" %s\" to \" %s\" " , __func__, ilGetError (), filename.c_str (), fullPath .c_str ());
413
+ LOG (" [CBitmap::%s] error 0x%x (re-)saving \" %s\" to \" %s\" " , __func__, ilGetError (), filename.c_str (), fsFullPath .c_str ());
412
414
}
413
415
}
414
416
@@ -427,14 +429,16 @@ bool CBitmap::SaveFloat(std::string const& filename) const
427
429
// seems IL_ORIGIN_SET only works in ilLoad and not in ilTexImage nor in ilSaveImage
428
430
// so we need to flip the image ourself
429
431
const float * memf = reinterpret_cast <const float *>(&mem[0 ]);
430
- typedef unsigned short uint16;
431
- std::unique_ptr<uint16[]> buf (new uint16[xsize * ysize]);
432
- const int ymax = (ysize - 1 );
432
+
433
+ std::vector<uint16_t > buf (xsize * ysize);
434
+
435
+ const int ymax = ysize - 1 ;
436
+
433
437
for (int y = 0 ; y < ysize; ++y) {
434
438
for (int x = 0 ; x < xsize; ++x) {
435
439
const int bi = x + (xsize * (ymax - y));
436
- const int mi = x + (xsize * (y));
437
- uint16 us = memf[mi] * 0xFFFF ; // convert float 0..1 to ushort
440
+ const int mi = x + (xsize * ( y));
441
+ uint16_t us = memf[mi] * 0xFFFF ; // convert float 0..1 to ushort
438
442
buf[bi] = us;
439
443
}
440
444
}
@@ -449,10 +453,16 @@ bool CBitmap::SaveFloat(std::string const& filename) const
449
453
450
454
// note: DevIL only generates a 16bit grayscale PNG when format is IL_UNSIGNED_SHORT!
451
455
// IL_FLOAT is converted to RGB with 8bit colordepth!
452
- ilTexImage (xsize, ysize, 1 , 1 , IL_LUMINANCE, IL_UNSIGNED_SHORT, buf.get ());
456
+ ilTexImage (xsize, ysize, 1 , 1 , IL_LUMINANCE, IL_UNSIGNED_SHORT, buf.data ());
453
457
454
- const std::string fullpath = dataDirsAccess.LocateFile (filename, FileQueryFlags::WRITE);
455
- const bool success = ilSaveImage (fullpath.c_str ());
458
+
459
+ // const std::string& imageExt = FileSystem::GetExtension(filename);
460
+ const std::string& fsFullPath = dataDirsAccess.LocateFile (filename, FileQueryFlags::WRITE);
461
+
462
+ const std::vector<ILchar> ilFullPath (fsFullPath.begin (), fsFullPath.end ());
463
+
464
+
465
+ const bool success = ilSaveImage (ilFullPath.data ());
456
466
457
467
ilDeleteImages (1 , &ImageName);
458
468
return success;
@@ -785,16 +795,15 @@ SDL_Surface* CBitmap::CreateSDLSurface() const
785
795
return surface;
786
796
}
787
797
788
- unsigned char * surfData = new unsigned char [ xsize * ysize * channels] ;
789
- memcpy (surfData, &mem[0 ], xsize * ysize * channels);
798
+ std::vector< unsigned char > surfData ( xsize * ysize * channels) ;
799
+ memcpy (surfData. data () , &mem[0 ], xsize * ysize * channels);
790
800
791
801
792
802
// This will only work with 24bit RGB and 32bit RGBA pictures
793
- surface = SDL_CreateRGBSurfaceFrom (surfData, xsize, ysize, 8 * channels, xsize * channels, 0x000000FF , 0x0000FF00 , 0x00FF0000 , (channels == 4 ) ? 0xFF000000 : 0 );
794
- if (surface == nullptr ) {
803
+ surface = SDL_CreateRGBSurfaceFrom (surfData.data (), xsize, ysize, 8 * channels, xsize * channels, 0x000000FF , 0x0000FF00 , 0x00FF0000 , (channels == 4 ) ? 0xFF000000 : 0 );
804
+
805
+ if (surface == nullptr )
795
806
LOG_L (L_WARNING, " CBitmap::CreateSDLSurface Failed!" );
796
- delete[] surfData;
797
- }
798
807
799
808
return surface;
800
809
}
@@ -942,16 +951,18 @@ void CBitmap::Tint(const float tint[3])
942
951
943
952
void CBitmap::ReverseYAxis ()
944
953
{
945
- if (compressed) return ; // don't try to flip DDS
946
- unsigned char * tmpLine = new unsigned char [channels * xsize];
947
- for (int y=0 ; y < (ysize / 2 ); ++y) {
954
+ if (compressed)
955
+ return ; // don't try to flip DDS
956
+
957
+ std::vector<unsigned char > tmpLine (channels * xsize);
958
+
959
+ for (int y = 0 ; y < (ysize / 2 ); ++y) {
948
960
const int pixelLow = (((y ) * xsize) + 0 ) * channels;
949
961
const int pixelHigh = (((ysize - 1 - y) * xsize) + 0 ) * channels;
950
962
951
963
// copy the whole line
952
- std::copy (mem.begin () + pixelHigh, mem.begin () + pixelHigh + channels * xsize, tmpLine);
953
- std::copy (mem.begin () + pixelLow, mem.begin () + pixelLow + channels * xsize, mem.begin () + pixelHigh);
954
- std::copy (tmpLine, tmpLine + channels * xsize, mem.begin () + pixelLow);
964
+ std::copy (mem.begin () + pixelHigh, mem.begin () + pixelHigh + channels * xsize, tmpLine. data () );
965
+ std::copy (mem.begin () + pixelLow , mem.begin () + pixelLow + channels * xsize, mem.begin () + pixelHigh);
966
+ std::copy (tmpLine. data () , tmpLine. data () + channels * xsize, mem.begin () + pixelLow);
955
967
}
956
- delete[] tmpLine;
957
968
}
0 commit comments