Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pixel.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*!
  2. * \file src/utils/pixel.cpp
  3. * \brief Pixel buffer utilities
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "utils/pixel.h"
  9. unsigned char* generateColorTexture(const unsigned char* rgba, unsigned int width,
  10. unsigned int height, unsigned int bpp) {
  11. assert(rgba != nullptr);
  12. assert(width > 0);
  13. assert(height > 0);
  14. assert((bpp % 8) == 0);
  15. unsigned char* image = new unsigned char[height * width * (bpp / 8)];
  16. for (unsigned int i = 0; i < (width * height); i++) {
  17. for (unsigned int a = 0; a < (bpp / 8); a++) {
  18. image[(i * (bpp / 8)) + a] = rgba[a];
  19. }
  20. }
  21. return image;
  22. }
  23. void bgr2rgb24(unsigned char* image, unsigned int w, unsigned int h) {
  24. assert(image != nullptr);
  25. assert(w > 0);
  26. assert(h > 0);
  27. for (unsigned int i = 0; i < (w * h); ++i) {
  28. /* 24-bit BGR to RGB */
  29. unsigned char swap = image[(i * 3) + 2];
  30. image[(i * 3) + 2] = image[i * 3];
  31. image[i * 3] = swap;
  32. }
  33. }
  34. void bgra2rgba32(unsigned char* image, unsigned int w, unsigned int h) {
  35. assert(image != nullptr);
  36. assert(w > 0);
  37. assert(h > 0);
  38. for (unsigned int i = 0; i < (w * h); ++i) {
  39. /* 32-bit BGRA to RGBA */
  40. unsigned char swap = image[(i * 4) + 2];
  41. image[(i * 4) + 2] = image[(i * 4)];
  42. image[i * 4] = swap;
  43. }
  44. }
  45. void argb2rgba32(unsigned char* image, unsigned int w, unsigned int h) {
  46. assert(image != nullptr);
  47. assert(w > 0);
  48. assert(h > 0);
  49. for (unsigned int i = 0; i < (w * h); ++i) {
  50. // 32-bit ARGB to RGBA
  51. unsigned char swap = image[i * 4];
  52. image[i * 4] = image[(i * 4) + 1];
  53. image[(i * 4) + 1] = image[(i * 4) + 2];
  54. image[(i * 4) + 2] = image[(i * 4) + 3];
  55. image[(i * 4) + 3] = swap;
  56. }
  57. }
  58. unsigned char* argb16to32(unsigned char* image, unsigned int w, unsigned int h) {
  59. assert(image != nullptr);
  60. assert(w > 0);
  61. assert(h > 0);
  62. unsigned char* img = new unsigned char[w * h * 4];
  63. for (unsigned int i = 0; i < (w * h); ++i) {
  64. // arrr.rrgg gggb.bbbb shift to 5bit
  65. img[i * 4] = (image[(i * 2) + 1] & 0x80) ? 0xFF : 0; // A
  66. img[(i * 4) + 1] = (image[(i * 2) + 1] & 0x7C) >> 2; // R
  67. img[(i * 4) + 2] = (image[(i * 2) + 1] & 0x03) << 3;
  68. img[(i * 4) + 2] |= (image[i * 2] & 0xE0) >> 5; // G
  69. img[(i * 4) + 3] = image[i * 2] & 0x1F; // B
  70. img[(i * 4) + 1] <<= 3; // R
  71. img[(i * 4) + 2] <<= 3; // G
  72. img[(i * 4) + 3] <<= 3; // B
  73. }
  74. return img;
  75. }
  76. #define NEXT_POWER(x) do { \
  77. unsigned int i; \
  78. for (i = 1; i < (x); i *= 2); \
  79. (x) = i; \
  80. } while (false);
  81. // This code based off on gluScaleImage()
  82. unsigned char* scaleBuffer(unsigned char* image, unsigned int* w, unsigned int* h,
  83. unsigned int bpp) {
  84. unsigned int width = *w;
  85. unsigned int height = *h;
  86. assert(image != nullptr);
  87. assert(width > 0);
  88. assert(height > 0);
  89. assert((bpp % 8) == 0);
  90. unsigned int components = bpp / 8;
  91. unsigned int original_height = height;
  92. unsigned int original_width = width;
  93. NEXT_POWER(height);
  94. NEXT_POWER(width);
  95. // Check to see if scaling is needed
  96. if (height == original_height && width == original_width)
  97. return nullptr;
  98. *w = width;
  99. *h = height;
  100. unsigned char* timage = new unsigned char[height * width * components];
  101. float* tempin = new float[original_width * original_height * components];
  102. float* tempout = new float[width * height * components];
  103. // Copy user data to float format.
  104. for (unsigned int i = 0; i < original_height * original_width * components; ++i) {
  105. tempin[i] = (float)image[i];
  106. }
  107. // Determine which filter to use by checking ratios.
  108. float sx;
  109. if (width > 1) {
  110. sx = (float)(original_width - 1) / (float)(width - 1);
  111. } else {
  112. sx = (float)(original_width - 1);
  113. }
  114. float sy;
  115. if (height > 1) {
  116. sy = (float)(original_height - 1) / (float)(height - 1);
  117. } else {
  118. sy = (float)(original_height - 1);
  119. }
  120. if (sx < 1.0 && sy < 1.0) { // Magnify both width and height: use weighted sample of 4 pixels
  121. for (unsigned int i = 0; i < height; ++i) {
  122. unsigned int i0 = (unsigned int)(i * sy);
  123. unsigned int i1 = i0 + 1;
  124. if (i1 >= original_height) {
  125. i1 = original_height - 1;
  126. }
  127. float alpha = i * sy - i0;
  128. for (unsigned int j = 0; j < width; ++j) {
  129. unsigned int j0 = (unsigned int)(j * sx);
  130. unsigned int j1 = j0 + 1;
  131. if (j1 >= original_width) {
  132. j1 = original_width - 1;
  133. }
  134. float beta = j * sx - j0;
  135. // Compute weighted average of pixels in rect (i0,j0)-(i1,j1)
  136. float* src00 = tempin + (i0 * original_width + j0) * components;
  137. float* src01 = tempin + (i0 * original_width + j1) * components;
  138. float* src10 = tempin + (i1 * original_width + j0) * components;
  139. float* src11 = tempin + (i1 * original_width + j1) * components;
  140. float* dst = tempout + (i * width + j) * components;
  141. for (unsigned int k = 0; k < components; ++k) {
  142. float s1 = *src00++ * (1.0f - beta) + *src01++ * beta;
  143. float s2 = *src10++ * (1.0f - beta) + *src11++ * beta;
  144. *dst++ = s1 * (1.0f - alpha) + s2 * alpha;
  145. }
  146. }
  147. }
  148. } else { // Shrink width and/or height: use an unweighted box filter
  149. for (unsigned int i = 0; i < height; ++i) {
  150. unsigned int i0 = (unsigned int)(i * sy);
  151. unsigned int i1 = i0 + 1;
  152. if (i1 >= original_height) {
  153. i1 = original_height - 1;
  154. }
  155. for (unsigned int j = 0; j < width; ++j) {
  156. unsigned int j0 = (unsigned int)(j * sx);
  157. unsigned int j1 = j0 + 1;
  158. if (j1 >= original_width) {
  159. j1 = original_width - 1;
  160. }
  161. float* dst = tempout + (i * width + j) * components;
  162. // Compute average of pixels in the rectangle (i0,j0)-(i1,j1)
  163. for (unsigned int k = 0; k < components; ++k) {
  164. float sum = 0.0;
  165. for (unsigned int ii = i0; ii <= i1; ++ii) {
  166. for (unsigned int jj = j0; jj <= j1; ++jj) {
  167. sum += *(tempin + (ii * original_width + jj)
  168. * components + k);
  169. }
  170. }
  171. sum /= (j1 - j0 + 1) * (i1 - i0 + 1);
  172. *dst++ = sum;
  173. }
  174. }
  175. }
  176. }
  177. // Copy to our results.
  178. for (unsigned int i = 0; i < height * width * components; ++i) {
  179. timage[i] = (unsigned char)tempout[i];
  180. }
  181. // Delete our temp buffers.
  182. delete [] tempin;
  183. delete [] tempout;
  184. return timage;
  185. }