ImgMakers.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "StdInc.h"
  2. #include "SDL_image.h"
  3. #include "Images.h"
  4. #include "FilesHeaders.h"
  5. namespace Gfx
  6. {
  7. /*********** Images formats supported by SDL ***********/
  8. CImage * CImage::makeBySDL(void* data, size_t fileSize, const char* fileExt)
  9. {
  10. SDL_Surface* ret = IMG_LoadTyped_RW(
  11. //create SDL_RW with our data (will be deleted by SDL)
  12. SDL_RWFromConstMem(data, fileSize),
  13. 1, // mark it for auto-deleting
  14. const_cast<char*>(fileExt)); //pass extension without dot (+1 character)
  15. if (ret)
  16. {
  17. CImage * img;
  18. if (ret->format->palette)
  19. {
  20. //set correct value for alpha\unused channel
  21. for (int i=0; i< ret->format->palette->ncolors; i++)
  22. ret->format->palette->colors[i].unused = 255;
  23. CPaletteRGBA* pal = new CPaletteRGBA((ColorRGBA*)ret->format->palette->colors);
  24. img = new CPalettedBitmap(ret->w, ret->h, *pal, (ui8*)ret->pixels);
  25. }
  26. else if (ret->format->BytesPerPixel == 3)
  27. {
  28. img = new CBitmap32(ret->w, ret->h, (ColorRGB*)ret->pixels, ret->format->Rmask == 0x00FF0000);
  29. }
  30. else if (ret->format->BytesPerPixel == 4)
  31. {
  32. img = new CBitmap32(ret->w, ret->h, (ColorRGBA*)ret->pixels, ret->format->Rmask == 0x00FF0000);
  33. }
  34. SDL_FreeSurface(ret);
  35. return img;
  36. }
  37. return nullptr;
  38. }
  39. #define LE(x) SDL_SwapLE32(x)
  40. /*********** H3 PCX image format ***********/
  41. CImage * CImage::makeFromPCX(const SH3PcxFile& pcx, size_t fileSize)
  42. {
  43. ui32 imgSize = LE(pcx.size);
  44. ui32 width = LE(pcx.width);
  45. ui32 height = LE(pcx.height);
  46. if (imgSize == width*height)
  47. {
  48. if (H3PCX_HEADER_SIZE + RGB_PALETTE_SIZE + imgSize > fileSize) return nullptr;
  49. CPaletteRGBA* ppal = new CPaletteRGBA(pcx.palette(fileSize), 0);
  50. return new CPalettedBitmap(width, height, *ppal, pcx.data);
  51. }
  52. if (imgSize == width*height*3)
  53. {
  54. if (H3PCX_HEADER_SIZE + imgSize > fileSize) return nullptr;
  55. return new CBitmap32(width, height, (ColorRGB*)pcx.data);
  56. }
  57. return nullptr;
  58. }
  59. /*********** H3 DEF animation frame ***********/
  60. CImage* CImage::makeFromDEF(const struct SH3DefFile& def, size_t fileSize)
  61. {
  62. CPaletteRGBA* ppal = new CPaletteRGBA(def.palette, def.type == LE(71) ? 1 : 10);
  63. CPalettedBitmap* img = makeFromDEFSprite(def.getSprite(def.firstBlock.offsets()[0]), *ppal);
  64. if (img == nullptr) ppal->Unlink();
  65. return img;
  66. }
  67. CPalettedBitmap* CImage::makeFromDEFSprite(const SH3DefSprite& spr, CPaletteRGBA& pal)
  68. {
  69. ui32 format = LE(spr.format);
  70. if (format > 3) return nullptr;
  71. ui32 fullWidth = LE(spr.fullWidth);
  72. ui32 fullHeight = LE(spr.fullHeight);
  73. ui32 intWidth = LE(spr.width);
  74. ui32 intHeight = LE(spr.height);
  75. ui32 leftMargin = LE(spr.leftMargin);
  76. ui32 topMargin = LE(spr.topMargin);
  77. bool noMargins = (leftMargin == 0 && topMargin == 0 && fullWidth == intWidth && fullHeight == intHeight);
  78. if (format == 0)
  79. {
  80. if (noMargins)
  81. {
  82. return new CPalettedBitmap(fullWidth, fullHeight, pal, spr.data);
  83. }
  84. return new CPalBitmapWithMargin(fullWidth, fullHeight, leftMargin, topMargin, intWidth, intHeight, pal, spr.data);
  85. }
  86. if (noMargins)
  87. {
  88. return new CPalettedBitmap(fullWidth, fullHeight, pal, spr.data, format);
  89. }
  90. return new CPalBitmapWithMargin(fullWidth, fullHeight, leftMargin, topMargin, intWidth, intHeight, pal, spr.data, format);
  91. }
  92. }