#include "traitementPGM.h" /** * @brief The read of PGM file in P5 format * * @param p The PGM struct where data is set and stored * @param filename The filename to read all data * @return int32_t Return -1 if error */ int32_t pgm_read_from_file(pgm *p, char *filename) { if (filename == NULL || p == NULL) { return -1; } char header[BUFSIZ]; int32_t c = 0, r = 0, intensity = 0; FILE *f = fopen(filename, "rb"); fscanf(f, "%s", header); fscanf(f, "%d %d", &r, &c); // r = Y number of line fscanf(f, "%d", &intensity); matrix_init(&p->pixels, c, r); p->max = intensity; for (uint32_t i = 0; i < p->pixels.col; i += 1) { for (uint32_t j = 0; j < p->pixels.row; j += 1) { fread(&p->pixels.data[i][j], sizeof(uint16_t), 1, f); } } fclose(f); return 0; } /** * @brief Write output file PGM version P2 beceause P5 not working but is best * * @param p The PGM pointer of struct to use * @param filename The filename of file to create and write inside */ void pgm_write_to_file(pgm *p, char *filename) { FILE *f = fopen(filename, "w"); fprintf(f, "P2\n"); fprintf(f, "%d %d\n", p->pixels.row, p->pixels.col); fprintf(f, "%d\n", p->max); for (uint32_t i = 0; i < p->pixels.col; i += 1) { for (uint32_t j = 0; j < p->pixels.row; j += 1) { fprintf(f, "%d ", p->pixels.data[i][j]); } fprintf(f, "\n"); } fclose(f); } /** * @brief Display the image with SDL * * @param img The image PGM to display * @param filname The name of the window or the filename displayed */ void PrintImagePGM(pgm img, char * filname) { printf("Image sélectionnée '%s' de taille %dx%d lumMax:%d\n", filname, img.pixels.col, img.pixels.row, img.max); struct gfx_context_t *ImagePGM = initWindow(img.pixels.row, img.pixels.col, filname); dessineTab2D(ImagePGM, img.pixels.data, img.pixels.col, img.pixels.row); gfx_present(ImagePGM); waitSpacePress(); gfx_destroy(ImagePGM); }