Chaos Project

Game Development => Sea of Code => Topic started by: Ryex on January 29, 2011, 04:31:24 am

Title: need help translating c code
Post by: Ryex on January 29, 2011, 04:31:24 am
so this c code here : http://pastebin.com/9DxJ3t7X (http://pastebin.com/9DxJ3t7X)
contains matrix manipulations the are supposed to do hue rotations on RGB color values. I've been trying to do hue rotations in python using pygame like so
Code: Python
class SurfaceHueChange(object):

    @staticmethod
    def change_hue(surface, hue):
        t = time.time()
        surface.lock()
        for x in range(surface.get_width()):
            for y in range(surface.get_height()):
                color = pygame.Color(*surface.get_at((x, y)))
                hsva = list(color.hsva)
                hsva[0] = (hsva[0] + float(hue)) % 360.0
                color.hsva = hsva
                surface.set_at((x, y), (color.r, color.b, color.g, color.a))
        surface.unlock()
        print time.time() - t

and while it "works" for some reason the hue changes will not line up with RMXP's hue change with the same angle rotation or for that matter any image editer I could get my hands on with the ability. I even got my hands on a c# program that did hue rotations of images. and I looked at the code and can't see a difference between what I'm doing here and what it did (this c# can be found here http://www.codeproject.com/KB/GDI-plus/HSLColorSpace.aspx?msg=2871940 (http://www.codeproject.com/KB/GDI-plus/HSLColorSpace.aspx?msg=2871940))

so I'm hoping that if I can figure out how this c code works and translate it to python the rotation will line up properly.

of course if any one can spot a flaw in by implementation that would be causing my problems that would be good too.
Title: Re: need help translating c code
Post by: Blizzard on January 29, 2011, 05:08:44 am
Only three things come to my mind right now.

1. You should check the documentation and make sure hsva[0] is actually going from 0 to 360 and not 0 to 100 or 0.0 to 1.0. Some systems have the tendency to impose their own ranges on various implementations because the developer was a lazy ass to adjust his way of thinking toward the standard.

2. It's possible that RMXP's hue changes towards the negative actually. So it should be "(hsva[0] - float(hue)) % 360.0". Just make sure you handle the overflow properly since you're going into negative.

3. Careful when using % on floats. You might want to look up if there is a fmod function for that. e.g. in C and C++ using % with a float will not work right.
Title: Re: need help translating c code
Post by: Ryex on January 29, 2011, 06:00:50 am
ya I checked in to the % on floats it works properly I didn't check on the range of hue though from the few values I printed out it seemed to be going from 0 to 360 though.

yep checked it dose in deed go to 360, also I checked the negative version and it is not right either
Title: Re: need help translating c code
Post by: Blizzard on January 29, 2011, 07:17:31 am
That's really weird. :/
What exactly happens? How is the result different from RMXP?
Title: Re: need help translating c code
Post by: Ryex on January 29, 2011, 07:33:45 am
original file
Spoiler: ShowHide
(http://img255.imageshack.us/img255/6512/bobbk.jpg)



with
(hsva[0] - float(hue)) % 360.0 

the % 360 actually keeps it in 0-360 properly
Spoiler: ShowHide
(http://img43.imageshack.us/img43/5117/capture2bd.png)



with
(hsva[0] + float(hue)) % 360.0 

Spoiler: ShowHide
(http://img97.imageshack.us/img97/2845/capturewsx.png)


as you can tell the left side is the result in rmxp and indeed any other image editor and the right side is mymethod
Title: Re: need help translating c code
Post by: Blizzard on January 29, 2011, 08:05:36 am
Hm... This is inverted. Green is on the opposite side of violet on the hue wheel.
Try this code just for the sake of it:

(hsva[0] + float(hue) + 180) % 360.0
Title: Re: need help translating c code
Post by: Ryex on January 29, 2011, 08:55:49 am
+180.0: ShowHide
(http://img510.imageshack.us/img510/1653/capturewt.png)
Title: Re: need help translating c code
Post by: Blizzard on January 29, 2011, 09:35:22 am
Check if hvsa= actually does what it's supposed to. Maybe the RGB isn't convert if you just set HSV.
Title: Re: need help translating c code
Post by: Ryex on January 29, 2011, 09:39:46 am
nope it works properly that was one of the first things I checked
Title: Re: need help translating c code
Post by: Blizzard on January 29, 2011, 10:28:00 am
So setting HSV does change RGB?

Ok, time to do this brute force. Take a 1x1 pixel pure red image. Load it into RMXP at bitmap. Run all hues from 0 to 359 and save the RGB values to a file (or possibly a CSV file so you can open it in Excel to analyze the data later). Do the same in RMPY. Compare the results. Somewhere there should be a set of numbers that is similar or you should be able to figure out a pattern. You can also try first all values from 0 to 350, step 10 first so you get a quick overview before analyzing the whole thing. This should give you the data to figure out why your code does it differently.
Title: Re: need help translating c code
Post by: Ryex on January 29, 2011, 05:14:58 pm
oh god I'm a fucking moron
surface.set_at((x, y), (color.r, color.b, color.g, color.a))

dose any one else see something wrong with that line? here let me fix it
surface.set_at((x, y), (color.r, color.g, color.b, color.a))

well what to you know I just waisted 6 or 7 hours of work last night trying to figure out what the hell i did wrong when the only problem was I switched the green and blue components of the color.

I feel really stupid right now.

proof that it works: ShowHide
(http://img190.imageshack.us/img190/231/captureiw.png)
Title: Re: need help translating c code
Post by: Blizzard on January 30, 2011, 04:49:20 am
LMAO!
I've done stupider things, don't worry about it. xD