LCOV - code coverage report
Current view: top level - src/utils/foxtools - MFXImageHelper.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 75.6 % 90 68
Test Date: 2025-05-17 15:32:44 Functions: 100.0 % 4 4

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2005-2025 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    MFXImageHelper.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @date    2005-05-04
      17              : ///
      18              : // missing_desc
      19              : /****************************************************************************/
      20              : #include <config.h>
      21              : 
      22              : #include "fxheader.h"
      23              : #include <FXPNGImage.h>
      24              : #include <FXJPGImage.h>
      25              : #ifdef _MSC_VER
      26              : #pragma warning(push)
      27              : #pragma warning(disable: 4244 4242) // do not warn about integer conversions
      28              : #endif
      29              : #include <FXTIFImage.h>
      30              : #ifdef _MSC_VER
      31              : #pragma warning(pop)
      32              : #endif
      33              : #include <utils/common/ToString.h>
      34              : #include "MFXImageHelper.h"
      35              : 
      36              : 
      37              : void
      38          338 : MFXImageHelper::checkSupported(FXString ext) {
      39          338 :     if (comparecase(ext, "png") == 0) {
      40          320 :         if (!FXPNGImage::supported) {
      41            0 :             throw InvalidArgument("Fox was compiled without png support!");
      42              :         }
      43           18 :     } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
      44            6 :         if (!FXJPGImage::supported) {
      45            0 :             throw InvalidArgument("Fox was compiled without jpg support!");
      46              :         }
      47           12 :     } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
      48            2 :         if (!FXTIFImage::supported) {
      49            0 :             throw InvalidArgument("Fox was compiled without tif support!");
      50              :         }
      51              :     }
      52          338 : }
      53              : 
      54              : 
      55              : FXImage*
      56           30 : MFXImageHelper::loadImage(FXApp* a, const std::string& file) {
      57           30 :     FXString ext = FXPath::extension(file.c_str());
      58           30 :     checkSupported(ext);
      59              :     FXImage* img = nullptr;
      60           30 :     if (comparecase(ext, "gif") == 0) {
      61            7 :         img = new FXGIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      62           23 :     } else if (comparecase(ext, "bmp") == 0) {
      63            0 :         img = new FXBMPImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      64           23 :     } else if (comparecase(ext, "xpm") == 0) {
      65            0 :         img = new FXXPMImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      66           23 :     } else if (comparecase(ext, "pcx") == 0) {
      67            0 :         img = new FXPCXImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      68           23 :     } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
      69            0 :         img = new FXICOImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      70           23 :     } else if (comparecase(ext, "tga") == 0) {
      71            0 :         img = new FXTGAImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      72           23 :     } else if (comparecase(ext, "rgb") == 0) {
      73            0 :         img = new FXRGBImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      74           23 :     } else if (comparecase(ext, "xbm") == 0) {
      75            0 :         img = new FXXBMImage(a, nullptr, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      76           23 :     } else if (comparecase(ext, "png") == 0) {
      77           15 :         img = new FXPNGImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      78            8 :     } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
      79            6 :         img = new FXJPGImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      80            2 :     } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
      81            2 :         img = new FXTIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
      82              :     } else {
      83            0 :         throw InvalidArgument("Unknown file extension '" + toString(ext.text()) + "' for image '" + file + "'!");
      84              :     }
      85              : 
      86           30 :     FXFileStream stream;
      87           30 :     if (img != nullptr && stream.open(file.c_str(), FXStreamLoad)) {
      88           28 :         a->beginWaitCursor();
      89           28 :         img->loadPixels(stream);
      90           28 :         stream.close();
      91              : 
      92           28 :         img->create();
      93           28 :         a->endWaitCursor();
      94              :     } else {
      95            2 :         delete img;
      96            4 :         throw InvalidArgument("Loading failed!");
      97              :     }
      98           28 :     return img;
      99           32 : }
     100              : 
     101              : 
     102              : FXbool
     103           28 : MFXImageHelper::scalePower2(FXImage* image, const int maxSize) {
     104              :     FXint newHeight = 0;
     105          682 :     for (FXint exp = 30; exp >= 0; exp--) {
     106          682 :         newHeight = 2 << exp;
     107          682 :         if (newHeight <= maxSize && (image->getHeight() & newHeight)) {
     108              :             break;
     109              :         }
     110              :     }
     111           28 :     if (2 * newHeight <= maxSize && (2 * newHeight - image->getHeight() < image->getHeight() - newHeight)) {
     112              :         newHeight *= 2;
     113              :     }
     114              :     FXint newWidth = 0;
     115          686 :     for (FXint exp = 30; exp >= 0; exp--) {
     116          686 :         newWidth = 2 << exp;
     117          686 :         if (newWidth <= maxSize && (image->getWidth() & newWidth)) {
     118              :             break;
     119              :         }
     120              :     }
     121           28 :     if (2 * newWidth <= maxSize && (2 * newWidth - image->getWidth() < image->getWidth() - newWidth)) {
     122              :         newWidth *= 2;
     123              :     }
     124           28 :     if (newHeight == image->getHeight() && newWidth == image->getWidth()) {
     125              :         return false;
     126              :     }
     127           15 :     image->scale(newWidth, newHeight);
     128           15 :     return true;
     129              : }
     130              : 
     131              : 
     132              : // smell: yellow (the save functions may have additional options, not regarded)
     133              : // Save file
     134              : FXbool
     135          308 : MFXImageHelper::saveImage(const std::string& file,
     136              :                           int width, int height, FXColor* data) {
     137          308 :     FXString ext = FXPath::extension(file.c_str());
     138          308 :     checkSupported(ext);
     139          308 :     FXFileStream stream;
     140          308 :     if (!stream.open(file.c_str(), FXStreamSave)) {
     141            0 :         throw InvalidArgument("Could not open file for writing!");
     142              :     }
     143          308 :     if (comparecase(ext, "gif") == 0) {
     144            0 :         return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
     145          308 :     } else if (comparecase(ext, "bmp") == 0) {
     146            0 :         return fxsaveBMP(stream, data, width, height);
     147          308 :     } else if (comparecase(ext, "xpm") == 0) {
     148            0 :         return fxsaveXPM(stream, data, width, height);
     149          308 :     } else if (comparecase(ext, "pcx") == 0) {
     150            0 :         return fxsavePCX(stream, data, width, height);
     151          308 :     } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
     152            0 :         return fxsaveICO(stream, data, width, height);
     153          308 :     } else if (comparecase(ext, "tga") == 0) {
     154            0 :         return fxsaveTGA(stream, data, width, height);
     155          308 :     } else if (comparecase(ext, "rgb") == 0) {
     156            0 :         return fxsaveRGB(stream, data, width, height);
     157          308 :     } else if (comparecase(ext, "xbm") == 0) {
     158            0 :         return fxsaveXBM(stream, data, width, height);
     159          308 :     } else if (comparecase(ext, "png") == 0) {
     160          305 :         return fxsavePNG(stream, data, width, height);
     161            3 :     } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
     162            0 :         return fxsaveJPG(stream, data, width, height, 75);
     163            3 :     } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
     164            0 :         return fxsaveTIF(stream, data, width, height, 0);
     165              :     }
     166            6 :     throw InvalidArgument("Unknown file extension for image!");
     167          311 : }
     168              : 
     169              : 
     170              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1