1 #ifndef _IMAGE_LOADER_
2 #define _IMAGE_LOADER_
3 /*
4
5 Copyright (C) 1991-2001 and beyond by Bungie Studios, Inc.
6 and the "Aleph One" developers.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 This license is contained in the file "COPYING",
19 which is included with this source code; it is available online at
20 http://www.gnu.org/licenses/gpl.html
21
22 Image-Loader Interface File,
23 by Loren Petrich,
24 October 21, 2000
25
26 This file contains an image-descriptor object and a function for loading the image
27 from a file.
28
29 */
30
31 #include "DDS.h"
32 #include <vector>
33 #include "cseries.h"
34 #include "FileHandler.h"
35
36 // Need an object to hold the read-in image.
37 class ImageDescriptor
38 {
39 int Width; // along scanlines
40 int Height; // scanline to scanline
41
42 double VScale;
43 double UScale;
44
45 uint32 *Pixels;
46 int Size;
47
48 int MipMapCount;
49
50 public:
51
IsPresent()52 bool IsPresent() const {return (Pixels != NULL); }
IsPremultiplied()53 bool IsPremultiplied() const { return (IsPresent() ? PremultipliedAlpha : false); }
54
55 bool LoadFromFile(FileSpecifier& File, int ImgMode, int flags, int actual_width = 0, int actual_height = 0, int maxSize = 0);
56
57 // Size of level 0 image
GetWidth()58 int GetWidth() const {return Width;}
GetHeight()59 int GetHeight() const {return Height;}
GetNumPixels()60 int GetNumPixels() const {return Width*Height;}
61
GetMipMapCount()62 int GetMipMapCount() const { return MipMapCount; }
GetTotalBytes()63 int GetTotalBytes() const { return Size; }
GetBufferSize()64 int GetBufferSize() const { return Size; }
GetFormat()65 int GetFormat() const { return Format; }
66
GetVScale()67 double GetVScale() const { return VScale; }
GetUScale()68 double GetUScale() const { return UScale; }
69
70 // Pixel accessors
GetPixel(int Horiz,int Vert)71 uint32& GetPixel(int Horiz, int Vert) {return Pixels[Width*(Vert%Height) + (Horiz%Width)];}
GetPixelBasePtr()72 uint32 *GetPixelBasePtr() {return Pixels;}
GetBuffer()73 const uint32 *GetBuffer() const { return Pixels; }
GetBuffer()74 uint32 *GetBuffer() { return Pixels; }
75
76 uint32 *GetMipMapPtr(int Level);
77 const uint32 *GetMipMapPtr(int Level) const;
78 int GetMipMapSize(int level) const;
79
80 // Reallocation
81 void Resize(int _Width, int _Height);
82
83 // mipmappy operations
84 void Resize(int _Width, int _Height, int _TotalBytes);
85
86 bool Minify();
87
88 bool MakeRGBA();
89 bool MakeDXTC3();
90
91 void PremultiplyAlpha();
92 bool PremultipliedAlpha; // public so find silhouette version can unset
93
94 // Clearing
Clear()95 void Clear()
96 {Width = Height = Size = 0; delete []Pixels; Pixels = NULL;}
97
98 ImageDescriptor(const ImageDescriptor &CopyFrom);
99
ImageDescriptor()100 ImageDescriptor(): Width(0), Height(0), VScale(1.0), UScale(1.0), Pixels(NULL), Size(0), PremultipliedAlpha(false) {}
101
102 // asumes RGBA8
103 ImageDescriptor(int width, int height, uint32 *pixels);
104
105 enum ImageFormat {
106 RGBA8,
107 DXTC1,
108 DXTC3,
109 DXTC5,
110 Unknown
111 };
112
~ImageDescriptor()113 ~ImageDescriptor()
114 {
115 delete []Pixels;
116 Pixels = NULL;
117 }
118
119 private:
120 bool LoadDDSFromFile(FileSpecifier& File, int flags, int actual_width = 0, int actual_height = 0, int maxSize = 0);
121 bool LoadMipMapFromFile(OpenedFile &File, int flags, int level, DDSURFACEDESC2 &ddsd, int skip);
122 bool SkipMipMapFromFile(OpenedFile &File, int flags, int level, DDSURFACEDESC2 &ddsd);
123
124 ImageFormat Format;
125 };
126
127 template <typename T>
128 class copy_on_edit
129 {
130 public:
copy_on_edit()131 copy_on_edit() : _original(NULL), _copy(NULL) { };
132
set(const T * original)133 void set(const T* original) {
134 if (_copy) {
135 delete _copy;
136 _copy = NULL;
137 }
138 _original = original;
139 }
140
set(T * original)141 void set(T* original) {
142 if (_copy) {
143 delete _copy;
144 _copy= NULL;
145 }
146 _original = original;
147 }
148
149
get()150 const T* get() {
151 if (_copy)
152 return (const T*) _copy;
153 else
154 return (const T*) _original;
155 }
156
edit()157 T* edit() {
158 if (!_original) {
159 return _copy;
160 } else {
161 if (!_copy) {
162 _copy = new T(*_original);
163 }
164 return _copy;
165 }
166 }
167
168 // takes possession of copy
edit(T * copy)169 T* edit(T* copy) {
170 if (_copy) {
171 delete _copy;
172 }
173 _original = NULL;
174 _copy = copy;
175 return _copy;
176 }
177
~copy_on_edit()178 ~copy_on_edit() {
179 if (_copy) {
180 delete _copy;
181 _copy = NULL;
182 }
183 }
184
185 private:
186 T* _original;
187 T* _copy;
188 };
189
190 typedef copy_on_edit<ImageDescriptor> ImageDescriptorManager;
191
192
193 // What to load: image colors (must be loaded first)
194 // or image opacity (replaces the default, which is 100% opaque everywhere).
195 // The image-opacity image must have the same size as the color image;
196 // it is interpreted as a grayscale image.
197 enum {
198 ImageLoader_Colors,
199 ImageLoader_Opacity
200 };
201
202 enum {
203 ImageLoader_ResizeToPowersOfTwo = 0x1,
204 ImageLoader_CanUseDXTC = 0x2,
205 ImageLoader_LoadMipMaps = 0x4,
206 ImageLoader_LoadDXTC1AsDXTC3 = 0x8,
207 ImageLoader_ImageIsAlreadyPremultiplied = 0x10
208 };
209 // Returns whether or not the loading was successful
210 //bool LoadImageFromFile(ImageDescriptor& Img, FileSpecifier& File, int ImgMode, int flags, int maxSize = 0);
211
212 uint32 *GetMipMapPtr(uint32 *pixels, int size, int level, int width, int height, int format);
213
214 #endif
215