Thread: Changing pixel colour

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    Changing pixel colour

    Hi, i have 2300 bitmap images very similar to this one

    http://mage.me.uk/files/hab12d1_001.bmp


    and what i basically need to do is change every white pixel in the image to blue. I haven't manipulated any images before in c and I'd like some pointers on how to go about it. In addition, after one image is done, ideally the program would switch to the next bitmap file.

    So an English summary of the program would be.

    open bitmap image.
    look at the first pixel in the image.
    if the pixel is anything other than white skip it.
    If the pixel is white, change it to blue and go to next pixel.
    if all pixels have been examined, go to next image in the directory and repeat process.

    If anyone knows of existing software or code which does this, that would be great too. Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That sounds like the correct solution. I'm not sure there are any software out there to do that.

    If you have photoshop, you could of course do it with "Magic Wand", but with thousands of images, it would certainly be faster to do it by (slow) software.

    You should look for a library package to load and save whatever image format you are using.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Start with writing a program to display the filenames of all your bitmap images that you will want to later edit, iterating through a directory/folder location on your computer. Just display them one-to-a-line to the console or output them to a file.

    Once you've got that working you'll need to figure out the bitmap format and work on opening and editing the individual files as required.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hk_mp5kpdw View Post
    Once you've got that working you'll need to figure out the bitmap format and work on opening and editing the individual files as required.
    Almost certainly, you read the file in as binary with fread("rb") to an unsigned char array, and go byte by byte thru the array. What each byte means depends on the nature of the format.

    Unfortunately, as I have noticed before, the significant links at "wotsit" are sometimes dead. So you will have to do some digging around and reading of technical specs.
    Last edited by MK27; 06-08-2009 at 12:18 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Almost certainly, you read the file in as binary with fread("rb") to an unsigned char array, and go byte by byte thru the array. What each byte means depends on the nature of the format.
    Sure. But without knowing what for example the headers mean, you won't know a lot of other things you need to know to process the file. The header may not even be the same size for all files [some headers have "optional" content that may or may not be there].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    Sure. But without knowing what for example the headers mean, you won't know a lot of other things you need to know to process the file. The header may not even be the same size for all files [some headers have "optional" content that may or may not be there].
    Even worse, some file formats allow a limited/customized palette, so you might have to do some work just to add "blue" as a possibility! But I don't thing bmp is like that.

    Definitely, dissecting/dissembling/interpreting/understanding the header is essential. Those "technical docs" should elaborate.
    Last edited by MK27; 06-08-2009 at 12:44 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I would use Image Magick. You could use "compose" to treat the white color as transparent, and underlay the image with a solid color (whatever color you want to convert white into). The result will be all white pixels changing to your chosen color.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    I would use Image Magick. You could use "compose" to treat the white color as transparent, and underlay the image with a solid color (whatever color you want to convert white into). The result will be all white pixels changing to your chosen color.
    That does sound like a solution. I was thinking that on hardware platforms, you'd use "color-keying", which is similar to this solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Great, thanks people, imagemagick worked perfectly, i used
    Code:
    chii:~/Desktop/Stimuli$convert hab14d1_007.bmp -fill blue -opaque white hab14d1_007done.gmp
    which did the job. I'm tempted to just put all the file names in a shell script and be done with it as I'm having difficulty finding examples on using the c api. There seems to be ample information on using imagemagick in the command line. does anyone know of a good place where examples of the api in use could be found. Excuse me if there are some obvious ones, i have not spotted them :P. Thanks again

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How about using ls | xargs to perform the processing. Just make sure you output to a differnet directory, or you will end up with an infinite loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    e.g.
    Code:
    $ mkdir converted
    $ for file in *.bmp; do convert $file -fill blue -opaque white converted/$file; done
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Ah great, that worked really well. Done them all now, thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting Pixel Colour Screen
    By god_of_war in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2006, 01:17 PM
  2. changing colour of the background ...
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2005, 08:53 AM
  3. Changing screen colour
    By cc870 in forum C Programming
    Replies: 4
    Last Post: 07-01-2005, 07:12 AM
  4. Changing the colour of static text.
    By -leech- in forum Windows Programming
    Replies: 2
    Last Post: 02-09-2003, 09:09 PM
  5. Changing pixel color
    By jwhitaker3 in forum C Programming
    Replies: 1
    Last Post: 01-28-2003, 09:46 AM