Created
          November 28, 2013 15:22 
        
      - 
      
- 
        Save vittorioromeo/7693607 to your computer and use it in GitHub Desktop. 
    Takes an image as an input, returns the same image, outlined by 1px, as output.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Copyright (c) 2013 Vittorio Romeo | |
| // License: Academic Free License ("AFL") v. 3.0 | |
| // AFL License page: http://opensource.org/licenses/AFL-3.0 | |
| #define SSVS_N_USE_JSON | |
| #include <vector> | |
| #include <string> | |
| #include <SFML/System.hpp> | |
| #include <SFML/Graphics.hpp> | |
| #include <SSVUtils/SSVUtils.hpp> | |
| #include <SSVStart/SSVStart.hpp> | |
| constexpr unsigned int tolerance{15}; | |
| int main(int argc, char* argv[]) | |
| { | |
| std::vector<std::string> clArgs; for(int i{0}; i < argc; ++i) clArgs.emplace_back(argv[i]); | |
| if(clArgs.size() < 2) | |
| { | |
| ssvu::lo("Error") << "Please specify an image file name." << std::endl; | |
| return 1; | |
| } | |
| std::string imgPath{clArgs[1]}; | |
| sf::Image img; | |
| if(!img.loadFromFile(imgPath)) | |
| { | |
| ssvu::lo("Error") << "<" + clArgs[1] + "> is not a valid image file." << std::endl; | |
| return 1; | |
| } | |
| unsigned int width{img.getSize().x}, height{img.getSize().y}; | |
| sf::Color outlineColor{sf::Color::Black}; | |
| sf::Image result; | |
| result.create(width, height, img.getPixelsPtr()); | |
| for(auto iX(1u); iX < width - 1; ++iX) | |
| for(auto iY(1u); iY < height - 1; ++iY) | |
| { | |
| auto px(img.getPixel(iX, iY)); | |
| auto alpha(px.a); | |
| auto diffR(std::abs(px.r - outlineColor.r)); | |
| auto diffG(std::abs(px.g - outlineColor.g)); | |
| auto diffB(std::abs(px.b - outlineColor.b)); | |
| auto diffOverall((diffR + diffG + diffB) / 3); | |
| if(alpha > tolerance) | |
| { | |
| for(int nX{-1}; nX <= 1; ++nX) | |
| for(int nY{-1}; nY <= 1; ++nY) | |
| if(result.getPixel(iX + nX, iY + nY).a == 0) | |
| result.setPixel(iX + nX, iY + nY, sf::Color{0, 0, 0, diffOverall}); | |
| } | |
| } | |
| result.saveToFile(clArgs[1] + "_outlined.png"); | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment