need help translating c code

Started by Ryex, January 29, 2011, 04:31:24 am

Previous topic - Next topic

Ryex

so this c code here : 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)

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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

January 29, 2011, 06:00:50 am #2 Last Edit: January 29, 2011, 06:14:06 am by Ryex
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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

That's really weird. :/
What exactly happens? How is the result different from RMXP?
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

original file
Spoiler: ShowHide



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

the % 360 actually keeps it in 0-360 properly
Spoiler: ShowHide



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

Spoiler: ShowHide


as you can tell the left side is the result in rmxp and indeed any other image editor and the right side is mymethod
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

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
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Check if hvsa= actually does what it's supposed to. Maybe the RGB isn't convert if you just set HSV.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

nope it works properly that was one of the first things I checked
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Ryex

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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

LMAO!
I've done stupider things, don't worry about it. xD
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.