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 331 : MFXImageHelper::checkSupported(FXString ext) {
39 331 : if (comparecase(ext, "png") == 0) {
40 316 : if (!FXPNGImage::supported) {
41 0 : throw InvalidArgument("Fox was compiled without png support!");
42 : }
43 15 : } 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 9 : } 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 331 : }
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 7 : img = new FXGIFImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
62 22 : } else if (comparecase(ext, "bmp") == 0) {
63 0 : img = new FXBMPImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
64 22 : } else if (comparecase(ext, "xpm") == 0) {
65 0 : img = new FXXPMImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
66 22 : } else if (comparecase(ext, "pcx") == 0) {
67 0 : img = new FXPCXImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
68 22 : } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
69 0 : img = new FXICOImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
70 22 : } else if (comparecase(ext, "tga") == 0) {
71 0 : img = new FXTGAImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
72 22 : } else if (comparecase(ext, "rgb") == 0) {
73 0 : img = new FXRGBImage(a, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
74 22 : } else if (comparecase(ext, "xbm") == 0) {
75 0 : img = new FXXBMImage(a, nullptr, nullptr, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
76 22 : } else if (comparecase(ext, "png") == 0) {
77 14 : 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 28 : a->beginWaitCursor();
89 28 : img->loadPixels(stream);
90 28 : stream.close();
91 :
92 28 : img->create();
93 28 : a->endWaitCursor();
94 : } else {
95 1 : delete img;
96 2 : throw InvalidArgument("Loading failed!");
97 : }
98 28 : return img;
99 30 : }
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 302 : MFXImageHelper::saveImage(const std::string& file,
136 : int width, int height, FXColor* data) {
137 302 : FXString ext = FXPath::extension(file.c_str());
138 302 : checkSupported(ext);
139 302 : FXFileStream stream;
140 302 : if (!stream.open(file.c_str(), FXStreamSave)) {
141 0 : throw InvalidArgument("Could not open file for writing!");
142 : }
143 302 : if (comparecase(ext, "gif") == 0) {
144 0 : return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
145 302 : } else if (comparecase(ext, "bmp") == 0) {
146 0 : return fxsaveBMP(stream, data, width, height);
147 302 : } else if (comparecase(ext, "xpm") == 0) {
148 0 : return fxsaveXPM(stream, data, width, height);
149 302 : } else if (comparecase(ext, "pcx") == 0) {
150 0 : return fxsavePCX(stream, data, width, height);
151 302 : } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
152 0 : return fxsaveICO(stream, data, width, height);
153 302 : } else if (comparecase(ext, "tga") == 0) {
154 0 : return fxsaveTGA(stream, data, width, height);
155 302 : } else if (comparecase(ext, "rgb") == 0) {
156 0 : return fxsaveRGB(stream, data, width, height);
157 302 : } else if (comparecase(ext, "xbm") == 0) {
158 0 : return fxsaveXBM(stream, data, width, height);
159 302 : } else if (comparecase(ext, "png") == 0) {
160 302 : return fxsavePNG(stream, data, width, height);
161 0 : } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
162 0 : return fxsaveJPG(stream, data, width, height, 75);
163 0 : } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
164 0 : return fxsaveTIF(stream, data, width, height, 0);
165 : }
166 0 : throw InvalidArgument("Unknown file extension for image!");
167 302 : }
168 :
169 :
170 : /****************************************************************************/
|