Carbon GetPixel Equivalent

Discussion in 'Mac Programming' started by printf, Sep 27, 2008.

  1. macrumors regular

    Joined:
    Aug 27, 2008
    #1
    in windows you can actually grab the rgb value (colorref) of a pixel through the a window's device context, and extrapolate the individual color channels.

    i looked at the CGContext reference and it made no mention of such a function. are there any documented/undocumented workarounds/hacks anyone can share.
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    What are you actually trying to achieve?
     
  3. thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #3
    ideally, i'd call a function that passed in a WindowRef and an x,y coord, and have it fill an RGBColor object with it's corresponding RGB values.

    like so:
    Code:
    CGGetPixel(mywindowref,&myrgbcolor);
    
    surely this isn't asking a lot
     
  4. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    No, that is _not_ what you are trying to achieve. That is what _you_ think might help you do what you want to achieve.
     
  5. macrumors member

    Joined:
    Aug 16, 2008
    #5
    I think what he's trying to achieve is to return the RGB colour of a given pixel, by a specified x and y coordinate (and window reference)
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Not that I have any idea on this, but i think the question is why the RGB value is needed? What will you do with it once you have it?

    -Lee
     
  7. thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #7
    correct, or a CGContext reference, though the Carbon documentation doesn't mention anything like this..


    huh? i'm perfectly aware of what i am trying to achieve. thank you :)

    conquer the world.


    honestly, i just want an answer to this question. if it cannot be done, great, just say that. i'm just porting code from a windows app, and this win32 api is used EXTENSIVELY. i'm intelligent enough to figure out a work-around, but if apple offers this out of the box with one of their api's, or if you know of a work-around, i'd be ever so greatful if you provided it.

    thank you :)
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    Usually people here will try to help others with their programming problems. If you want an answer to a question, that is a completely different thing. And even though you tried to avoid it, you finally told us what you wanted to achieve: Port some piece of Windows code with minimal changes.

    The question that I asked is what anyone with some experience would ask: If anyone tries to read a pixel value from a window, then they are most likely trying hard to go into the completely wrong direction. And reading a pixel from a CGContextRef doesn't even make sense: What if that context is a PDF context and doesn't even have the concept of pixels?
     
  9. thread starter macrumors regular

    Joined:
    Aug 27, 2008
    #9
    i'd rather not divulge why i need this, simply because it IS the wrong approach. however, if the api were at my disposal, i'd rather save myself the time of doing it the right way.

    having said that, i've already developed and implemented the work-around and it works fine.

    if you want to know what i was trying to achieve, i have tabs created from device-independent bitmaps that are drawn to the screen individually. they should display a shadow, which is essentially a real-time filter applied over whatever the tab is positioned over, be it another tab, or just a blank space. since there was no access to the image buffer that drew the previous tab or other content, getpixel was used to 'see' what was visible in the windows device context. each pixel value retreived with getpixel was then passed through the filter with a shadow mask to generate the new pixel.

    yes, this was not the best way to do it, but it worked. i appreciate you wanting to get to the root of my problem and guide me down the right path, but i'd have to argue that sometimes in programming, there's no such thing as 'right'. if you've been programming for any reasonable amount of time, you've probably cut corners to get something done and meet a deadline - and the last thing you want to do is get a technical lecture on how inefficient your code/approach is.

    and also, i don't know how you can state with complete certainty that grabbing a pixel value from a window is the completely wrong direction. perhaps, i'm not as seasoned as you, but i've learned in my years that for every rule, there is an exception. why would other programming technologies have created that api in the first place, if it were completely wrong? now, i have work to do, so let's just leave that as a rhetorical question.

    thanks everyone for your help, or good intentions!
     
  10. macrumors member

    Joined:
    Aug 16, 2008
    #10
    Yeah, I agree with you printf. Sometimes just an answer is nice, without having to explain every detail of the context (especially if it's something you'd rather keep to yourself). I thought this is what help forums were all about - asking a question and receiving an answer.

    Am sorry I can't help with this however. Maybe in a few months! :cool:
     
  11. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #11
    You are contradicting yourself. This is a help forum - not an "asking a question and receiving an answer" forum. I asked ages ago "What are you trying to achieve" and very long time later I've found out what printf _really_ wanted to achieve: Draing shadows. What would have happened if he had asked about drawing shadows in the first place?

    Typing "macosx draw shadow" into Google would have found the following link:

    http://lists.apple.com/archives/Cocoa-dev/2001/Jun/msg00917.html

    You could also have a look at NSShadow.h. But the real thing seems to be this link:

    http://developer.apple.com/document...hquartz2d/dq_shadows/chapter_8_section_3.html

    You want to draw shadows, and Apple has an API to draw shadows. Three lines of code (safe the graphics state, set up a shadow effect with parameters like width of the shadow, blur, colour, direction), do your normal drawing, and finally restore the old graphics state. And the same lines of code can be used for all 2d graphics operation.

    If there wasn't an API for drawing shadows, the answer would have been drawing with an alpha value. That's what the first link suggested. There is no need to read pixel values for a window, you just tell your graphics API that you want to mix new graphics operations with whatever is already there on the screen.
     
  12. macrumors member

    Joined:
    Aug 16, 2008
    #12
    Sorry, how else does someone obtain help from a forum without asking a question and receiving an answer, or searching previous posts where someone has asked a question and received an answer?

    I understand where you're coming from, I really do! You want to put people on the right track to acheive their goals, and in order to do that - you need to know the context of their question (which sometimes means knowing the application). However, sometimes that's not what someone posts to a help forum for. Sometimes they just want a simple answer to a simple question. That to me, is still a form of helping someone out which comes under the remit of a 'help' forum.
     
  13. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    Example: You have heard stories that taking cocaine helps against toothache. Your tooth hurts, and you ask on a forum where to buy cocaine. What would be more helpful: Giving you the phone number of the nearest drug dealer, or figuring out your actual problem and getting you to a dentist? Very often "a simple answer to a simple question" is not helping at all. There are times where someone is stuck in a hole and asks how to dig deeper in the hope of getting out at the other end.
     
  14. macrumors member

    Joined:
    Aug 16, 2008
    #14
    Another example: You decide on a friday night that you'd like to smoke a joint - so you come on a forum to ask where you can buy some. Is it more helpful that someone points you in the right direction to get hold of some, or that they lecture you about where it was grown, how it was processed, how it was imported from columbia and how best to roll it?

    I think this is a matter of perception, not reality - and better that we agree to disagree.
     

Share This Page