SFML community forums

Help => Graphics => Topic started by: DrAdventure on March 20, 2016, 10:20:02 pm

Title: Size depending on pixels
Post by: DrAdventure on March 20, 2016, 10:20:02 pm
How can i set size of image depending on the last pixels position
for example:
In image two pixels , frist pixel in the end of the right and the second pixel in the end of the left.The distance between them 40 pixel so i need to resize the imge to 41 pixel. Is it posible?
Title: AW: Size depending on pixels
Post by: eXpl0it3r on March 21, 2016, 07:37:33 am
Use math and the scale function of a sprite. ;)
Title: Re: Size depending on pixels
Post by: DrAdventure on March 21, 2016, 04:46:21 pm
But scale function won't delete the empty space in image where no pixel in there , right?
Title: Re: Size depending on pixels
Post by: bitano on March 21, 2016, 04:48:27 pm
I want to help...but i have no idea what you're talking about or what you're trying to accomplish...

Is it me, or...?  :(
Title: Re: Size depending on pixels
Post by: DrAdventure on March 21, 2016, 07:39:10 pm
 i can't explain in the right way but i think this image will help
(http://m.UploadEdit.com/ea3u/1458585415488.jpg)
Title: Re: Size depending on pixels
Post by: Perde on March 21, 2016, 07:52:19 pm
i can't explain in the right way but i think this image will help
(http://m.UploadEdit.com/ea3u/1458585415488.jpg)
void sf::Sprite::setTextureRect(const IntRect & rectangle)
http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Sprite.php#a3fefec419a4e6a90c0fd54c793d82ec2
Title: Re: Size depending on pixels
Post by: DrAdventure on March 21, 2016, 08:24:31 pm
you are so close from what i mean

Now i want to make a function return  IntRect can fit any texture i want, is it possible?
Title: Re: Size depending on pixels
Post by: Laurent on March 21, 2016, 08:29:43 pm
So you want a function that checks the pixels of an image, and returns the smallest possible rectangle that encloses the "shape" (defined by non-white pixels? black pixels?) in it?
Title: Re: Size depending on pixels
Post by: bitano on March 21, 2016, 08:33:01 pm
So you want a function that checks the pixels of an image, and returns the smallest possible rectangle that encloses the "shape" (defined by non-white pixels? black pixels?) in it?
Yeah looks like he wants to crop the actual image content inside an image. But are the images indeed black/white? or do you want to check for any colour pixels against a single color background?
Or do you want to check against alpha channel?
Title: Re: Size depending on pixels
Post by: DrAdventure on March 21, 2016, 08:58:41 pm
 want to check against alpha channel 

And thanks you really understand what i mean :)
Title: Re: Size depending on pixels
Post by: bitano on March 21, 2016, 09:06:38 pm
    sf::Image srcImg, destImg;
    srcImg.loadFromFile("img_in.png");
    sf::Vector2u dims = srcImg.getSize();
    sf::IntRect rect = sf::IntRect(-1, -1, -1, -1);
    for (sf::Uint16 x = 0; x < dims.x; x++) {
        for (sf::Uint16 y = 0; y < dims.y; y++) {
            if (srcImg.getPixel(x,y) == sf::Color::Transparent) continue; // don't do anything when pixel's transparent
            if (rect.left   == -1 || x < rect.left)   rect.left   = x;
            if (rect.top    == -1 || y < rect.top)    rect.top    = y;
            if (rect.width  == -1 || x > rect.width)  rect.width  = x;
            if (rect.height == -1 || y > rect.height) rect.height = y;
        }
    }
    destImg.create(rect.width-rect.left+1, rect.height-rect.top+1, sf::Color::Transparent); // resize dest img
    destImg.copy(srcImg, 0, 0, rect, true); // copy defined rectangle of the source image to destination
    destImg.saveToFile("img_out.png");
 

This is the best i could come up with atm...I'm actually working so don't have more than notepad :D
Hope it works or at least steers you into the direction of your solution

[UPDATE]
Tested it at home just now, made some slight changes, but it works so yeah. Depending on what you want to use it for this bit of code might suit you.
Title: Re: Size depending on pixels
Post by: DrAdventure on March 22, 2016, 01:14:34 pm
thanks alot i am grateful for you ;)