Results 1 to 10 of 10

Thread: Reading pixels?

  1. #1

    Reading pixels?

    Does anyone have documentation or examples for the thinBasic command (getPixel?) which reads through a BMP and can store the RGB value of each pixel into an array?

    I don't need it for any special effects, which I understand is the most common use for this, but I do need to fill a 2D matrix with values that depend on (though are not the same as) the pixel values of a BMP.

    Any help? Thanks.

    Intio
    Play Ocai at  [size=12pt]ocai.webs.com[/size]

  2. #2
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Reading pixels?

    Are you just working with files because thats really easy for what you want.


    Here is something I was playing with a while back.

    Attached Files Attached Files
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  3. #3

    Re: Reading pixels?

    Thanks Abraxas.

    That was essentially what I asked for, but I think I should have phrased my question differently. I'd rather load a texture and display it off-screen and then read the pixels directly off the texture.

    I got your code working, but it seems a bit much for what I want to do (fill a static 2D grid with values, and the start position of a few moveable things - all from the colours of a small BMP), since I just need the RGB values rather that having to negotiate and keep track of bytes and increments so that I do not read off the wrong data.

    I saw the '%XPRINT_GetPixel' statement in the help files, but I am not sure how to implement it (I have been using several different languages recently, and find it increasingly difficult keeping track of what commands and syntax are resident for each language! I'll be sticking with thinBasic for now though ), would this be a simpler way of using a single command to read pixel info.?

    Thanks for any help, and thanks for the code sample - it may come in useful for other things.
    Play Ocai at  [size=12pt]ocai.webs.com[/size]

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Reading pixels?

    Hi Intio,

    you did not mention "texture" in your original post, then the Abraxas reply seemed pretty ok to me.

    If you need to go 3D, then use TBGL module to load BMP file using TBGL_LoadTexture, then setup 2D projection using TBGL_RenderMatrix2D, and then pick the RGB value at specified location using TBGL_GetPixelInfo.

    Well, I attached working code for you


    Petr

    P.S. But the file processing way is cleaner, I am with Abraxas on this.
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5

    Re: Reading pixels?

    You're right, I didn't make my request clear in the original post - though Abraxas has provided me with a very useful code snippet that I will be sure to study in future.

    I'll give your example a try when I have some free time, thanks!



    Play Ocai at  [size=12pt]ocai.webs.com[/size]

  6. #6

    Re: Reading pixels?

    Everything is working now as it should, thanks Abraxas and Petr!

    P.S. One thing I did notice, when I use the GetPixelInfo command on one graphics chipset I get a slightly innacurate result; the Red and Blue values are 2 or 3 points below what they should be and the Green value is 2 or 3 points higher that what it should be.

    When I say "what it should be", I of course mean the value that I set in the code itself. It is never wildly innacurate, only by a few points. I tried changing BYTE variables to LONG but it made no difference. Changing the colour set in the code doesn't make it accurate - it just means the pixel info is still a few points off. This happens even if I read pixels from the uniform background colour.

    And this only happens on some chipsets (integrated Intel 9**GM/GL family), whilst others within the same chipset family will display perfectly accurate info.

    It's nothing that will affect my code (I always intended to include some buffer zone when I read pixel values, so that they do not have to be the exact value expected, but can be off by a few points), it just intrigues me.

    Anyhoo.. thanks for the help Petr and Abraxas. ;D
    Play Ocai at  [size=12pt]ocai.webs.com[/size]

  7. #7
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Reading pixels?

    Intel seem to have real problems with their drivers and/or hardware.

    glad everything is ok.


    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  8. #8

    Re: Reading pixels?

    When you read from the screen, you are reading the display color. (Graphic settings can alter those, as well as video-translation. Some cards use the BMP colors as a suggestion, and display the closest color from a list of compressed colors.)

    BMP is (256, 256, 256) RGB... However, certain colors are used by the GUI, and are reserved colors for layers and overlays. (VIDEO uses BLUE), for example... that is one color that will never display on the screen in an image. It is used to tell the DRIVERS where to draw the "WINDOW" or "OVERLAY".

    Older computers also have designated, "Windows colors". Those never appear in images. The colors are shortened to a close-color. (I think windows only has about 64 colors which it wipes from the available colors.)

    That was also a big issue with Web-Browsers, back in the day. They translated HEX into a "Close" color... Thus... Netscape and MSIE and OPERA, all showed a 256 BMP or GIF or HEX color "FF44FF", as another color, that was not the actual value. (Yet, amazingly, with the overlay, they displayed normal colors for images over 256 colors.)

    Your best bet, for accuracy, is to pull the value from a data-file... and DRAW the color on the screen, and use that same value to DRAW your other data... (So you are playing with the same "True" values. Your eyes will not notice the 2-val offset on the color, but your data will.)

  9. #9
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,128
    Rep Power
    732

    Re: Reading pixels?

    Hi Intio,

    that is why I recommend to analyze file as Abraxas and now ISAWHIM suggests, contrary to rely on graphic card output.
    Drrivers make some optimizations, which can result in not so accurrate output.

    Intel does great CPUs, but I cannot say the same about their graphic cards ( I even hesitate to call it GPU in case of Intel ). It might be because graphics innovation is not their target, but this is exactly why me and Intel we are no big friends

    If I am not wrong, none of the Intel cards supports OpenGL 2.1 at the moment, which is 2 years old thing. They do not have 1.5 as well, which is even older.

    Good thing is with TBGL you code safely - it is written in way all features can be executed on 99.99% cards, and some commands even scale according to architecture they run on.

    Sadly it is not in my power to control output and correct it. All textures are internally stored with 8 bits of precision per channel = 8 Red, 8 Green, 8 Blue, 8 Alpha.

    I checked my code to load 256 color textures ( as is the test one ), and there is no rounding involved - I just take colors from pallete and assign them to pixels directly.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  10. #10

    Re: Reading pixels?

    Thank you for the explanations ISAWHIM and Petr. That's not a platitude - I genuinely find all this detail interesting.

    As I said, it looks like I'll pay more attention to Abraxas's code ;D
    Play Ocai at  [size=12pt]ocai.webs.com[/size]

Similar Threads

  1. String size in pixels or units
    By Pipes in forum TBGL General
    Replies: 9
    Last Post: 18-06-2009, 17:28

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •