Thread: float/double to integer?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    float/double to integer?

    Hi,

    I´m trying to write a program that does some color conversion, but I cannot get it to work right. It seem that that the compiler doesn´t like conversion between int and double. Here´s the code:

    Code:
    /* colorconvert.c */
    
    int rgb2yuv(int xrgb){
    
    	unsigned char r,g,b,y,u,v;
    	
    	r=(xrgb&&0x00FF0000)>>16;
    	g=(xrgb&&0x0000FF00)>>8;
    	b=(xrgb&&0x000000FF);
    
    	y=(unsigned char) (0.257*(double)r)+(0.504*(float)g)+(0.098*(float)b) + 16;
    	u=(unsigned char) -(0.148*(double)r)-(0.291*(float)g)+(0.439*(float)b) + 128;
    	v=(unsigned char) (0.439*(double)r)-(0.368*(float)g)-(0.071*(float)b) + 128;
    
    	return (y<<16+u<<8+v);
    }
    
    
    int yuv2rgb(int xyuv){ 	/* Actually xycrcb */
    
    	unsigned char r,g,b,y,u,v;
    
    	y=(xyuv&&0x00FF0000)>>16;
    	u=(xyuv&&0x0000FF00)>>8;
    	v=(xyuv&&0x000000FF);
    
    	r=(unsigned char) 1.164*(y-16)+1.596*(v-128);
    	g=(unsigned char) 1.164*(y-16)-0.813*(v-128)-0.391*(u-128);
    	b=(unsigned char) 1.164*(y-16)+2.018*(u-128);
    
    	return (r<<16+g<<8+b);
    }
    Any suggestions. I´ve written a sample main function if your interested:

    Code:
    /* main.cc */
    
    #include <iostream>
    using namespace std;
    
    #include <colorconvert.c>
    
    int main(){
    	int c1,c2,c3;
    	
    	c1=0x0045B391;
    	c2=0x001633A1;
    	c3=0x00D543F1;
    	
    	cout << hex << "c1=" << c1 << endl;
    	cout << hex << "c2=" << c2 << endl;
    	cout << hex << "c3=" << c3 << endl << endl;
    	
    	c1=rgb2yuv(c1);
    	c2=rgb2yuv(c2);
    	c3=rgb2yuv(c3);
    	
    	cout << hex << "c1=" << c1 << endl;
    	cout << hex << "c2=" << c2 << endl;
    	cout << hex << "c3=" << c3 << endl << endl;
    	
    	c1=yuv2rgb(c1);
    	c2=yuv2rgb(c2);
    	c3=yuv2rgb(c3);
    	
    	cout << hex << "c1=" << c1 << endl;
    	cout << hex << "c2=" << c2 << endl;
    	cout << hex << "c3=" << c3 << endl << endl;
    	
    }
    Any suggestions?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    y<<16+u<<8+v
    "+" has precedence over "<<" - use parens.

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Thanks, that solved one bug.

    The program, though, still doesn´t work correctly. Anymore suggestions?

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278

    Re: float/double to integer?

    Originally posted by Hardboy
    Code:
    	r=(unsigned char) 1.164*(y-16)+1.596*(v-128);
    	g=(unsigned char) 1.164*(y-16)-0.813*(v-128)-0.391*(u-128);
    	b=(unsigned char) 1.164*(y-16)+2.018*(u-128);
    Here, it appears that the 1.164 will be converted to unsigned char first. How about putting some brackets around the equation, and trying that out.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    JasonD gots some good eyes!

    Its a good programming habbit to just always use parens, even when you don't need em. Can make your code a more readable too (as well as correct!)

    gg

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Just found the serious bug.

    Í used logical AND instead of bitwise AND. Resulted in every byte converted to 1...

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Damn! That code would make a great exercise in debugging - no offense
    Too the 3 of us to flush em all out.

    gg

  8. #8
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by Codeplug
    Damn! That code would make a great exercise in debugging - no offense
    Too the 3 of us to flush em all out.
    You're right - it'd be a great exercise. To be honest, I stopped looking for errors after I found the type-casting error, but I also overlooked the && vs & operators, since that's the first place I looked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM