1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #ifndef _BITMAP_H
4 #define _BITMAP_H
5 
6 #include <string>
7 #ifndef BITMAP_NO_OPENGL
8 	#include "nv_dds.h"
9 #endif // !BITMAP_NO_OPENGL
10 #include "System/float3.h"
11 #include "System/Color.h"
12 
13 
14 struct SDL_Surface;
15 
16 
17 class CBitmap
18 {
19 public:
20 	CBitmap(const unsigned char* data, int xsize, int ysize, int channels = 4);
21 	CBitmap();
22 	CBitmap(const CBitmap& old);
23 	CBitmap& operator=(const CBitmap& bm);
24 	CBitmap(CBitmap&& bm);
25 	CBitmap& operator=(CBitmap&& bm);
26 
27 	virtual ~CBitmap();
28 
29 	void Alloc(int w, int h, int c);
30 	void Alloc(int w, int h);
31 	void AllocDummy(const SColor fill = SColor(255,0,0,255));
32 
33 	/// Load data from a file on the VFS
34 	bool Load(std::string const& filename, unsigned char defaultAlpha = 255);
35 	/// Load data from a gray-scale file on the VFS
36 	bool LoadGrayscale(std::string const& filename);
37 	bool Save(std::string const& filename, bool opaque = true) const;
38 
39 	const unsigned int CreateTexture(bool mipmaps = false) const;
40 	const unsigned int CreateDDSTexture(unsigned int texID = 0, bool mipmaps = false) const;
41 
42 	void CreateAlpha(unsigned char red, unsigned char green, unsigned char blue);
43 	void SetTransparent(const SColor& c, const SColor trans = SColor(0,0,0,0));
44 
45 	void Renormalize(float3 newCol);
46 	void Blur(int iterations = 1, float weight = 1.0f);
47 
48 	void CopySubImage(const CBitmap& src, int x, int y);
49 
50 	/**
51 	 * Allocates a new SDL_Surface, and feeds it with the data of this bitmap.
52 	 * @param newPixelData
53 	 *        if false, the returned struct will have a pointer
54 	 *        to the internal pixel data (mem), which means it is only valid
55 	 *        as long as mem is not freed, which should only happen
56 	 *        when the this bitmap is deleted.
57 	 *        If true, an array is allocated with new, and has to be deleted
58 	 *        after SDL_FreeSurface() is called.
59 	 * Note:
60 	 * - You have to free the surface with SDL_FreeSurface(surface)
61 	 *   if you do not need it anymore!
62 	 */
63 	SDL_Surface* CreateSDLSurface(bool newPixelData = false) const;
64 
65 	unsigned char* mem;
66 	int xsize;
67 	int ysize;
68 	int channels;
69 	bool compressed;
70 
71 #ifndef BITMAP_NO_OPENGL
72 	int textype; //! GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP, ...
73 	nv_dds::CDDSImage* ddsimage;
74 #endif // !BITMAP_NO_OPENGL
75 
76 public:
77 	CBitmap CanvasResize(const int newx, const int newy, const bool center = true) const;
78 	CBitmap CreateRescaled(int newx, int newy) const;
79 	void ReverseYAxis();
80 	void InvertColors();
81 	void InvertAlpha();
82 	void GrayScale();
83 	void Tint(const float tint[3]);
84 };
85 
86 #endif // _BITMAP_H
87