Thread: searching images

  1. #1
    Registered User joed's Avatar
    Join Date
    Mar 2004

    searching images

    I have many functions which require searching through images, i.e.

    for(y = 0; y < h; y++) {
      for(x = 0; x < w; x++) {
        do_whatever(x, y);
    I would like to do something cleaner, like:

    int max = w * h;
    int count = 0;

    while(count < max) {
      int y = count / w;
      int x = count % w;  
      do_whatever(x, y);
    (Assume the / and % are replaced with fast methods, such as shifts...)

    Is there a better way, performance is important since each pixel is visited seperately.

    I'm mainly only trying to clean up my code before my project gets too large. The nested loops are too deep. Thanks,


  2. #2
    Been here, done that.
    Join Date
    May 2003
    I don't find the while() loop cleaner. It's harder to read. I doubt you're saving anything by using calculations insted of increments.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User joed's Avatar
    Join Date
    Mar 2004
    My solution was to not worry about the smaller functions, and split the larger ones into pieces. That took care of most of the ugly nesting. Your right, the other idea was counter-intuitive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM