Cafu Engine
Bitmap.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_BITMAP_LIB_HPP_INCLUDED
8 #define CAFU_BITMAP_LIB_HPP_INCLUDED
9 
10 #include "Templates/Array.hpp"
11 
12 #if defined(_WIN32) && _MSC_VER<1600
13 #include "pstdint.h" // Paul Hsieh's portable implementation of the stdint.h header.
14 #else
15 #include <stdint.h>
16 #endif
17 
18 
19 /// This class represents a RGBA bitmap.
20 struct BitmapT
21 {
22  class LoadErrorT {};
23 
24  unsigned int SizeX;
25  unsigned int SizeY;
26  ArrayT<uint32_t> Data;
27 
28 
29  /// Constructor that creates an empty bitmap.
30  BitmapT();
31 
32  /// Constructor that creates a bitmap from the file 'FileName'.
33  /// The file format is automatically determined (only!) by looking at the suffix of 'FileName'.
34  /// Currently supported file formats are BMP (Windows Bitmaps), TGA (TrueVision Targa), JPG (JPEG) and PNG (Portable Network Graphics).
35  /// A 'LoadErrorT' exception is thrown if the bitmap could not be loaded for any reason,
36  /// including file not found errors, unsupported file formats, or unsupported sub-file formats (like paletted Windows bitmaps).
37  ///
38  /// Note that the Cafu file system (cf::FileSys) is used to load the image!
39  /// That means that an appropriate file system must be mounted in order to be able to create/load an image.
40  BitmapT(const char* FileName) /*throw (LoadErrorT)*/;
41 
42  /// Constructor that creates a bitmap from the memory pointed to by 'Buffer'.
43  BitmapT(unsigned int Width, unsigned int Height, const uint32_t* Buffer=NULL);
44 
45  /// Named constructor which returns a built-in "?FILE NOT FOUND" bitmap.
47 
48 
49  /// These methods return and accept values in the range from 0 to 255.
50  /// The SetPixel() method will clamp to this range if necessary.
51  void GetPixel(unsigned int x, unsigned int y, int& r, int& g, int& b) const;
52  void SetPixel(unsigned int x, unsigned int y, int r, int g, int b);
53  void GetPixel(unsigned int x, unsigned int y, int& r, int& g, int& b, int& a) const;
54  void SetPixel(unsigned int x, unsigned int y, int r, int g, int b, int a);
55 
56  /// These methods return and accept values in the range from 0.0 to 1.0.
57  /// The SetPixel() method will clamp to this range if necessary.
58  void GetPixel(unsigned int x, unsigned int y, float& r, float& g, float& b) const;
59  void SetPixel(unsigned int x, unsigned int y, float r, float g, float b);
60  void GetPixel(unsigned int x, unsigned int y, float& r, float& g, float& b, float& a) const;
61  void SetPixel(unsigned int x, unsigned int y, float r, float g, float b, float a);
62 
63 
64  /// This method applies the gamma value 'Gamma' to this bitmap.
65  void ApplyGamma(float Gamma);
66 
67  /// Scales the bitmap to the new dimensions NewSizeX and NewSizeY.
68  /// You may specify 0 for NewSizeX or NewSizeY in order to keep that direction unchanged.
69  void Scale(unsigned int NewSizeX, unsigned int NewSizeY);
70 
71  /// This functions computes a paletted (8 BPP) image from this bitmap,
72  /// using the NeuQuant Neural-Net quantization algorithm (c) by Anthony Dekker, 1994.
73  /// See "Kohonen neural networks for optimal colour quantization"
74  /// in "Network: Computation in Neural Systems" Vol. 5 (1994) pp 351-367.
75  /// for a discussion of the algorithm.
76  /// See also http://members.ozemail.com.au/~dekker/NEUQUANT.HTML
77  /// The returned pointer contains 256*3 palette bytes (256 RGB color triples),
78  /// followed by 'SizeX*SizeY' bytes of image indices into the palette.
79  /// It is the callers responsibility to call 'delete[]' on the returned pointer.
80  char* GetPalettedImage() const;
81 
82  /// Writes the contents of this bitmap into a file with name 'FileName'.
83  /// The file format is automatically determined by looking at the suffix of 'FileName'.
84  /// Currently supported file formats are BMP (Windows Bitmaps), JPG (JPEG) and PNG (Portable Network Graphics).
85  /// NOTE: When saving the bitmap in BMP or JPG file format, the alpha channel is lost!
86  /// Returns 'true' on success, 'false' on failure.
87  /// Reasons for failure include unknown file formats (e.g. passing "hello.tga" for 'FileName'),
88  /// and problems with the file itself (like invalid path, no disk space left, and so on).
89  bool SaveToDisk(const char* FileName) const;
90 };
91 
92 #endif
bool SaveToDisk(const char *FileName) const
Writes the contents of this bitmap into a file with name 'FileName'.
Definition: Bitmap.cpp:1250
char * GetPalettedImage() const
This functions computes a paletted (8 BPP) image from this bitmap, using the NeuQuant Neural-Net quan...
Definition: Bitmap.cpp:737
void ApplyGamma(float Gamma)
This method applies the gamma value 'Gamma' to this bitmap.
Definition: Bitmap.cpp:555
Definition: Bitmap.hpp:22
This class represents a RGBA bitmap.
Definition: Bitmap.hpp:20
static BitmapT GetBuiltInFileNotFoundBitmap()
Named constructor which returns a built-in "?FILE NOT FOUND" bitmap.
Definition: Bitmap.cpp:435
void Scale(unsigned int NewSizeX, unsigned int NewSizeY)
Scales the bitmap to the new dimensions NewSizeX and NewSizeY.
Definition: Bitmap.cpp:590
void GetPixel(unsigned int x, unsigned int y, int &r, int &g, int &b) const
These methods return and accept values in the range from 0 to 255.
Definition: Bitmap.cpp:449
BitmapT()
Constructor that creates an empty bitmap.
Definition: Bitmap.cpp:81