texture.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "hash.h"
  6. #include "texture.h"
  7. #include <png.h>
  8. #include <setjmp.h>
  9. // something more sophisticated will be needed for tex arrays and view sharing
  10. // a start is a start
  11. BitmapRGBA8* readPNG(char* path) {
  12. int ret;
  13. BitmapRGBA8* b = (BitmapRGBA8*)calloc(sizeof(BitmapRGBA8), 1);
  14. if(!b) return NULL;
  15. ret = readPNG2(path, b);
  16. if(ret) {
  17. if(b->data) free(b->data);
  18. free(b);
  19. return NULL;
  20. }
  21. return b;
  22. }
  23. int readPNG2(char* path, BitmapRGBA8* bmp) {
  24. FILE* f;
  25. png_byte sig[8];
  26. png_bytep* rowPtrs;
  27. int i;
  28. png_byte colorType;
  29. png_byte bitDepth;
  30. f = fopen(path, "rb");
  31. if(!f) {
  32. fprintf(stderr, "Could not open \"%s\" (readPNG).\n", path);
  33. return 1;
  34. }
  35. fread(sig, 1, 8, f);
  36. if(png_sig_cmp(sig, 0, 8)) {
  37. fprintf(stderr, "\"%s\" is not a valid PNG file.\n", path);
  38. fclose(f);
  39. return 1;
  40. }
  41. png_structp readStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  42. if (!readStruct) {
  43. fprintf(stderr, "Failed to load \"%s\". readPNG Error 1.\n", path);
  44. fclose(f);
  45. return 1;
  46. }
  47. png_infop infoStruct = png_create_info_struct(readStruct);
  48. if (!infoStruct) {
  49. fprintf(stderr, "Failed to load \"%s\". readPNG Error 2.\n", path);
  50. png_destroy_read_struct(&readStruct, (png_infopp)0, (png_infopp)0);
  51. fclose(f);
  52. return 1;
  53. };
  54. bmp->path = path;
  55. // exceptions are evil. the alternative with libPNG is a bit worse. ever heard of return codes libPNG devs?
  56. if (setjmp(png_jmpbuf(readStruct))) {
  57. fprintf(stderr, "Failed to load \"%s\". readPNG Error 3.\n", path);
  58. png_destroy_read_struct(&readStruct, (png_infopp)0, (png_infopp)0);
  59. if(bmp->data) free(bmp->data);
  60. free(bmp);
  61. fclose(f);
  62. return 1;
  63. }
  64. png_init_io(readStruct, f);
  65. png_set_sig_bytes(readStruct, 8);
  66. png_read_info(readStruct, infoStruct);
  67. bmp->width = png_get_image_width(readStruct, infoStruct);
  68. bmp->height = png_get_image_height(readStruct, infoStruct);
  69. // coerce the fileinto 8-bit RGBA
  70. bitDepth = png_get_bit_depth(readStruct, infoStruct);
  71. colorType = png_get_color_type(readStruct, infoStruct);
  72. if(colorType == PNG_COLOR_TYPE_PALETTE) {
  73. png_set_palette_to_rgb(readStruct);
  74. }
  75. if(colorType == PNG_COLOR_TYPE_GRAY) {
  76. png_set_expand_gray_1_2_4_to_8(readStruct);
  77. png_set_expand(readStruct);
  78. }
  79. if(bitDepth == 16) {
  80. png_set_strip_16(readStruct);
  81. }
  82. if(bitDepth < 8) {
  83. png_set_packing(readStruct);
  84. }
  85. if(png_get_valid(readStruct, infoStruct, PNG_INFO_tRNS)) {
  86. png_set_tRNS_to_alpha(readStruct);
  87. }
  88. if(colorType == PNG_COLOR_TYPE_GRAY) {
  89. png_set_gray_to_rgb(readStruct);
  90. png_set_expand(readStruct);
  91. }
  92. if(colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
  93. png_set_gray_to_rgb(readStruct);
  94. }
  95. // if(colorType > PNG_COLOR_TYPE_PALETTE) {
  96. // png_color_16 background = {.red= 255, .green = 255, .blue = 255};
  97. // png_set_background(readStruct, &background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
  98. // }
  99. // png_set_filler(readStruct, 0x00, PNG_FILLER_AFTER);
  100. // printf("interlace: %d\n", png_set_interlace_handling(readStruct));
  101. png_read_update_info(readStruct, infoStruct);
  102. // read the data
  103. bmp->data = (uint32_t*)malloc(bmp->width * bmp->height * sizeof(uint32_t));
  104. rowPtrs = (png_bytep*)malloc(sizeof(png_bytep) * bmp->height);
  105. for(i = 0; i < bmp->height; i++) {
  106. rowPtrs[i] = (png_bytep)(bmp->data + (bmp->width * i));
  107. }
  108. png_read_image(readStruct, rowPtrs);
  109. free(rowPtrs);
  110. png_destroy_read_struct(&readStruct, (png_infopp)0, (png_infopp)0);
  111. fclose(f);
  112. // printf("Loaded \"%s\".\n", path);
  113. return 0;
  114. }
  115. static uint32_t average(BitmapRGBA8* bmp, Vector2 min, Vector2 max) {
  116. int w = bmp->width;
  117. int xmin, xmax, ymin, ymax;
  118. int x, y;
  119. union {
  120. uint32_t u;
  121. uint8_t b[4];
  122. } u;
  123. xmin = MAX(0, floor(min.x));
  124. xmax = MIN(bmp->width, ceil(max.x));
  125. ymin = MAX(0, floor(min.y));
  126. ymax = MIN(bmp->height, ceil(max.y));
  127. float r = 0.0;
  128. float g = 0.0;
  129. float b = 0.0;
  130. float a = 0.0;
  131. for(y = ymin; y < ymax; y++) {
  132. for(x = xmin; x < xmax; x++) {
  133. u.u = bmp->data[(int)(ceil(x) + (w * ceil(y)))];
  134. r += (float)u.b[0]; // these may not correspond properly to the component
  135. g += (float)u.b[1]; // but it doesn't matter as long as everything is
  136. b += (float)u.b[2]; // consistent across the function
  137. a += (float)u.b[3];
  138. }
  139. }
  140. float totalWeight = (xmax - xmin) * (ymax - ymin);
  141. r /= totalWeight;
  142. g /= totalWeight;
  143. b /= totalWeight;
  144. a /= totalWeight;
  145. u.b[0] = iclamp(r, 0, 255);
  146. u.b[1] = iclamp(g, 0, 255);
  147. u.b[2] = iclamp(b, 0, 255);
  148. u.b[3] = iclamp(a, 0, 255);
  149. return u.u;
  150. }
  151. static BitmapRGBA8* resample(BitmapRGBA8* in, Vector2i outSz) {
  152. int ox, oy;
  153. float scaleFactor = in->width / outSz.x;
  154. BitmapRGBA8* out;
  155. out = calloc(1, sizeof(out));
  156. out->width = outSz.x;
  157. out->height = outSz.y;
  158. out->data = malloc(out->width * out->height * sizeof(*out->data));
  159. // really shitty algorithm
  160. for(oy = 0; oy < outSz.y; oy++) {
  161. for(ox = 0; ox < outSz.x; ox++) {
  162. out->data[ox + (oy * outSz.x)] = average(
  163. in,
  164. (Vector2){ox * scaleFactor, oy * scaleFactor},
  165. (Vector2){ox * (scaleFactor + 1), oy * (scaleFactor + 1)}
  166. );
  167. }
  168. }
  169. return out;
  170. }
  171. // nearest resizing
  172. static uint32_t bmpsample(BitmapRGBA8* b, Vector2i p) {
  173. return b->data[iclamp(p.x, 0, b->width) + (iclamp(p.y, 0, b->height) * b->width)];
  174. }
  175. static uint32_t bmpsamplef(BitmapRGBA8* b, Vector2 p) {
  176. Vector2i pi = {floor(p.x + .5), floor(p.y + .5)};
  177. return bmpsample(b, pi);
  178. }
  179. static uint32_t bmpsamplescale(BitmapRGBA8* b, Vector2i p, float scale) {
  180. Vector2i pi = {floor((p.x * scale) + .5), floor((p.y * scale) + .5)};
  181. return bmpsample(b, pi);
  182. }
  183. static BitmapRGBA8* nearestRescale(BitmapRGBA8* in, Vector2i outSz) {
  184. int ox, oy;
  185. float scaleFactor = (float)outSz.x / (float)in->width;
  186. BitmapRGBA8* out;
  187. out = calloc(1, sizeof(out));
  188. out->width = outSz.x;
  189. out->height = outSz.y;
  190. out->data = malloc(out->width * out->height * sizeof(*out->data));
  191. for(oy = 0; oy < outSz.y; oy++) {
  192. for(ox = 0; ox < outSz.x; ox++) {
  193. out->data[ox + (oy * outSz.x)] = bmpsamplescale(in, (Vector2i){ox, oy}, 1.0f / scaleFactor);
  194. }
  195. }
  196. return out;
  197. }
  198. // linear rescaling
  199. static void unpack8(uint32_t v, uint32_t* r, uint32_t* g, uint32_t* b, uint32_t* a) {
  200. union {
  201. uint32_t n;
  202. uint8_t b[4];
  203. } u;
  204. u.n = v;
  205. *r = u.b[0];
  206. *g = u.b[1];
  207. *b = u.b[2];
  208. *a = u.b[3];
  209. }
  210. static void unpackadd8(uint32_t v, uint32_t* o) {
  211. union {
  212. uint32_t n;
  213. uint8_t b[4];
  214. } u;
  215. u.n = v;
  216. o[0] += u.b[0];
  217. o[1] += u.b[1];
  218. o[2] += u.b[2];
  219. o[3] += u.b[3];
  220. }
  221. static uint32_t repackdiv8(uint32_t* o, float divisor) {
  222. union {
  223. uint32_t n;
  224. uint8_t b[4];
  225. } u;
  226. u.b[0] = (float)o[0] / divisor;
  227. u.b[1] = (float)o[1] / divisor;
  228. u.b[2] = (float)o[2] / divisor;
  229. u.b[3] = (float)o[3] / divisor;
  230. return u.n;
  231. }
  232. static uint32_t bmplinsamplescale(BitmapRGBA8* b, Vector2i p, int scale) {
  233. int x, y;
  234. float invscale = (float)scale;
  235. Vector2i p0 = {p.x * invscale, p.y * invscale};
  236. uint32_t acc[4] = {0,0,0,0};
  237. for(y = 0; y < scale; y++) {
  238. for(x = 0; x < scale; x++) {
  239. unpackadd8(bmpsample(b, (Vector2i){p0.x + x, p0.y + y}), acc);
  240. }
  241. }
  242. return repackdiv8(acc, scale * scale);
  243. }
  244. static BitmapRGBA8* linearDownscale(BitmapRGBA8* in, Vector2i outSz) {
  245. int ox, oy;
  246. int scaleFactor = (float)in->width / (float)outSz.x;
  247. BitmapRGBA8* out;
  248. out = calloc(1, sizeof(out));
  249. out->width = outSz.x;
  250. out->height = outSz.y;
  251. out->data = malloc(out->width * out->height * sizeof(*out->data));
  252. for(oy = 0; oy < outSz.y; oy++) {
  253. for(ox = 0; ox < outSz.x; ox++) {
  254. out->data[ox + (oy * outSz.x)] = bmplinsamplescale(in, (Vector2i){ox, oy}, scaleFactor);
  255. }
  256. }
  257. return out;
  258. }
  259. // actually, the argument is a void**, but the compiler complains. stupid standards...
  260. size_t ptrlen(const void* a) {
  261. size_t i = 0;
  262. void** b = (void**)a;
  263. while(*b++) i++;
  264. return i;
  265. }
  266. static int depth_bytes[] = {
  267. [TEXDEPTH_8] = 1,
  268. [TEXDEPTH_16] = 2,
  269. [TEXDEPTH_32] = 4,
  270. [TEXDEPTH_FLOAT] = 4,
  271. [TEXDEPTH_DOUBLE] = 8,
  272. };
  273. TexBitmap* TexBitmap_create(int w, int h, enum TextureDepth d, int channels) {
  274. int dbytes;
  275. TexBitmap* bmp;
  276. if(d >= TEXDEPTH_MAXVALUE) return NULL;
  277. dbytes = depth_bytes[d];
  278. bmp = calloc(1, sizeof(*bmp));
  279. bmp->data8 = malloc(w * h * channels * dbytes);
  280. bmp->width = w;
  281. bmp->height = h;
  282. bmp->channels = channels;
  283. return bmp;
  284. }