LCOV - code coverage report
Current view: top level - src/utils/foxtools - MFXImageHelper.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 68 90 75.6 %
Date: 2024-04-27 15:34:54 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2005-2024 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         439 : MFXImageHelper::checkSupported(FXString ext) {
      39         439 :     if (comparecase(ext, "png") == 0) {
      40         421 :         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         439 : }
      53             : 
      54             : 
      55             : FXImage*
      56          29 : MFXImageHelper::loadImage(FXApp* a, const std::string& file) {
      57          29 :     FXString ext = FXPath::extension(file.c_str());
      58          29 :     checkSupported(ext);
      59             :     FXImage* img = nullptr;
      60          29 :     if (comparecase(ext, "gif") == 0) {
      61           6 :         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          29 :     FXFileStream stream;
      87          29 :     if (img != nullptr && stream.open(file.c_str(), FXStreamLoad)) {
      88          27 :         a->beginWaitCursor();
      89          27 :         img->loadPixels(stream);
      90          27 :         stream.close();
      91             : 
      92          27 :         img->create();
      93          27 :         a->endWaitCursor();
      94             :     } else {
      95           2 :         delete img;
      96           4 :         throw InvalidArgument("Loading failed!");
      97             :     }
      98          27 :     return img;
      99          31 : }
     100             : 
     101             : 
     102             : FXbool
     103          27 : MFXImageHelper::scalePower2(FXImage* image, const int maxSize) {
     104             :     FXint newHeight = 0;
     105         656 :     for (FXint exp = 30; exp >= 0; exp--) {
     106         656 :         newHeight = 2 << exp;
     107         656 :         if (newHeight <= maxSize && (image->getHeight() & newHeight)) {
     108             :             break;
     109             :         }
     110             :     }
     111          27 :     if (2 * newHeight <= maxSize && (2 * newHeight - image->getHeight() < image->getHeight() - newHeight)) {
     112             :         newHeight *= 2;
     113             :     }
     114             :     FXint newWidth = 0;
     115         660 :     for (FXint exp = 30; exp >= 0; exp--) {
     116         660 :         newWidth = 2 << exp;
     117         660 :         if (newWidth <= maxSize && (image->getWidth() & newWidth)) {
     118             :             break;
     119             :         }
     120             :     }
     121          27 :     if (2 * newWidth <= maxSize && (2 * newWidth - image->getWidth() < image->getWidth() - newWidth)) {
     122             :         newWidth *= 2;
     123             :     }
     124          27 :     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         410 : MFXImageHelper::saveImage(const std::string& file,
     136             :                           int width, int height, FXColor* data) {
     137         410 :     FXString ext = FXPath::extension(file.c_str());
     138         410 :     checkSupported(ext);
     139         410 :     FXFileStream stream;
     140         410 :     if (!stream.open(file.c_str(), FXStreamSave)) {
     141           0 :         throw InvalidArgument("Could not open file for writing!");
     142             :     }
     143         410 :     if (comparecase(ext, "gif") == 0) {
     144           0 :         return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
     145         410 :     } else if (comparecase(ext, "bmp") == 0) {
     146           0 :         return fxsaveBMP(stream, data, width, height);
     147         410 :     } else if (comparecase(ext, "xpm") == 0) {
     148           0 :         return fxsaveXPM(stream, data, width, height);
     149         410 :     } else if (comparecase(ext, "pcx") == 0) {
     150           0 :         return fxsavePCX(stream, data, width, height);
     151         410 :     } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
     152           0 :         return fxsaveICO(stream, data, width, height);
     153         410 :     } else if (comparecase(ext, "tga") == 0) {
     154           0 :         return fxsaveTGA(stream, data, width, height);
     155         410 :     } else if (comparecase(ext, "rgb") == 0) {
     156           0 :         return fxsaveRGB(stream, data, width, height);
     157         410 :     } else if (comparecase(ext, "xbm") == 0) {
     158           0 :         return fxsaveXBM(stream, data, width, height);
     159         410 :     } else if (comparecase(ext, "png") == 0) {
     160         406 :         return fxsavePNG(stream, data, width, height);
     161           4 :     } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
     162           0 :         return fxsaveJPG(stream, data, width, height, 75);
     163           4 :     } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
     164           0 :         return fxsaveTIF(stream, data, width, height, 0);
     165             :     }
     166           8 :     throw InvalidArgument("Unknown file extension for image!");
     167         414 : }
     168             : 
     169             : 
     170             : /****************************************************************************/

Generated by: LCOV version 1.14