makedisttex.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <edtaa3/edtaa3func.h>
  10. #define STB_IMAGE_IMPLEMENTATION
  11. #include <stb/stb_image.c>
  12. #define BX_NAMESPACE 1
  13. #include <bx/bx.h>
  14. #include <bx/commandline.h>
  15. #include <bx/uint32_t.h>
  16. long int fsize(FILE* _file)
  17. {
  18. long int pos = ftell(_file);
  19. fseek(_file, 0L, SEEK_END);
  20. long int size = ftell(_file);
  21. fseek(_file, pos, SEEK_SET);
  22. return size;
  23. }
  24. void edtaa3(double* _img, uint16_t _width, uint16_t _height, double* _out)
  25. {
  26. uint32_t size = _width*_height;
  27. short* xdist = (short*)malloc(size*sizeof(short) );
  28. short* ydist = (short*)malloc(size*sizeof(short) );
  29. double* gx = (double*)malloc(size*sizeof(double) );
  30. double* gy = (double*)malloc(size*sizeof(double) );
  31. computegradient(_img, _width, _height, gx, gy);
  32. edtaa3(_img, gx, gy, _width, _height, xdist, ydist, _out);
  33. for (uint32_t ii = 0; ii < size; ++ii)
  34. {
  35. if (_out[ii] < 0.0)
  36. {
  37. _out[ii] = 0.0;
  38. }
  39. }
  40. free(xdist);
  41. free(ydist);
  42. free(gx);
  43. free(gy);
  44. }
  45. void saveTga(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, bool _grayscale, const void* _data)
  46. {
  47. FILE* file = fopen(_filePath, "wb");
  48. if ( NULL != file )
  49. {
  50. uint8_t type = _grayscale ? 3 : 2;
  51. uint8_t bpp = _grayscale ? 8 : 32;
  52. uint8_t xorig = 0;
  53. uint8_t yorig = 0;
  54. putc(0, file);
  55. putc(0, file);
  56. putc(type, file);
  57. putc(0, file);
  58. putc(0, file);
  59. putc(0, file);
  60. putc(0, file);
  61. putc(0, file);
  62. putc(0, file);
  63. putc(xorig, file);
  64. putc(0, file);
  65. putc(yorig, file);
  66. putc(_width&0xff, file);
  67. putc( (_width>>8)&0xff, file);
  68. putc(_height&0xff, file);
  69. putc( (_height>>8)&0xff, file);
  70. putc(bpp, file);
  71. putc(32, file);
  72. uint32_t width = _width * bpp / 8;
  73. uint8_t* data = (uint8_t*)_data;
  74. for (uint32_t yy = 0; yy < _height; ++yy)
  75. {
  76. fwrite(data, width, 1, file);
  77. data += _pitch;
  78. }
  79. fclose(file);
  80. }
  81. }
  82. inline double min(double _a, double _b)
  83. {
  84. return _a > _b ? _b : _a;
  85. }
  86. inline double max(double _a, double _b)
  87. {
  88. return _a > _b ? _a : _b;
  89. }
  90. inline double clamp(double _val, double _min, double _max)
  91. {
  92. return max(min(_val, _max), _min);
  93. }
  94. inline double saturate(double _val)
  95. {
  96. return clamp(_val, 0.0, 1.0);
  97. }
  98. int main(int _argc, const char* _argv[])
  99. {
  100. bx::CommandLine cmdLine(_argc, _argv);
  101. const char* inFilePath = cmdLine.findOption('i');
  102. if (NULL == inFilePath)
  103. {
  104. fprintf(stderr, "Input file name must be specified.\n");
  105. return EXIT_FAILURE;
  106. }
  107. const char* outFilePath = cmdLine.findOption('o');
  108. if (NULL == outFilePath)
  109. {
  110. fprintf(stderr, "Output file name must be specified.\n");
  111. return EXIT_FAILURE;
  112. }
  113. double edge = 16.0;
  114. const char* edgeOpt = cmdLine.findOption('e');
  115. if (NULL != edgeOpt)
  116. {
  117. edge = atof(edgeOpt);
  118. }
  119. int width;
  120. int height;
  121. int comp;
  122. stbi_uc* img = stbi_load(inFilePath, &width, &height, &comp, 1);
  123. if (NULL == img)
  124. {
  125. fprintf(stderr, "Failed to load %s.\n", inFilePath);
  126. return EXIT_FAILURE;
  127. }
  128. uint32_t size = width*height;
  129. double* imgIn = (double*)malloc(size*sizeof(double) );
  130. double* outside = (double*)malloc(size*sizeof(double) );
  131. double* inside = (double*)malloc(size*sizeof(double) );
  132. for (uint32_t ii = 0; ii < size; ++ii)
  133. {
  134. imgIn[ii] = double(img[ii])/255.0;
  135. }
  136. edtaa3(imgIn, width, height, outside);
  137. for (uint32_t ii = 0; ii < size; ++ii)
  138. {
  139. imgIn[ii] = 1.0 - imgIn[ii];
  140. }
  141. edtaa3(imgIn, width, height, inside);
  142. free(imgIn);
  143. uint8_t* grayscale = (uint8_t*)malloc(size);
  144. double edgeOffset = edge*0.5;
  145. double invEdge = 1.0/edge;
  146. for (uint32_t ii = 0; ii < size; ++ii)
  147. {
  148. double dist = saturate( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge);
  149. grayscale[ii] = 255-uint8_t(dist * 255.0);
  150. }
  151. free(inside);
  152. free(outside);
  153. saveTga(outFilePath, width, height, width, true, grayscale);
  154. free(grayscale);
  155. return EXIT_SUCCESS;
  156. }