mdlimage.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "mdlimage.h"
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include "ifilesystem.h"
  21. #include "bytestreamutils.h"
  22. #include "stream/textstream.h"
  23. #include "imagelib.h"
  24. #include "mdlformat.h"
  25. #include "ident.h"
  26. unsigned char mdl_palette[768];
  27. /*
  28. ==============
  29. Texture_InitPalette
  30. ==============
  31. */
  32. void Texture_InitPalette (byte *pal)
  33. {
  34. int r,g,b;
  35. int i;
  36. int inf;
  37. byte gammatable[256];
  38. double gamma;
  39. gamma = 1.0;//g_qeglobals.d_savedinfo.fGamma;
  40. if (gamma == 1.0)
  41. {
  42. for (i=0 ; i<256 ; i++)
  43. gammatable[i] = i;
  44. } else
  45. {
  46. for (i=0 ; i<256 ; i++)
  47. {
  48. inf = (int)(255 * pow ( (i+0.5)/255.5 , gamma ) + 0.5);
  49. if (inf < 0)
  50. inf = 0;
  51. if (inf > 255)
  52. inf = 255;
  53. gammatable[i] = inf;
  54. }
  55. }
  56. for (i=0 ; i<256 ; i++)
  57. {
  58. r = gammatable[pal[0]];
  59. g = gammatable[pal[1]];
  60. b = gammatable[pal[2]];
  61. pal += 3;
  62. //v = (r<<24) + (g<<16) + (b<<8) + 255;
  63. //v = BigLong (v);
  64. //mdl_palette[i] = v;
  65. mdl_palette[i*3+0] = r;
  66. mdl_palette[i*3+1] = g;
  67. mdl_palette[i*3+2] = b;
  68. }
  69. }
  70. bool LoadPalette()
  71. {
  72. unsigned char* buffer;
  73. //int len =
  74. vfsLoadFile ("gfx/palette.lmp", (void **) &buffer);
  75. if (buffer == 0)
  76. return false;
  77. Texture_InitPalette(buffer);
  78. vfsFreeFile(buffer);
  79. return true;
  80. }
  81. Image* LoadMDLImageBuff(byte* buffer)
  82. {
  83. if(!LoadPalette())
  84. {
  85. return 0;
  86. }
  87. if(!ident_equal(buffer, MDL_IDENT))
  88. {
  89. globalErrorStream() << "LoadMDLImage: data has wrong ident\n";
  90. return 0;
  91. }
  92. PointerInputStream inputStream(buffer);
  93. inputStream.seek(4 + 4 + 12 + 12 + 4 + 12);
  94. //int numskins =
  95. istream_read_int32_le(inputStream);
  96. int skinwidth = istream_read_int32_le(inputStream);
  97. int skinheight = istream_read_int32_le(inputStream);
  98. inputStream.seek(4 + 4 + 4 + 4 + 4 + 4);
  99. switch(istream_read_int32_le(inputStream))
  100. {
  101. case MDL_SKIN_SINGLE:
  102. break;
  103. case MDL_SKIN_GROUP:
  104. int numskins = istream_read_int32_le(inputStream);
  105. inputStream.seek(numskins * 4);
  106. break;
  107. }
  108. RGBAImage* image = new RGBAImage(skinwidth, skinheight);
  109. unsigned char* pRGBA = image->getRGBAPixels();
  110. for(int i=0; i<(skinheight); i++)
  111. {
  112. for(int j=0; j<(skinwidth); j++)
  113. {
  114. byte index = istream_read_byte(inputStream);
  115. *pRGBA++ = mdl_palette[index * 3 + 0];
  116. *pRGBA++ = mdl_palette[index * 3 + 1];
  117. *pRGBA++ = mdl_palette[index * 3 + 2];
  118. *pRGBA++ = 255;
  119. }
  120. }
  121. return image;
  122. }
  123. Image* LoadMDLImage(ArchiveFile& file)
  124. {
  125. ScopedArchiveBuffer buffer(file);
  126. return LoadMDLImageBuff( buffer.buffer );
  127. }
  128. void MDLImage_Destroy(byte* pic)
  129. {
  130. free(pic);
  131. }