C
CAFxX
i'm writing a program that executes some calculations on a bitmap loaded in
memory.
these calculation ends up with pixel wth values far over 255, but i need
them to be between 0 and 255 since i got to write them in a bmp file (in
which RGB values are limited to 8 bits per channel). so i need to have them
scaled down.
first of all i find the highest and lowest of those valueskeep in mind
that max, min, bmp[], ptr are ulong and w, h are uint):
// FIND HIGHEST/LOWEST VALUES TO BE USED IN 0-255 NORMALIZATION
max = 0; min = 0-1;
for (int ptr=0; ptr<w*h*3; ptr++) {
if (max < bmp[ptr]) {
max = bmp[ptr];
} else if (min > bmp[ptr]) {
min = bmp[ptr];
}
}
then i normalize everything to 255:
// NORMALIZE VALUES
for (int ptr=0; ptr<w*h*3; ptr++) {
bmp[ptr] = (unsigned long)((float)(bmp[ptr] - min) / (float)max *
(float)255);
}
at this point, everything is alright. if i write in a bmp file the contents
of the array i get what i expected.
but as soon as i denormalize and apply the inverse of the calculations i did
before (i tested them separately: without normalization/denormalization they
work fine)
//DENORMALIZE VALUES
for (int ptr=0; ptr<w*h*3; ptr++) {
bmp[ptr] = (unsigned long)((float)bmp[ptr] / (float)255 * (float)max) + min;
}
then i write down the content of the array:
// OUTPUTTING LOOPs
// COPY SOURCE IMAGE BMP HEADER - WORKING
fseek(fp1, 0, SEEK_SET);
for (int i=0; i<54; i++) {
fputc(fgetc(fp1),fp2);
}
// WRITE DATA CONTAINED IN THE BMP ARRAY IN THE SAME ORDER THEY WERE READ -
WORKING
for (int ptr=0; ptr<w*h*3; ptr++) {
fputc ((unsigned int)bmp[ptr], fp2);
}
but the bitmap i end up with it's a mess of random pixels.
i think i'm doing something wrong in the data type conversion of the
denormalization loop. anyone knows why? i do thank you in advance.
p.s. if you want the full source of the program go to
sourceforge.net/projects/simplify. the function you got to point at is void
test ()
memory.
these calculation ends up with pixel wth values far over 255, but i need
them to be between 0 and 255 since i got to write them in a bmp file (in
which RGB values are limited to 8 bits per channel). so i need to have them
scaled down.
first of all i find the highest and lowest of those valueskeep in mind
that max, min, bmp[], ptr are ulong and w, h are uint):
// FIND HIGHEST/LOWEST VALUES TO BE USED IN 0-255 NORMALIZATION
max = 0; min = 0-1;
for (int ptr=0; ptr<w*h*3; ptr++) {
if (max < bmp[ptr]) {
max = bmp[ptr];
} else if (min > bmp[ptr]) {
min = bmp[ptr];
}
}
then i normalize everything to 255:
// NORMALIZE VALUES
for (int ptr=0; ptr<w*h*3; ptr++) {
bmp[ptr] = (unsigned long)((float)(bmp[ptr] - min) / (float)max *
(float)255);
}
at this point, everything is alright. if i write in a bmp file the contents
of the array i get what i expected.
but as soon as i denormalize and apply the inverse of the calculations i did
before (i tested them separately: without normalization/denormalization they
work fine)
//DENORMALIZE VALUES
for (int ptr=0; ptr<w*h*3; ptr++) {
bmp[ptr] = (unsigned long)((float)bmp[ptr] / (float)255 * (float)max) + min;
}
then i write down the content of the array:
// OUTPUTTING LOOPs
// COPY SOURCE IMAGE BMP HEADER - WORKING
fseek(fp1, 0, SEEK_SET);
for (int i=0; i<54; i++) {
fputc(fgetc(fp1),fp2);
}
// WRITE DATA CONTAINED IN THE BMP ARRAY IN THE SAME ORDER THEY WERE READ -
WORKING
for (int ptr=0; ptr<w*h*3; ptr++) {
fputc ((unsigned int)bmp[ptr], fp2);
}
but the bitmap i end up with it's a mess of random pixels.
i think i'm doing something wrong in the data type conversion of the
denormalization loop. anyone knows why? i do thank you in advance.
p.s. if you want the full source of the program go to
sourceforge.net/projects/simplify. the function you got to point at is void
test ()