/*calcul de la checkSum -> least significant byte de la somme de tous les octets sauf les octets de SYNC */ void calculateCheckSum(struct trame* trameAEnv) { char* checkSum = (char*) malloc(sizeof (char[2])); char* lSB = (char*) malloc(sizeof (char[2])); int sum = 0, i; memset(lSB, '\0', 2); for (i = 0; i < 8; i = i + 2) { checkSum[0] = trameAEnv->DATA[i]; checkSum[1] = trameAEnv->DATA[i + 1]; sum += hexToInt(checkSum); } for (i = 0; i < 8; i = i + 2) { checkSum[0] = trameAEnv->ID[i]; checkSum[1] = trameAEnv->ID[i + 1]; sum += hexToInt(checkSum); } checkSum[0] = trameAEnv->HEADER[0]; checkSum[1] = trameAEnv->LENGHT[0]; sum += hexToInt(checkSum); for (i = 0; i < 2; i = i + 2) { checkSum[0] = trameAEnv->ORG[i]; checkSum[1] = trameAEnv->ORG[i + 1]; sum += hexToInt(checkSum); } for (i = 0; i < 2; i = i + 2) { checkSum[0] = trameAEnv->STATUS[i]; checkSum[1] = trameAEnv->STATUS[i + 1]; sum += hexToInt(checkSum); } itochar(sum, checkSum, 16); lSB[0] = checkSum[1]; lSB[1] = checkSum[0]; trameAEnv->CHECKSUM = lSB; free(checkSum); }
int writePgm(char *fileName, MyImage *image) { char parameters_str[5]; int i; const char *format = "P5"; if (image->flag == 0) { return -1; } FILE *fp = fopen(fileName, "w"); if (!fp) { printf("Unable to open file %s\n", fileName); return -1; } fputs(format, fp); fputc('\n', fp); itochar(image->width, parameters_str, 10); fputs(parameters_str, fp); parameters_str[0] = 0; fputc(' ', fp); itochar(image->height, parameters_str, 10); fputs(parameters_str, fp); parameters_str[0] = 0; fputc('\n', fp); itochar(image->maxgrey, parameters_str, 10); fputs(parameters_str, fp); fputc('\n', fp); for (i = 0; i < (image->width * image->height); i++) { fputc(image->data[i], fp); } fclose(fp); return 0; }