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 6.8KB

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