SUMO - Simulation of Urban MObility
MFXImageHelper.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // missing_desc
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2005-2016 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <fx.h>
33 #include <FXPNGImage.h>
34 #include <FXJPGImage.h>
35 #include <FXTIFImage.h>
36 #include <utils/common/ToString.h>
37 #include "MFXImageHelper.h"
38 
39 #include <cassert>
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 void
47  if (comparecase(ext, "png") == 0) {
48  if (!FXPNGImage::supported) {
49  throw InvalidArgument("Fox was compiled without png support!");
50  }
51  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
52  if (!FXJPGImage::supported) {
53  throw InvalidArgument("Fox was compiled without jpg support!");
54  }
55  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
56  if (!FXTIFImage::supported) {
57  throw InvalidArgument("Fox was compiled without tif support!");
58  }
59  }
60 }
61 
62 
63 FXImage*
64 MFXImageHelper::loadImage(FXApp* a, const std::string& file) {
65  FXString ext = FXPath::extension(file.c_str());
66  checkSupported(ext);
67  FXImage* img = NULL;
68  if (comparecase(ext, "gif") == 0) {
69  img = new FXGIFImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
70  } else if (comparecase(ext, "bmp") == 0) {
71  img = new FXBMPImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
72  } else if (comparecase(ext, "xpm") == 0) {
73  img = new FXXPMImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
74  } else if (comparecase(ext, "pcx") == 0) {
75  img = new FXPCXImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
76  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
77  img = new FXICOImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
78  } else if (comparecase(ext, "tga") == 0) {
79  img = new FXTGAImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
80  } else if (comparecase(ext, "rgb") == 0) {
81  img = new FXRGBImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
82  } else if (comparecase(ext, "xbm") == 0) {
83  img = new FXXBMImage(a, NULL, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
84  } else if (comparecase(ext, "png") == 0) {
85  img = new FXPNGImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
86  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
87  img = new FXJPGImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
88  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
89  img = new FXTIFImage(a, NULL, IMAGE_KEEP | IMAGE_SHMI | IMAGE_SHMP);
90  } else {
91  throw InvalidArgument("Unknown file extension '" + toString(ext.text()) + "' for image '" + file + "'!");
92  }
93 
94  FXFileStream stream;
95  if (img != NULL && stream.open(file.c_str(), FXStreamLoad)) {
96  a->beginWaitCursor();
97  img->loadPixels(stream);
98  stream.close();
99 
100  img->create();
101  a->endWaitCursor();
102  } else {
103  delete img;
104  throw InvalidArgument("Loading failed!");
105  }
106  return img;
107 }
108 
109 
110 FXbool
111 MFXImageHelper::scalePower2(FXImage* image, const int maxSize) {
112  FXint newHeight = 0;
113  for (FXint exp = 30; exp >= 0; exp--) {
114  newHeight = 2 << exp;
115  if (newHeight <= maxSize && (image->getHeight() & newHeight)) {
116  break;
117  }
118  }
119  if (2 * newHeight <= maxSize && (2 * newHeight - image->getHeight() < image->getHeight() - newHeight)) {
120  newHeight *= 2;
121  }
122  FXint newWidth = 0;
123  for (FXint exp = 30; exp >= 0; exp--) {
124  newWidth = 2 << exp;
125  if (newWidth <= maxSize && (image->getWidth() & newWidth)) {
126  break;
127  }
128  }
129  if (2 * newWidth <= maxSize && (2 * newWidth - image->getWidth() < image->getWidth() - newWidth)) {
130  newWidth *= 2;
131  }
132  if (newHeight == image->getHeight() && newWidth == image->getWidth()) {
133  return false;
134  }
135  image->scale(newWidth, newHeight);
136  return true;
137 }
138 
139 
140 // smell: yellow (the save functions may have additional options, not regarded)
141 // Save file
142 FXbool
143 MFXImageHelper::saveImage(const std::string& file,
144  int width, int height, FXColor* data) {
145  FXString ext = FXPath::extension(file.c_str());
146  checkSupported(ext);
147  FXFileStream stream;
148  if (!stream.open(file.c_str(), FXStreamSave)) {
149  throw InvalidArgument("Could not open file for writing!");
150  }
151  if (comparecase(ext, "gif") == 0) {
152  return fxsaveGIF(stream, data, width, height, false /* !!! "fast" */);
153  } else if (comparecase(ext, "bmp") == 0) {
154  return fxsaveBMP(stream, data, width, height);
155  } else if (comparecase(ext, "xpm") == 0) {
156  return fxsaveXPM(stream, data, width, height);
157  } else if (comparecase(ext, "pcx") == 0) {
158  return fxsavePCX(stream, data, width, height);
159  } else if (comparecase(ext, "ico") == 0 || comparecase(ext, "cur") == 0) {
160  return fxsaveICO(stream, data, width, height);
161  } else if (comparecase(ext, "tga") == 0) {
162  return fxsaveTGA(stream, data, width, height);
163  } else if (comparecase(ext, "rgb") == 0) {
164  return fxsaveRGB(stream, data, width, height);
165  } else if (comparecase(ext, "xbm") == 0) {
166  return fxsaveXBM(stream, data, width, height);
167  } else if (comparecase(ext, "png") == 0) {
168  return fxsavePNG(stream, data, width, height);
169  } else if (comparecase(ext, "jpg") == 0 || comparecase(ext, "jpeg") == 0) {
170  return fxsaveJPG(stream, data, width, height, 75);
171  } else if (comparecase(ext, "tif") == 0 || comparecase(ext, "tiff") == 0) {
172  return fxsaveTIF(stream, data, width, height, 0);
173  }
174  throw InvalidArgument("Unknown file extension for image!");
175 }
176 
177 
178 
179 /****************************************************************************/
180 
static FXbool scalePower2(FXImage *image, int maxSize=(2<< 29))
static FXbool saveImage(const std::string &file, int width, int height, FXColor *data)
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
static FXImage * loadImage(FXApp *a, const std::string &file)
static void checkSupported(FXString ext)