Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
image.h
Go to the documentation of this file.
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2012, Polish Portal of Colobot (PPC)
3 // *
4 // * This program is free software: you can redistribute it and/or modify
5 // * it under the terms of the GNU General Public License as published by
6 // * the Free Software Foundation, either version 3 of the License, or
7 // * (at your option) any later version.
8 // *
9 // * This program is distributed in the hope that it will be useful,
10 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // * GNU General Public License for more details.
13 // *
14 // * You should have received a copy of the GNU General Public License
15 // * along with this program. If not, see http://www.gnu.org/licenses/.
16 
22 #pragma once
23 
24 
25 #include "graphics/core/color.h"
26 
27 #include "math/intpoint.h"
28 
29 #include <stddef.h>
30 #include <string>
31 
32 
33 // Forward declaration without including headers to clutter the code
34 struct SDL_Surface;
35 
37 
39 struct ImageData
40 {
42  SDL_Surface* surface;
43 
44  ImageData() { surface = NULL; }
45 };
46 
54 class CImage
55 {
56 private:
58  CImage(const CImage &other) {}
60  void operator=(const CImage &other) {}
61 
62 public:
64  CImage();
66  CImage(Math::IntPoint size);
68  virtual ~CImage();
69 
71  void Free();
72 
74  bool IsEmpty() const;
75 
77  ImageData* GetData();
78 
80  Math::IntPoint GetSize() const;
81 
83  void Fill(Gfx::IntColor color);
84 
86  void SetPixel(Math::IntPoint pixel, Gfx::Color color);
87 
89  void SetPixelInt(Math::IntPoint pixel, Gfx::IntColor color);
90 
93 
96 
99 
101  void ConvertToRGBA();
102 
104  bool Load(const std::string &fileName);
105 
107  bool SavePNG(const std::string &fileName);
108 
110  std::string GetError();
111 
112 private:
114  void BlitToNewRGBASurface(int width, int height);
115 
117  std::string m_error;
119  ImageData* m_data;
120 };
121 
void PadToNearestPowerOfTwo()
Pads the image to nearest power of 2 dimensions.
Definition: image.cpp:211
void SetPixelInt(Math::IntPoint pixel, Gfx::IntColor color)
Sets the precise color at given pixel.
Definition: image.cpp:313
Gfx::IntColor GetPixelInt(Math::IntPoint pixel)
Returns the precise color at given pixel.
Definition: image.cpp:253
Color with integer values.
Definition: color.h:97
void ConvertToRGBA()
Convert the image to RGBA surface.
Definition: image.cpp:224
void Fill(Gfx::IntColor color)
Fills the whole image with given color.
Definition: image.cpp:197
void Free()
Frees the allocated image data.
Definition: image.cpp:169
bool IsEmpty() const
Returns whether the image is empty (has null data)
Definition: image.cpp:164
Gfx::Color GetPixel(Math::IntPoint pixel)
Returns the color at given pixel.
Definition: image.cpp:301
SDL_Surface * surface
SDL surface with image data.
Definition: image.h:42
Math::IntPoint GetSize() const
Returns the image size.
Definition: image.cpp:188
Color structs and related functions.
bool Load(const std::string &fileName)
Loads an image from the specified file.
Definition: image.cpp:375
Image loaded from file.
Definition: image.h:54
Implementation-specific image data.
Definition: image.h:39
ImageData * GetData()
Returns the image data; if empty - returns nullptr.
Definition: image.cpp:183
bool SavePNG(const std::string &fileName)
Saves the image to the specified file in PNG format.
Definition: image.cpp:402
void SetPixel(Math::IntPoint pixel, Gfx::Color color)
Sets the color at given pixel.
Definition: image.cpp:365
CImage()
Constructs empty image (with NULL data)
Definition: image.cpp:147
2D Point with integer coords
Definition: intpoint.h:35
RGBA color.
Definition: color.h:35
IntPoint struct.
std::string GetError()
Returns the last error.
Definition: image.cpp:370
virtual ~CImage()
Destroys image, calling Free()
Definition: image.cpp:159