1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <iostream>
#include <fstream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
unsigned int toGrey1(sf::Color col)
{
unsigned char ret;
ret = (unsigned int) (0.299 * (float) col.r + 0.587 * (float) col.g + 0.114 * (float) col.b);
return ret >> 7;
}
unsigned int round(float x)
{
unsigned int ret = (unsigned int) x;
if(x - (float) ret >= 0.5)
ret ++;
return ret;
}
int main(int argc, char **argv)
{
if(argc != 2)
{
cout << "Converts an image into a B&W 318*212 24bpp BMP" << endl;
cout << "Usage: IMG2BMP <input_file>" << endl;
return 0;
}
sf::Image img, dst;
float xFactor, yFactor;
if(!img.LoadFromFile(argv[1]))
{
cout << "Erreur: impossible d'ouvrir le fichier d'entree: \"" << argv[2] << "\"" << endl;
return 0;
}
xFactor = img.GetWidth() / 318.f;
yFactor = img.GetHeight() / 212.f;
dst.Create(318,212, sf::Color::White);
for(unsigned int i = 0; i < 318; i ++)
for(unsigned int j = 0; j < 212; j ++)
dst.SetPixel(i, j, toGrey1(img.GetPixel(round((float)i * xFactor), round((float) j * yFactor))) ? sf::Color::White : sf::Color::Black);
dst.SaveToFile("./converted.bmp");
return 0;
} |
Partager