12 unsigned int height,
unsigned int bpp) {
15 assert((bpp == 24) || (bpp == 32));
17 unsigned char* image =
new unsigned char[height * width * (bpp / 8)];
18 for (
unsigned int i = 0; i < (width * height); i++) {
19 image[i * (bpp / 8)] = (
unsigned char)(rgba.r * 255);
20 image[(i * (bpp / 8)) + 1] = (
unsigned char)(rgba.g * 255);
21 image[(i * (bpp / 8)) + 2] = (
unsigned char)(rgba.b * 255);
23 image[(i * (bpp / 8)) + 3] = (
unsigned char)(rgba.a * 255);
29 void argb2rgba32(
unsigned char* image,
unsigned int w,
unsigned int h) {
34 for (
unsigned int i = 0; i < (w * h); ++i) {
36 unsigned char swap = image[i * 4];
37 image[i * 4] = image[(i * 4) + 1];
38 image[(i * 4) + 1] = image[(i * 4) + 2];
39 image[(i * 4) + 2] = image[(i * 4) + 3];
40 image[(i * 4) + 3] = swap;
44 unsigned char*
argb16to32(
unsigned char* image,
unsigned int w,
unsigned int h) {
49 unsigned char* img =
new unsigned char[w * h * 4];
50 for (
unsigned int i = 0; i < (w * h); ++i) {
52 img[i * 4] = (image[(i * 2) + 1] & 0x80) ? 0xFF : 0;
53 img[(i * 4) + 1] = (image[(i * 2) + 1] & 0x7C) >> 2;
54 img[(i * 4) + 2] = (image[(i * 2) + 1] & 0x03) << 3;
55 img[(i * 4) + 2] |= (image[i * 2] & 0xE0) >> 5;
56 img[(i * 4) + 3] = image[i * 2] & 0x1F;
58 img[(i * 4) + 1] <<= 3;
59 img[(i * 4) + 2] <<= 3;
60 img[(i * 4) + 3] <<= 3;
65 unsigned char*
grayscale2rgba(
unsigned char* image,
unsigned int w,
unsigned int h) {
70 unsigned char* img =
new unsigned char[w * h * 4];
71 for (
unsigned int i = 0; i < (w * h); i++) {
72 img[i * 4] = image[i];
73 img[(i * 4) + 1] = image[i];
74 img[(i * 4) + 2] = image[i];
75 img[(i * 4) + 3] = (image[i] == 0) ? 0 : 255;
80 #define NEXT_POWER(x) do { \
82 for (i = 1; i < (x); i *= 2); \
87 unsigned char*
scaleBuffer(
unsigned char* image,
unsigned int* w,
unsigned int* h,
89 unsigned int width = *w;
90 unsigned int height = *h;
96 unsigned int components = bpp / 8;
97 unsigned int original_height = height;
98 unsigned int original_width = width;
104 if (height == original_height && width == original_width)
110 unsigned char* timage =
new unsigned char[height * width * components];
111 float* tempin =
new float[original_width * original_height * components];
112 float* tempout =
new float[width * height * components];
115 for (
unsigned int i = 0; i < original_height * original_width * components; ++i) {
116 tempin[i] = (float)image[i];
122 sx = (float)(original_width - 1) / (float)(width - 1);
124 sx = (float)(original_width - 1);
129 sy = (float)(original_height - 1) / (float)(height - 1);
131 sy = (float)(original_height - 1);
134 if (sx < 1.0 && sy < 1.0) {
135 for (
unsigned int i = 0; i < height; ++i) {
136 unsigned int i0 = (
unsigned int)(i * sy);
137 unsigned int i1 = i0 + 1;
139 if (i1 >= original_height) {
140 i1 = original_height - 1;
143 float alpha = i * sy - i0;
145 for (
unsigned int j = 0; j < width; ++j) {
146 unsigned int j0 = (
unsigned int)(j * sx);
147 unsigned int j1 = j0 + 1;
149 if (j1 >= original_width) {
150 j1 = original_width - 1;
153 float beta = j * sx - j0;
156 float* src00 = tempin + (i0 * original_width + j0) * components;
157 float* src01 = tempin + (i0 * original_width + j1) * components;
158 float* src10 = tempin + (i1 * original_width + j0) * components;
159 float* src11 = tempin + (i1 * original_width + j1) * components;
161 float* dst = tempout + (i * width + j) * components;
163 for (
unsigned int k = 0; k < components; ++k) {
164 float s1 = *src00++ * (1.0f - beta) + *src01++ * beta;
165 float s2 = *src10++ * (1.0f - beta) + *src11++ * beta;
166 *dst++ = s1 * (1.0f - alpha) + s2 * alpha;
171 for (
unsigned int i = 0; i < height; ++i) {
172 unsigned int i0 = (
unsigned int)(i * sy);
173 unsigned int i1 = i0 + 1;
175 if (i1 >= original_height) {
176 i1 = original_height - 1;
179 for (
unsigned int j = 0; j < width; ++j) {
180 unsigned int j0 = (
unsigned int)(j * sx);
181 unsigned int j1 = j0 + 1;
183 if (j1 >= original_width) {
184 j1 = original_width - 1;
187 float* dst = tempout + (i * width + j) * components;
190 for (
unsigned int k = 0; k < components; ++k) {
193 for (
unsigned int ii = i0; ii <= i1; ++ii) {
194 for (
unsigned int jj = j0; jj <= j1; ++jj) {
195 sum += *(tempin + (ii * original_width + jj)
200 sum /= (j1 - j0 + 1) * (i1 - i0 + 1);
208 for (
unsigned int i = 0; i < height * width * components; ++i) {
209 timage[i] = (
unsigned char)tempout[i];
unsigned char * argb16to32(unsigned char *image, unsigned int w, unsigned int h)
unsigned char * scaleBuffer(unsigned char *image, unsigned int *w, unsigned int *h, unsigned int bpp)
unsigned char * grayscale2rgba(unsigned char *image, unsigned int w, unsigned int h)
void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h)
unsigned char * generateColorTexture(glm::vec4 rgba, unsigned int width, unsigned int height, unsigned int bpp)