]> icculus.org git repositories - divverent/netradiant.git/blob - libs/imagelib.h
NOW I do it right: #woxblox#
[divverent/netradiant.git] / libs / imagelib.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_IMAGELIB_H)
23 #define INCLUDED_IMAGELIB_H
24
25 #include "iimage.h"
26 #include "iarchive.h"
27 #include "idatastream.h"
28 #include <stdlib.h>
29
30 struct RGBAPixel
31 {
32   unsigned char red, green, blue, alpha;
33 };
34
35 class RGBAImage : public Image
36 {
37   RGBAImage(const RGBAImage& other);
38   RGBAImage& operator=(const RGBAImage& other);
39 public:
40   RGBAPixel* pixels;
41   unsigned int width, height;
42
43   RGBAImage(unsigned int _width, unsigned int _height)
44     : pixels(new RGBAPixel[_width * _height]), width(_width), height(_height)
45   {
46   }
47   ~RGBAImage()
48   {
49     delete[] pixels;
50   }
51
52   void release()
53   {
54     delete this;
55   }
56   byte* getRGBAPixels() const
57   {
58     return reinterpret_cast<byte*>(pixels);
59   }
60   unsigned int getWidth() const
61   {
62     return width;
63   }
64   unsigned int getHeight() const
65   {
66     return height;
67   }
68 };
69
70 class RGBAImageFlags : public RGBAImage
71 {
72 public:
73   int m_surfaceFlags;
74   int m_contentFlags;
75   int m_value;
76   RGBAImageFlags(unsigned short _width, unsigned short _height, int surfaceFlags, int contentFlags, int value) :
77     RGBAImage(_width, _height), m_surfaceFlags(surfaceFlags), m_contentFlags(contentFlags), m_value(value)
78   {
79   }
80
81   int getSurfaceFlags() const
82   {
83     return m_surfaceFlags;
84   }
85   int getContentFlags() const
86   {
87     return m_contentFlags;
88   }
89   int getValue() const
90   {
91     return m_value;
92   }
93 };
94
95
96 inline InputStream::byte_type* ArchiveFile_loadBuffer(ArchiveFile& file, std::size_t& length)
97 {
98   InputStream::byte_type* buffer = (InputStream::byte_type*)malloc(file.size() + 1);
99   length = file.getInputStream().read(buffer, file.size());
100   buffer[file.size()] = 0;
101   return buffer;
102 }
103
104 inline void ArchiveFile_freeBuffer(InputStream::byte_type* buffer)
105 {
106   free(buffer);
107 }
108
109 class ScopedArchiveBuffer
110 {
111 public:
112   std::size_t length;
113   InputStream::byte_type* buffer;
114
115   ScopedArchiveBuffer(ArchiveFile& file)
116   {
117     buffer = ArchiveFile_loadBuffer(file, length);
118   }
119   ~ScopedArchiveBuffer()
120   {
121     ArchiveFile_freeBuffer(buffer);
122   }
123 };
124
125 class PointerInputStream : public InputStream
126 {
127   const byte* m_read;
128 public:
129   PointerInputStream(const byte* pointer)
130     : m_read(pointer)
131   {
132   }
133   std::size_t read(byte* buffer, std::size_t length)
134   {
135     const byte* end = m_read + length;
136     while(m_read != end)
137     {
138       *buffer++ = *m_read++;
139     }
140     return length;
141   }
142   void seek(std::size_t offset)
143   {
144     m_read += offset;
145   }
146   const byte* get()
147   {
148     return m_read;
149   }
150 };
151
152 #endif