#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pngio.h"
#include "hpgl-reader.h"
int min(int a, int b) {
return a < b ? a : b;
}
int max(int a, int b) {
return a > b ? a : b;
}
int clamp(int val) {
return min(max(val, 0), 255);
}
void set_r(int r, struct image *img, int row, int col) {
if (img == null) {
return;
}
img->data[row * img->width * 3 + col * 3] = clamp(r);
}
void set_g(int g, struct image *img, int row, int col) {
if (img == null) {
return;
}
img->data[row * img->width * 3 + col * 3 + 1] = clamp(g);
}
void set_b(int b, struct image *img, int row, int col) {
if (img == null) {
return;
}
img->data[row * img->width * 3 + col * 3 + 2] = clamp(b);
}
void set_a(int a, struct image *img, int row, int col) {
if (img == null) {
return;
}
img->data[row * img->width * 3 + col * 3 + 3] = clamp(a);
}
struct image *IN_function(){
struct image *imag;
imag=(struct image *)malloc(sizeof(struct image));
imag->height=2400;
imag->width=3200;
int nombre_channels_rgb = 4;
imag->data=(unsigned char **)malloc(sizeof(unsigned char)*imag->width*imag->height*nombre_channels_rgb);
/*for (int i=0; i<imag->width; i++){
imag->data[i]=(unsigned char *)malloc(sizeof(unsigned char)*imag->height);
}*/
return imag;
}
int SP_function(struct hpgl_command *cmd){
int code_couleur=cmd->parms[0];
return 0;
/*
cf. doc projet prof
0 -> black
//convertir le chiffre retourne selon la table du prof en code rgb à vérifier
*/
}
int 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 afficher_encodage_detecte(char *nom_systeme_encodage, struct hpgl_command *cmd){
int final_encoding = 0;
final_encoding=cmd->parms[0];
printf("%s ", nom_systeme_encodage
);
switch(final_encoding){
case 0:
break;
case 1:
printf("9825 Character Set ");
break;
case 2:
break;
case 3:
break;
case 4:
printf("Spanish/Latin American");
break;
case 6:
break;
case 7:
printf("ROMAN 8 Extensions \n");
break;
case 9:
break;
case 30:
break;
case 31:
printf("ISO Swedish for Names \n");
break;
case 32:
printf("ISO Norway, Version 1 \n");
break;
case 33:
break;
case 34:
break;
case 35:
break;
case 36:
break;
case 37:
break;
case 38:
break;
case 39:
printf("ISO Norway, Version 2 \n");
}
}
void CS_function(struct hpgl_command *cmd) {
char * s = "encodage par défaut";
afficher_encodage_detecte(s, cmd);
}
void CA_function(struct hpgl_command *cmd) {
char *s="encodage alternatif";
afficher_encodage_detecte(s, cmd);
}
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];
//*******************************************************
//essai image noir complete pour voir si ca fonctionne
for(int i=0; i<img->width; i++) {
for(int j=0; j<img->height;j++){
set_r(0, img, i, j);
set_g(0, img, i, j);
set_b(0, img, i, j);
set_a(255, img, i, j);
}
}
//*******************************************************
//TESTS DIVERS
//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[ligne][colonne]=0;
}
}*/
/*int fin_image = (img->width*img->height)-2;
for(int i=0; i < fin_image;i++){
img->data[i]='0';
img->data[i+1]='0';
img->data[i+2]='0';
}*/
/*for(int i=0; i<img->width; i++) {
for(int j=0; j<img->height;j++){
if(img->data[i][j]!=0){
img->data[i][j]=255;
}
}
}*/
//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;
int current_color;
int point_courant [2] = {0, 0};
/*
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){
current_color=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, point_courant);
}else if(strcmp(cmd->command, "CS")==0){
CS_function(cmd);
}else if(strcmp(cmd->command, "CA")==0){
CA_function(cmd);
}
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)