Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
image.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include "graphics/core/color.h"
29 
30 #include "math/intpoint.h"
31 
32 #include <stddef.h>
33 #include <string>
34 
35 
36 // Forward declaration without including headers to clutter the code
37 struct SDL_Surface;
38 
40 
42 struct ImageData
43 {
45  SDL_Surface* surface;
46 
47  ImageData() { surface = NULL; }
48 };
49 
57 class CImage
58 {
59 private:
61  CImage(const CImage &other) {}
63  void operator=(const CImage &other) {}
64 
65 public:
67  CImage();
69  CImage(Math::IntPoint size);
71  virtual ~CImage();
72 
74  void Free();
75 
77  bool IsEmpty() const;
78 
80  ImageData* GetData();
81 
83  Math::IntPoint GetSize() const;
84 
86  void Fill(Gfx::IntColor color);
87 
89  void SetPixel(Math::IntPoint pixel, Gfx::Color color);
90 
92  void SetPixelInt(Math::IntPoint pixel, Gfx::IntColor color);
93 
96 
99 
101  void PadToNearestPowerOfTwo();
102 
104  void ConvertToRGBA();
105 
107  bool Load(const std::string &fileName);
108 
110  bool SavePNG(const std::string &fileName);
111 
113  std::string GetError();
114 
116  void flipVertically();
117 
119  void SetDataPixels(void *pixels);
120 
121 private:
123  void BlitToNewRGBASurface(int width, int height);
124 
126  std::string m_error;
128  ImageData* m_data;
129 };
130 
void PadToNearestPowerOfTwo()
Pads the image to nearest power of 2 dimensions.
Definition: image.cpp:215
void SetPixelInt(Math::IntPoint pixel, Gfx::IntColor color)
Sets the precise color at given pixel.
Definition: image.cpp:317
Gfx::IntColor GetPixelInt(Math::IntPoint pixel)
Returns the precise color at given pixel.
Definition: image.cpp:257
Color with integer values.
Definition: color.h:100
void flipVertically()
Flips the image vertically.
Definition: image.cpp:447
void ConvertToRGBA()
Convert the image to RGBA surface.
Definition: image.cpp:228
void Fill(Gfx::IntColor color)
Fills the whole image with given color.
Definition: image.cpp:201
void Free()
Frees the allocated image data.
Definition: image.cpp:173
bool IsEmpty() const
Returns whether the image is empty (has null data)
Definition: image.cpp:168
Gfx::Color GetPixel(Math::IntPoint pixel)
Returns the color at given pixel.
Definition: image.cpp:305
void SetDataPixels(void *pixels)
sets/replaces the pixels from the surface
Definition: image.cpp:434
SDL_Surface * surface
SDL surface with image data.
Definition: image.h:45
Math::IntPoint GetSize() const
Returns the image size.
Definition: image.cpp:192
Color structs and related functions.
bool Load(const std::string &fileName)
Loads an image from the specified file.
Definition: image.cpp:379
Image loaded from file.
Definition: image.h:57
Implementation-specific image data.
Definition: image.h:42
ImageData * GetData()
Returns the image data; if empty - returns nullptr.
Definition: image.cpp:187
bool SavePNG(const std::string &fileName)
Saves the image to the specified file in PNG format.
Definition: image.cpp:415
void SetPixel(Math::IntPoint pixel, Gfx::Color color)
Sets the color at given pixel.
Definition: image.cpp:369
CImage()
Constructs empty image (with NULL data)
Definition: image.cpp:151
2D Point with integer coords
Definition: intpoint.h:38
RGBA color.
Definition: color.h:38
IntPoint struct.
std::string GetError()
Returns the last error.
Definition: image.cpp:374
virtual ~CImage()
Destroys image, calling Free()
Definition: image.cpp:163