123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <cmath>
- #include <iomanip>
- class BMP2Gcode
- {
- public:
- BMP2Gcode();
- void traitement(char* nom_fichier);
- void fichier_conf();
- private:
- unsigned int BMP_larg();
- unsigned int BMP_haut();
- unsigned int BMP_offset();
- unsigned int BMP_profondeur();
- int entete();
- int donnees();
- int sortie();
- void BMP_larg(unsigned int nbr);
- void BMP_haut(unsigned int nbr);
- void BMP_offset(unsigned int nbr);
- void BMP_profondeur(unsigned int nbr);
- unsigned int conversion(unsigned int valeur, unsigned int min, unsigned int max);
- unsigned int BMP_largeur, BMP_hauteur, BMP_off, BMP_prof, BMP_taille;
- double conf_taille_laser, conf_puissance_min, conf_puissance_max, conf_vitesse, conf_taille_image;
- std::vector<unsigned int> tab_donnees;
- std::string nom_fichier_bmp;
- };
- BMP2Gcode::BMP2Gcode()
- :BMP_largeur(0), BMP_hauteur(0), BMP_off(0), BMP_prof(0), BMP_taille(0)
- {}
- void BMP2Gcode::traitement(char* nom_fichier)
- {
- nom_fichier_bmp = nom_fichier;
- entete();
- donnees();
- sortie();
- }
- unsigned int BMP2Gcode::BMP_larg()
- {
- return BMP_largeur;
- }
- unsigned int BMP2Gcode::BMP_haut()
- {
- return BMP_hauteur;
- }
- unsigned int BMP2Gcode::BMP_offset()
- {
- return BMP_off;
- }
- unsigned int BMP2Gcode::BMP_profondeur()
- {
- return BMP_prof;
- }
- int BMP2Gcode::entete()
- {
-
- std::ifstream fichier(nom_fichier_bmp, std::ifstream::binary);
-
- if(!fichier)
- {
- std::cerr << "impossible d'ouvrir le fichier" << std::endl;
- return 1;
- }
-
- char *entete = new char [54];
- fichier.read(entete, 54);
- if(entete[0] == 'B')
- {
- if(entete[1] == 'M')
- {
- std::cout << "Bitmap windows" << std::endl;
- }
- }
-
-
-
- BMP_larg(*(int*)&entete[18]);
- BMP_haut(*(int*)&entete[22]);
- BMP_offset(*(int*)&entete[10]);
- BMP_profondeur(*(short*)&entete[28]);
- delete[] entete;
- fichier.close();
-
- return 0;
- }
- int BMP2Gcode::donnees()
- {
-
- std::ifstream fichier(nom_fichier_bmp, std::ifstream::binary);
-
- if(!fichier)
- {
- std::cerr << "impossible d'ouvrir le fichier" << std::endl;
- return 1;
- }
-
- if(BMP_off == 0)
- {entete();}
-
- int limite = BMP_larg()*BMP_haut() * BMP_profondeur()/8 + BMP_offset();
-
- char *donnees = new char [ limite ];
- fichier.read(donnees, limite);
- for(int i = BMP_offset() ; i < limite ; i += 3)
- {
- if(donnees[i] < 0)
- {tab_donnees.push_back(256 + (int)donnees[i]);}
- else
- {tab_donnees.push_back((int)donnees[i]);}
- }
-
- delete[] donnees;
- fichier.close();
- return 0;
- }
- int BMP2Gcode::sortie()
- {
- std::string nom_fichier_gcode;
- nom_fichier_gcode = nom_fichier_bmp + ".gcode";
- std::ofstream fichier_sortie;
- fichier_sortie.open (nom_fichier_gcode);
-
- double taille_pixel = (double)(conf_taille_image)/(double)(BMP_largeur), position_Y = 0;
- int sens = 1, indice = 0, ligne = 1;
-
- conf_vitesse *= 60;
-
- taille_pixel = round(10.0*taille_pixel)/10.0;
- std::cout << "largeur = " << BMP_largeur << " pixel" << std::endl;
- std::cout << "taille pixel = " << taille_pixel << " mm" << std::endl;
-
-
- fichier_sortie << "G91" << std::endl;
-
-
- fichier_sortie << "M106 P1 S0" << std::endl;
-
-
- fichier_sortie << "G1 F" << conf_vitesse << std::endl;
-
- while(ligne <= BMP_hauteur)
- {
-
- while(position_Y <= ligne * taille_pixel)
- {
- int nbr_pixels = 0;
-
- while(nbr_pixels < BMP_largeur)
- {
- unsigned int puissance = conversion(255 - tab_donnees[indice], conf_puissance_min, conf_puissance_max);
-
- int nbr = 1;
- while( puissance == conversion(255 - tab_donnees[indice + nbr], conf_puissance_min, conf_puissance_max)
- && indice + nbr < BMP_largeur)
- {nbr ++;}
-
- fichier_sortie << "M106 P1 S" << puissance << std::endl;
-
- fichier_sortie << "G1 X" << sens * taille_pixel * nbr << std::endl;
-
- nbr_pixels += nbr;
-
- indice += sens * nbr;
- }
-
- fichier_sortie << "G1 Y" << conf_taille_laser << std::endl;
-
- position_Y += conf_taille_laser;
-
- sens *= -1;
- }
-
- ligne ++;
- indice += BMP_largeur;
- fichier_sortie << ";ligne numéro " << ligne << std::endl;
- }
-
-
- fichier_sortie << "M106 P1 S0" << std::endl;
- fichier_sortie.close();
- return 0;
- }
- void BMP2Gcode::BMP_larg(unsigned int nbr)
- {
- BMP_largeur = nbr;
- }
- void BMP2Gcode::BMP_haut(unsigned int nbr)
- {
- BMP_hauteur = nbr;
- }
- void BMP2Gcode::BMP_offset(unsigned int nbr)
- {
- BMP_off = nbr;
- }
- void BMP2Gcode::BMP_profondeur(unsigned int nbr)
- {
- BMP_prof = nbr;
- }
- unsigned int BMP2Gcode::conversion(unsigned int valeur, unsigned int min, unsigned int max)
- {
-
- return valeur * (max - min)/255 + min;
- }
- void BMP2Gcode::fichier_conf()
- {
-
- std::ifstream fichier_conf("laser.conf");
-
-
- if(!fichier_conf)
- {
-
- std::ofstream fichier_conf;
- fichier_conf.open("laser.conf");
- fichier_conf << "taille_laser(mm) 0.2" << std::endl;
- fichier_conf << "puissance_min 0" << std::endl;
- fichier_conf << "puissance_max 255" << std::endl;
- fichier_conf << "vitesse(mm/s) 20" << std::endl;
- fichier_conf << "taille_image_x(mm) 150" << std::endl;
- std::cout << "*************************************************" <<std::endl;
- std::cout << "ATTENTION, un fichier de configuration a été crée" <<std::endl;
- std::cout << "Il ne contient probablement pas les bonnes valeurs" <<std::endl;
- std::cout << "Pour les connaitre il est nécessaire dutiliser" <<std::endl;
- std::cout << "les scripts d'étalonnage" <<std::endl;
- std::cout << "*************************************************" <<std::endl;
- }
- else
- {
-
- std::string clef;
- double valeur;
- while(fichier_conf >> clef >> valeur)
- {
- if(clef == "taille_laser(mm)")
- {conf_taille_laser = valeur;}
- if(clef == "puissance_min")
- {conf_puissance_min = valeur;}
- if(clef == "puissance_max")
- {conf_puissance_max = valeur;}
- if(clef == "vitesse(mm/s)")
- {conf_vitesse = valeur;}
- if(clef == "taille_image_x(mm)")
- {conf_taille_image = valeur;}
- }
- std::cout << "***CONFIGURATION***" << std::endl;
- std::cout << "taille_laser = " << conf_taille_laser << " mm" << std::endl;
- std::cout << "puissance_min = " << conf_puissance_min << " PWM 0-255" << std::endl;
- std::cout << "puissance_max = " << conf_puissance_max << " PWM 0-255" << std::endl;
- std::cout << "vitesse = " << conf_vitesse << " mm/s" << std::endl;
- std::cout << "taille_image = " << conf_taille_image << " mm" << std::endl;
- }
- fichier_conf.close();
- }
- int main(int argc, char* argv[])
- {
- if(argc >= 2)
- {
- std::string entree = argv[1];
- BMP2Gcode image;
- image.fichier_conf();
- image.traitement(argv[1]);
- }
- else
- {std::cout << "Veuillez préciser un nom de fichier BMP" << std::endl;}
- return 1;
- }
|