Skip to content

Commit 59ad680

Browse files
authored
More fs::path inside core (#7786)
#changelog #filesystem
1 parent 66fa605 commit 59ad680

File tree

9 files changed

+163
-141
lines changed

9 files changed

+163
-141
lines changed

libs/openFrameworks/graphics/ofCairoRenderer.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void ofCairoRenderer::setup(const of::filesystem::path & _filename, Type _type,
5656
}
5757
}
5858

59-
if (filename != "") {
59+
if (!filename.empty()) {
6060
switch (type) {
6161
case PDF:
6262
case SVG:
@@ -69,18 +69,16 @@ void ofCairoRenderer::setup(const of::filesystem::path & _filename, Type _type,
6969

7070
switch (type) {
7171
case PDF:
72-
if (filename == "") {
72+
if (filename.empty()) {
7373
surface = cairo_pdf_surface_create_for_stream(&ofCairoRenderer::stream_function, this, outputsize.width, outputsize.height);
7474
} else {
75-
// FIXME: Future - once ofToDataPath returns fs::path, remove c_str()
7675
surface = cairo_pdf_surface_create(ofToDataPath(filename).c_str(), outputsize.width, outputsize.height);
7776
}
7877
break;
7978
case SVG:
80-
if (filename == "") {
79+
if (filename.empty()) {
8180
surface = cairo_svg_surface_create_for_stream(&ofCairoRenderer::stream_function, this, outputsize.width, outputsize.height);
8281
} else {
83-
// FIXME: Future - once ofToDataPath returns fs::path, remove c_str()
8482
surface = cairo_svg_surface_create(ofToDataPath(filename).c_str(), outputsize.width, outputsize.height);
8583
}
8684
break;
@@ -121,7 +119,7 @@ void ofCairoRenderer::flush() {
121119
void ofCairoRenderer::close() {
122120
if (surface) {
123121
cairo_surface_flush(surface);
124-
if (type == IMAGE && filename != "") {
122+
if (type == IMAGE && !filename.empty()) {
125123
ofSaveImage(imageBuffer, filename);
126124
}
127125
cairo_surface_finish(surface);
@@ -1376,7 +1374,7 @@ ofPixels & ofCairoRenderer::getImageSurfacePixels() {
13761374
}
13771375

13781376
ofBuffer & ofCairoRenderer::getContentBuffer() {
1379-
if (filename != "" || (type != SVG && type != PDF)) {
1377+
if (!filename.empty() || (type != SVG && type != PDF)) {
13801378
ofLogError("ofCairoRenderer") << "getContentBuffer(): can only get buffer from memory allocated renderer for svg or pdf";
13811379
}
13821380
return streamBuffer;

libs/openFrameworks/graphics/ofImage.cpp

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static int getJpegOptionFromImageLoadSetting(const ofImageLoadSettings &settings
196196
}
197197

198198
template<typename PixelType>
199-
static bool loadImage(ofPixels_<PixelType> & pix, const of::filesystem::path& _fileName, const ofImageLoadSettings& settings){
199+
static bool loadImage(ofPixels_<PixelType> & pix, const of::filesystem::path & _fileName, const ofImageLoadSettings & settings){
200200
ofInitFreeImage();
201201

202202
auto uriStr = _fileName.string();
@@ -225,23 +225,34 @@ static bool loadImage(ofPixels_<PixelType> & pix, const of::filesystem::path& _f
225225
return ofLoadImage(pix, ofLoadURL(_fileName.string()).data);
226226
}
227227

228-
auto fileName = ofToDataPath(_fileName, true);
228+
auto fileName = ofToDataPathFS(_fileName, true);
229229
bool bLoaded = false;
230230
FIBITMAP * bmp = nullptr;
231231

232232
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
233+
#ifdef OF_OS_WINDOWS
234+
fif = FreeImage_GetFileTypeU(fileName.c_str(), 0);
235+
#else
233236
fif = FreeImage_GetFileType(fileName.c_str(), 0);
237+
#endif
234238
if(fif == FIF_UNKNOWN) {
235239
// or guess via filename
236-
fif = FreeImage_GetFIFFromFilename(fileName.c_str());
240+
#ifdef OF_OS_WINDOWS
241+
fif = FreeImage_GetFIFFromFilenameU(_fileName.extension().c_str());
242+
#else
243+
fif = FreeImage_GetFIFFromFilename(_fileName.extension().c_str());
244+
#endif
237245
}
238246
if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
247+
int option = 0;
239248
if(fif == FIF_JPEG) {
240-
int option = getJpegOptionFromImageLoadSetting(settings);
241-
bmp = FreeImage_Load(fif, fileName.c_str(), option | settings.freeImageFlags);
242-
} else {
243-
bmp = FreeImage_Load(fif, fileName.c_str(), 0 | settings.freeImageFlags);
249+
option = getJpegOptionFromImageLoadSetting(settings);
244250
}
251+
#ifdef OF_OS_WINDOWS
252+
bmp = FreeImage_LoadU(fif, fileName.c_str(), option | settings.freeImageFlags);
253+
#else
254+
bmp = FreeImage_Load(fif, fileName.c_str(), option | settings.freeImageFlags);
255+
#endif
245256

246257
if (bmp != nullptr){
247258
bLoaded = true;
@@ -284,12 +295,11 @@ static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer, const
284295

285296

286297
//make the image!!
298+
int option = 0;
287299
if(fif == FIF_JPEG) {
288-
int option = getJpegOptionFromImageLoadSetting(settings);
289-
bmp = FreeImage_LoadFromMemory(fif, hmem, option | settings.freeImageFlags);
290-
} else {
291-
bmp = FreeImage_LoadFromMemory(fif, hmem, settings.freeImageFlags);
300+
option = getJpegOptionFromImageLoadSetting(settings);
292301
}
302+
bmp = FreeImage_LoadFromMemory(fif, hmem, option | settings.freeImageFlags);
293303

294304
if( bmp != nullptr ){
295305
bLoaded = true;
@@ -313,37 +323,37 @@ static bool loadImage(ofPixels_<PixelType> & pix, const ofBuffer & buffer, const
313323
}
314324

315325
//----------------------------------------------------------------
316-
bool ofLoadImage(ofPixels & pix, const of::filesystem::path& path, const ofImageLoadSettings &settings) {
326+
bool ofLoadImage(ofPixels & pix, const of::filesystem::path & path, const ofImageLoadSettings & settings) {
317327
return loadImage(pix, path, settings);
318328
}
319329

320330
//----------------------------------------------------------------
321-
bool ofLoadImage(ofPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings) {
331+
bool ofLoadImage(ofPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings & settings) {
322332
return loadImage(pix, buffer, settings);
323333
}
324334

325335
//----------------------------------------------------------------
326-
bool ofLoadImage(ofShortPixels & pix, const of::filesystem::path& path, const ofImageLoadSettings &settings) {
336+
bool ofLoadImage(ofShortPixels & pix, const of::filesystem::path & path, const ofImageLoadSettings & settings) {
327337
return loadImage(pix, path, settings);
328338
}
329339

330340
//----------------------------------------------------------------
331-
bool ofLoadImage(ofShortPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings) {
341+
bool ofLoadImage(ofShortPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings & settings) {
332342
return loadImage(pix, buffer, settings);
333343
}
334344

335345
//----------------------------------------------------------------
336-
bool ofLoadImage(ofFloatPixels & pix, const of::filesystem::path& path, const ofImageLoadSettings &settings) {
346+
bool ofLoadImage(ofFloatPixels & pix, const of::filesystem::path & path, const ofImageLoadSettings & settings) {
337347
return loadImage(pix, path, settings);
338348
}
339349

340350
//----------------------------------------------------------------
341-
bool ofLoadImage(ofFloatPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings &settings) {
351+
bool ofLoadImage(ofFloatPixels & pix, const ofBuffer & buffer, const ofImageLoadSettings & settings) {
342352
return loadImage(pix, buffer, settings);
343353
}
344354

345355
//----------------------------------------------------------------
346-
bool ofLoadImage(ofTexture & tex, const of::filesystem::path& path, const ofImageLoadSettings &settings ) {
356+
bool ofLoadImage(ofTexture & tex, const of::filesystem::path & path, const ofImageLoadSettings & settings ) {
347357
return ofLoadImage( tex, path, false, settings );
348358
}
349359

@@ -404,20 +414,28 @@ static bool saveImage(const ofPixels_<PixelType> & _pix, const of::filesystem::p
404414
}
405415

406416
ofFilePath::createEnclosingDirectory(_fileName);
407-
auto fileName = ofToDataPath(_fileName);
417+
auto fileName = ofToDataPathFS(_fileName);
408418
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
419+
#ifdef OF_OS_WINDOWS
420+
fif = FreeImage_GetFileTypeU(fileName.c_str(), 0);
421+
#else
409422
fif = FreeImage_GetFileType(fileName.c_str(), 0);
423+
#endif
410424
if(fif == FIF_UNKNOWN) {
411425
// or guess via filename
412-
fif = FreeImage_GetFIFFromFilename(fileName.c_str());
426+
#ifdef OF_OS_WINDOWS
427+
fif = FreeImage_GetFIFFromFilenameU(_fileName.extension().c_str());
428+
#else
429+
fif = FreeImage_GetFIFFromFilename(_fileName.extension().c_str());
430+
#endif
413431
}
414432
if(fif==FIF_JPEG && (_pix.getNumChannels()==4 || _pix.getBitsPerChannel() > 8)){
415433
ofPixels pix3 = _pix;
416434
if( pix3.getPixelFormat() == OF_PIXELS_BGRA ){
417435
pix3.swapRgb();
418436
}
419437
pix3.setNumChannels(3);
420-
return saveImage(pix3,_fileName,qualityLevel);
438+
return saveImage(pix3, _fileName, qualityLevel);
421439
}
422440

423441
FIBITMAP * bmp = nullptr;
@@ -447,11 +465,15 @@ static bool saveImage(const ofPixels_<PixelType> & _pix, const of::filesystem::p
447465
case OF_IMAGE_QUALITY_HIGH: quality = JPEG_QUALITYGOOD; break;
448466
case OF_IMAGE_QUALITY_BEST: quality = JPEG_QUALITYSUPERB; break;
449467
}
468+
#ifdef OF_OS_WINDOWS
469+
retValue = FreeImage_SaveU(fif, bmp, fileName.c_str(), quality);
470+
#else
450471
retValue = FreeImage_Save(fif, bmp, fileName.c_str(), quality);
472+
#endif
451473
} else {
452474
if(qualityLevel != OF_IMAGE_QUALITY_BEST) {
453475
ofLogWarning("ofImage") << "saveImage(): ofImageCompressionType only applies to JPEGs,"
454-
<< " ignoring value for \" "<< fileName << "\"";
476+
<< " ignoring value for "<< _fileName;
455477
}
456478

457479
if (fif == FIF_GIF) {
@@ -463,12 +485,20 @@ static bool saveImage(const ofPixels_<PixelType> & _pix, const of::filesystem::p
463485
// this will create a 256-color palette from the image
464486
convertedBmp = FreeImage_ColorQuantize(bmp, FIQ_NNQUANT);
465487
}
488+
#ifdef OF_OS_WINDOWS
489+
retValue = FreeImage_SaveU(fif, convertedBmp, fileName.c_str());
490+
#else
466491
retValue = FreeImage_Save(fif, convertedBmp, fileName.c_str());
492+
#endif
467493
if (convertedBmp != nullptr){
468494
FreeImage_Unload(convertedBmp);
469495
}
470496
} else {
497+
#ifdef OF_OS_WINDOWS
498+
retValue = FreeImage_SaveU(fif, bmp, fileName.c_str());
499+
#else
471500
retValue = FreeImage_Save(fif, bmp, fileName.c_str());
501+
#endif
472502
}
473503
}
474504
}
@@ -746,14 +776,14 @@ bool ofImage_<PixelType>::loadImage(const ofFile & file){
746776

747777
//----------------------------------------------------------
748778
template<typename PixelType>
749-
bool ofImage_<PixelType>::load(const of::filesystem::path& fileName, const ofImageLoadSettings &settings){
779+
bool ofImage_<PixelType>::load(const of::filesystem::path & fileName, const ofImageLoadSettings & settings){
750780
#if defined(TARGET_ANDROID)
751781
ofAddListener(ofxAndroidEvents().unloadGL,this,&ofImage_<PixelType>::unloadTexture);
752782
ofAddListener(ofxAndroidEvents().reloadGL,this,&ofImage_<PixelType>::update);
753783
#endif
754784
bool bLoadedOk = ofLoadImage(pixels, fileName, settings);
755785
if (!bLoadedOk) {
756-
ofLogError("ofImage") << "loadImage(): couldn't load image from " << fileName << "";
786+
ofLogError("ofImage") << "loadImage(): couldn't load image from " << fileName;
757787
clear();
758788
return false;
759789
}

libs/openFrameworks/graphics/ofTrueTypeFont.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ static string osxFontPathByName(const string& fontname){
253253
#ifdef TARGET_WIN32
254254
#include <unordered_map>
255255
// font font face -> file name name mapping
256+
// FIXME: second -> fs::path
256257
static std::unordered_map<string, string> fonts_table;
257258
// read font linking information from registry, and store in std::map
258259
//------------------------------------------------------------------
@@ -327,14 +328,14 @@ void initWindows(){
327328
}
328329

329330

330-
static string winFontPathByName(const string& fontname ){
331+
static string winFontPathByName(const string & fontname){
331332
return fonts_table[fontname];
332333
}
333334
#endif
334335

335336
#ifdef TARGET_LINUX
336337
//------------------------------------------------------------------
337-
static string linuxFontPathByName(const string& fontname){
338+
static string linuxFontPathByName(const string & fontname){
338339
string filename;
339340
FcPattern * pattern = FcNameParse((const FcChar8*)fontname.c_str());
340341
FcBool ret = FcConfigSubstitute(0,pattern,FcMatchPattern);
@@ -369,15 +370,15 @@ static string linuxFontPathByName(const string& fontname){
369370
#endif
370371

371372
//-----------------------------------------------------------
373+
// FIXME: it makes no sense to have _fontname and filename if filename will be rewritten inside this function
372374
static bool loadFontFace(const of::filesystem::path& _fontname, FT_Face & face, of::filesystem::path & filename, int index){
373-
of::filesystem::path fontname = _fontname;
374-
filename = ofToDataPath(_fontname,true);
375-
ofFile fontFile(filename,ofFile::Reference);
375+
auto fontname = _fontname;
376+
filename = ofToDataPathFS(_fontname,true);
376377
int fontID = index;
377-
if(!fontFile.exists()){
378+
if(!of::filesystem::exists(filename)){
378379
#ifdef TARGET_LINUX
379-
// FIXME: update function linuxFontPathByName to use path instead of string
380-
filename = linuxFontPathByName(fontname.string());
380+
// FIXME: fs::path in input and output
381+
filename = linuxFontPathByName(_fontname.string());
381382
#elif defined(TARGET_OSX)
382383
if(fontname==OF_TTF_SANS){
383384
fontname = "Helvetica Neue";
@@ -391,6 +392,7 @@ static bool loadFontFace(const of::filesystem::path& _fontname, FT_Face & face,
391392
}else if(fontname==OF_TTF_MONO){
392393
fontname = "Menlo Regular";
393394
}
395+
// FIXME: fs::path in input and output
394396
filename = osxFontPathByName(fontname.string());
395397
#elif defined(TARGET_WIN32)
396398
if(fontname==OF_TTF_SANS){
@@ -400,13 +402,14 @@ static bool loadFontFace(const of::filesystem::path& _fontname, FT_Face & face,
400402
}else if(fontname==OF_TTF_MONO){
401403
fontname = "Courier New";
402404
}
405+
// FIXME: fs::path in input and output
403406
filename = winFontPathByName(fontname.string());
404407
#endif
405408
if(filename == "" ){
406409
ofLogError("ofTrueTypeFont") << "loadFontFace(): couldn't find font " << fontname;
407410
return false;
408411
}
409-
ofLogVerbose("ofTrueTypeFont") << "loadFontFace(): " << fontname << " not a file in data loading system font from \"" << filename << "\"";
412+
ofLogVerbose("ofTrueTypeFont") << "loadFontFace(): " << fontname << " not a file in data loading system font from " << filename;
410413
}
411414
FT_Error err;
412415
err = FT_New_Face( library, filename.string().c_str(), fontID, &face );

0 commit comments

Comments
 (0)