image_DXT.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Jonathan Dummer
  3. 2007-07-31-10.32
  4. simple DXT compression / decompression code
  5. public domain
  6. */
  7. #ifndef HEADER_IMAGE_DXT
  8. #define HEADER_IMAGE_DXT
  9. /**
  10. Converts an image from an array of unsigned chars (RGB or RGBA) to
  11. DXT1 or DXT5, then saves the converted image to disk.
  12. \return 0 if failed, otherwise returns 1
  13. **/
  14. int
  15. save_image_as_DDS
  16. (
  17. const char *filename,
  18. int width, int height, int channels,
  19. const unsigned char *const data
  20. );
  21. /**
  22. take an image and convert it to DXT1 (no alpha)
  23. **/
  24. unsigned char*
  25. convert_image_to_DXT1
  26. (
  27. const unsigned char *const uncompressed,
  28. int width, int height, int channels,
  29. int *out_size
  30. );
  31. /**
  32. take an image and convert it to DXT5 (with alpha)
  33. **/
  34. unsigned char*
  35. convert_image_to_DXT5
  36. (
  37. const unsigned char *const uncompressed,
  38. int width, int height, int channels,
  39. int *out_size
  40. );
  41. /** A bunch of DirectDraw Surface structures and flags **/
  42. typedef struct
  43. {
  44. unsigned int dwMagic;
  45. unsigned int dwSize;
  46. unsigned int dwFlags;
  47. unsigned int dwHeight;
  48. unsigned int dwWidth;
  49. unsigned int dwPitchOrLinearSize;
  50. unsigned int dwDepth;
  51. unsigned int dwMipMapCount;
  52. unsigned int dwReserved1[ 11 ];
  53. /* DDPIXELFORMAT */
  54. struct
  55. {
  56. unsigned int dwSize;
  57. unsigned int dwFlags;
  58. unsigned int dwFourCC;
  59. unsigned int dwRGBBitCount;
  60. unsigned int dwRBitMask;
  61. unsigned int dwGBitMask;
  62. unsigned int dwBBitMask;
  63. unsigned int dwAlphaBitMask;
  64. }
  65. sPixelFormat;
  66. /* DDCAPS2 */
  67. struct
  68. {
  69. unsigned int dwCaps1;
  70. unsigned int dwCaps2;
  71. unsigned int dwDDSX;
  72. unsigned int dwReserved;
  73. }
  74. sCaps;
  75. unsigned int dwReserved2;
  76. }
  77. DDS_header ;
  78. /* the following constants were copied directly off the MSDN website */
  79. /* The dwFlags member of the original DDSURFACEDESC2 structure
  80. can be set to one or more of the following values. */
  81. #define DDSD_CAPS 0x00000001
  82. #define DDSD_HEIGHT 0x00000002
  83. #define DDSD_WIDTH 0x00000004
  84. #define DDSD_PITCH 0x00000008
  85. #define DDSD_PIXELFORMAT 0x00001000
  86. #define DDSD_MIPMAPCOUNT 0x00020000
  87. #define DDSD_LINEARSIZE 0x00080000
  88. #define DDSD_DEPTH 0x00800000
  89. /* DirectDraw Pixel Format */
  90. #define DDPF_ALPHAPIXELS 0x00000001
  91. #define DDPF_FOURCC 0x00000004
  92. #define DDPF_RGB 0x00000040
  93. /* The dwCaps1 member of the DDSCAPS2 structure can be
  94. set to one or more of the following values. */
  95. #define DDSCAPS_COMPLEX 0x00000008
  96. #define DDSCAPS_TEXTURE 0x00001000
  97. #define DDSCAPS_MIPMAP 0x00400000
  98. /* The dwCaps2 member of the DDSCAPS2 structure can be
  99. set to one or more of the following values. */
  100. #define DDSCAPS2_CUBEMAP 0x00000200
  101. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  102. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  103. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  104. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  105. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  106. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  107. #define DDSCAPS2_VOLUME 0x00200000
  108. #endif /* HEADER_IMAGE_DXT */