445,797 Members | 1,836 Online Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

# problems with data type conversion (maybe)

 P: n/a 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 values:(keep 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 bmp[ptr]) { min = bmp[ptr]; } } then i normalize everything to 255: // NORMALIZE VALUES for (int ptr=0; ptr
8 Replies

 P: n/a "CAFxX" wrote in message news:bo**********@news.wplus.net... // FIND HIGHEST/LOWEST VALUES TO BE USED IN 0-255 NORMALIZATION max = 0; min = 0-1; 0-1? How about just -1? And unless min is unsigned, you don't even want to do that. Th easiest way is just to seed min and max with the first pixel value. min = bmp; max = bmp for(int ptr = 1; ... // NORMALIZE VALUES for (int ptr=0; ptr

 P: n/a this is the complete source of my program. in my first post i forgot to paste the line max -= min; but it was already there. are your instructions still valid? thanks for your help. #include #include #include "Simplify_private.h" // DEV-CPP version informations using namespace std; // GLOBALs unsigned short c; //Components counter unsigned int x; //X-axis pixel coordinate counter unsigned int y; //Y-axis pixel coordinate counter int rx; //X-axis spreading distance counter int ry; //Y-axis spreading distance counter unsigned int w; //Source image width unsigned int h; //Source image height unsigned short s; //Spreading factor unsigned long max; //Normalizing MAX value unsigned long min = 0-1; //Normalizing MIN value unsigned long dummybytes; //Number of dummy bytes per line unsigned long ptr; //*bmp array pseudo-pointer FILE *fp1; //Input file pointer FILE *fp2; //Output file pointer unsigned long *bmp; //Memory pointer void test() { fseek (fp1, 54, SEEK_SET); // BLURRING LOOP - WORKING for (y=0; y= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] += byte; } } } } } fseek(fp1, dummybytes, SEEK_CUR); } fseek(fp1, 54, SEEK_SET); for (ptr=0; ptr bmp[ptr]) { min = bmp[ptr]; } } printf ("%d %d\n", max, min); max -= min; // NORMALIZE VALUES for (ptr=w*3; ptr bmp[ptr]) { min = bmp[ptr]; } } printf ("%d %d\n", max, min); // DEBLURRING LOOP - WORKING for (y=0; y= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] -= bmp[(x+y*w)*3+c]; } } } } } } // OUTPUTTING LOOPs // COPY SOURCE IMAGE BMP HEADER - WORKING fseek(fp1, 0, SEEK_SET); for (ptr=0; ptr<54; ptr++) { fputc(fgetc(fp1),fp2); } // WRITE DATA CONTAINED IN THE BMP ARRAY IN THE SAME ORDER THEY WERE READ - WORKING for (ptr=0; ptr ha scritto nel messaggio news:3f*********************@news.newshosting.com. .. "CAFxX" wrote in message news:bo**********@news.wplus.net... // FIND HIGHEST/LOWEST VALUES TO BE USED IN 0-255 NORMALIZATION max = 0; min = 0-1; 0-1? How about just -1? And unless min is unsigned, you don't even want to do that. Th easiest way is just to seed min and max with the first pixel value. min = bmp; max = bmp for(int ptr = 1; ... // NORMALIZE VALUES for (int ptr=0; ptr

 P: n/a i really can't understand what's wrong in here. i placed this code for (ptr=0; ptr255) { printf ("%7d: %12d\n", ptr, (unsigned int)bmp[ptr]); } } after every loop to track the memory contents and i discovered that normalizing and denormalizing are working. but what i can't understand is that if i place it after the deblurring loop i get huge and even negatives (even if bmp[] is unsigned long) values! what's funny is that if i turn off the normalization/denormalization process, the blurring/deblurring filters work correctly. in case, this is the source (try to feed it a 24 bit uncompressed bmp file with just a coloured text on a flat white bg): // Simplify 0.0.7 // (C) 2003 CAFxX #include #include #include "Simplify_private.h" // DEV-CPP version informations using namespace std; // GLOBALs unsigned short c; //Components counter unsigned int x; //X-axis pixel coordinate counter unsigned int y; //Y-axis pixel coordinate counter int rx; //X-axis spreading distance counter int ry; //Y-axis spreading distance counter unsigned int w; //Source image width unsigned int h; //Source image height unsigned short s; //Spreading factor unsigned long max; //Normalizing MAX value unsigned long min; //Normalizing MIN value unsigned long dummybytes; //Number of dummy bytes per line unsigned long ptr; //*bmp array pseudo-pointer FILE *fp1; //Input file pointer FILE *fp2; //Output file pointer unsigned long *bmp; //Memory pointer void test() { if ((fp1 = fopen("test.bmp", "rb")) == NULL) { exit(2); } if ((fp2 = fopen("out.bmp", "wb")) == NULL) { exit(1); } fseek (fp1, 54, SEEK_SET); // BLURRING LOOP - WORKING for (y=0; y= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] += byte; } } } } } fseek(fp1, dummybytes, SEEK_CUR); } fseek(fp1, 54, SEEK_SET); for (ptr=0; ptr bmp[ptr]) { min = bmp[ptr]; } } printf ("%10d %10d\n", max, min); max -= min; // NORMALIZE VALUES - WORKKING for (ptr=w*3; ptr>= 8; // it should be /255, but i think it will work bmp[ptr] += min; } // DEBLURRING LOOP - WORKING for (y=0; y= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] -= bmp[(x+y*w)*3+c]; } } } } } } // OUTPUTTING LOOPs // COPY SOURCE IMAGE BMP HEADER - WORKING fseek(fp1, 0, SEEK_SET); for (ptr=0; ptr<54; ptr++) { fputc(fgetc(fp1),fp2); } // WRITE DATA CONTAINED IN THE BMP ARRAY IN THE SAME ORDER THEY WERE READ - WORKING for (ptr=0; ptr "); scanf("%d",&w); if (w <= 0) { exit(0); } // EXIT IF IMAGE DIMENSIONS ARE NOT VALID printf("Source image height > "); scanf("%d",&h); if (h <= 0) { exit(0); } // COLLECTING ENCODING SETTINGS printf("Spreading factor > "); scanf("%d",&s); // DUMMY BYTES PER LINE if ((fp1 = fopen("test.bmp", "rb")) == NULL) { exit(2); } dummybytes = (flen(fp1) - h * w * 3 + 54) / h; fclose(fp1); // ALLOCATE MEMORY AND START PROCESSING bmp = (unsigned long*)malloc(sizeof(unsigned long)*w*h*3); if (bmp == NULL) { exit(3); } test(); free(bmp); system ("pause"); return 0; } else { printf ("Wrong parameter number"); exit(4); } } thanks in advance to anyone that will help. "CAFxX" ha scritto nel messaggio news:bo**********@news.wplus.net... 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 values:(keep 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 bmp[ptr]) { min = bmp[ptr]; } } then i normalize everything to 255: // NORMALIZE VALUES for (int ptr=0; ptr

 P: n/a hmmm, posted the wrong loop i really can't understand what's wrong in here. i placed this code this is the right one: for (ptr=0; ptr255) { printf ("%7d: %12d\n", ptr, (unsigned int)bmp[ptr]); } } "CAFxX" ha scritto nel messaggio news:bp***********@news.wplus.net... i really can't understand what's wrong in here. i placed this code for (ptr=0; ptr255) { printf ("%7d: %12d\n", ptr, (unsigned int)bmp[ptr]); } } after every loop to track the memory contents and i discovered that normalizing and denormalizing are working. but what i can't understand is that if i place it after the deblurring loop i get huge and even negatives (even if bmp[] is unsigned long) values! what's funny is that if i turn off the normalization/denormalization process, the blurring/deblurring filters work correctly. in case, this is the source (try to feed it a 24 bit uncompressed bmp file with just a coloured text on a flat white bg): // Simplify 0.0.7 // (C) 2003 CAFxX #include #include #include "Simplify_private.h" // DEV-CPP version informations using namespace std; // GLOBALs unsigned short c; //Components counter unsigned int x; //X-axis pixel coordinate counter unsigned int y; //Y-axis pixel coordinate counter int rx; //X-axis spreading distance counter int ry; //Y-axis spreading distance counter unsigned int w; //Source image width unsigned int h; //Source image height unsigned short s; //Spreading factor unsigned long max; //Normalizing MAX value unsigned long min; //Normalizing MIN value unsigned long dummybytes; //Number of dummy bytes per line unsigned long ptr; //*bmp array pseudo-pointer FILE *fp1; //Input file pointer FILE *fp2; //Output file pointer unsigned long *bmp; //Memory pointer void test() { if ((fp1 = fopen("test.bmp", "rb")) == NULL) { exit(2); } if ((fp2 = fopen("out.bmp", "wb")) == NULL) { exit(1); } fseek (fp1, 54, SEEK_SET); // BLURRING LOOP - WORKING for (y=0; y= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] += byte; } } } } } fseek(fp1, dummybytes, SEEK_CUR); } fseek(fp1, 54, SEEK_SET); for (ptr=0; ptr bmp[ptr]) { min = bmp[ptr]; } } printf ("%10d %10d\n", max, min); max -= min; // NORMALIZE VALUES - WORKKING for (ptr=w*3; ptr>= 8; // it should be /255, but i think it will work bmp[ptr] += min; } // DEBLURRING LOOP - WORKING for (y=0; y= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] -= bmp[(x+y*w)*3+c]; } } } } } } // OUTPUTTING LOOPs // COPY SOURCE IMAGE BMP HEADER - WORKING fseek(fp1, 0, SEEK_SET); for (ptr=0; ptr<54; ptr++) { fputc(fgetc(fp1),fp2); } // WRITE DATA CONTAINED IN THE BMP ARRAY IN THE SAME ORDER THEY WERE READ - WORKING for (ptr=0; ptr "); scanf("%d",&w); if (w <= 0) { exit(0); } // EXIT IF IMAGE DIMENSIONS ARE NOT VALID printf("Source image height > "); scanf("%d",&h); if (h <= 0) { exit(0); } // COLLECTING ENCODING SETTINGS printf("Spreading factor > "); scanf("%d",&s); // DUMMY BYTES PER LINE if ((fp1 = fopen("test.bmp", "rb")) == NULL) { exit(2); } dummybytes = (flen(fp1) - h * w * 3 + 54) / h; fclose(fp1); // ALLOCATE MEMORY AND START PROCESSING bmp = (unsigned long*)malloc(sizeof(unsigned long)*w*h*3); if (bmp == NULL) { exit(3); } test(); free(bmp); system ("pause"); return 0; } else { printf ("Wrong parameter number"); exit(4); } } thanks in advance to anyone that will help. "CAFxX" ha scritto nel messaggio news:bo**********@news.wplus.net... 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 values:(keep 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 bmp[ptr]) { min = bmp[ptr]; } } then i normalize everything to 255: // NORMALIZE VALUES for (int ptr=0; ptr

 P: n/a "CAFxX" wrote in message news:bp***********@news.wplus.net... if (bmp[ptr]<0 or bmp[ptr]>255) { printf ("%7d: %12d\n", ptr, (unsigned int)bmp[ptr]); } The unsigned int cast doesn't do anything useful. %d always expects an int. Jul 19 '05 #6

 P: n/a "CAFxX" wrote in message news:bp***********@news.wplus.net... for (ptr=w*3; ptr= w) or (y+ry >= h))) { // to avoid page fault bmp[(x+rx+(y+ry)*w)*3+c] -= bmp[(x+y*w)*3+c]; } } } } } The above looks bogus. Are you trying to implement an unsharp mask? You most likely need two buffers, the resultant output pixel is hosed by the modification you make. Jul 19 '05 #7 