Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Splitting an image into tiles


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 george7378   Members   -  Reputation: 1328

Like
0Likes
Like

Posted 05 May 2014 - 06:12 PM

Hi!

 

Let's say I have an image and I want to loop through the pixels. I know how to do this in scan-line form:

 

for (x = 0; x < w; x++)
{
for (y = 0; y < h; y++)
{
currentPixel = image[y*w + x];
}
}

 

How would I do the same thing if I was splitting my image into square tiles?

 

Thanks :)



Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 11356

Like
2Likes
Like

Posted 05 May 2014 - 06:25 PM

Something like this? Pseudocode, of course, four nested loops is unsightly and can probably be refactored depending on your code, e.g. foreach tile process tile.

for (tileY = 0; tileY < h / tileH; ++tileY)
    for (tileX = 0; tileX < w / tileW; ++tileX)
        for (y = tileY * tileH; y < (tileY + 1) * tileH; ++y)
            for (x = tileX * tileW; x < (tileX + 1) * tileW; ++x)
            {
                // do stuff on pixel (x, y)
            }

Obviously the image needs to split properly into tiles of the specified dimension (tileW, tileH), otherwise you'll need to handle the leftover pixels somehow.


“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


#3 Ravyne   GDNet+   -  Reputation: 10851

Like
3Likes
Like

Posted 05 May 2014 - 06:40 PM

So, first of all, fix your loops -- having y inside x is terrible for your cache performance.

 

The general answer to tiling the image is to wrap your y-x pixel loop in another y-x tile loop. Divide the height and width of the whole image by the height and width of your tiles, respectively, to get the number of iterations in your y-x tile loops. You'll need to take special care of images that don't divide evenly into perfect tiles.


Edited by Ravyne, 05 May 2014 - 06:44 PM.

throw table_exception("(ノ ゜Д゜)ノ ︵ ┻━┻");


#4 ferrous   Members   -  Reputation: 2959

Like
0Likes
Like

Posted 06 May 2014 - 04:10 PM

Reminds me that one of the things we wrote as interns back in the old Game Boy Advance days, was a tool to check the image for repeated tiles.  Is one tile nearly the same as another tile, what about if the tile was rotated by some 90 degree increment, would it match then, etc.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS