1 /**
2  * Copyright (c) 2006-2016 LOVE Development Team
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  **/
20 
21 #ifndef LOVE_IMAGE_COMPRESSED_IMAGE_DATA_H
22 #define LOVE_IMAGE_COMPRESSED_IMAGE_DATA_H
23 
24 // LOVE
25 #include "common/Data.h"
26 #include "common/StringMap.h"
27 #include "common/int.h"
28 
29 // STL
30 #include <vector>
31 
32 namespace love
33 {
34 namespace image
35 {
36 
37 /**
38  * CompressedImageData represents image data which is designed to be uploaded to
39  * the GPU and rendered in its compressed form, without being decompressed.
40  * http://renderingpipeline.com/2012/07/texture-compression/
41  **/
42 class CompressedImageData : public Data
43 {
44 public:
45 
46 	// Recognized compressed image data formats.
47 	enum Format
48 	{
49 		FORMAT_UNKNOWN,
50 		FORMAT_DXT1,
51 		FORMAT_DXT3,
52 		FORMAT_DXT5,
53 		FORMAT_BC4,
54 		FORMAT_BC4s,
55 		FORMAT_BC5,
56 		FORMAT_BC5s,
57 		FORMAT_BC6H,
58 		FORMAT_BC6Hs,
59 		FORMAT_BC7,
60 		FORMAT_PVR1_RGB2,
61 		FORMAT_PVR1_RGB4,
62 		FORMAT_PVR1_RGBA2,
63 		FORMAT_PVR1_RGBA4,
64 		FORMAT_ETC1,
65 		FORMAT_ETC2_RGB,
66 		FORMAT_ETC2_RGBA,
67 		FORMAT_ETC2_RGBA1,
68 		FORMAT_EAC_R,
69 		FORMAT_EAC_Rs,
70 		FORMAT_EAC_RG,
71 		FORMAT_EAC_RGs,
72 		FORMAT_ASTC_4x4,
73 		FORMAT_ASTC_5x4,
74 		FORMAT_ASTC_5x5,
75 		FORMAT_ASTC_6x5,
76 		FORMAT_ASTC_6x6,
77 		FORMAT_ASTC_8x5,
78 		FORMAT_ASTC_8x6,
79 		FORMAT_ASTC_8x8,
80 		FORMAT_ASTC_10x5,
81 		FORMAT_ASTC_10x6,
82 		FORMAT_ASTC_10x8,
83 		FORMAT_ASTC_10x10,
84 		FORMAT_ASTC_12x10,
85 		FORMAT_ASTC_12x12,
86 		FORMAT_MAX_ENUM
87 	};
88 
89 	// Compressed image data can have multiple mipmap levels, each represented
90 	// by a sub-image.
91 	struct SubImage
92 	{
93 		int width, height;
94 		size_t size;
95 		uint8 *data; // Should not have ownership of the data.
96 	};
97 
98 	CompressedImageData();
99 	virtual ~CompressedImageData();
100 
101 	// Implements Data.
102 	virtual void *getData() const;
103 	virtual size_t getSize() const;
104 
105 	/**
106 	 * Gets the number of mipmaps in this Compressed Image Data.
107 	 * Includes the base image level.
108 	 **/
109 	int getMipmapCount() const;
110 
111 	/**
112 	 * Gets the size in bytes of a sub-image at the specified mipmap level.
113 	 **/
114 	size_t getSize(int miplevel) const;
115 
116 	/**
117 	 * Gets the byte data of a sub-image at the specified mipmap level.
118 	 **/
119 	void *getData(int miplevel) const;
120 
121 	/**
122 	 * Gets the width of a sub-image at the specified mipmap level.
123 	 **/
124 	int getWidth(int miplevel = 0) const;
125 
126 	/**
127 	 * Gets the height of a sub-image at the specified mipmap level.
128 	 **/
129 	int getHeight(int miplevel = 0) const;
130 
131 	/**
132 	 * Gets the format of the compressed data.
133 	 **/
134 	Format getFormat() const;
135 
136 	bool isSRGB() const;
137 
138 	static bool getConstant(const char *in, Format &out);
139 	static bool getConstant(Format in, const char *&out);
140 
141 protected:
142 
143 	Format format;
144 
145 	bool sRGB;
146 
147 	// Single block of memory containing all of the sub-images.
148 	uint8 *data;
149 	size_t dataSize;
150 
151 	// Texture info for each mipmap level.
152 	std::vector<SubImage> dataImages;
153 
154 	void checkMipmapLevelExists(int miplevel) const;
155 
156 private:
157 
158 	static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
159 	static StringMap<Format, FORMAT_MAX_ENUM> formats;
160 
161 }; // CompressedImageData
162 
163 } // image
164 } // love
165 
166 #endif // LOVE_IMAGE_COMPRESSED_IMAGE_DATA_H
167