makedisttex.cpp 4.0 KB

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