Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Size depending on pixels  (Read 3699 times)

0 Members and 1 Guest are viewing this topic.

DrAdventure

  • Newbie
  • *
  • Posts: 8
    • View Profile
Size depending on pixels
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
AW: Size depending on pixels
« Reply #1 on: March 21, 2016, 07:37:33 am »
Use math and the scale function of a sprite. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

DrAdventure

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Size depending on pixels
« Reply #2 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?

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Size depending on pixels
« Reply #3 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...?  :(

DrAdventure

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Size depending on pixels
« Reply #4 on: March 21, 2016, 07:39:10 pm »
 i can't explain in the right way but i think this image will help

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Size depending on pixels
« Reply #5 on: March 21, 2016, 07:52:19 pm »
i can't explain in the right way but i think this image will help

void sf::Sprite::setTextureRect(const IntRect & rectangle)
http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Sprite.php#a3fefec419a4e6a90c0fd54c793d82ec2
« Last Edit: March 21, 2016, 07:59:55 pm by Perde »

DrAdventure

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Size depending on pixels
« Reply #6 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Size depending on pixels
« Reply #7 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?
Laurent Gomila - SFML developer

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Size depending on pixels
« Reply #8 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?

DrAdventure

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Size depending on pixels
« Reply #9 on: March 21, 2016, 08:58:41 pm »
 want to check against alpha channel 

And thanks you really understand what i mean :)
« Last Edit: March 21, 2016, 09:04:26 pm by DrAdventure »

bitano

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Size depending on pixels
« Reply #10 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.
« Last Edit: March 21, 2016, 10:29:51 pm by bitano »

DrAdventure

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Size depending on pixels
« Reply #11 on: March 22, 2016, 01:14:34 pm »
thanks alot i am grateful for you ;)