#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pngio.h"
#include "hpgl-reader.h"
struct image *IN_function(){
struct image *imag;
imag=(struct image *)malloc(sizeof(struct image *));
imag->height=500;
imag->width=500;
unsigned char **data;
data=(unsigned char **)malloc(sizeof(char *)*imag->width*imag->height);
for (int i=0; i<imag->width; i++){
imag->data[i]=(char *)malloc(sizeof(char)*imag->height);
}
return imag;
}
void SP_function(struct hpgl_command *cmd){
/*
cf. doc projet prof
0 -> black
.
.
.
> black
//convertir le chiffre retourne selon la table du prof en code rgb à vérifier
*/
}
void PU_function(struct hpgl_command *cmd, struct image *img){
int rc;
rc = write_png("image_generee.png", img);
if(rc < 0) {
fprintf(stderr, "Couldn't write %s.\n", "image_generee.png");
return 1;
}
free_image(img);
}
void PD_function(struct hpgl_command *cmd, struct image *img, int *point_courant){
//1 determiner taille complete image
// SINON pour chaque commande , etudier les limites et reallouer
//2 splitter les plages de colorisations, faire une boucle qui itère par pas de 2, bouble va aller jusqu'à nparms, et position courante à parms[i], à chaque tour de boucle la position courante va devenir la position que l'on vient de tracer
int point_fin_trait[2];
for(int i=0;i<cmd->nparms;i+=2){
// récupération des positions du segment à tracer
//1 er extremite du segment
point_fin_trait[0]=cmd->parms[i];
point_fin_trait[1]=cmd->parms[i+1];
//tracer la ligne = modifier le struct img, point courant = debut du segment
for(int ligne=point_courant[0];ligne<point_fin_trait[0];ligne++) {
for(int colonne=point_courant[1];colonne<point_fin_trait[1];colonne++) {
//img->data[i][j]=;
}
}
//2 eme extremite du segment
point_courant[0]=point_fin_trait[0];
point_courant[1]=point_fin_trait[1];
}
}
int
main(int argc, char **argv) {
struct hpgl_file *hpf;
struct image *imgg;
/*
if(argc != 2) {
fprintf(stderr, "Syntax: %s filename\n", argv[0]);
exit(1);
}
*/
hpf = hpgl_open("HPGL_Example.hpgl"); // modifié
if(hpf == null) {
perror("open");
exit(1);
}
while(1) {
struct hpgl_command *cmd = hpgl_read(hpf);
if(cmd == null)
break;
printf("%c%c", cmd->command
[0], cmd->command
[1]);
for(int i = 0; i < cmd->nparms; i++) {
if(i > 0)
}
if(strcmp(cmd->command, "IN")==0){
imgg=IN_function(cmd);
}else if(strcmp(cmd->command, "SP")==0){
SP_function(cmd);
}
else if(strcmp(cmd->command, "PU")==0){
PU_function(cmd, imgg);
}else if(strcmp(cmd->command, "PD"==0){
PD_function(cmd, imgg);
}
hpgl_free_command(cmd);
}
hpgl_close(hpf);
return 0;
}
// FAIRE FONCTION IN : initialise l'état de la table traçante
//FAIRE FONCTION SP : SELECT PEN ou SET PEN choisit la plume numéro 1, normalement une plume noire
//FONCTION PU : PEN UP qui lève la plume
//FONCTION PU0, 0 qui lève la plume et se positionne aux coordonnées (0,0)
//FONCTION PD100,0,100, 100,0,100,0,0 : PEN DOWN qui baisse la plume puis trace des segments de droite depuis la position courante jusqu'aux points de coordonnées(100,0) puis (100, 100), (0, 100) et revient en (0, 0)