Thread: Switching to RGB from GRB

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    35

    Switching to RGB from GRB

    I'm using this code:

    Code:
    for (imageidx = 0; imageidx < bmpinfo->biSizeImage; imageidx+=1)
    {
    	swaprgb = bmpimage[imageidx];
    	bmpimage[imageidx] = bmpimage[imageidx + 1];
    	bmpimage[imageidx + 1] = swaprgb;
    }
    It's fully working, but red is displayed as green.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    It's fully working
    but red is displayed as green
    Contradiction...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Code:
    swaprgb = bmpimage[imageidx + 1];
    bmpimage[imageidx + 1] = bmpimage[imageidx];
    bmpimage[imageidx] = swaprgb;
    ???

    Unless I'm just completely missing what your trying to do.

    Try explaining more.
    What is C++?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yeah I was going to help...but I'm lost.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    It's fully working, but red is displayed as green.
    ROFLOL

  6. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by Bubba
    Yeah I was going to help...but I'm lost.
    lol.

    I just cant figure out what hes doing.

    Maybe if you post some more code?
    What is C++?

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I guess he's looping through all bytes in a 24bit GRB buffer to change them to RGB. In that case, these are some neccessary changes:
    Code:
    for (imageidx = 0; imageidx < bmpinfo->biSizeImage / 3; imageidx+=1)
    {
    	swaprgb = bmpimage[imageidx * 3];
    	bmpimage[imageidx] = bmpimage[imageidx * 3 + 1];
    	bmpimage[imageidx * 3 + 1] = swaprgb;
    }
    (assuming biSizeImage is the number of bytes in the buffer, not the number of pixels)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    lol

    Magos, that's not working. Here's my code, hope you guys can help me out:

    Code:
    unsigned char *loadbmp(char *filename, BITMAPINFOHEADER *bmpinfo)
    {
    	FILE *fileptr;
    	BITMAPFILEHEADER bmpfile;
    	unsigned char *bmpimage;
    	int imageidx = 0;
    	unsigned char swaprgb;
    
    	fileptr = fopen(filename, "rb");
    	if (fileptr == NULL){return 0;}
    
    	fread(&bmpfile, sizeof(BITMAPFILEHEADER), 1, fileptr);
    	if (bmpfile.bfType != BITMAP_ID)
    	{
    		fclose(fileptr);
    		return 0;
    	}
    
    	fread(bmpinfo, sizeof(BITMAPINFOHEADER), 1, fileptr);
    
    	fseek(fileptr, bmpfile.bfOffBits, SEEK_SET);
    
    	bmpimage=(unsigned char*)malloc(bmpinfo->biSizeImage);
    	if (!bmpimage)
    	{
    		free(bmpimage);
    		fclose(fileptr);
    		return 0;
    	}
    
    	fread(bmpimage, 1, bmpinfo->biSizeImage, fileptr);
    	if (bmpimage == NULL)
    	{
    		fclose(fileptr);
    		return 0;
    	}
    
       for (imageidx = 0; imageidx < bmpinfo->biSizeImage / 3; imageidx+=1)
       {
    	    swaprgb = bmpimage[imageidx * 3];
    	    bmpimage[imageidx] = bmpimage[imageidx * 3 + 1];
    	    bmpimage[imageidx * 3 + 1] = swaprgb;
       }
    
    	fclose(fileptr);
    	return bmpimage;
    }

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Code:
    	if (!bmpimage)
    	{
    		free(bmpimage);
    free(NULL); doesn't do anything useful for mankind, watch out for it.
    hello, internet!

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Also be advised that bmps do not store their data as GRB but BGR.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    So that would be:

    Code:
    for (imageidx = 0; imageidx < bmpinfo->biSizeImage; imageidx+=3)	{
    	swaprgb = bmpimage[imageidx];
    	bmpimage[imageidx] = bmpimage[imageidx + 2];
    	bmpimage[imageidx + 2] = swaprgb;
    }
    Which doesn't work. Here's a screenie of the program,
    http://www.qbnz.com/ForgedQB/Screenie.bmp
    And the texture:
    http://www.qbnz.com/ForgedQB/HQ.bmp

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    This would swap GRB to RGB.

    Code:
        for (imageidx = 0; imageidx < bmpinfo->biSizeImage / 3; imageidx+=1)
           {
        	    swaprgb= bmpimage[imageidx *3 +0];
        	    bmpimage[imageidx *3 +0]= bmpimage[imageidx *3  +1];
        	    bmpimage[imageidx *3 +1]= swaprgb;
           }
    Oh, and yeah, I know +0 doesn't do anything. But it makes it easier to read which seems to be the main problem in this case. Every compiler worth a cent will optimize it away.

    This will...

    1) store red
    2) overwrite red with green
    3) replace green with stored red value

    You could also remove all "*3" and use for (imageidx= 0; imageidx < bmpinfo->biSizeImage; imageidx+=3). Best sollution would probably to use a struct with char r,g,b and cast your bytearray to an array of that struct with one third the indexcount.
    Last edited by Nyda; 09-27-2004 at 09:07 AM.

  13. #13
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    Same result as in the screenie... I guess it's just my code

  14. #14
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by HQSneaker
    Same result as in the screenie... I guess it's just my code
    Unfortunatly my browser only supports webstandard images. If it's only that the wrong channels are swapped you should be able to correct that issue.

  15. #15
    Registered User
    Join Date
    Jul 2004
    Posts
    35
    It's a dark spectrum, but you're able to see the texture throught it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting RGB to Hex.
    By mintsmike in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 12:22 PM
  2. OpenGL: Pixel addition
    By Hunter2 in forum Game Programming
    Replies: 4
    Last Post: 12-15-2008, 02:36 PM
  3. Stopwatch program (need help!)
    By modnar in forum C Programming
    Replies: 9
    Last Post: 03-22-2004, 12:42 AM
  4. Background color in RGB
    By bennyandthejets in forum Windows Programming
    Replies: 16
    Last Post: 06-20-2003, 05:29 AM
  5. Switching frequency and Bandwidth???
    By pastecopy in forum Tech Board
    Replies: 0
    Last Post: 03-23-2003, 11:13 AM