Quote:
so I guess the problem is in my lookup table function. How do I correctly do this ?
I find nothing wrong with your scaling table. Of course you have to initilialize it (calling
scaling
) before calling
makeshift
.
Note you are assigning a
float
to an
unsigned char
. I believe your compiled warned you about. You actually don't need at all to use
float
s for the
pix_scaling
array.
[update]
I suppose there is something wrong in the code you didn't show.
The following program
#include <stdlib.h>
#include <stdio.h>
float pix_scaling [256];
static void scaling(void)
{
for (int i = 0; i < 256; i++)
pix_scaling[i] = (i * 0.5) + 25;
}
#define H 4
#define W 4
int main()
{
unsigned char image[H*W*4];
unsigned char image_scaled[H*W*4];
for (int y = 0; y < H; ++y)
{
for (int x = 0; x < W; ++x)
{
for (int c = 0; c < 3; ++c)
{
int i = y* W * 4 + x * 4 + c;
image[i] = (unsigned char)rand();
}
}
}
scaling();
unsigned char *p = image;
unsigned char *p1 = image_scaled;
float r,g,b;
for(int i = 0; i < H*W; i++)
{
r = pix_scaling[p[0]]; g = pix_scaling[p[1]];
b = pix_scaling[p[2]];
if (r < 0) *(p1+0) = 0;
else if(r > 255) *(p1+0) = 255;
else *(p1+0) = r;
if (g < 0) *(p1+1) = 0;
else if(g > 255) *(p1+1) = 255;
else *(p1+1) = g;
if (b < 0) *(p1+2) = 0;
else if(b > 255) *(p1+2) = 255;
else *(p1+2) = b;
p +=4;
p1 +=4;
}
for (int y = 0; y < H; ++y)
{
for (int x = 0; x < W; ++x)
{
int i = y* W * 4 + x * 4 ;
printf("(%d,%d), image { %d, %d, %d }, image_scaled {%d, %d, %d}\n", x, y, image[i], image[i+1], image[i+2], image_scaled[i], image_scaled[i+1], image_scaled[i+2]);
}
}
return 0;
}
produces:
(0,0), image { 103, 198, 105 }, image_scaled {76, 124, 77}
(1,0), image { 115, 81, 255 }, image_scaled {82, 65, 152}
(2,0), image { 74, 236, 41 }, image_scaled {62, 143, 45}
(3,0), image { 205, 186, 171 }, image_scaled {127, 118, 110}
(0,1), image { 242, 251, 227 }, image_scaled {146, 150, 138}
(1,1), image { 70, 124, 194 }, image_scaled {60, 87, 122}
(2,1), image { 84, 248, 27 }, image_scaled {67, 149, 38}
(3,1), image { 232, 231, 141 }, image_scaled {141, 140, 95}
(0,2), image { 118, 90, 46 }, image_scaled {84, 70, 48}
(1,2), image { 99, 51, 159 }, image_scaled {74, 50, 104}
(2,2), image { 201, 154, 102 }, image_scaled {125, 102, 76}
(3,2), image { 50, 13, 183 }, image_scaled {50, 31, 116}
(0,3), image { 49, 88, 163 }, image_scaled {49, 69, 106}
(1,3), image { 90, 37, 93 }, image_scaled {70, 43, 71}
(2,3), image { 5, 23, 88 }, image_scaled {27, 36, 69}
(3,3), image { 233, 94, 212 }, image_scaled {141, 72, 131}
[/update]