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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define TMP_STR_SIZE 256
#define MEM_MAX 0x81000000
#define MEM_OK 0x80002001
unsigned char* tab_image_source;
unsigned char* tab_image_final;
unsigned int width; /* largeur de l'image */
unsigned int height; /* hauteur de l'image */
unsigned char color_max;
enum format {BIN, ASCII} pgm_form;
const char delimiteur [2] = " ";
const char *token;
/* principe tableaux : ligne par ligne */
/* vous ne devez pas modifier ce programme : seuls les tableaux peuvent être modifiables !*/
void open_file (char* fichier_source) {
char tmp_str[TMP_STR_SIZE];
unsigned long int val = 0;
width = 0;
height = 0;
color_max = 0;
tab_image_source = NULL;
tab_image_final = NULL;
FILE* source = NULL;
/* init variables ok */
source = fopen (fichier_source, "r");
if (!source) {fprintf (stderr, "Fichier inexistant\n"); return;}
do { fgets(tmp_str, TMP_STR_SIZE, source); } while (tmp_str[0]=='#');
if (tmp_str[0]=='P') {
// fprintf(stderr, " %s\n", tmp_str);
if (tmp_str[1]=='2') { pgm_form=ASCII; }
else if (tmp_str[1]=='5') { pgm_form=BIN;}
else { fprintf (stderr, "Erreur de format P2 P5\n"); fclose(source); return; }
}
else {fprintf (stderr, "Erreur de format P2 P5\n"); fclose(source); return; }
/* le fichier est lisible correctement */
do { fgets(tmp_str, TMP_STR_SIZE, source); } while (tmp_str[0]=='#');
/* lecture du nombre colonnes et lignes*/
sscanf(tmp_str, "%d %d", &(width), &(height));
if ((width<4)||(height<4)) {
printf("Impossible image trop petite \n");
fclose(source);
return;
}
fgets(tmp_str, TMP_STR_SIZE, source);
sscanf(tmp_str, "%s", &(color_max));
if (color_max>255) {
printf("Impossible color_max > 255\n");
fclose(source);
return;
}
/* on regarde si assez espace mémoire */
tab_image_source = (unsigned char*) MEM_OK;
tab_image_final = (unsigned char*) (MEM_OK + (width*height));
printf("coucou\n");
/*if (((*(tab_image_final))+(width*height)+1)> MEM_MAX) {
printf("Impossible memoire insuffisante\n");
fclose(source);
return;
}*/
printf("**avant apres ****\n");
/* on remplit tab_image_source */
for (val=0; val < (width*height)-1; val++)
{
// printf("**coucou apres ****\n");
if (pgm_form==ASCII)
{
if (fgets(tmp_str, TMP_STR_SIZE, source) != NULL )
{
if (atoi(tmp_str) > 255) {
fprintf (stderr, "Erreur de format 255\n");
fclose(source);
return;
}
else
{
token = strtok(tmp_str, delimiteur);
// fprintf(stderr," strtok %s", token);
tab_image_source[val]= (*(token));
val=val+1;
while( token != NULL )
{
printf( " %s", token );
token = strtok(NULL, delimiteur);
//tab_image_source[val]= (*(token));
val=val+1;
//fprintf(stderr," strtok %s", strtok(tmp_str, delimiteur));
//tab_image_source[val]=atoi(strtok(NULL, delimiteur));
}
}
}
}
else
{
fread(&(tab_image_source[val]), sizeof(unsigned char), 1, source);
}
}
printf ("image écrite en mémoire \n");
}
void write_file (char* fichier_nouveau) {
unsigned long int val = 0;
FILE* nouveau = NULL;
/* on vérifie si pas problème de mémoire */
if (((*(tab_image_final))+(width*height)+1)> MEM_MAX) {
printf("Impossible memoire insuffisante\n");
return;
}
nouveau=fopen(fichier_nouveau,"w");
if (nouveau == NULL) {
printf("Impossible d'ouvrir le fichier nouveau %s \n",fichier_nouveau);
return;
}
if (pgm_form==BIN) {fputs("P5\n", nouveau); } else {fputs("P2\n", nouveau); }
fprintf(nouveau, "%i %i\n", width, height);
fprintf(nouveau, "%i\n", color_max);
/* entête écrite */
for (val = 0; val < (width*height)-1; val++) {
if (pgm_form == ASCII) {
fprintf(nouveau, "%i\n", tab_image_final[val]);
}
else {
fputc(tab_image_final[val], nouveau);
}
}
fclose(nouveau);
printf("fichier écrit \n");
}
int main ()
{
open_file ("image.pgm");
return 0x12;
} |
Partager