imagelib.h 3.1 KB

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