texture.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef __EACSMB_texture_h__
  2. #define __EACSMB_texture_h__
  3. #include "c3dlas.h"
  4. #include "hash.h"
  5. #include "ds.h"
  6. // defined in main.c. dropped in here for visibility. was originally in a utilities file.
  7. char* pathJoin(const char* a, const char* b);
  8. const char* pathExt(const char* path);
  9. const char* pathExt2(const char* path, int* end);
  10. #define streq(a, b) (0 == strcmp(a, b))
  11. #define strcaseeq(a, b) (0 == strcasecmp(a, b))
  12. // lots of cruft in this file from the parent project
  13. typedef struct {
  14. char* path;
  15. short width, height;
  16. uint32_t* data;
  17. } BitmapRGBA8;
  18. enum TextureDepth {
  19. TEXDEPTH_8,
  20. TEXDEPTH_16,
  21. TEXDEPTH_32,
  22. TEXDEPTH_FLOAT,
  23. TEXDEPTH_DOUBLE,
  24. TEXDEPTH_MAXVALUE
  25. };
  26. typedef struct TexBitmap {
  27. short channels;
  28. enum TextureDepth depth;
  29. unsigned int width, height;
  30. union {
  31. uint8_t* data8;
  32. uint16_t* data16;
  33. uint32_t* data32;
  34. float* fdata;
  35. double* ddata;
  36. };
  37. } TexBitmap;
  38. typedef struct FloatBitmap {
  39. unsigned int w, h;
  40. float* data;
  41. } FloatBitmap;
  42. typedef struct FloatTex {
  43. char channels;
  44. unsigned int w, h;
  45. FloatBitmap* bmps[4];
  46. } FloatTex;
  47. BitmapRGBA8* readPNG(char* path);
  48. int readPNG2(char* path, BitmapRGBA8* bmp);
  49. TexBitmap* TexBitmap_create(int w, int h, enum TextureDepth d, int channels);
  50. int TexBitmap_pixelStride(TexBitmap* bmp);
  51. int TexBitmap_componentSize(TexBitmap* bmp);
  52. void* TexBitmap_pixelPointer(TexBitmap* bmp, int x, int y);
  53. void TexBitmap_sampleFloat(TexBitmap* bmp, int x, int y, float* out);
  54. BitmapRGBA8* TexGen_Generate(char* source, Vector2i size);
  55. FloatBitmap* FloatBitmap_alloc(int width, int height);
  56. FloatTex* FloatTex_alloc(int width, int height, int channels);
  57. void FloatTex_free(FloatTex* ft);
  58. FloatTex* FloatTex_similar(FloatTex* orig);
  59. FloatTex* FloatTex_copy(FloatTex* orig);
  60. float FloatTex_sample(FloatTex* ft, float xf, float yf, int channel);
  61. float FloatTex_texelFetch(FloatTex* ft, int x, int y, int channel);
  62. BitmapRGBA8* FloatTex_ToRGBA8(FloatTex* ft);
  63. // found in textureAtlas.c
  64. typedef struct TextureAtlasItem {
  65. Vector2 offsetPx;
  66. Vector2 offsetNorm;
  67. Vector2 sizePx;
  68. Vector2 sizeNorm;
  69. int index;
  70. } TextureAtlasItem;
  71. // for building the atlas
  72. typedef struct TextureAtlasSource {
  73. char* name;
  74. float aspectRatio;
  75. Vector2 size;
  76. uint8_t* data;
  77. } TextureAtlasSource;
  78. typedef struct TextureAtlas {
  79. HashTable(TextureAtlasItem*) items;
  80. int width;
  81. VEC(uint32_t*) atlas;
  82. char* pngFileFormat;
  83. VEC(TextureAtlasSource*) sources;
  84. int maxAtlasSize;
  85. int verbose;
  86. } TextureAtlas;
  87. TextureAtlas* TextureAtlas_alloc();
  88. void TextureAtlas_init(TextureAtlas* ta);
  89. void TextureAtlas_addPNG(TextureAtlas* ta, char* path);
  90. void TextureAtlas_addFolder(TextureAtlas* ta, char* prefix, char* dirPath, int recursive);
  91. void TextureAtlas_finalize(TextureAtlas* ta);
  92. #endif // __EACSMB_texture_h__